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;
1076 snd_pcm_stream_lock_irqsave(substream, flags);
1077 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1078 snd_pcm_stream_unlock_irqrestore(substream, flags);
1083 * snd_pcm_suspend_all
1084 * @pcm: the PCM instance
1086 * Trigger SUSPEND to all substreams in the given pcm.
1087 * After this call, all streams are changed to SUSPENDED state.
1089 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1091 struct snd_pcm_substream *substream;
1092 int stream, err = 0;
1094 for (stream = 0; stream < 2; stream++) {
1095 for (substream = pcm->streams[stream].substream;
1096 substream; substream = substream->next) {
1097 /* FIXME: the open/close code should lock this as well */
1098 if (substream->runtime == NULL)
1100 err = snd_pcm_suspend(substream);
1101 if (err < 0 && err != -EBUSY)
1110 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1112 struct snd_pcm_runtime *runtime = substream->runtime;
1113 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1115 runtime->trigger_master = substream;
1119 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1121 struct snd_pcm_runtime *runtime = substream->runtime;
1122 if (runtime->trigger_master != substream)
1124 /* DMA not running previously? */
1125 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1126 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1127 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1129 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1132 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1134 if (substream->runtime->trigger_master == substream &&
1135 snd_pcm_running(substream))
1136 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1139 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1141 struct snd_pcm_runtime *runtime = substream->runtime;
1142 snd_pcm_trigger_tstamp(substream);
1143 if (substream->timer)
1144 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1145 &runtime->trigger_tstamp);
1146 runtime->status->state = runtime->status->suspended_state;
1147 if (runtime->sleep_min)
1148 snd_pcm_tick_prepare(substream);
1151 static struct action_ops snd_pcm_action_resume = {
1152 .pre_action = snd_pcm_pre_resume,
1153 .do_action = snd_pcm_do_resume,
1154 .undo_action = snd_pcm_undo_resume,
1155 .post_action = snd_pcm_post_resume
1158 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1160 struct snd_card *card = substream->pcm->card;
1163 snd_power_lock(card);
1164 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1165 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1166 snd_power_unlock(card);
1172 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1177 #endif /* CONFIG_PM */
1182 * Change the RUNNING stream(s) to XRUN state.
1184 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1186 struct snd_card *card = substream->pcm->card;
1187 struct snd_pcm_runtime *runtime = substream->runtime;
1190 snd_power_lock(card);
1191 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1192 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1197 snd_pcm_stream_lock_irq(substream);
1198 switch (runtime->status->state) {
1199 case SNDRV_PCM_STATE_XRUN:
1200 result = 0; /* already there */
1202 case SNDRV_PCM_STATE_RUNNING:
1203 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1208 snd_pcm_stream_unlock_irq(substream);
1210 snd_power_unlock(card);
1217 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1219 struct snd_pcm_runtime *runtime = substream->runtime;
1220 switch (runtime->status->state) {
1221 case SNDRV_PCM_STATE_RUNNING:
1222 case SNDRV_PCM_STATE_PREPARED:
1223 case SNDRV_PCM_STATE_PAUSED:
1224 case SNDRV_PCM_STATE_SUSPENDED:
1231 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1237 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1238 runtime->hw_ptr_base = 0;
1239 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1240 runtime->status->hw_ptr % runtime->period_size;
1241 runtime->silence_start = runtime->status->hw_ptr;
1242 runtime->silence_filled = 0;
1246 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 runtime->control->appl_ptr = runtime->status->hw_ptr;
1250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1251 runtime->silence_size > 0)
1252 snd_pcm_playback_silence(substream, ULONG_MAX);
1255 static struct action_ops snd_pcm_action_reset = {
1256 .pre_action = snd_pcm_pre_reset,
1257 .do_action = snd_pcm_do_reset,
1258 .post_action = snd_pcm_post_reset
1261 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1263 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1269 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
1271 struct snd_pcm_runtime *runtime = substream->runtime;
1272 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1274 if (snd_pcm_running(substream))
1279 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1282 err = substream->ops->prepare(substream);
1285 return snd_pcm_do_reset(substream, 0);
1288 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1290 struct snd_pcm_runtime *runtime = substream->runtime;
1291 runtime->control->appl_ptr = runtime->status->hw_ptr;
1292 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1295 static struct action_ops snd_pcm_action_prepare = {
1296 .pre_action = snd_pcm_pre_prepare,
1297 .do_action = snd_pcm_do_prepare,
1298 .post_action = snd_pcm_post_prepare
1303 * @substream: the PCM substream instance
1305 * Prepare the PCM substream to be triggerable.
1307 int snd_pcm_prepare(struct snd_pcm_substream *substream)
1310 struct snd_card *card = substream->pcm->card;
1312 snd_power_lock(card);
1313 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1314 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1315 snd_power_unlock(card);
1323 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1325 if (substream->ffile->f_flags & O_NONBLOCK)
1327 substream->runtime->trigger_master = substream;
1331 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1333 struct snd_pcm_runtime *runtime = substream->runtime;
1334 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1335 switch (runtime->status->state) {
1336 case SNDRV_PCM_STATE_PREPARED:
1337 /* start playback stream if possible */
1338 if (! snd_pcm_playback_empty(substream)) {
1339 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1340 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1343 case SNDRV_PCM_STATE_RUNNING:
1344 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1350 /* stop running stream */
1351 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1352 int state = snd_pcm_capture_avail(runtime) > 0 ?
1353 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1354 snd_pcm_do_stop(substream, state);
1355 snd_pcm_post_stop(substream, state);
1361 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1365 static struct action_ops snd_pcm_action_drain_init = {
1366 .pre_action = snd_pcm_pre_drain_init,
1367 .do_action = snd_pcm_do_drain_init,
1368 .post_action = snd_pcm_post_drain_init
1372 struct snd_pcm_substream *substream;
1374 snd_pcm_uframes_t stop_threshold;
1377 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1380 * Drain the stream(s).
1381 * When the substream is linked, sync until the draining of all playback streams
1383 * After this call, all streams are supposed to be either SETUP or DRAINING
1384 * (capture only) state.
1386 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1388 struct snd_card *card;
1389 struct snd_pcm_runtime *runtime;
1390 struct list_head *pos;
1393 struct drain_rec *drec, drec_tmp, *d;
1395 snd_assert(substream != NULL, return -ENXIO);
1396 card = substream->pcm->card;
1397 runtime = substream->runtime;
1399 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1402 snd_power_lock(card);
1403 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1404 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1406 snd_power_unlock(card);
1411 /* allocate temporary record for drain sync */
1412 down_read(&snd_pcm_link_rwsem);
1413 if (snd_pcm_stream_linked(substream)) {
1414 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1416 up_read(&snd_pcm_link_rwsem);
1417 snd_power_unlock(card);
1423 /* count only playback streams */
1425 snd_pcm_group_for_each(pos, substream) {
1426 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1427 runtime = s->runtime;
1428 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1429 d = &drec[num_drecs++];
1431 init_waitqueue_entry(&d->wait, current);
1432 add_wait_queue(&runtime->sleep, &d->wait);
1433 /* stop_threshold fixup to avoid endless loop when
1434 * stop_threshold > buffer_size
1436 d->stop_threshold = runtime->stop_threshold;
1437 if (runtime->stop_threshold > runtime->buffer_size)
1438 runtime->stop_threshold = runtime->buffer_size;
1441 up_read(&snd_pcm_link_rwsem);
1445 snd_pcm_stream_lock_irq(substream);
1447 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1448 snd_pcm_pause(substream, 0);
1450 /* pre-start/stop - all running streams are changed to DRAINING state */
1451 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1453 snd_pcm_stream_unlock_irq(substream);
1459 if (signal_pending(current)) {
1460 result = -ERESTARTSYS;
1464 for (i = 0; i < num_drecs; i++) {
1465 runtime = drec[i].substream->runtime;
1466 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1470 break; /* yes, all drained */
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 snd_pcm_stream_unlock_irq(substream);
1474 snd_power_unlock(card);
1475 tout = schedule_timeout(10 * HZ);
1476 snd_power_lock(card);
1477 snd_pcm_stream_lock_irq(substream);
1479 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1482 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1483 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1490 snd_pcm_stream_unlock_irq(substream);
1493 for (i = 0; i < num_drecs; i++) {
1495 runtime = d->substream->runtime;
1496 remove_wait_queue(&runtime->sleep, &d->wait);
1497 runtime->stop_threshold = d->stop_threshold;
1500 if (drec != &drec_tmp)
1502 snd_power_unlock(card);
1510 * Immediately put all linked substreams into SETUP state.
1512 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1514 struct snd_pcm_runtime *runtime;
1515 struct snd_card *card;
1518 snd_assert(substream != NULL, return -ENXIO);
1519 runtime = substream->runtime;
1520 card = substream->pcm->card;
1522 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1525 snd_power_lock(card);
1526 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1527 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1532 snd_pcm_stream_lock_irq(substream);
1534 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1535 snd_pcm_pause(substream, 0);
1537 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1538 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1539 snd_pcm_stream_unlock_irq(substream);
1541 snd_power_unlock(card);
1546 /* WARNING: Don't forget to fput back the file */
1547 static struct file *snd_pcm_file_fd(int fd)
1550 struct inode *inode;
1551 unsigned short minor;
1555 inode = file->f_dentry->d_inode;
1556 if (!S_ISCHR(inode->i_mode) ||
1557 imajor(inode) != snd_major) {
1561 minor = iminor(inode);
1563 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1573 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1577 struct snd_pcm_file *pcm_file;
1578 struct snd_pcm_substream *substream1;
1580 file = snd_pcm_file_fd(fd);
1583 pcm_file = file->private_data;
1584 substream1 = pcm_file->substream;
1585 down_write(&snd_pcm_link_rwsem);
1586 write_lock_irq(&snd_pcm_link_rwlock);
1587 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1588 substream->runtime->status->state != substream1->runtime->status->state) {
1592 if (snd_pcm_stream_linked(substream1)) {
1596 if (!snd_pcm_stream_linked(substream)) {
1597 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1598 if (substream->group == NULL) {
1602 spin_lock_init(&substream->group->lock);
1603 INIT_LIST_HEAD(&substream->group->substreams);
1604 list_add_tail(&substream->link_list, &substream->group->substreams);
1605 substream->group->count = 1;
1607 list_add_tail(&substream1->link_list, &substream->group->substreams);
1608 substream->group->count++;
1609 substream1->group = substream->group;
1611 write_unlock_irq(&snd_pcm_link_rwlock);
1612 up_write(&snd_pcm_link_rwsem);
1617 static void relink_to_local(struct snd_pcm_substream *substream)
1619 substream->group = &substream->self_group;
1620 INIT_LIST_HEAD(&substream->self_group.substreams);
1621 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1624 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1626 struct list_head *pos;
1629 down_write(&snd_pcm_link_rwsem);
1630 write_lock_irq(&snd_pcm_link_rwlock);
1631 if (!snd_pcm_stream_linked(substream)) {
1635 list_del(&substream->link_list);
1636 substream->group->count--;
1637 if (substream->group->count == 1) { /* detach the last stream, too */
1638 snd_pcm_group_for_each(pos, substream) {
1639 relink_to_local(snd_pcm_group_substream_entry(pos));
1642 kfree(substream->group);
1644 relink_to_local(substream);
1646 write_unlock_irq(&snd_pcm_link_rwlock);
1647 up_write(&snd_pcm_link_rwsem);
1654 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1655 struct snd_pcm_hw_rule *rule)
1657 struct snd_interval t;
1658 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1659 hw_param_interval_c(params, rule->deps[1]), &t);
1660 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1663 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1664 struct snd_pcm_hw_rule *rule)
1666 struct snd_interval t;
1667 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1668 hw_param_interval_c(params, rule->deps[1]), &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1672 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
1675 struct snd_interval t;
1676 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1677 hw_param_interval_c(params, rule->deps[1]),
1678 (unsigned long) rule->private, &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1682 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1685 struct snd_interval t;
1686 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1687 (unsigned long) rule->private,
1688 hw_param_interval_c(params, rule->deps[1]), &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1692 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
1696 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1698 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1700 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1702 if (! snd_mask_test(mask, k))
1704 bits = snd_pcm_format_physical_width(k);
1706 continue; /* ignore invalid formats */
1707 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1708 snd_mask_reset(&m, k);
1710 return snd_mask_refine(mask, &m);
1713 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1714 struct snd_pcm_hw_rule *rule)
1716 struct snd_interval t;
1722 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1724 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1726 bits = snd_pcm_format_physical_width(k);
1728 continue; /* ignore invalid formats */
1729 if (t.min > (unsigned)bits)
1731 if (t.max < (unsigned)bits)
1735 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1738 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1739 #error "Change this table"
1742 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1743 48000, 64000, 88200, 96000, 176400, 192000 };
1745 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1746 struct snd_pcm_hw_rule *rule)
1748 struct snd_pcm_hardware *hw = rule->private;
1749 return snd_interval_list(hw_param_interval(params, rule->var),
1750 ARRAY_SIZE(rates), rates, hw->rates);
1753 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
1756 struct snd_interval t;
1757 struct snd_pcm_substream *substream = rule->private;
1759 t.max = substream->buffer_bytes_max;
1763 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1766 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1768 struct snd_pcm_runtime *runtime = substream->runtime;
1769 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1772 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1773 snd_mask_any(constrs_mask(constrs, k));
1776 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1777 snd_interval_any(constrs_interval(constrs, k));
1780 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1781 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1782 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1783 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1786 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1787 snd_pcm_hw_rule_format, NULL,
1788 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1791 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1792 snd_pcm_hw_rule_sample_bits, NULL,
1793 SNDRV_PCM_HW_PARAM_FORMAT,
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_div, NULL,
1799 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1802 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1803 snd_pcm_hw_rule_mul, NULL,
1804 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1808 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1809 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1812 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1813 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1814 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1818 snd_pcm_hw_rule_div, NULL,
1819 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1823 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1824 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1828 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1829 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1833 snd_pcm_hw_rule_div, NULL,
1834 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1838 snd_pcm_hw_rule_div, NULL,
1839 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1843 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1844 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1848 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1849 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1853 snd_pcm_hw_rule_mul, NULL,
1854 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1859 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1863 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1864 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1868 snd_pcm_hw_rule_muldivk, (void*) 8,
1869 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1873 snd_pcm_hw_rule_muldivk, (void*) 8,
1874 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1878 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1879 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1883 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1890 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1892 struct snd_pcm_runtime *runtime = substream->runtime;
1893 struct snd_pcm_hardware *hw = &runtime->hw;
1895 unsigned int mask = 0;
1897 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1898 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1899 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1900 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1901 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1902 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1903 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1904 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1905 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1906 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1907 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1909 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1910 snd_assert(err >= 0, return -EINVAL);
1912 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1913 snd_assert(err >= 0, return -EINVAL);
1915 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1916 snd_assert(err >= 0, return -EINVAL);
1918 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1919 hw->channels_min, hw->channels_max);
1920 snd_assert(err >= 0, return -EINVAL);
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1923 hw->rate_min, hw->rate_max);
1924 snd_assert(err >= 0, return -EINVAL);
1926 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1927 hw->period_bytes_min, hw->period_bytes_max);
1928 snd_assert(err >= 0, return -EINVAL);
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1931 hw->periods_min, hw->periods_max);
1932 snd_assert(err >= 0, return -EINVAL);
1934 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1935 hw->period_bytes_min, hw->buffer_bytes_max);
1936 snd_assert(err >= 0, return -EINVAL);
1938 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1939 snd_pcm_hw_rule_buffer_bytes_max, substream,
1940 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1945 if (runtime->dma_bytes) {
1946 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1947 snd_assert(err >= 0, return -EINVAL);
1950 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1951 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1952 snd_pcm_hw_rule_rate, hw,
1953 SNDRV_PCM_HW_PARAM_RATE, -1);
1958 /* FIXME: this belong to lowlevel */
1959 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1960 1000000 / HZ, 1000000 / HZ);
1961 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1966 static void snd_pcm_add_file(struct snd_pcm_str *str,
1967 struct snd_pcm_file *pcm_file)
1969 pcm_file->next = str->files;
1970 str->files = pcm_file;
1973 static void snd_pcm_remove_file(struct snd_pcm_str *str,
1974 struct snd_pcm_file *pcm_file)
1976 struct snd_pcm_file * pcm_file1;
1977 if (str->files == pcm_file) {
1978 str->files = pcm_file->next;
1980 pcm_file1 = str->files;
1981 while (pcm_file1 && pcm_file1->next != pcm_file)
1982 pcm_file1 = pcm_file1->next;
1983 if (pcm_file1 != NULL)
1984 pcm_file1->next = pcm_file->next;
1988 static int snd_pcm_release_file(struct snd_pcm_file * pcm_file)
1990 struct snd_pcm_substream *substream;
1991 struct snd_pcm_runtime *runtime;
1992 struct snd_pcm_str * str;
1994 snd_assert(pcm_file != NULL, return -ENXIO);
1995 substream = pcm_file->substream;
1996 snd_assert(substream != NULL, return -ENXIO);
1997 runtime = substream->runtime;
1998 str = substream->pstr;
1999 snd_pcm_unlink(substream);
2000 if (substream->ffile != NULL) {
2001 if (substream->ops->hw_free != NULL)
2002 substream->ops->hw_free(substream);
2003 substream->ops->close(substream);
2004 substream->ffile = NULL;
2006 snd_pcm_remove_file(str, pcm_file);
2007 snd_pcm_release_substream(substream);
2012 static int snd_pcm_open_file(struct file *file,
2013 struct snd_pcm *pcm,
2015 struct snd_pcm_file **rpcm_file)
2018 struct snd_pcm_file *pcm_file;
2019 struct snd_pcm_substream *substream;
2020 struct snd_pcm_str *str;
2022 snd_assert(rpcm_file != NULL, return -EINVAL);
2025 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2026 if (pcm_file == NULL) {
2030 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2035 str = substream->pstr;
2036 substream->file = pcm_file;
2037 substream->no_mmap_ctrl = 0;
2039 pcm_file->substream = substream;
2041 snd_pcm_add_file(str, pcm_file);
2043 err = snd_pcm_hw_constraints_init(substream);
2045 snd_printd("snd_pcm_hw_constraints_init failed\n");
2046 snd_pcm_release_file(pcm_file);
2050 if ((err = substream->ops->open(substream)) < 0) {
2051 snd_pcm_release_file(pcm_file);
2054 substream->ffile = file;
2056 err = snd_pcm_hw_constraints_complete(substream);
2058 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2059 snd_pcm_release_file(pcm_file);
2063 file->private_data = pcm_file;
2064 *rpcm_file = pcm_file;
2068 static int snd_pcm_open(struct inode *inode, struct file *file)
2070 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2071 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2073 struct snd_pcm *pcm;
2074 struct snd_pcm_file *pcm_file;
2077 if (device < SNDRV_MINOR_PCM_PLAYBACK || device >= SNDRV_MINOR_DEVICES)
2079 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2084 err = snd_card_file_add(pcm->card, file);
2087 if (!try_module_get(pcm->card->module)) {
2091 init_waitqueue_entry(&wait, current);
2092 add_wait_queue(&pcm->open_wait, &wait);
2093 down(&pcm->open_mutex);
2095 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2098 if (err == -EAGAIN) {
2099 if (file->f_flags & O_NONBLOCK) {
2105 set_current_state(TASK_INTERRUPTIBLE);
2106 up(&pcm->open_mutex);
2108 down(&pcm->open_mutex);
2109 if (signal_pending(current)) {
2114 remove_wait_queue(&pcm->open_wait, &wait);
2115 up(&pcm->open_mutex);
2121 module_put(pcm->card->module);
2123 snd_card_file_remove(pcm->card, file);
2128 static int snd_pcm_release(struct inode *inode, struct file *file)
2130 struct snd_pcm *pcm;
2131 struct snd_pcm_substream *substream;
2132 struct snd_pcm_file *pcm_file;
2134 pcm_file = file->private_data;
2135 substream = pcm_file->substream;
2136 snd_assert(substream != NULL, return -ENXIO);
2137 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2138 pcm = substream->pcm;
2139 snd_pcm_drop(substream);
2140 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2141 down(&pcm->open_mutex);
2142 snd_pcm_release_file(pcm_file);
2143 up(&pcm->open_mutex);
2144 wake_up(&pcm->open_wait);
2145 module_put(pcm->card->module);
2146 snd_card_file_remove(pcm->card, file);
2150 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2151 snd_pcm_uframes_t frames)
2153 struct snd_pcm_runtime *runtime = substream->runtime;
2154 snd_pcm_sframes_t appl_ptr;
2155 snd_pcm_sframes_t ret;
2156 snd_pcm_sframes_t hw_avail;
2161 snd_pcm_stream_lock_irq(substream);
2162 switch (runtime->status->state) {
2163 case SNDRV_PCM_STATE_PREPARED:
2165 case SNDRV_PCM_STATE_DRAINING:
2166 case SNDRV_PCM_STATE_RUNNING:
2167 if (snd_pcm_update_hw_ptr(substream) >= 0)
2170 case SNDRV_PCM_STATE_XRUN:
2178 hw_avail = snd_pcm_playback_hw_avail(runtime);
2179 if (hw_avail <= 0) {
2183 if (frames > (snd_pcm_uframes_t)hw_avail)
2186 frames -= frames % runtime->xfer_align;
2187 appl_ptr = runtime->control->appl_ptr - frames;
2189 appl_ptr += runtime->boundary;
2190 runtime->control->appl_ptr = appl_ptr;
2191 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2193 snd_pcm_tick_prepare(substream);
2196 snd_pcm_stream_unlock_irq(substream);
2200 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2201 snd_pcm_uframes_t frames)
2203 struct snd_pcm_runtime *runtime = substream->runtime;
2204 snd_pcm_sframes_t appl_ptr;
2205 snd_pcm_sframes_t ret;
2206 snd_pcm_sframes_t hw_avail;
2211 snd_pcm_stream_lock_irq(substream);
2212 switch (runtime->status->state) {
2213 case SNDRV_PCM_STATE_PREPARED:
2214 case SNDRV_PCM_STATE_DRAINING:
2216 case SNDRV_PCM_STATE_RUNNING:
2217 if (snd_pcm_update_hw_ptr(substream) >= 0)
2220 case SNDRV_PCM_STATE_XRUN:
2228 hw_avail = snd_pcm_capture_hw_avail(runtime);
2229 if (hw_avail <= 0) {
2233 if (frames > (snd_pcm_uframes_t)hw_avail)
2236 frames -= frames % runtime->xfer_align;
2237 appl_ptr = runtime->control->appl_ptr - frames;
2239 appl_ptr += runtime->boundary;
2240 runtime->control->appl_ptr = appl_ptr;
2241 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2243 snd_pcm_tick_prepare(substream);
2246 snd_pcm_stream_unlock_irq(substream);
2250 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2251 snd_pcm_uframes_t frames)
2253 struct snd_pcm_runtime *runtime = substream->runtime;
2254 snd_pcm_sframes_t appl_ptr;
2255 snd_pcm_sframes_t ret;
2256 snd_pcm_sframes_t avail;
2261 snd_pcm_stream_lock_irq(substream);
2262 switch (runtime->status->state) {
2263 case SNDRV_PCM_STATE_PREPARED:
2264 case SNDRV_PCM_STATE_PAUSED:
2266 case SNDRV_PCM_STATE_DRAINING:
2267 case SNDRV_PCM_STATE_RUNNING:
2268 if (snd_pcm_update_hw_ptr(substream) >= 0)
2271 case SNDRV_PCM_STATE_XRUN:
2279 avail = snd_pcm_playback_avail(runtime);
2284 if (frames > (snd_pcm_uframes_t)avail)
2287 frames -= frames % runtime->xfer_align;
2288 appl_ptr = runtime->control->appl_ptr + frames;
2289 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2290 appl_ptr -= runtime->boundary;
2291 runtime->control->appl_ptr = appl_ptr;
2292 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2294 snd_pcm_tick_prepare(substream);
2297 snd_pcm_stream_unlock_irq(substream);
2301 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2302 snd_pcm_uframes_t frames)
2304 struct snd_pcm_runtime *runtime = substream->runtime;
2305 snd_pcm_sframes_t appl_ptr;
2306 snd_pcm_sframes_t ret;
2307 snd_pcm_sframes_t avail;
2312 snd_pcm_stream_lock_irq(substream);
2313 switch (runtime->status->state) {
2314 case SNDRV_PCM_STATE_PREPARED:
2315 case SNDRV_PCM_STATE_DRAINING:
2316 case SNDRV_PCM_STATE_PAUSED:
2318 case SNDRV_PCM_STATE_RUNNING:
2319 if (snd_pcm_update_hw_ptr(substream) >= 0)
2322 case SNDRV_PCM_STATE_XRUN:
2330 avail = snd_pcm_capture_avail(runtime);
2335 if (frames > (snd_pcm_uframes_t)avail)
2338 frames -= frames % runtime->xfer_align;
2339 appl_ptr = runtime->control->appl_ptr + frames;
2340 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2341 appl_ptr -= runtime->boundary;
2342 runtime->control->appl_ptr = appl_ptr;
2343 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2345 snd_pcm_tick_prepare(substream);
2348 snd_pcm_stream_unlock_irq(substream);
2352 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2354 struct snd_pcm_runtime *runtime = substream->runtime;
2357 snd_pcm_stream_lock_irq(substream);
2358 switch (runtime->status->state) {
2359 case SNDRV_PCM_STATE_DRAINING:
2360 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2362 case SNDRV_PCM_STATE_RUNNING:
2363 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2366 case SNDRV_PCM_STATE_PREPARED:
2367 case SNDRV_PCM_STATE_SUSPENDED:
2370 case SNDRV_PCM_STATE_XRUN:
2378 snd_pcm_stream_unlock_irq(substream);
2382 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2383 snd_pcm_sframes_t __user *res)
2385 struct snd_pcm_runtime *runtime = substream->runtime;
2387 snd_pcm_sframes_t n = 0;
2389 snd_pcm_stream_lock_irq(substream);
2390 switch (runtime->status->state) {
2391 case SNDRV_PCM_STATE_DRAINING:
2392 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2394 case SNDRV_PCM_STATE_RUNNING:
2395 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2398 case SNDRV_PCM_STATE_PREPARED:
2399 case SNDRV_PCM_STATE_SUSPENDED:
2401 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2402 n = snd_pcm_playback_hw_avail(runtime);
2404 n = snd_pcm_capture_avail(runtime);
2406 case SNDRV_PCM_STATE_XRUN:
2414 snd_pcm_stream_unlock_irq(substream);
2416 if (put_user(n, res))
2421 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2422 struct snd_pcm_sync_ptr __user *_sync_ptr)
2424 struct snd_pcm_runtime *runtime = substream->runtime;
2425 struct snd_pcm_sync_ptr sync_ptr;
2426 volatile struct snd_pcm_mmap_status *status;
2427 volatile struct snd_pcm_mmap_control *control;
2430 memset(&sync_ptr, 0, sizeof(sync_ptr));
2431 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2433 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2435 status = runtime->status;
2436 control = runtime->control;
2437 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2438 err = snd_pcm_hwsync(substream);
2442 snd_pcm_stream_lock_irq(substream);
2443 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2444 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2446 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2447 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2448 control->avail_min = sync_ptr.c.control.avail_min;
2450 sync_ptr.c.control.avail_min = control->avail_min;
2451 sync_ptr.s.status.state = status->state;
2452 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2453 sync_ptr.s.status.tstamp = status->tstamp;
2454 sync_ptr.s.status.suspended_state = status->suspended_state;
2455 snd_pcm_stream_unlock_irq(substream);
2456 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2461 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2462 unsigned int cmd, void __user *arg);
2463 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2464 unsigned int cmd, void __user *arg);
2466 static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
2467 unsigned int cmd, void __user *arg)
2469 snd_assert(substream != NULL, return -ENXIO);
2472 case SNDRV_PCM_IOCTL_PVERSION:
2473 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2474 case SNDRV_PCM_IOCTL_INFO:
2475 return snd_pcm_info_user(substream, arg);
2476 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2478 case SNDRV_PCM_IOCTL_HW_REFINE:
2479 return snd_pcm_hw_refine_user(substream, arg);
2480 case SNDRV_PCM_IOCTL_HW_PARAMS:
2481 return snd_pcm_hw_params_user(substream, arg);
2482 case SNDRV_PCM_IOCTL_HW_FREE:
2483 return snd_pcm_hw_free(substream);
2484 case SNDRV_PCM_IOCTL_SW_PARAMS:
2485 return snd_pcm_sw_params_user(substream, arg);
2486 case SNDRV_PCM_IOCTL_STATUS:
2487 return snd_pcm_status_user(substream, arg);
2488 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2489 return snd_pcm_channel_info_user(substream, arg);
2490 case SNDRV_PCM_IOCTL_PREPARE:
2491 return snd_pcm_prepare(substream);
2492 case SNDRV_PCM_IOCTL_RESET:
2493 return snd_pcm_reset(substream);
2494 case SNDRV_PCM_IOCTL_START:
2495 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2496 case SNDRV_PCM_IOCTL_LINK:
2497 return snd_pcm_link(substream, (int)(unsigned long) arg);
2498 case SNDRV_PCM_IOCTL_UNLINK:
2499 return snd_pcm_unlink(substream);
2500 case SNDRV_PCM_IOCTL_RESUME:
2501 return snd_pcm_resume(substream);
2502 case SNDRV_PCM_IOCTL_XRUN:
2503 return snd_pcm_xrun(substream);
2504 case SNDRV_PCM_IOCTL_HWSYNC:
2505 return snd_pcm_hwsync(substream);
2506 case SNDRV_PCM_IOCTL_DELAY:
2507 return snd_pcm_delay(substream, arg);
2508 case SNDRV_PCM_IOCTL_SYNC_PTR:
2509 return snd_pcm_sync_ptr(substream, arg);
2510 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2511 return snd_pcm_hw_refine_old_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2513 return snd_pcm_hw_params_old_user(substream, arg);
2514 case SNDRV_PCM_IOCTL_DRAIN:
2515 return snd_pcm_drain(substream);
2516 case SNDRV_PCM_IOCTL_DROP:
2517 return snd_pcm_drop(substream);
2519 snd_printd("unknown ioctl = 0x%x\n", cmd);
2523 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2524 unsigned int cmd, void __user *arg)
2526 snd_assert(substream != NULL, return -ENXIO);
2527 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2529 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2531 struct snd_xferi xferi;
2532 struct snd_xferi __user *_xferi = arg;
2533 struct snd_pcm_runtime *runtime = substream->runtime;
2534 snd_pcm_sframes_t result;
2535 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2537 if (put_user(0, &_xferi->result))
2539 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2541 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2542 __put_user(result, &_xferi->result);
2543 return result < 0 ? result : 0;
2545 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2547 struct snd_xfern xfern;
2548 struct snd_xfern __user *_xfern = arg;
2549 struct snd_pcm_runtime *runtime = substream->runtime;
2551 snd_pcm_sframes_t result;
2552 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2554 if (runtime->channels > 128)
2556 if (put_user(0, &_xfern->result))
2558 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2560 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2563 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2567 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2569 __put_user(result, &_xfern->result);
2570 return result < 0 ? result : 0;
2572 case SNDRV_PCM_IOCTL_REWIND:
2574 snd_pcm_uframes_t frames;
2575 snd_pcm_uframes_t __user *_frames = arg;
2576 snd_pcm_sframes_t result;
2577 if (get_user(frames, _frames))
2579 if (put_user(0, _frames))
2581 result = snd_pcm_playback_rewind(substream, frames);
2582 __put_user(result, _frames);
2583 return result < 0 ? result : 0;
2585 case SNDRV_PCM_IOCTL_FORWARD:
2587 snd_pcm_uframes_t frames;
2588 snd_pcm_uframes_t __user *_frames = arg;
2589 snd_pcm_sframes_t result;
2590 if (get_user(frames, _frames))
2592 if (put_user(0, _frames))
2594 result = snd_pcm_playback_forward(substream, frames);
2595 __put_user(result, _frames);
2596 return result < 0 ? result : 0;
2598 case SNDRV_PCM_IOCTL_PAUSE:
2601 snd_pcm_stream_lock_irq(substream);
2602 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2603 snd_pcm_stream_unlock_irq(substream);
2607 return snd_pcm_common_ioctl1(substream, cmd, arg);
2610 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2611 unsigned int cmd, void __user *arg)
2613 snd_assert(substream != NULL, return -ENXIO);
2614 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2616 case SNDRV_PCM_IOCTL_READI_FRAMES:
2618 struct snd_xferi xferi;
2619 struct snd_xferi __user *_xferi = arg;
2620 struct snd_pcm_runtime *runtime = substream->runtime;
2621 snd_pcm_sframes_t result;
2622 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2624 if (put_user(0, &_xferi->result))
2626 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2628 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2629 __put_user(result, &_xferi->result);
2630 return result < 0 ? result : 0;
2632 case SNDRV_PCM_IOCTL_READN_FRAMES:
2634 struct snd_xfern xfern;
2635 struct snd_xfern __user *_xfern = arg;
2636 struct snd_pcm_runtime *runtime = substream->runtime;
2638 snd_pcm_sframes_t result;
2639 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2641 if (runtime->channels > 128)
2643 if (put_user(0, &_xfern->result))
2645 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2647 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2650 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2654 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2656 __put_user(result, &_xfern->result);
2657 return result < 0 ? result : 0;
2659 case SNDRV_PCM_IOCTL_REWIND:
2661 snd_pcm_uframes_t frames;
2662 snd_pcm_uframes_t __user *_frames = arg;
2663 snd_pcm_sframes_t result;
2664 if (get_user(frames, _frames))
2666 if (put_user(0, _frames))
2668 result = snd_pcm_capture_rewind(substream, frames);
2669 __put_user(result, _frames);
2670 return result < 0 ? result : 0;
2672 case SNDRV_PCM_IOCTL_FORWARD:
2674 snd_pcm_uframes_t frames;
2675 snd_pcm_uframes_t __user *_frames = arg;
2676 snd_pcm_sframes_t result;
2677 if (get_user(frames, _frames))
2679 if (put_user(0, _frames))
2681 result = snd_pcm_capture_forward(substream, frames);
2682 __put_user(result, _frames);
2683 return result < 0 ? result : 0;
2686 return snd_pcm_common_ioctl1(substream, cmd, arg);
2689 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2692 struct snd_pcm_file *pcm_file;
2694 pcm_file = file->private_data;
2696 if (((cmd >> 8) & 0xff) != 'A')
2699 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2702 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2705 struct snd_pcm_file *pcm_file;
2707 pcm_file = file->private_data;
2709 if (((cmd >> 8) & 0xff) != 'A')
2712 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2715 int snd_pcm_kernel_playback_ioctl(struct snd_pcm_substream *substream,
2716 unsigned int cmd, void *arg)
2721 fs = snd_enter_user();
2722 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2727 int snd_pcm_kernel_capture_ioctl(struct snd_pcm_substream *substream,
2728 unsigned int cmd, void *arg)
2733 fs = snd_enter_user();
2734 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2739 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2740 unsigned int cmd, void *arg)
2742 switch (substream->stream) {
2743 case SNDRV_PCM_STREAM_PLAYBACK:
2744 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2745 case SNDRV_PCM_STREAM_CAPTURE:
2746 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2752 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2755 struct snd_pcm_file *pcm_file;
2756 struct snd_pcm_substream *substream;
2757 struct snd_pcm_runtime *runtime;
2758 snd_pcm_sframes_t result;
2760 pcm_file = file->private_data;
2761 substream = pcm_file->substream;
2762 snd_assert(substream != NULL, return -ENXIO);
2763 runtime = substream->runtime;
2764 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2766 if (!frame_aligned(runtime, count))
2768 count = bytes_to_frames(runtime, count);
2769 result = snd_pcm_lib_read(substream, buf, count);
2771 result = frames_to_bytes(runtime, result);
2775 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2776 size_t count, loff_t * offset)
2778 struct snd_pcm_file *pcm_file;
2779 struct snd_pcm_substream *substream;
2780 struct snd_pcm_runtime *runtime;
2781 snd_pcm_sframes_t result;
2783 pcm_file = file->private_data;
2784 substream = pcm_file->substream;
2785 snd_assert(substream != NULL, result = -ENXIO; goto end);
2786 runtime = substream->runtime;
2787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2791 if (!frame_aligned(runtime, count)) {
2795 count = bytes_to_frames(runtime, count);
2796 result = snd_pcm_lib_write(substream, buf, count);
2798 result = frames_to_bytes(runtime, result);
2803 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2804 unsigned long count, loff_t * offset)
2807 struct snd_pcm_file *pcm_file;
2808 struct snd_pcm_substream *substream;
2809 struct snd_pcm_runtime *runtime;
2810 snd_pcm_sframes_t result;
2813 snd_pcm_uframes_t frames;
2815 pcm_file = file->private_data;
2816 substream = pcm_file->substream;
2817 snd_assert(substream != NULL, return -ENXIO);
2818 runtime = substream->runtime;
2819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 if (count > 1024 || count != runtime->channels)
2823 if (!frame_aligned(runtime, _vector->iov_len))
2825 frames = bytes_to_samples(runtime, _vector->iov_len);
2826 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2829 for (i = 0; i < count; ++i)
2830 bufs[i] = _vector[i].iov_base;
2831 result = snd_pcm_lib_readv(substream, bufs, frames);
2833 result = frames_to_bytes(runtime, result);
2838 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2839 unsigned long count, loff_t * offset)
2841 struct snd_pcm_file *pcm_file;
2842 struct snd_pcm_substream *substream;
2843 struct snd_pcm_runtime *runtime;
2844 snd_pcm_sframes_t result;
2847 snd_pcm_uframes_t frames;
2849 pcm_file = file->private_data;
2850 substream = pcm_file->substream;
2851 snd_assert(substream != NULL, result = -ENXIO; goto end);
2852 runtime = substream->runtime;
2853 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2857 if (count > 128 || count != runtime->channels ||
2858 !frame_aligned(runtime, _vector->iov_len)) {
2862 frames = bytes_to_samples(runtime, _vector->iov_len);
2863 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2866 for (i = 0; i < count; ++i)
2867 bufs[i] = _vector[i].iov_base;
2868 result = snd_pcm_lib_writev(substream, bufs, frames);
2870 result = frames_to_bytes(runtime, result);
2876 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2878 struct snd_pcm_file *pcm_file;
2879 struct snd_pcm_substream *substream;
2880 struct snd_pcm_runtime *runtime;
2882 snd_pcm_uframes_t avail;
2884 pcm_file = file->private_data;
2886 substream = pcm_file->substream;
2887 snd_assert(substream != NULL, return -ENXIO);
2888 runtime = substream->runtime;
2890 poll_wait(file, &runtime->sleep, wait);
2892 snd_pcm_stream_lock_irq(substream);
2893 avail = snd_pcm_playback_avail(runtime);
2894 switch (runtime->status->state) {
2895 case SNDRV_PCM_STATE_RUNNING:
2896 case SNDRV_PCM_STATE_PREPARED:
2897 case SNDRV_PCM_STATE_PAUSED:
2898 if (avail >= runtime->control->avail_min) {
2899 mask = POLLOUT | POLLWRNORM;
2903 case SNDRV_PCM_STATE_DRAINING:
2907 mask = POLLOUT | POLLWRNORM | POLLERR;
2910 snd_pcm_stream_unlock_irq(substream);
2914 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2916 struct snd_pcm_file *pcm_file;
2917 struct snd_pcm_substream *substream;
2918 struct snd_pcm_runtime *runtime;
2920 snd_pcm_uframes_t avail;
2922 pcm_file = file->private_data;
2924 substream = pcm_file->substream;
2925 snd_assert(substream != NULL, return -ENXIO);
2926 runtime = substream->runtime;
2928 poll_wait(file, &runtime->sleep, wait);
2930 snd_pcm_stream_lock_irq(substream);
2931 avail = snd_pcm_capture_avail(runtime);
2932 switch (runtime->status->state) {
2933 case SNDRV_PCM_STATE_RUNNING:
2934 case SNDRV_PCM_STATE_PREPARED:
2935 case SNDRV_PCM_STATE_PAUSED:
2936 if (avail >= runtime->control->avail_min) {
2937 mask = POLLIN | POLLRDNORM;
2942 case SNDRV_PCM_STATE_DRAINING:
2944 mask = POLLIN | POLLRDNORM;
2949 mask = POLLIN | POLLRDNORM | POLLERR;
2952 snd_pcm_stream_unlock_irq(substream);
2961 * Only on coherent architectures, we can mmap the status and the control records
2962 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2964 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2966 * mmap status record
2968 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2969 unsigned long address, int *type)
2971 struct snd_pcm_substream *substream = area->vm_private_data;
2972 struct snd_pcm_runtime *runtime;
2975 if (substream == NULL)
2977 runtime = substream->runtime;
2978 page = virt_to_page(runtime->status);
2981 *type = VM_FAULT_MINOR;
2985 static struct vm_operations_struct snd_pcm_vm_ops_status =
2987 .nopage = snd_pcm_mmap_status_nopage,
2990 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
2991 struct vm_area_struct *area)
2993 struct snd_pcm_runtime *runtime;
2995 if (!(area->vm_flags & VM_READ))
2997 runtime = substream->runtime;
2998 snd_assert(runtime != NULL, return -EAGAIN);
2999 size = area->vm_end - area->vm_start;
3000 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3002 area->vm_ops = &snd_pcm_vm_ops_status;
3003 area->vm_private_data = substream;
3004 area->vm_flags |= VM_RESERVED;
3009 * mmap control record
3011 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3012 unsigned long address, int *type)
3014 struct snd_pcm_substream *substream = area->vm_private_data;
3015 struct snd_pcm_runtime *runtime;
3018 if (substream == NULL)
3020 runtime = substream->runtime;
3021 page = virt_to_page(runtime->control);
3024 *type = VM_FAULT_MINOR;
3028 static struct vm_operations_struct snd_pcm_vm_ops_control =
3030 .nopage = snd_pcm_mmap_control_nopage,
3033 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3034 struct vm_area_struct *area)
3036 struct snd_pcm_runtime *runtime;
3038 if (!(area->vm_flags & VM_READ))
3040 runtime = substream->runtime;
3041 snd_assert(runtime != NULL, return -EAGAIN);
3042 size = area->vm_end - area->vm_start;
3043 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3045 area->vm_ops = &snd_pcm_vm_ops_control;
3046 area->vm_private_data = substream;
3047 area->vm_flags |= VM_RESERVED;
3050 #else /* ! coherent mmap */
3052 * don't support mmap for status and control records.
3054 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3055 struct vm_area_struct *area)
3059 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3060 struct vm_area_struct *area)
3064 #endif /* coherent mmap */
3067 * nopage callback for mmapping a RAM page
3069 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3070 unsigned long address, int *type)
3072 struct snd_pcm_substream *substream = area->vm_private_data;
3073 struct snd_pcm_runtime *runtime;
3074 unsigned long offset;
3079 if (substream == NULL)
3081 runtime = substream->runtime;
3082 offset = area->vm_pgoff << PAGE_SHIFT;
3083 offset += address - area->vm_start;
3084 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3085 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3086 if (offset > dma_bytes - PAGE_SIZE)
3087 return NOPAGE_SIGBUS;
3088 if (substream->ops->page) {
3089 page = substream->ops->page(substream, offset);
3093 vaddr = runtime->dma_area + offset;
3094 page = virt_to_page(vaddr);
3098 *type = VM_FAULT_MINOR;
3102 static struct vm_operations_struct snd_pcm_vm_ops_data =
3104 .open = snd_pcm_mmap_data_open,
3105 .close = snd_pcm_mmap_data_close,
3106 .nopage = snd_pcm_mmap_data_nopage,
3110 * mmap the DMA buffer on RAM
3112 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3113 struct vm_area_struct *area)
3115 area->vm_ops = &snd_pcm_vm_ops_data;
3116 area->vm_private_data = substream;
3117 area->vm_flags |= VM_RESERVED;
3118 atomic_inc(&substream->runtime->mmap_count);
3123 * mmap the DMA buffer on I/O memory area
3125 #if SNDRV_PCM_INFO_MMAP_IOMEM
3126 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3128 .open = snd_pcm_mmap_data_open,
3129 .close = snd_pcm_mmap_data_close,
3132 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3133 struct vm_area_struct *area)
3136 unsigned long offset;
3138 #ifdef pgprot_noncached
3139 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3141 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3142 area->vm_private_data = substream;
3143 area->vm_flags |= VM_IO;
3144 size = area->vm_end - area->vm_start;
3145 offset = area->vm_pgoff << PAGE_SHIFT;
3146 if (io_remap_pfn_range(area, area->vm_start,
3147 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3148 size, area->vm_page_prot))
3150 atomic_inc(&substream->runtime->mmap_count);
3153 #endif /* SNDRV_PCM_INFO_MMAP */
3158 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3159 struct vm_area_struct *area)
3161 struct snd_pcm_runtime *runtime;
3163 unsigned long offset;
3166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3167 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3170 if (!(area->vm_flags & VM_READ))
3173 runtime = substream->runtime;
3174 snd_assert(runtime != NULL, return -EAGAIN);
3175 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3177 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3179 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3180 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3182 size = area->vm_end - area->vm_start;
3183 offset = area->vm_pgoff << PAGE_SHIFT;
3184 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3185 if ((size_t)size > dma_bytes)
3187 if (offset > dma_bytes - size)
3190 if (substream->ops->mmap)
3191 return substream->ops->mmap(substream, area);
3193 return snd_pcm_default_mmap(substream, area);
3196 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3198 struct snd_pcm_file * pcm_file;
3199 struct snd_pcm_substream *substream;
3200 unsigned long offset;
3202 pcm_file = file->private_data;
3203 substream = pcm_file->substream;
3204 snd_assert(substream != NULL, return -ENXIO);
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3208 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3209 if (substream->no_mmap_ctrl)
3211 return snd_pcm_mmap_status(substream, file, area);
3212 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3213 if (substream->no_mmap_ctrl)
3215 return snd_pcm_mmap_control(substream, file, area);
3217 return snd_pcm_mmap_data(substream, file, area);
3222 static int snd_pcm_fasync(int fd, struct file * file, int on)
3224 struct snd_pcm_file * pcm_file;
3225 struct snd_pcm_substream *substream;
3226 struct snd_pcm_runtime *runtime;
3229 pcm_file = file->private_data;
3230 substream = pcm_file->substream;
3231 snd_assert(substream != NULL, return -ENXIO);
3232 runtime = substream->runtime;
3234 err = fasync_helper(fd, file, on, &runtime->fasync);
3243 #ifdef CONFIG_COMPAT
3244 #include "pcm_compat.c"
3246 #define snd_pcm_ioctl_compat NULL
3250 * To be removed helpers to keep binary compatibility
3253 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3254 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3256 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3257 struct snd_pcm_hw_params_old *oparams)
3261 memset(params, 0, sizeof(*params));
3262 params->flags = oparams->flags;
3263 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3264 params->masks[i].bits[0] = oparams->masks[i];
3265 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3266 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3267 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3268 params->info = oparams->info;
3269 params->msbits = oparams->msbits;
3270 params->rate_num = oparams->rate_num;
3271 params->rate_den = oparams->rate_den;
3272 params->fifo_size = oparams->fifo_size;
3275 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3276 struct snd_pcm_hw_params *params)
3280 memset(oparams, 0, sizeof(*oparams));
3281 oparams->flags = params->flags;
3282 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3283 oparams->masks[i] = params->masks[i].bits[0];
3284 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3285 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3286 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3287 oparams->info = params->info;
3288 oparams->msbits = params->msbits;
3289 oparams->rate_num = params->rate_num;
3290 oparams->rate_den = params->rate_den;
3291 oparams->fifo_size = params->fifo_size;
3294 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3295 struct snd_pcm_hw_params_old __user * _oparams)
3297 struct snd_pcm_hw_params *params;
3298 struct snd_pcm_hw_params_old *oparams = NULL;
3301 params = kmalloc(sizeof(*params), GFP_KERNEL);
3306 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3312 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3316 snd_pcm_hw_convert_from_old_params(params, oparams);
3317 err = snd_pcm_hw_refine(substream, params);
3318 snd_pcm_hw_convert_to_old_params(oparams, params);
3319 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3329 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3330 struct snd_pcm_hw_params_old __user * _oparams)
3332 struct snd_pcm_hw_params *params;
3333 struct snd_pcm_hw_params_old *oparams = NULL;
3336 params = kmalloc(sizeof(*params), GFP_KERNEL);
3341 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3346 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3350 snd_pcm_hw_convert_from_old_params(params, oparams);
3351 err = snd_pcm_hw_params(substream, params);
3352 snd_pcm_hw_convert_to_old_params(oparams, params);
3353 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3367 static struct file_operations snd_pcm_f_ops_playback = {
3368 .owner = THIS_MODULE,
3369 .write = snd_pcm_write,
3370 .writev = snd_pcm_writev,
3371 .open = snd_pcm_open,
3372 .release = snd_pcm_release,
3373 .poll = snd_pcm_playback_poll,
3374 .unlocked_ioctl = snd_pcm_playback_ioctl,
3375 .compat_ioctl = snd_pcm_ioctl_compat,
3376 .mmap = snd_pcm_mmap,
3377 .fasync = snd_pcm_fasync,
3380 static struct file_operations snd_pcm_f_ops_capture = {
3381 .owner = THIS_MODULE,
3382 .read = snd_pcm_read,
3383 .readv = snd_pcm_readv,
3384 .open = snd_pcm_open,
3385 .release = snd_pcm_release,
3386 .poll = snd_pcm_capture_poll,
3387 .unlocked_ioctl = snd_pcm_capture_ioctl,
3388 .compat_ioctl = snd_pcm_ioctl_compat,
3389 .mmap = snd_pcm_mmap,
3390 .fasync = snd_pcm_fasync,
3393 struct snd_minor snd_pcm_reg[2] =
3396 .comment = "digital audio playback",
3397 .f_ops = &snd_pcm_f_ops_playback,
3400 .comment = "digital audio capture",
3401 .f_ops = &snd_pcm_f_ops_capture,