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 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
67 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock);
74 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 static inline mm_segment_t snd_enter_user(void)
79 mm_segment_t fs = get_fs();
84 static inline void snd_leave_user(mm_segment_t fs)
91 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
93 struct snd_pcm_runtime *runtime;
94 struct snd_pcm *pcm = substream->pcm;
95 struct snd_pcm_str *pstr = substream->pstr;
97 snd_assert(substream != NULL, return -ENXIO);
98 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
122 struct snd_pcm_info *info;
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
128 err = snd_pcm_info(substream, info);
130 if (copy_to_user(_info, info, sizeof(*info)))
140 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141 char *snd_pcm_hw_param_names[] = {
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
160 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
186 if (!(params->rmask & (1 << k)))
189 printk("%s = ", snd_pcm_hw_param_names[k]);
190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197 params->cmask |= 1 << k;
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
206 if (!(params->rmask & (1 << k)))
209 printk("%s = ", snd_pcm_hw_param_names[k]);
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
228 params->cmask |= 1 << k;
233 for (k = 0; k < constrs->rules_num; k++)
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
239 for (k = 0; k < constrs->rules_num; k++) {
240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
243 if (r->cond && !(r->cond & params->flags))
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
254 printk("Rule %d [%p]: ", k, r->func);
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
261 i = hw_param_interval(params, r->var);
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
271 changed = r->func(params, r);
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
313 hw = &substream->runtime->hw;
315 params->info = hw->info;
316 if (!params->fifo_size)
317 params->fifo_size = hw->fifo_size;
322 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params __user * _params)
325 struct snd_pcm_hw_params *params;
328 params = kmalloc(sizeof(*params), GFP_KERNEL);
333 if (copy_from_user(params, _params, sizeof(*params))) {
337 err = snd_pcm_hw_refine(substream, params);
338 if (copy_to_user(_params, params, sizeof(*params))) {
347 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
348 struct snd_pcm_hw_params *params)
350 struct snd_pcm_runtime *runtime;
353 snd_pcm_uframes_t frames;
355 snd_assert(substream != NULL, return -ENXIO);
356 runtime = substream->runtime;
357 snd_assert(runtime != NULL, return -ENXIO);
358 snd_pcm_stream_lock_irq(substream);
359 switch (runtime->status->state) {
360 case SNDRV_PCM_STATE_OPEN:
361 case SNDRV_PCM_STATE_SETUP:
362 case SNDRV_PCM_STATE_PREPARED:
365 snd_pcm_stream_unlock_irq(substream);
368 snd_pcm_stream_unlock_irq(substream);
369 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
370 if (!substream->oss.oss)
372 if (atomic_read(&runtime->mmap_count))
376 err = snd_pcm_hw_refine(substream, params);
380 err = snd_pcm_hw_params_choose(substream, params);
384 if (substream->ops->hw_params != NULL) {
385 err = substream->ops->hw_params(substream, params);
390 runtime->access = params_access(params);
391 runtime->format = params_format(params);
392 runtime->subformat = params_subformat(params);
393 runtime->channels = params_channels(params);
394 runtime->rate = params_rate(params);
395 runtime->period_size = params_period_size(params);
396 runtime->periods = params_periods(params);
397 runtime->buffer_size = params_buffer_size(params);
398 runtime->tick_time = params_tick_time(params);
399 runtime->info = params->info;
400 runtime->rate_num = params->rate_num;
401 runtime->rate_den = params->rate_den;
403 bits = snd_pcm_format_physical_width(runtime->format);
404 runtime->sample_bits = bits;
405 bits *= runtime->channels;
406 runtime->frame_bits = bits;
408 while (bits % 8 != 0) {
412 runtime->byte_align = bits / 8;
413 runtime->min_align = frames;
415 /* Default sw params */
416 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
417 runtime->period_step = 1;
418 runtime->sleep_min = 0;
419 runtime->control->avail_min = runtime->period_size;
420 runtime->xfer_align = runtime->period_size;
421 runtime->start_threshold = 1;
422 runtime->stop_threshold = runtime->buffer_size;
423 runtime->silence_threshold = 0;
424 runtime->silence_size = 0;
425 runtime->boundary = runtime->buffer_size;
426 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
427 runtime->boundary *= 2;
429 snd_pcm_timer_resolution_change(substream);
430 runtime->status->state = SNDRV_PCM_STATE_SETUP;
433 /* hardware might be unuseable from this time,
434 so we force application to retry to set
435 the correct hardware parameter settings */
436 runtime->status->state = SNDRV_PCM_STATE_OPEN;
437 if (substream->ops->hw_free != NULL)
438 substream->ops->hw_free(substream);
442 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
443 struct snd_pcm_hw_params __user * _params)
445 struct snd_pcm_hw_params *params;
448 params = kmalloc(sizeof(*params), GFP_KERNEL);
453 if (copy_from_user(params, _params, sizeof(*params))) {
457 err = snd_pcm_hw_params(substream, params);
458 if (copy_to_user(_params, params, sizeof(*params))) {
467 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
469 struct snd_pcm_runtime *runtime;
472 snd_assert(substream != NULL, return -ENXIO);
473 runtime = substream->runtime;
474 snd_assert(runtime != NULL, return -ENXIO);
475 snd_pcm_stream_lock_irq(substream);
476 switch (runtime->status->state) {
477 case SNDRV_PCM_STATE_SETUP:
478 case SNDRV_PCM_STATE_PREPARED:
481 snd_pcm_stream_unlock_irq(substream);
484 snd_pcm_stream_unlock_irq(substream);
485 if (atomic_read(&runtime->mmap_count))
487 if (substream->ops->hw_free)
488 result = substream->ops->hw_free(substream);
489 runtime->status->state = SNDRV_PCM_STATE_OPEN;
493 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
494 struct snd_pcm_sw_params *params)
496 struct snd_pcm_runtime *runtime;
498 snd_assert(substream != NULL, return -ENXIO);
499 runtime = substream->runtime;
500 snd_assert(runtime != NULL, return -ENXIO);
501 snd_pcm_stream_lock_irq(substream);
502 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
503 snd_pcm_stream_unlock_irq(substream);
506 snd_pcm_stream_unlock_irq(substream);
508 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
510 if (params->avail_min == 0)
512 if (params->xfer_align == 0 ||
513 params->xfer_align % runtime->min_align != 0)
515 if (params->silence_size >= runtime->boundary) {
516 if (params->silence_threshold != 0)
519 if (params->silence_size > params->silence_threshold)
521 if (params->silence_threshold > runtime->buffer_size)
524 snd_pcm_stream_lock_irq(substream);
525 runtime->tstamp_mode = params->tstamp_mode;
526 runtime->sleep_min = params->sleep_min;
527 runtime->period_step = params->period_step;
528 runtime->control->avail_min = params->avail_min;
529 runtime->start_threshold = params->start_threshold;
530 runtime->stop_threshold = params->stop_threshold;
531 runtime->silence_threshold = params->silence_threshold;
532 runtime->silence_size = params->silence_size;
533 runtime->xfer_align = params->xfer_align;
534 params->boundary = runtime->boundary;
535 if (snd_pcm_running(substream)) {
536 if (runtime->sleep_min)
537 snd_pcm_tick_prepare(substream);
539 snd_pcm_tick_set(substream, 0);
540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
541 runtime->silence_size > 0)
542 snd_pcm_playback_silence(substream, ULONG_MAX);
543 wake_up(&runtime->sleep);
545 snd_pcm_stream_unlock_irq(substream);
549 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
550 struct snd_pcm_sw_params __user * _params)
552 struct snd_pcm_sw_params params;
554 if (copy_from_user(¶ms, _params, sizeof(params)))
556 err = snd_pcm_sw_params(substream, ¶ms);
557 if (copy_to_user(_params, ¶ms, sizeof(params)))
562 int snd_pcm_status(struct snd_pcm_substream *substream,
563 struct snd_pcm_status *status)
565 struct snd_pcm_runtime *runtime = substream->runtime;
567 snd_pcm_stream_lock_irq(substream);
568 status->state = runtime->status->state;
569 status->suspended_state = runtime->status->suspended_state;
570 if (status->state == SNDRV_PCM_STATE_OPEN)
572 status->trigger_tstamp = runtime->trigger_tstamp;
573 if (snd_pcm_running(substream)) {
574 snd_pcm_update_hw_ptr(substream);
575 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
576 status->tstamp = runtime->status->tstamp;
578 getnstimeofday(&status->tstamp);
580 getnstimeofday(&status->tstamp);
581 status->appl_ptr = runtime->control->appl_ptr;
582 status->hw_ptr = runtime->status->hw_ptr;
583 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
584 status->avail = snd_pcm_playback_avail(runtime);
585 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
586 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
587 status->delay = runtime->buffer_size - status->avail;
591 status->avail = snd_pcm_capture_avail(runtime);
592 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
593 status->delay = status->avail;
597 status->avail_max = runtime->avail_max;
598 status->overrange = runtime->overrange;
599 runtime->avail_max = 0;
600 runtime->overrange = 0;
602 snd_pcm_stream_unlock_irq(substream);
606 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
607 struct snd_pcm_status __user * _status)
609 struct snd_pcm_status status;
610 struct snd_pcm_runtime *runtime;
613 snd_assert(substream != NULL, return -ENXIO);
614 runtime = substream->runtime;
615 memset(&status, 0, sizeof(status));
616 res = snd_pcm_status(substream, &status);
619 if (copy_to_user(_status, &status, sizeof(status)))
624 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
625 struct snd_pcm_channel_info * info)
627 struct snd_pcm_runtime *runtime;
628 unsigned int channel;
630 snd_assert(substream != NULL, return -ENXIO);
631 channel = info->channel;
632 runtime = substream->runtime;
633 snd_pcm_stream_lock_irq(substream);
634 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
635 snd_pcm_stream_unlock_irq(substream);
638 snd_pcm_stream_unlock_irq(substream);
639 if (channel >= runtime->channels)
641 memset(info, 0, sizeof(*info));
642 info->channel = channel;
643 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
646 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
647 struct snd_pcm_channel_info __user * _info)
649 struct snd_pcm_channel_info info;
652 if (copy_from_user(&info, _info, sizeof(info)))
654 res = snd_pcm_channel_info(substream, &info);
657 if (copy_to_user(_info, &info, sizeof(info)))
662 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 if (runtime->trigger_master == NULL)
667 if (runtime->trigger_master == substream) {
668 getnstimeofday(&runtime->trigger_tstamp);
670 snd_pcm_trigger_tstamp(runtime->trigger_master);
671 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
673 runtime->trigger_master = NULL;
677 int (*pre_action)(struct snd_pcm_substream *substream, int state);
678 int (*do_action)(struct snd_pcm_substream *substream, int state);
679 void (*undo_action)(struct snd_pcm_substream *substream, int state);
680 void (*post_action)(struct snd_pcm_substream *substream, int state);
684 * this functions is core for handling of linked stream
685 * Note: the stream state might be changed also on failure
686 * Note2: call with calling stream lock + link lock
688 static int snd_pcm_action_group(struct action_ops *ops,
689 struct snd_pcm_substream *substream,
690 int state, int do_lock)
692 struct list_head *pos;
693 struct snd_pcm_substream *s = NULL;
694 struct snd_pcm_substream *s1;
697 snd_pcm_group_for_each(pos, substream) {
698 s = snd_pcm_group_substream_entry(pos);
699 if (do_lock && s != substream)
700 spin_lock(&s->self_group.lock);
701 res = ops->pre_action(s, state);
705 snd_pcm_group_for_each(pos, substream) {
706 s = snd_pcm_group_substream_entry(pos);
707 res = ops->do_action(s, state);
709 if (ops->undo_action) {
710 snd_pcm_group_for_each(pos, substream) {
711 s1 = snd_pcm_group_substream_entry(pos);
712 if (s1 == s) /* failed stream */
714 ops->undo_action(s1, state);
717 s = NULL; /* unlock all */
721 snd_pcm_group_for_each(pos, substream) {
722 s = snd_pcm_group_substream_entry(pos);
723 ops->post_action(s, state);
728 snd_pcm_group_for_each(pos, substream) {
729 s1 = snd_pcm_group_substream_entry(pos);
731 spin_unlock(&s1->self_group.lock);
732 if (s1 == s) /* end */
740 * Note: call with stream lock
742 static int snd_pcm_action_single(struct action_ops *ops,
743 struct snd_pcm_substream *substream,
748 res = ops->pre_action(substream, state);
751 res = ops->do_action(substream, state);
753 ops->post_action(substream, state);
754 else if (ops->undo_action)
755 ops->undo_action(substream, state);
760 * Note: call with stream lock
762 static int snd_pcm_action(struct action_ops *ops,
763 struct snd_pcm_substream *substream,
768 if (snd_pcm_stream_linked(substream)) {
769 if (!spin_trylock(&substream->group->lock)) {
770 spin_unlock(&substream->self_group.lock);
771 spin_lock(&substream->group->lock);
772 spin_lock(&substream->self_group.lock);
774 res = snd_pcm_action_group(ops, substream, state, 1);
775 spin_unlock(&substream->group->lock);
777 res = snd_pcm_action_single(ops, substream, state);
783 * Note: don't use any locks before
785 static int snd_pcm_action_lock_irq(struct action_ops *ops,
786 struct snd_pcm_substream *substream,
791 read_lock_irq(&snd_pcm_link_rwlock);
792 if (snd_pcm_stream_linked(substream)) {
793 spin_lock(&substream->group->lock);
794 spin_lock(&substream->self_group.lock);
795 res = snd_pcm_action_group(ops, substream, state, 1);
796 spin_unlock(&substream->self_group.lock);
797 spin_unlock(&substream->group->lock);
799 spin_lock(&substream->self_group.lock);
800 res = snd_pcm_action_single(ops, substream, state);
801 spin_unlock(&substream->self_group.lock);
803 read_unlock_irq(&snd_pcm_link_rwlock);
809 static int snd_pcm_action_nonatomic(struct action_ops *ops,
810 struct snd_pcm_substream *substream,
815 down_read(&snd_pcm_link_rwsem);
816 if (snd_pcm_stream_linked(substream))
817 res = snd_pcm_action_group(ops, substream, state, 0);
819 res = snd_pcm_action_single(ops, substream, state);
820 up_read(&snd_pcm_link_rwsem);
827 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
829 struct snd_pcm_runtime *runtime = substream->runtime;
830 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
832 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
833 !snd_pcm_playback_data(substream))
835 runtime->trigger_master = substream;
839 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
841 if (substream->runtime->trigger_master != substream)
843 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
846 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
848 if (substream->runtime->trigger_master == substream)
849 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
852 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
854 struct snd_pcm_runtime *runtime = substream->runtime;
855 snd_pcm_trigger_tstamp(substream);
856 runtime->status->state = state;
857 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
858 runtime->silence_size > 0)
859 snd_pcm_playback_silence(substream, ULONG_MAX);
860 if (runtime->sleep_min)
861 snd_pcm_tick_prepare(substream);
862 if (substream->timer)
863 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
864 &runtime->trigger_tstamp);
867 static struct action_ops snd_pcm_action_start = {
868 .pre_action = snd_pcm_pre_start,
869 .do_action = snd_pcm_do_start,
870 .undo_action = snd_pcm_undo_start,
871 .post_action = snd_pcm_post_start
876 * @substream: the PCM substream instance
878 * Start all linked streams.
880 int snd_pcm_start(struct snd_pcm_substream *substream)
882 return snd_pcm_action(&snd_pcm_action_start, substream,
883 SNDRV_PCM_STATE_RUNNING);
889 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
891 struct snd_pcm_runtime *runtime = substream->runtime;
892 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
894 runtime->trigger_master = substream;
898 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
900 if (substream->runtime->trigger_master == substream &&
901 snd_pcm_running(substream))
902 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
903 return 0; /* unconditonally stop all substreams */
906 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
908 struct snd_pcm_runtime *runtime = substream->runtime;
909 if (runtime->status->state != state) {
910 snd_pcm_trigger_tstamp(substream);
911 if (substream->timer)
912 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
913 &runtime->trigger_tstamp);
914 runtime->status->state = state;
915 snd_pcm_tick_set(substream, 0);
917 wake_up(&runtime->sleep);
920 static struct action_ops snd_pcm_action_stop = {
921 .pre_action = snd_pcm_pre_stop,
922 .do_action = snd_pcm_do_stop,
923 .post_action = snd_pcm_post_stop
928 * @substream: the PCM substream instance
929 * @state: PCM state after stopping the stream
931 * Try to stop all running streams in the substream group.
932 * The state of each stream is changed to the given value after that unconditionally.
934 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
936 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
941 * @substream: the PCM substream
943 * Stop the DMA only when the given stream is playback.
944 * The state is changed to SETUP.
945 * Unlike snd_pcm_stop(), this affects only the given stream.
947 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
949 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
950 SNDRV_PCM_STATE_SETUP);
956 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
958 struct snd_pcm_runtime *runtime = substream->runtime;
959 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
962 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
964 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
966 runtime->trigger_master = substream;
970 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
972 if (substream->runtime->trigger_master != substream)
974 return substream->ops->trigger(substream,
975 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
976 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
979 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
981 if (substream->runtime->trigger_master == substream)
982 substream->ops->trigger(substream,
983 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
984 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
987 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
989 struct snd_pcm_runtime *runtime = substream->runtime;
990 snd_pcm_trigger_tstamp(substream);
992 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
993 if (substream->timer)
994 snd_timer_notify(substream->timer,
995 SNDRV_TIMER_EVENT_MPAUSE,
996 &runtime->trigger_tstamp);
997 snd_pcm_tick_set(substream, 0);
998 wake_up(&runtime->sleep);
1000 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1001 if (runtime->sleep_min)
1002 snd_pcm_tick_prepare(substream);
1003 if (substream->timer)
1004 snd_timer_notify(substream->timer,
1005 SNDRV_TIMER_EVENT_MCONTINUE,
1006 &runtime->trigger_tstamp);
1010 static struct action_ops snd_pcm_action_pause = {
1011 .pre_action = snd_pcm_pre_pause,
1012 .do_action = snd_pcm_do_pause,
1013 .undo_action = snd_pcm_undo_pause,
1014 .post_action = snd_pcm_post_pause
1018 * Push/release the pause for all linked streams.
1020 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1022 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1028 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1030 struct snd_pcm_runtime *runtime = substream->runtime;
1031 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1033 runtime->trigger_master = substream;
1037 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1039 struct snd_pcm_runtime *runtime = substream->runtime;
1040 if (runtime->trigger_master != substream)
1042 if (! snd_pcm_running(substream))
1044 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1045 return 0; /* suspend unconditionally */
1048 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1050 struct snd_pcm_runtime *runtime = substream->runtime;
1051 snd_pcm_trigger_tstamp(substream);
1052 if (substream->timer)
1053 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1054 &runtime->trigger_tstamp);
1055 runtime->status->suspended_state = runtime->status->state;
1056 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1057 snd_pcm_tick_set(substream, 0);
1058 wake_up(&runtime->sleep);
1061 static struct action_ops snd_pcm_action_suspend = {
1062 .pre_action = snd_pcm_pre_suspend,
1063 .do_action = snd_pcm_do_suspend,
1064 .post_action = snd_pcm_post_suspend
1069 * @substream: the PCM substream
1071 * Trigger SUSPEND to all linked streams.
1072 * After this call, all streams are changed to SUSPENDED state.
1074 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1077 unsigned long flags;
1082 snd_pcm_stream_lock_irqsave(substream, flags);
1083 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1084 snd_pcm_stream_unlock_irqrestore(substream, flags);
1089 * snd_pcm_suspend_all
1090 * @pcm: the PCM instance
1092 * Trigger SUSPEND to all substreams in the given pcm.
1093 * After this call, all streams are changed to SUSPENDED state.
1095 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1097 struct snd_pcm_substream *substream;
1098 int stream, err = 0;
1103 for (stream = 0; stream < 2; stream++) {
1104 for (substream = pcm->streams[stream].substream;
1105 substream; substream = substream->next) {
1106 /* FIXME: the open/close code should lock this as well */
1107 if (substream->runtime == NULL)
1109 err = snd_pcm_suspend(substream);
1110 if (err < 0 && err != -EBUSY)
1119 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1121 struct snd_pcm_runtime *runtime = substream->runtime;
1122 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1124 runtime->trigger_master = substream;
1128 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1130 struct snd_pcm_runtime *runtime = substream->runtime;
1131 if (runtime->trigger_master != substream)
1133 /* DMA not running previously? */
1134 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1135 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1136 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1138 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1141 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1143 if (substream->runtime->trigger_master == substream &&
1144 snd_pcm_running(substream))
1145 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1148 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1150 struct snd_pcm_runtime *runtime = substream->runtime;
1151 snd_pcm_trigger_tstamp(substream);
1152 if (substream->timer)
1153 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1154 &runtime->trigger_tstamp);
1155 runtime->status->state = runtime->status->suspended_state;
1156 if (runtime->sleep_min)
1157 snd_pcm_tick_prepare(substream);
1160 static struct action_ops snd_pcm_action_resume = {
1161 .pre_action = snd_pcm_pre_resume,
1162 .do_action = snd_pcm_do_resume,
1163 .undo_action = snd_pcm_undo_resume,
1164 .post_action = snd_pcm_post_resume
1167 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1169 struct snd_card *card = substream->pcm->card;
1172 snd_power_lock(card);
1173 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1174 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1175 snd_power_unlock(card);
1181 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1186 #endif /* CONFIG_PM */
1191 * Change the RUNNING stream(s) to XRUN state.
1193 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1195 struct snd_card *card = substream->pcm->card;
1196 struct snd_pcm_runtime *runtime = substream->runtime;
1199 snd_power_lock(card);
1200 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1201 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1206 snd_pcm_stream_lock_irq(substream);
1207 switch (runtime->status->state) {
1208 case SNDRV_PCM_STATE_XRUN:
1209 result = 0; /* already there */
1211 case SNDRV_PCM_STATE_RUNNING:
1212 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1217 snd_pcm_stream_unlock_irq(substream);
1219 snd_power_unlock(card);
1226 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1228 struct snd_pcm_runtime *runtime = substream->runtime;
1229 switch (runtime->status->state) {
1230 case SNDRV_PCM_STATE_RUNNING:
1231 case SNDRV_PCM_STATE_PREPARED:
1232 case SNDRV_PCM_STATE_PAUSED:
1233 case SNDRV_PCM_STATE_SUSPENDED:
1240 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1242 struct snd_pcm_runtime *runtime = substream->runtime;
1243 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1246 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1247 runtime->hw_ptr_base = 0;
1248 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1249 runtime->status->hw_ptr % runtime->period_size;
1250 runtime->silence_start = runtime->status->hw_ptr;
1251 runtime->silence_filled = 0;
1255 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1257 struct snd_pcm_runtime *runtime = substream->runtime;
1258 runtime->control->appl_ptr = runtime->status->hw_ptr;
1259 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1260 runtime->silence_size > 0)
1261 snd_pcm_playback_silence(substream, ULONG_MAX);
1264 static struct action_ops snd_pcm_action_reset = {
1265 .pre_action = snd_pcm_pre_reset,
1266 .do_action = snd_pcm_do_reset,
1267 .post_action = snd_pcm_post_reset
1270 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1272 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1278 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
1280 struct snd_pcm_runtime *runtime = substream->runtime;
1281 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1283 if (snd_pcm_running(substream))
1288 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1291 err = substream->ops->prepare(substream);
1294 return snd_pcm_do_reset(substream, 0);
1297 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1299 struct snd_pcm_runtime *runtime = substream->runtime;
1300 runtime->control->appl_ptr = runtime->status->hw_ptr;
1301 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1304 static struct action_ops snd_pcm_action_prepare = {
1305 .pre_action = snd_pcm_pre_prepare,
1306 .do_action = snd_pcm_do_prepare,
1307 .post_action = snd_pcm_post_prepare
1312 * @substream: the PCM substream instance
1314 * Prepare the PCM substream to be triggerable.
1316 static int snd_pcm_prepare(struct snd_pcm_substream *substream)
1319 struct snd_card *card = substream->pcm->card;
1321 snd_power_lock(card);
1322 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1323 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1324 snd_power_unlock(card);
1332 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1334 if (substream->ffile->f_flags & O_NONBLOCK)
1336 substream->runtime->trigger_master = substream;
1340 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1342 struct snd_pcm_runtime *runtime = substream->runtime;
1343 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1344 switch (runtime->status->state) {
1345 case SNDRV_PCM_STATE_PREPARED:
1346 /* start playback stream if possible */
1347 if (! snd_pcm_playback_empty(substream)) {
1348 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1349 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1352 case SNDRV_PCM_STATE_RUNNING:
1353 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1359 /* stop running stream */
1360 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1361 int state = snd_pcm_capture_avail(runtime) > 0 ?
1362 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1363 snd_pcm_do_stop(substream, state);
1364 snd_pcm_post_stop(substream, state);
1370 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1374 static struct action_ops snd_pcm_action_drain_init = {
1375 .pre_action = snd_pcm_pre_drain_init,
1376 .do_action = snd_pcm_do_drain_init,
1377 .post_action = snd_pcm_post_drain_init
1381 struct snd_pcm_substream *substream;
1383 snd_pcm_uframes_t stop_threshold;
1386 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1389 * Drain the stream(s).
1390 * When the substream is linked, sync until the draining of all playback streams
1392 * After this call, all streams are supposed to be either SETUP or DRAINING
1393 * (capture only) state.
1395 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1397 struct snd_card *card;
1398 struct snd_pcm_runtime *runtime;
1399 struct list_head *pos;
1402 struct drain_rec *drec, drec_tmp, *d;
1404 snd_assert(substream != NULL, return -ENXIO);
1405 card = substream->pcm->card;
1406 runtime = substream->runtime;
1408 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1411 snd_power_lock(card);
1412 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1413 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1415 snd_power_unlock(card);
1420 /* allocate temporary record for drain sync */
1421 down_read(&snd_pcm_link_rwsem);
1422 if (snd_pcm_stream_linked(substream)) {
1423 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1425 up_read(&snd_pcm_link_rwsem);
1426 snd_power_unlock(card);
1432 /* count only playback streams */
1434 snd_pcm_group_for_each(pos, substream) {
1435 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1436 runtime = s->runtime;
1437 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1438 d = &drec[num_drecs++];
1440 init_waitqueue_entry(&d->wait, current);
1441 add_wait_queue(&runtime->sleep, &d->wait);
1442 /* stop_threshold fixup to avoid endless loop when
1443 * stop_threshold > buffer_size
1445 d->stop_threshold = runtime->stop_threshold;
1446 if (runtime->stop_threshold > runtime->buffer_size)
1447 runtime->stop_threshold = runtime->buffer_size;
1450 up_read(&snd_pcm_link_rwsem);
1454 snd_pcm_stream_lock_irq(substream);
1456 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1457 snd_pcm_pause(substream, 0);
1459 /* pre-start/stop - all running streams are changed to DRAINING state */
1460 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1462 snd_pcm_stream_unlock_irq(substream);
1468 if (signal_pending(current)) {
1469 result = -ERESTARTSYS;
1473 for (i = 0; i < num_drecs; i++) {
1474 runtime = drec[i].substream->runtime;
1475 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1479 break; /* yes, all drained */
1481 set_current_state(TASK_INTERRUPTIBLE);
1482 snd_pcm_stream_unlock_irq(substream);
1483 snd_power_unlock(card);
1484 tout = schedule_timeout(10 * HZ);
1485 snd_power_lock(card);
1486 snd_pcm_stream_lock_irq(substream);
1488 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1491 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1492 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1499 snd_pcm_stream_unlock_irq(substream);
1502 for (i = 0; i < num_drecs; i++) {
1504 runtime = d->substream->runtime;
1505 remove_wait_queue(&runtime->sleep, &d->wait);
1506 runtime->stop_threshold = d->stop_threshold;
1509 if (drec != &drec_tmp)
1511 snd_power_unlock(card);
1519 * Immediately put all linked substreams into SETUP state.
1521 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1523 struct snd_pcm_runtime *runtime;
1524 struct snd_card *card;
1527 snd_assert(substream != NULL, return -ENXIO);
1528 runtime = substream->runtime;
1529 card = substream->pcm->card;
1531 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1534 snd_power_lock(card);
1535 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1536 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1541 snd_pcm_stream_lock_irq(substream);
1543 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1544 snd_pcm_pause(substream, 0);
1546 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1547 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1548 snd_pcm_stream_unlock_irq(substream);
1550 snd_power_unlock(card);
1555 /* WARNING: Don't forget to fput back the file */
1556 static struct file *snd_pcm_file_fd(int fd)
1559 struct inode *inode;
1565 inode = file->f_dentry->d_inode;
1566 if (!S_ISCHR(inode->i_mode) ||
1567 imajor(inode) != snd_major) {
1571 minor = iminor(inode);
1572 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1573 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1583 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1587 struct snd_pcm_file *pcm_file;
1588 struct snd_pcm_substream *substream1;
1590 file = snd_pcm_file_fd(fd);
1593 pcm_file = file->private_data;
1594 substream1 = pcm_file->substream;
1595 down_write(&snd_pcm_link_rwsem);
1596 write_lock_irq(&snd_pcm_link_rwlock);
1597 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1598 substream->runtime->status->state != substream1->runtime->status->state) {
1602 if (snd_pcm_stream_linked(substream1)) {
1606 if (!snd_pcm_stream_linked(substream)) {
1607 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1608 if (substream->group == NULL) {
1612 spin_lock_init(&substream->group->lock);
1613 INIT_LIST_HEAD(&substream->group->substreams);
1614 list_add_tail(&substream->link_list, &substream->group->substreams);
1615 substream->group->count = 1;
1617 list_add_tail(&substream1->link_list, &substream->group->substreams);
1618 substream->group->count++;
1619 substream1->group = substream->group;
1621 write_unlock_irq(&snd_pcm_link_rwlock);
1622 up_write(&snd_pcm_link_rwsem);
1627 static void relink_to_local(struct snd_pcm_substream *substream)
1629 substream->group = &substream->self_group;
1630 INIT_LIST_HEAD(&substream->self_group.substreams);
1631 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1634 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1636 struct list_head *pos;
1639 down_write(&snd_pcm_link_rwsem);
1640 write_lock_irq(&snd_pcm_link_rwlock);
1641 if (!snd_pcm_stream_linked(substream)) {
1645 list_del(&substream->link_list);
1646 substream->group->count--;
1647 if (substream->group->count == 1) { /* detach the last stream, too */
1648 snd_pcm_group_for_each(pos, substream) {
1649 relink_to_local(snd_pcm_group_substream_entry(pos));
1652 kfree(substream->group);
1654 relink_to_local(substream);
1656 write_unlock_irq(&snd_pcm_link_rwlock);
1657 up_write(&snd_pcm_link_rwsem);
1664 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1665 struct snd_pcm_hw_rule *rule)
1667 struct snd_interval t;
1668 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1669 hw_param_interval_c(params, rule->deps[1]), &t);
1670 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1673 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1674 struct snd_pcm_hw_rule *rule)
1676 struct snd_interval t;
1677 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1678 hw_param_interval_c(params, rule->deps[1]), &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1682 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1685 struct snd_interval t;
1686 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1687 hw_param_interval_c(params, rule->deps[1]),
1688 (unsigned long) rule->private, &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1692 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
1695 struct snd_interval t;
1696 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1697 (unsigned long) rule->private,
1698 hw_param_interval_c(params, rule->deps[1]), &t);
1699 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1702 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1703 struct snd_pcm_hw_rule *rule)
1706 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1708 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1710 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1712 if (! snd_mask_test(mask, k))
1714 bits = snd_pcm_format_physical_width(k);
1716 continue; /* ignore invalid formats */
1717 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1718 snd_mask_reset(&m, k);
1720 return snd_mask_refine(mask, &m);
1723 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1724 struct snd_pcm_hw_rule *rule)
1726 struct snd_interval t;
1732 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1734 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1736 bits = snd_pcm_format_physical_width(k);
1738 continue; /* ignore invalid formats */
1739 if (t.min > (unsigned)bits)
1741 if (t.max < (unsigned)bits)
1745 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1748 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1749 #error "Change this table"
1752 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1753 48000, 64000, 88200, 96000, 176400, 192000 };
1755 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1756 struct snd_pcm_hw_rule *rule)
1758 struct snd_pcm_hardware *hw = rule->private;
1759 return snd_interval_list(hw_param_interval(params, rule->var),
1760 ARRAY_SIZE(rates), rates, hw->rates);
1763 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1764 struct snd_pcm_hw_rule *rule)
1766 struct snd_interval t;
1767 struct snd_pcm_substream *substream = rule->private;
1769 t.max = substream->buffer_bytes_max;
1773 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1776 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1778 struct snd_pcm_runtime *runtime = substream->runtime;
1779 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1782 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1783 snd_mask_any(constrs_mask(constrs, k));
1786 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1787 snd_interval_any(constrs_interval(constrs, k));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1794 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1796 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1797 snd_pcm_hw_rule_format, NULL,
1798 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1801 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1802 snd_pcm_hw_rule_sample_bits, NULL,
1803 SNDRV_PCM_HW_PARAM_FORMAT,
1804 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1808 snd_pcm_hw_rule_div, NULL,
1809 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1812 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1813 snd_pcm_hw_rule_mul, NULL,
1814 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1818 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1819 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1823 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1824 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1828 snd_pcm_hw_rule_div, NULL,
1829 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1833 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1834 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1838 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1839 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1843 snd_pcm_hw_rule_div, NULL,
1844 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1848 snd_pcm_hw_rule_div, NULL,
1849 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1853 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1854 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1858 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1859 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1863 snd_pcm_hw_rule_mul, NULL,
1864 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1868 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1869 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1873 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1878 snd_pcm_hw_rule_muldivk, (void*) 8,
1879 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1883 snd_pcm_hw_rule_muldivk, (void*) 8,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1888 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1889 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1893 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1894 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1900 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1902 struct snd_pcm_runtime *runtime = substream->runtime;
1903 struct snd_pcm_hardware *hw = &runtime->hw;
1905 unsigned int mask = 0;
1907 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1908 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1909 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1910 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1911 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1912 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1913 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1914 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1915 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1916 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1917 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1919 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1920 snd_assert(err >= 0, return -EINVAL);
1922 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1923 snd_assert(err >= 0, return -EINVAL);
1925 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1926 snd_assert(err >= 0, return -EINVAL);
1928 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1929 hw->channels_min, hw->channels_max);
1930 snd_assert(err >= 0, return -EINVAL);
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1933 hw->rate_min, hw->rate_max);
1934 snd_assert(err >= 0, return -EINVAL);
1936 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1937 hw->period_bytes_min, hw->period_bytes_max);
1938 snd_assert(err >= 0, return -EINVAL);
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1941 hw->periods_min, hw->periods_max);
1942 snd_assert(err >= 0, return -EINVAL);
1944 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1945 hw->period_bytes_min, hw->buffer_bytes_max);
1946 snd_assert(err >= 0, return -EINVAL);
1948 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1949 snd_pcm_hw_rule_buffer_bytes_max, substream,
1950 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1955 if (runtime->dma_bytes) {
1956 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1957 snd_assert(err >= 0, return -EINVAL);
1960 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1961 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1962 snd_pcm_hw_rule_rate, hw,
1963 SNDRV_PCM_HW_PARAM_RATE, -1);
1968 /* FIXME: this belong to lowlevel */
1969 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1970 1000000 / HZ, 1000000 / HZ);
1971 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1976 static void snd_pcm_add_file(struct snd_pcm_str *str,
1977 struct snd_pcm_file *pcm_file)
1979 pcm_file->next = str->files;
1980 str->files = pcm_file;
1983 static void snd_pcm_remove_file(struct snd_pcm_str *str,
1984 struct snd_pcm_file *pcm_file)
1986 struct snd_pcm_file * pcm_file1;
1987 if (str->files == pcm_file) {
1988 str->files = pcm_file->next;
1990 pcm_file1 = str->files;
1991 while (pcm_file1 && pcm_file1->next != pcm_file)
1992 pcm_file1 = pcm_file1->next;
1993 if (pcm_file1 != NULL)
1994 pcm_file1->next = pcm_file->next;
1998 static void pcm_release_private(struct snd_pcm_substream *substream)
2000 struct snd_pcm_file *pcm_file = substream->file;
2002 snd_pcm_unlink(substream);
2003 snd_pcm_remove_file(substream->pstr, pcm_file);
2007 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2009 snd_pcm_drop(substream);
2010 if (substream->hw_opened) {
2011 if (substream->ops->hw_free != NULL)
2012 substream->ops->hw_free(substream);
2013 substream->ops->close(substream);
2014 substream->hw_opened = 0;
2016 if (substream->pcm_release) {
2017 substream->pcm_release(substream);
2018 substream->pcm_release = NULL;
2020 snd_pcm_detach_substream(substream);
2023 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2025 struct snd_pcm_substream **rsubstream)
2027 struct snd_pcm_substream *substream;
2030 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2033 substream->no_mmap_ctrl = 0;
2034 err = snd_pcm_hw_constraints_init(substream);
2036 snd_printd("snd_pcm_hw_constraints_init failed\n");
2040 if ((err = substream->ops->open(substream)) < 0)
2043 substream->hw_opened = 1;
2045 err = snd_pcm_hw_constraints_complete(substream);
2047 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2051 *rsubstream = substream;
2055 snd_pcm_release_substream(substream);
2059 static int snd_pcm_open_file(struct file *file,
2060 struct snd_pcm *pcm,
2062 struct snd_pcm_file **rpcm_file)
2064 struct snd_pcm_file *pcm_file;
2065 struct snd_pcm_substream *substream;
2066 struct snd_pcm_str *str;
2069 snd_assert(rpcm_file != NULL, return -EINVAL);
2072 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2076 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2077 if (pcm_file == NULL) {
2078 snd_pcm_release_substream(substream);
2081 str = substream->pstr;
2082 substream->file = pcm_file;
2083 substream->pcm_release = pcm_release_private;
2084 pcm_file->substream = substream;
2085 snd_pcm_add_file(str, pcm_file);
2087 file->private_data = pcm_file;
2088 *rpcm_file = pcm_file;
2092 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2094 struct snd_pcm *pcm;
2096 pcm = snd_lookup_minor_data(iminor(inode),
2097 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2098 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2101 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2103 struct snd_pcm *pcm;
2105 pcm = snd_lookup_minor_data(iminor(inode),
2106 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2107 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2110 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2113 struct snd_pcm_file *pcm_file;
2120 err = snd_card_file_add(pcm->card, file);
2123 if (!try_module_get(pcm->card->module)) {
2127 init_waitqueue_entry(&wait, current);
2128 add_wait_queue(&pcm->open_wait, &wait);
2129 mutex_lock(&pcm->open_mutex);
2131 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2134 if (err == -EAGAIN) {
2135 if (file->f_flags & O_NONBLOCK) {
2141 set_current_state(TASK_INTERRUPTIBLE);
2142 mutex_unlock(&pcm->open_mutex);
2144 mutex_lock(&pcm->open_mutex);
2145 if (signal_pending(current)) {
2150 remove_wait_queue(&pcm->open_wait, &wait);
2151 mutex_unlock(&pcm->open_mutex);
2157 module_put(pcm->card->module);
2159 snd_card_file_remove(pcm->card, file);
2164 static int snd_pcm_release(struct inode *inode, struct file *file)
2166 struct snd_pcm *pcm;
2167 struct snd_pcm_substream *substream;
2168 struct snd_pcm_file *pcm_file;
2170 pcm_file = file->private_data;
2171 substream = pcm_file->substream;
2172 snd_assert(substream != NULL, return -ENXIO);
2173 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2174 pcm = substream->pcm;
2175 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2176 mutex_lock(&pcm->open_mutex);
2177 snd_pcm_release_substream(substream);
2178 mutex_unlock(&pcm->open_mutex);
2179 wake_up(&pcm->open_wait);
2180 module_put(pcm->card->module);
2181 snd_card_file_remove(pcm->card, file);
2185 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2186 snd_pcm_uframes_t frames)
2188 struct snd_pcm_runtime *runtime = substream->runtime;
2189 snd_pcm_sframes_t appl_ptr;
2190 snd_pcm_sframes_t ret;
2191 snd_pcm_sframes_t hw_avail;
2196 snd_pcm_stream_lock_irq(substream);
2197 switch (runtime->status->state) {
2198 case SNDRV_PCM_STATE_PREPARED:
2200 case SNDRV_PCM_STATE_DRAINING:
2201 case SNDRV_PCM_STATE_RUNNING:
2202 if (snd_pcm_update_hw_ptr(substream) >= 0)
2205 case SNDRV_PCM_STATE_XRUN:
2213 hw_avail = snd_pcm_playback_hw_avail(runtime);
2214 if (hw_avail <= 0) {
2218 if (frames > (snd_pcm_uframes_t)hw_avail)
2221 frames -= frames % runtime->xfer_align;
2222 appl_ptr = runtime->control->appl_ptr - frames;
2224 appl_ptr += runtime->boundary;
2225 runtime->control->appl_ptr = appl_ptr;
2226 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2228 snd_pcm_tick_prepare(substream);
2231 snd_pcm_stream_unlock_irq(substream);
2235 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2236 snd_pcm_uframes_t frames)
2238 struct snd_pcm_runtime *runtime = substream->runtime;
2239 snd_pcm_sframes_t appl_ptr;
2240 snd_pcm_sframes_t ret;
2241 snd_pcm_sframes_t hw_avail;
2246 snd_pcm_stream_lock_irq(substream);
2247 switch (runtime->status->state) {
2248 case SNDRV_PCM_STATE_PREPARED:
2249 case SNDRV_PCM_STATE_DRAINING:
2251 case SNDRV_PCM_STATE_RUNNING:
2252 if (snd_pcm_update_hw_ptr(substream) >= 0)
2255 case SNDRV_PCM_STATE_XRUN:
2263 hw_avail = snd_pcm_capture_hw_avail(runtime);
2264 if (hw_avail <= 0) {
2268 if (frames > (snd_pcm_uframes_t)hw_avail)
2271 frames -= frames % runtime->xfer_align;
2272 appl_ptr = runtime->control->appl_ptr - frames;
2274 appl_ptr += runtime->boundary;
2275 runtime->control->appl_ptr = appl_ptr;
2276 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2278 snd_pcm_tick_prepare(substream);
2281 snd_pcm_stream_unlock_irq(substream);
2285 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2286 snd_pcm_uframes_t frames)
2288 struct snd_pcm_runtime *runtime = substream->runtime;
2289 snd_pcm_sframes_t appl_ptr;
2290 snd_pcm_sframes_t ret;
2291 snd_pcm_sframes_t avail;
2296 snd_pcm_stream_lock_irq(substream);
2297 switch (runtime->status->state) {
2298 case SNDRV_PCM_STATE_PREPARED:
2299 case SNDRV_PCM_STATE_PAUSED:
2301 case SNDRV_PCM_STATE_DRAINING:
2302 case SNDRV_PCM_STATE_RUNNING:
2303 if (snd_pcm_update_hw_ptr(substream) >= 0)
2306 case SNDRV_PCM_STATE_XRUN:
2314 avail = snd_pcm_playback_avail(runtime);
2319 if (frames > (snd_pcm_uframes_t)avail)
2322 frames -= frames % runtime->xfer_align;
2323 appl_ptr = runtime->control->appl_ptr + frames;
2324 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2325 appl_ptr -= runtime->boundary;
2326 runtime->control->appl_ptr = appl_ptr;
2327 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2329 snd_pcm_tick_prepare(substream);
2332 snd_pcm_stream_unlock_irq(substream);
2336 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2337 snd_pcm_uframes_t frames)
2339 struct snd_pcm_runtime *runtime = substream->runtime;
2340 snd_pcm_sframes_t appl_ptr;
2341 snd_pcm_sframes_t ret;
2342 snd_pcm_sframes_t avail;
2347 snd_pcm_stream_lock_irq(substream);
2348 switch (runtime->status->state) {
2349 case SNDRV_PCM_STATE_PREPARED:
2350 case SNDRV_PCM_STATE_DRAINING:
2351 case SNDRV_PCM_STATE_PAUSED:
2353 case SNDRV_PCM_STATE_RUNNING:
2354 if (snd_pcm_update_hw_ptr(substream) >= 0)
2357 case SNDRV_PCM_STATE_XRUN:
2365 avail = snd_pcm_capture_avail(runtime);
2370 if (frames > (snd_pcm_uframes_t)avail)
2373 frames -= frames % runtime->xfer_align;
2374 appl_ptr = runtime->control->appl_ptr + frames;
2375 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2376 appl_ptr -= runtime->boundary;
2377 runtime->control->appl_ptr = appl_ptr;
2378 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2380 snd_pcm_tick_prepare(substream);
2383 snd_pcm_stream_unlock_irq(substream);
2387 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2389 struct snd_pcm_runtime *runtime = substream->runtime;
2392 snd_pcm_stream_lock_irq(substream);
2393 switch (runtime->status->state) {
2394 case SNDRV_PCM_STATE_DRAINING:
2395 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2397 case SNDRV_PCM_STATE_RUNNING:
2398 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2401 case SNDRV_PCM_STATE_PREPARED:
2402 case SNDRV_PCM_STATE_SUSPENDED:
2405 case SNDRV_PCM_STATE_XRUN:
2413 snd_pcm_stream_unlock_irq(substream);
2417 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2418 snd_pcm_sframes_t __user *res)
2420 struct snd_pcm_runtime *runtime = substream->runtime;
2422 snd_pcm_sframes_t n = 0;
2424 snd_pcm_stream_lock_irq(substream);
2425 switch (runtime->status->state) {
2426 case SNDRV_PCM_STATE_DRAINING:
2427 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2429 case SNDRV_PCM_STATE_RUNNING:
2430 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2433 case SNDRV_PCM_STATE_PREPARED:
2434 case SNDRV_PCM_STATE_SUSPENDED:
2436 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2437 n = snd_pcm_playback_hw_avail(runtime);
2439 n = snd_pcm_capture_avail(runtime);
2441 case SNDRV_PCM_STATE_XRUN:
2449 snd_pcm_stream_unlock_irq(substream);
2451 if (put_user(n, res))
2456 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2457 struct snd_pcm_sync_ptr __user *_sync_ptr)
2459 struct snd_pcm_runtime *runtime = substream->runtime;
2460 struct snd_pcm_sync_ptr sync_ptr;
2461 volatile struct snd_pcm_mmap_status *status;
2462 volatile struct snd_pcm_mmap_control *control;
2465 memset(&sync_ptr, 0, sizeof(sync_ptr));
2466 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2468 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2470 status = runtime->status;
2471 control = runtime->control;
2472 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2473 err = snd_pcm_hwsync(substream);
2477 snd_pcm_stream_lock_irq(substream);
2478 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2479 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2481 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2482 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2483 control->avail_min = sync_ptr.c.control.avail_min;
2485 sync_ptr.c.control.avail_min = control->avail_min;
2486 sync_ptr.s.status.state = status->state;
2487 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2488 sync_ptr.s.status.tstamp = status->tstamp;
2489 sync_ptr.s.status.suspended_state = status->suspended_state;
2490 snd_pcm_stream_unlock_irq(substream);
2491 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2496 static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
2497 unsigned int cmd, void __user *arg)
2499 snd_assert(substream != NULL, return -ENXIO);
2502 case SNDRV_PCM_IOCTL_PVERSION:
2503 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2504 case SNDRV_PCM_IOCTL_INFO:
2505 return snd_pcm_info_user(substream, arg);
2506 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2508 case SNDRV_PCM_IOCTL_HW_REFINE:
2509 return snd_pcm_hw_refine_user(substream, arg);
2510 case SNDRV_PCM_IOCTL_HW_PARAMS:
2511 return snd_pcm_hw_params_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_HW_FREE:
2513 return snd_pcm_hw_free(substream);
2514 case SNDRV_PCM_IOCTL_SW_PARAMS:
2515 return snd_pcm_sw_params_user(substream, arg);
2516 case SNDRV_PCM_IOCTL_STATUS:
2517 return snd_pcm_status_user(substream, arg);
2518 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2519 return snd_pcm_channel_info_user(substream, arg);
2520 case SNDRV_PCM_IOCTL_PREPARE:
2521 return snd_pcm_prepare(substream);
2522 case SNDRV_PCM_IOCTL_RESET:
2523 return snd_pcm_reset(substream);
2524 case SNDRV_PCM_IOCTL_START:
2525 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2526 case SNDRV_PCM_IOCTL_LINK:
2527 return snd_pcm_link(substream, (int)(unsigned long) arg);
2528 case SNDRV_PCM_IOCTL_UNLINK:
2529 return snd_pcm_unlink(substream);
2530 case SNDRV_PCM_IOCTL_RESUME:
2531 return snd_pcm_resume(substream);
2532 case SNDRV_PCM_IOCTL_XRUN:
2533 return snd_pcm_xrun(substream);
2534 case SNDRV_PCM_IOCTL_HWSYNC:
2535 return snd_pcm_hwsync(substream);
2536 case SNDRV_PCM_IOCTL_DELAY:
2537 return snd_pcm_delay(substream, arg);
2538 case SNDRV_PCM_IOCTL_SYNC_PTR:
2539 return snd_pcm_sync_ptr(substream, arg);
2540 #ifdef CONFIG_SND_SUPPORT_OLD_API
2541 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2542 return snd_pcm_hw_refine_old_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2544 return snd_pcm_hw_params_old_user(substream, arg);
2546 case SNDRV_PCM_IOCTL_DRAIN:
2547 return snd_pcm_drain(substream);
2548 case SNDRV_PCM_IOCTL_DROP:
2549 return snd_pcm_drop(substream);
2550 case SNDRV_PCM_IOCTL_PAUSE:
2553 snd_pcm_stream_lock_irq(substream);
2554 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2555 snd_pcm_stream_unlock_irq(substream);
2559 snd_printd("unknown ioctl = 0x%x\n", cmd);
2563 static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
2564 unsigned int cmd, void __user *arg)
2566 snd_assert(substream != NULL, return -ENXIO);
2567 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2569 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2571 struct snd_xferi xferi;
2572 struct snd_xferi __user *_xferi = arg;
2573 struct snd_pcm_runtime *runtime = substream->runtime;
2574 snd_pcm_sframes_t result;
2575 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2577 if (put_user(0, &_xferi->result))
2579 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2581 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2582 __put_user(result, &_xferi->result);
2583 return result < 0 ? result : 0;
2585 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2587 struct snd_xfern xfern;
2588 struct snd_xfern __user *_xfern = arg;
2589 struct snd_pcm_runtime *runtime = substream->runtime;
2591 snd_pcm_sframes_t result;
2592 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2594 if (runtime->channels > 128)
2596 if (put_user(0, &_xfern->result))
2598 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2600 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2603 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2607 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2609 __put_user(result, &_xfern->result);
2610 return result < 0 ? result : 0;
2612 case SNDRV_PCM_IOCTL_REWIND:
2614 snd_pcm_uframes_t frames;
2615 snd_pcm_uframes_t __user *_frames = arg;
2616 snd_pcm_sframes_t result;
2617 if (get_user(frames, _frames))
2619 if (put_user(0, _frames))
2621 result = snd_pcm_playback_rewind(substream, frames);
2622 __put_user(result, _frames);
2623 return result < 0 ? result : 0;
2625 case SNDRV_PCM_IOCTL_FORWARD:
2627 snd_pcm_uframes_t frames;
2628 snd_pcm_uframes_t __user *_frames = arg;
2629 snd_pcm_sframes_t result;
2630 if (get_user(frames, _frames))
2632 if (put_user(0, _frames))
2634 result = snd_pcm_playback_forward(substream, frames);
2635 __put_user(result, _frames);
2636 return result < 0 ? result : 0;
2639 return snd_pcm_common_ioctl1(substream, cmd, arg);
2642 static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
2643 unsigned int cmd, void __user *arg)
2645 snd_assert(substream != NULL, return -ENXIO);
2646 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2648 case SNDRV_PCM_IOCTL_READI_FRAMES:
2650 struct snd_xferi xferi;
2651 struct snd_xferi __user *_xferi = arg;
2652 struct snd_pcm_runtime *runtime = substream->runtime;
2653 snd_pcm_sframes_t result;
2654 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2656 if (put_user(0, &_xferi->result))
2658 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2660 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2661 __put_user(result, &_xferi->result);
2662 return result < 0 ? result : 0;
2664 case SNDRV_PCM_IOCTL_READN_FRAMES:
2666 struct snd_xfern xfern;
2667 struct snd_xfern __user *_xfern = arg;
2668 struct snd_pcm_runtime *runtime = substream->runtime;
2670 snd_pcm_sframes_t result;
2671 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2673 if (runtime->channels > 128)
2675 if (put_user(0, &_xfern->result))
2677 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2679 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2682 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2686 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2688 __put_user(result, &_xfern->result);
2689 return result < 0 ? result : 0;
2691 case SNDRV_PCM_IOCTL_REWIND:
2693 snd_pcm_uframes_t frames;
2694 snd_pcm_uframes_t __user *_frames = arg;
2695 snd_pcm_sframes_t result;
2696 if (get_user(frames, _frames))
2698 if (put_user(0, _frames))
2700 result = snd_pcm_capture_rewind(substream, frames);
2701 __put_user(result, _frames);
2702 return result < 0 ? result : 0;
2704 case SNDRV_PCM_IOCTL_FORWARD:
2706 snd_pcm_uframes_t frames;
2707 snd_pcm_uframes_t __user *_frames = arg;
2708 snd_pcm_sframes_t result;
2709 if (get_user(frames, _frames))
2711 if (put_user(0, _frames))
2713 result = snd_pcm_capture_forward(substream, frames);
2714 __put_user(result, _frames);
2715 return result < 0 ? result : 0;
2718 return snd_pcm_common_ioctl1(substream, cmd, arg);
2721 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2724 struct snd_pcm_file *pcm_file;
2726 pcm_file = file->private_data;
2728 if (((cmd >> 8) & 0xff) != 'A')
2731 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2734 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2737 struct snd_pcm_file *pcm_file;
2739 pcm_file = file->private_data;
2741 if (((cmd >> 8) & 0xff) != 'A')
2744 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2747 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2748 unsigned int cmd, void *arg)
2753 fs = snd_enter_user();
2754 switch (substream->stream) {
2755 case SNDRV_PCM_STREAM_PLAYBACK:
2756 result = snd_pcm_playback_ioctl1(substream,
2757 cmd, (void __user *)arg);
2759 case SNDRV_PCM_STREAM_CAPTURE:
2760 result = snd_pcm_capture_ioctl1(substream,
2761 cmd, (void __user *)arg);
2771 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2774 struct snd_pcm_file *pcm_file;
2775 struct snd_pcm_substream *substream;
2776 struct snd_pcm_runtime *runtime;
2777 snd_pcm_sframes_t result;
2779 pcm_file = file->private_data;
2780 substream = pcm_file->substream;
2781 snd_assert(substream != NULL, return -ENXIO);
2782 runtime = substream->runtime;
2783 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2785 if (!frame_aligned(runtime, count))
2787 count = bytes_to_frames(runtime, count);
2788 result = snd_pcm_lib_read(substream, buf, count);
2790 result = frames_to_bytes(runtime, result);
2794 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2795 size_t count, loff_t * offset)
2797 struct snd_pcm_file *pcm_file;
2798 struct snd_pcm_substream *substream;
2799 struct snd_pcm_runtime *runtime;
2800 snd_pcm_sframes_t result;
2802 pcm_file = file->private_data;
2803 substream = pcm_file->substream;
2804 snd_assert(substream != NULL, result = -ENXIO; goto end);
2805 runtime = substream->runtime;
2806 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2810 if (!frame_aligned(runtime, count)) {
2814 count = bytes_to_frames(runtime, count);
2815 result = snd_pcm_lib_write(substream, buf, count);
2817 result = frames_to_bytes(runtime, result);
2822 static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2823 unsigned long count, loff_t * offset)
2826 struct snd_pcm_file *pcm_file;
2827 struct snd_pcm_substream *substream;
2828 struct snd_pcm_runtime *runtime;
2829 snd_pcm_sframes_t result;
2832 snd_pcm_uframes_t frames;
2834 pcm_file = file->private_data;
2835 substream = pcm_file->substream;
2836 snd_assert(substream != NULL, return -ENXIO);
2837 runtime = substream->runtime;
2838 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2840 if (count > 1024 || count != runtime->channels)
2842 if (!frame_aligned(runtime, _vector->iov_len))
2844 frames = bytes_to_samples(runtime, _vector->iov_len);
2845 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2848 for (i = 0; i < count; ++i)
2849 bufs[i] = _vector[i].iov_base;
2850 result = snd_pcm_lib_readv(substream, bufs, frames);
2852 result = frames_to_bytes(runtime, result);
2857 static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2858 unsigned long count, loff_t * offset)
2860 struct snd_pcm_file *pcm_file;
2861 struct snd_pcm_substream *substream;
2862 struct snd_pcm_runtime *runtime;
2863 snd_pcm_sframes_t result;
2866 snd_pcm_uframes_t frames;
2868 pcm_file = file->private_data;
2869 substream = pcm_file->substream;
2870 snd_assert(substream != NULL, result = -ENXIO; goto end);
2871 runtime = substream->runtime;
2872 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2876 if (count > 128 || count != runtime->channels ||
2877 !frame_aligned(runtime, _vector->iov_len)) {
2881 frames = bytes_to_samples(runtime, _vector->iov_len);
2882 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2885 for (i = 0; i < count; ++i)
2886 bufs[i] = _vector[i].iov_base;
2887 result = snd_pcm_lib_writev(substream, bufs, frames);
2889 result = frames_to_bytes(runtime, result);
2895 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2897 struct snd_pcm_file *pcm_file;
2898 struct snd_pcm_substream *substream;
2899 struct snd_pcm_runtime *runtime;
2901 snd_pcm_uframes_t avail;
2903 pcm_file = file->private_data;
2905 substream = pcm_file->substream;
2906 snd_assert(substream != NULL, return -ENXIO);
2907 runtime = substream->runtime;
2909 poll_wait(file, &runtime->sleep, wait);
2911 snd_pcm_stream_lock_irq(substream);
2912 avail = snd_pcm_playback_avail(runtime);
2913 switch (runtime->status->state) {
2914 case SNDRV_PCM_STATE_RUNNING:
2915 case SNDRV_PCM_STATE_PREPARED:
2916 case SNDRV_PCM_STATE_PAUSED:
2917 if (avail >= runtime->control->avail_min) {
2918 mask = POLLOUT | POLLWRNORM;
2922 case SNDRV_PCM_STATE_DRAINING:
2926 mask = POLLOUT | POLLWRNORM | POLLERR;
2929 snd_pcm_stream_unlock_irq(substream);
2933 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2935 struct snd_pcm_file *pcm_file;
2936 struct snd_pcm_substream *substream;
2937 struct snd_pcm_runtime *runtime;
2939 snd_pcm_uframes_t avail;
2941 pcm_file = file->private_data;
2943 substream = pcm_file->substream;
2944 snd_assert(substream != NULL, return -ENXIO);
2945 runtime = substream->runtime;
2947 poll_wait(file, &runtime->sleep, wait);
2949 snd_pcm_stream_lock_irq(substream);
2950 avail = snd_pcm_capture_avail(runtime);
2951 switch (runtime->status->state) {
2952 case SNDRV_PCM_STATE_RUNNING:
2953 case SNDRV_PCM_STATE_PREPARED:
2954 case SNDRV_PCM_STATE_PAUSED:
2955 if (avail >= runtime->control->avail_min) {
2956 mask = POLLIN | POLLRDNORM;
2961 case SNDRV_PCM_STATE_DRAINING:
2963 mask = POLLIN | POLLRDNORM;
2968 mask = POLLIN | POLLRDNORM | POLLERR;
2971 snd_pcm_stream_unlock_irq(substream);
2980 * Only on coherent architectures, we can mmap the status and the control records
2981 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2983 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2985 * mmap status record
2987 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2988 unsigned long address, int *type)
2990 struct snd_pcm_substream *substream = area->vm_private_data;
2991 struct snd_pcm_runtime *runtime;
2994 if (substream == NULL)
2996 runtime = substream->runtime;
2997 page = virt_to_page(runtime->status);
3000 *type = VM_FAULT_MINOR;
3004 static struct vm_operations_struct snd_pcm_vm_ops_status =
3006 .nopage = snd_pcm_mmap_status_nopage,
3009 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3010 struct vm_area_struct *area)
3012 struct snd_pcm_runtime *runtime;
3014 if (!(area->vm_flags & VM_READ))
3016 runtime = substream->runtime;
3017 snd_assert(runtime != NULL, return -EAGAIN);
3018 size = area->vm_end - area->vm_start;
3019 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3021 area->vm_ops = &snd_pcm_vm_ops_status;
3022 area->vm_private_data = substream;
3023 area->vm_flags |= VM_RESERVED;
3028 * mmap control record
3030 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3031 unsigned long address, int *type)
3033 struct snd_pcm_substream *substream = area->vm_private_data;
3034 struct snd_pcm_runtime *runtime;
3037 if (substream == NULL)
3039 runtime = substream->runtime;
3040 page = virt_to_page(runtime->control);
3043 *type = VM_FAULT_MINOR;
3047 static struct vm_operations_struct snd_pcm_vm_ops_control =
3049 .nopage = snd_pcm_mmap_control_nopage,
3052 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3053 struct vm_area_struct *area)
3055 struct snd_pcm_runtime *runtime;
3057 if (!(area->vm_flags & VM_READ))
3059 runtime = substream->runtime;
3060 snd_assert(runtime != NULL, return -EAGAIN);
3061 size = area->vm_end - area->vm_start;
3062 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3064 area->vm_ops = &snd_pcm_vm_ops_control;
3065 area->vm_private_data = substream;
3066 area->vm_flags |= VM_RESERVED;
3069 #else /* ! coherent mmap */
3071 * don't support mmap for status and control records.
3073 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3074 struct vm_area_struct *area)
3078 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3079 struct vm_area_struct *area)
3083 #endif /* coherent mmap */
3086 * nopage callback for mmapping a RAM page
3088 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3089 unsigned long address, int *type)
3091 struct snd_pcm_substream *substream = area->vm_private_data;
3092 struct snd_pcm_runtime *runtime;
3093 unsigned long offset;
3098 if (substream == NULL)
3100 runtime = substream->runtime;
3101 offset = area->vm_pgoff << PAGE_SHIFT;
3102 offset += address - area->vm_start;
3103 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3104 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3105 if (offset > dma_bytes - PAGE_SIZE)
3106 return NOPAGE_SIGBUS;
3107 if (substream->ops->page) {
3108 page = substream->ops->page(substream, offset);
3112 vaddr = runtime->dma_area + offset;
3113 page = virt_to_page(vaddr);
3117 *type = VM_FAULT_MINOR;
3121 static struct vm_operations_struct snd_pcm_vm_ops_data =
3123 .open = snd_pcm_mmap_data_open,
3124 .close = snd_pcm_mmap_data_close,
3125 .nopage = snd_pcm_mmap_data_nopage,
3129 * mmap the DMA buffer on RAM
3131 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3132 struct vm_area_struct *area)
3134 area->vm_ops = &snd_pcm_vm_ops_data;
3135 area->vm_private_data = substream;
3136 area->vm_flags |= VM_RESERVED;
3137 atomic_inc(&substream->runtime->mmap_count);
3142 * mmap the DMA buffer on I/O memory area
3144 #if SNDRV_PCM_INFO_MMAP_IOMEM
3145 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3147 .open = snd_pcm_mmap_data_open,
3148 .close = snd_pcm_mmap_data_close,
3151 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3152 struct vm_area_struct *area)
3155 unsigned long offset;
3157 #ifdef pgprot_noncached
3158 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3160 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3161 area->vm_private_data = substream;
3162 area->vm_flags |= VM_IO;
3163 size = area->vm_end - area->vm_start;
3164 offset = area->vm_pgoff << PAGE_SHIFT;
3165 if (io_remap_pfn_range(area, area->vm_start,
3166 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3167 size, area->vm_page_prot))
3169 atomic_inc(&substream->runtime->mmap_count);
3172 #endif /* SNDRV_PCM_INFO_MMAP */
3177 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3178 struct vm_area_struct *area)
3180 struct snd_pcm_runtime *runtime;
3182 unsigned long offset;
3185 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3186 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3189 if (!(area->vm_flags & VM_READ))
3192 runtime = substream->runtime;
3193 snd_assert(runtime != NULL, return -EAGAIN);
3194 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3196 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3198 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3199 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3201 size = area->vm_end - area->vm_start;
3202 offset = area->vm_pgoff << PAGE_SHIFT;
3203 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3204 if ((size_t)size > dma_bytes)
3206 if (offset > dma_bytes - size)
3209 if (substream->ops->mmap)
3210 return substream->ops->mmap(substream, area);
3212 return snd_pcm_default_mmap(substream, area);
3215 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3217 struct snd_pcm_file * pcm_file;
3218 struct snd_pcm_substream *substream;
3219 unsigned long offset;
3221 pcm_file = file->private_data;
3222 substream = pcm_file->substream;
3223 snd_assert(substream != NULL, return -ENXIO);
3225 offset = area->vm_pgoff << PAGE_SHIFT;
3227 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3228 if (substream->no_mmap_ctrl)
3230 return snd_pcm_mmap_status(substream, file, area);
3231 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3232 if (substream->no_mmap_ctrl)
3234 return snd_pcm_mmap_control(substream, file, area);
3236 return snd_pcm_mmap_data(substream, file, area);
3241 static int snd_pcm_fasync(int fd, struct file * file, int on)
3243 struct snd_pcm_file * pcm_file;
3244 struct snd_pcm_substream *substream;
3245 struct snd_pcm_runtime *runtime;
3248 pcm_file = file->private_data;
3249 substream = pcm_file->substream;
3250 snd_assert(substream != NULL, return -ENXIO);
3251 runtime = substream->runtime;
3253 err = fasync_helper(fd, file, on, &runtime->fasync);
3262 #ifdef CONFIG_COMPAT
3263 #include "pcm_compat.c"
3265 #define snd_pcm_ioctl_compat NULL
3269 * To be removed helpers to keep binary compatibility
3272 #ifdef CONFIG_SND_SUPPORT_OLD_API
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))) {
3382 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3388 struct file_operations snd_pcm_f_ops[2] = {
3390 .owner = THIS_MODULE,
3391 .write = snd_pcm_write,
3392 .writev = snd_pcm_writev,
3393 .open = snd_pcm_playback_open,
3394 .release = snd_pcm_release,
3395 .poll = snd_pcm_playback_poll,
3396 .unlocked_ioctl = snd_pcm_playback_ioctl,
3397 .compat_ioctl = snd_pcm_ioctl_compat,
3398 .mmap = snd_pcm_mmap,
3399 .fasync = snd_pcm_fasync,
3402 .owner = THIS_MODULE,
3403 .read = snd_pcm_read,
3404 .readv = snd_pcm_readv,
3405 .open = snd_pcm_capture_open,
3406 .release = snd_pcm_release,
3407 .poll = snd_pcm_capture_poll,
3408 .unlocked_ioctl = snd_pcm_capture_ioctl,
3409 .compat_ioctl = snd_pcm_ioctl_compat,
3410 .mmap = snd_pcm_mmap,
3411 .fasync = snd_pcm_fasync,