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
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 snd_pcm_gettime(runtime, &status->tstamp);
599 status->appl_ptr = runtime->control->appl_ptr;
600 status->hw_ptr = runtime->status->hw_ptr;
601 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
602 status->avail = snd_pcm_playback_avail(runtime);
603 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
604 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
605 status->delay = runtime->buffer_size - status->avail;
609 status->avail = snd_pcm_capture_avail(runtime);
610 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
611 status->delay = status->avail;
615 status->avail_max = runtime->avail_max;
616 status->overrange = runtime->overrange;
617 runtime->avail_max = 0;
618 runtime->overrange = 0;
620 snd_pcm_stream_unlock_irq(substream);
624 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
625 struct snd_pcm_status __user * _status)
627 struct snd_pcm_status status;
628 struct snd_pcm_runtime *runtime;
631 snd_assert(substream != NULL, return -ENXIO);
632 runtime = substream->runtime;
633 memset(&status, 0, sizeof(status));
634 res = snd_pcm_status(substream, &status);
637 if (copy_to_user(_status, &status, sizeof(status)))
642 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
643 struct snd_pcm_channel_info * info)
645 struct snd_pcm_runtime *runtime;
646 unsigned int channel;
648 snd_assert(substream != NULL, return -ENXIO);
649 channel = info->channel;
650 runtime = substream->runtime;
651 snd_pcm_stream_lock_irq(substream);
652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
653 snd_pcm_stream_unlock_irq(substream);
656 snd_pcm_stream_unlock_irq(substream);
657 if (channel >= runtime->channels)
659 memset(info, 0, sizeof(*info));
660 info->channel = channel;
661 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
664 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
665 struct snd_pcm_channel_info __user * _info)
667 struct snd_pcm_channel_info info;
670 if (copy_from_user(&info, _info, sizeof(info)))
672 res = snd_pcm_channel_info(substream, &info);
675 if (copy_to_user(_info, &info, sizeof(info)))
680 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
682 struct snd_pcm_runtime *runtime = substream->runtime;
683 if (runtime->trigger_master == NULL)
685 if (runtime->trigger_master == substream) {
686 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
688 snd_pcm_trigger_tstamp(runtime->trigger_master);
689 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
691 runtime->trigger_master = NULL;
695 int (*pre_action)(struct snd_pcm_substream *substream, int state);
696 int (*do_action)(struct snd_pcm_substream *substream, int state);
697 void (*undo_action)(struct snd_pcm_substream *substream, int state);
698 void (*post_action)(struct snd_pcm_substream *substream, int state);
702 * this functions is core for handling of linked stream
703 * Note: the stream state might be changed also on failure
704 * Note2: call with calling stream lock + link lock
706 static int snd_pcm_action_group(struct action_ops *ops,
707 struct snd_pcm_substream *substream,
708 int state, int do_lock)
710 struct snd_pcm_substream *s = NULL;
711 struct snd_pcm_substream *s1;
714 snd_pcm_group_for_each_entry(s, substream) {
715 if (do_lock && s != substream)
716 spin_lock_nested(&s->self_group.lock,
717 SINGLE_DEPTH_NESTING);
718 res = ops->pre_action(s, state);
722 snd_pcm_group_for_each_entry(s, substream) {
723 res = ops->do_action(s, state);
725 if (ops->undo_action) {
726 snd_pcm_group_for_each_entry(s1, substream) {
727 if (s1 == s) /* failed stream */
729 ops->undo_action(s1, state);
732 s = NULL; /* unlock all */
736 snd_pcm_group_for_each_entry(s, substream) {
737 ops->post_action(s, state);
742 snd_pcm_group_for_each_entry(s1, substream) {
744 spin_unlock(&s1->self_group.lock);
745 if (s1 == s) /* end */
753 * Note: call with stream lock
755 static int snd_pcm_action_single(struct action_ops *ops,
756 struct snd_pcm_substream *substream,
761 res = ops->pre_action(substream, state);
764 res = ops->do_action(substream, state);
766 ops->post_action(substream, state);
767 else if (ops->undo_action)
768 ops->undo_action(substream, state);
773 * Note: call with stream lock
775 static int snd_pcm_action(struct action_ops *ops,
776 struct snd_pcm_substream *substream,
781 if (snd_pcm_stream_linked(substream)) {
782 if (!spin_trylock(&substream->group->lock)) {
783 spin_unlock(&substream->self_group.lock);
784 spin_lock(&substream->group->lock);
785 spin_lock(&substream->self_group.lock);
787 res = snd_pcm_action_group(ops, substream, state, 1);
788 spin_unlock(&substream->group->lock);
790 res = snd_pcm_action_single(ops, substream, state);
796 * Note: don't use any locks before
798 static int snd_pcm_action_lock_irq(struct action_ops *ops,
799 struct snd_pcm_substream *substream,
804 read_lock_irq(&snd_pcm_link_rwlock);
805 if (snd_pcm_stream_linked(substream)) {
806 spin_lock(&substream->group->lock);
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_group(ops, substream, state, 1);
809 spin_unlock(&substream->self_group.lock);
810 spin_unlock(&substream->group->lock);
812 spin_lock(&substream->self_group.lock);
813 res = snd_pcm_action_single(ops, substream, state);
814 spin_unlock(&substream->self_group.lock);
816 read_unlock_irq(&snd_pcm_link_rwlock);
822 static int snd_pcm_action_nonatomic(struct action_ops *ops,
823 struct snd_pcm_substream *substream,
828 down_read(&snd_pcm_link_rwsem);
829 if (snd_pcm_stream_linked(substream))
830 res = snd_pcm_action_group(ops, substream, state, 0);
832 res = snd_pcm_action_single(ops, substream, state);
833 up_read(&snd_pcm_link_rwsem);
840 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
842 struct snd_pcm_runtime *runtime = substream->runtime;
843 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
846 !snd_pcm_playback_data(substream))
848 runtime->trigger_master = substream;
852 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
854 if (substream->runtime->trigger_master != substream)
856 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
859 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
861 if (substream->runtime->trigger_master == substream)
862 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
865 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
867 struct snd_pcm_runtime *runtime = substream->runtime;
868 snd_pcm_trigger_tstamp(substream);
869 runtime->status->state = state;
870 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
871 runtime->silence_size > 0)
872 snd_pcm_playback_silence(substream, ULONG_MAX);
873 if (runtime->sleep_min)
874 snd_pcm_tick_prepare(substream);
875 if (substream->timer)
876 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
877 &runtime->trigger_tstamp);
880 static struct action_ops snd_pcm_action_start = {
881 .pre_action = snd_pcm_pre_start,
882 .do_action = snd_pcm_do_start,
883 .undo_action = snd_pcm_undo_start,
884 .post_action = snd_pcm_post_start
889 * @substream: the PCM substream instance
891 * Start all linked streams.
893 int snd_pcm_start(struct snd_pcm_substream *substream)
895 return snd_pcm_action(&snd_pcm_action_start, substream,
896 SNDRV_PCM_STATE_RUNNING);
902 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
904 struct snd_pcm_runtime *runtime = substream->runtime;
905 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
907 runtime->trigger_master = substream;
911 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
913 if (substream->runtime->trigger_master == substream &&
914 snd_pcm_running(substream))
915 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
916 return 0; /* unconditonally stop all substreams */
919 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
921 struct snd_pcm_runtime *runtime = substream->runtime;
922 if (runtime->status->state != state) {
923 snd_pcm_trigger_tstamp(substream);
924 if (substream->timer)
925 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
926 &runtime->trigger_tstamp);
927 runtime->status->state = state;
928 snd_pcm_tick_set(substream, 0);
930 wake_up(&runtime->sleep);
933 static struct action_ops snd_pcm_action_stop = {
934 .pre_action = snd_pcm_pre_stop,
935 .do_action = snd_pcm_do_stop,
936 .post_action = snd_pcm_post_stop
941 * @substream: the PCM substream instance
942 * @state: PCM state after stopping the stream
944 * Try to stop all running streams in the substream group.
945 * The state of each stream is changed to the given value after that unconditionally.
947 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
949 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
952 EXPORT_SYMBOL(snd_pcm_stop);
956 * @substream: the PCM substream
958 * Stop the DMA only when the given stream is playback.
959 * The state is changed to SETUP.
960 * Unlike snd_pcm_stop(), this affects only the given stream.
962 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
964 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
965 SNDRV_PCM_STATE_SETUP);
971 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
973 struct snd_pcm_runtime *runtime = substream->runtime;
974 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
977 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
979 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
981 runtime->trigger_master = substream;
985 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
987 if (substream->runtime->trigger_master != substream)
989 return substream->ops->trigger(substream,
990 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
991 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
994 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
996 if (substream->runtime->trigger_master == substream)
997 substream->ops->trigger(substream,
998 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
999 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1002 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 snd_pcm_trigger_tstamp(substream);
1007 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1008 if (substream->timer)
1009 snd_timer_notify(substream->timer,
1010 SNDRV_TIMER_EVENT_MPAUSE,
1011 &runtime->trigger_tstamp);
1012 snd_pcm_tick_set(substream, 0);
1013 wake_up(&runtime->sleep);
1015 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1016 if (runtime->sleep_min)
1017 snd_pcm_tick_prepare(substream);
1018 if (substream->timer)
1019 snd_timer_notify(substream->timer,
1020 SNDRV_TIMER_EVENT_MCONTINUE,
1021 &runtime->trigger_tstamp);
1025 static struct action_ops snd_pcm_action_pause = {
1026 .pre_action = snd_pcm_pre_pause,
1027 .do_action = snd_pcm_do_pause,
1028 .undo_action = snd_pcm_undo_pause,
1029 .post_action = snd_pcm_post_pause
1033 * Push/release the pause for all linked streams.
1035 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1037 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1043 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1045 struct snd_pcm_runtime *runtime = substream->runtime;
1046 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1048 runtime->trigger_master = substream;
1052 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1054 struct snd_pcm_runtime *runtime = substream->runtime;
1055 if (runtime->trigger_master != substream)
1057 if (! snd_pcm_running(substream))
1059 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1060 return 0; /* suspend unconditionally */
1063 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1065 struct snd_pcm_runtime *runtime = substream->runtime;
1066 snd_pcm_trigger_tstamp(substream);
1067 if (substream->timer)
1068 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1069 &runtime->trigger_tstamp);
1070 runtime->status->suspended_state = runtime->status->state;
1071 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1072 snd_pcm_tick_set(substream, 0);
1073 wake_up(&runtime->sleep);
1076 static struct action_ops snd_pcm_action_suspend = {
1077 .pre_action = snd_pcm_pre_suspend,
1078 .do_action = snd_pcm_do_suspend,
1079 .post_action = snd_pcm_post_suspend
1084 * @substream: the PCM substream
1086 * Trigger SUSPEND to all linked streams.
1087 * After this call, all streams are changed to SUSPENDED state.
1089 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1092 unsigned long flags;
1097 snd_pcm_stream_lock_irqsave(substream, flags);
1098 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1099 snd_pcm_stream_unlock_irqrestore(substream, flags);
1103 EXPORT_SYMBOL(snd_pcm_suspend);
1106 * snd_pcm_suspend_all
1107 * @pcm: the PCM instance
1109 * Trigger SUSPEND to all substreams in the given pcm.
1110 * After this call, all streams are changed to SUSPENDED state.
1112 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1114 struct snd_pcm_substream *substream;
1115 int stream, err = 0;
1120 for (stream = 0; stream < 2; stream++) {
1121 for (substream = pcm->streams[stream].substream;
1122 substream; substream = substream->next) {
1123 /* FIXME: the open/close code should lock this as well */
1124 if (substream->runtime == NULL)
1126 err = snd_pcm_suspend(substream);
1127 if (err < 0 && err != -EBUSY)
1134 EXPORT_SYMBOL(snd_pcm_suspend_all);
1138 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1140 struct snd_pcm_runtime *runtime = substream->runtime;
1141 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1143 runtime->trigger_master = substream;
1147 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1149 struct snd_pcm_runtime *runtime = substream->runtime;
1150 if (runtime->trigger_master != substream)
1152 /* DMA not running previously? */
1153 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1154 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1155 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1157 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1160 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1162 if (substream->runtime->trigger_master == substream &&
1163 snd_pcm_running(substream))
1164 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1167 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1169 struct snd_pcm_runtime *runtime = substream->runtime;
1170 snd_pcm_trigger_tstamp(substream);
1171 if (substream->timer)
1172 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1173 &runtime->trigger_tstamp);
1174 runtime->status->state = runtime->status->suspended_state;
1175 if (runtime->sleep_min)
1176 snd_pcm_tick_prepare(substream);
1179 static struct action_ops snd_pcm_action_resume = {
1180 .pre_action = snd_pcm_pre_resume,
1181 .do_action = snd_pcm_do_resume,
1182 .undo_action = snd_pcm_undo_resume,
1183 .post_action = snd_pcm_post_resume
1186 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1188 struct snd_card *card = substream->pcm->card;
1191 snd_power_lock(card);
1192 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1193 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1194 snd_power_unlock(card);
1200 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1205 #endif /* CONFIG_PM */
1210 * Change the RUNNING stream(s) to XRUN state.
1212 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1214 struct snd_card *card = substream->pcm->card;
1215 struct snd_pcm_runtime *runtime = substream->runtime;
1218 snd_power_lock(card);
1219 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1220 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1225 snd_pcm_stream_lock_irq(substream);
1226 switch (runtime->status->state) {
1227 case SNDRV_PCM_STATE_XRUN:
1228 result = 0; /* already there */
1230 case SNDRV_PCM_STATE_RUNNING:
1231 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1236 snd_pcm_stream_unlock_irq(substream);
1238 snd_power_unlock(card);
1245 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1247 struct snd_pcm_runtime *runtime = substream->runtime;
1248 switch (runtime->status->state) {
1249 case SNDRV_PCM_STATE_RUNNING:
1250 case SNDRV_PCM_STATE_PREPARED:
1251 case SNDRV_PCM_STATE_PAUSED:
1252 case SNDRV_PCM_STATE_SUSPENDED:
1259 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1261 struct snd_pcm_runtime *runtime = substream->runtime;
1262 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1265 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1266 runtime->hw_ptr_base = 0;
1267 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1268 runtime->status->hw_ptr % runtime->period_size;
1269 runtime->silence_start = runtime->status->hw_ptr;
1270 runtime->silence_filled = 0;
1274 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1276 struct snd_pcm_runtime *runtime = substream->runtime;
1277 runtime->control->appl_ptr = runtime->status->hw_ptr;
1278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1279 runtime->silence_size > 0)
1280 snd_pcm_playback_silence(substream, ULONG_MAX);
1283 static struct action_ops snd_pcm_action_reset = {
1284 .pre_action = snd_pcm_pre_reset,
1285 .do_action = snd_pcm_do_reset,
1286 .post_action = snd_pcm_post_reset
1289 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1291 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1297 /* we use the second argument for updating f_flags */
1298 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1301 struct snd_pcm_runtime *runtime = substream->runtime;
1302 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1303 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1305 if (snd_pcm_running(substream))
1307 substream->f_flags = f_flags;
1311 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1314 err = substream->ops->prepare(substream);
1317 return snd_pcm_do_reset(substream, 0);
1320 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1322 struct snd_pcm_runtime *runtime = substream->runtime;
1323 runtime->control->appl_ptr = runtime->status->hw_ptr;
1324 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1327 static struct action_ops snd_pcm_action_prepare = {
1328 .pre_action = snd_pcm_pre_prepare,
1329 .do_action = snd_pcm_do_prepare,
1330 .post_action = snd_pcm_post_prepare
1335 * @substream: the PCM substream instance
1336 * @file: file to refer f_flags
1338 * Prepare the PCM substream to be triggerable.
1340 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1344 struct snd_card *card = substream->pcm->card;
1348 f_flags = file->f_flags;
1350 f_flags = substream->f_flags;
1352 snd_power_lock(card);
1353 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1354 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1355 substream, f_flags);
1356 snd_power_unlock(card);
1364 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1366 if (substream->f_flags & O_NONBLOCK)
1368 substream->runtime->trigger_master = substream;
1372 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1374 struct snd_pcm_runtime *runtime = substream->runtime;
1375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1376 switch (runtime->status->state) {
1377 case SNDRV_PCM_STATE_PREPARED:
1378 /* start playback stream if possible */
1379 if (! snd_pcm_playback_empty(substream)) {
1380 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1381 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1384 case SNDRV_PCM_STATE_RUNNING:
1385 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1391 /* stop running stream */
1392 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1393 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1394 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1395 snd_pcm_do_stop(substream, new_state);
1396 snd_pcm_post_stop(substream, new_state);
1402 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1406 static struct action_ops snd_pcm_action_drain_init = {
1407 .pre_action = snd_pcm_pre_drain_init,
1408 .do_action = snd_pcm_do_drain_init,
1409 .post_action = snd_pcm_post_drain_init
1413 struct snd_pcm_substream *substream;
1415 snd_pcm_uframes_t stop_threshold;
1418 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1421 * Drain the stream(s).
1422 * When the substream is linked, sync until the draining of all playback streams
1424 * After this call, all streams are supposed to be either SETUP or DRAINING
1425 * (capture only) state.
1427 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1429 struct snd_card *card;
1430 struct snd_pcm_runtime *runtime;
1431 struct snd_pcm_substream *s;
1434 struct drain_rec *drec, drec_tmp, *d;
1436 snd_assert(substream != NULL, return -ENXIO);
1437 card = substream->pcm->card;
1438 runtime = substream->runtime;
1440 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1443 snd_power_lock(card);
1444 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1445 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1447 snd_power_unlock(card);
1452 /* allocate temporary record for drain sync */
1453 down_read(&snd_pcm_link_rwsem);
1454 if (snd_pcm_stream_linked(substream)) {
1455 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1457 up_read(&snd_pcm_link_rwsem);
1458 snd_power_unlock(card);
1464 /* count only playback streams */
1466 snd_pcm_group_for_each_entry(s, substream) {
1467 runtime = s->runtime;
1468 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1469 d = &drec[num_drecs++];
1471 init_waitqueue_entry(&d->wait, current);
1472 add_wait_queue(&runtime->sleep, &d->wait);
1473 /* stop_threshold fixup to avoid endless loop when
1474 * stop_threshold > buffer_size
1476 d->stop_threshold = runtime->stop_threshold;
1477 if (runtime->stop_threshold > runtime->buffer_size)
1478 runtime->stop_threshold = runtime->buffer_size;
1481 up_read(&snd_pcm_link_rwsem);
1483 snd_pcm_stream_lock_irq(substream);
1485 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1486 snd_pcm_pause(substream, 0);
1488 /* pre-start/stop - all running streams are changed to DRAINING state */
1489 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1491 snd_pcm_stream_unlock_irq(substream);
1497 if (signal_pending(current)) {
1498 result = -ERESTARTSYS;
1502 for (i = 0; i < num_drecs; i++) {
1503 runtime = drec[i].substream->runtime;
1504 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1508 break; /* yes, all drained */
1510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_unlock_irq(substream);
1512 snd_power_unlock(card);
1513 tout = schedule_timeout(10 * HZ);
1514 snd_power_lock(card);
1515 snd_pcm_stream_lock_irq(substream);
1517 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1520 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1521 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1528 snd_pcm_stream_unlock_irq(substream);
1531 for (i = 0; i < num_drecs; i++) {
1533 runtime = d->substream->runtime;
1534 remove_wait_queue(&runtime->sleep, &d->wait);
1535 runtime->stop_threshold = d->stop_threshold;
1538 if (drec != &drec_tmp)
1540 snd_power_unlock(card);
1548 * Immediately put all linked substreams into SETUP state.
1550 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1552 struct snd_pcm_runtime *runtime;
1553 struct snd_card *card;
1556 snd_assert(substream != NULL, return -ENXIO);
1557 runtime = substream->runtime;
1558 card = substream->pcm->card;
1560 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1561 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1564 snd_power_lock(card);
1565 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1566 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1571 snd_pcm_stream_lock_irq(substream);
1573 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1574 snd_pcm_pause(substream, 0);
1576 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1577 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1578 snd_pcm_stream_unlock_irq(substream);
1580 snd_power_unlock(card);
1585 /* WARNING: Don't forget to fput back the file */
1586 static struct file *snd_pcm_file_fd(int fd)
1589 struct inode *inode;
1595 inode = file->f_path.dentry->d_inode;
1596 if (!S_ISCHR(inode->i_mode) ||
1597 imajor(inode) != snd_major) {
1601 minor = iminor(inode);
1602 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1603 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1613 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1617 struct snd_pcm_file *pcm_file;
1618 struct snd_pcm_substream *substream1;
1620 file = snd_pcm_file_fd(fd);
1623 pcm_file = file->private_data;
1624 substream1 = pcm_file->substream;
1625 down_write(&snd_pcm_link_rwsem);
1626 write_lock_irq(&snd_pcm_link_rwlock);
1627 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1628 substream->runtime->status->state != substream1->runtime->status->state) {
1632 if (snd_pcm_stream_linked(substream1)) {
1636 if (!snd_pcm_stream_linked(substream)) {
1637 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1638 if (substream->group == NULL) {
1642 spin_lock_init(&substream->group->lock);
1643 INIT_LIST_HEAD(&substream->group->substreams);
1644 list_add_tail(&substream->link_list, &substream->group->substreams);
1645 substream->group->count = 1;
1647 list_add_tail(&substream1->link_list, &substream->group->substreams);
1648 substream->group->count++;
1649 substream1->group = substream->group;
1651 write_unlock_irq(&snd_pcm_link_rwlock);
1652 up_write(&snd_pcm_link_rwsem);
1657 static void relink_to_local(struct snd_pcm_substream *substream)
1659 substream->group = &substream->self_group;
1660 INIT_LIST_HEAD(&substream->self_group.substreams);
1661 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1664 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1666 struct snd_pcm_substream *s;
1669 down_write(&snd_pcm_link_rwsem);
1670 write_lock_irq(&snd_pcm_link_rwlock);
1671 if (!snd_pcm_stream_linked(substream)) {
1675 list_del(&substream->link_list);
1676 substream->group->count--;
1677 if (substream->group->count == 1) { /* detach the last stream, too */
1678 snd_pcm_group_for_each_entry(s, substream) {
1682 kfree(substream->group);
1684 relink_to_local(substream);
1686 write_unlock_irq(&snd_pcm_link_rwlock);
1687 up_write(&snd_pcm_link_rwsem);
1694 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1695 struct snd_pcm_hw_rule *rule)
1697 struct snd_interval t;
1698 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1699 hw_param_interval_c(params, rule->deps[1]), &t);
1700 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1703 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1704 struct snd_pcm_hw_rule *rule)
1706 struct snd_interval t;
1707 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1708 hw_param_interval_c(params, rule->deps[1]), &t);
1709 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1712 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1713 struct snd_pcm_hw_rule *rule)
1715 struct snd_interval t;
1716 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1717 hw_param_interval_c(params, rule->deps[1]),
1718 (unsigned long) rule->private, &t);
1719 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1722 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1723 struct snd_pcm_hw_rule *rule)
1725 struct snd_interval t;
1726 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1727 (unsigned long) rule->private,
1728 hw_param_interval_c(params, rule->deps[1]), &t);
1729 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1732 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1733 struct snd_pcm_hw_rule *rule)
1736 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1738 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1740 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1742 if (! snd_mask_test(mask, k))
1744 bits = snd_pcm_format_physical_width(k);
1746 continue; /* ignore invalid formats */
1747 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1748 snd_mask_reset(&m, k);
1750 return snd_mask_refine(mask, &m);
1753 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
1756 struct snd_interval t;
1762 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1764 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1766 bits = snd_pcm_format_physical_width(k);
1768 continue; /* ignore invalid formats */
1769 if (t.min > (unsigned)bits)
1771 if (t.max < (unsigned)bits)
1775 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1778 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1779 #error "Change this table"
1782 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1783 48000, 64000, 88200, 96000, 176400, 192000 };
1785 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1786 .count = ARRAY_SIZE(rates),
1790 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1791 struct snd_pcm_hw_rule *rule)
1793 struct snd_pcm_hardware *hw = rule->private;
1794 return snd_interval_list(hw_param_interval(params, rule->var),
1795 snd_pcm_known_rates.count,
1796 snd_pcm_known_rates.list, hw->rates);
1799 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1800 struct snd_pcm_hw_rule *rule)
1802 struct snd_interval t;
1803 struct snd_pcm_substream *substream = rule->private;
1805 t.max = substream->buffer_bytes_max;
1809 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1812 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1814 struct snd_pcm_runtime *runtime = substream->runtime;
1815 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1818 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1819 snd_mask_any(constrs_mask(constrs, k));
1822 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1823 snd_interval_any(constrs_interval(constrs, k));
1826 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1827 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1828 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1829 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1833 snd_pcm_hw_rule_format, NULL,
1834 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1838 snd_pcm_hw_rule_sample_bits, NULL,
1839 SNDRV_PCM_HW_PARAM_FORMAT,
1840 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1844 snd_pcm_hw_rule_div, NULL,
1845 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1849 snd_pcm_hw_rule_mul, NULL,
1850 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1855 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1859 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1860 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1864 snd_pcm_hw_rule_div, NULL,
1865 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1869 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1879 snd_pcm_hw_rule_div, NULL,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1884 snd_pcm_hw_rule_div, NULL,
1885 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1889 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1893 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1894 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1895 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1898 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1899 snd_pcm_hw_rule_mul, NULL,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1903 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1904 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1908 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1909 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1910 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1913 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1914 snd_pcm_hw_rule_muldivk, (void*) 8,
1915 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1918 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1919 snd_pcm_hw_rule_muldivk, (void*) 8,
1920 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1923 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1924 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1925 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1928 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1929 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1930 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1936 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1938 struct snd_pcm_runtime *runtime = substream->runtime;
1939 struct snd_pcm_hardware *hw = &runtime->hw;
1941 unsigned int mask = 0;
1943 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1944 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1945 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1946 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1947 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1948 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1949 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1950 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1951 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1952 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1953 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1955 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1956 snd_assert(err >= 0, return -EINVAL);
1958 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1959 snd_assert(err >= 0, return -EINVAL);
1961 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1962 snd_assert(err >= 0, return -EINVAL);
1964 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1965 hw->channels_min, hw->channels_max);
1966 snd_assert(err >= 0, return -EINVAL);
1968 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1969 hw->rate_min, hw->rate_max);
1970 snd_assert(err >= 0, return -EINVAL);
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1973 hw->period_bytes_min, hw->period_bytes_max);
1974 snd_assert(err >= 0, return -EINVAL);
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1977 hw->periods_min, hw->periods_max);
1978 snd_assert(err >= 0, return -EINVAL);
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1981 hw->period_bytes_min, hw->buffer_bytes_max);
1982 snd_assert(err >= 0, return -EINVAL);
1984 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1985 snd_pcm_hw_rule_buffer_bytes_max, substream,
1986 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1991 if (runtime->dma_bytes) {
1992 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1993 snd_assert(err >= 0, return -EINVAL);
1996 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1997 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1998 snd_pcm_hw_rule_rate, hw,
1999 SNDRV_PCM_HW_PARAM_RATE, -1);
2004 /* FIXME: this belong to lowlevel */
2005 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2006 1000000 / HZ, 1000000 / HZ);
2007 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2012 static void pcm_release_private(struct snd_pcm_substream *substream)
2014 snd_pcm_unlink(substream);
2017 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2019 substream->ref_count--;
2020 if (substream->ref_count > 0)
2023 snd_pcm_drop(substream);
2024 if (substream->hw_opened) {
2025 if (substream->ops->hw_free != NULL)
2026 substream->ops->hw_free(substream);
2027 substream->ops->close(substream);
2028 substream->hw_opened = 0;
2030 if (substream->pcm_release) {
2031 substream->pcm_release(substream);
2032 substream->pcm_release = NULL;
2034 snd_pcm_detach_substream(substream);
2037 EXPORT_SYMBOL(snd_pcm_release_substream);
2039 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2041 struct snd_pcm_substream **rsubstream)
2043 struct snd_pcm_substream *substream;
2046 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2049 if (substream->ref_count > 1) {
2050 *rsubstream = substream;
2054 err = snd_pcm_hw_constraints_init(substream);
2056 snd_printd("snd_pcm_hw_constraints_init failed\n");
2060 if ((err = substream->ops->open(substream)) < 0)
2063 substream->hw_opened = 1;
2065 err = snd_pcm_hw_constraints_complete(substream);
2067 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2071 *rsubstream = substream;
2075 snd_pcm_release_substream(substream);
2079 EXPORT_SYMBOL(snd_pcm_open_substream);
2081 static int snd_pcm_open_file(struct file *file,
2082 struct snd_pcm *pcm,
2084 struct snd_pcm_file **rpcm_file)
2086 struct snd_pcm_file *pcm_file;
2087 struct snd_pcm_substream *substream;
2088 struct snd_pcm_str *str;
2091 snd_assert(rpcm_file != NULL, return -EINVAL);
2094 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2098 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2099 if (pcm_file == NULL) {
2100 snd_pcm_release_substream(substream);
2103 pcm_file->substream = substream;
2104 if (substream->ref_count == 1) {
2105 str = substream->pstr;
2106 substream->file = pcm_file;
2107 substream->pcm_release = pcm_release_private;
2109 file->private_data = pcm_file;
2110 *rpcm_file = pcm_file;
2114 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2116 struct snd_pcm *pcm;
2118 pcm = snd_lookup_minor_data(iminor(inode),
2119 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2120 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2123 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2125 struct snd_pcm *pcm;
2127 pcm = snd_lookup_minor_data(iminor(inode),
2128 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2129 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2132 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2135 struct snd_pcm_file *pcm_file;
2142 err = snd_card_file_add(pcm->card, file);
2145 if (!try_module_get(pcm->card->module)) {
2149 init_waitqueue_entry(&wait, current);
2150 add_wait_queue(&pcm->open_wait, &wait);
2151 mutex_lock(&pcm->open_mutex);
2153 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2156 if (err == -EAGAIN) {
2157 if (file->f_flags & O_NONBLOCK) {
2163 set_current_state(TASK_INTERRUPTIBLE);
2164 mutex_unlock(&pcm->open_mutex);
2166 mutex_lock(&pcm->open_mutex);
2167 if (signal_pending(current)) {
2172 remove_wait_queue(&pcm->open_wait, &wait);
2173 mutex_unlock(&pcm->open_mutex);
2179 module_put(pcm->card->module);
2181 snd_card_file_remove(pcm->card, file);
2186 static int snd_pcm_release(struct inode *inode, struct file *file)
2188 struct snd_pcm *pcm;
2189 struct snd_pcm_substream *substream;
2190 struct snd_pcm_file *pcm_file;
2192 pcm_file = file->private_data;
2193 substream = pcm_file->substream;
2194 snd_assert(substream != NULL, return -ENXIO);
2195 pcm = substream->pcm;
2196 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2197 mutex_lock(&pcm->open_mutex);
2198 snd_pcm_release_substream(substream);
2200 mutex_unlock(&pcm->open_mutex);
2201 wake_up(&pcm->open_wait);
2202 module_put(pcm->card->module);
2203 snd_card_file_remove(pcm->card, file);
2207 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2208 snd_pcm_uframes_t frames)
2210 struct snd_pcm_runtime *runtime = substream->runtime;
2211 snd_pcm_sframes_t appl_ptr;
2212 snd_pcm_sframes_t ret;
2213 snd_pcm_sframes_t hw_avail;
2218 snd_pcm_stream_lock_irq(substream);
2219 switch (runtime->status->state) {
2220 case SNDRV_PCM_STATE_PREPARED:
2222 case SNDRV_PCM_STATE_DRAINING:
2223 case SNDRV_PCM_STATE_RUNNING:
2224 if (snd_pcm_update_hw_ptr(substream) >= 0)
2227 case SNDRV_PCM_STATE_XRUN:
2235 hw_avail = snd_pcm_playback_hw_avail(runtime);
2236 if (hw_avail <= 0) {
2240 if (frames > (snd_pcm_uframes_t)hw_avail)
2243 frames -= frames % runtime->xfer_align;
2244 appl_ptr = runtime->control->appl_ptr - frames;
2246 appl_ptr += runtime->boundary;
2247 runtime->control->appl_ptr = appl_ptr;
2248 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2250 snd_pcm_tick_prepare(substream);
2253 snd_pcm_stream_unlock_irq(substream);
2257 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2258 snd_pcm_uframes_t frames)
2260 struct snd_pcm_runtime *runtime = substream->runtime;
2261 snd_pcm_sframes_t appl_ptr;
2262 snd_pcm_sframes_t ret;
2263 snd_pcm_sframes_t hw_avail;
2268 snd_pcm_stream_lock_irq(substream);
2269 switch (runtime->status->state) {
2270 case SNDRV_PCM_STATE_PREPARED:
2271 case SNDRV_PCM_STATE_DRAINING:
2273 case SNDRV_PCM_STATE_RUNNING:
2274 if (snd_pcm_update_hw_ptr(substream) >= 0)
2277 case SNDRV_PCM_STATE_XRUN:
2285 hw_avail = snd_pcm_capture_hw_avail(runtime);
2286 if (hw_avail <= 0) {
2290 if (frames > (snd_pcm_uframes_t)hw_avail)
2293 frames -= frames % runtime->xfer_align;
2294 appl_ptr = runtime->control->appl_ptr - frames;
2296 appl_ptr += runtime->boundary;
2297 runtime->control->appl_ptr = appl_ptr;
2298 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2300 snd_pcm_tick_prepare(substream);
2303 snd_pcm_stream_unlock_irq(substream);
2307 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2308 snd_pcm_uframes_t frames)
2310 struct snd_pcm_runtime *runtime = substream->runtime;
2311 snd_pcm_sframes_t appl_ptr;
2312 snd_pcm_sframes_t ret;
2313 snd_pcm_sframes_t avail;
2318 snd_pcm_stream_lock_irq(substream);
2319 switch (runtime->status->state) {
2320 case SNDRV_PCM_STATE_PREPARED:
2321 case SNDRV_PCM_STATE_PAUSED:
2323 case SNDRV_PCM_STATE_DRAINING:
2324 case SNDRV_PCM_STATE_RUNNING:
2325 if (snd_pcm_update_hw_ptr(substream) >= 0)
2328 case SNDRV_PCM_STATE_XRUN:
2336 avail = snd_pcm_playback_avail(runtime);
2341 if (frames > (snd_pcm_uframes_t)avail)
2344 frames -= frames % runtime->xfer_align;
2345 appl_ptr = runtime->control->appl_ptr + frames;
2346 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2347 appl_ptr -= runtime->boundary;
2348 runtime->control->appl_ptr = appl_ptr;
2349 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2351 snd_pcm_tick_prepare(substream);
2354 snd_pcm_stream_unlock_irq(substream);
2358 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2359 snd_pcm_uframes_t frames)
2361 struct snd_pcm_runtime *runtime = substream->runtime;
2362 snd_pcm_sframes_t appl_ptr;
2363 snd_pcm_sframes_t ret;
2364 snd_pcm_sframes_t avail;
2369 snd_pcm_stream_lock_irq(substream);
2370 switch (runtime->status->state) {
2371 case SNDRV_PCM_STATE_PREPARED:
2372 case SNDRV_PCM_STATE_DRAINING:
2373 case SNDRV_PCM_STATE_PAUSED:
2375 case SNDRV_PCM_STATE_RUNNING:
2376 if (snd_pcm_update_hw_ptr(substream) >= 0)
2379 case SNDRV_PCM_STATE_XRUN:
2387 avail = snd_pcm_capture_avail(runtime);
2392 if (frames > (snd_pcm_uframes_t)avail)
2395 frames -= frames % runtime->xfer_align;
2396 appl_ptr = runtime->control->appl_ptr + frames;
2397 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2398 appl_ptr -= runtime->boundary;
2399 runtime->control->appl_ptr = appl_ptr;
2400 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2402 snd_pcm_tick_prepare(substream);
2405 snd_pcm_stream_unlock_irq(substream);
2409 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2411 struct snd_pcm_runtime *runtime = substream->runtime;
2414 snd_pcm_stream_lock_irq(substream);
2415 switch (runtime->status->state) {
2416 case SNDRV_PCM_STATE_DRAINING:
2417 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2419 case SNDRV_PCM_STATE_RUNNING:
2420 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2423 case SNDRV_PCM_STATE_PREPARED:
2424 case SNDRV_PCM_STATE_SUSPENDED:
2427 case SNDRV_PCM_STATE_XRUN:
2435 snd_pcm_stream_unlock_irq(substream);
2439 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2440 snd_pcm_sframes_t __user *res)
2442 struct snd_pcm_runtime *runtime = substream->runtime;
2444 snd_pcm_sframes_t n = 0;
2446 snd_pcm_stream_lock_irq(substream);
2447 switch (runtime->status->state) {
2448 case SNDRV_PCM_STATE_DRAINING:
2449 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2451 case SNDRV_PCM_STATE_RUNNING:
2452 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2455 case SNDRV_PCM_STATE_PREPARED:
2456 case SNDRV_PCM_STATE_SUSPENDED:
2458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2459 n = snd_pcm_playback_hw_avail(runtime);
2461 n = snd_pcm_capture_avail(runtime);
2463 case SNDRV_PCM_STATE_XRUN:
2471 snd_pcm_stream_unlock_irq(substream);
2473 if (put_user(n, res))
2478 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2479 struct snd_pcm_sync_ptr __user *_sync_ptr)
2481 struct snd_pcm_runtime *runtime = substream->runtime;
2482 struct snd_pcm_sync_ptr sync_ptr;
2483 volatile struct snd_pcm_mmap_status *status;
2484 volatile struct snd_pcm_mmap_control *control;
2487 memset(&sync_ptr, 0, sizeof(sync_ptr));
2488 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2490 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2492 status = runtime->status;
2493 control = runtime->control;
2494 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2495 err = snd_pcm_hwsync(substream);
2499 snd_pcm_stream_lock_irq(substream);
2500 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2501 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2503 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2504 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2505 control->avail_min = sync_ptr.c.control.avail_min;
2507 sync_ptr.c.control.avail_min = control->avail_min;
2508 sync_ptr.s.status.state = status->state;
2509 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2510 sync_ptr.s.status.tstamp = status->tstamp;
2511 sync_ptr.s.status.suspended_state = status->suspended_state;
2512 snd_pcm_stream_unlock_irq(substream);
2513 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2518 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2520 struct snd_pcm_runtime *runtime = substream->runtime;
2523 if (get_user(arg, _arg))
2525 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2527 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2528 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2529 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2533 static int snd_pcm_common_ioctl1(struct file *file,
2534 struct snd_pcm_substream *substream,
2535 unsigned int cmd, void __user *arg)
2537 snd_assert(substream != NULL, return -ENXIO);
2540 case SNDRV_PCM_IOCTL_PVERSION:
2541 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2542 case SNDRV_PCM_IOCTL_INFO:
2543 return snd_pcm_info_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2546 case SNDRV_PCM_IOCTL_TTSTAMP:
2547 return snd_pcm_tstamp(substream, arg);
2548 case SNDRV_PCM_IOCTL_HW_REFINE:
2549 return snd_pcm_hw_refine_user(substream, arg);
2550 case SNDRV_PCM_IOCTL_HW_PARAMS:
2551 return snd_pcm_hw_params_user(substream, arg);
2552 case SNDRV_PCM_IOCTL_HW_FREE:
2553 return snd_pcm_hw_free(substream);
2554 case SNDRV_PCM_IOCTL_SW_PARAMS:
2555 return snd_pcm_sw_params_user(substream, arg);
2556 case SNDRV_PCM_IOCTL_STATUS:
2557 return snd_pcm_status_user(substream, arg);
2558 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2559 return snd_pcm_channel_info_user(substream, arg);
2560 case SNDRV_PCM_IOCTL_PREPARE:
2561 return snd_pcm_prepare(substream, file);
2562 case SNDRV_PCM_IOCTL_RESET:
2563 return snd_pcm_reset(substream);
2564 case SNDRV_PCM_IOCTL_START:
2565 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2566 case SNDRV_PCM_IOCTL_LINK:
2567 return snd_pcm_link(substream, (int)(unsigned long) arg);
2568 case SNDRV_PCM_IOCTL_UNLINK:
2569 return snd_pcm_unlink(substream);
2570 case SNDRV_PCM_IOCTL_RESUME:
2571 return snd_pcm_resume(substream);
2572 case SNDRV_PCM_IOCTL_XRUN:
2573 return snd_pcm_xrun(substream);
2574 case SNDRV_PCM_IOCTL_HWSYNC:
2575 return snd_pcm_hwsync(substream);
2576 case SNDRV_PCM_IOCTL_DELAY:
2577 return snd_pcm_delay(substream, arg);
2578 case SNDRV_PCM_IOCTL_SYNC_PTR:
2579 return snd_pcm_sync_ptr(substream, arg);
2580 #ifdef CONFIG_SND_SUPPORT_OLD_API
2581 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2582 return snd_pcm_hw_refine_old_user(substream, arg);
2583 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2584 return snd_pcm_hw_params_old_user(substream, arg);
2586 case SNDRV_PCM_IOCTL_DRAIN:
2587 return snd_pcm_drain(substream);
2588 case SNDRV_PCM_IOCTL_DROP:
2589 return snd_pcm_drop(substream);
2590 case SNDRV_PCM_IOCTL_PAUSE:
2593 snd_pcm_stream_lock_irq(substream);
2594 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2595 snd_pcm_stream_unlock_irq(substream);
2599 snd_printd("unknown ioctl = 0x%x\n", cmd);
2603 static int snd_pcm_playback_ioctl1(struct file *file,
2604 struct snd_pcm_substream *substream,
2605 unsigned int cmd, void __user *arg)
2607 snd_assert(substream != NULL, return -ENXIO);
2608 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2610 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2612 struct snd_xferi xferi;
2613 struct snd_xferi __user *_xferi = arg;
2614 struct snd_pcm_runtime *runtime = substream->runtime;
2615 snd_pcm_sframes_t result;
2616 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2618 if (put_user(0, &_xferi->result))
2620 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2622 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2623 __put_user(result, &_xferi->result);
2624 return result < 0 ? result : 0;
2626 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2628 struct snd_xfern xfern;
2629 struct snd_xfern __user *_xfern = arg;
2630 struct snd_pcm_runtime *runtime = substream->runtime;
2632 snd_pcm_sframes_t result;
2633 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2635 if (runtime->channels > 128)
2637 if (put_user(0, &_xfern->result))
2639 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2641 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2644 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2648 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2650 __put_user(result, &_xfern->result);
2651 return result < 0 ? result : 0;
2653 case SNDRV_PCM_IOCTL_REWIND:
2655 snd_pcm_uframes_t frames;
2656 snd_pcm_uframes_t __user *_frames = arg;
2657 snd_pcm_sframes_t result;
2658 if (get_user(frames, _frames))
2660 if (put_user(0, _frames))
2662 result = snd_pcm_playback_rewind(substream, frames);
2663 __put_user(result, _frames);
2664 return result < 0 ? result : 0;
2666 case SNDRV_PCM_IOCTL_FORWARD:
2668 snd_pcm_uframes_t frames;
2669 snd_pcm_uframes_t __user *_frames = arg;
2670 snd_pcm_sframes_t result;
2671 if (get_user(frames, _frames))
2673 if (put_user(0, _frames))
2675 result = snd_pcm_playback_forward(substream, frames);
2676 __put_user(result, _frames);
2677 return result < 0 ? result : 0;
2680 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2683 static int snd_pcm_capture_ioctl1(struct file *file,
2684 struct snd_pcm_substream *substream,
2685 unsigned int cmd, void __user *arg)
2687 snd_assert(substream != NULL, return -ENXIO);
2688 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2690 case SNDRV_PCM_IOCTL_READI_FRAMES:
2692 struct snd_xferi xferi;
2693 struct snd_xferi __user *_xferi = arg;
2694 struct snd_pcm_runtime *runtime = substream->runtime;
2695 snd_pcm_sframes_t result;
2696 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2698 if (put_user(0, &_xferi->result))
2700 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2702 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2703 __put_user(result, &_xferi->result);
2704 return result < 0 ? result : 0;
2706 case SNDRV_PCM_IOCTL_READN_FRAMES:
2708 struct snd_xfern xfern;
2709 struct snd_xfern __user *_xfern = arg;
2710 struct snd_pcm_runtime *runtime = substream->runtime;
2712 snd_pcm_sframes_t result;
2713 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2715 if (runtime->channels > 128)
2717 if (put_user(0, &_xfern->result))
2719 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2721 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2724 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2728 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2730 __put_user(result, &_xfern->result);
2731 return result < 0 ? result : 0;
2733 case SNDRV_PCM_IOCTL_REWIND:
2735 snd_pcm_uframes_t frames;
2736 snd_pcm_uframes_t __user *_frames = arg;
2737 snd_pcm_sframes_t result;
2738 if (get_user(frames, _frames))
2740 if (put_user(0, _frames))
2742 result = snd_pcm_capture_rewind(substream, frames);
2743 __put_user(result, _frames);
2744 return result < 0 ? result : 0;
2746 case SNDRV_PCM_IOCTL_FORWARD:
2748 snd_pcm_uframes_t frames;
2749 snd_pcm_uframes_t __user *_frames = arg;
2750 snd_pcm_sframes_t result;
2751 if (get_user(frames, _frames))
2753 if (put_user(0, _frames))
2755 result = snd_pcm_capture_forward(substream, frames);
2756 __put_user(result, _frames);
2757 return result < 0 ? result : 0;
2760 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2763 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2766 struct snd_pcm_file *pcm_file;
2768 pcm_file = file->private_data;
2770 if (((cmd >> 8) & 0xff) != 'A')
2773 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2774 (void __user *)arg);
2777 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2780 struct snd_pcm_file *pcm_file;
2782 pcm_file = file->private_data;
2784 if (((cmd >> 8) & 0xff) != 'A')
2787 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2788 (void __user *)arg);
2791 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2792 unsigned int cmd, void *arg)
2797 fs = snd_enter_user();
2798 switch (substream->stream) {
2799 case SNDRV_PCM_STREAM_PLAYBACK:
2800 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2801 (void __user *)arg);
2803 case SNDRV_PCM_STREAM_CAPTURE:
2804 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2805 (void __user *)arg);
2815 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2817 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2820 struct snd_pcm_file *pcm_file;
2821 struct snd_pcm_substream *substream;
2822 struct snd_pcm_runtime *runtime;
2823 snd_pcm_sframes_t result;
2825 pcm_file = file->private_data;
2826 substream = pcm_file->substream;
2827 snd_assert(substream != NULL, return -ENXIO);
2828 runtime = substream->runtime;
2829 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2831 if (!frame_aligned(runtime, count))
2833 count = bytes_to_frames(runtime, count);
2834 result = snd_pcm_lib_read(substream, buf, count);
2836 result = frames_to_bytes(runtime, result);
2840 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2841 size_t count, loff_t * offset)
2843 struct snd_pcm_file *pcm_file;
2844 struct snd_pcm_substream *substream;
2845 struct snd_pcm_runtime *runtime;
2846 snd_pcm_sframes_t result;
2848 pcm_file = file->private_data;
2849 substream = pcm_file->substream;
2850 snd_assert(substream != NULL, result = -ENXIO; goto end);
2851 runtime = substream->runtime;
2852 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2856 if (!frame_aligned(runtime, count)) {
2860 count = bytes_to_frames(runtime, count);
2861 result = snd_pcm_lib_write(substream, buf, count);
2863 result = frames_to_bytes(runtime, result);
2868 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2869 unsigned long nr_segs, loff_t pos)
2872 struct snd_pcm_file *pcm_file;
2873 struct snd_pcm_substream *substream;
2874 struct snd_pcm_runtime *runtime;
2875 snd_pcm_sframes_t result;
2878 snd_pcm_uframes_t frames;
2880 pcm_file = iocb->ki_filp->private_data;
2881 substream = pcm_file->substream;
2882 snd_assert(substream != NULL, return -ENXIO);
2883 runtime = substream->runtime;
2884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 if (nr_segs > 1024 || nr_segs != runtime->channels)
2888 if (!frame_aligned(runtime, iov->iov_len))
2890 frames = bytes_to_samples(runtime, iov->iov_len);
2891 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2894 for (i = 0; i < nr_segs; ++i)
2895 bufs[i] = iov[i].iov_base;
2896 result = snd_pcm_lib_readv(substream, bufs, frames);
2898 result = frames_to_bytes(runtime, result);
2903 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2904 unsigned long nr_segs, loff_t pos)
2906 struct snd_pcm_file *pcm_file;
2907 struct snd_pcm_substream *substream;
2908 struct snd_pcm_runtime *runtime;
2909 snd_pcm_sframes_t result;
2912 snd_pcm_uframes_t frames;
2914 pcm_file = iocb->ki_filp->private_data;
2915 substream = pcm_file->substream;
2916 snd_assert(substream != NULL, result = -ENXIO; goto end);
2917 runtime = substream->runtime;
2918 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2922 if (nr_segs > 128 || nr_segs != runtime->channels ||
2923 !frame_aligned(runtime, iov->iov_len)) {
2927 frames = bytes_to_samples(runtime, iov->iov_len);
2928 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2931 for (i = 0; i < nr_segs; ++i)
2932 bufs[i] = iov[i].iov_base;
2933 result = snd_pcm_lib_writev(substream, bufs, frames);
2935 result = frames_to_bytes(runtime, result);
2941 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2943 struct snd_pcm_file *pcm_file;
2944 struct snd_pcm_substream *substream;
2945 struct snd_pcm_runtime *runtime;
2947 snd_pcm_uframes_t avail;
2949 pcm_file = file->private_data;
2951 substream = pcm_file->substream;
2952 snd_assert(substream != NULL, return -ENXIO);
2953 runtime = substream->runtime;
2955 poll_wait(file, &runtime->sleep, wait);
2957 snd_pcm_stream_lock_irq(substream);
2958 avail = snd_pcm_playback_avail(runtime);
2959 switch (runtime->status->state) {
2960 case SNDRV_PCM_STATE_RUNNING:
2961 case SNDRV_PCM_STATE_PREPARED:
2962 case SNDRV_PCM_STATE_PAUSED:
2963 if (avail >= runtime->control->avail_min) {
2964 mask = POLLOUT | POLLWRNORM;
2968 case SNDRV_PCM_STATE_DRAINING:
2972 mask = POLLOUT | POLLWRNORM | POLLERR;
2975 snd_pcm_stream_unlock_irq(substream);
2979 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2981 struct snd_pcm_file *pcm_file;
2982 struct snd_pcm_substream *substream;
2983 struct snd_pcm_runtime *runtime;
2985 snd_pcm_uframes_t avail;
2987 pcm_file = file->private_data;
2989 substream = pcm_file->substream;
2990 snd_assert(substream != NULL, return -ENXIO);
2991 runtime = substream->runtime;
2993 poll_wait(file, &runtime->sleep, wait);
2995 snd_pcm_stream_lock_irq(substream);
2996 avail = snd_pcm_capture_avail(runtime);
2997 switch (runtime->status->state) {
2998 case SNDRV_PCM_STATE_RUNNING:
2999 case SNDRV_PCM_STATE_PREPARED:
3000 case SNDRV_PCM_STATE_PAUSED:
3001 if (avail >= runtime->control->avail_min) {
3002 mask = POLLIN | POLLRDNORM;
3007 case SNDRV_PCM_STATE_DRAINING:
3009 mask = POLLIN | POLLRDNORM;
3014 mask = POLLIN | POLLRDNORM | POLLERR;
3017 snd_pcm_stream_unlock_irq(substream);
3026 * Only on coherent architectures, we can mmap the status and the control records
3027 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3029 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3031 * mmap status record
3033 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3034 struct vm_fault *vmf)
3036 struct snd_pcm_substream *substream = area->vm_private_data;
3037 struct snd_pcm_runtime *runtime;
3039 if (substream == NULL)
3040 return VM_FAULT_SIGBUS;
3041 runtime = substream->runtime;
3042 vmf->page = virt_to_page(runtime->status);
3043 get_page(vmf->page);
3047 static struct vm_operations_struct snd_pcm_vm_ops_status =
3049 .fault = snd_pcm_mmap_status_fault,
3052 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3053 struct vm_area_struct *area)
3055 struct snd_pcm_runtime *runtime;
3057 if (!(area->vm_flags & VM_READ))
3059 runtime = substream->runtime;
3060 snd_assert(runtime != NULL, return -EAGAIN);
3061 size = area->vm_end - area->vm_start;
3062 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3064 area->vm_ops = &snd_pcm_vm_ops_status;
3065 area->vm_private_data = substream;
3066 area->vm_flags |= VM_RESERVED;
3071 * mmap control record
3073 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3074 struct vm_fault *vmf)
3076 struct snd_pcm_substream *substream = area->vm_private_data;
3077 struct snd_pcm_runtime *runtime;
3079 if (substream == NULL)
3080 return VM_FAULT_SIGBUS;
3081 runtime = substream->runtime;
3082 vmf->page = virt_to_page(runtime->control);
3083 get_page(vmf->page);
3087 static struct vm_operations_struct snd_pcm_vm_ops_control =
3089 .fault = snd_pcm_mmap_control_fault,
3092 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3093 struct vm_area_struct *area)
3095 struct snd_pcm_runtime *runtime;
3097 if (!(area->vm_flags & VM_READ))
3099 runtime = substream->runtime;
3100 snd_assert(runtime != NULL, return -EAGAIN);
3101 size = area->vm_end - area->vm_start;
3102 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3104 area->vm_ops = &snd_pcm_vm_ops_control;
3105 area->vm_private_data = substream;
3106 area->vm_flags |= VM_RESERVED;
3109 #else /* ! coherent mmap */
3111 * don't support mmap for status and control records.
3113 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3114 struct vm_area_struct *area)
3118 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3119 struct vm_area_struct *area)
3123 #endif /* coherent mmap */
3126 * fault callback for mmapping a RAM page
3128 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3129 struct vm_fault *vmf)
3131 struct snd_pcm_substream *substream = area->vm_private_data;
3132 struct snd_pcm_runtime *runtime;
3133 unsigned long offset;
3138 if (substream == NULL)
3139 return VM_FAULT_SIGBUS;
3140 runtime = substream->runtime;
3141 offset = vmf->pgoff << PAGE_SHIFT;
3142 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3143 if (offset > dma_bytes - PAGE_SIZE)
3144 return VM_FAULT_SIGBUS;
3145 if (substream->ops->page) {
3146 page = substream->ops->page(substream, offset);
3148 return VM_FAULT_SIGBUS;
3150 vaddr = runtime->dma_area + offset;
3151 page = virt_to_page(vaddr);
3158 static struct vm_operations_struct snd_pcm_vm_ops_data =
3160 .open = snd_pcm_mmap_data_open,
3161 .close = snd_pcm_mmap_data_close,
3162 .fault = snd_pcm_mmap_data_fault,
3166 * mmap the DMA buffer on RAM
3168 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3169 struct vm_area_struct *area)
3171 area->vm_ops = &snd_pcm_vm_ops_data;
3172 area->vm_private_data = substream;
3173 area->vm_flags |= VM_RESERVED;
3174 atomic_inc(&substream->mmap_count);
3179 * mmap the DMA buffer on I/O memory area
3181 #if SNDRV_PCM_INFO_MMAP_IOMEM
3182 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3184 .open = snd_pcm_mmap_data_open,
3185 .close = snd_pcm_mmap_data_close,
3188 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3189 struct vm_area_struct *area)
3192 unsigned long offset;
3194 #ifdef pgprot_noncached
3195 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3197 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3198 area->vm_private_data = substream;
3199 area->vm_flags |= VM_IO;
3200 size = area->vm_end - area->vm_start;
3201 offset = area->vm_pgoff << PAGE_SHIFT;
3202 if (io_remap_pfn_range(area, area->vm_start,
3203 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3204 size, area->vm_page_prot))
3206 atomic_inc(&substream->mmap_count);
3210 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3211 #endif /* SNDRV_PCM_INFO_MMAP */
3216 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3217 struct vm_area_struct *area)
3219 struct snd_pcm_runtime *runtime;
3221 unsigned long offset;
3224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3225 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3228 if (!(area->vm_flags & VM_READ))
3231 runtime = substream->runtime;
3232 snd_assert(runtime != NULL, return -EAGAIN);
3233 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3235 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3237 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3238 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3240 size = area->vm_end - area->vm_start;
3241 offset = area->vm_pgoff << PAGE_SHIFT;
3242 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3243 if ((size_t)size > dma_bytes)
3245 if (offset > dma_bytes - size)
3248 if (substream->ops->mmap)
3249 return substream->ops->mmap(substream, area);
3251 return snd_pcm_default_mmap(substream, area);
3254 EXPORT_SYMBOL(snd_pcm_mmap_data);
3256 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3258 struct snd_pcm_file * pcm_file;
3259 struct snd_pcm_substream *substream;
3260 unsigned long offset;
3262 pcm_file = file->private_data;
3263 substream = pcm_file->substream;
3264 snd_assert(substream != NULL, return -ENXIO);
3266 offset = area->vm_pgoff << PAGE_SHIFT;
3268 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3269 if (pcm_file->no_compat_mmap)
3271 return snd_pcm_mmap_status(substream, file, area);
3272 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3273 if (pcm_file->no_compat_mmap)
3275 return snd_pcm_mmap_control(substream, file, area);
3277 return snd_pcm_mmap_data(substream, file, area);
3282 static int snd_pcm_fasync(int fd, struct file * file, int on)
3284 struct snd_pcm_file * pcm_file;
3285 struct snd_pcm_substream *substream;
3286 struct snd_pcm_runtime *runtime;
3289 pcm_file = file->private_data;
3290 substream = pcm_file->substream;
3291 snd_assert(substream != NULL, return -ENXIO);
3292 runtime = substream->runtime;
3294 err = fasync_helper(fd, file, on, &runtime->fasync);
3303 #ifdef CONFIG_COMPAT
3304 #include "pcm_compat.c"
3306 #define snd_pcm_ioctl_compat NULL
3310 * To be removed helpers to keep binary compatibility
3313 #ifdef CONFIG_SND_SUPPORT_OLD_API
3314 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3315 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3317 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3318 struct snd_pcm_hw_params_old *oparams)
3322 memset(params, 0, sizeof(*params));
3323 params->flags = oparams->flags;
3324 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3325 params->masks[i].bits[0] = oparams->masks[i];
3326 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3327 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3328 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3329 params->info = oparams->info;
3330 params->msbits = oparams->msbits;
3331 params->rate_num = oparams->rate_num;
3332 params->rate_den = oparams->rate_den;
3333 params->fifo_size = oparams->fifo_size;
3336 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3337 struct snd_pcm_hw_params *params)
3341 memset(oparams, 0, sizeof(*oparams));
3342 oparams->flags = params->flags;
3343 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3344 oparams->masks[i] = params->masks[i].bits[0];
3345 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3346 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3347 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3348 oparams->info = params->info;
3349 oparams->msbits = params->msbits;
3350 oparams->rate_num = params->rate_num;
3351 oparams->rate_den = params->rate_den;
3352 oparams->fifo_size = params->fifo_size;
3355 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
3358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
3367 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3373 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3377 snd_pcm_hw_convert_from_old_params(params, oparams);
3378 err = snd_pcm_hw_refine(substream, params);
3379 snd_pcm_hw_convert_to_old_params(oparams, params);
3380 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3390 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3391 struct snd_pcm_hw_params_old __user * _oparams)
3393 struct snd_pcm_hw_params *params;
3394 struct snd_pcm_hw_params_old *oparams = NULL;
3397 params = kmalloc(sizeof(*params), GFP_KERNEL);
3402 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3407 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3411 snd_pcm_hw_convert_from_old_params(params, oparams);
3412 err = snd_pcm_hw_params(substream, params);
3413 snd_pcm_hw_convert_to_old_params(oparams, params);
3414 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3423 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3429 const struct file_operations snd_pcm_f_ops[2] = {
3431 .owner = THIS_MODULE,
3432 .write = snd_pcm_write,
3433 .aio_write = snd_pcm_aio_write,
3434 .open = snd_pcm_playback_open,
3435 .release = snd_pcm_release,
3436 .poll = snd_pcm_playback_poll,
3437 .unlocked_ioctl = snd_pcm_playback_ioctl,
3438 .compat_ioctl = snd_pcm_ioctl_compat,
3439 .mmap = snd_pcm_mmap,
3440 .fasync = snd_pcm_fasync,
3443 .owner = THIS_MODULE,
3444 .read = snd_pcm_read,
3445 .aio_read = snd_pcm_aio_read,
3446 .open = snd_pcm_capture_open,
3447 .release = snd_pcm_release,
3448 .poll = snd_pcm_capture_poll,
3449 .unlocked_ioctl = snd_pcm_capture_ioctl,
3450 .compat_ioctl = snd_pcm_ioctl_compat,
3451 .mmap = snd_pcm_mmap,
3452 .fasync = snd_pcm_fasync,