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/smp_lock.h>
25 #include <linux/file.h>
26 #include <linux/slab.h>
27 #include <linux/time.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 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
59 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
61 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params_old __user * _oparams);
63 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
70 DEFINE_RWLOCK(snd_pcm_link_rwlock);
71 static DECLARE_RWSEM(snd_pcm_link_rwsem);
74 static inline mm_segment_t snd_enter_user(void)
76 mm_segment_t fs = get_fs();
81 static inline void snd_leave_user(mm_segment_t fs)
88 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
90 struct snd_pcm_runtime *runtime;
91 struct snd_pcm *pcm = substream->pcm;
92 struct snd_pcm_str *pstr = substream->pstr;
94 snd_assert(substream != NULL, return -ENXIO);
95 memset(info, 0, sizeof(*info));
96 info->card = pcm->card->number;
97 info->device = pcm->device;
98 info->stream = substream->stream;
99 info->subdevice = substream->number;
100 strlcpy(info->id, pcm->id, sizeof(info->id));
101 strlcpy(info->name, pcm->name, sizeof(info->name));
102 info->dev_class = pcm->dev_class;
103 info->dev_subclass = pcm->dev_subclass;
104 info->subdevices_count = pstr->substream_count;
105 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
106 strlcpy(info->subname, substream->name, sizeof(info->subname));
107 runtime = substream->runtime;
108 /* AB: FIXME!!! This is definitely nonsense */
110 info->sync = runtime->sync;
111 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
116 int snd_pcm_info_user(struct snd_pcm_substream *substream,
117 struct snd_pcm_info __user * _info)
119 struct snd_pcm_info *info;
122 info = kmalloc(sizeof(*info), GFP_KERNEL);
125 err = snd_pcm_info(substream, info);
127 if (copy_to_user(_info, info, sizeof(*info)))
137 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
138 char *snd_pcm_hw_param_names[] = {
142 HW_PARAM(SAMPLE_BITS),
143 HW_PARAM(FRAME_BITS),
146 HW_PARAM(PERIOD_TIME),
147 HW_PARAM(PERIOD_SIZE),
148 HW_PARAM(PERIOD_BYTES),
150 HW_PARAM(BUFFER_TIME),
151 HW_PARAM(BUFFER_SIZE),
152 HW_PARAM(BUFFER_BYTES),
157 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
158 struct snd_pcm_hw_params *params)
161 struct snd_pcm_hardware *hw;
162 struct snd_interval *i = NULL;
163 struct snd_mask *m = NULL;
164 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
165 unsigned int rstamps[constrs->rules_num];
166 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
167 unsigned int stamp = 2;
171 params->fifo_size = 0;
172 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
174 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
175 params->rate_num = 0;
176 params->rate_den = 0;
179 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
180 m = hw_param_mask(params, k);
181 if (snd_mask_empty(m))
183 if (!(params->rmask & (1 << k)))
186 printk("%s = ", snd_pcm_hw_param_names[k]);
187 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
189 changed = snd_mask_refine(m, constrs_mask(constrs, k));
191 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
194 params->cmask |= 1 << k;
199 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
200 i = hw_param_interval(params, k);
201 if (snd_interval_empty(i))
203 if (!(params->rmask & (1 << k)))
206 printk("%s = ", snd_pcm_hw_param_names[k]);
211 i->openmin ? '(' : '[', i->min,
212 i->max, i->openmax ? ')' : ']');
215 changed = snd_interval_refine(i, constrs_interval(constrs, k));
220 printk("%c%u %u%c\n",
221 i->openmin ? '(' : '[', i->min,
222 i->max, i->openmax ? ')' : ']');
225 params->cmask |= 1 << k;
230 for (k = 0; k < constrs->rules_num; k++)
232 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
233 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
236 for (k = 0; k < constrs->rules_num; k++) {
237 struct snd_pcm_hw_rule *r = &constrs->rules[k];
240 if (r->cond && !(r->cond & params->flags))
242 for (d = 0; r->deps[d] >= 0; d++) {
243 if (vstamps[r->deps[d]] > rstamps[k]) {
251 printk("Rule %d [%p]: ", k, r->func);
253 printk("%s = ", snd_pcm_hw_param_names[r->var]);
254 if (hw_is_mask(r->var)) {
255 m = hw_param_mask(params, r->var);
256 printk("%x", *m->bits);
258 i = hw_param_interval(params, r->var);
263 i->openmin ? '(' : '[', i->min,
264 i->max, i->openmax ? ')' : ']');
268 changed = r->func(params, r);
272 if (hw_is_mask(r->var))
273 printk("%x", *m->bits);
279 i->openmin ? '(' : '[', i->min,
280 i->max, i->openmax ? ')' : ']');
286 if (changed && r->var >= 0) {
287 params->cmask |= (1 << r->var);
288 vstamps[r->var] = stamp;
296 if (!params->msbits) {
297 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
298 if (snd_interval_single(i))
299 params->msbits = snd_interval_value(i);
302 if (!params->rate_den) {
303 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
304 if (snd_interval_single(i)) {
305 params->rate_num = snd_interval_value(i);
306 params->rate_den = 1;
310 hw = &substream->runtime->hw;
312 params->info = hw->info;
313 if (!params->fifo_size)
314 params->fifo_size = hw->fifo_size;
319 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params __user * _params)
322 struct snd_pcm_hw_params *params;
325 params = kmalloc(sizeof(*params), GFP_KERNEL);
330 if (copy_from_user(params, _params, sizeof(*params))) {
334 err = snd_pcm_hw_refine(substream, params);
335 if (copy_to_user(_params, params, sizeof(*params))) {
344 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
345 struct snd_pcm_hw_params *params)
347 struct snd_pcm_runtime *runtime;
350 snd_pcm_uframes_t frames;
352 snd_assert(substream != NULL, return -ENXIO);
353 runtime = substream->runtime;
354 snd_assert(runtime != NULL, return -ENXIO);
355 snd_pcm_stream_lock_irq(substream);
356 switch (runtime->status->state) {
357 case SNDRV_PCM_STATE_OPEN:
358 case SNDRV_PCM_STATE_SETUP:
359 case SNDRV_PCM_STATE_PREPARED:
362 snd_pcm_stream_unlock_irq(substream);
365 snd_pcm_stream_unlock_irq(substream);
366 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
367 if (!substream->oss.oss)
369 if (atomic_read(&runtime->mmap_count))
373 err = snd_pcm_hw_refine(substream, params);
377 err = snd_pcm_hw_params_choose(substream, params);
381 if (substream->ops->hw_params != NULL) {
382 err = substream->ops->hw_params(substream, params);
387 runtime->access = params_access(params);
388 runtime->format = params_format(params);
389 runtime->subformat = params_subformat(params);
390 runtime->channels = params_channels(params);
391 runtime->rate = params_rate(params);
392 runtime->period_size = params_period_size(params);
393 runtime->periods = params_periods(params);
394 runtime->buffer_size = params_buffer_size(params);
395 runtime->tick_time = params_tick_time(params);
396 runtime->info = params->info;
397 runtime->rate_num = params->rate_num;
398 runtime->rate_den = params->rate_den;
400 bits = snd_pcm_format_physical_width(runtime->format);
401 runtime->sample_bits = bits;
402 bits *= runtime->channels;
403 runtime->frame_bits = bits;
405 while (bits % 8 != 0) {
409 runtime->byte_align = bits / 8;
410 runtime->min_align = frames;
412 /* Default sw params */
413 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
414 runtime->period_step = 1;
415 runtime->sleep_min = 0;
416 runtime->control->avail_min = runtime->period_size;
417 runtime->xfer_align = runtime->period_size;
418 runtime->start_threshold = 1;
419 runtime->stop_threshold = runtime->buffer_size;
420 runtime->silence_threshold = 0;
421 runtime->silence_size = 0;
422 runtime->boundary = runtime->buffer_size;
423 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
424 runtime->boundary *= 2;
426 snd_pcm_timer_resolution_change(substream);
427 runtime->status->state = SNDRV_PCM_STATE_SETUP;
430 /* hardware might be unuseable from this time,
431 so we force application to retry to set
432 the correct hardware parameter settings */
433 runtime->status->state = SNDRV_PCM_STATE_OPEN;
434 if (substream->ops->hw_free != NULL)
435 substream->ops->hw_free(substream);
439 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
440 struct snd_pcm_hw_params __user * _params)
442 struct snd_pcm_hw_params *params;
445 params = kmalloc(sizeof(*params), GFP_KERNEL);
450 if (copy_from_user(params, _params, sizeof(*params))) {
454 err = snd_pcm_hw_params(substream, params);
455 if (copy_to_user(_params, params, sizeof(*params))) {
464 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
466 struct snd_pcm_runtime *runtime;
469 snd_assert(substream != NULL, return -ENXIO);
470 runtime = substream->runtime;
471 snd_assert(runtime != NULL, return -ENXIO);
472 snd_pcm_stream_lock_irq(substream);
473 switch (runtime->status->state) {
474 case SNDRV_PCM_STATE_SETUP:
475 case SNDRV_PCM_STATE_PREPARED:
478 snd_pcm_stream_unlock_irq(substream);
481 snd_pcm_stream_unlock_irq(substream);
482 if (atomic_read(&runtime->mmap_count))
484 if (substream->ops->hw_free)
485 result = substream->ops->hw_free(substream);
486 runtime->status->state = SNDRV_PCM_STATE_OPEN;
490 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
491 struct snd_pcm_sw_params *params)
493 struct snd_pcm_runtime *runtime;
495 snd_assert(substream != NULL, return -ENXIO);
496 runtime = substream->runtime;
497 snd_assert(runtime != NULL, return -ENXIO);
498 snd_pcm_stream_lock_irq(substream);
499 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
500 snd_pcm_stream_unlock_irq(substream);
503 snd_pcm_stream_unlock_irq(substream);
505 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
507 if (params->avail_min == 0)
509 if (params->xfer_align == 0 ||
510 params->xfer_align % runtime->min_align != 0)
512 if (params->silence_size >= runtime->boundary) {
513 if (params->silence_threshold != 0)
516 if (params->silence_size > params->silence_threshold)
518 if (params->silence_threshold > runtime->buffer_size)
521 snd_pcm_stream_lock_irq(substream);
522 runtime->tstamp_mode = params->tstamp_mode;
523 runtime->sleep_min = params->sleep_min;
524 runtime->period_step = params->period_step;
525 runtime->control->avail_min = params->avail_min;
526 runtime->start_threshold = params->start_threshold;
527 runtime->stop_threshold = params->stop_threshold;
528 runtime->silence_threshold = params->silence_threshold;
529 runtime->silence_size = params->silence_size;
530 runtime->xfer_align = params->xfer_align;
531 params->boundary = runtime->boundary;
532 if (snd_pcm_running(substream)) {
533 if (runtime->sleep_min)
534 snd_pcm_tick_prepare(substream);
536 snd_pcm_tick_set(substream, 0);
537 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
538 runtime->silence_size > 0)
539 snd_pcm_playback_silence(substream, ULONG_MAX);
540 wake_up(&runtime->sleep);
542 snd_pcm_stream_unlock_irq(substream);
546 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
547 struct snd_pcm_sw_params __user * _params)
549 struct snd_pcm_sw_params params;
551 if (copy_from_user(¶ms, _params, sizeof(params)))
553 err = snd_pcm_sw_params(substream, ¶ms);
554 if (copy_to_user(_params, ¶ms, sizeof(params)))
559 int snd_pcm_status(struct snd_pcm_substream *substream,
560 struct snd_pcm_status *status)
562 struct snd_pcm_runtime *runtime = substream->runtime;
564 snd_pcm_stream_lock_irq(substream);
565 status->state = runtime->status->state;
566 status->suspended_state = runtime->status->suspended_state;
567 if (status->state == SNDRV_PCM_STATE_OPEN)
569 status->trigger_tstamp = runtime->trigger_tstamp;
570 if (snd_pcm_running(substream)) {
571 snd_pcm_update_hw_ptr(substream);
572 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
573 status->tstamp = runtime->status->tstamp;
575 getnstimeofday(&status->tstamp);
577 getnstimeofday(&status->tstamp);
578 status->appl_ptr = runtime->control->appl_ptr;
579 status->hw_ptr = runtime->status->hw_ptr;
580 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
581 status->avail = snd_pcm_playback_avail(runtime);
582 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
583 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
584 status->delay = runtime->buffer_size - status->avail;
588 status->avail = snd_pcm_capture_avail(runtime);
589 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
590 status->delay = status->avail;
594 status->avail_max = runtime->avail_max;
595 status->overrange = runtime->overrange;
596 runtime->avail_max = 0;
597 runtime->overrange = 0;
599 snd_pcm_stream_unlock_irq(substream);
603 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
604 struct snd_pcm_status __user * _status)
606 struct snd_pcm_status status;
607 struct snd_pcm_runtime *runtime;
610 snd_assert(substream != NULL, return -ENXIO);
611 runtime = substream->runtime;
612 memset(&status, 0, sizeof(status));
613 res = snd_pcm_status(substream, &status);
616 if (copy_to_user(_status, &status, sizeof(status)))
621 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
622 struct snd_pcm_channel_info * info)
624 struct snd_pcm_runtime *runtime;
625 unsigned int channel;
627 snd_assert(substream != NULL, return -ENXIO);
628 channel = info->channel;
629 runtime = substream->runtime;
630 snd_pcm_stream_lock_irq(substream);
631 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
632 snd_pcm_stream_unlock_irq(substream);
635 snd_pcm_stream_unlock_irq(substream);
636 if (channel >= runtime->channels)
638 memset(info, 0, sizeof(*info));
639 info->channel = channel;
640 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
643 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
644 struct snd_pcm_channel_info __user * _info)
646 struct snd_pcm_channel_info info;
649 if (copy_from_user(&info, _info, sizeof(info)))
651 res = snd_pcm_channel_info(substream, &info);
654 if (copy_to_user(_info, &info, sizeof(info)))
659 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
661 struct snd_pcm_runtime *runtime = substream->runtime;
662 if (runtime->trigger_master == NULL)
664 if (runtime->trigger_master == substream) {
665 getnstimeofday(&runtime->trigger_tstamp);
667 snd_pcm_trigger_tstamp(runtime->trigger_master);
668 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
670 runtime->trigger_master = NULL;
674 int (*pre_action)(struct snd_pcm_substream *substream, int state);
675 int (*do_action)(struct snd_pcm_substream *substream, int state);
676 void (*undo_action)(struct snd_pcm_substream *substream, int state);
677 void (*post_action)(struct snd_pcm_substream *substream, int state);
681 * this functions is core for handling of linked stream
682 * Note: the stream state might be changed also on failure
683 * Note2: call with calling stream lock + link lock
685 static int snd_pcm_action_group(struct action_ops *ops,
686 struct snd_pcm_substream *substream,
687 int state, int do_lock)
689 struct list_head *pos;
690 struct snd_pcm_substream *s = NULL;
691 struct snd_pcm_substream *s1;
694 snd_pcm_group_for_each(pos, substream) {
695 s = snd_pcm_group_substream_entry(pos);
696 if (do_lock && s != substream)
697 spin_lock(&s->self_group.lock);
698 res = ops->pre_action(s, state);
702 snd_pcm_group_for_each(pos, substream) {
703 s = snd_pcm_group_substream_entry(pos);
704 res = ops->do_action(s, state);
706 if (ops->undo_action) {
707 snd_pcm_group_for_each(pos, substream) {
708 s1 = snd_pcm_group_substream_entry(pos);
709 if (s1 == s) /* failed stream */
711 ops->undo_action(s1, state);
714 s = NULL; /* unlock all */
718 snd_pcm_group_for_each(pos, substream) {
719 s = snd_pcm_group_substream_entry(pos);
720 ops->post_action(s, state);
725 snd_pcm_group_for_each(pos, substream) {
726 s1 = snd_pcm_group_substream_entry(pos);
728 spin_unlock(&s1->self_group.lock);
729 if (s1 == s) /* end */
737 * Note: call with stream lock
739 static int snd_pcm_action_single(struct action_ops *ops,
740 struct snd_pcm_substream *substream,
745 res = ops->pre_action(substream, state);
748 res = ops->do_action(substream, state);
750 ops->post_action(substream, state);
751 else if (ops->undo_action)
752 ops->undo_action(substream, state);
757 * Note: call with stream lock
759 static int snd_pcm_action(struct action_ops *ops,
760 struct snd_pcm_substream *substream,
765 if (snd_pcm_stream_linked(substream)) {
766 if (!spin_trylock(&substream->group->lock)) {
767 spin_unlock(&substream->self_group.lock);
768 spin_lock(&substream->group->lock);
769 spin_lock(&substream->self_group.lock);
771 res = snd_pcm_action_group(ops, substream, state, 1);
772 spin_unlock(&substream->group->lock);
774 res = snd_pcm_action_single(ops, substream, state);
780 * Note: don't use any locks before
782 static int snd_pcm_action_lock_irq(struct action_ops *ops,
783 struct snd_pcm_substream *substream,
788 read_lock_irq(&snd_pcm_link_rwlock);
789 if (snd_pcm_stream_linked(substream)) {
790 spin_lock(&substream->group->lock);
791 spin_lock(&substream->self_group.lock);
792 res = snd_pcm_action_group(ops, substream, state, 1);
793 spin_unlock(&substream->self_group.lock);
794 spin_unlock(&substream->group->lock);
796 spin_lock(&substream->self_group.lock);
797 res = snd_pcm_action_single(ops, substream, state);
798 spin_unlock(&substream->self_group.lock);
800 read_unlock_irq(&snd_pcm_link_rwlock);
806 static int snd_pcm_action_nonatomic(struct action_ops *ops,
807 struct snd_pcm_substream *substream,
812 down_read(&snd_pcm_link_rwsem);
813 if (snd_pcm_stream_linked(substream))
814 res = snd_pcm_action_group(ops, substream, state, 0);
816 res = snd_pcm_action_single(ops, substream, state);
817 up_read(&snd_pcm_link_rwsem);
824 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
826 struct snd_pcm_runtime *runtime = substream->runtime;
827 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
830 !snd_pcm_playback_data(substream))
832 runtime->trigger_master = substream;
836 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
838 if (substream->runtime->trigger_master != substream)
840 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
843 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
845 if (substream->runtime->trigger_master == substream)
846 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
849 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
851 struct snd_pcm_runtime *runtime = substream->runtime;
852 snd_pcm_trigger_tstamp(substream);
853 runtime->status->state = state;
854 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
855 runtime->silence_size > 0)
856 snd_pcm_playback_silence(substream, ULONG_MAX);
857 if (runtime->sleep_min)
858 snd_pcm_tick_prepare(substream);
859 if (substream->timer)
860 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
861 &runtime->trigger_tstamp);
864 static struct action_ops snd_pcm_action_start = {
865 .pre_action = snd_pcm_pre_start,
866 .do_action = snd_pcm_do_start,
867 .undo_action = snd_pcm_undo_start,
868 .post_action = snd_pcm_post_start
873 * @substream: the PCM substream instance
875 * Start all linked streams.
877 int snd_pcm_start(struct snd_pcm_substream *substream)
879 return snd_pcm_action(&snd_pcm_action_start, substream,
880 SNDRV_PCM_STATE_RUNNING);
886 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
891 runtime->trigger_master = substream;
895 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
897 if (substream->runtime->trigger_master == substream &&
898 snd_pcm_running(substream))
899 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
900 return 0; /* unconditonally stop all substreams */
903 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
905 struct snd_pcm_runtime *runtime = substream->runtime;
906 if (runtime->status->state != state) {
907 snd_pcm_trigger_tstamp(substream);
908 if (substream->timer)
909 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
910 &runtime->trigger_tstamp);
911 runtime->status->state = state;
912 snd_pcm_tick_set(substream, 0);
914 wake_up(&runtime->sleep);
917 static struct action_ops snd_pcm_action_stop = {
918 .pre_action = snd_pcm_pre_stop,
919 .do_action = snd_pcm_do_stop,
920 .post_action = snd_pcm_post_stop
925 * @substream: the PCM substream instance
926 * @state: PCM state after stopping the stream
928 * Try to stop all running streams in the substream group.
929 * The state of each stream is changed to the given value after that unconditionally.
931 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
933 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
938 * @substream: the PCM substream
940 * Stop the DMA only when the given stream is playback.
941 * The state is changed to SETUP.
942 * Unlike snd_pcm_stop(), this affects only the given stream.
944 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
946 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
947 SNDRV_PCM_STATE_SETUP);
953 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
955 struct snd_pcm_runtime *runtime = substream->runtime;
956 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
959 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
961 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
963 runtime->trigger_master = substream;
967 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
969 if (substream->runtime->trigger_master != substream)
971 return substream->ops->trigger(substream,
972 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
973 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
976 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
978 if (substream->runtime->trigger_master == substream)
979 substream->ops->trigger(substream,
980 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
981 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
984 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
986 struct snd_pcm_runtime *runtime = substream->runtime;
987 snd_pcm_trigger_tstamp(substream);
989 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
990 if (substream->timer)
991 snd_timer_notify(substream->timer,
992 SNDRV_TIMER_EVENT_MPAUSE,
993 &runtime->trigger_tstamp);
994 snd_pcm_tick_set(substream, 0);
995 wake_up(&runtime->sleep);
997 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
998 if (runtime->sleep_min)
999 snd_pcm_tick_prepare(substream);
1000 if (substream->timer)
1001 snd_timer_notify(substream->timer,
1002 SNDRV_TIMER_EVENT_MCONTINUE,
1003 &runtime->trigger_tstamp);
1007 static struct action_ops snd_pcm_action_pause = {
1008 .pre_action = snd_pcm_pre_pause,
1009 .do_action = snd_pcm_do_pause,
1010 .undo_action = snd_pcm_undo_pause,
1011 .post_action = snd_pcm_post_pause
1015 * Push/release the pause for all linked streams.
1017 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1019 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1025 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1027 struct snd_pcm_runtime *runtime = substream->runtime;
1028 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1030 runtime->trigger_master = substream;
1034 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1036 struct snd_pcm_runtime *runtime = substream->runtime;
1037 if (runtime->trigger_master != substream)
1039 if (! snd_pcm_running(substream))
1041 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1042 return 0; /* suspend unconditionally */
1045 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1047 struct snd_pcm_runtime *runtime = substream->runtime;
1048 snd_pcm_trigger_tstamp(substream);
1049 if (substream->timer)
1050 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1051 &runtime->trigger_tstamp);
1052 runtime->status->suspended_state = runtime->status->state;
1053 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1054 snd_pcm_tick_set(substream, 0);
1055 wake_up(&runtime->sleep);
1058 static struct action_ops snd_pcm_action_suspend = {
1059 .pre_action = snd_pcm_pre_suspend,
1060 .do_action = snd_pcm_do_suspend,
1061 .post_action = snd_pcm_post_suspend
1066 * @substream: the PCM substream
1068 * Trigger SUSPEND to all linked streams.
1069 * After this call, all streams are changed to SUSPENDED state.
1071 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1074 unsigned long flags;
1079 snd_pcm_stream_lock_irqsave(substream, flags);
1080 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1081 snd_pcm_stream_unlock_irqrestore(substream, flags);
1086 * snd_pcm_suspend_all
1087 * @pcm: the PCM instance
1089 * Trigger SUSPEND to all substreams in the given pcm.
1090 * After this call, all streams are changed to SUSPENDED state.
1092 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1094 struct snd_pcm_substream *substream;
1095 int stream, err = 0;
1100 for (stream = 0; stream < 2; stream++) {
1101 for (substream = pcm->streams[stream].substream;
1102 substream; substream = substream->next) {
1103 /* FIXME: the open/close code should lock this as well */
1104 if (substream->runtime == NULL)
1106 err = snd_pcm_suspend(substream);
1107 if (err < 0 && err != -EBUSY)
1116 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1121 runtime->trigger_master = substream;
1125 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1127 struct snd_pcm_runtime *runtime = substream->runtime;
1128 if (runtime->trigger_master != substream)
1130 /* DMA not running previously? */
1131 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1132 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1133 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1135 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1138 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1140 if (substream->runtime->trigger_master == substream &&
1141 snd_pcm_running(substream))
1142 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1145 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1147 struct snd_pcm_runtime *runtime = substream->runtime;
1148 snd_pcm_trigger_tstamp(substream);
1149 if (substream->timer)
1150 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1151 &runtime->trigger_tstamp);
1152 runtime->status->state = runtime->status->suspended_state;
1153 if (runtime->sleep_min)
1154 snd_pcm_tick_prepare(substream);
1157 static struct action_ops snd_pcm_action_resume = {
1158 .pre_action = snd_pcm_pre_resume,
1159 .do_action = snd_pcm_do_resume,
1160 .undo_action = snd_pcm_undo_resume,
1161 .post_action = snd_pcm_post_resume
1164 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1166 struct snd_card *card = substream->pcm->card;
1169 snd_power_lock(card);
1170 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1171 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1172 snd_power_unlock(card);
1178 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1183 #endif /* CONFIG_PM */
1188 * Change the RUNNING stream(s) to XRUN state.
1190 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1192 struct snd_card *card = substream->pcm->card;
1193 struct snd_pcm_runtime *runtime = substream->runtime;
1196 snd_power_lock(card);
1197 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1198 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1203 snd_pcm_stream_lock_irq(substream);
1204 switch (runtime->status->state) {
1205 case SNDRV_PCM_STATE_XRUN:
1206 result = 0; /* already there */
1208 case SNDRV_PCM_STATE_RUNNING:
1209 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1214 snd_pcm_stream_unlock_irq(substream);
1216 snd_power_unlock(card);
1223 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1225 struct snd_pcm_runtime *runtime = substream->runtime;
1226 switch (runtime->status->state) {
1227 case SNDRV_PCM_STATE_RUNNING:
1228 case SNDRV_PCM_STATE_PREPARED:
1229 case SNDRV_PCM_STATE_PAUSED:
1230 case SNDRV_PCM_STATE_SUSPENDED:
1237 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1239 struct snd_pcm_runtime *runtime = substream->runtime;
1240 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1243 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1244 runtime->hw_ptr_base = 0;
1245 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1246 runtime->status->hw_ptr % runtime->period_size;
1247 runtime->silence_start = runtime->status->hw_ptr;
1248 runtime->silence_filled = 0;
1252 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1254 struct snd_pcm_runtime *runtime = substream->runtime;
1255 runtime->control->appl_ptr = runtime->status->hw_ptr;
1256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1257 runtime->silence_size > 0)
1258 snd_pcm_playback_silence(substream, ULONG_MAX);
1261 static struct action_ops snd_pcm_action_reset = {
1262 .pre_action = snd_pcm_pre_reset,
1263 .do_action = snd_pcm_do_reset,
1264 .post_action = snd_pcm_post_reset
1267 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1269 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1275 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
1277 struct snd_pcm_runtime *runtime = substream->runtime;
1278 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1280 if (snd_pcm_running(substream))
1285 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1288 err = substream->ops->prepare(substream);
1291 return snd_pcm_do_reset(substream, 0);
1294 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1296 struct snd_pcm_runtime *runtime = substream->runtime;
1297 runtime->control->appl_ptr = runtime->status->hw_ptr;
1298 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1301 static struct action_ops snd_pcm_action_prepare = {
1302 .pre_action = snd_pcm_pre_prepare,
1303 .do_action = snd_pcm_do_prepare,
1304 .post_action = snd_pcm_post_prepare
1309 * @substream: the PCM substream instance
1311 * Prepare the PCM substream to be triggerable.
1313 int snd_pcm_prepare(struct snd_pcm_substream *substream)
1316 struct snd_card *card = substream->pcm->card;
1318 snd_power_lock(card);
1319 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1320 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1321 snd_power_unlock(card);
1329 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1331 if (substream->ffile->f_flags & O_NONBLOCK)
1333 substream->runtime->trigger_master = substream;
1337 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1339 struct snd_pcm_runtime *runtime = substream->runtime;
1340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1341 switch (runtime->status->state) {
1342 case SNDRV_PCM_STATE_PREPARED:
1343 /* start playback stream if possible */
1344 if (! snd_pcm_playback_empty(substream)) {
1345 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1346 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1349 case SNDRV_PCM_STATE_RUNNING:
1350 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1356 /* stop running stream */
1357 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1358 int state = snd_pcm_capture_avail(runtime) > 0 ?
1359 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1360 snd_pcm_do_stop(substream, state);
1361 snd_pcm_post_stop(substream, state);
1367 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1371 static struct action_ops snd_pcm_action_drain_init = {
1372 .pre_action = snd_pcm_pre_drain_init,
1373 .do_action = snd_pcm_do_drain_init,
1374 .post_action = snd_pcm_post_drain_init
1378 struct snd_pcm_substream *substream;
1380 snd_pcm_uframes_t stop_threshold;
1383 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1386 * Drain the stream(s).
1387 * When the substream is linked, sync until the draining of all playback streams
1389 * After this call, all streams are supposed to be either SETUP or DRAINING
1390 * (capture only) state.
1392 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1394 struct snd_card *card;
1395 struct snd_pcm_runtime *runtime;
1396 struct list_head *pos;
1399 struct drain_rec *drec, drec_tmp, *d;
1401 snd_assert(substream != NULL, return -ENXIO);
1402 card = substream->pcm->card;
1403 runtime = substream->runtime;
1405 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1408 snd_power_lock(card);
1409 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1410 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1412 snd_power_unlock(card);
1417 /* allocate temporary record for drain sync */
1418 down_read(&snd_pcm_link_rwsem);
1419 if (snd_pcm_stream_linked(substream)) {
1420 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1422 up_read(&snd_pcm_link_rwsem);
1423 snd_power_unlock(card);
1429 /* count only playback streams */
1431 snd_pcm_group_for_each(pos, substream) {
1432 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1433 runtime = s->runtime;
1434 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1435 d = &drec[num_drecs++];
1437 init_waitqueue_entry(&d->wait, current);
1438 add_wait_queue(&runtime->sleep, &d->wait);
1439 /* stop_threshold fixup to avoid endless loop when
1440 * stop_threshold > buffer_size
1442 d->stop_threshold = runtime->stop_threshold;
1443 if (runtime->stop_threshold > runtime->buffer_size)
1444 runtime->stop_threshold = runtime->buffer_size;
1447 up_read(&snd_pcm_link_rwsem);
1451 snd_pcm_stream_lock_irq(substream);
1453 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1454 snd_pcm_pause(substream, 0);
1456 /* pre-start/stop - all running streams are changed to DRAINING state */
1457 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1459 snd_pcm_stream_unlock_irq(substream);
1465 if (signal_pending(current)) {
1466 result = -ERESTARTSYS;
1470 for (i = 0; i < num_drecs; i++) {
1471 runtime = drec[i].substream->runtime;
1472 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1476 break; /* yes, all drained */
1478 set_current_state(TASK_INTERRUPTIBLE);
1479 snd_pcm_stream_unlock_irq(substream);
1480 snd_power_unlock(card);
1481 tout = schedule_timeout(10 * HZ);
1482 snd_power_lock(card);
1483 snd_pcm_stream_lock_irq(substream);
1485 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1488 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1489 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1496 snd_pcm_stream_unlock_irq(substream);
1499 for (i = 0; i < num_drecs; i++) {
1501 runtime = d->substream->runtime;
1502 remove_wait_queue(&runtime->sleep, &d->wait);
1503 runtime->stop_threshold = d->stop_threshold;
1506 if (drec != &drec_tmp)
1508 snd_power_unlock(card);
1516 * Immediately put all linked substreams into SETUP state.
1518 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1520 struct snd_pcm_runtime *runtime;
1521 struct snd_card *card;
1524 snd_assert(substream != NULL, return -ENXIO);
1525 runtime = substream->runtime;
1526 card = substream->pcm->card;
1528 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1531 snd_power_lock(card);
1532 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1533 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1538 snd_pcm_stream_lock_irq(substream);
1540 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1541 snd_pcm_pause(substream, 0);
1543 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1544 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1545 snd_pcm_stream_unlock_irq(substream);
1547 snd_power_unlock(card);
1552 /* WARNING: Don't forget to fput back the file */
1553 static struct file *snd_pcm_file_fd(int fd)
1556 struct inode *inode;
1557 unsigned short minor;
1561 inode = file->f_dentry->d_inode;
1562 if (!S_ISCHR(inode->i_mode) ||
1563 imajor(inode) != snd_major) {
1567 minor = iminor(inode);
1569 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1579 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1583 struct snd_pcm_file *pcm_file;
1584 struct snd_pcm_substream *substream1;
1586 file = snd_pcm_file_fd(fd);
1589 pcm_file = file->private_data;
1590 substream1 = pcm_file->substream;
1591 down_write(&snd_pcm_link_rwsem);
1592 write_lock_irq(&snd_pcm_link_rwlock);
1593 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1594 substream->runtime->status->state != substream1->runtime->status->state) {
1598 if (snd_pcm_stream_linked(substream1)) {
1602 if (!snd_pcm_stream_linked(substream)) {
1603 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1604 if (substream->group == NULL) {
1608 spin_lock_init(&substream->group->lock);
1609 INIT_LIST_HEAD(&substream->group->substreams);
1610 list_add_tail(&substream->link_list, &substream->group->substreams);
1611 substream->group->count = 1;
1613 list_add_tail(&substream1->link_list, &substream->group->substreams);
1614 substream->group->count++;
1615 substream1->group = substream->group;
1617 write_unlock_irq(&snd_pcm_link_rwlock);
1618 up_write(&snd_pcm_link_rwsem);
1623 static void relink_to_local(struct snd_pcm_substream *substream)
1625 substream->group = &substream->self_group;
1626 INIT_LIST_HEAD(&substream->self_group.substreams);
1627 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1630 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1632 struct list_head *pos;
1635 down_write(&snd_pcm_link_rwsem);
1636 write_lock_irq(&snd_pcm_link_rwlock);
1637 if (!snd_pcm_stream_linked(substream)) {
1641 list_del(&substream->link_list);
1642 substream->group->count--;
1643 if (substream->group->count == 1) { /* detach the last stream, too */
1644 snd_pcm_group_for_each(pos, substream) {
1645 relink_to_local(snd_pcm_group_substream_entry(pos));
1648 kfree(substream->group);
1650 relink_to_local(substream);
1652 write_unlock_irq(&snd_pcm_link_rwlock);
1653 up_write(&snd_pcm_link_rwsem);
1660 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1661 struct snd_pcm_hw_rule *rule)
1663 struct snd_interval t;
1664 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1665 hw_param_interval_c(params, rule->deps[1]), &t);
1666 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1669 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1670 struct snd_pcm_hw_rule *rule)
1672 struct snd_interval t;
1673 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1674 hw_param_interval_c(params, rule->deps[1]), &t);
1675 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1678 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1679 struct snd_pcm_hw_rule *rule)
1681 struct snd_interval t;
1682 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1683 hw_param_interval_c(params, rule->deps[1]),
1684 (unsigned long) rule->private, &t);
1685 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1688 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1689 struct snd_pcm_hw_rule *rule)
1691 struct snd_interval t;
1692 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1693 (unsigned long) rule->private,
1694 hw_param_interval_c(params, rule->deps[1]), &t);
1695 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1698 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1699 struct snd_pcm_hw_rule *rule)
1702 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1704 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1706 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1708 if (! snd_mask_test(mask, k))
1710 bits = snd_pcm_format_physical_width(k);
1712 continue; /* ignore invalid formats */
1713 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1714 snd_mask_reset(&m, k);
1716 return snd_mask_refine(mask, &m);
1719 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1720 struct snd_pcm_hw_rule *rule)
1722 struct snd_interval t;
1728 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1730 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1732 bits = snd_pcm_format_physical_width(k);
1734 continue; /* ignore invalid formats */
1735 if (t.min > (unsigned)bits)
1737 if (t.max < (unsigned)bits)
1741 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1744 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1745 #error "Change this table"
1748 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1749 48000, 64000, 88200, 96000, 176400, 192000 };
1751 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1752 struct snd_pcm_hw_rule *rule)
1754 struct snd_pcm_hardware *hw = rule->private;
1755 return snd_interval_list(hw_param_interval(params, rule->var),
1756 ARRAY_SIZE(rates), rates, hw->rates);
1759 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1760 struct snd_pcm_hw_rule *rule)
1762 struct snd_interval t;
1763 struct snd_pcm_substream *substream = rule->private;
1765 t.max = substream->buffer_bytes_max;
1769 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1772 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1774 struct snd_pcm_runtime *runtime = substream->runtime;
1775 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1778 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1779 snd_mask_any(constrs_mask(constrs, k));
1782 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1783 snd_interval_any(constrs_interval(constrs, k));
1786 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1787 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1788 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1792 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1793 snd_pcm_hw_rule_format, NULL,
1794 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1797 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1798 snd_pcm_hw_rule_sample_bits, NULL,
1799 SNDRV_PCM_HW_PARAM_FORMAT,
1800 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1804 snd_pcm_hw_rule_div, NULL,
1805 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mul, NULL,
1810 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1814 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1815 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1819 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1820 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1824 snd_pcm_hw_rule_div, NULL,
1825 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1829 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1830 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1834 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1839 snd_pcm_hw_rule_div, NULL,
1840 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_div, NULL,
1845 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1849 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1850 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1854 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1855 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_mul, NULL,
1860 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1864 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1869 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1874 snd_pcm_hw_rule_muldivk, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1879 snd_pcm_hw_rule_muldivk, (void*) 8,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1884 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1889 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1890 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1896 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1898 struct snd_pcm_runtime *runtime = substream->runtime;
1899 struct snd_pcm_hardware *hw = &runtime->hw;
1901 unsigned int mask = 0;
1903 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1904 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1905 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1906 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1907 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1908 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1911 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1912 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1913 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1915 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1916 snd_assert(err >= 0, return -EINVAL);
1918 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1919 snd_assert(err >= 0, return -EINVAL);
1921 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1922 snd_assert(err >= 0, return -EINVAL);
1924 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1925 hw->channels_min, hw->channels_max);
1926 snd_assert(err >= 0, return -EINVAL);
1928 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1929 hw->rate_min, hw->rate_max);
1930 snd_assert(err >= 0, return -EINVAL);
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1933 hw->period_bytes_min, hw->period_bytes_max);
1934 snd_assert(err >= 0, return -EINVAL);
1936 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1937 hw->periods_min, hw->periods_max);
1938 snd_assert(err >= 0, return -EINVAL);
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1941 hw->period_bytes_min, hw->buffer_bytes_max);
1942 snd_assert(err >= 0, return -EINVAL);
1944 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1945 snd_pcm_hw_rule_buffer_bytes_max, substream,
1946 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1951 if (runtime->dma_bytes) {
1952 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1953 snd_assert(err >= 0, return -EINVAL);
1956 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1957 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1958 snd_pcm_hw_rule_rate, hw,
1959 SNDRV_PCM_HW_PARAM_RATE, -1);
1964 /* FIXME: this belong to lowlevel */
1965 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1966 1000000 / HZ, 1000000 / HZ);
1967 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1972 static void snd_pcm_add_file(struct snd_pcm_str *str,
1973 struct snd_pcm_file *pcm_file)
1975 pcm_file->next = str->files;
1976 str->files = pcm_file;
1979 static void snd_pcm_remove_file(struct snd_pcm_str *str,
1980 struct snd_pcm_file *pcm_file)
1982 struct snd_pcm_file * pcm_file1;
1983 if (str->files == pcm_file) {
1984 str->files = pcm_file->next;
1986 pcm_file1 = str->files;
1987 while (pcm_file1 && pcm_file1->next != pcm_file)
1988 pcm_file1 = pcm_file1->next;
1989 if (pcm_file1 != NULL)
1990 pcm_file1->next = pcm_file->next;
1994 static int snd_pcm_release_file(struct snd_pcm_file * pcm_file)
1996 struct snd_pcm_substream *substream;
1997 struct snd_pcm_runtime *runtime;
1998 struct snd_pcm_str * str;
2000 snd_assert(pcm_file != NULL, return -ENXIO);
2001 substream = pcm_file->substream;
2002 snd_assert(substream != NULL, return -ENXIO);
2003 runtime = substream->runtime;
2004 str = substream->pstr;
2005 snd_pcm_unlink(substream);
2006 if (substream->ffile != NULL) {
2007 if (substream->ops->hw_free != NULL)
2008 substream->ops->hw_free(substream);
2009 substream->ops->close(substream);
2010 substream->ffile = NULL;
2012 snd_pcm_remove_file(str, pcm_file);
2013 snd_pcm_release_substream(substream);
2018 static int snd_pcm_open_file(struct file *file,
2019 struct snd_pcm *pcm,
2021 struct snd_pcm_file **rpcm_file)
2024 struct snd_pcm_file *pcm_file;
2025 struct snd_pcm_substream *substream;
2026 struct snd_pcm_str *str;
2028 snd_assert(rpcm_file != NULL, return -EINVAL);
2031 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2032 if (pcm_file == NULL) {
2036 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2041 str = substream->pstr;
2042 substream->file = pcm_file;
2043 substream->no_mmap_ctrl = 0;
2045 pcm_file->substream = substream;
2047 snd_pcm_add_file(str, pcm_file);
2049 err = snd_pcm_hw_constraints_init(substream);
2051 snd_printd("snd_pcm_hw_constraints_init failed\n");
2052 snd_pcm_release_file(pcm_file);
2056 if ((err = substream->ops->open(substream)) < 0) {
2057 snd_pcm_release_file(pcm_file);
2060 substream->ffile = file;
2062 err = snd_pcm_hw_constraints_complete(substream);
2064 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2065 snd_pcm_release_file(pcm_file);
2069 file->private_data = pcm_file;
2070 *rpcm_file = pcm_file;
2074 static int snd_pcm_open(struct inode *inode, struct file *file)
2076 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2077 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2079 struct snd_pcm *pcm;
2080 struct snd_pcm_file *pcm_file;
2083 if (device < SNDRV_MINOR_PCM_PLAYBACK || device >= SNDRV_MINOR_DEVICES)
2085 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2090 err = snd_card_file_add(pcm->card, file);
2093 if (!try_module_get(pcm->card->module)) {
2097 init_waitqueue_entry(&wait, current);
2098 add_wait_queue(&pcm->open_wait, &wait);
2099 down(&pcm->open_mutex);
2101 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2104 if (err == -EAGAIN) {
2105 if (file->f_flags & O_NONBLOCK) {
2111 set_current_state(TASK_INTERRUPTIBLE);
2112 up(&pcm->open_mutex);
2114 down(&pcm->open_mutex);
2115 if (signal_pending(current)) {
2120 remove_wait_queue(&pcm->open_wait, &wait);
2121 up(&pcm->open_mutex);
2127 module_put(pcm->card->module);
2129 snd_card_file_remove(pcm->card, file);
2134 static int snd_pcm_release(struct inode *inode, struct file *file)
2136 struct snd_pcm *pcm;
2137 struct snd_pcm_substream *substream;
2138 struct snd_pcm_file *pcm_file;
2140 pcm_file = file->private_data;
2141 substream = pcm_file->substream;
2142 snd_assert(substream != NULL, return -ENXIO);
2143 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2144 pcm = substream->pcm;
2145 snd_pcm_drop(substream);
2146 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2147 down(&pcm->open_mutex);
2148 snd_pcm_release_file(pcm_file);
2149 up(&pcm->open_mutex);
2150 wake_up(&pcm->open_wait);
2151 module_put(pcm->card->module);
2152 snd_card_file_remove(pcm->card, file);
2156 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2157 snd_pcm_uframes_t frames)
2159 struct snd_pcm_runtime *runtime = substream->runtime;
2160 snd_pcm_sframes_t appl_ptr;
2161 snd_pcm_sframes_t ret;
2162 snd_pcm_sframes_t hw_avail;
2167 snd_pcm_stream_lock_irq(substream);
2168 switch (runtime->status->state) {
2169 case SNDRV_PCM_STATE_PREPARED:
2171 case SNDRV_PCM_STATE_DRAINING:
2172 case SNDRV_PCM_STATE_RUNNING:
2173 if (snd_pcm_update_hw_ptr(substream) >= 0)
2176 case SNDRV_PCM_STATE_XRUN:
2184 hw_avail = snd_pcm_playback_hw_avail(runtime);
2185 if (hw_avail <= 0) {
2189 if (frames > (snd_pcm_uframes_t)hw_avail)
2192 frames -= frames % runtime->xfer_align;
2193 appl_ptr = runtime->control->appl_ptr - frames;
2195 appl_ptr += runtime->boundary;
2196 runtime->control->appl_ptr = appl_ptr;
2197 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2199 snd_pcm_tick_prepare(substream);
2202 snd_pcm_stream_unlock_irq(substream);
2206 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2207 snd_pcm_uframes_t frames)
2209 struct snd_pcm_runtime *runtime = substream->runtime;
2210 snd_pcm_sframes_t appl_ptr;
2211 snd_pcm_sframes_t ret;
2212 snd_pcm_sframes_t hw_avail;
2217 snd_pcm_stream_lock_irq(substream);
2218 switch (runtime->status->state) {
2219 case SNDRV_PCM_STATE_PREPARED:
2220 case SNDRV_PCM_STATE_DRAINING:
2222 case SNDRV_PCM_STATE_RUNNING:
2223 if (snd_pcm_update_hw_ptr(substream) >= 0)
2226 case SNDRV_PCM_STATE_XRUN:
2234 hw_avail = snd_pcm_capture_hw_avail(runtime);
2235 if (hw_avail <= 0) {
2239 if (frames > (snd_pcm_uframes_t)hw_avail)
2242 frames -= frames % runtime->xfer_align;
2243 appl_ptr = runtime->control->appl_ptr - frames;
2245 appl_ptr += runtime->boundary;
2246 runtime->control->appl_ptr = appl_ptr;
2247 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2249 snd_pcm_tick_prepare(substream);
2252 snd_pcm_stream_unlock_irq(substream);
2256 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2257 snd_pcm_uframes_t frames)
2259 struct snd_pcm_runtime *runtime = substream->runtime;
2260 snd_pcm_sframes_t appl_ptr;
2261 snd_pcm_sframes_t ret;
2262 snd_pcm_sframes_t avail;
2267 snd_pcm_stream_lock_irq(substream);
2268 switch (runtime->status->state) {
2269 case SNDRV_PCM_STATE_PREPARED:
2270 case SNDRV_PCM_STATE_PAUSED:
2272 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 avail = snd_pcm_playback_avail(runtime);
2290 if (frames > (snd_pcm_uframes_t)avail)
2293 frames -= frames % runtime->xfer_align;
2294 appl_ptr = runtime->control->appl_ptr + frames;
2295 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
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_capture_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_DRAINING:
2322 case SNDRV_PCM_STATE_PAUSED:
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_capture_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 int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2360 struct snd_pcm_runtime *runtime = substream->runtime;
2363 snd_pcm_stream_lock_irq(substream);
2364 switch (runtime->status->state) {
2365 case SNDRV_PCM_STATE_DRAINING:
2366 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2368 case SNDRV_PCM_STATE_RUNNING:
2369 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2372 case SNDRV_PCM_STATE_PREPARED:
2373 case SNDRV_PCM_STATE_SUSPENDED:
2376 case SNDRV_PCM_STATE_XRUN:
2384 snd_pcm_stream_unlock_irq(substream);
2388 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2389 snd_pcm_sframes_t __user *res)
2391 struct snd_pcm_runtime *runtime = substream->runtime;
2393 snd_pcm_sframes_t n = 0;
2395 snd_pcm_stream_lock_irq(substream);
2396 switch (runtime->status->state) {
2397 case SNDRV_PCM_STATE_DRAINING:
2398 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2400 case SNDRV_PCM_STATE_RUNNING:
2401 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2404 case SNDRV_PCM_STATE_PREPARED:
2405 case SNDRV_PCM_STATE_SUSPENDED:
2407 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2408 n = snd_pcm_playback_hw_avail(runtime);
2410 n = snd_pcm_capture_avail(runtime);
2412 case SNDRV_PCM_STATE_XRUN:
2420 snd_pcm_stream_unlock_irq(substream);
2422 if (put_user(n, res))
2427 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2428 struct snd_pcm_sync_ptr __user *_sync_ptr)
2430 struct snd_pcm_runtime *runtime = substream->runtime;
2431 struct snd_pcm_sync_ptr sync_ptr;
2432 volatile struct snd_pcm_mmap_status *status;
2433 volatile struct snd_pcm_mmap_control *control;
2436 memset(&sync_ptr, 0, sizeof(sync_ptr));
2437 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2439 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2441 status = runtime->status;
2442 control = runtime->control;
2443 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2444 err = snd_pcm_hwsync(substream);
2448 snd_pcm_stream_lock_irq(substream);
2449 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2450 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2452 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2453 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2454 control->avail_min = sync_ptr.c.control.avail_min;
2456 sync_ptr.c.control.avail_min = control->avail_min;
2457 sync_ptr.s.status.state = status->state;
2458 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2459 sync_ptr.s.status.tstamp = status->tstamp;
2460 sync_ptr.s.status.suspended_state = status->suspended_state;
2461 snd_pcm_stream_unlock_irq(substream);
2462 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2467 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2468 unsigned int cmd, void __user *arg);
2469 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2470 unsigned int cmd, void __user *arg);
2472 static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
2473 unsigned int cmd, void __user *arg)
2475 snd_assert(substream != NULL, return -ENXIO);
2478 case SNDRV_PCM_IOCTL_PVERSION:
2479 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2480 case SNDRV_PCM_IOCTL_INFO:
2481 return snd_pcm_info_user(substream, arg);
2482 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2484 case SNDRV_PCM_IOCTL_HW_REFINE:
2485 return snd_pcm_hw_refine_user(substream, arg);
2486 case SNDRV_PCM_IOCTL_HW_PARAMS:
2487 return snd_pcm_hw_params_user(substream, arg);
2488 case SNDRV_PCM_IOCTL_HW_FREE:
2489 return snd_pcm_hw_free(substream);
2490 case SNDRV_PCM_IOCTL_SW_PARAMS:
2491 return snd_pcm_sw_params_user(substream, arg);
2492 case SNDRV_PCM_IOCTL_STATUS:
2493 return snd_pcm_status_user(substream, arg);
2494 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2495 return snd_pcm_channel_info_user(substream, arg);
2496 case SNDRV_PCM_IOCTL_PREPARE:
2497 return snd_pcm_prepare(substream);
2498 case SNDRV_PCM_IOCTL_RESET:
2499 return snd_pcm_reset(substream);
2500 case SNDRV_PCM_IOCTL_START:
2501 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2502 case SNDRV_PCM_IOCTL_LINK:
2503 return snd_pcm_link(substream, (int)(unsigned long) arg);
2504 case SNDRV_PCM_IOCTL_UNLINK:
2505 return snd_pcm_unlink(substream);
2506 case SNDRV_PCM_IOCTL_RESUME:
2507 return snd_pcm_resume(substream);
2508 case SNDRV_PCM_IOCTL_XRUN:
2509 return snd_pcm_xrun(substream);
2510 case SNDRV_PCM_IOCTL_HWSYNC:
2511 return snd_pcm_hwsync(substream);
2512 case SNDRV_PCM_IOCTL_DELAY:
2513 return snd_pcm_delay(substream, arg);
2514 case SNDRV_PCM_IOCTL_SYNC_PTR:
2515 return snd_pcm_sync_ptr(substream, arg);
2516 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2517 return snd_pcm_hw_refine_old_user(substream, arg);
2518 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2519 return snd_pcm_hw_params_old_user(substream, arg);
2520 case SNDRV_PCM_IOCTL_DRAIN:
2521 return snd_pcm_drain(substream);
2522 case SNDRV_PCM_IOCTL_DROP:
2523 return snd_pcm_drop(substream);
2525 snd_printd("unknown ioctl = 0x%x\n", cmd);
2529 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2530 unsigned int cmd, void __user *arg)
2532 snd_assert(substream != NULL, return -ENXIO);
2533 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2535 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2537 struct snd_xferi xferi;
2538 struct snd_xferi __user *_xferi = arg;
2539 struct snd_pcm_runtime *runtime = substream->runtime;
2540 snd_pcm_sframes_t result;
2541 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2543 if (put_user(0, &_xferi->result))
2545 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2547 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2548 __put_user(result, &_xferi->result);
2549 return result < 0 ? result : 0;
2551 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2553 struct snd_xfern xfern;
2554 struct snd_xfern __user *_xfern = arg;
2555 struct snd_pcm_runtime *runtime = substream->runtime;
2557 snd_pcm_sframes_t result;
2558 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2560 if (runtime->channels > 128)
2562 if (put_user(0, &_xfern->result))
2564 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2566 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2569 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2573 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2575 __put_user(result, &_xfern->result);
2576 return result < 0 ? result : 0;
2578 case SNDRV_PCM_IOCTL_REWIND:
2580 snd_pcm_uframes_t frames;
2581 snd_pcm_uframes_t __user *_frames = arg;
2582 snd_pcm_sframes_t result;
2583 if (get_user(frames, _frames))
2585 if (put_user(0, _frames))
2587 result = snd_pcm_playback_rewind(substream, frames);
2588 __put_user(result, _frames);
2589 return result < 0 ? result : 0;
2591 case SNDRV_PCM_IOCTL_FORWARD:
2593 snd_pcm_uframes_t frames;
2594 snd_pcm_uframes_t __user *_frames = arg;
2595 snd_pcm_sframes_t result;
2596 if (get_user(frames, _frames))
2598 if (put_user(0, _frames))
2600 result = snd_pcm_playback_forward(substream, frames);
2601 __put_user(result, _frames);
2602 return result < 0 ? result : 0;
2604 case SNDRV_PCM_IOCTL_PAUSE:
2607 snd_pcm_stream_lock_irq(substream);
2608 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2609 snd_pcm_stream_unlock_irq(substream);
2613 return snd_pcm_common_ioctl1(substream, cmd, arg);
2616 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2617 unsigned int cmd, void __user *arg)
2619 snd_assert(substream != NULL, return -ENXIO);
2620 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2622 case SNDRV_PCM_IOCTL_READI_FRAMES:
2624 struct snd_xferi xferi;
2625 struct snd_xferi __user *_xferi = arg;
2626 struct snd_pcm_runtime *runtime = substream->runtime;
2627 snd_pcm_sframes_t result;
2628 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2630 if (put_user(0, &_xferi->result))
2632 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2634 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2635 __put_user(result, &_xferi->result);
2636 return result < 0 ? result : 0;
2638 case SNDRV_PCM_IOCTL_READN_FRAMES:
2640 struct snd_xfern xfern;
2641 struct snd_xfern __user *_xfern = arg;
2642 struct snd_pcm_runtime *runtime = substream->runtime;
2644 snd_pcm_sframes_t result;
2645 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2647 if (runtime->channels > 128)
2649 if (put_user(0, &_xfern->result))
2651 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2653 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2656 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2660 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2662 __put_user(result, &_xfern->result);
2663 return result < 0 ? result : 0;
2665 case SNDRV_PCM_IOCTL_REWIND:
2667 snd_pcm_uframes_t frames;
2668 snd_pcm_uframes_t __user *_frames = arg;
2669 snd_pcm_sframes_t result;
2670 if (get_user(frames, _frames))
2672 if (put_user(0, _frames))
2674 result = snd_pcm_capture_rewind(substream, frames);
2675 __put_user(result, _frames);
2676 return result < 0 ? result : 0;
2678 case SNDRV_PCM_IOCTL_FORWARD:
2680 snd_pcm_uframes_t frames;
2681 snd_pcm_uframes_t __user *_frames = arg;
2682 snd_pcm_sframes_t result;
2683 if (get_user(frames, _frames))
2685 if (put_user(0, _frames))
2687 result = snd_pcm_capture_forward(substream, frames);
2688 __put_user(result, _frames);
2689 return result < 0 ? result : 0;
2692 return snd_pcm_common_ioctl1(substream, cmd, arg);
2695 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2698 struct snd_pcm_file *pcm_file;
2700 pcm_file = file->private_data;
2702 if (((cmd >> 8) & 0xff) != 'A')
2705 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2708 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2711 struct snd_pcm_file *pcm_file;
2713 pcm_file = file->private_data;
2715 if (((cmd >> 8) & 0xff) != 'A')
2718 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2721 int snd_pcm_kernel_playback_ioctl(struct snd_pcm_substream *substream,
2722 unsigned int cmd, void *arg)
2727 fs = snd_enter_user();
2728 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2733 int snd_pcm_kernel_capture_ioctl(struct snd_pcm_substream *substream,
2734 unsigned int cmd, void *arg)
2739 fs = snd_enter_user();
2740 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2745 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2746 unsigned int cmd, void *arg)
2748 switch (substream->stream) {
2749 case SNDRV_PCM_STREAM_PLAYBACK:
2750 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2751 case SNDRV_PCM_STREAM_CAPTURE:
2752 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2758 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2761 struct snd_pcm_file *pcm_file;
2762 struct snd_pcm_substream *substream;
2763 struct snd_pcm_runtime *runtime;
2764 snd_pcm_sframes_t result;
2766 pcm_file = file->private_data;
2767 substream = pcm_file->substream;
2768 snd_assert(substream != NULL, return -ENXIO);
2769 runtime = substream->runtime;
2770 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2772 if (!frame_aligned(runtime, count))
2774 count = bytes_to_frames(runtime, count);
2775 result = snd_pcm_lib_read(substream, buf, count);
2777 result = frames_to_bytes(runtime, result);
2781 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2782 size_t count, loff_t * offset)
2784 struct snd_pcm_file *pcm_file;
2785 struct snd_pcm_substream *substream;
2786 struct snd_pcm_runtime *runtime;
2787 snd_pcm_sframes_t result;
2789 pcm_file = file->private_data;
2790 substream = pcm_file->substream;
2791 snd_assert(substream != NULL, result = -ENXIO; goto end);
2792 runtime = substream->runtime;
2793 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2797 if (!frame_aligned(runtime, count)) {
2801 count = bytes_to_frames(runtime, count);
2802 result = snd_pcm_lib_write(substream, buf, count);
2804 result = frames_to_bytes(runtime, result);
2809 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2810 unsigned long count, loff_t * offset)
2813 struct snd_pcm_file *pcm_file;
2814 struct snd_pcm_substream *substream;
2815 struct snd_pcm_runtime *runtime;
2816 snd_pcm_sframes_t result;
2819 snd_pcm_uframes_t frames;
2821 pcm_file = file->private_data;
2822 substream = pcm_file->substream;
2823 snd_assert(substream != NULL, return -ENXIO);
2824 runtime = substream->runtime;
2825 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2827 if (count > 1024 || count != runtime->channels)
2829 if (!frame_aligned(runtime, _vector->iov_len))
2831 frames = bytes_to_samples(runtime, _vector->iov_len);
2832 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2835 for (i = 0; i < count; ++i)
2836 bufs[i] = _vector[i].iov_base;
2837 result = snd_pcm_lib_readv(substream, bufs, frames);
2839 result = frames_to_bytes(runtime, result);
2844 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2845 unsigned long count, loff_t * offset)
2847 struct snd_pcm_file *pcm_file;
2848 struct snd_pcm_substream *substream;
2849 struct snd_pcm_runtime *runtime;
2850 snd_pcm_sframes_t result;
2853 snd_pcm_uframes_t frames;
2855 pcm_file = file->private_data;
2856 substream = pcm_file->substream;
2857 snd_assert(substream != NULL, result = -ENXIO; goto end);
2858 runtime = substream->runtime;
2859 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2863 if (count > 128 || count != runtime->channels ||
2864 !frame_aligned(runtime, _vector->iov_len)) {
2868 frames = bytes_to_samples(runtime, _vector->iov_len);
2869 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2872 for (i = 0; i < count; ++i)
2873 bufs[i] = _vector[i].iov_base;
2874 result = snd_pcm_lib_writev(substream, bufs, frames);
2876 result = frames_to_bytes(runtime, result);
2882 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2884 struct snd_pcm_file *pcm_file;
2885 struct snd_pcm_substream *substream;
2886 struct snd_pcm_runtime *runtime;
2888 snd_pcm_uframes_t avail;
2890 pcm_file = file->private_data;
2892 substream = pcm_file->substream;
2893 snd_assert(substream != NULL, return -ENXIO);
2894 runtime = substream->runtime;
2896 poll_wait(file, &runtime->sleep, wait);
2898 snd_pcm_stream_lock_irq(substream);
2899 avail = snd_pcm_playback_avail(runtime);
2900 switch (runtime->status->state) {
2901 case SNDRV_PCM_STATE_RUNNING:
2902 case SNDRV_PCM_STATE_PREPARED:
2903 case SNDRV_PCM_STATE_PAUSED:
2904 if (avail >= runtime->control->avail_min) {
2905 mask = POLLOUT | POLLWRNORM;
2909 case SNDRV_PCM_STATE_DRAINING:
2913 mask = POLLOUT | POLLWRNORM | POLLERR;
2916 snd_pcm_stream_unlock_irq(substream);
2920 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2922 struct snd_pcm_file *pcm_file;
2923 struct snd_pcm_substream *substream;
2924 struct snd_pcm_runtime *runtime;
2926 snd_pcm_uframes_t avail;
2928 pcm_file = file->private_data;
2930 substream = pcm_file->substream;
2931 snd_assert(substream != NULL, return -ENXIO);
2932 runtime = substream->runtime;
2934 poll_wait(file, &runtime->sleep, wait);
2936 snd_pcm_stream_lock_irq(substream);
2937 avail = snd_pcm_capture_avail(runtime);
2938 switch (runtime->status->state) {
2939 case SNDRV_PCM_STATE_RUNNING:
2940 case SNDRV_PCM_STATE_PREPARED:
2941 case SNDRV_PCM_STATE_PAUSED:
2942 if (avail >= runtime->control->avail_min) {
2943 mask = POLLIN | POLLRDNORM;
2948 case SNDRV_PCM_STATE_DRAINING:
2950 mask = POLLIN | POLLRDNORM;
2955 mask = POLLIN | POLLRDNORM | POLLERR;
2958 snd_pcm_stream_unlock_irq(substream);
2967 * Only on coherent architectures, we can mmap the status and the control records
2968 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2970 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2972 * mmap status record
2974 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2975 unsigned long address, int *type)
2977 struct snd_pcm_substream *substream = area->vm_private_data;
2978 struct snd_pcm_runtime *runtime;
2981 if (substream == NULL)
2983 runtime = substream->runtime;
2984 page = virt_to_page(runtime->status);
2987 *type = VM_FAULT_MINOR;
2991 static struct vm_operations_struct snd_pcm_vm_ops_status =
2993 .nopage = snd_pcm_mmap_status_nopage,
2996 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
2997 struct vm_area_struct *area)
2999 struct snd_pcm_runtime *runtime;
3001 if (!(area->vm_flags & VM_READ))
3003 runtime = substream->runtime;
3004 snd_assert(runtime != NULL, return -EAGAIN);
3005 size = area->vm_end - area->vm_start;
3006 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3008 area->vm_ops = &snd_pcm_vm_ops_status;
3009 area->vm_private_data = substream;
3010 area->vm_flags |= VM_RESERVED;
3015 * mmap control record
3017 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3018 unsigned long address, int *type)
3020 struct snd_pcm_substream *substream = area->vm_private_data;
3021 struct snd_pcm_runtime *runtime;
3024 if (substream == NULL)
3026 runtime = substream->runtime;
3027 page = virt_to_page(runtime->control);
3030 *type = VM_FAULT_MINOR;
3034 static struct vm_operations_struct snd_pcm_vm_ops_control =
3036 .nopage = snd_pcm_mmap_control_nopage,
3039 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3040 struct vm_area_struct *area)
3042 struct snd_pcm_runtime *runtime;
3044 if (!(area->vm_flags & VM_READ))
3046 runtime = substream->runtime;
3047 snd_assert(runtime != NULL, return -EAGAIN);
3048 size = area->vm_end - area->vm_start;
3049 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3051 area->vm_ops = &snd_pcm_vm_ops_control;
3052 area->vm_private_data = substream;
3053 area->vm_flags |= VM_RESERVED;
3056 #else /* ! coherent mmap */
3058 * don't support mmap for status and control records.
3060 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3061 struct vm_area_struct *area)
3065 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3066 struct vm_area_struct *area)
3070 #endif /* coherent mmap */
3073 * nopage callback for mmapping a RAM page
3075 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3076 unsigned long address, int *type)
3078 struct snd_pcm_substream *substream = area->vm_private_data;
3079 struct snd_pcm_runtime *runtime;
3080 unsigned long offset;
3085 if (substream == NULL)
3087 runtime = substream->runtime;
3088 offset = area->vm_pgoff << PAGE_SHIFT;
3089 offset += address - area->vm_start;
3090 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3091 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3092 if (offset > dma_bytes - PAGE_SIZE)
3093 return NOPAGE_SIGBUS;
3094 if (substream->ops->page) {
3095 page = substream->ops->page(substream, offset);
3099 vaddr = runtime->dma_area + offset;
3100 page = virt_to_page(vaddr);
3104 *type = VM_FAULT_MINOR;
3108 static struct vm_operations_struct snd_pcm_vm_ops_data =
3110 .open = snd_pcm_mmap_data_open,
3111 .close = snd_pcm_mmap_data_close,
3112 .nopage = snd_pcm_mmap_data_nopage,
3116 * mmap the DMA buffer on RAM
3118 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3119 struct vm_area_struct *area)
3121 area->vm_ops = &snd_pcm_vm_ops_data;
3122 area->vm_private_data = substream;
3123 area->vm_flags |= VM_RESERVED;
3124 atomic_inc(&substream->runtime->mmap_count);
3129 * mmap the DMA buffer on I/O memory area
3131 #if SNDRV_PCM_INFO_MMAP_IOMEM
3132 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3134 .open = snd_pcm_mmap_data_open,
3135 .close = snd_pcm_mmap_data_close,
3138 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3139 struct vm_area_struct *area)
3142 unsigned long offset;
3144 #ifdef pgprot_noncached
3145 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3147 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3148 area->vm_private_data = substream;
3149 area->vm_flags |= VM_IO;
3150 size = area->vm_end - area->vm_start;
3151 offset = area->vm_pgoff << PAGE_SHIFT;
3152 if (io_remap_pfn_range(area, area->vm_start,
3153 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3154 size, area->vm_page_prot))
3156 atomic_inc(&substream->runtime->mmap_count);
3159 #endif /* SNDRV_PCM_INFO_MMAP */
3164 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3165 struct vm_area_struct *area)
3167 struct snd_pcm_runtime *runtime;
3169 unsigned long offset;
3172 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3173 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3176 if (!(area->vm_flags & VM_READ))
3179 runtime = substream->runtime;
3180 snd_assert(runtime != NULL, return -EAGAIN);
3181 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3183 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3185 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3186 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3188 size = area->vm_end - area->vm_start;
3189 offset = area->vm_pgoff << PAGE_SHIFT;
3190 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3191 if ((size_t)size > dma_bytes)
3193 if (offset > dma_bytes - size)
3196 if (substream->ops->mmap)
3197 return substream->ops->mmap(substream, area);
3199 return snd_pcm_default_mmap(substream, area);
3202 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3204 struct snd_pcm_file * pcm_file;
3205 struct snd_pcm_substream *substream;
3206 unsigned long offset;
3208 pcm_file = file->private_data;
3209 substream = pcm_file->substream;
3210 snd_assert(substream != NULL, return -ENXIO);
3212 offset = area->vm_pgoff << PAGE_SHIFT;
3214 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3215 if (substream->no_mmap_ctrl)
3217 return snd_pcm_mmap_status(substream, file, area);
3218 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3219 if (substream->no_mmap_ctrl)
3221 return snd_pcm_mmap_control(substream, file, area);
3223 return snd_pcm_mmap_data(substream, file, area);
3228 static int snd_pcm_fasync(int fd, struct file * file, int on)
3230 struct snd_pcm_file * pcm_file;
3231 struct snd_pcm_substream *substream;
3232 struct snd_pcm_runtime *runtime;
3235 pcm_file = file->private_data;
3236 substream = pcm_file->substream;
3237 snd_assert(substream != NULL, return -ENXIO);
3238 runtime = substream->runtime;
3240 err = fasync_helper(fd, file, on, &runtime->fasync);
3249 #ifdef CONFIG_COMPAT
3250 #include "pcm_compat.c"
3252 #define snd_pcm_ioctl_compat NULL
3256 * To be removed helpers to keep binary compatibility
3259 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3260 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3262 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3263 struct snd_pcm_hw_params_old *oparams)
3267 memset(params, 0, sizeof(*params));
3268 params->flags = oparams->flags;
3269 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3270 params->masks[i].bits[0] = oparams->masks[i];
3271 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3272 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3273 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3274 params->info = oparams->info;
3275 params->msbits = oparams->msbits;
3276 params->rate_num = oparams->rate_num;
3277 params->rate_den = oparams->rate_den;
3278 params->fifo_size = oparams->fifo_size;
3281 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3282 struct snd_pcm_hw_params *params)
3286 memset(oparams, 0, sizeof(*oparams));
3287 oparams->flags = params->flags;
3288 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3289 oparams->masks[i] = params->masks[i].bits[0];
3290 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3291 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3292 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3293 oparams->info = params->info;
3294 oparams->msbits = params->msbits;
3295 oparams->rate_num = params->rate_num;
3296 oparams->rate_den = params->rate_den;
3297 oparams->fifo_size = params->fifo_size;
3300 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3301 struct snd_pcm_hw_params_old __user * _oparams)
3303 struct snd_pcm_hw_params *params;
3304 struct snd_pcm_hw_params_old *oparams = NULL;
3307 params = kmalloc(sizeof(*params), GFP_KERNEL);
3312 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3318 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3322 snd_pcm_hw_convert_from_old_params(params, oparams);
3323 err = snd_pcm_hw_refine(substream, params);
3324 snd_pcm_hw_convert_to_old_params(oparams, params);
3325 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3335 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3336 struct snd_pcm_hw_params_old __user * _oparams)
3338 struct snd_pcm_hw_params *params;
3339 struct snd_pcm_hw_params_old *oparams = NULL;
3342 params = kmalloc(sizeof(*params), GFP_KERNEL);
3347 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3352 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3356 snd_pcm_hw_convert_from_old_params(params, oparams);
3357 err = snd_pcm_hw_params(substream, params);
3358 snd_pcm_hw_convert_to_old_params(oparams, params);
3359 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3373 struct file_operations snd_pcm_f_ops[2] = {
3375 .owner = THIS_MODULE,
3376 .write = snd_pcm_write,
3377 .writev = snd_pcm_writev,
3378 .open = snd_pcm_open,
3379 .release = snd_pcm_release,
3380 .poll = snd_pcm_playback_poll,
3381 .unlocked_ioctl = snd_pcm_playback_ioctl,
3382 .compat_ioctl = snd_pcm_ioctl_compat,
3383 .mmap = snd_pcm_mmap,
3384 .fasync = snd_pcm_fasync,
3387 .owner = THIS_MODULE,
3388 .read = snd_pcm_read,
3389 .readv = snd_pcm_readv,
3390 .open = snd_pcm_open,
3391 .release = snd_pcm_release,
3392 .poll = snd_pcm_capture_poll,
3393 .unlocked_ioctl = snd_pcm_capture_ioctl,
3394 .compat_ioctl = snd_pcm_ioctl_compat,
3395 .mmap = snd_pcm_mmap,
3396 .fasync = snd_pcm_fasync,