2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/latency.h>
27 #include <linux/uio.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/info.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/timer.h>
34 #include <sound/minors.h>
41 struct snd_pcm_hw_params_old {
43 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
44 SNDRV_PCM_HW_PARAM_ACCESS + 1];
45 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
46 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
51 unsigned int rate_num;
52 unsigned int rate_den;
53 snd_pcm_uframes_t fifo_size;
54 unsigned char reserved[64];
57 #ifdef CONFIG_SND_SUPPORT_OLD_API
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);
66 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
72 DEFINE_RWLOCK(snd_pcm_link_rwlock);
73 EXPORT_SYMBOL(snd_pcm_link_rwlock);
75 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 EXPORT_SYMBOL(snd_pcm_hw_refine);
324 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
325 struct snd_pcm_hw_params __user * _params)
327 struct snd_pcm_hw_params *params;
330 params = kmalloc(sizeof(*params), GFP_KERNEL);
335 if (copy_from_user(params, _params, sizeof(*params))) {
339 err = snd_pcm_hw_refine(substream, params);
340 if (copy_to_user(_params, params, sizeof(*params))) {
349 static int period_to_usecs(struct snd_pcm_runtime *runtime)
354 return -1; /* invalid */
356 /* take 75% of period time as the deadline */
357 usecs = (750000 / runtime->rate) * runtime->period_size;
358 usecs += ((750000 % runtime->rate) * runtime->period_size) /
364 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
365 struct snd_pcm_hw_params *params)
367 struct snd_pcm_runtime *runtime;
370 snd_pcm_uframes_t frames;
372 snd_assert(substream != NULL, return -ENXIO);
373 runtime = substream->runtime;
374 snd_assert(runtime != NULL, return -ENXIO);
375 snd_pcm_stream_lock_irq(substream);
376 switch (runtime->status->state) {
377 case SNDRV_PCM_STATE_OPEN:
378 case SNDRV_PCM_STATE_SETUP:
379 case SNDRV_PCM_STATE_PREPARED:
382 snd_pcm_stream_unlock_irq(substream);
385 snd_pcm_stream_unlock_irq(substream);
386 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
387 if (!substream->oss.oss)
389 if (atomic_read(&substream->mmap_count))
393 err = snd_pcm_hw_refine(substream, params);
397 err = snd_pcm_hw_params_choose(substream, params);
401 if (substream->ops->hw_params != NULL) {
402 err = substream->ops->hw_params(substream, params);
407 runtime->access = params_access(params);
408 runtime->format = params_format(params);
409 runtime->subformat = params_subformat(params);
410 runtime->channels = params_channels(params);
411 runtime->rate = params_rate(params);
412 runtime->period_size = params_period_size(params);
413 runtime->periods = params_periods(params);
414 runtime->buffer_size = params_buffer_size(params);
415 runtime->info = params->info;
416 runtime->rate_num = params->rate_num;
417 runtime->rate_den = params->rate_den;
419 bits = snd_pcm_format_physical_width(runtime->format);
420 runtime->sample_bits = bits;
421 bits *= runtime->channels;
422 runtime->frame_bits = bits;
424 while (bits % 8 != 0) {
428 runtime->byte_align = bits / 8;
429 runtime->min_align = frames;
431 /* Default sw params */
432 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
433 runtime->period_step = 1;
434 runtime->control->avail_min = runtime->period_size;
435 runtime->start_threshold = 1;
436 runtime->stop_threshold = runtime->buffer_size;
437 runtime->silence_threshold = 0;
438 runtime->silence_size = 0;
439 runtime->boundary = runtime->buffer_size;
440 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
441 runtime->boundary *= 2;
443 snd_pcm_timer_resolution_change(substream);
444 runtime->status->state = SNDRV_PCM_STATE_SETUP;
446 remove_acceptable_latency(substream->latency_id);
447 if ((usecs = period_to_usecs(runtime)) >= 0)
448 set_acceptable_latency(substream->latency_id, usecs);
451 /* hardware might be unuseable from this time,
452 so we force application to retry to set
453 the correct hardware parameter settings */
454 runtime->status->state = SNDRV_PCM_STATE_OPEN;
455 if (substream->ops->hw_free != NULL)
456 substream->ops->hw_free(substream);
460 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
461 struct snd_pcm_hw_params __user * _params)
463 struct snd_pcm_hw_params *params;
466 params = kmalloc(sizeof(*params), GFP_KERNEL);
471 if (copy_from_user(params, _params, sizeof(*params))) {
475 err = snd_pcm_hw_params(substream, params);
476 if (copy_to_user(_params, params, sizeof(*params))) {
485 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
487 struct snd_pcm_runtime *runtime;
490 snd_assert(substream != NULL, return -ENXIO);
491 runtime = substream->runtime;
492 snd_assert(runtime != NULL, return -ENXIO);
493 snd_pcm_stream_lock_irq(substream);
494 switch (runtime->status->state) {
495 case SNDRV_PCM_STATE_SETUP:
496 case SNDRV_PCM_STATE_PREPARED:
499 snd_pcm_stream_unlock_irq(substream);
502 snd_pcm_stream_unlock_irq(substream);
503 if (atomic_read(&substream->mmap_count))
505 if (substream->ops->hw_free)
506 result = substream->ops->hw_free(substream);
507 runtime->status->state = SNDRV_PCM_STATE_OPEN;
508 remove_acceptable_latency(substream->latency_id);
512 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
513 struct snd_pcm_sw_params *params)
515 struct snd_pcm_runtime *runtime;
517 snd_assert(substream != NULL, return -ENXIO);
518 runtime = substream->runtime;
519 snd_assert(runtime != NULL, return -ENXIO);
520 snd_pcm_stream_lock_irq(substream);
521 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
522 snd_pcm_stream_unlock_irq(substream);
525 snd_pcm_stream_unlock_irq(substream);
527 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
529 if (params->avail_min == 0)
531 if (params->silence_size >= runtime->boundary) {
532 if (params->silence_threshold != 0)
535 if (params->silence_size > params->silence_threshold)
537 if (params->silence_threshold > runtime->buffer_size)
540 snd_pcm_stream_lock_irq(substream);
541 runtime->tstamp_mode = params->tstamp_mode;
542 runtime->period_step = params->period_step;
543 runtime->control->avail_min = params->avail_min;
544 runtime->start_threshold = params->start_threshold;
545 runtime->stop_threshold = params->stop_threshold;
546 runtime->silence_threshold = params->silence_threshold;
547 runtime->silence_size = params->silence_size;
548 params->boundary = runtime->boundary;
549 if (snd_pcm_running(substream)) {
550 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
551 runtime->silence_size > 0)
552 snd_pcm_playback_silence(substream, ULONG_MAX);
553 wake_up(&runtime->sleep);
555 snd_pcm_stream_unlock_irq(substream);
559 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
560 struct snd_pcm_sw_params __user * _params)
562 struct snd_pcm_sw_params params;
564 if (copy_from_user(¶ms, _params, sizeof(params)))
566 err = snd_pcm_sw_params(substream, ¶ms);
567 if (copy_to_user(_params, ¶ms, sizeof(params)))
572 int snd_pcm_status(struct snd_pcm_substream *substream,
573 struct snd_pcm_status *status)
575 struct snd_pcm_runtime *runtime = substream->runtime;
577 snd_pcm_stream_lock_irq(substream);
578 status->state = runtime->status->state;
579 status->suspended_state = runtime->status->suspended_state;
580 if (status->state == SNDRV_PCM_STATE_OPEN)
582 status->trigger_tstamp = runtime->trigger_tstamp;
583 if (snd_pcm_running(substream))
584 snd_pcm_update_hw_ptr(substream);
585 snd_pcm_gettime(runtime, &status->tstamp);
586 status->appl_ptr = runtime->control->appl_ptr;
587 status->hw_ptr = runtime->status->hw_ptr;
588 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
589 status->avail = snd_pcm_playback_avail(runtime);
590 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
591 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
592 status->delay = runtime->buffer_size - status->avail;
596 status->avail = snd_pcm_capture_avail(runtime);
597 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
598 status->delay = status->avail;
602 status->avail_max = runtime->avail_max;
603 status->overrange = runtime->overrange;
604 runtime->avail_max = 0;
605 runtime->overrange = 0;
607 snd_pcm_stream_unlock_irq(substream);
611 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
612 struct snd_pcm_status __user * _status)
614 struct snd_pcm_status status;
615 struct snd_pcm_runtime *runtime;
618 snd_assert(substream != NULL, return -ENXIO);
619 runtime = substream->runtime;
620 memset(&status, 0, sizeof(status));
621 res = snd_pcm_status(substream, &status);
624 if (copy_to_user(_status, &status, sizeof(status)))
629 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
630 struct snd_pcm_channel_info * info)
632 struct snd_pcm_runtime *runtime;
633 unsigned int channel;
635 snd_assert(substream != NULL, return -ENXIO);
636 channel = info->channel;
637 runtime = substream->runtime;
638 snd_pcm_stream_lock_irq(substream);
639 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
640 snd_pcm_stream_unlock_irq(substream);
643 snd_pcm_stream_unlock_irq(substream);
644 if (channel >= runtime->channels)
646 memset(info, 0, sizeof(*info));
647 info->channel = channel;
648 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
651 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
652 struct snd_pcm_channel_info __user * _info)
654 struct snd_pcm_channel_info info;
657 if (copy_from_user(&info, _info, sizeof(info)))
659 res = snd_pcm_channel_info(substream, &info);
662 if (copy_to_user(_info, &info, sizeof(info)))
667 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
669 struct snd_pcm_runtime *runtime = substream->runtime;
670 if (runtime->trigger_master == NULL)
672 if (runtime->trigger_master == substream) {
673 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
675 snd_pcm_trigger_tstamp(runtime->trigger_master);
676 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
678 runtime->trigger_master = NULL;
682 int (*pre_action)(struct snd_pcm_substream *substream, int state);
683 int (*do_action)(struct snd_pcm_substream *substream, int state);
684 void (*undo_action)(struct snd_pcm_substream *substream, int state);
685 void (*post_action)(struct snd_pcm_substream *substream, int state);
689 * this functions is core for handling of linked stream
690 * Note: the stream state might be changed also on failure
691 * Note2: call with calling stream lock + link lock
693 static int snd_pcm_action_group(struct action_ops *ops,
694 struct snd_pcm_substream *substream,
695 int state, int do_lock)
697 struct snd_pcm_substream *s = NULL;
698 struct snd_pcm_substream *s1;
701 snd_pcm_group_for_each_entry(s, substream) {
702 if (do_lock && s != substream)
703 spin_lock_nested(&s->self_group.lock,
704 SINGLE_DEPTH_NESTING);
705 res = ops->pre_action(s, state);
709 snd_pcm_group_for_each_entry(s, substream) {
710 res = ops->do_action(s, state);
712 if (ops->undo_action) {
713 snd_pcm_group_for_each_entry(s1, substream) {
714 if (s1 == s) /* failed stream */
716 ops->undo_action(s1, state);
719 s = NULL; /* unlock all */
723 snd_pcm_group_for_each_entry(s, substream) {
724 ops->post_action(s, state);
729 snd_pcm_group_for_each_entry(s1, substream) {
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 (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;
914 wake_up(&runtime->sleep);
917 static struct action_ops snd_pcm_action_stop = {
918 .pre_action = snd_pcm_pre_stop,
919 .do_action = snd_pcm_do_stop,
920 .post_action = snd_pcm_post_stop
925 * @substream: the PCM substream instance
926 * @state: PCM state after stopping the stream
928 * Try to stop all running streams in the substream group.
929 * The state of each stream is changed to the given value after that unconditionally.
931 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
933 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
936 EXPORT_SYMBOL(snd_pcm_stop);
940 * @substream: the PCM substream
942 * Stop the DMA only when the given stream is playback.
943 * The state is changed to SETUP.
944 * Unlike snd_pcm_stop(), this affects only the given stream.
946 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
948 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
949 SNDRV_PCM_STATE_SETUP);
955 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
957 struct snd_pcm_runtime *runtime = substream->runtime;
958 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
961 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
963 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
965 runtime->trigger_master = substream;
969 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
971 if (substream->runtime->trigger_master != substream)
973 return substream->ops->trigger(substream,
974 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
975 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
978 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
980 if (substream->runtime->trigger_master == substream)
981 substream->ops->trigger(substream,
982 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
983 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
986 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
988 struct snd_pcm_runtime *runtime = substream->runtime;
989 snd_pcm_trigger_tstamp(substream);
991 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
992 if (substream->timer)
993 snd_timer_notify(substream->timer,
994 SNDRV_TIMER_EVENT_MPAUSE,
995 &runtime->trigger_tstamp);
996 wake_up(&runtime->sleep);
998 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
999 if (substream->timer)
1000 snd_timer_notify(substream->timer,
1001 SNDRV_TIMER_EVENT_MCONTINUE,
1002 &runtime->trigger_tstamp);
1006 static struct action_ops snd_pcm_action_pause = {
1007 .pre_action = snd_pcm_pre_pause,
1008 .do_action = snd_pcm_do_pause,
1009 .undo_action = snd_pcm_undo_pause,
1010 .post_action = snd_pcm_post_pause
1014 * Push/release the pause for all linked streams.
1016 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1018 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1024 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1026 struct snd_pcm_runtime *runtime = substream->runtime;
1027 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1029 runtime->trigger_master = substream;
1033 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1035 struct snd_pcm_runtime *runtime = substream->runtime;
1036 if (runtime->trigger_master != substream)
1038 if (! snd_pcm_running(substream))
1040 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1041 return 0; /* suspend unconditionally */
1044 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1046 struct snd_pcm_runtime *runtime = substream->runtime;
1047 snd_pcm_trigger_tstamp(substream);
1048 if (substream->timer)
1049 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1050 &runtime->trigger_tstamp);
1051 runtime->status->suspended_state = runtime->status->state;
1052 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1053 wake_up(&runtime->sleep);
1056 static struct action_ops snd_pcm_action_suspend = {
1057 .pre_action = snd_pcm_pre_suspend,
1058 .do_action = snd_pcm_do_suspend,
1059 .post_action = snd_pcm_post_suspend
1064 * @substream: the PCM substream
1066 * Trigger SUSPEND to all linked streams.
1067 * After this call, all streams are changed to SUSPENDED state.
1069 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1072 unsigned long flags;
1077 snd_pcm_stream_lock_irqsave(substream, flags);
1078 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1079 snd_pcm_stream_unlock_irqrestore(substream, flags);
1083 EXPORT_SYMBOL(snd_pcm_suspend);
1086 * snd_pcm_suspend_all
1087 * @pcm: the PCM instance
1089 * Trigger SUSPEND to all substreams in the given pcm.
1090 * After this call, all streams are changed to SUSPENDED state.
1092 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1094 struct snd_pcm_substream *substream;
1095 int stream, err = 0;
1100 for (stream = 0; stream < 2; stream++) {
1101 for (substream = pcm->streams[stream].substream;
1102 substream; substream = substream->next) {
1103 /* FIXME: the open/close code should lock this as well */
1104 if (substream->runtime == NULL)
1106 err = snd_pcm_suspend(substream);
1107 if (err < 0 && err != -EBUSY)
1114 EXPORT_SYMBOL(snd_pcm_suspend_all);
1118 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1120 struct snd_pcm_runtime *runtime = substream->runtime;
1121 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1123 runtime->trigger_master = substream;
1127 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 if (runtime->trigger_master != substream)
1132 /* DMA not running previously? */
1133 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1134 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1135 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1137 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1140 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1142 if (substream->runtime->trigger_master == substream &&
1143 snd_pcm_running(substream))
1144 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1147 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1149 struct snd_pcm_runtime *runtime = substream->runtime;
1150 snd_pcm_trigger_tstamp(substream);
1151 if (substream->timer)
1152 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1153 &runtime->trigger_tstamp);
1154 runtime->status->state = runtime->status->suspended_state;
1157 static struct action_ops snd_pcm_action_resume = {
1158 .pre_action = snd_pcm_pre_resume,
1159 .do_action = snd_pcm_do_resume,
1160 .undo_action = snd_pcm_undo_resume,
1161 .post_action = snd_pcm_post_resume
1164 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1166 struct snd_card *card = substream->pcm->card;
1169 snd_power_lock(card);
1170 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1171 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1172 snd_power_unlock(card);
1178 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1183 #endif /* CONFIG_PM */
1188 * Change the RUNNING stream(s) to XRUN state.
1190 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1192 struct snd_card *card = substream->pcm->card;
1193 struct snd_pcm_runtime *runtime = substream->runtime;
1196 snd_power_lock(card);
1197 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1198 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1203 snd_pcm_stream_lock_irq(substream);
1204 switch (runtime->status->state) {
1205 case SNDRV_PCM_STATE_XRUN:
1206 result = 0; /* already there */
1208 case SNDRV_PCM_STATE_RUNNING:
1209 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1214 snd_pcm_stream_unlock_irq(substream);
1216 snd_power_unlock(card);
1223 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1225 struct snd_pcm_runtime *runtime = substream->runtime;
1226 switch (runtime->status->state) {
1227 case SNDRV_PCM_STATE_RUNNING:
1228 case SNDRV_PCM_STATE_PREPARED:
1229 case SNDRV_PCM_STATE_PAUSED:
1230 case SNDRV_PCM_STATE_SUSPENDED:
1237 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1239 struct snd_pcm_runtime *runtime = substream->runtime;
1240 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1243 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1244 runtime->hw_ptr_base = 0;
1245 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1246 runtime->status->hw_ptr % runtime->period_size;
1247 runtime->silence_start = runtime->status->hw_ptr;
1248 runtime->silence_filled = 0;
1252 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1254 struct snd_pcm_runtime *runtime = substream->runtime;
1255 runtime->control->appl_ptr = runtime->status->hw_ptr;
1256 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1257 runtime->silence_size > 0)
1258 snd_pcm_playback_silence(substream, ULONG_MAX);
1261 static struct action_ops snd_pcm_action_reset = {
1262 .pre_action = snd_pcm_pre_reset,
1263 .do_action = snd_pcm_do_reset,
1264 .post_action = snd_pcm_post_reset
1267 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1269 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1275 /* we use the second argument for updating f_flags */
1276 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1279 struct snd_pcm_runtime *runtime = substream->runtime;
1280 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1281 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1283 if (snd_pcm_running(substream))
1285 substream->f_flags = f_flags;
1289 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1292 err = substream->ops->prepare(substream);
1295 return snd_pcm_do_reset(substream, 0);
1298 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1300 struct snd_pcm_runtime *runtime = substream->runtime;
1301 runtime->control->appl_ptr = runtime->status->hw_ptr;
1302 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1305 static struct action_ops snd_pcm_action_prepare = {
1306 .pre_action = snd_pcm_pre_prepare,
1307 .do_action = snd_pcm_do_prepare,
1308 .post_action = snd_pcm_post_prepare
1313 * @substream: the PCM substream instance
1314 * @file: file to refer f_flags
1316 * Prepare the PCM substream to be triggerable.
1318 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1322 struct snd_card *card = substream->pcm->card;
1326 f_flags = file->f_flags;
1328 f_flags = substream->f_flags;
1330 snd_power_lock(card);
1331 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1332 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1333 substream, f_flags);
1334 snd_power_unlock(card);
1342 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1344 if (substream->f_flags & O_NONBLOCK)
1346 substream->runtime->trigger_master = substream;
1350 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1352 struct snd_pcm_runtime *runtime = substream->runtime;
1353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1354 switch (runtime->status->state) {
1355 case SNDRV_PCM_STATE_PREPARED:
1356 /* start playback stream if possible */
1357 if (! snd_pcm_playback_empty(substream)) {
1358 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1359 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1362 case SNDRV_PCM_STATE_RUNNING:
1363 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1369 /* stop running stream */
1370 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1371 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1372 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1373 snd_pcm_do_stop(substream, new_state);
1374 snd_pcm_post_stop(substream, new_state);
1380 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1384 static struct action_ops snd_pcm_action_drain_init = {
1385 .pre_action = snd_pcm_pre_drain_init,
1386 .do_action = snd_pcm_do_drain_init,
1387 .post_action = snd_pcm_post_drain_init
1391 struct snd_pcm_substream *substream;
1393 snd_pcm_uframes_t stop_threshold;
1396 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1399 * Drain the stream(s).
1400 * When the substream is linked, sync until the draining of all playback streams
1402 * After this call, all streams are supposed to be either SETUP or DRAINING
1403 * (capture only) state.
1405 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1407 struct snd_card *card;
1408 struct snd_pcm_runtime *runtime;
1409 struct snd_pcm_substream *s;
1412 struct drain_rec *drec, drec_tmp, *d;
1414 snd_assert(substream != NULL, return -ENXIO);
1415 card = substream->pcm->card;
1416 runtime = substream->runtime;
1418 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1421 snd_power_lock(card);
1422 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1423 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1425 snd_power_unlock(card);
1430 /* allocate temporary record for drain sync */
1431 down_read(&snd_pcm_link_rwsem);
1432 if (snd_pcm_stream_linked(substream)) {
1433 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1435 up_read(&snd_pcm_link_rwsem);
1436 snd_power_unlock(card);
1442 /* count only playback streams */
1444 snd_pcm_group_for_each_entry(s, substream) {
1445 runtime = s->runtime;
1446 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1447 d = &drec[num_drecs++];
1449 init_waitqueue_entry(&d->wait, current);
1450 add_wait_queue(&runtime->sleep, &d->wait);
1451 /* stop_threshold fixup to avoid endless loop when
1452 * stop_threshold > buffer_size
1454 d->stop_threshold = runtime->stop_threshold;
1455 if (runtime->stop_threshold > runtime->buffer_size)
1456 runtime->stop_threshold = runtime->buffer_size;
1459 up_read(&snd_pcm_link_rwsem);
1461 snd_pcm_stream_lock_irq(substream);
1463 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1464 snd_pcm_pause(substream, 0);
1466 /* pre-start/stop - all running streams are changed to DRAINING state */
1467 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1469 snd_pcm_stream_unlock_irq(substream);
1475 if (signal_pending(current)) {
1476 result = -ERESTARTSYS;
1480 for (i = 0; i < num_drecs; i++) {
1481 runtime = drec[i].substream->runtime;
1482 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1486 break; /* yes, all drained */
1488 set_current_state(TASK_INTERRUPTIBLE);
1489 snd_pcm_stream_unlock_irq(substream);
1490 snd_power_unlock(card);
1491 tout = schedule_timeout(10 * HZ);
1492 snd_power_lock(card);
1493 snd_pcm_stream_lock_irq(substream);
1495 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1498 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1499 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1506 snd_pcm_stream_unlock_irq(substream);
1509 for (i = 0; i < num_drecs; i++) {
1511 runtime = d->substream->runtime;
1512 remove_wait_queue(&runtime->sleep, &d->wait);
1513 runtime->stop_threshold = d->stop_threshold;
1516 if (drec != &drec_tmp)
1518 snd_power_unlock(card);
1526 * Immediately put all linked substreams into SETUP state.
1528 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1530 struct snd_pcm_runtime *runtime;
1531 struct snd_card *card;
1534 snd_assert(substream != NULL, return -ENXIO);
1535 runtime = substream->runtime;
1536 card = substream->pcm->card;
1538 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1539 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1542 snd_power_lock(card);
1543 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1544 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1549 snd_pcm_stream_lock_irq(substream);
1551 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1552 snd_pcm_pause(substream, 0);
1554 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1555 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1556 snd_pcm_stream_unlock_irq(substream);
1558 snd_power_unlock(card);
1563 /* WARNING: Don't forget to fput back the file */
1564 static struct file *snd_pcm_file_fd(int fd)
1567 struct inode *inode;
1573 inode = file->f_path.dentry->d_inode;
1574 if (!S_ISCHR(inode->i_mode) ||
1575 imajor(inode) != snd_major) {
1579 minor = iminor(inode);
1580 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1581 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1591 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1595 struct snd_pcm_file *pcm_file;
1596 struct snd_pcm_substream *substream1;
1598 file = snd_pcm_file_fd(fd);
1601 pcm_file = file->private_data;
1602 substream1 = pcm_file->substream;
1603 down_write(&snd_pcm_link_rwsem);
1604 write_lock_irq(&snd_pcm_link_rwlock);
1605 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1606 substream->runtime->status->state != substream1->runtime->status->state) {
1610 if (snd_pcm_stream_linked(substream1)) {
1614 if (!snd_pcm_stream_linked(substream)) {
1615 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1616 if (substream->group == NULL) {
1620 spin_lock_init(&substream->group->lock);
1621 INIT_LIST_HEAD(&substream->group->substreams);
1622 list_add_tail(&substream->link_list, &substream->group->substreams);
1623 substream->group->count = 1;
1625 list_add_tail(&substream1->link_list, &substream->group->substreams);
1626 substream->group->count++;
1627 substream1->group = substream->group;
1629 write_unlock_irq(&snd_pcm_link_rwlock);
1630 up_write(&snd_pcm_link_rwsem);
1635 static void relink_to_local(struct snd_pcm_substream *substream)
1637 substream->group = &substream->self_group;
1638 INIT_LIST_HEAD(&substream->self_group.substreams);
1639 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1642 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1644 struct snd_pcm_substream *s;
1647 down_write(&snd_pcm_link_rwsem);
1648 write_lock_irq(&snd_pcm_link_rwlock);
1649 if (!snd_pcm_stream_linked(substream)) {
1653 list_del(&substream->link_list);
1654 substream->group->count--;
1655 if (substream->group->count == 1) { /* detach the last stream, too */
1656 snd_pcm_group_for_each_entry(s, substream) {
1660 kfree(substream->group);
1662 relink_to_local(substream);
1664 write_unlock_irq(&snd_pcm_link_rwlock);
1665 up_write(&snd_pcm_link_rwsem);
1672 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
1675 struct snd_interval t;
1676 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1677 hw_param_interval_c(params, rule->deps[1]), &t);
1678 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1681 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1682 struct snd_pcm_hw_rule *rule)
1684 struct snd_interval t;
1685 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
1693 struct snd_interval t;
1694 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1695 hw_param_interval_c(params, rule->deps[1]),
1696 (unsigned long) rule->private, &t);
1697 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1700 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1701 struct snd_pcm_hw_rule *rule)
1703 struct snd_interval t;
1704 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1705 (unsigned long) rule->private,
1706 hw_param_interval_c(params, rule->deps[1]), &t);
1707 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1710 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1711 struct snd_pcm_hw_rule *rule)
1714 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1716 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1718 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1720 if (! snd_mask_test(mask, k))
1722 bits = snd_pcm_format_physical_width(k);
1724 continue; /* ignore invalid formats */
1725 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1726 snd_mask_reset(&m, k);
1728 return snd_mask_refine(mask, &m);
1731 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1732 struct snd_pcm_hw_rule *rule)
1734 struct snd_interval t;
1740 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1742 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1744 bits = snd_pcm_format_physical_width(k);
1746 continue; /* ignore invalid formats */
1747 if (t.min > (unsigned)bits)
1749 if (t.max < (unsigned)bits)
1753 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1756 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1757 #error "Change this table"
1760 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1761 48000, 64000, 88200, 96000, 176400, 192000 };
1763 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1764 .count = ARRAY_SIZE(rates),
1768 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1769 struct snd_pcm_hw_rule *rule)
1771 struct snd_pcm_hardware *hw = rule->private;
1772 return snd_interval_list(hw_param_interval(params, rule->var),
1773 snd_pcm_known_rates.count,
1774 snd_pcm_known_rates.list, hw->rates);
1777 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1778 struct snd_pcm_hw_rule *rule)
1780 struct snd_interval t;
1781 struct snd_pcm_substream *substream = rule->private;
1783 t.max = substream->buffer_bytes_max;
1787 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1790 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1792 struct snd_pcm_runtime *runtime = substream->runtime;
1793 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1796 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1797 snd_mask_any(constrs_mask(constrs, k));
1800 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1801 snd_interval_any(constrs_interval(constrs, k));
1804 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1805 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1806 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1807 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1808 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1811 snd_pcm_hw_rule_format, NULL,
1812 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1816 snd_pcm_hw_rule_sample_bits, NULL,
1817 SNDRV_PCM_HW_PARAM_FORMAT,
1818 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1822 snd_pcm_hw_rule_div, NULL,
1823 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1827 snd_pcm_hw_rule_mul, NULL,
1828 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1833 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1837 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1838 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1848 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1853 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1857 snd_pcm_hw_rule_div, NULL,
1858 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1862 snd_pcm_hw_rule_div, NULL,
1863 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1872 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1877 snd_pcm_hw_rule_mul, NULL,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1882 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1883 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1887 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1892 snd_pcm_hw_rule_muldivk, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1897 snd_pcm_hw_rule_muldivk, (void*) 8,
1898 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1902 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1907 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1908 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1914 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1916 struct snd_pcm_runtime *runtime = substream->runtime;
1917 struct snd_pcm_hardware *hw = &runtime->hw;
1919 unsigned int mask = 0;
1921 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1922 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1923 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1924 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1925 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1926 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1927 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1928 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1929 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1930 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1931 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1933 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1934 snd_assert(err >= 0, return -EINVAL);
1936 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1937 snd_assert(err >= 0, return -EINVAL);
1939 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1940 snd_assert(err >= 0, return -EINVAL);
1942 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1943 hw->channels_min, hw->channels_max);
1944 snd_assert(err >= 0, return -EINVAL);
1946 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1947 hw->rate_min, hw->rate_max);
1948 snd_assert(err >= 0, return -EINVAL);
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1951 hw->period_bytes_min, hw->period_bytes_max);
1952 snd_assert(err >= 0, return -EINVAL);
1954 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1955 hw->periods_min, hw->periods_max);
1956 snd_assert(err >= 0, return -EINVAL);
1958 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1959 hw->period_bytes_min, hw->buffer_bytes_max);
1960 snd_assert(err >= 0, return -EINVAL);
1962 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1963 snd_pcm_hw_rule_buffer_bytes_max, substream,
1964 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1969 if (runtime->dma_bytes) {
1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1971 snd_assert(err >= 0, return -EINVAL);
1974 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1975 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1976 snd_pcm_hw_rule_rate, hw,
1977 SNDRV_PCM_HW_PARAM_RATE, -1);
1982 /* FIXME: this belong to lowlevel */
1983 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1988 static void pcm_release_private(struct snd_pcm_substream *substream)
1990 snd_pcm_unlink(substream);
1993 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1995 substream->ref_count--;
1996 if (substream->ref_count > 0)
1999 snd_pcm_drop(substream);
2000 if (substream->hw_opened) {
2001 if (substream->ops->hw_free != NULL)
2002 substream->ops->hw_free(substream);
2003 substream->ops->close(substream);
2004 substream->hw_opened = 0;
2006 if (substream->pcm_release) {
2007 substream->pcm_release(substream);
2008 substream->pcm_release = NULL;
2010 snd_pcm_detach_substream(substream);
2013 EXPORT_SYMBOL(snd_pcm_release_substream);
2015 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2017 struct snd_pcm_substream **rsubstream)
2019 struct snd_pcm_substream *substream;
2022 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2025 if (substream->ref_count > 1) {
2026 *rsubstream = substream;
2030 err = snd_pcm_hw_constraints_init(substream);
2032 snd_printd("snd_pcm_hw_constraints_init failed\n");
2036 if ((err = substream->ops->open(substream)) < 0)
2039 substream->hw_opened = 1;
2041 err = snd_pcm_hw_constraints_complete(substream);
2043 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2047 *rsubstream = substream;
2051 snd_pcm_release_substream(substream);
2055 EXPORT_SYMBOL(snd_pcm_open_substream);
2057 static int snd_pcm_open_file(struct file *file,
2058 struct snd_pcm *pcm,
2060 struct snd_pcm_file **rpcm_file)
2062 struct snd_pcm_file *pcm_file;
2063 struct snd_pcm_substream *substream;
2064 struct snd_pcm_str *str;
2067 snd_assert(rpcm_file != NULL, return -EINVAL);
2070 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2074 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2075 if (pcm_file == NULL) {
2076 snd_pcm_release_substream(substream);
2079 pcm_file->substream = substream;
2080 if (substream->ref_count == 1) {
2081 str = substream->pstr;
2082 substream->file = pcm_file;
2083 substream->pcm_release = pcm_release_private;
2085 file->private_data = pcm_file;
2086 *rpcm_file = pcm_file;
2090 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2092 struct snd_pcm *pcm;
2094 pcm = snd_lookup_minor_data(iminor(inode),
2095 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2096 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2099 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2101 struct snd_pcm *pcm;
2103 pcm = snd_lookup_minor_data(iminor(inode),
2104 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2105 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2108 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2111 struct snd_pcm_file *pcm_file;
2118 err = snd_card_file_add(pcm->card, file);
2121 if (!try_module_get(pcm->card->module)) {
2125 init_waitqueue_entry(&wait, current);
2126 add_wait_queue(&pcm->open_wait, &wait);
2127 mutex_lock(&pcm->open_mutex);
2129 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2132 if (err == -EAGAIN) {
2133 if (file->f_flags & O_NONBLOCK) {
2139 set_current_state(TASK_INTERRUPTIBLE);
2140 mutex_unlock(&pcm->open_mutex);
2142 mutex_lock(&pcm->open_mutex);
2143 if (signal_pending(current)) {
2148 remove_wait_queue(&pcm->open_wait, &wait);
2149 mutex_unlock(&pcm->open_mutex);
2155 module_put(pcm->card->module);
2157 snd_card_file_remove(pcm->card, file);
2162 static int snd_pcm_release(struct inode *inode, struct file *file)
2164 struct snd_pcm *pcm;
2165 struct snd_pcm_substream *substream;
2166 struct snd_pcm_file *pcm_file;
2168 pcm_file = file->private_data;
2169 substream = pcm_file->substream;
2170 snd_assert(substream != NULL, return -ENXIO);
2171 pcm = substream->pcm;
2172 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2173 mutex_lock(&pcm->open_mutex);
2174 snd_pcm_release_substream(substream);
2176 mutex_unlock(&pcm->open_mutex);
2177 wake_up(&pcm->open_wait);
2178 module_put(pcm->card->module);
2179 snd_card_file_remove(pcm->card, file);
2183 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2184 snd_pcm_uframes_t frames)
2186 struct snd_pcm_runtime *runtime = substream->runtime;
2187 snd_pcm_sframes_t appl_ptr;
2188 snd_pcm_sframes_t ret;
2189 snd_pcm_sframes_t hw_avail;
2194 snd_pcm_stream_lock_irq(substream);
2195 switch (runtime->status->state) {
2196 case SNDRV_PCM_STATE_PREPARED:
2198 case SNDRV_PCM_STATE_DRAINING:
2199 case SNDRV_PCM_STATE_RUNNING:
2200 if (snd_pcm_update_hw_ptr(substream) >= 0)
2203 case SNDRV_PCM_STATE_XRUN:
2211 hw_avail = snd_pcm_playback_hw_avail(runtime);
2212 if (hw_avail <= 0) {
2216 if (frames > (snd_pcm_uframes_t)hw_avail)
2218 appl_ptr = runtime->control->appl_ptr - frames;
2220 appl_ptr += runtime->boundary;
2221 runtime->control->appl_ptr = appl_ptr;
2224 snd_pcm_stream_unlock_irq(substream);
2228 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2229 snd_pcm_uframes_t frames)
2231 struct snd_pcm_runtime *runtime = substream->runtime;
2232 snd_pcm_sframes_t appl_ptr;
2233 snd_pcm_sframes_t ret;
2234 snd_pcm_sframes_t hw_avail;
2239 snd_pcm_stream_lock_irq(substream);
2240 switch (runtime->status->state) {
2241 case SNDRV_PCM_STATE_PREPARED:
2242 case SNDRV_PCM_STATE_DRAINING:
2244 case SNDRV_PCM_STATE_RUNNING:
2245 if (snd_pcm_update_hw_ptr(substream) >= 0)
2248 case SNDRV_PCM_STATE_XRUN:
2256 hw_avail = snd_pcm_capture_hw_avail(runtime);
2257 if (hw_avail <= 0) {
2261 if (frames > (snd_pcm_uframes_t)hw_avail)
2263 appl_ptr = runtime->control->appl_ptr - frames;
2265 appl_ptr += runtime->boundary;
2266 runtime->control->appl_ptr = appl_ptr;
2269 snd_pcm_stream_unlock_irq(substream);
2273 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2274 snd_pcm_uframes_t frames)
2276 struct snd_pcm_runtime *runtime = substream->runtime;
2277 snd_pcm_sframes_t appl_ptr;
2278 snd_pcm_sframes_t ret;
2279 snd_pcm_sframes_t avail;
2284 snd_pcm_stream_lock_irq(substream);
2285 switch (runtime->status->state) {
2286 case SNDRV_PCM_STATE_PREPARED:
2287 case SNDRV_PCM_STATE_PAUSED:
2289 case SNDRV_PCM_STATE_DRAINING:
2290 case SNDRV_PCM_STATE_RUNNING:
2291 if (snd_pcm_update_hw_ptr(substream) >= 0)
2294 case SNDRV_PCM_STATE_XRUN:
2302 avail = snd_pcm_playback_avail(runtime);
2307 if (frames > (snd_pcm_uframes_t)avail)
2309 appl_ptr = runtime->control->appl_ptr + frames;
2310 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2311 appl_ptr -= runtime->boundary;
2312 runtime->control->appl_ptr = appl_ptr;
2315 snd_pcm_stream_unlock_irq(substream);
2319 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2320 snd_pcm_uframes_t frames)
2322 struct snd_pcm_runtime *runtime = substream->runtime;
2323 snd_pcm_sframes_t appl_ptr;
2324 snd_pcm_sframes_t ret;
2325 snd_pcm_sframes_t avail;
2330 snd_pcm_stream_lock_irq(substream);
2331 switch (runtime->status->state) {
2332 case SNDRV_PCM_STATE_PREPARED:
2333 case SNDRV_PCM_STATE_DRAINING:
2334 case SNDRV_PCM_STATE_PAUSED:
2336 case SNDRV_PCM_STATE_RUNNING:
2337 if (snd_pcm_update_hw_ptr(substream) >= 0)
2340 case SNDRV_PCM_STATE_XRUN:
2348 avail = snd_pcm_capture_avail(runtime);
2353 if (frames > (snd_pcm_uframes_t)avail)
2355 appl_ptr = runtime->control->appl_ptr + frames;
2356 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2357 appl_ptr -= runtime->boundary;
2358 runtime->control->appl_ptr = appl_ptr;
2361 snd_pcm_stream_unlock_irq(substream);
2365 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2367 struct snd_pcm_runtime *runtime = substream->runtime;
2370 snd_pcm_stream_lock_irq(substream);
2371 switch (runtime->status->state) {
2372 case SNDRV_PCM_STATE_DRAINING:
2373 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2375 case SNDRV_PCM_STATE_RUNNING:
2376 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2379 case SNDRV_PCM_STATE_PREPARED:
2380 case SNDRV_PCM_STATE_SUSPENDED:
2383 case SNDRV_PCM_STATE_XRUN:
2391 snd_pcm_stream_unlock_irq(substream);
2395 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2396 snd_pcm_sframes_t __user *res)
2398 struct snd_pcm_runtime *runtime = substream->runtime;
2400 snd_pcm_sframes_t n = 0;
2402 snd_pcm_stream_lock_irq(substream);
2403 switch (runtime->status->state) {
2404 case SNDRV_PCM_STATE_DRAINING:
2405 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2407 case SNDRV_PCM_STATE_RUNNING:
2408 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2411 case SNDRV_PCM_STATE_PREPARED:
2412 case SNDRV_PCM_STATE_SUSPENDED:
2414 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2415 n = snd_pcm_playback_hw_avail(runtime);
2417 n = snd_pcm_capture_avail(runtime);
2419 case SNDRV_PCM_STATE_XRUN:
2427 snd_pcm_stream_unlock_irq(substream);
2429 if (put_user(n, res))
2434 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2435 struct snd_pcm_sync_ptr __user *_sync_ptr)
2437 struct snd_pcm_runtime *runtime = substream->runtime;
2438 struct snd_pcm_sync_ptr sync_ptr;
2439 volatile struct snd_pcm_mmap_status *status;
2440 volatile struct snd_pcm_mmap_control *control;
2443 memset(&sync_ptr, 0, sizeof(sync_ptr));
2444 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2446 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2448 status = runtime->status;
2449 control = runtime->control;
2450 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2451 err = snd_pcm_hwsync(substream);
2455 snd_pcm_stream_lock_irq(substream);
2456 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2457 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2459 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2460 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2461 control->avail_min = sync_ptr.c.control.avail_min;
2463 sync_ptr.c.control.avail_min = control->avail_min;
2464 sync_ptr.s.status.state = status->state;
2465 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2466 sync_ptr.s.status.tstamp = status->tstamp;
2467 sync_ptr.s.status.suspended_state = status->suspended_state;
2468 snd_pcm_stream_unlock_irq(substream);
2469 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2474 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2476 struct snd_pcm_runtime *runtime = substream->runtime;
2479 if (get_user(arg, _arg))
2481 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2483 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2484 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2485 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2489 static int snd_pcm_common_ioctl1(struct file *file,
2490 struct snd_pcm_substream *substream,
2491 unsigned int cmd, void __user *arg)
2493 snd_assert(substream != NULL, return -ENXIO);
2496 case SNDRV_PCM_IOCTL_PVERSION:
2497 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2498 case SNDRV_PCM_IOCTL_INFO:
2499 return snd_pcm_info_user(substream, arg);
2500 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2502 case SNDRV_PCM_IOCTL_TTSTAMP:
2503 return snd_pcm_tstamp(substream, arg);
2504 case SNDRV_PCM_IOCTL_HW_REFINE:
2505 return snd_pcm_hw_refine_user(substream, arg);
2506 case SNDRV_PCM_IOCTL_HW_PARAMS:
2507 return snd_pcm_hw_params_user(substream, arg);
2508 case SNDRV_PCM_IOCTL_HW_FREE:
2509 return snd_pcm_hw_free(substream);
2510 case SNDRV_PCM_IOCTL_SW_PARAMS:
2511 return snd_pcm_sw_params_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_STATUS:
2513 return snd_pcm_status_user(substream, arg);
2514 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2515 return snd_pcm_channel_info_user(substream, arg);
2516 case SNDRV_PCM_IOCTL_PREPARE:
2517 return snd_pcm_prepare(substream, file);
2518 case SNDRV_PCM_IOCTL_RESET:
2519 return snd_pcm_reset(substream);
2520 case SNDRV_PCM_IOCTL_START:
2521 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2522 case SNDRV_PCM_IOCTL_LINK:
2523 return snd_pcm_link(substream, (int)(unsigned long) arg);
2524 case SNDRV_PCM_IOCTL_UNLINK:
2525 return snd_pcm_unlink(substream);
2526 case SNDRV_PCM_IOCTL_RESUME:
2527 return snd_pcm_resume(substream);
2528 case SNDRV_PCM_IOCTL_XRUN:
2529 return snd_pcm_xrun(substream);
2530 case SNDRV_PCM_IOCTL_HWSYNC:
2531 return snd_pcm_hwsync(substream);
2532 case SNDRV_PCM_IOCTL_DELAY:
2533 return snd_pcm_delay(substream, arg);
2534 case SNDRV_PCM_IOCTL_SYNC_PTR:
2535 return snd_pcm_sync_ptr(substream, arg);
2536 #ifdef CONFIG_SND_SUPPORT_OLD_API
2537 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2538 return snd_pcm_hw_refine_old_user(substream, arg);
2539 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2540 return snd_pcm_hw_params_old_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_DRAIN:
2543 return snd_pcm_drain(substream);
2544 case SNDRV_PCM_IOCTL_DROP:
2545 return snd_pcm_drop(substream);
2546 case SNDRV_PCM_IOCTL_PAUSE:
2549 snd_pcm_stream_lock_irq(substream);
2550 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2551 snd_pcm_stream_unlock_irq(substream);
2555 snd_printd("unknown ioctl = 0x%x\n", cmd);
2559 static int snd_pcm_playback_ioctl1(struct file *file,
2560 struct snd_pcm_substream *substream,
2561 unsigned int cmd, void __user *arg)
2563 snd_assert(substream != NULL, return -ENXIO);
2564 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2566 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2568 struct snd_xferi xferi;
2569 struct snd_xferi __user *_xferi = arg;
2570 struct snd_pcm_runtime *runtime = substream->runtime;
2571 snd_pcm_sframes_t result;
2572 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2574 if (put_user(0, &_xferi->result))
2576 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2578 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2579 __put_user(result, &_xferi->result);
2580 return result < 0 ? result : 0;
2582 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2584 struct snd_xfern xfern;
2585 struct snd_xfern __user *_xfern = arg;
2586 struct snd_pcm_runtime *runtime = substream->runtime;
2588 snd_pcm_sframes_t result;
2589 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2591 if (runtime->channels > 128)
2593 if (put_user(0, &_xfern->result))
2595 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2597 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2600 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2604 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2606 __put_user(result, &_xfern->result);
2607 return result < 0 ? result : 0;
2609 case SNDRV_PCM_IOCTL_REWIND:
2611 snd_pcm_uframes_t frames;
2612 snd_pcm_uframes_t __user *_frames = arg;
2613 snd_pcm_sframes_t result;
2614 if (get_user(frames, _frames))
2616 if (put_user(0, _frames))
2618 result = snd_pcm_playback_rewind(substream, frames);
2619 __put_user(result, _frames);
2620 return result < 0 ? result : 0;
2622 case SNDRV_PCM_IOCTL_FORWARD:
2624 snd_pcm_uframes_t frames;
2625 snd_pcm_uframes_t __user *_frames = arg;
2626 snd_pcm_sframes_t result;
2627 if (get_user(frames, _frames))
2629 if (put_user(0, _frames))
2631 result = snd_pcm_playback_forward(substream, frames);
2632 __put_user(result, _frames);
2633 return result < 0 ? result : 0;
2636 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2639 static int snd_pcm_capture_ioctl1(struct file *file,
2640 struct snd_pcm_substream *substream,
2641 unsigned int cmd, void __user *arg)
2643 snd_assert(substream != NULL, return -ENXIO);
2644 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2646 case SNDRV_PCM_IOCTL_READI_FRAMES:
2648 struct snd_xferi xferi;
2649 struct snd_xferi __user *_xferi = arg;
2650 struct snd_pcm_runtime *runtime = substream->runtime;
2651 snd_pcm_sframes_t result;
2652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2654 if (put_user(0, &_xferi->result))
2656 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2658 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2659 __put_user(result, &_xferi->result);
2660 return result < 0 ? result : 0;
2662 case SNDRV_PCM_IOCTL_READN_FRAMES:
2664 struct snd_xfern xfern;
2665 struct snd_xfern __user *_xfern = arg;
2666 struct snd_pcm_runtime *runtime = substream->runtime;
2668 snd_pcm_sframes_t result;
2669 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2671 if (runtime->channels > 128)
2673 if (put_user(0, &_xfern->result))
2675 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2677 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2680 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2684 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2686 __put_user(result, &_xfern->result);
2687 return result < 0 ? result : 0;
2689 case SNDRV_PCM_IOCTL_REWIND:
2691 snd_pcm_uframes_t frames;
2692 snd_pcm_uframes_t __user *_frames = arg;
2693 snd_pcm_sframes_t result;
2694 if (get_user(frames, _frames))
2696 if (put_user(0, _frames))
2698 result = snd_pcm_capture_rewind(substream, frames);
2699 __put_user(result, _frames);
2700 return result < 0 ? result : 0;
2702 case SNDRV_PCM_IOCTL_FORWARD:
2704 snd_pcm_uframes_t frames;
2705 snd_pcm_uframes_t __user *_frames = arg;
2706 snd_pcm_sframes_t result;
2707 if (get_user(frames, _frames))
2709 if (put_user(0, _frames))
2711 result = snd_pcm_capture_forward(substream, frames);
2712 __put_user(result, _frames);
2713 return result < 0 ? result : 0;
2716 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2719 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2722 struct snd_pcm_file *pcm_file;
2724 pcm_file = file->private_data;
2726 if (((cmd >> 8) & 0xff) != 'A')
2729 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2730 (void __user *)arg);
2733 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2736 struct snd_pcm_file *pcm_file;
2738 pcm_file = file->private_data;
2740 if (((cmd >> 8) & 0xff) != 'A')
2743 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2744 (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(NULL, substream, cmd,
2757 (void __user *)arg);
2759 case SNDRV_PCM_STREAM_CAPTURE:
2760 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2761 (void __user *)arg);
2771 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2773 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2776 struct snd_pcm_file *pcm_file;
2777 struct snd_pcm_substream *substream;
2778 struct snd_pcm_runtime *runtime;
2779 snd_pcm_sframes_t result;
2781 pcm_file = file->private_data;
2782 substream = pcm_file->substream;
2783 snd_assert(substream != NULL, return -ENXIO);
2784 runtime = substream->runtime;
2785 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2787 if (!frame_aligned(runtime, count))
2789 count = bytes_to_frames(runtime, count);
2790 result = snd_pcm_lib_read(substream, buf, count);
2792 result = frames_to_bytes(runtime, result);
2796 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2797 size_t count, loff_t * offset)
2799 struct snd_pcm_file *pcm_file;
2800 struct snd_pcm_substream *substream;
2801 struct snd_pcm_runtime *runtime;
2802 snd_pcm_sframes_t result;
2804 pcm_file = file->private_data;
2805 substream = pcm_file->substream;
2806 snd_assert(substream != NULL, result = -ENXIO; goto end);
2807 runtime = substream->runtime;
2808 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2812 if (!frame_aligned(runtime, count)) {
2816 count = bytes_to_frames(runtime, count);
2817 result = snd_pcm_lib_write(substream, buf, count);
2819 result = frames_to_bytes(runtime, result);
2824 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2825 unsigned long nr_segs, loff_t pos)
2828 struct snd_pcm_file *pcm_file;
2829 struct snd_pcm_substream *substream;
2830 struct snd_pcm_runtime *runtime;
2831 snd_pcm_sframes_t result;
2834 snd_pcm_uframes_t frames;
2836 pcm_file = iocb->ki_filp->private_data;
2837 substream = pcm_file->substream;
2838 snd_assert(substream != NULL, return -ENXIO);
2839 runtime = substream->runtime;
2840 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2842 if (nr_segs > 1024 || nr_segs != runtime->channels)
2844 if (!frame_aligned(runtime, iov->iov_len))
2846 frames = bytes_to_samples(runtime, iov->iov_len);
2847 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2850 for (i = 0; i < nr_segs; ++i)
2851 bufs[i] = iov[i].iov_base;
2852 result = snd_pcm_lib_readv(substream, bufs, frames);
2854 result = frames_to_bytes(runtime, result);
2859 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2860 unsigned long nr_segs, loff_t pos)
2862 struct snd_pcm_file *pcm_file;
2863 struct snd_pcm_substream *substream;
2864 struct snd_pcm_runtime *runtime;
2865 snd_pcm_sframes_t result;
2868 snd_pcm_uframes_t frames;
2870 pcm_file = iocb->ki_filp->private_data;
2871 substream = pcm_file->substream;
2872 snd_assert(substream != NULL, result = -ENXIO; goto end);
2873 runtime = substream->runtime;
2874 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2878 if (nr_segs > 128 || nr_segs != runtime->channels ||
2879 !frame_aligned(runtime, iov->iov_len)) {
2883 frames = bytes_to_samples(runtime, iov->iov_len);
2884 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2887 for (i = 0; i < nr_segs; ++i)
2888 bufs[i] = iov[i].iov_base;
2889 result = snd_pcm_lib_writev(substream, bufs, frames);
2891 result = frames_to_bytes(runtime, result);
2897 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2899 struct snd_pcm_file *pcm_file;
2900 struct snd_pcm_substream *substream;
2901 struct snd_pcm_runtime *runtime;
2903 snd_pcm_uframes_t avail;
2905 pcm_file = file->private_data;
2907 substream = pcm_file->substream;
2908 snd_assert(substream != NULL, return -ENXIO);
2909 runtime = substream->runtime;
2911 poll_wait(file, &runtime->sleep, wait);
2913 snd_pcm_stream_lock_irq(substream);
2914 avail = snd_pcm_playback_avail(runtime);
2915 switch (runtime->status->state) {
2916 case SNDRV_PCM_STATE_RUNNING:
2917 case SNDRV_PCM_STATE_PREPARED:
2918 case SNDRV_PCM_STATE_PAUSED:
2919 if (avail >= runtime->control->avail_min) {
2920 mask = POLLOUT | POLLWRNORM;
2924 case SNDRV_PCM_STATE_DRAINING:
2928 mask = POLLOUT | POLLWRNORM | POLLERR;
2931 snd_pcm_stream_unlock_irq(substream);
2935 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2937 struct snd_pcm_file *pcm_file;
2938 struct snd_pcm_substream *substream;
2939 struct snd_pcm_runtime *runtime;
2941 snd_pcm_uframes_t avail;
2943 pcm_file = file->private_data;
2945 substream = pcm_file->substream;
2946 snd_assert(substream != NULL, return -ENXIO);
2947 runtime = substream->runtime;
2949 poll_wait(file, &runtime->sleep, wait);
2951 snd_pcm_stream_lock_irq(substream);
2952 avail = snd_pcm_capture_avail(runtime);
2953 switch (runtime->status->state) {
2954 case SNDRV_PCM_STATE_RUNNING:
2955 case SNDRV_PCM_STATE_PREPARED:
2956 case SNDRV_PCM_STATE_PAUSED:
2957 if (avail >= runtime->control->avail_min) {
2958 mask = POLLIN | POLLRDNORM;
2963 case SNDRV_PCM_STATE_DRAINING:
2965 mask = POLLIN | POLLRDNORM;
2970 mask = POLLIN | POLLRDNORM | POLLERR;
2973 snd_pcm_stream_unlock_irq(substream);
2982 * Only on coherent architectures, we can mmap the status and the control records
2983 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2985 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2987 * mmap status record
2989 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2990 struct vm_fault *vmf)
2992 struct snd_pcm_substream *substream = area->vm_private_data;
2993 struct snd_pcm_runtime *runtime;
2995 if (substream == NULL)
2996 return VM_FAULT_SIGBUS;
2997 runtime = substream->runtime;
2998 vmf->page = virt_to_page(runtime->status);
2999 get_page(vmf->page);
3003 static struct vm_operations_struct snd_pcm_vm_ops_status =
3005 .fault = snd_pcm_mmap_status_fault,
3008 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3009 struct vm_area_struct *area)
3011 struct snd_pcm_runtime *runtime;
3013 if (!(area->vm_flags & VM_READ))
3015 runtime = substream->runtime;
3016 snd_assert(runtime != NULL, return -EAGAIN);
3017 size = area->vm_end - area->vm_start;
3018 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3020 area->vm_ops = &snd_pcm_vm_ops_status;
3021 area->vm_private_data = substream;
3022 area->vm_flags |= VM_RESERVED;
3027 * mmap control record
3029 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3030 struct vm_fault *vmf)
3032 struct snd_pcm_substream *substream = area->vm_private_data;
3033 struct snd_pcm_runtime *runtime;
3035 if (substream == NULL)
3036 return VM_FAULT_SIGBUS;
3037 runtime = substream->runtime;
3038 vmf->page = virt_to_page(runtime->control);
3039 get_page(vmf->page);
3043 static struct vm_operations_struct snd_pcm_vm_ops_control =
3045 .fault = snd_pcm_mmap_control_fault,
3048 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3049 struct vm_area_struct *area)
3051 struct snd_pcm_runtime *runtime;
3053 if (!(area->vm_flags & VM_READ))
3055 runtime = substream->runtime;
3056 snd_assert(runtime != NULL, return -EAGAIN);
3057 size = area->vm_end - area->vm_start;
3058 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3060 area->vm_ops = &snd_pcm_vm_ops_control;
3061 area->vm_private_data = substream;
3062 area->vm_flags |= VM_RESERVED;
3065 #else /* ! coherent mmap */
3067 * don't support mmap for status and control records.
3069 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3070 struct vm_area_struct *area)
3074 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3075 struct vm_area_struct *area)
3079 #endif /* coherent mmap */
3082 * fault callback for mmapping a RAM page
3084 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3085 struct vm_fault *vmf)
3087 struct snd_pcm_substream *substream = area->vm_private_data;
3088 struct snd_pcm_runtime *runtime;
3089 unsigned long offset;
3094 if (substream == NULL)
3095 return VM_FAULT_SIGBUS;
3096 runtime = substream->runtime;
3097 offset = vmf->pgoff << PAGE_SHIFT;
3098 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3099 if (offset > dma_bytes - PAGE_SIZE)
3100 return VM_FAULT_SIGBUS;
3101 if (substream->ops->page) {
3102 page = substream->ops->page(substream, offset);
3104 return VM_FAULT_SIGBUS;
3106 vaddr = runtime->dma_area + offset;
3107 page = virt_to_page(vaddr);
3114 static struct vm_operations_struct snd_pcm_vm_ops_data =
3116 .open = snd_pcm_mmap_data_open,
3117 .close = snd_pcm_mmap_data_close,
3118 .fault = snd_pcm_mmap_data_fault,
3122 * mmap the DMA buffer on RAM
3124 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3125 struct vm_area_struct *area)
3127 area->vm_ops = &snd_pcm_vm_ops_data;
3128 area->vm_private_data = substream;
3129 area->vm_flags |= VM_RESERVED;
3130 atomic_inc(&substream->mmap_count);
3135 * mmap the DMA buffer on I/O memory area
3137 #if SNDRV_PCM_INFO_MMAP_IOMEM
3138 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3140 .open = snd_pcm_mmap_data_open,
3141 .close = snd_pcm_mmap_data_close,
3144 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3145 struct vm_area_struct *area)
3148 unsigned long offset;
3150 #ifdef pgprot_noncached
3151 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3153 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3154 area->vm_private_data = substream;
3155 area->vm_flags |= VM_IO;
3156 size = area->vm_end - area->vm_start;
3157 offset = area->vm_pgoff << PAGE_SHIFT;
3158 if (io_remap_pfn_range(area, area->vm_start,
3159 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3160 size, area->vm_page_prot))
3162 atomic_inc(&substream->mmap_count);
3166 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3167 #endif /* SNDRV_PCM_INFO_MMAP */
3172 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3173 struct vm_area_struct *area)
3175 struct snd_pcm_runtime *runtime;
3177 unsigned long offset;
3180 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3181 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3184 if (!(area->vm_flags & VM_READ))
3187 runtime = substream->runtime;
3188 snd_assert(runtime != NULL, return -EAGAIN);
3189 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3191 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3193 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3194 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3196 size = area->vm_end - area->vm_start;
3197 offset = area->vm_pgoff << PAGE_SHIFT;
3198 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3199 if ((size_t)size > dma_bytes)
3201 if (offset > dma_bytes - size)
3204 if (substream->ops->mmap)
3205 return substream->ops->mmap(substream, area);
3207 return snd_pcm_default_mmap(substream, area);
3210 EXPORT_SYMBOL(snd_pcm_mmap_data);
3212 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3214 struct snd_pcm_file * pcm_file;
3215 struct snd_pcm_substream *substream;
3216 unsigned long offset;
3218 pcm_file = file->private_data;
3219 substream = pcm_file->substream;
3220 snd_assert(substream != NULL, return -ENXIO);
3222 offset = area->vm_pgoff << PAGE_SHIFT;
3224 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3225 if (pcm_file->no_compat_mmap)
3227 return snd_pcm_mmap_status(substream, file, area);
3228 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3229 if (pcm_file->no_compat_mmap)
3231 return snd_pcm_mmap_control(substream, file, area);
3233 return snd_pcm_mmap_data(substream, file, area);
3238 static int snd_pcm_fasync(int fd, struct file * file, int on)
3240 struct snd_pcm_file * pcm_file;
3241 struct snd_pcm_substream *substream;
3242 struct snd_pcm_runtime *runtime;
3245 pcm_file = file->private_data;
3246 substream = pcm_file->substream;
3247 snd_assert(substream != NULL, return -ENXIO);
3248 runtime = substream->runtime;
3250 err = fasync_helper(fd, file, on, &runtime->fasync);
3259 #ifdef CONFIG_COMPAT
3260 #include "pcm_compat.c"
3262 #define snd_pcm_ioctl_compat NULL
3266 * To be removed helpers to keep binary compatibility
3269 #ifdef CONFIG_SND_SUPPORT_OLD_API
3270 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3271 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3273 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3274 struct snd_pcm_hw_params_old *oparams)
3278 memset(params, 0, sizeof(*params));
3279 params->flags = oparams->flags;
3280 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3281 params->masks[i].bits[0] = oparams->masks[i];
3282 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3283 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3284 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3285 params->info = oparams->info;
3286 params->msbits = oparams->msbits;
3287 params->rate_num = oparams->rate_num;
3288 params->rate_den = oparams->rate_den;
3289 params->fifo_size = oparams->fifo_size;
3292 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3293 struct snd_pcm_hw_params *params)
3297 memset(oparams, 0, sizeof(*oparams));
3298 oparams->flags = params->flags;
3299 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3300 oparams->masks[i] = params->masks[i].bits[0];
3301 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3302 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3303 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3304 oparams->info = params->info;
3305 oparams->msbits = params->msbits;
3306 oparams->rate_num = params->rate_num;
3307 oparams->rate_den = params->rate_den;
3308 oparams->fifo_size = params->fifo_size;
3311 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3312 struct snd_pcm_hw_params_old __user * _oparams)
3314 struct snd_pcm_hw_params *params;
3315 struct snd_pcm_hw_params_old *oparams = NULL;
3318 params = kmalloc(sizeof(*params), GFP_KERNEL);
3323 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3329 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3333 snd_pcm_hw_convert_from_old_params(params, oparams);
3334 err = snd_pcm_hw_refine(substream, params);
3335 snd_pcm_hw_convert_to_old_params(oparams, params);
3336 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3346 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3347 struct snd_pcm_hw_params_old __user * _oparams)
3349 struct snd_pcm_hw_params *params;
3350 struct snd_pcm_hw_params_old *oparams = NULL;
3353 params = kmalloc(sizeof(*params), GFP_KERNEL);
3358 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3363 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3367 snd_pcm_hw_convert_from_old_params(params, oparams);
3368 err = snd_pcm_hw_params(substream, params);
3369 snd_pcm_hw_convert_to_old_params(oparams, params);
3370 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3379 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3385 const struct file_operations snd_pcm_f_ops[2] = {
3387 .owner = THIS_MODULE,
3388 .write = snd_pcm_write,
3389 .aio_write = snd_pcm_aio_write,
3390 .open = snd_pcm_playback_open,
3391 .release = snd_pcm_release,
3392 .poll = snd_pcm_playback_poll,
3393 .unlocked_ioctl = snd_pcm_playback_ioctl,
3394 .compat_ioctl = snd_pcm_ioctl_compat,
3395 .mmap = snd_pcm_mmap,
3396 .fasync = snd_pcm_fasync,
3399 .owner = THIS_MODULE,
3400 .read = snd_pcm_read,
3401 .aio_read = snd_pcm_aio_read,
3402 .open = snd_pcm_capture_open,
3403 .release = snd_pcm_release,
3404 .poll = snd_pcm_capture_poll,
3405 .unlocked_ioctl = snd_pcm_capture_ioctl,
3406 .compat_ioctl = snd_pcm_ioctl_compat,
3407 .mmap = snd_pcm_mmap,
3408 .fasync = snd_pcm_fasync,