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);
65 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
71 DEFINE_RWLOCK(snd_pcm_link_rwlock);
72 static DECLARE_RWSEM(snd_pcm_link_rwsem);
75 static inline mm_segment_t snd_enter_user(void)
77 mm_segment_t fs = get_fs();
82 static inline void snd_leave_user(mm_segment_t fs)
89 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
91 struct snd_pcm_runtime *runtime;
92 struct snd_pcm *pcm = substream->pcm;
93 struct snd_pcm_str *pstr = substream->pstr;
95 snd_assert(substream != NULL, return -ENXIO);
96 memset(info, 0, sizeof(*info));
97 info->card = pcm->card->number;
98 info->device = pcm->device;
99 info->stream = substream->stream;
100 info->subdevice = substream->number;
101 strlcpy(info->id, pcm->id, sizeof(info->id));
102 strlcpy(info->name, pcm->name, sizeof(info->name));
103 info->dev_class = pcm->dev_class;
104 info->dev_subclass = pcm->dev_subclass;
105 info->subdevices_count = pstr->substream_count;
106 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
107 strlcpy(info->subname, substream->name, sizeof(info->subname));
108 runtime = substream->runtime;
109 /* AB: FIXME!!! This is definitely nonsense */
111 info->sync = runtime->sync;
112 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
117 int snd_pcm_info_user(struct snd_pcm_substream *substream,
118 struct snd_pcm_info __user * _info)
120 struct snd_pcm_info *info;
123 info = kmalloc(sizeof(*info), GFP_KERNEL);
126 err = snd_pcm_info(substream, info);
128 if (copy_to_user(_info, info, sizeof(*info)))
138 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
139 char *snd_pcm_hw_param_names[] = {
143 HW_PARAM(SAMPLE_BITS),
144 HW_PARAM(FRAME_BITS),
147 HW_PARAM(PERIOD_TIME),
148 HW_PARAM(PERIOD_SIZE),
149 HW_PARAM(PERIOD_BYTES),
151 HW_PARAM(BUFFER_TIME),
152 HW_PARAM(BUFFER_SIZE),
153 HW_PARAM(BUFFER_BYTES),
158 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
159 struct snd_pcm_hw_params *params)
162 struct snd_pcm_hardware *hw;
163 struct snd_interval *i = NULL;
164 struct snd_mask *m = NULL;
165 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
166 unsigned int rstamps[constrs->rules_num];
167 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
168 unsigned int stamp = 2;
172 params->fifo_size = 0;
173 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
176 params->rate_num = 0;
177 params->rate_den = 0;
180 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
181 m = hw_param_mask(params, k);
182 if (snd_mask_empty(m))
184 if (!(params->rmask & (1 << k)))
187 printk("%s = ", snd_pcm_hw_param_names[k]);
188 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
190 changed = snd_mask_refine(m, constrs_mask(constrs, k));
192 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195 params->cmask |= 1 << k;
200 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
201 i = hw_param_interval(params, k);
202 if (snd_interval_empty(i))
204 if (!(params->rmask & (1 << k)))
207 printk("%s = ", snd_pcm_hw_param_names[k]);
212 i->openmin ? '(' : '[', i->min,
213 i->max, i->openmax ? ')' : ']');
216 changed = snd_interval_refine(i, constrs_interval(constrs, k));
221 printk("%c%u %u%c\n",
222 i->openmin ? '(' : '[', i->min,
223 i->max, i->openmax ? ')' : ']');
226 params->cmask |= 1 << k;
231 for (k = 0; k < constrs->rules_num; k++)
233 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
234 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
237 for (k = 0; k < constrs->rules_num; k++) {
238 struct snd_pcm_hw_rule *r = &constrs->rules[k];
241 if (r->cond && !(r->cond & params->flags))
243 for (d = 0; r->deps[d] >= 0; d++) {
244 if (vstamps[r->deps[d]] > rstamps[k]) {
252 printk("Rule %d [%p]: ", k, r->func);
254 printk("%s = ", snd_pcm_hw_param_names[r->var]);
255 if (hw_is_mask(r->var)) {
256 m = hw_param_mask(params, r->var);
257 printk("%x", *m->bits);
259 i = hw_param_interval(params, r->var);
264 i->openmin ? '(' : '[', i->min,
265 i->max, i->openmax ? ')' : ']');
269 changed = r->func(params, r);
273 if (hw_is_mask(r->var))
274 printk("%x", *m->bits);
280 i->openmin ? '(' : '[', i->min,
281 i->max, i->openmax ? ')' : ']');
287 if (changed && r->var >= 0) {
288 params->cmask |= (1 << r->var);
289 vstamps[r->var] = stamp;
297 if (!params->msbits) {
298 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
299 if (snd_interval_single(i))
300 params->msbits = snd_interval_value(i);
303 if (!params->rate_den) {
304 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
305 if (snd_interval_single(i)) {
306 params->rate_num = snd_interval_value(i);
307 params->rate_den = 1;
311 hw = &substream->runtime->hw;
313 params->info = hw->info;
314 if (!params->fifo_size)
315 params->fifo_size = hw->fifo_size;
320 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params __user * _params)
323 struct snd_pcm_hw_params *params;
326 params = kmalloc(sizeof(*params), GFP_KERNEL);
331 if (copy_from_user(params, _params, sizeof(*params))) {
335 err = snd_pcm_hw_refine(substream, params);
336 if (copy_to_user(_params, params, sizeof(*params))) {
345 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
346 struct snd_pcm_hw_params *params)
348 struct snd_pcm_runtime *runtime;
351 snd_pcm_uframes_t frames;
353 snd_assert(substream != NULL, return -ENXIO);
354 runtime = substream->runtime;
355 snd_assert(runtime != NULL, return -ENXIO);
356 snd_pcm_stream_lock_irq(substream);
357 switch (runtime->status->state) {
358 case SNDRV_PCM_STATE_OPEN:
359 case SNDRV_PCM_STATE_SETUP:
360 case SNDRV_PCM_STATE_PREPARED:
363 snd_pcm_stream_unlock_irq(substream);
366 snd_pcm_stream_unlock_irq(substream);
367 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
368 if (!substream->oss.oss)
370 if (atomic_read(&runtime->mmap_count))
374 err = snd_pcm_hw_refine(substream, params);
378 err = snd_pcm_hw_params_choose(substream, params);
382 if (substream->ops->hw_params != NULL) {
383 err = substream->ops->hw_params(substream, params);
388 runtime->access = params_access(params);
389 runtime->format = params_format(params);
390 runtime->subformat = params_subformat(params);
391 runtime->channels = params_channels(params);
392 runtime->rate = params_rate(params);
393 runtime->period_size = params_period_size(params);
394 runtime->periods = params_periods(params);
395 runtime->buffer_size = params_buffer_size(params);
396 runtime->tick_time = params_tick_time(params);
397 runtime->info = params->info;
398 runtime->rate_num = params->rate_num;
399 runtime->rate_den = params->rate_den;
401 bits = snd_pcm_format_physical_width(runtime->format);
402 runtime->sample_bits = bits;
403 bits *= runtime->channels;
404 runtime->frame_bits = bits;
406 while (bits % 8 != 0) {
410 runtime->byte_align = bits / 8;
411 runtime->min_align = frames;
413 /* Default sw params */
414 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
415 runtime->period_step = 1;
416 runtime->sleep_min = 0;
417 runtime->control->avail_min = runtime->period_size;
418 runtime->xfer_align = runtime->period_size;
419 runtime->start_threshold = 1;
420 runtime->stop_threshold = runtime->buffer_size;
421 runtime->silence_threshold = 0;
422 runtime->silence_size = 0;
423 runtime->boundary = runtime->buffer_size;
424 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
425 runtime->boundary *= 2;
427 snd_pcm_timer_resolution_change(substream);
428 runtime->status->state = SNDRV_PCM_STATE_SETUP;
431 /* hardware might be unuseable from this time,
432 so we force application to retry to set
433 the correct hardware parameter settings */
434 runtime->status->state = SNDRV_PCM_STATE_OPEN;
435 if (substream->ops->hw_free != NULL)
436 substream->ops->hw_free(substream);
440 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params __user * _params)
443 struct snd_pcm_hw_params *params;
446 params = kmalloc(sizeof(*params), GFP_KERNEL);
451 if (copy_from_user(params, _params, sizeof(*params))) {
455 err = snd_pcm_hw_params(substream, params);
456 if (copy_to_user(_params, params, sizeof(*params))) {
465 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
467 struct snd_pcm_runtime *runtime;
470 snd_assert(substream != NULL, return -ENXIO);
471 runtime = substream->runtime;
472 snd_assert(runtime != NULL, return -ENXIO);
473 snd_pcm_stream_lock_irq(substream);
474 switch (runtime->status->state) {
475 case SNDRV_PCM_STATE_SETUP:
476 case SNDRV_PCM_STATE_PREPARED:
479 snd_pcm_stream_unlock_irq(substream);
482 snd_pcm_stream_unlock_irq(substream);
483 if (atomic_read(&runtime->mmap_count))
485 if (substream->ops->hw_free)
486 result = substream->ops->hw_free(substream);
487 runtime->status->state = SNDRV_PCM_STATE_OPEN;
491 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
492 struct snd_pcm_sw_params *params)
494 struct snd_pcm_runtime *runtime;
496 snd_assert(substream != NULL, return -ENXIO);
497 runtime = substream->runtime;
498 snd_assert(runtime != NULL, return -ENXIO);
499 snd_pcm_stream_lock_irq(substream);
500 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
501 snd_pcm_stream_unlock_irq(substream);
504 snd_pcm_stream_unlock_irq(substream);
506 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
508 if (params->avail_min == 0)
510 if (params->xfer_align == 0 ||
511 params->xfer_align % runtime->min_align != 0)
513 if (params->silence_size >= runtime->boundary) {
514 if (params->silence_threshold != 0)
517 if (params->silence_size > params->silence_threshold)
519 if (params->silence_threshold > runtime->buffer_size)
522 snd_pcm_stream_lock_irq(substream);
523 runtime->tstamp_mode = params->tstamp_mode;
524 runtime->sleep_min = params->sleep_min;
525 runtime->period_step = params->period_step;
526 runtime->control->avail_min = params->avail_min;
527 runtime->start_threshold = params->start_threshold;
528 runtime->stop_threshold = params->stop_threshold;
529 runtime->silence_threshold = params->silence_threshold;
530 runtime->silence_size = params->silence_size;
531 runtime->xfer_align = params->xfer_align;
532 params->boundary = runtime->boundary;
533 if (snd_pcm_running(substream)) {
534 if (runtime->sleep_min)
535 snd_pcm_tick_prepare(substream);
537 snd_pcm_tick_set(substream, 0);
538 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
539 runtime->silence_size > 0)
540 snd_pcm_playback_silence(substream, ULONG_MAX);
541 wake_up(&runtime->sleep);
543 snd_pcm_stream_unlock_irq(substream);
547 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
548 struct snd_pcm_sw_params __user * _params)
550 struct snd_pcm_sw_params params;
552 if (copy_from_user(¶ms, _params, sizeof(params)))
554 err = snd_pcm_sw_params(substream, ¶ms);
555 if (copy_to_user(_params, ¶ms, sizeof(params)))
560 int snd_pcm_status(struct snd_pcm_substream *substream,
561 struct snd_pcm_status *status)
563 struct snd_pcm_runtime *runtime = substream->runtime;
565 snd_pcm_stream_lock_irq(substream);
566 status->state = runtime->status->state;
567 status->suspended_state = runtime->status->suspended_state;
568 if (status->state == SNDRV_PCM_STATE_OPEN)
570 status->trigger_tstamp = runtime->trigger_tstamp;
571 if (snd_pcm_running(substream)) {
572 snd_pcm_update_hw_ptr(substream);
573 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
574 status->tstamp = runtime->status->tstamp;
576 getnstimeofday(&status->tstamp);
578 getnstimeofday(&status->tstamp);
579 status->appl_ptr = runtime->control->appl_ptr;
580 status->hw_ptr = runtime->status->hw_ptr;
581 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
582 status->avail = snd_pcm_playback_avail(runtime);
583 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
584 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
585 status->delay = runtime->buffer_size - status->avail;
589 status->avail = snd_pcm_capture_avail(runtime);
590 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
591 status->delay = status->avail;
595 status->avail_max = runtime->avail_max;
596 status->overrange = runtime->overrange;
597 runtime->avail_max = 0;
598 runtime->overrange = 0;
600 snd_pcm_stream_unlock_irq(substream);
604 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
605 struct snd_pcm_status __user * _status)
607 struct snd_pcm_status status;
608 struct snd_pcm_runtime *runtime;
611 snd_assert(substream != NULL, return -ENXIO);
612 runtime = substream->runtime;
613 memset(&status, 0, sizeof(status));
614 res = snd_pcm_status(substream, &status);
617 if (copy_to_user(_status, &status, sizeof(status)))
622 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
623 struct snd_pcm_channel_info * info)
625 struct snd_pcm_runtime *runtime;
626 unsigned int channel;
628 snd_assert(substream != NULL, return -ENXIO);
629 channel = info->channel;
630 runtime = substream->runtime;
631 snd_pcm_stream_lock_irq(substream);
632 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
633 snd_pcm_stream_unlock_irq(substream);
636 snd_pcm_stream_unlock_irq(substream);
637 if (channel >= runtime->channels)
639 memset(info, 0, sizeof(*info));
640 info->channel = channel;
641 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
644 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
645 struct snd_pcm_channel_info __user * _info)
647 struct snd_pcm_channel_info info;
650 if (copy_from_user(&info, _info, sizeof(info)))
652 res = snd_pcm_channel_info(substream, &info);
655 if (copy_to_user(_info, &info, sizeof(info)))
660 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 if (runtime->trigger_master == NULL)
665 if (runtime->trigger_master == substream) {
666 getnstimeofday(&runtime->trigger_tstamp);
668 snd_pcm_trigger_tstamp(runtime->trigger_master);
669 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
671 runtime->trigger_master = NULL;
675 int (*pre_action)(struct snd_pcm_substream *substream, int state);
676 int (*do_action)(struct snd_pcm_substream *substream, int state);
677 void (*undo_action)(struct snd_pcm_substream *substream, int state);
678 void (*post_action)(struct snd_pcm_substream *substream, int state);
682 * this functions is core for handling of linked stream
683 * Note: the stream state might be changed also on failure
684 * Note2: call with calling stream lock + link lock
686 static int snd_pcm_action_group(struct action_ops *ops,
687 struct snd_pcm_substream *substream,
688 int state, int do_lock)
690 struct list_head *pos;
691 struct snd_pcm_substream *s = NULL;
692 struct snd_pcm_substream *s1;
695 snd_pcm_group_for_each(pos, substream) {
696 s = snd_pcm_group_substream_entry(pos);
697 if (do_lock && s != substream)
698 spin_lock(&s->self_group.lock);
699 res = ops->pre_action(s, state);
703 snd_pcm_group_for_each(pos, substream) {
704 s = snd_pcm_group_substream_entry(pos);
705 res = ops->do_action(s, state);
707 if (ops->undo_action) {
708 snd_pcm_group_for_each(pos, substream) {
709 s1 = snd_pcm_group_substream_entry(pos);
710 if (s1 == s) /* failed stream */
712 ops->undo_action(s1, state);
715 s = NULL; /* unlock all */
719 snd_pcm_group_for_each(pos, substream) {
720 s = snd_pcm_group_substream_entry(pos);
721 ops->post_action(s, state);
726 snd_pcm_group_for_each(pos, substream) {
727 s1 = snd_pcm_group_substream_entry(pos);
729 spin_unlock(&s1->self_group.lock);
730 if (s1 == s) /* end */
738 * Note: call with stream lock
740 static int snd_pcm_action_single(struct action_ops *ops,
741 struct snd_pcm_substream *substream,
746 res = ops->pre_action(substream, state);
749 res = ops->do_action(substream, state);
751 ops->post_action(substream, state);
752 else if (ops->undo_action)
753 ops->undo_action(substream, state);
758 * Note: call with stream lock
760 static int snd_pcm_action(struct action_ops *ops,
761 struct snd_pcm_substream *substream,
766 if (snd_pcm_stream_linked(substream)) {
767 if (!spin_trylock(&substream->group->lock)) {
768 spin_unlock(&substream->self_group.lock);
769 spin_lock(&substream->group->lock);
770 spin_lock(&substream->self_group.lock);
772 res = snd_pcm_action_group(ops, substream, state, 1);
773 spin_unlock(&substream->group->lock);
775 res = snd_pcm_action_single(ops, substream, state);
781 * Note: don't use any locks before
783 static int snd_pcm_action_lock_irq(struct action_ops *ops,
784 struct snd_pcm_substream *substream,
789 read_lock_irq(&snd_pcm_link_rwlock);
790 if (snd_pcm_stream_linked(substream)) {
791 spin_lock(&substream->group->lock);
792 spin_lock(&substream->self_group.lock);
793 res = snd_pcm_action_group(ops, substream, state, 1);
794 spin_unlock(&substream->self_group.lock);
795 spin_unlock(&substream->group->lock);
797 spin_lock(&substream->self_group.lock);
798 res = snd_pcm_action_single(ops, substream, state);
799 spin_unlock(&substream->self_group.lock);
801 read_unlock_irq(&snd_pcm_link_rwlock);
807 static int snd_pcm_action_nonatomic(struct action_ops *ops,
808 struct snd_pcm_substream *substream,
813 down_read(&snd_pcm_link_rwsem);
814 if (snd_pcm_stream_linked(substream))
815 res = snd_pcm_action_group(ops, substream, state, 0);
817 res = snd_pcm_action_single(ops, substream, state);
818 up_read(&snd_pcm_link_rwsem);
825 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
827 struct snd_pcm_runtime *runtime = substream->runtime;
828 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
830 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
831 !snd_pcm_playback_data(substream))
833 runtime->trigger_master = substream;
837 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
839 if (substream->runtime->trigger_master != substream)
841 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
844 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
846 if (substream->runtime->trigger_master == substream)
847 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
850 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
852 struct snd_pcm_runtime *runtime = substream->runtime;
853 snd_pcm_trigger_tstamp(substream);
854 runtime->status->state = state;
855 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
856 runtime->silence_size > 0)
857 snd_pcm_playback_silence(substream, ULONG_MAX);
858 if (runtime->sleep_min)
859 snd_pcm_tick_prepare(substream);
860 if (substream->timer)
861 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
862 &runtime->trigger_tstamp);
865 static struct action_ops snd_pcm_action_start = {
866 .pre_action = snd_pcm_pre_start,
867 .do_action = snd_pcm_do_start,
868 .undo_action = snd_pcm_undo_start,
869 .post_action = snd_pcm_post_start
874 * @substream: the PCM substream instance
876 * Start all linked streams.
878 int snd_pcm_start(struct snd_pcm_substream *substream)
880 return snd_pcm_action(&snd_pcm_action_start, substream,
881 SNDRV_PCM_STATE_RUNNING);
887 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
889 struct snd_pcm_runtime *runtime = substream->runtime;
890 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
892 runtime->trigger_master = substream;
896 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
898 if (substream->runtime->trigger_master == substream &&
899 snd_pcm_running(substream))
900 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
901 return 0; /* unconditonally stop all substreams */
904 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 if (runtime->status->state != state) {
908 snd_pcm_trigger_tstamp(substream);
909 if (substream->timer)
910 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
911 &runtime->trigger_tstamp);
912 runtime->status->state = state;
913 snd_pcm_tick_set(substream, 0);
915 wake_up(&runtime->sleep);
918 static struct action_ops snd_pcm_action_stop = {
919 .pre_action = snd_pcm_pre_stop,
920 .do_action = snd_pcm_do_stop,
921 .post_action = snd_pcm_post_stop
926 * @substream: the PCM substream instance
927 * @state: PCM state after stopping the stream
929 * Try to stop all running streams in the substream group.
930 * The state of each stream is changed to the given value after that unconditionally.
932 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
934 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
939 * @substream: the PCM substream
941 * Stop the DMA only when the given stream is playback.
942 * The state is changed to SETUP.
943 * Unlike snd_pcm_stop(), this affects only the given stream.
945 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
947 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
948 SNDRV_PCM_STATE_SETUP);
954 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
956 struct snd_pcm_runtime *runtime = substream->runtime;
957 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
960 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
962 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
964 runtime->trigger_master = substream;
968 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
970 if (substream->runtime->trigger_master != substream)
972 return substream->ops->trigger(substream,
973 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
974 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
977 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
979 if (substream->runtime->trigger_master == substream)
980 substream->ops->trigger(substream,
981 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
982 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
985 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
987 struct snd_pcm_runtime *runtime = substream->runtime;
988 snd_pcm_trigger_tstamp(substream);
990 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
991 if (substream->timer)
992 snd_timer_notify(substream->timer,
993 SNDRV_TIMER_EVENT_MPAUSE,
994 &runtime->trigger_tstamp);
995 snd_pcm_tick_set(substream, 0);
996 wake_up(&runtime->sleep);
998 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
999 if (runtime->sleep_min)
1000 snd_pcm_tick_prepare(substream);
1001 if (substream->timer)
1002 snd_timer_notify(substream->timer,
1003 SNDRV_TIMER_EVENT_MCONTINUE,
1004 &runtime->trigger_tstamp);
1008 static struct action_ops snd_pcm_action_pause = {
1009 .pre_action = snd_pcm_pre_pause,
1010 .do_action = snd_pcm_do_pause,
1011 .undo_action = snd_pcm_undo_pause,
1012 .post_action = snd_pcm_post_pause
1016 * Push/release the pause for all linked streams.
1018 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1020 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1026 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1028 struct snd_pcm_runtime *runtime = substream->runtime;
1029 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1031 runtime->trigger_master = substream;
1035 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 if (runtime->trigger_master != substream)
1040 if (! snd_pcm_running(substream))
1042 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1043 return 0; /* suspend unconditionally */
1046 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1048 struct snd_pcm_runtime *runtime = substream->runtime;
1049 snd_pcm_trigger_tstamp(substream);
1050 if (substream->timer)
1051 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1052 &runtime->trigger_tstamp);
1053 runtime->status->suspended_state = runtime->status->state;
1054 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1055 snd_pcm_tick_set(substream, 0);
1056 wake_up(&runtime->sleep);
1059 static struct action_ops snd_pcm_action_suspend = {
1060 .pre_action = snd_pcm_pre_suspend,
1061 .do_action = snd_pcm_do_suspend,
1062 .post_action = snd_pcm_post_suspend
1067 * @substream: the PCM substream
1069 * Trigger SUSPEND to all linked streams.
1070 * After this call, all streams are changed to SUSPENDED state.
1072 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1075 unsigned long flags;
1080 snd_pcm_stream_lock_irqsave(substream, flags);
1081 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1082 snd_pcm_stream_unlock_irqrestore(substream, flags);
1087 * snd_pcm_suspend_all
1088 * @pcm: the PCM instance
1090 * Trigger SUSPEND to all substreams in the given pcm.
1091 * After this call, all streams are changed to SUSPENDED state.
1093 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1095 struct snd_pcm_substream *substream;
1096 int stream, err = 0;
1101 for (stream = 0; stream < 2; stream++) {
1102 for (substream = pcm->streams[stream].substream;
1103 substream; substream = substream->next) {
1104 /* FIXME: the open/close code should lock this as well */
1105 if (substream->runtime == NULL)
1107 err = snd_pcm_suspend(substream);
1108 if (err < 0 && err != -EBUSY)
1117 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1119 struct snd_pcm_runtime *runtime = substream->runtime;
1120 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1122 runtime->trigger_master = substream;
1126 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1128 struct snd_pcm_runtime *runtime = substream->runtime;
1129 if (runtime->trigger_master != substream)
1131 /* DMA not running previously? */
1132 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1133 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1134 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1136 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1139 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1141 if (substream->runtime->trigger_master == substream &&
1142 snd_pcm_running(substream))
1143 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1146 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1148 struct snd_pcm_runtime *runtime = substream->runtime;
1149 snd_pcm_trigger_tstamp(substream);
1150 if (substream->timer)
1151 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1152 &runtime->trigger_tstamp);
1153 runtime->status->state = runtime->status->suspended_state;
1154 if (runtime->sleep_min)
1155 snd_pcm_tick_prepare(substream);
1158 static struct action_ops snd_pcm_action_resume = {
1159 .pre_action = snd_pcm_pre_resume,
1160 .do_action = snd_pcm_do_resume,
1161 .undo_action = snd_pcm_undo_resume,
1162 .post_action = snd_pcm_post_resume
1165 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1167 struct snd_card *card = substream->pcm->card;
1170 snd_power_lock(card);
1171 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1172 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1173 snd_power_unlock(card);
1179 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1184 #endif /* CONFIG_PM */
1189 * Change the RUNNING stream(s) to XRUN state.
1191 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1193 struct snd_card *card = substream->pcm->card;
1194 struct snd_pcm_runtime *runtime = substream->runtime;
1197 snd_power_lock(card);
1198 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1199 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1204 snd_pcm_stream_lock_irq(substream);
1205 switch (runtime->status->state) {
1206 case SNDRV_PCM_STATE_XRUN:
1207 result = 0; /* already there */
1209 case SNDRV_PCM_STATE_RUNNING:
1210 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1215 snd_pcm_stream_unlock_irq(substream);
1217 snd_power_unlock(card);
1224 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1226 struct snd_pcm_runtime *runtime = substream->runtime;
1227 switch (runtime->status->state) {
1228 case SNDRV_PCM_STATE_RUNNING:
1229 case SNDRV_PCM_STATE_PREPARED:
1230 case SNDRV_PCM_STATE_PAUSED:
1231 case SNDRV_PCM_STATE_SUSPENDED:
1238 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1241 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1244 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1245 runtime->hw_ptr_base = 0;
1246 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1247 runtime->status->hw_ptr % runtime->period_size;
1248 runtime->silence_start = runtime->status->hw_ptr;
1249 runtime->silence_filled = 0;
1253 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1255 struct snd_pcm_runtime *runtime = substream->runtime;
1256 runtime->control->appl_ptr = runtime->status->hw_ptr;
1257 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1258 runtime->silence_size > 0)
1259 snd_pcm_playback_silence(substream, ULONG_MAX);
1262 static struct action_ops snd_pcm_action_reset = {
1263 .pre_action = snd_pcm_pre_reset,
1264 .do_action = snd_pcm_do_reset,
1265 .post_action = snd_pcm_post_reset
1268 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1270 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1276 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
1278 struct snd_pcm_runtime *runtime = substream->runtime;
1279 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1281 if (snd_pcm_running(substream))
1286 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1289 err = substream->ops->prepare(substream);
1292 return snd_pcm_do_reset(substream, 0);
1295 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1297 struct snd_pcm_runtime *runtime = substream->runtime;
1298 runtime->control->appl_ptr = runtime->status->hw_ptr;
1299 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1302 static struct action_ops snd_pcm_action_prepare = {
1303 .pre_action = snd_pcm_pre_prepare,
1304 .do_action = snd_pcm_do_prepare,
1305 .post_action = snd_pcm_post_prepare
1310 * @substream: the PCM substream instance
1312 * Prepare the PCM substream to be triggerable.
1314 int snd_pcm_prepare(struct snd_pcm_substream *substream)
1317 struct snd_card *card = substream->pcm->card;
1319 snd_power_lock(card);
1320 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1321 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1322 snd_power_unlock(card);
1330 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1332 if (substream->ffile->f_flags & O_NONBLOCK)
1334 substream->runtime->trigger_master = substream;
1338 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1340 struct snd_pcm_runtime *runtime = substream->runtime;
1341 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1342 switch (runtime->status->state) {
1343 case SNDRV_PCM_STATE_PREPARED:
1344 /* start playback stream if possible */
1345 if (! snd_pcm_playback_empty(substream)) {
1346 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1347 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1350 case SNDRV_PCM_STATE_RUNNING:
1351 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1357 /* stop running stream */
1358 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1359 int state = snd_pcm_capture_avail(runtime) > 0 ?
1360 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1361 snd_pcm_do_stop(substream, state);
1362 snd_pcm_post_stop(substream, state);
1368 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1372 static struct action_ops snd_pcm_action_drain_init = {
1373 .pre_action = snd_pcm_pre_drain_init,
1374 .do_action = snd_pcm_do_drain_init,
1375 .post_action = snd_pcm_post_drain_init
1379 struct snd_pcm_substream *substream;
1381 snd_pcm_uframes_t stop_threshold;
1384 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1387 * Drain the stream(s).
1388 * When the substream is linked, sync until the draining of all playback streams
1390 * After this call, all streams are supposed to be either SETUP or DRAINING
1391 * (capture only) state.
1393 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1395 struct snd_card *card;
1396 struct snd_pcm_runtime *runtime;
1397 struct list_head *pos;
1400 struct drain_rec *drec, drec_tmp, *d;
1402 snd_assert(substream != NULL, return -ENXIO);
1403 card = substream->pcm->card;
1404 runtime = substream->runtime;
1406 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1409 snd_power_lock(card);
1410 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1411 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1413 snd_power_unlock(card);
1418 /* allocate temporary record for drain sync */
1419 down_read(&snd_pcm_link_rwsem);
1420 if (snd_pcm_stream_linked(substream)) {
1421 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1423 up_read(&snd_pcm_link_rwsem);
1424 snd_power_unlock(card);
1430 /* count only playback streams */
1432 snd_pcm_group_for_each(pos, substream) {
1433 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1434 runtime = s->runtime;
1435 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1436 d = &drec[num_drecs++];
1438 init_waitqueue_entry(&d->wait, current);
1439 add_wait_queue(&runtime->sleep, &d->wait);
1440 /* stop_threshold fixup to avoid endless loop when
1441 * stop_threshold > buffer_size
1443 d->stop_threshold = runtime->stop_threshold;
1444 if (runtime->stop_threshold > runtime->buffer_size)
1445 runtime->stop_threshold = runtime->buffer_size;
1448 up_read(&snd_pcm_link_rwsem);
1452 snd_pcm_stream_lock_irq(substream);
1454 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1455 snd_pcm_pause(substream, 0);
1457 /* pre-start/stop - all running streams are changed to DRAINING state */
1458 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1460 snd_pcm_stream_unlock_irq(substream);
1466 if (signal_pending(current)) {
1467 result = -ERESTARTSYS;
1471 for (i = 0; i < num_drecs; i++) {
1472 runtime = drec[i].substream->runtime;
1473 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1477 break; /* yes, all drained */
1479 set_current_state(TASK_INTERRUPTIBLE);
1480 snd_pcm_stream_unlock_irq(substream);
1481 snd_power_unlock(card);
1482 tout = schedule_timeout(10 * HZ);
1483 snd_power_lock(card);
1484 snd_pcm_stream_lock_irq(substream);
1486 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1489 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1490 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1497 snd_pcm_stream_unlock_irq(substream);
1500 for (i = 0; i < num_drecs; i++) {
1502 runtime = d->substream->runtime;
1503 remove_wait_queue(&runtime->sleep, &d->wait);
1504 runtime->stop_threshold = d->stop_threshold;
1507 if (drec != &drec_tmp)
1509 snd_power_unlock(card);
1517 * Immediately put all linked substreams into SETUP state.
1519 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1521 struct snd_pcm_runtime *runtime;
1522 struct snd_card *card;
1525 snd_assert(substream != NULL, return -ENXIO);
1526 runtime = substream->runtime;
1527 card = substream->pcm->card;
1529 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1532 snd_power_lock(card);
1533 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1534 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1539 snd_pcm_stream_lock_irq(substream);
1541 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1542 snd_pcm_pause(substream, 0);
1544 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1545 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1546 snd_pcm_stream_unlock_irq(substream);
1548 snd_power_unlock(card);
1553 /* WARNING: Don't forget to fput back the file */
1554 static struct file *snd_pcm_file_fd(int fd)
1557 struct inode *inode;
1563 inode = file->f_dentry->d_inode;
1564 if (!S_ISCHR(inode->i_mode) ||
1565 imajor(inode) != snd_major) {
1569 minor = iminor(inode);
1570 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1571 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1581 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1585 struct snd_pcm_file *pcm_file;
1586 struct snd_pcm_substream *substream1;
1588 file = snd_pcm_file_fd(fd);
1591 pcm_file = file->private_data;
1592 substream1 = pcm_file->substream;
1593 down_write(&snd_pcm_link_rwsem);
1594 write_lock_irq(&snd_pcm_link_rwlock);
1595 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1596 substream->runtime->status->state != substream1->runtime->status->state) {
1600 if (snd_pcm_stream_linked(substream1)) {
1604 if (!snd_pcm_stream_linked(substream)) {
1605 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1606 if (substream->group == NULL) {
1610 spin_lock_init(&substream->group->lock);
1611 INIT_LIST_HEAD(&substream->group->substreams);
1612 list_add_tail(&substream->link_list, &substream->group->substreams);
1613 substream->group->count = 1;
1615 list_add_tail(&substream1->link_list, &substream->group->substreams);
1616 substream->group->count++;
1617 substream1->group = substream->group;
1619 write_unlock_irq(&snd_pcm_link_rwlock);
1620 up_write(&snd_pcm_link_rwsem);
1625 static void relink_to_local(struct snd_pcm_substream *substream)
1627 substream->group = &substream->self_group;
1628 INIT_LIST_HEAD(&substream->self_group.substreams);
1629 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1632 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1634 struct list_head *pos;
1637 down_write(&snd_pcm_link_rwsem);
1638 write_lock_irq(&snd_pcm_link_rwlock);
1639 if (!snd_pcm_stream_linked(substream)) {
1643 list_del(&substream->link_list);
1644 substream->group->count--;
1645 if (substream->group->count == 1) { /* detach the last stream, too */
1646 snd_pcm_group_for_each(pos, substream) {
1647 relink_to_local(snd_pcm_group_substream_entry(pos));
1650 kfree(substream->group);
1652 relink_to_local(substream);
1654 write_unlock_irq(&snd_pcm_link_rwlock);
1655 up_write(&snd_pcm_link_rwsem);
1662 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1663 struct snd_pcm_hw_rule *rule)
1665 struct snd_interval t;
1666 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1667 hw_param_interval_c(params, rule->deps[1]), &t);
1668 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1671 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1672 struct snd_pcm_hw_rule *rule)
1674 struct snd_interval t;
1675 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1676 hw_param_interval_c(params, rule->deps[1]), &t);
1677 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1680 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1681 struct snd_pcm_hw_rule *rule)
1683 struct snd_interval t;
1684 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1685 hw_param_interval_c(params, rule->deps[1]),
1686 (unsigned long) rule->private, &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
1693 struct snd_interval t;
1694 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1695 (unsigned long) rule->private,
1696 hw_param_interval_c(params, rule->deps[1]), &t);
1697 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1700 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1701 struct snd_pcm_hw_rule *rule)
1704 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1706 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1708 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1710 if (! snd_mask_test(mask, k))
1712 bits = snd_pcm_format_physical_width(k);
1714 continue; /* ignore invalid formats */
1715 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1716 snd_mask_reset(&m, k);
1718 return snd_mask_refine(mask, &m);
1721 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1722 struct snd_pcm_hw_rule *rule)
1724 struct snd_interval t;
1730 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1732 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1734 bits = snd_pcm_format_physical_width(k);
1736 continue; /* ignore invalid formats */
1737 if (t.min > (unsigned)bits)
1739 if (t.max < (unsigned)bits)
1743 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1746 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1747 #error "Change this table"
1750 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1751 48000, 64000, 88200, 96000, 176400, 192000 };
1753 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
1756 struct snd_pcm_hardware *hw = rule->private;
1757 return snd_interval_list(hw_param_interval(params, rule->var),
1758 ARRAY_SIZE(rates), rates, hw->rates);
1761 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1762 struct snd_pcm_hw_rule *rule)
1764 struct snd_interval t;
1765 struct snd_pcm_substream *substream = rule->private;
1767 t.max = substream->buffer_bytes_max;
1771 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1774 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1776 struct snd_pcm_runtime *runtime = substream->runtime;
1777 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1780 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1781 snd_mask_any(constrs_mask(constrs, k));
1784 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1785 snd_interval_any(constrs_interval(constrs, k));
1788 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1794 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1795 snd_pcm_hw_rule_format, NULL,
1796 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1799 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1800 snd_pcm_hw_rule_sample_bits, NULL,
1801 SNDRV_PCM_HW_PARAM_FORMAT,
1802 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1805 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1806 snd_pcm_hw_rule_div, NULL,
1807 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1811 snd_pcm_hw_rule_mul, NULL,
1812 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1816 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1817 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1821 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1822 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1825 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1826 snd_pcm_hw_rule_div, NULL,
1827 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1831 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1832 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1836 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1837 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1841 snd_pcm_hw_rule_div, NULL,
1842 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1846 snd_pcm_hw_rule_div, NULL,
1847 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1851 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1852 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1856 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1861 snd_pcm_hw_rule_mul, NULL,
1862 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1866 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1867 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1871 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1872 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1876 snd_pcm_hw_rule_muldivk, (void*) 8,
1877 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1880 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1881 snd_pcm_hw_rule_muldivk, (void*) 8,
1882 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1885 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1886 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1887 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1890 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1891 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1892 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1898 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1900 struct snd_pcm_runtime *runtime = substream->runtime;
1901 struct snd_pcm_hardware *hw = &runtime->hw;
1903 unsigned int mask = 0;
1905 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1906 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1907 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1908 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1909 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1910 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1911 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1912 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1913 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1914 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1915 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1917 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1918 snd_assert(err >= 0, return -EINVAL);
1920 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1921 snd_assert(err >= 0, return -EINVAL);
1923 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1924 snd_assert(err >= 0, return -EINVAL);
1926 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1927 hw->channels_min, hw->channels_max);
1928 snd_assert(err >= 0, return -EINVAL);
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1931 hw->rate_min, hw->rate_max);
1932 snd_assert(err >= 0, return -EINVAL);
1934 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1935 hw->period_bytes_min, hw->period_bytes_max);
1936 snd_assert(err >= 0, return -EINVAL);
1938 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1939 hw->periods_min, hw->periods_max);
1940 snd_assert(err >= 0, return -EINVAL);
1942 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1943 hw->period_bytes_min, hw->buffer_bytes_max);
1944 snd_assert(err >= 0, return -EINVAL);
1946 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1947 snd_pcm_hw_rule_buffer_bytes_max, substream,
1948 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1953 if (runtime->dma_bytes) {
1954 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1955 snd_assert(err >= 0, return -EINVAL);
1958 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1959 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1960 snd_pcm_hw_rule_rate, hw,
1961 SNDRV_PCM_HW_PARAM_RATE, -1);
1966 /* FIXME: this belong to lowlevel */
1967 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1968 1000000 / HZ, 1000000 / HZ);
1969 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1974 static void snd_pcm_add_file(struct snd_pcm_str *str,
1975 struct snd_pcm_file *pcm_file)
1977 pcm_file->next = str->files;
1978 str->files = pcm_file;
1981 static void snd_pcm_remove_file(struct snd_pcm_str *str,
1982 struct snd_pcm_file *pcm_file)
1984 struct snd_pcm_file * pcm_file1;
1985 if (str->files == pcm_file) {
1986 str->files = pcm_file->next;
1988 pcm_file1 = str->files;
1989 while (pcm_file1 && pcm_file1->next != pcm_file)
1990 pcm_file1 = pcm_file1->next;
1991 if (pcm_file1 != NULL)
1992 pcm_file1->next = pcm_file->next;
1996 static int snd_pcm_release_file(struct snd_pcm_file * pcm_file)
1998 struct snd_pcm_substream *substream;
1999 struct snd_pcm_runtime *runtime;
2000 struct snd_pcm_str * str;
2002 snd_assert(pcm_file != NULL, return -ENXIO);
2003 substream = pcm_file->substream;
2004 snd_assert(substream != NULL, return -ENXIO);
2005 runtime = substream->runtime;
2006 str = substream->pstr;
2007 snd_pcm_unlink(substream);
2008 if (substream->ffile != NULL) {
2009 if (substream->ops->hw_free != NULL)
2010 substream->ops->hw_free(substream);
2011 substream->ops->close(substream);
2012 substream->ffile = NULL;
2014 snd_pcm_remove_file(str, pcm_file);
2015 snd_pcm_release_substream(substream);
2020 static int snd_pcm_open_file(struct file *file,
2021 struct snd_pcm *pcm,
2023 struct snd_pcm_file **rpcm_file)
2026 struct snd_pcm_file *pcm_file;
2027 struct snd_pcm_substream *substream;
2028 struct snd_pcm_str *str;
2030 snd_assert(rpcm_file != NULL, return -EINVAL);
2033 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2034 if (pcm_file == NULL) {
2038 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2043 str = substream->pstr;
2044 substream->file = pcm_file;
2045 substream->no_mmap_ctrl = 0;
2047 pcm_file->substream = substream;
2049 snd_pcm_add_file(str, pcm_file);
2051 err = snd_pcm_hw_constraints_init(substream);
2053 snd_printd("snd_pcm_hw_constraints_init failed\n");
2054 snd_pcm_release_file(pcm_file);
2058 if ((err = substream->ops->open(substream)) < 0) {
2059 snd_pcm_release_file(pcm_file);
2062 substream->ffile = file;
2064 err = snd_pcm_hw_constraints_complete(substream);
2066 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2067 snd_pcm_release_file(pcm_file);
2071 file->private_data = pcm_file;
2072 *rpcm_file = pcm_file;
2076 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2078 struct snd_pcm *pcm;
2080 pcm = snd_lookup_minor_data(iminor(inode),
2081 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2082 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2085 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2087 struct snd_pcm *pcm;
2089 pcm = snd_lookup_minor_data(iminor(inode),
2090 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2091 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2094 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2097 struct snd_pcm_file *pcm_file;
2104 err = snd_card_file_add(pcm->card, file);
2107 if (!try_module_get(pcm->card->module)) {
2111 init_waitqueue_entry(&wait, current);
2112 add_wait_queue(&pcm->open_wait, &wait);
2113 down(&pcm->open_mutex);
2115 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2118 if (err == -EAGAIN) {
2119 if (file->f_flags & O_NONBLOCK) {
2125 set_current_state(TASK_INTERRUPTIBLE);
2126 up(&pcm->open_mutex);
2128 down(&pcm->open_mutex);
2129 if (signal_pending(current)) {
2134 remove_wait_queue(&pcm->open_wait, &wait);
2135 up(&pcm->open_mutex);
2141 module_put(pcm->card->module);
2143 snd_card_file_remove(pcm->card, file);
2148 static int snd_pcm_release(struct inode *inode, struct file *file)
2150 struct snd_pcm *pcm;
2151 struct snd_pcm_substream *substream;
2152 struct snd_pcm_file *pcm_file;
2154 pcm_file = file->private_data;
2155 substream = pcm_file->substream;
2156 snd_assert(substream != NULL, return -ENXIO);
2157 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2158 pcm = substream->pcm;
2159 snd_pcm_drop(substream);
2160 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2161 down(&pcm->open_mutex);
2162 snd_pcm_release_file(pcm_file);
2163 up(&pcm->open_mutex);
2164 wake_up(&pcm->open_wait);
2165 module_put(pcm->card->module);
2166 snd_card_file_remove(pcm->card, file);
2170 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2171 snd_pcm_uframes_t frames)
2173 struct snd_pcm_runtime *runtime = substream->runtime;
2174 snd_pcm_sframes_t appl_ptr;
2175 snd_pcm_sframes_t ret;
2176 snd_pcm_sframes_t hw_avail;
2181 snd_pcm_stream_lock_irq(substream);
2182 switch (runtime->status->state) {
2183 case SNDRV_PCM_STATE_PREPARED:
2185 case SNDRV_PCM_STATE_DRAINING:
2186 case SNDRV_PCM_STATE_RUNNING:
2187 if (snd_pcm_update_hw_ptr(substream) >= 0)
2190 case SNDRV_PCM_STATE_XRUN:
2198 hw_avail = snd_pcm_playback_hw_avail(runtime);
2199 if (hw_avail <= 0) {
2203 if (frames > (snd_pcm_uframes_t)hw_avail)
2206 frames -= frames % runtime->xfer_align;
2207 appl_ptr = runtime->control->appl_ptr - frames;
2209 appl_ptr += runtime->boundary;
2210 runtime->control->appl_ptr = appl_ptr;
2211 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2213 snd_pcm_tick_prepare(substream);
2216 snd_pcm_stream_unlock_irq(substream);
2220 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2221 snd_pcm_uframes_t frames)
2223 struct snd_pcm_runtime *runtime = substream->runtime;
2224 snd_pcm_sframes_t appl_ptr;
2225 snd_pcm_sframes_t ret;
2226 snd_pcm_sframes_t hw_avail;
2231 snd_pcm_stream_lock_irq(substream);
2232 switch (runtime->status->state) {
2233 case SNDRV_PCM_STATE_PREPARED:
2234 case SNDRV_PCM_STATE_DRAINING:
2236 case SNDRV_PCM_STATE_RUNNING:
2237 if (snd_pcm_update_hw_ptr(substream) >= 0)
2240 case SNDRV_PCM_STATE_XRUN:
2248 hw_avail = snd_pcm_capture_hw_avail(runtime);
2249 if (hw_avail <= 0) {
2253 if (frames > (snd_pcm_uframes_t)hw_avail)
2256 frames -= frames % runtime->xfer_align;
2257 appl_ptr = runtime->control->appl_ptr - frames;
2259 appl_ptr += runtime->boundary;
2260 runtime->control->appl_ptr = appl_ptr;
2261 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2263 snd_pcm_tick_prepare(substream);
2266 snd_pcm_stream_unlock_irq(substream);
2270 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2271 snd_pcm_uframes_t frames)
2273 struct snd_pcm_runtime *runtime = substream->runtime;
2274 snd_pcm_sframes_t appl_ptr;
2275 snd_pcm_sframes_t ret;
2276 snd_pcm_sframes_t avail;
2281 snd_pcm_stream_lock_irq(substream);
2282 switch (runtime->status->state) {
2283 case SNDRV_PCM_STATE_PREPARED:
2284 case SNDRV_PCM_STATE_PAUSED:
2286 case SNDRV_PCM_STATE_DRAINING:
2287 case SNDRV_PCM_STATE_RUNNING:
2288 if (snd_pcm_update_hw_ptr(substream) >= 0)
2291 case SNDRV_PCM_STATE_XRUN:
2299 avail = snd_pcm_playback_avail(runtime);
2304 if (frames > (snd_pcm_uframes_t)avail)
2307 frames -= frames % runtime->xfer_align;
2308 appl_ptr = runtime->control->appl_ptr + frames;
2309 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2310 appl_ptr -= runtime->boundary;
2311 runtime->control->appl_ptr = appl_ptr;
2312 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2314 snd_pcm_tick_prepare(substream);
2317 snd_pcm_stream_unlock_irq(substream);
2321 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2322 snd_pcm_uframes_t frames)
2324 struct snd_pcm_runtime *runtime = substream->runtime;
2325 snd_pcm_sframes_t appl_ptr;
2326 snd_pcm_sframes_t ret;
2327 snd_pcm_sframes_t avail;
2332 snd_pcm_stream_lock_irq(substream);
2333 switch (runtime->status->state) {
2334 case SNDRV_PCM_STATE_PREPARED:
2335 case SNDRV_PCM_STATE_DRAINING:
2336 case SNDRV_PCM_STATE_PAUSED:
2338 case SNDRV_PCM_STATE_RUNNING:
2339 if (snd_pcm_update_hw_ptr(substream) >= 0)
2342 case SNDRV_PCM_STATE_XRUN:
2350 avail = snd_pcm_capture_avail(runtime);
2355 if (frames > (snd_pcm_uframes_t)avail)
2358 frames -= frames % runtime->xfer_align;
2359 appl_ptr = runtime->control->appl_ptr + frames;
2360 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2361 appl_ptr -= runtime->boundary;
2362 runtime->control->appl_ptr = appl_ptr;
2363 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2365 snd_pcm_tick_prepare(substream);
2368 snd_pcm_stream_unlock_irq(substream);
2372 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2374 struct snd_pcm_runtime *runtime = substream->runtime;
2377 snd_pcm_stream_lock_irq(substream);
2378 switch (runtime->status->state) {
2379 case SNDRV_PCM_STATE_DRAINING:
2380 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2382 case SNDRV_PCM_STATE_RUNNING:
2383 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2386 case SNDRV_PCM_STATE_PREPARED:
2387 case SNDRV_PCM_STATE_SUSPENDED:
2390 case SNDRV_PCM_STATE_XRUN:
2398 snd_pcm_stream_unlock_irq(substream);
2402 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2403 snd_pcm_sframes_t __user *res)
2405 struct snd_pcm_runtime *runtime = substream->runtime;
2407 snd_pcm_sframes_t n = 0;
2409 snd_pcm_stream_lock_irq(substream);
2410 switch (runtime->status->state) {
2411 case SNDRV_PCM_STATE_DRAINING:
2412 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2414 case SNDRV_PCM_STATE_RUNNING:
2415 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2418 case SNDRV_PCM_STATE_PREPARED:
2419 case SNDRV_PCM_STATE_SUSPENDED:
2421 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2422 n = snd_pcm_playback_hw_avail(runtime);
2424 n = snd_pcm_capture_avail(runtime);
2426 case SNDRV_PCM_STATE_XRUN:
2434 snd_pcm_stream_unlock_irq(substream);
2436 if (put_user(n, res))
2441 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2442 struct snd_pcm_sync_ptr __user *_sync_ptr)
2444 struct snd_pcm_runtime *runtime = substream->runtime;
2445 struct snd_pcm_sync_ptr sync_ptr;
2446 volatile struct snd_pcm_mmap_status *status;
2447 volatile struct snd_pcm_mmap_control *control;
2450 memset(&sync_ptr, 0, sizeof(sync_ptr));
2451 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2453 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2455 status = runtime->status;
2456 control = runtime->control;
2457 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2458 err = snd_pcm_hwsync(substream);
2462 snd_pcm_stream_lock_irq(substream);
2463 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2464 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2466 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2467 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2468 control->avail_min = sync_ptr.c.control.avail_min;
2470 sync_ptr.c.control.avail_min = control->avail_min;
2471 sync_ptr.s.status.state = status->state;
2472 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2473 sync_ptr.s.status.tstamp = status->tstamp;
2474 sync_ptr.s.status.suspended_state = status->suspended_state;
2475 snd_pcm_stream_unlock_irq(substream);
2476 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2481 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2482 unsigned int cmd, void __user *arg);
2483 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2484 unsigned int cmd, void __user *arg);
2486 static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
2487 unsigned int cmd, void __user *arg)
2489 snd_assert(substream != NULL, return -ENXIO);
2492 case SNDRV_PCM_IOCTL_PVERSION:
2493 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2494 case SNDRV_PCM_IOCTL_INFO:
2495 return snd_pcm_info_user(substream, arg);
2496 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2498 case SNDRV_PCM_IOCTL_HW_REFINE:
2499 return snd_pcm_hw_refine_user(substream, arg);
2500 case SNDRV_PCM_IOCTL_HW_PARAMS:
2501 return snd_pcm_hw_params_user(substream, arg);
2502 case SNDRV_PCM_IOCTL_HW_FREE:
2503 return snd_pcm_hw_free(substream);
2504 case SNDRV_PCM_IOCTL_SW_PARAMS:
2505 return snd_pcm_sw_params_user(substream, arg);
2506 case SNDRV_PCM_IOCTL_STATUS:
2507 return snd_pcm_status_user(substream, arg);
2508 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2509 return snd_pcm_channel_info_user(substream, arg);
2510 case SNDRV_PCM_IOCTL_PREPARE:
2511 return snd_pcm_prepare(substream);
2512 case SNDRV_PCM_IOCTL_RESET:
2513 return snd_pcm_reset(substream);
2514 case SNDRV_PCM_IOCTL_START:
2515 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2516 case SNDRV_PCM_IOCTL_LINK:
2517 return snd_pcm_link(substream, (int)(unsigned long) arg);
2518 case SNDRV_PCM_IOCTL_UNLINK:
2519 return snd_pcm_unlink(substream);
2520 case SNDRV_PCM_IOCTL_RESUME:
2521 return snd_pcm_resume(substream);
2522 case SNDRV_PCM_IOCTL_XRUN:
2523 return snd_pcm_xrun(substream);
2524 case SNDRV_PCM_IOCTL_HWSYNC:
2525 return snd_pcm_hwsync(substream);
2526 case SNDRV_PCM_IOCTL_DELAY:
2527 return snd_pcm_delay(substream, arg);
2528 case SNDRV_PCM_IOCTL_SYNC_PTR:
2529 return snd_pcm_sync_ptr(substream, arg);
2530 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2531 return snd_pcm_hw_refine_old_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2533 return snd_pcm_hw_params_old_user(substream, arg);
2534 case SNDRV_PCM_IOCTL_DRAIN:
2535 return snd_pcm_drain(substream);
2536 case SNDRV_PCM_IOCTL_DROP:
2537 return snd_pcm_drop(substream);
2539 snd_printd("unknown ioctl = 0x%x\n", cmd);
2543 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2544 unsigned int cmd, void __user *arg)
2546 snd_assert(substream != NULL, return -ENXIO);
2547 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2549 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2551 struct snd_xferi xferi;
2552 struct snd_xferi __user *_xferi = arg;
2553 struct snd_pcm_runtime *runtime = substream->runtime;
2554 snd_pcm_sframes_t result;
2555 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2557 if (put_user(0, &_xferi->result))
2559 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2561 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2562 __put_user(result, &_xferi->result);
2563 return result < 0 ? result : 0;
2565 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2567 struct snd_xfern xfern;
2568 struct snd_xfern __user *_xfern = arg;
2569 struct snd_pcm_runtime *runtime = substream->runtime;
2571 snd_pcm_sframes_t result;
2572 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2574 if (runtime->channels > 128)
2576 if (put_user(0, &_xfern->result))
2578 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2580 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2583 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2587 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2589 __put_user(result, &_xfern->result);
2590 return result < 0 ? result : 0;
2592 case SNDRV_PCM_IOCTL_REWIND:
2594 snd_pcm_uframes_t frames;
2595 snd_pcm_uframes_t __user *_frames = arg;
2596 snd_pcm_sframes_t result;
2597 if (get_user(frames, _frames))
2599 if (put_user(0, _frames))
2601 result = snd_pcm_playback_rewind(substream, frames);
2602 __put_user(result, _frames);
2603 return result < 0 ? result : 0;
2605 case SNDRV_PCM_IOCTL_FORWARD:
2607 snd_pcm_uframes_t frames;
2608 snd_pcm_uframes_t __user *_frames = arg;
2609 snd_pcm_sframes_t result;
2610 if (get_user(frames, _frames))
2612 if (put_user(0, _frames))
2614 result = snd_pcm_playback_forward(substream, frames);
2615 __put_user(result, _frames);
2616 return result < 0 ? result : 0;
2618 case SNDRV_PCM_IOCTL_PAUSE:
2621 snd_pcm_stream_lock_irq(substream);
2622 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2623 snd_pcm_stream_unlock_irq(substream);
2627 return snd_pcm_common_ioctl1(substream, cmd, arg);
2630 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2631 unsigned int cmd, void __user *arg)
2633 snd_assert(substream != NULL, return -ENXIO);
2634 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2636 case SNDRV_PCM_IOCTL_READI_FRAMES:
2638 struct snd_xferi xferi;
2639 struct snd_xferi __user *_xferi = arg;
2640 struct snd_pcm_runtime *runtime = substream->runtime;
2641 snd_pcm_sframes_t result;
2642 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2644 if (put_user(0, &_xferi->result))
2646 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2648 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2649 __put_user(result, &_xferi->result);
2650 return result < 0 ? result : 0;
2652 case SNDRV_PCM_IOCTL_READN_FRAMES:
2654 struct snd_xfern xfern;
2655 struct snd_xfern __user *_xfern = arg;
2656 struct snd_pcm_runtime *runtime = substream->runtime;
2658 snd_pcm_sframes_t result;
2659 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2661 if (runtime->channels > 128)
2663 if (put_user(0, &_xfern->result))
2665 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2667 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2670 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2674 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2676 __put_user(result, &_xfern->result);
2677 return result < 0 ? result : 0;
2679 case SNDRV_PCM_IOCTL_REWIND:
2681 snd_pcm_uframes_t frames;
2682 snd_pcm_uframes_t __user *_frames = arg;
2683 snd_pcm_sframes_t result;
2684 if (get_user(frames, _frames))
2686 if (put_user(0, _frames))
2688 result = snd_pcm_capture_rewind(substream, frames);
2689 __put_user(result, _frames);
2690 return result < 0 ? result : 0;
2692 case SNDRV_PCM_IOCTL_FORWARD:
2694 snd_pcm_uframes_t frames;
2695 snd_pcm_uframes_t __user *_frames = arg;
2696 snd_pcm_sframes_t result;
2697 if (get_user(frames, _frames))
2699 if (put_user(0, _frames))
2701 result = snd_pcm_capture_forward(substream, frames);
2702 __put_user(result, _frames);
2703 return result < 0 ? result : 0;
2706 return snd_pcm_common_ioctl1(substream, cmd, arg);
2709 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2712 struct snd_pcm_file *pcm_file;
2714 pcm_file = file->private_data;
2716 if (((cmd >> 8) & 0xff) != 'A')
2719 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2722 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2725 struct snd_pcm_file *pcm_file;
2727 pcm_file = file->private_data;
2729 if (((cmd >> 8) & 0xff) != 'A')
2732 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2735 int snd_pcm_kernel_playback_ioctl(struct snd_pcm_substream *substream,
2736 unsigned int cmd, void *arg)
2741 fs = snd_enter_user();
2742 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2747 int snd_pcm_kernel_capture_ioctl(struct snd_pcm_substream *substream,
2748 unsigned int cmd, void *arg)
2753 fs = snd_enter_user();
2754 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2759 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2760 unsigned int cmd, void *arg)
2762 switch (substream->stream) {
2763 case SNDRV_PCM_STREAM_PLAYBACK:
2764 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2765 case SNDRV_PCM_STREAM_CAPTURE:
2766 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2772 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2775 struct snd_pcm_file *pcm_file;
2776 struct snd_pcm_substream *substream;
2777 struct snd_pcm_runtime *runtime;
2778 snd_pcm_sframes_t result;
2780 pcm_file = file->private_data;
2781 substream = pcm_file->substream;
2782 snd_assert(substream != NULL, return -ENXIO);
2783 runtime = substream->runtime;
2784 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2786 if (!frame_aligned(runtime, count))
2788 count = bytes_to_frames(runtime, count);
2789 result = snd_pcm_lib_read(substream, buf, count);
2791 result = frames_to_bytes(runtime, result);
2795 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2796 size_t count, loff_t * offset)
2798 struct snd_pcm_file *pcm_file;
2799 struct snd_pcm_substream *substream;
2800 struct snd_pcm_runtime *runtime;
2801 snd_pcm_sframes_t result;
2803 pcm_file = file->private_data;
2804 substream = pcm_file->substream;
2805 snd_assert(substream != NULL, result = -ENXIO; goto end);
2806 runtime = substream->runtime;
2807 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2811 if (!frame_aligned(runtime, count)) {
2815 count = bytes_to_frames(runtime, count);
2816 result = snd_pcm_lib_write(substream, buf, count);
2818 result = frames_to_bytes(runtime, result);
2823 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2824 unsigned long count, loff_t * offset)
2827 struct snd_pcm_file *pcm_file;
2828 struct snd_pcm_substream *substream;
2829 struct snd_pcm_runtime *runtime;
2830 snd_pcm_sframes_t result;
2833 snd_pcm_uframes_t frames;
2835 pcm_file = file->private_data;
2836 substream = pcm_file->substream;
2837 snd_assert(substream != NULL, return -ENXIO);
2838 runtime = substream->runtime;
2839 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2841 if (count > 1024 || count != runtime->channels)
2843 if (!frame_aligned(runtime, _vector->iov_len))
2845 frames = bytes_to_samples(runtime, _vector->iov_len);
2846 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2849 for (i = 0; i < count; ++i)
2850 bufs[i] = _vector[i].iov_base;
2851 result = snd_pcm_lib_readv(substream, bufs, frames);
2853 result = frames_to_bytes(runtime, result);
2858 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2859 unsigned long count, loff_t * offset)
2861 struct snd_pcm_file *pcm_file;
2862 struct snd_pcm_substream *substream;
2863 struct snd_pcm_runtime *runtime;
2864 snd_pcm_sframes_t result;
2867 snd_pcm_uframes_t frames;
2869 pcm_file = file->private_data;
2870 substream = pcm_file->substream;
2871 snd_assert(substream != NULL, result = -ENXIO; goto end);
2872 runtime = substream->runtime;
2873 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2877 if (count > 128 || count != runtime->channels ||
2878 !frame_aligned(runtime, _vector->iov_len)) {
2882 frames = bytes_to_samples(runtime, _vector->iov_len);
2883 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2886 for (i = 0; i < count; ++i)
2887 bufs[i] = _vector[i].iov_base;
2888 result = snd_pcm_lib_writev(substream, bufs, frames);
2890 result = frames_to_bytes(runtime, result);
2896 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2898 struct snd_pcm_file *pcm_file;
2899 struct snd_pcm_substream *substream;
2900 struct snd_pcm_runtime *runtime;
2902 snd_pcm_uframes_t avail;
2904 pcm_file = file->private_data;
2906 substream = pcm_file->substream;
2907 snd_assert(substream != NULL, return -ENXIO);
2908 runtime = substream->runtime;
2910 poll_wait(file, &runtime->sleep, wait);
2912 snd_pcm_stream_lock_irq(substream);
2913 avail = snd_pcm_playback_avail(runtime);
2914 switch (runtime->status->state) {
2915 case SNDRV_PCM_STATE_RUNNING:
2916 case SNDRV_PCM_STATE_PREPARED:
2917 case SNDRV_PCM_STATE_PAUSED:
2918 if (avail >= runtime->control->avail_min) {
2919 mask = POLLOUT | POLLWRNORM;
2923 case SNDRV_PCM_STATE_DRAINING:
2927 mask = POLLOUT | POLLWRNORM | POLLERR;
2930 snd_pcm_stream_unlock_irq(substream);
2934 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2936 struct snd_pcm_file *pcm_file;
2937 struct snd_pcm_substream *substream;
2938 struct snd_pcm_runtime *runtime;
2940 snd_pcm_uframes_t avail;
2942 pcm_file = file->private_data;
2944 substream = pcm_file->substream;
2945 snd_assert(substream != NULL, return -ENXIO);
2946 runtime = substream->runtime;
2948 poll_wait(file, &runtime->sleep, wait);
2950 snd_pcm_stream_lock_irq(substream);
2951 avail = snd_pcm_capture_avail(runtime);
2952 switch (runtime->status->state) {
2953 case SNDRV_PCM_STATE_RUNNING:
2954 case SNDRV_PCM_STATE_PREPARED:
2955 case SNDRV_PCM_STATE_PAUSED:
2956 if (avail >= runtime->control->avail_min) {
2957 mask = POLLIN | POLLRDNORM;
2962 case SNDRV_PCM_STATE_DRAINING:
2964 mask = POLLIN | POLLRDNORM;
2969 mask = POLLIN | POLLRDNORM | POLLERR;
2972 snd_pcm_stream_unlock_irq(substream);
2981 * Only on coherent architectures, we can mmap the status and the control records
2982 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2984 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2986 * mmap status record
2988 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2989 unsigned long address, int *type)
2991 struct snd_pcm_substream *substream = area->vm_private_data;
2992 struct snd_pcm_runtime *runtime;
2995 if (substream == NULL)
2997 runtime = substream->runtime;
2998 page = virt_to_page(runtime->status);
3001 *type = VM_FAULT_MINOR;
3005 static struct vm_operations_struct snd_pcm_vm_ops_status =
3007 .nopage = snd_pcm_mmap_status_nopage,
3010 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3011 struct vm_area_struct *area)
3013 struct snd_pcm_runtime *runtime;
3015 if (!(area->vm_flags & VM_READ))
3017 runtime = substream->runtime;
3018 snd_assert(runtime != NULL, return -EAGAIN);
3019 size = area->vm_end - area->vm_start;
3020 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3022 area->vm_ops = &snd_pcm_vm_ops_status;
3023 area->vm_private_data = substream;
3024 area->vm_flags |= VM_RESERVED;
3029 * mmap control record
3031 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3032 unsigned long address, int *type)
3034 struct snd_pcm_substream *substream = area->vm_private_data;
3035 struct snd_pcm_runtime *runtime;
3038 if (substream == NULL)
3040 runtime = substream->runtime;
3041 page = virt_to_page(runtime->control);
3044 *type = VM_FAULT_MINOR;
3048 static struct vm_operations_struct snd_pcm_vm_ops_control =
3050 .nopage = snd_pcm_mmap_control_nopage,
3053 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3054 struct vm_area_struct *area)
3056 struct snd_pcm_runtime *runtime;
3058 if (!(area->vm_flags & VM_READ))
3060 runtime = substream->runtime;
3061 snd_assert(runtime != NULL, return -EAGAIN);
3062 size = area->vm_end - area->vm_start;
3063 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3065 area->vm_ops = &snd_pcm_vm_ops_control;
3066 area->vm_private_data = substream;
3067 area->vm_flags |= VM_RESERVED;
3070 #else /* ! coherent mmap */
3072 * don't support mmap for status and control records.
3074 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3075 struct vm_area_struct *area)
3079 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3080 struct vm_area_struct *area)
3084 #endif /* coherent mmap */
3087 * nopage callback for mmapping a RAM page
3089 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3090 unsigned long address, int *type)
3092 struct snd_pcm_substream *substream = area->vm_private_data;
3093 struct snd_pcm_runtime *runtime;
3094 unsigned long offset;
3099 if (substream == NULL)
3101 runtime = substream->runtime;
3102 offset = area->vm_pgoff << PAGE_SHIFT;
3103 offset += address - area->vm_start;
3104 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3105 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3106 if (offset > dma_bytes - PAGE_SIZE)
3107 return NOPAGE_SIGBUS;
3108 if (substream->ops->page) {
3109 page = substream->ops->page(substream, offset);
3113 vaddr = runtime->dma_area + offset;
3114 page = virt_to_page(vaddr);
3118 *type = VM_FAULT_MINOR;
3122 static struct vm_operations_struct snd_pcm_vm_ops_data =
3124 .open = snd_pcm_mmap_data_open,
3125 .close = snd_pcm_mmap_data_close,
3126 .nopage = snd_pcm_mmap_data_nopage,
3130 * mmap the DMA buffer on RAM
3132 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3133 struct vm_area_struct *area)
3135 area->vm_ops = &snd_pcm_vm_ops_data;
3136 area->vm_private_data = substream;
3137 area->vm_flags |= VM_RESERVED;
3138 atomic_inc(&substream->runtime->mmap_count);
3143 * mmap the DMA buffer on I/O memory area
3145 #if SNDRV_PCM_INFO_MMAP_IOMEM
3146 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3148 .open = snd_pcm_mmap_data_open,
3149 .close = snd_pcm_mmap_data_close,
3152 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3153 struct vm_area_struct *area)
3156 unsigned long offset;
3158 #ifdef pgprot_noncached
3159 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3161 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3162 area->vm_private_data = substream;
3163 area->vm_flags |= VM_IO;
3164 size = area->vm_end - area->vm_start;
3165 offset = area->vm_pgoff << PAGE_SHIFT;
3166 if (io_remap_pfn_range(area, area->vm_start,
3167 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3168 size, area->vm_page_prot))
3170 atomic_inc(&substream->runtime->mmap_count);
3173 #endif /* SNDRV_PCM_INFO_MMAP */
3178 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3179 struct vm_area_struct *area)
3181 struct snd_pcm_runtime *runtime;
3183 unsigned long offset;
3186 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3187 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3190 if (!(area->vm_flags & VM_READ))
3193 runtime = substream->runtime;
3194 snd_assert(runtime != NULL, return -EAGAIN);
3195 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3197 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3199 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3200 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3202 size = area->vm_end - area->vm_start;
3203 offset = area->vm_pgoff << PAGE_SHIFT;
3204 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3205 if ((size_t)size > dma_bytes)
3207 if (offset > dma_bytes - size)
3210 if (substream->ops->mmap)
3211 return substream->ops->mmap(substream, area);
3213 return snd_pcm_default_mmap(substream, area);
3216 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3218 struct snd_pcm_file * pcm_file;
3219 struct snd_pcm_substream *substream;
3220 unsigned long offset;
3222 pcm_file = file->private_data;
3223 substream = pcm_file->substream;
3224 snd_assert(substream != NULL, return -ENXIO);
3226 offset = area->vm_pgoff << PAGE_SHIFT;
3228 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3229 if (substream->no_mmap_ctrl)
3231 return snd_pcm_mmap_status(substream, file, area);
3232 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3233 if (substream->no_mmap_ctrl)
3235 return snd_pcm_mmap_control(substream, file, area);
3237 return snd_pcm_mmap_data(substream, file, area);
3242 static int snd_pcm_fasync(int fd, struct file * file, int on)
3244 struct snd_pcm_file * pcm_file;
3245 struct snd_pcm_substream *substream;
3246 struct snd_pcm_runtime *runtime;
3249 pcm_file = file->private_data;
3250 substream = pcm_file->substream;
3251 snd_assert(substream != NULL, return -ENXIO);
3252 runtime = substream->runtime;
3254 err = fasync_helper(fd, file, on, &runtime->fasync);
3263 #ifdef CONFIG_COMPAT
3264 #include "pcm_compat.c"
3266 #define snd_pcm_ioctl_compat NULL
3270 * To be removed helpers to keep binary compatibility
3273 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3274 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3276 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3277 struct snd_pcm_hw_params_old *oparams)
3281 memset(params, 0, sizeof(*params));
3282 params->flags = oparams->flags;
3283 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3284 params->masks[i].bits[0] = oparams->masks[i];
3285 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3286 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3287 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3288 params->info = oparams->info;
3289 params->msbits = oparams->msbits;
3290 params->rate_num = oparams->rate_num;
3291 params->rate_den = oparams->rate_den;
3292 params->fifo_size = oparams->fifo_size;
3295 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3296 struct snd_pcm_hw_params *params)
3300 memset(oparams, 0, sizeof(*oparams));
3301 oparams->flags = params->flags;
3302 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3303 oparams->masks[i] = params->masks[i].bits[0];
3304 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3305 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3306 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3307 oparams->info = params->info;
3308 oparams->msbits = params->msbits;
3309 oparams->rate_num = params->rate_num;
3310 oparams->rate_den = params->rate_den;
3311 oparams->fifo_size = params->fifo_size;
3314 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3315 struct snd_pcm_hw_params_old __user * _oparams)
3317 struct snd_pcm_hw_params *params;
3318 struct snd_pcm_hw_params_old *oparams = NULL;
3321 params = kmalloc(sizeof(*params), GFP_KERNEL);
3326 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3332 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3336 snd_pcm_hw_convert_from_old_params(params, oparams);
3337 err = snd_pcm_hw_refine(substream, params);
3338 snd_pcm_hw_convert_to_old_params(oparams, params);
3339 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3349 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3350 struct snd_pcm_hw_params_old __user * _oparams)
3352 struct snd_pcm_hw_params *params;
3353 struct snd_pcm_hw_params_old *oparams = NULL;
3356 params = kmalloc(sizeof(*params), GFP_KERNEL);
3361 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3366 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3370 snd_pcm_hw_convert_from_old_params(params, oparams);
3371 err = snd_pcm_hw_params(substream, params);
3372 snd_pcm_hw_convert_to_old_params(oparams, params);
3373 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3387 struct file_operations snd_pcm_f_ops[2] = {
3389 .owner = THIS_MODULE,
3390 .write = snd_pcm_write,
3391 .writev = snd_pcm_writev,
3392 .open = snd_pcm_playback_open,
3393 .release = snd_pcm_release,
3394 .poll = snd_pcm_playback_poll,
3395 .unlocked_ioctl = snd_pcm_playback_ioctl,
3396 .compat_ioctl = snd_pcm_ioctl_compat,
3397 .mmap = snd_pcm_mmap,
3398 .fasync = snd_pcm_fasync,
3401 .owner = THIS_MODULE,
3402 .read = snd_pcm_read,
3403 .readv = snd_pcm_readv,
3404 .open = snd_pcm_capture_open,
3405 .release = snd_pcm_release,
3406 .poll = snd_pcm_capture_poll,
3407 .unlocked_ioctl = snd_pcm_capture_ioctl,
3408 .compat_ioctl = snd_pcm_ioctl_compat,
3409 .mmap = snd_pcm_mmap,
3410 .fasync = snd_pcm_fasync,