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/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 DEFINE_RWLOCK(snd_pcm_link_rwlock);
75 EXPORT_SYMBOL(snd_pcm_link_rwlock);
77 static DECLARE_RWSEM(snd_pcm_link_rwsem);
79 static inline mm_segment_t snd_enter_user(void)
81 mm_segment_t fs = get_fs();
86 static inline void snd_leave_user(mm_segment_t fs)
93 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
95 struct snd_pcm_runtime *runtime;
96 struct snd_pcm *pcm = substream->pcm;
97 struct snd_pcm_str *pstr = substream->pstr;
99 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
120 int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
123 struct snd_pcm_info *info;
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
129 err = snd_pcm_info(substream, info);
131 if (copy_to_user(_info, info, sizeof(*info)))
141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142 char *snd_pcm_hw_param_names[] = {
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
161 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
187 if (!(params->rmask & (1 << k)))
190 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
198 params->cmask |= 1 << k;
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
207 if (!(params->rmask & (1 << k)))
210 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
229 params->cmask |= 1 << k;
234 for (k = 0; k < constrs->rules_num; k++)
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
240 for (k = 0; k < constrs->rules_num; k++) {
241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
244 if (r->cond && !(r->cond & params->flags))
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
255 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
262 i = hw_param_interval(params, r->var);
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
272 changed = r->func(params, r);
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
314 hw = &substream->runtime->hw;
316 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
317 if (!params->fifo_size) {
318 if (snd_mask_min(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT]) ==
319 snd_mask_max(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT]) &&
320 snd_mask_min(¶ms->masks[SNDRV_PCM_HW_PARAM_CHANNELS]) ==
321 snd_mask_max(¶ms->masks[SNDRV_PCM_HW_PARAM_CHANNELS])) {
322 changed = substream->ops->ioctl(substream,
323 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
332 EXPORT_SYMBOL(snd_pcm_hw_refine);
334 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
335 struct snd_pcm_hw_params __user * _params)
337 struct snd_pcm_hw_params *params;
340 params = memdup_user(_params, sizeof(*params));
342 return PTR_ERR(params);
344 err = snd_pcm_hw_refine(substream, params);
345 if (copy_to_user(_params, params, sizeof(*params))) {
354 static int period_to_usecs(struct snd_pcm_runtime *runtime)
359 return -1; /* invalid */
361 /* take 75% of period time as the deadline */
362 usecs = (750000 / runtime->rate) * runtime->period_size;
363 usecs += ((750000 % runtime->rate) * runtime->period_size) /
369 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
370 struct snd_pcm_hw_params *params)
372 struct snd_pcm_runtime *runtime;
375 snd_pcm_uframes_t frames;
377 if (PCM_RUNTIME_CHECK(substream))
379 runtime = substream->runtime;
380 snd_pcm_stream_lock_irq(substream);
381 switch (runtime->status->state) {
382 case SNDRV_PCM_STATE_OPEN:
383 case SNDRV_PCM_STATE_SETUP:
384 case SNDRV_PCM_STATE_PREPARED:
387 snd_pcm_stream_unlock_irq(substream);
390 snd_pcm_stream_unlock_irq(substream);
391 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
392 if (!substream->oss.oss)
394 if (atomic_read(&substream->mmap_count))
398 err = snd_pcm_hw_refine(substream, params);
402 err = snd_pcm_hw_params_choose(substream, params);
406 if (substream->ops->hw_params != NULL) {
407 err = substream->ops->hw_params(substream, params);
412 runtime->access = params_access(params);
413 runtime->format = params_format(params);
414 runtime->subformat = params_subformat(params);
415 runtime->channels = params_channels(params);
416 runtime->rate = params_rate(params);
417 runtime->period_size = params_period_size(params);
418 runtime->periods = params_periods(params);
419 runtime->buffer_size = params_buffer_size(params);
420 runtime->info = params->info;
421 runtime->rate_num = params->rate_num;
422 runtime->rate_den = params->rate_den;
424 bits = snd_pcm_format_physical_width(runtime->format);
425 runtime->sample_bits = bits;
426 bits *= runtime->channels;
427 runtime->frame_bits = bits;
429 while (bits % 8 != 0) {
433 runtime->byte_align = bits / 8;
434 runtime->min_align = frames;
436 /* Default sw params */
437 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
438 runtime->period_step = 1;
439 runtime->control->avail_min = runtime->period_size;
440 runtime->start_threshold = 1;
441 runtime->stop_threshold = runtime->buffer_size;
442 runtime->silence_threshold = 0;
443 runtime->silence_size = 0;
444 runtime->boundary = runtime->buffer_size;
445 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
446 runtime->boundary *= 2;
448 snd_pcm_timer_resolution_change(substream);
449 runtime->status->state = SNDRV_PCM_STATE_SETUP;
451 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
452 substream->latency_id);
453 if ((usecs = period_to_usecs(runtime)) >= 0)
454 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
455 substream->latency_id, usecs);
458 /* hardware might be unuseable from this time,
459 so we force application to retry to set
460 the correct hardware parameter settings */
461 runtime->status->state = SNDRV_PCM_STATE_OPEN;
462 if (substream->ops->hw_free != NULL)
463 substream->ops->hw_free(substream);
467 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
468 struct snd_pcm_hw_params __user * _params)
470 struct snd_pcm_hw_params *params;
473 params = memdup_user(_params, sizeof(*params));
475 return PTR_ERR(params);
477 err = snd_pcm_hw_params(substream, params);
478 if (copy_to_user(_params, params, sizeof(*params))) {
487 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
489 struct snd_pcm_runtime *runtime;
492 if (PCM_RUNTIME_CHECK(substream))
494 runtime = substream->runtime;
495 snd_pcm_stream_lock_irq(substream);
496 switch (runtime->status->state) {
497 case SNDRV_PCM_STATE_SETUP:
498 case SNDRV_PCM_STATE_PREPARED:
501 snd_pcm_stream_unlock_irq(substream);
504 snd_pcm_stream_unlock_irq(substream);
505 if (atomic_read(&substream->mmap_count))
507 if (substream->ops->hw_free)
508 result = substream->ops->hw_free(substream);
509 runtime->status->state = SNDRV_PCM_STATE_OPEN;
510 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
511 substream->latency_id);
515 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
516 struct snd_pcm_sw_params *params)
518 struct snd_pcm_runtime *runtime;
521 if (PCM_RUNTIME_CHECK(substream))
523 runtime = substream->runtime;
524 snd_pcm_stream_lock_irq(substream);
525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
526 snd_pcm_stream_unlock_irq(substream);
529 snd_pcm_stream_unlock_irq(substream);
531 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
533 if (params->avail_min == 0)
535 if (params->silence_size >= runtime->boundary) {
536 if (params->silence_threshold != 0)
539 if (params->silence_size > params->silence_threshold)
541 if (params->silence_threshold > runtime->buffer_size)
545 snd_pcm_stream_lock_irq(substream);
546 runtime->tstamp_mode = params->tstamp_mode;
547 runtime->period_step = params->period_step;
548 runtime->control->avail_min = params->avail_min;
549 runtime->start_threshold = params->start_threshold;
550 runtime->stop_threshold = params->stop_threshold;
551 runtime->silence_threshold = params->silence_threshold;
552 runtime->silence_size = params->silence_size;
553 params->boundary = runtime->boundary;
554 if (snd_pcm_running(substream)) {
555 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
556 runtime->silence_size > 0)
557 snd_pcm_playback_silence(substream, ULONG_MAX);
558 err = snd_pcm_update_state(substream, runtime);
560 snd_pcm_stream_unlock_irq(substream);
564 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
565 struct snd_pcm_sw_params __user * _params)
567 struct snd_pcm_sw_params params;
569 if (copy_from_user(¶ms, _params, sizeof(params)))
571 err = snd_pcm_sw_params(substream, ¶ms);
572 if (copy_to_user(_params, ¶ms, sizeof(params)))
577 int snd_pcm_status(struct snd_pcm_substream *substream,
578 struct snd_pcm_status *status)
580 struct snd_pcm_runtime *runtime = substream->runtime;
582 snd_pcm_stream_lock_irq(substream);
583 status->state = runtime->status->state;
584 status->suspended_state = runtime->status->suspended_state;
585 if (status->state == SNDRV_PCM_STATE_OPEN)
587 status->trigger_tstamp = runtime->trigger_tstamp;
588 if (snd_pcm_running(substream)) {
589 snd_pcm_update_hw_ptr(substream);
590 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
591 status->tstamp = runtime->status->tstamp;
595 snd_pcm_gettime(runtime, &status->tstamp);
597 status->appl_ptr = runtime->control->appl_ptr;
598 status->hw_ptr = runtime->status->hw_ptr;
599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
600 status->avail = snd_pcm_playback_avail(runtime);
601 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
602 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
603 status->delay = runtime->buffer_size - status->avail;
604 status->delay += runtime->delay;
608 status->avail = snd_pcm_capture_avail(runtime);
609 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
610 status->delay = status->avail + runtime->delay;
614 status->avail_max = runtime->avail_max;
615 status->overrange = runtime->overrange;
616 runtime->avail_max = 0;
617 runtime->overrange = 0;
619 snd_pcm_stream_unlock_irq(substream);
623 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
624 struct snd_pcm_status __user * _status)
626 struct snd_pcm_status status;
629 memset(&status, 0, sizeof(status));
630 res = snd_pcm_status(substream, &status);
633 if (copy_to_user(_status, &status, sizeof(status)))
638 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
639 struct snd_pcm_channel_info * info)
641 struct snd_pcm_runtime *runtime;
642 unsigned int channel;
644 channel = info->channel;
645 runtime = substream->runtime;
646 snd_pcm_stream_lock_irq(substream);
647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
648 snd_pcm_stream_unlock_irq(substream);
651 snd_pcm_stream_unlock_irq(substream);
652 if (channel >= runtime->channels)
654 memset(info, 0, sizeof(*info));
655 info->channel = channel;
656 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
659 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
660 struct snd_pcm_channel_info __user * _info)
662 struct snd_pcm_channel_info info;
665 if (copy_from_user(&info, _info, sizeof(info)))
667 res = snd_pcm_channel_info(substream, &info);
670 if (copy_to_user(_info, &info, sizeof(info)))
675 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
677 struct snd_pcm_runtime *runtime = substream->runtime;
678 if (runtime->trigger_master == NULL)
680 if (runtime->trigger_master == substream) {
681 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
683 snd_pcm_trigger_tstamp(runtime->trigger_master);
684 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
686 runtime->trigger_master = NULL;
690 int (*pre_action)(struct snd_pcm_substream *substream, int state);
691 int (*do_action)(struct snd_pcm_substream *substream, int state);
692 void (*undo_action)(struct snd_pcm_substream *substream, int state);
693 void (*post_action)(struct snd_pcm_substream *substream, int state);
697 * this functions is core for handling of linked stream
698 * Note: the stream state might be changed also on failure
699 * Note2: call with calling stream lock + link lock
701 static int snd_pcm_action_group(struct action_ops *ops,
702 struct snd_pcm_substream *substream,
703 int state, int do_lock)
705 struct snd_pcm_substream *s = NULL;
706 struct snd_pcm_substream *s1;
709 snd_pcm_group_for_each_entry(s, substream) {
710 if (do_lock && s != substream)
711 spin_lock_nested(&s->self_group.lock,
712 SINGLE_DEPTH_NESTING);
713 res = ops->pre_action(s, state);
717 snd_pcm_group_for_each_entry(s, substream) {
718 res = ops->do_action(s, state);
720 if (ops->undo_action) {
721 snd_pcm_group_for_each_entry(s1, substream) {
722 if (s1 == s) /* failed stream */
724 ops->undo_action(s1, state);
727 s = NULL; /* unlock all */
731 snd_pcm_group_for_each_entry(s, substream) {
732 ops->post_action(s, state);
737 snd_pcm_group_for_each_entry(s1, substream) {
739 spin_unlock(&s1->self_group.lock);
740 if (s1 == s) /* end */
748 * Note: call with stream lock
750 static int snd_pcm_action_single(struct action_ops *ops,
751 struct snd_pcm_substream *substream,
756 res = ops->pre_action(substream, state);
759 res = ops->do_action(substream, state);
761 ops->post_action(substream, state);
762 else if (ops->undo_action)
763 ops->undo_action(substream, state);
768 * Note: call with stream lock
770 static int snd_pcm_action(struct action_ops *ops,
771 struct snd_pcm_substream *substream,
776 if (snd_pcm_stream_linked(substream)) {
777 if (!spin_trylock(&substream->group->lock)) {
778 spin_unlock(&substream->self_group.lock);
779 spin_lock(&substream->group->lock);
780 spin_lock(&substream->self_group.lock);
782 res = snd_pcm_action_group(ops, substream, state, 1);
783 spin_unlock(&substream->group->lock);
785 res = snd_pcm_action_single(ops, substream, state);
791 * Note: don't use any locks before
793 static int snd_pcm_action_lock_irq(struct action_ops *ops,
794 struct snd_pcm_substream *substream,
799 read_lock_irq(&snd_pcm_link_rwlock);
800 if (snd_pcm_stream_linked(substream)) {
801 spin_lock(&substream->group->lock);
802 spin_lock(&substream->self_group.lock);
803 res = snd_pcm_action_group(ops, substream, state, 1);
804 spin_unlock(&substream->self_group.lock);
805 spin_unlock(&substream->group->lock);
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_single(ops, substream, state);
809 spin_unlock(&substream->self_group.lock);
811 read_unlock_irq(&snd_pcm_link_rwlock);
817 static int snd_pcm_action_nonatomic(struct action_ops *ops,
818 struct snd_pcm_substream *substream,
823 down_read(&snd_pcm_link_rwsem);
824 if (snd_pcm_stream_linked(substream))
825 res = snd_pcm_action_group(ops, substream, state, 0);
827 res = snd_pcm_action_single(ops, substream, state);
828 up_read(&snd_pcm_link_rwsem);
835 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
837 struct snd_pcm_runtime *runtime = substream->runtime;
838 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
841 !snd_pcm_playback_data(substream))
843 runtime->trigger_master = substream;
847 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
849 if (substream->runtime->trigger_master != substream)
851 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
854 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
856 if (substream->runtime->trigger_master == substream)
857 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
860 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
862 struct snd_pcm_runtime *runtime = substream->runtime;
863 snd_pcm_trigger_tstamp(substream);
864 runtime->hw_ptr_jiffies = jiffies;
865 runtime->status->state = state;
866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 runtime->silence_size > 0)
868 snd_pcm_playback_silence(substream, ULONG_MAX);
869 if (substream->timer)
870 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
871 &runtime->trigger_tstamp);
874 static struct action_ops snd_pcm_action_start = {
875 .pre_action = snd_pcm_pre_start,
876 .do_action = snd_pcm_do_start,
877 .undo_action = snd_pcm_undo_start,
878 .post_action = snd_pcm_post_start
882 * snd_pcm_start - start all linked streams
883 * @substream: the PCM substream instance
885 int snd_pcm_start(struct snd_pcm_substream *substream)
887 return snd_pcm_action(&snd_pcm_action_start, substream,
888 SNDRV_PCM_STATE_RUNNING);
894 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
896 struct snd_pcm_runtime *runtime = substream->runtime;
897 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
899 runtime->trigger_master = substream;
903 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
905 if (substream->runtime->trigger_master == substream &&
906 snd_pcm_running(substream))
907 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
908 return 0; /* unconditonally stop all substreams */
911 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
913 struct snd_pcm_runtime *runtime = substream->runtime;
914 if (runtime->status->state != state) {
915 snd_pcm_trigger_tstamp(substream);
916 if (substream->timer)
917 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
918 &runtime->trigger_tstamp);
919 runtime->status->state = state;
921 wake_up(&runtime->sleep);
922 wake_up(&runtime->tsleep);
925 static struct action_ops snd_pcm_action_stop = {
926 .pre_action = snd_pcm_pre_stop,
927 .do_action = snd_pcm_do_stop,
928 .post_action = snd_pcm_post_stop
932 * snd_pcm_stop - try to stop all running streams in the substream group
933 * @substream: the PCM substream instance
934 * @state: PCM state after stopping the stream
936 * The state of each stream is then changed to the given state unconditionally.
938 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
940 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
943 EXPORT_SYMBOL(snd_pcm_stop);
946 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
947 * @substream: the PCM substream
949 * After stopping, the state is changed to SETUP.
950 * Unlike snd_pcm_stop(), this affects only the given stream.
952 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
954 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
955 SNDRV_PCM_STATE_SETUP);
961 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
963 struct snd_pcm_runtime *runtime = substream->runtime;
964 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
967 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
969 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
971 runtime->trigger_master = substream;
975 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
977 if (substream->runtime->trigger_master != substream)
979 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
980 * a delta betwen the current jiffies, this gives a large enough
981 * delta, effectively to skip the check once.
983 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
984 return substream->ops->trigger(substream,
985 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
986 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
989 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
991 if (substream->runtime->trigger_master == substream)
992 substream->ops->trigger(substream,
993 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
994 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
997 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
999 struct snd_pcm_runtime *runtime = substream->runtime;
1000 snd_pcm_trigger_tstamp(substream);
1002 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1003 if (substream->timer)
1004 snd_timer_notify(substream->timer,
1005 SNDRV_TIMER_EVENT_MPAUSE,
1006 &runtime->trigger_tstamp);
1007 wake_up(&runtime->sleep);
1008 wake_up(&runtime->tsleep);
1010 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1011 if (substream->timer)
1012 snd_timer_notify(substream->timer,
1013 SNDRV_TIMER_EVENT_MCONTINUE,
1014 &runtime->trigger_tstamp);
1018 static struct action_ops snd_pcm_action_pause = {
1019 .pre_action = snd_pcm_pre_pause,
1020 .do_action = snd_pcm_do_pause,
1021 .undo_action = snd_pcm_undo_pause,
1022 .post_action = snd_pcm_post_pause
1026 * Push/release the pause for all linked streams.
1028 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1030 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1036 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1038 struct snd_pcm_runtime *runtime = substream->runtime;
1039 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1041 runtime->trigger_master = substream;
1045 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1047 struct snd_pcm_runtime *runtime = substream->runtime;
1048 if (runtime->trigger_master != substream)
1050 if (! snd_pcm_running(substream))
1052 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1053 return 0; /* suspend unconditionally */
1056 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1058 struct snd_pcm_runtime *runtime = substream->runtime;
1059 snd_pcm_trigger_tstamp(substream);
1060 if (substream->timer)
1061 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1062 &runtime->trigger_tstamp);
1063 runtime->status->suspended_state = runtime->status->state;
1064 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1065 wake_up(&runtime->sleep);
1066 wake_up(&runtime->tsleep);
1069 static struct action_ops snd_pcm_action_suspend = {
1070 .pre_action = snd_pcm_pre_suspend,
1071 .do_action = snd_pcm_do_suspend,
1072 .post_action = snd_pcm_post_suspend
1076 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1077 * @substream: the PCM substream
1079 * After this call, all streams are changed to SUSPENDED state.
1081 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1084 unsigned long flags;
1089 snd_pcm_stream_lock_irqsave(substream, flags);
1090 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1091 snd_pcm_stream_unlock_irqrestore(substream, flags);
1095 EXPORT_SYMBOL(snd_pcm_suspend);
1098 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1099 * @pcm: the PCM instance
1101 * After this call, all streams are changed to SUSPENDED state.
1103 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1105 struct snd_pcm_substream *substream;
1106 int stream, err = 0;
1111 for (stream = 0; stream < 2; stream++) {
1112 for (substream = pcm->streams[stream].substream;
1113 substream; substream = substream->next) {
1114 /* FIXME: the open/close code should lock this as well */
1115 if (substream->runtime == NULL)
1117 err = snd_pcm_suspend(substream);
1118 if (err < 0 && err != -EBUSY)
1125 EXPORT_SYMBOL(snd_pcm_suspend_all);
1129 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1131 struct snd_pcm_runtime *runtime = substream->runtime;
1132 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1134 runtime->trigger_master = substream;
1138 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1140 struct snd_pcm_runtime *runtime = substream->runtime;
1141 if (runtime->trigger_master != substream)
1143 /* DMA not running previously? */
1144 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1145 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1146 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1148 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1151 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1153 if (substream->runtime->trigger_master == substream &&
1154 snd_pcm_running(substream))
1155 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1158 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1160 struct snd_pcm_runtime *runtime = substream->runtime;
1161 snd_pcm_trigger_tstamp(substream);
1162 if (substream->timer)
1163 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1164 &runtime->trigger_tstamp);
1165 runtime->status->state = runtime->status->suspended_state;
1168 static struct action_ops snd_pcm_action_resume = {
1169 .pre_action = snd_pcm_pre_resume,
1170 .do_action = snd_pcm_do_resume,
1171 .undo_action = snd_pcm_undo_resume,
1172 .post_action = snd_pcm_post_resume
1175 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1177 struct snd_card *card = substream->pcm->card;
1180 snd_power_lock(card);
1181 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1182 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1183 snd_power_unlock(card);
1189 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1194 #endif /* CONFIG_PM */
1199 * Change the RUNNING stream(s) to XRUN state.
1201 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1203 struct snd_card *card = substream->pcm->card;
1204 struct snd_pcm_runtime *runtime = substream->runtime;
1207 snd_power_lock(card);
1208 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1209 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1214 snd_pcm_stream_lock_irq(substream);
1215 switch (runtime->status->state) {
1216 case SNDRV_PCM_STATE_XRUN:
1217 result = 0; /* already there */
1219 case SNDRV_PCM_STATE_RUNNING:
1220 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1225 snd_pcm_stream_unlock_irq(substream);
1227 snd_power_unlock(card);
1234 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1236 struct snd_pcm_runtime *runtime = substream->runtime;
1237 switch (runtime->status->state) {
1238 case SNDRV_PCM_STATE_RUNNING:
1239 case SNDRV_PCM_STATE_PREPARED:
1240 case SNDRV_PCM_STATE_PAUSED:
1241 case SNDRV_PCM_STATE_SUSPENDED:
1248 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1250 struct snd_pcm_runtime *runtime = substream->runtime;
1251 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1254 runtime->hw_ptr_base = 0;
1255 runtime->silence_start = runtime->status->hw_ptr;
1256 runtime->silence_filled = 0;
1260 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1262 struct snd_pcm_runtime *runtime = substream->runtime;
1263 runtime->control->appl_ptr = runtime->status->hw_ptr;
1264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1265 runtime->silence_size > 0)
1266 snd_pcm_playback_silence(substream, ULONG_MAX);
1269 static struct action_ops snd_pcm_action_reset = {
1270 .pre_action = snd_pcm_pre_reset,
1271 .do_action = snd_pcm_do_reset,
1272 .post_action = snd_pcm_post_reset
1275 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1277 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1283 /* we use the second argument for updating f_flags */
1284 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1287 struct snd_pcm_runtime *runtime = substream->runtime;
1288 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1289 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1291 if (snd_pcm_running(substream))
1293 substream->f_flags = f_flags;
1297 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1300 err = substream->ops->prepare(substream);
1303 return snd_pcm_do_reset(substream, 0);
1306 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1308 struct snd_pcm_runtime *runtime = substream->runtime;
1309 runtime->control->appl_ptr = runtime->status->hw_ptr;
1310 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1313 static struct action_ops snd_pcm_action_prepare = {
1314 .pre_action = snd_pcm_pre_prepare,
1315 .do_action = snd_pcm_do_prepare,
1316 .post_action = snd_pcm_post_prepare
1320 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1321 * @substream: the PCM substream instance
1322 * @file: file to refer f_flags
1324 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1328 struct snd_card *card = substream->pcm->card;
1332 f_flags = file->f_flags;
1334 f_flags = substream->f_flags;
1336 snd_power_lock(card);
1337 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1338 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1339 substream, f_flags);
1340 snd_power_unlock(card);
1348 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1350 substream->runtime->trigger_master = substream;
1354 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1356 struct snd_pcm_runtime *runtime = substream->runtime;
1357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1358 switch (runtime->status->state) {
1359 case SNDRV_PCM_STATE_PREPARED:
1360 /* start playback stream if possible */
1361 if (! snd_pcm_playback_empty(substream)) {
1362 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1363 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1366 case SNDRV_PCM_STATE_RUNNING:
1367 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1373 /* stop running stream */
1374 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1375 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1376 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1377 snd_pcm_do_stop(substream, new_state);
1378 snd_pcm_post_stop(substream, new_state);
1384 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1388 static struct action_ops snd_pcm_action_drain_init = {
1389 .pre_action = snd_pcm_pre_drain_init,
1390 .do_action = snd_pcm_do_drain_init,
1391 .post_action = snd_pcm_post_drain_init
1394 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1397 * Drain the stream(s).
1398 * When the substream is linked, sync until the draining of all playback streams
1400 * After this call, all streams are supposed to be either SETUP or DRAINING
1401 * (capture only) state.
1403 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1406 struct snd_card *card;
1407 struct snd_pcm_runtime *runtime;
1408 struct snd_pcm_substream *s;
1413 card = substream->pcm->card;
1414 runtime = substream->runtime;
1416 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1419 snd_power_lock(card);
1420 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1421 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1423 snd_power_unlock(card);
1429 if (file->f_flags & O_NONBLOCK)
1431 } else if (substream->f_flags & O_NONBLOCK)
1434 down_read(&snd_pcm_link_rwsem);
1435 snd_pcm_stream_lock_irq(substream);
1437 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1438 snd_pcm_pause(substream, 0);
1440 /* pre-start/stop - all running streams are changed to DRAINING state */
1441 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1444 /* in non-blocking, we don't wait in ioctl but let caller poll */
1452 struct snd_pcm_runtime *to_check;
1453 if (signal_pending(current)) {
1454 result = -ERESTARTSYS;
1457 /* find a substream to drain */
1459 snd_pcm_group_for_each_entry(s, substream) {
1460 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1462 runtime = s->runtime;
1463 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1469 break; /* all drained */
1470 init_waitqueue_entry(&wait, current);
1471 add_wait_queue(&to_check->sleep, &wait);
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 snd_pcm_stream_unlock_irq(substream);
1474 up_read(&snd_pcm_link_rwsem);
1475 snd_power_unlock(card);
1476 tout = schedule_timeout(10 * HZ);
1477 snd_power_lock(card);
1478 down_read(&snd_pcm_link_rwsem);
1479 snd_pcm_stream_lock_irq(substream);
1480 remove_wait_queue(&to_check->sleep, &wait);
1482 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1485 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1486 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1494 snd_pcm_stream_unlock_irq(substream);
1495 up_read(&snd_pcm_link_rwsem);
1496 snd_power_unlock(card);
1504 * Immediately put all linked substreams into SETUP state.
1506 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1508 struct snd_pcm_runtime *runtime;
1509 struct snd_card *card;
1512 if (PCM_RUNTIME_CHECK(substream))
1514 runtime = substream->runtime;
1515 card = substream->pcm->card;
1517 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1518 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1519 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1522 snd_pcm_stream_lock_irq(substream);
1524 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1525 snd_pcm_pause(substream, 0);
1527 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1528 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1529 snd_pcm_stream_unlock_irq(substream);
1535 /* WARNING: Don't forget to fput back the file */
1536 static struct file *snd_pcm_file_fd(int fd)
1539 struct inode *inode;
1545 inode = file->f_path.dentry->d_inode;
1546 if (!S_ISCHR(inode->i_mode) ||
1547 imajor(inode) != snd_major) {
1551 minor = iminor(inode);
1552 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1553 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1563 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1567 struct snd_pcm_file *pcm_file;
1568 struct snd_pcm_substream *substream1;
1570 file = snd_pcm_file_fd(fd);
1573 pcm_file = file->private_data;
1574 substream1 = pcm_file->substream;
1575 down_write(&snd_pcm_link_rwsem);
1576 write_lock_irq(&snd_pcm_link_rwlock);
1577 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1578 substream->runtime->status->state != substream1->runtime->status->state) {
1582 if (snd_pcm_stream_linked(substream1)) {
1586 if (!snd_pcm_stream_linked(substream)) {
1587 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1588 if (substream->group == NULL) {
1592 spin_lock_init(&substream->group->lock);
1593 INIT_LIST_HEAD(&substream->group->substreams);
1594 list_add_tail(&substream->link_list, &substream->group->substreams);
1595 substream->group->count = 1;
1597 list_add_tail(&substream1->link_list, &substream->group->substreams);
1598 substream->group->count++;
1599 substream1->group = substream->group;
1601 write_unlock_irq(&snd_pcm_link_rwlock);
1602 up_write(&snd_pcm_link_rwsem);
1607 static void relink_to_local(struct snd_pcm_substream *substream)
1609 substream->group = &substream->self_group;
1610 INIT_LIST_HEAD(&substream->self_group.substreams);
1611 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1614 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1616 struct snd_pcm_substream *s;
1619 down_write(&snd_pcm_link_rwsem);
1620 write_lock_irq(&snd_pcm_link_rwlock);
1621 if (!snd_pcm_stream_linked(substream)) {
1625 list_del(&substream->link_list);
1626 substream->group->count--;
1627 if (substream->group->count == 1) { /* detach the last stream, too */
1628 snd_pcm_group_for_each_entry(s, substream) {
1632 kfree(substream->group);
1634 relink_to_local(substream);
1636 write_unlock_irq(&snd_pcm_link_rwlock);
1637 up_write(&snd_pcm_link_rwsem);
1644 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1645 struct snd_pcm_hw_rule *rule)
1647 struct snd_interval t;
1648 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1649 hw_param_interval_c(params, rule->deps[1]), &t);
1650 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1653 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1654 struct snd_pcm_hw_rule *rule)
1656 struct snd_interval t;
1657 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1658 hw_param_interval_c(params, rule->deps[1]), &t);
1659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1662 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1663 struct snd_pcm_hw_rule *rule)
1665 struct snd_interval t;
1666 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1667 hw_param_interval_c(params, rule->deps[1]),
1668 (unsigned long) rule->private, &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1672 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
1675 struct snd_interval t;
1676 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1677 (unsigned long) rule->private,
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_format(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1686 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1688 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1690 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1692 if (! snd_mask_test(mask, k))
1694 bits = snd_pcm_format_physical_width(k);
1696 continue; /* ignore invalid formats */
1697 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1698 snd_mask_reset(&m, k);
1700 return snd_mask_refine(mask, &m);
1703 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1704 struct snd_pcm_hw_rule *rule)
1706 struct snd_interval t;
1712 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1714 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1716 bits = snd_pcm_format_physical_width(k);
1718 continue; /* ignore invalid formats */
1719 if (t.min > (unsigned)bits)
1721 if (t.max < (unsigned)bits)
1725 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1728 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1729 #error "Change this table"
1732 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1733 48000, 64000, 88200, 96000, 176400, 192000 };
1735 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1736 .count = ARRAY_SIZE(rates),
1740 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
1743 struct snd_pcm_hardware *hw = rule->private;
1744 return snd_interval_list(hw_param_interval(params, rule->var),
1745 snd_pcm_known_rates.count,
1746 snd_pcm_known_rates.list, hw->rates);
1749 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1750 struct snd_pcm_hw_rule *rule)
1752 struct snd_interval t;
1753 struct snd_pcm_substream *substream = rule->private;
1755 t.max = substream->buffer_bytes_max;
1759 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1762 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1764 struct snd_pcm_runtime *runtime = substream->runtime;
1765 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1768 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1769 snd_mask_any(constrs_mask(constrs, k));
1772 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1773 snd_interval_any(constrs_interval(constrs, k));
1776 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1777 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1778 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1779 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1780 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1782 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1783 snd_pcm_hw_rule_format, NULL,
1784 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1787 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1788 snd_pcm_hw_rule_sample_bits, NULL,
1789 SNDRV_PCM_HW_PARAM_FORMAT,
1790 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1793 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1794 snd_pcm_hw_rule_div, NULL,
1795 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1798 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1799 snd_pcm_hw_rule_mul, NULL,
1800 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1804 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1805 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1814 snd_pcm_hw_rule_div, NULL,
1815 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1819 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1820 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1824 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1825 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1829 snd_pcm_hw_rule_div, NULL,
1830 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1834 snd_pcm_hw_rule_div, NULL,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1839 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1845 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1849 snd_pcm_hw_rule_mul, NULL,
1850 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1855 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1864 snd_pcm_hw_rule_muldivk, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1869 snd_pcm_hw_rule_muldivk, (void*) 8,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1888 struct snd_pcm_runtime *runtime = substream->runtime;
1889 struct snd_pcm_hardware *hw = &runtime->hw;
1891 unsigned int mask = 0;
1893 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1894 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1895 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1896 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1897 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1898 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1899 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1900 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1901 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1902 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1903 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1905 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1909 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1913 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1917 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1918 hw->channels_min, hw->channels_max);
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1923 hw->rate_min, hw->rate_max);
1927 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1928 hw->period_bytes_min, hw->period_bytes_max);
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1933 hw->periods_min, hw->periods_max);
1937 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1938 hw->period_bytes_min, hw->buffer_bytes_max);
1942 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1943 snd_pcm_hw_rule_buffer_bytes_max, substream,
1944 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1949 if (runtime->dma_bytes) {
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1955 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1956 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1957 snd_pcm_hw_rule_rate, hw,
1958 SNDRV_PCM_HW_PARAM_RATE, -1);
1963 /* FIXME: this belong to lowlevel */
1964 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1969 static void pcm_release_private(struct snd_pcm_substream *substream)
1971 snd_pcm_unlink(substream);
1974 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1976 substream->ref_count--;
1977 if (substream->ref_count > 0)
1980 snd_pcm_drop(substream);
1981 if (substream->hw_opened) {
1982 if (substream->ops->hw_free != NULL)
1983 substream->ops->hw_free(substream);
1984 substream->ops->close(substream);
1985 substream->hw_opened = 0;
1987 if (substream->pcm_release) {
1988 substream->pcm_release(substream);
1989 substream->pcm_release = NULL;
1991 snd_pcm_detach_substream(substream);
1994 EXPORT_SYMBOL(snd_pcm_release_substream);
1996 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1998 struct snd_pcm_substream **rsubstream)
2000 struct snd_pcm_substream *substream;
2003 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2006 if (substream->ref_count > 1) {
2007 *rsubstream = substream;
2011 err = snd_pcm_hw_constraints_init(substream);
2013 snd_printd("snd_pcm_hw_constraints_init failed\n");
2017 if ((err = substream->ops->open(substream)) < 0)
2020 substream->hw_opened = 1;
2022 err = snd_pcm_hw_constraints_complete(substream);
2024 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2028 *rsubstream = substream;
2032 snd_pcm_release_substream(substream);
2036 EXPORT_SYMBOL(snd_pcm_open_substream);
2038 static int snd_pcm_open_file(struct file *file,
2039 struct snd_pcm *pcm,
2041 struct snd_pcm_file **rpcm_file)
2043 struct snd_pcm_file *pcm_file;
2044 struct snd_pcm_substream *substream;
2045 struct snd_pcm_str *str;
2051 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2055 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2056 if (pcm_file == NULL) {
2057 snd_pcm_release_substream(substream);
2060 pcm_file->substream = substream;
2061 if (substream->ref_count == 1) {
2062 str = substream->pstr;
2063 substream->file = pcm_file;
2064 substream->pcm_release = pcm_release_private;
2066 file->private_data = pcm_file;
2068 *rpcm_file = pcm_file;
2072 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2074 struct snd_pcm *pcm;
2076 pcm = snd_lookup_minor_data(iminor(inode),
2077 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2078 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2081 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2083 struct snd_pcm *pcm;
2085 pcm = snd_lookup_minor_data(iminor(inode),
2086 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2087 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2090 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2093 struct snd_pcm_file *pcm_file;
2100 err = snd_card_file_add(pcm->card, file);
2103 if (!try_module_get(pcm->card->module)) {
2107 init_waitqueue_entry(&wait, current);
2108 add_wait_queue(&pcm->open_wait, &wait);
2109 mutex_lock(&pcm->open_mutex);
2111 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2114 if (err == -EAGAIN) {
2115 if (file->f_flags & O_NONBLOCK) {
2121 set_current_state(TASK_INTERRUPTIBLE);
2122 mutex_unlock(&pcm->open_mutex);
2124 mutex_lock(&pcm->open_mutex);
2125 if (signal_pending(current)) {
2130 remove_wait_queue(&pcm->open_wait, &wait);
2131 mutex_unlock(&pcm->open_mutex);
2137 module_put(pcm->card->module);
2139 snd_card_file_remove(pcm->card, file);
2144 static int snd_pcm_release(struct inode *inode, struct file *file)
2146 struct snd_pcm *pcm;
2147 struct snd_pcm_substream *substream;
2148 struct snd_pcm_file *pcm_file;
2150 pcm_file = file->private_data;
2151 substream = pcm_file->substream;
2152 if (snd_BUG_ON(!substream))
2154 pcm = substream->pcm;
2155 mutex_lock(&pcm->open_mutex);
2156 snd_pcm_release_substream(substream);
2158 mutex_unlock(&pcm->open_mutex);
2159 wake_up(&pcm->open_wait);
2160 module_put(pcm->card->module);
2161 snd_card_file_remove(pcm->card, file);
2165 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2166 snd_pcm_uframes_t frames)
2168 struct snd_pcm_runtime *runtime = substream->runtime;
2169 snd_pcm_sframes_t appl_ptr;
2170 snd_pcm_sframes_t ret;
2171 snd_pcm_sframes_t hw_avail;
2176 snd_pcm_stream_lock_irq(substream);
2177 switch (runtime->status->state) {
2178 case SNDRV_PCM_STATE_PREPARED:
2180 case SNDRV_PCM_STATE_DRAINING:
2181 case SNDRV_PCM_STATE_RUNNING:
2182 if (snd_pcm_update_hw_ptr(substream) >= 0)
2185 case SNDRV_PCM_STATE_XRUN:
2188 case SNDRV_PCM_STATE_SUSPENDED:
2196 hw_avail = snd_pcm_playback_hw_avail(runtime);
2197 if (hw_avail <= 0) {
2201 if (frames > (snd_pcm_uframes_t)hw_avail)
2203 appl_ptr = runtime->control->appl_ptr - frames;
2205 appl_ptr += runtime->boundary;
2206 runtime->control->appl_ptr = appl_ptr;
2209 snd_pcm_stream_unlock_irq(substream);
2213 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2214 snd_pcm_uframes_t frames)
2216 struct snd_pcm_runtime *runtime = substream->runtime;
2217 snd_pcm_sframes_t appl_ptr;
2218 snd_pcm_sframes_t ret;
2219 snd_pcm_sframes_t hw_avail;
2224 snd_pcm_stream_lock_irq(substream);
2225 switch (runtime->status->state) {
2226 case SNDRV_PCM_STATE_PREPARED:
2227 case SNDRV_PCM_STATE_DRAINING:
2229 case SNDRV_PCM_STATE_RUNNING:
2230 if (snd_pcm_update_hw_ptr(substream) >= 0)
2233 case SNDRV_PCM_STATE_XRUN:
2236 case SNDRV_PCM_STATE_SUSPENDED:
2244 hw_avail = snd_pcm_capture_hw_avail(runtime);
2245 if (hw_avail <= 0) {
2249 if (frames > (snd_pcm_uframes_t)hw_avail)
2251 appl_ptr = runtime->control->appl_ptr - frames;
2253 appl_ptr += runtime->boundary;
2254 runtime->control->appl_ptr = appl_ptr;
2257 snd_pcm_stream_unlock_irq(substream);
2261 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2262 snd_pcm_uframes_t frames)
2264 struct snd_pcm_runtime *runtime = substream->runtime;
2265 snd_pcm_sframes_t appl_ptr;
2266 snd_pcm_sframes_t ret;
2267 snd_pcm_sframes_t avail;
2272 snd_pcm_stream_lock_irq(substream);
2273 switch (runtime->status->state) {
2274 case SNDRV_PCM_STATE_PREPARED:
2275 case SNDRV_PCM_STATE_PAUSED:
2277 case SNDRV_PCM_STATE_DRAINING:
2278 case SNDRV_PCM_STATE_RUNNING:
2279 if (snd_pcm_update_hw_ptr(substream) >= 0)
2282 case SNDRV_PCM_STATE_XRUN:
2285 case SNDRV_PCM_STATE_SUSPENDED:
2293 avail = snd_pcm_playback_avail(runtime);
2298 if (frames > (snd_pcm_uframes_t)avail)
2300 appl_ptr = runtime->control->appl_ptr + frames;
2301 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2302 appl_ptr -= runtime->boundary;
2303 runtime->control->appl_ptr = appl_ptr;
2306 snd_pcm_stream_unlock_irq(substream);
2310 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2311 snd_pcm_uframes_t frames)
2313 struct snd_pcm_runtime *runtime = substream->runtime;
2314 snd_pcm_sframes_t appl_ptr;
2315 snd_pcm_sframes_t ret;
2316 snd_pcm_sframes_t avail;
2321 snd_pcm_stream_lock_irq(substream);
2322 switch (runtime->status->state) {
2323 case SNDRV_PCM_STATE_PREPARED:
2324 case SNDRV_PCM_STATE_DRAINING:
2325 case SNDRV_PCM_STATE_PAUSED:
2327 case SNDRV_PCM_STATE_RUNNING:
2328 if (snd_pcm_update_hw_ptr(substream) >= 0)
2331 case SNDRV_PCM_STATE_XRUN:
2334 case SNDRV_PCM_STATE_SUSPENDED:
2342 avail = snd_pcm_capture_avail(runtime);
2347 if (frames > (snd_pcm_uframes_t)avail)
2349 appl_ptr = runtime->control->appl_ptr + frames;
2350 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2351 appl_ptr -= runtime->boundary;
2352 runtime->control->appl_ptr = appl_ptr;
2355 snd_pcm_stream_unlock_irq(substream);
2359 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2361 struct snd_pcm_runtime *runtime = substream->runtime;
2364 snd_pcm_stream_lock_irq(substream);
2365 switch (runtime->status->state) {
2366 case SNDRV_PCM_STATE_DRAINING:
2367 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2369 case SNDRV_PCM_STATE_RUNNING:
2370 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2373 case SNDRV_PCM_STATE_PREPARED:
2374 case SNDRV_PCM_STATE_SUSPENDED:
2377 case SNDRV_PCM_STATE_XRUN:
2385 snd_pcm_stream_unlock_irq(substream);
2389 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2390 snd_pcm_sframes_t __user *res)
2392 struct snd_pcm_runtime *runtime = substream->runtime;
2394 snd_pcm_sframes_t n = 0;
2396 snd_pcm_stream_lock_irq(substream);
2397 switch (runtime->status->state) {
2398 case SNDRV_PCM_STATE_DRAINING:
2399 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2401 case SNDRV_PCM_STATE_RUNNING:
2402 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2405 case SNDRV_PCM_STATE_PREPARED:
2406 case SNDRV_PCM_STATE_SUSPENDED:
2408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2409 n = snd_pcm_playback_hw_avail(runtime);
2411 n = snd_pcm_capture_avail(runtime);
2412 n += runtime->delay;
2414 case SNDRV_PCM_STATE_XRUN:
2422 snd_pcm_stream_unlock_irq(substream);
2424 if (put_user(n, res))
2429 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2430 struct snd_pcm_sync_ptr __user *_sync_ptr)
2432 struct snd_pcm_runtime *runtime = substream->runtime;
2433 struct snd_pcm_sync_ptr sync_ptr;
2434 volatile struct snd_pcm_mmap_status *status;
2435 volatile struct snd_pcm_mmap_control *control;
2438 memset(&sync_ptr, 0, sizeof(sync_ptr));
2439 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2441 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2443 status = runtime->status;
2444 control = runtime->control;
2445 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2446 err = snd_pcm_hwsync(substream);
2450 snd_pcm_stream_lock_irq(substream);
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2452 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2454 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2455 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2456 control->avail_min = sync_ptr.c.control.avail_min;
2458 sync_ptr.c.control.avail_min = control->avail_min;
2459 sync_ptr.s.status.state = status->state;
2460 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2461 sync_ptr.s.status.tstamp = status->tstamp;
2462 sync_ptr.s.status.suspended_state = status->suspended_state;
2463 snd_pcm_stream_unlock_irq(substream);
2464 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2469 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2471 struct snd_pcm_runtime *runtime = substream->runtime;
2474 if (get_user(arg, _arg))
2476 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2478 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2479 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2480 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2484 static int snd_pcm_common_ioctl1(struct file *file,
2485 struct snd_pcm_substream *substream,
2486 unsigned int cmd, void __user *arg)
2489 case SNDRV_PCM_IOCTL_PVERSION:
2490 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2491 case SNDRV_PCM_IOCTL_INFO:
2492 return snd_pcm_info_user(substream, arg);
2493 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2495 case SNDRV_PCM_IOCTL_TTSTAMP:
2496 return snd_pcm_tstamp(substream, arg);
2497 case SNDRV_PCM_IOCTL_HW_REFINE:
2498 return snd_pcm_hw_refine_user(substream, arg);
2499 case SNDRV_PCM_IOCTL_HW_PARAMS:
2500 return snd_pcm_hw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_HW_FREE:
2502 return snd_pcm_hw_free(substream);
2503 case SNDRV_PCM_IOCTL_SW_PARAMS:
2504 return snd_pcm_sw_params_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_STATUS:
2506 return snd_pcm_status_user(substream, arg);
2507 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2508 return snd_pcm_channel_info_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_PREPARE:
2510 return snd_pcm_prepare(substream, file);
2511 case SNDRV_PCM_IOCTL_RESET:
2512 return snd_pcm_reset(substream);
2513 case SNDRV_PCM_IOCTL_START:
2514 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2515 case SNDRV_PCM_IOCTL_LINK:
2516 return snd_pcm_link(substream, (int)(unsigned long) arg);
2517 case SNDRV_PCM_IOCTL_UNLINK:
2518 return snd_pcm_unlink(substream);
2519 case SNDRV_PCM_IOCTL_RESUME:
2520 return snd_pcm_resume(substream);
2521 case SNDRV_PCM_IOCTL_XRUN:
2522 return snd_pcm_xrun(substream);
2523 case SNDRV_PCM_IOCTL_HWSYNC:
2524 return snd_pcm_hwsync(substream);
2525 case SNDRV_PCM_IOCTL_DELAY:
2526 return snd_pcm_delay(substream, arg);
2527 case SNDRV_PCM_IOCTL_SYNC_PTR:
2528 return snd_pcm_sync_ptr(substream, arg);
2529 #ifdef CONFIG_SND_SUPPORT_OLD_API
2530 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2531 return snd_pcm_hw_refine_old_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2533 return snd_pcm_hw_params_old_user(substream, arg);
2535 case SNDRV_PCM_IOCTL_DRAIN:
2536 return snd_pcm_drain(substream, file);
2537 case SNDRV_PCM_IOCTL_DROP:
2538 return snd_pcm_drop(substream);
2539 case SNDRV_PCM_IOCTL_PAUSE:
2542 snd_pcm_stream_lock_irq(substream);
2543 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2544 snd_pcm_stream_unlock_irq(substream);
2548 snd_printd("unknown ioctl = 0x%x\n", cmd);
2552 static int snd_pcm_playback_ioctl1(struct file *file,
2553 struct snd_pcm_substream *substream,
2554 unsigned int cmd, void __user *arg)
2556 if (snd_BUG_ON(!substream))
2558 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2561 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2563 struct snd_xferi xferi;
2564 struct snd_xferi __user *_xferi = arg;
2565 struct snd_pcm_runtime *runtime = substream->runtime;
2566 snd_pcm_sframes_t result;
2567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2569 if (put_user(0, &_xferi->result))
2571 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2573 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2574 __put_user(result, &_xferi->result);
2575 return result < 0 ? result : 0;
2577 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2579 struct snd_xfern xfern;
2580 struct snd_xfern __user *_xfern = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
2583 snd_pcm_sframes_t result;
2584 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2586 if (runtime->channels > 128)
2588 if (put_user(0, &_xfern->result))
2590 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2593 bufs = memdup_user(xfern.bufs,
2594 sizeof(void *) * runtime->channels);
2596 return PTR_ERR(bufs);
2597 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2599 __put_user(result, &_xfern->result);
2600 return result < 0 ? result : 0;
2602 case SNDRV_PCM_IOCTL_REWIND:
2604 snd_pcm_uframes_t frames;
2605 snd_pcm_uframes_t __user *_frames = arg;
2606 snd_pcm_sframes_t result;
2607 if (get_user(frames, _frames))
2609 if (put_user(0, _frames))
2611 result = snd_pcm_playback_rewind(substream, frames);
2612 __put_user(result, _frames);
2613 return result < 0 ? result : 0;
2615 case SNDRV_PCM_IOCTL_FORWARD:
2617 snd_pcm_uframes_t frames;
2618 snd_pcm_uframes_t __user *_frames = arg;
2619 snd_pcm_sframes_t result;
2620 if (get_user(frames, _frames))
2622 if (put_user(0, _frames))
2624 result = snd_pcm_playback_forward(substream, frames);
2625 __put_user(result, _frames);
2626 return result < 0 ? result : 0;
2629 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2632 static int snd_pcm_capture_ioctl1(struct file *file,
2633 struct snd_pcm_substream *substream,
2634 unsigned int cmd, void __user *arg)
2636 if (snd_BUG_ON(!substream))
2638 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2641 case SNDRV_PCM_IOCTL_READI_FRAMES:
2643 struct snd_xferi xferi;
2644 struct snd_xferi __user *_xferi = arg;
2645 struct snd_pcm_runtime *runtime = substream->runtime;
2646 snd_pcm_sframes_t result;
2647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2649 if (put_user(0, &_xferi->result))
2651 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2653 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2654 __put_user(result, &_xferi->result);
2655 return result < 0 ? result : 0;
2657 case SNDRV_PCM_IOCTL_READN_FRAMES:
2659 struct snd_xfern xfern;
2660 struct snd_xfern __user *_xfern = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
2663 snd_pcm_sframes_t result;
2664 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2666 if (runtime->channels > 128)
2668 if (put_user(0, &_xfern->result))
2670 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2673 bufs = memdup_user(xfern.bufs,
2674 sizeof(void *) * runtime->channels);
2676 return PTR_ERR(bufs);
2677 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2679 __put_user(result, &_xfern->result);
2680 return result < 0 ? result : 0;
2682 case SNDRV_PCM_IOCTL_REWIND:
2684 snd_pcm_uframes_t frames;
2685 snd_pcm_uframes_t __user *_frames = arg;
2686 snd_pcm_sframes_t result;
2687 if (get_user(frames, _frames))
2689 if (put_user(0, _frames))
2691 result = snd_pcm_capture_rewind(substream, frames);
2692 __put_user(result, _frames);
2693 return result < 0 ? result : 0;
2695 case SNDRV_PCM_IOCTL_FORWARD:
2697 snd_pcm_uframes_t frames;
2698 snd_pcm_uframes_t __user *_frames = arg;
2699 snd_pcm_sframes_t result;
2700 if (get_user(frames, _frames))
2702 if (put_user(0, _frames))
2704 result = snd_pcm_capture_forward(substream, frames);
2705 __put_user(result, _frames);
2706 return result < 0 ? result : 0;
2709 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2712 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2715 struct snd_pcm_file *pcm_file;
2717 pcm_file = file->private_data;
2719 if (((cmd >> 8) & 0xff) != 'A')
2722 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2723 (void __user *)arg);
2726 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2729 struct snd_pcm_file *pcm_file;
2731 pcm_file = file->private_data;
2733 if (((cmd >> 8) & 0xff) != 'A')
2736 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2737 (void __user *)arg);
2740 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2741 unsigned int cmd, void *arg)
2746 fs = snd_enter_user();
2747 switch (substream->stream) {
2748 case SNDRV_PCM_STREAM_PLAYBACK:
2749 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2750 (void __user *)arg);
2752 case SNDRV_PCM_STREAM_CAPTURE:
2753 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2754 (void __user *)arg);
2764 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2766 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2769 struct snd_pcm_file *pcm_file;
2770 struct snd_pcm_substream *substream;
2771 struct snd_pcm_runtime *runtime;
2772 snd_pcm_sframes_t result;
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
2776 if (PCM_RUNTIME_CHECK(substream))
2778 runtime = substream->runtime;
2779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2781 if (!frame_aligned(runtime, count))
2783 count = bytes_to_frames(runtime, count);
2784 result = snd_pcm_lib_read(substream, buf, count);
2786 result = frames_to_bytes(runtime, result);
2790 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2791 size_t count, loff_t * offset)
2793 struct snd_pcm_file *pcm_file;
2794 struct snd_pcm_substream *substream;
2795 struct snd_pcm_runtime *runtime;
2796 snd_pcm_sframes_t result;
2798 pcm_file = file->private_data;
2799 substream = pcm_file->substream;
2800 if (PCM_RUNTIME_CHECK(substream))
2802 runtime = substream->runtime;
2803 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2805 if (!frame_aligned(runtime, count))
2807 count = bytes_to_frames(runtime, count);
2808 result = snd_pcm_lib_write(substream, buf, count);
2810 result = frames_to_bytes(runtime, result);
2814 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2815 unsigned long nr_segs, loff_t pos)
2818 struct snd_pcm_file *pcm_file;
2819 struct snd_pcm_substream *substream;
2820 struct snd_pcm_runtime *runtime;
2821 snd_pcm_sframes_t result;
2824 snd_pcm_uframes_t frames;
2826 pcm_file = iocb->ki_filp->private_data;
2827 substream = pcm_file->substream;
2828 if (PCM_RUNTIME_CHECK(substream))
2830 runtime = substream->runtime;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2833 if (nr_segs > 1024 || nr_segs != runtime->channels)
2835 if (!frame_aligned(runtime, iov->iov_len))
2837 frames = bytes_to_samples(runtime, iov->iov_len);
2838 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2841 for (i = 0; i < nr_segs; ++i)
2842 bufs[i] = iov[i].iov_base;
2843 result = snd_pcm_lib_readv(substream, bufs, frames);
2845 result = frames_to_bytes(runtime, result);
2850 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2851 unsigned long nr_segs, loff_t pos)
2853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
2856 snd_pcm_sframes_t result;
2859 snd_pcm_uframes_t frames;
2861 pcm_file = iocb->ki_filp->private_data;
2862 substream = pcm_file->substream;
2863 if (PCM_RUNTIME_CHECK(substream))
2865 runtime = substream->runtime;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2868 if (nr_segs > 128 || nr_segs != runtime->channels ||
2869 !frame_aligned(runtime, iov->iov_len))
2871 frames = bytes_to_samples(runtime, iov->iov_len);
2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2875 for (i = 0; i < nr_segs; ++i)
2876 bufs[i] = iov[i].iov_base;
2877 result = snd_pcm_lib_writev(substream, bufs, frames);
2879 result = frames_to_bytes(runtime, result);
2884 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2886 struct snd_pcm_file *pcm_file;
2887 struct snd_pcm_substream *substream;
2888 struct snd_pcm_runtime *runtime;
2890 snd_pcm_uframes_t avail;
2892 pcm_file = file->private_data;
2894 substream = pcm_file->substream;
2895 if (PCM_RUNTIME_CHECK(substream))
2897 runtime = substream->runtime;
2899 poll_wait(file, &runtime->sleep, wait);
2901 snd_pcm_stream_lock_irq(substream);
2902 avail = snd_pcm_playback_avail(runtime);
2903 switch (runtime->status->state) {
2904 case SNDRV_PCM_STATE_RUNNING:
2905 case SNDRV_PCM_STATE_PREPARED:
2906 case SNDRV_PCM_STATE_PAUSED:
2907 if (avail >= runtime->control->avail_min) {
2908 mask = POLLOUT | POLLWRNORM;
2912 case SNDRV_PCM_STATE_DRAINING:
2916 mask = POLLOUT | POLLWRNORM | POLLERR;
2919 snd_pcm_stream_unlock_irq(substream);
2923 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2925 struct snd_pcm_file *pcm_file;
2926 struct snd_pcm_substream *substream;
2927 struct snd_pcm_runtime *runtime;
2929 snd_pcm_uframes_t avail;
2931 pcm_file = file->private_data;
2933 substream = pcm_file->substream;
2934 if (PCM_RUNTIME_CHECK(substream))
2936 runtime = substream->runtime;
2938 poll_wait(file, &runtime->sleep, wait);
2940 snd_pcm_stream_lock_irq(substream);
2941 avail = snd_pcm_capture_avail(runtime);
2942 switch (runtime->status->state) {
2943 case SNDRV_PCM_STATE_RUNNING:
2944 case SNDRV_PCM_STATE_PREPARED:
2945 case SNDRV_PCM_STATE_PAUSED:
2946 if (avail >= runtime->control->avail_min) {
2947 mask = POLLIN | POLLRDNORM;
2952 case SNDRV_PCM_STATE_DRAINING:
2954 mask = POLLIN | POLLRDNORM;
2959 mask = POLLIN | POLLRDNORM | POLLERR;
2962 snd_pcm_stream_unlock_irq(substream);
2971 * Only on coherent architectures, we can mmap the status and the control records
2972 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2974 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2976 * mmap status record
2978 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2979 struct vm_fault *vmf)
2981 struct snd_pcm_substream *substream = area->vm_private_data;
2982 struct snd_pcm_runtime *runtime;
2984 if (substream == NULL)
2985 return VM_FAULT_SIGBUS;
2986 runtime = substream->runtime;
2987 vmf->page = virt_to_page(runtime->status);
2988 get_page(vmf->page);
2992 static const struct vm_operations_struct snd_pcm_vm_ops_status =
2994 .fault = snd_pcm_mmap_status_fault,
2997 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
2998 struct vm_area_struct *area)
3000 struct snd_pcm_runtime *runtime;
3002 if (!(area->vm_flags & VM_READ))
3004 runtime = substream->runtime;
3005 size = area->vm_end - area->vm_start;
3006 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3008 area->vm_ops = &snd_pcm_vm_ops_status;
3009 area->vm_private_data = substream;
3010 area->vm_flags |= VM_RESERVED;
3015 * mmap control record
3017 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3018 struct vm_fault *vmf)
3020 struct snd_pcm_substream *substream = area->vm_private_data;
3021 struct snd_pcm_runtime *runtime;
3023 if (substream == NULL)
3024 return VM_FAULT_SIGBUS;
3025 runtime = substream->runtime;
3026 vmf->page = virt_to_page(runtime->control);
3027 get_page(vmf->page);
3031 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3033 .fault = snd_pcm_mmap_control_fault,
3036 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3037 struct vm_area_struct *area)
3039 struct snd_pcm_runtime *runtime;
3041 if (!(area->vm_flags & VM_READ))
3043 runtime = substream->runtime;
3044 size = area->vm_end - area->vm_start;
3045 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3047 area->vm_ops = &snd_pcm_vm_ops_control;
3048 area->vm_private_data = substream;
3049 area->vm_flags |= VM_RESERVED;
3052 #else /* ! coherent mmap */
3054 * don't support mmap for status and control records.
3056 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3057 struct vm_area_struct *area)
3061 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3062 struct vm_area_struct *area)
3066 #endif /* coherent mmap */
3068 static inline struct page *
3069 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3071 void *vaddr = substream->runtime->dma_area + ofs;
3072 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3073 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3074 return virt_to_page(CAC_ADDR(vaddr));
3076 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3077 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3078 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3079 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3080 /* assume dma_handle set via pfn_to_phys() in
3081 * mm/dma-noncoherent.c
3083 return pfn_to_page(addr >> PAGE_SHIFT);
3086 return virt_to_page(vaddr);
3090 * fault callback for mmapping a RAM page
3092 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3093 struct vm_fault *vmf)
3095 struct snd_pcm_substream *substream = area->vm_private_data;
3096 struct snd_pcm_runtime *runtime;
3097 unsigned long offset;
3101 if (substream == NULL)
3102 return VM_FAULT_SIGBUS;
3103 runtime = substream->runtime;
3104 offset = vmf->pgoff << PAGE_SHIFT;
3105 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3106 if (offset > dma_bytes - PAGE_SIZE)
3107 return VM_FAULT_SIGBUS;
3108 if (substream->ops->page)
3109 page = substream->ops->page(substream, offset);
3111 page = snd_pcm_default_page_ops(substream, offset);
3113 return VM_FAULT_SIGBUS;
3119 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3120 .open = snd_pcm_mmap_data_open,
3121 .close = snd_pcm_mmap_data_close,
3124 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3125 .open = snd_pcm_mmap_data_open,
3126 .close = snd_pcm_mmap_data_close,
3127 .fault = snd_pcm_mmap_data_fault,
3130 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3131 /* This should be defined / handled globally! */
3133 #define ARCH_HAS_DMA_MMAP_COHERENT
3138 * mmap the DMA buffer on RAM
3140 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3141 struct vm_area_struct *area)
3143 area->vm_flags |= VM_RESERVED;
3144 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3145 if (!substream->ops->page &&
3146 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3147 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3149 substream->runtime->dma_area,
3150 substream->runtime->dma_addr,
3151 area->vm_end - area->vm_start);
3152 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3153 /* mmap with fault handler */
3154 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3159 * mmap the DMA buffer on I/O memory area
3161 #if SNDRV_PCM_INFO_MMAP_IOMEM
3162 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3163 struct vm_area_struct *area)
3166 unsigned long offset;
3168 #ifdef pgprot_noncached
3169 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3171 area->vm_flags |= VM_IO;
3172 size = area->vm_end - area->vm_start;
3173 offset = area->vm_pgoff << PAGE_SHIFT;
3174 if (io_remap_pfn_range(area, area->vm_start,
3175 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3176 size, area->vm_page_prot))
3181 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3182 #endif /* SNDRV_PCM_INFO_MMAP */
3187 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3188 struct vm_area_struct *area)
3190 struct snd_pcm_runtime *runtime;
3192 unsigned long offset;
3196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3197 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3200 if (!(area->vm_flags & VM_READ))
3203 runtime = substream->runtime;
3204 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3206 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3208 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3209 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3211 size = area->vm_end - area->vm_start;
3212 offset = area->vm_pgoff << PAGE_SHIFT;
3213 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3214 if ((size_t)size > dma_bytes)
3216 if (offset > dma_bytes - size)
3219 area->vm_ops = &snd_pcm_vm_ops_data;
3220 area->vm_private_data = substream;
3221 if (substream->ops->mmap)
3222 err = substream->ops->mmap(substream, area);
3224 err = snd_pcm_default_mmap(substream, area);
3226 atomic_inc(&substream->mmap_count);
3230 EXPORT_SYMBOL(snd_pcm_mmap_data);
3232 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3234 struct snd_pcm_file * pcm_file;
3235 struct snd_pcm_substream *substream;
3236 unsigned long offset;
3238 pcm_file = file->private_data;
3239 substream = pcm_file->substream;
3240 if (PCM_RUNTIME_CHECK(substream))
3243 offset = area->vm_pgoff << PAGE_SHIFT;
3245 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3246 if (pcm_file->no_compat_mmap)
3248 return snd_pcm_mmap_status(substream, file, area);
3249 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3250 if (pcm_file->no_compat_mmap)
3252 return snd_pcm_mmap_control(substream, file, area);
3254 return snd_pcm_mmap_data(substream, file, area);
3259 static int snd_pcm_fasync(int fd, struct file * file, int on)
3261 struct snd_pcm_file * pcm_file;
3262 struct snd_pcm_substream *substream;
3263 struct snd_pcm_runtime *runtime;
3267 pcm_file = file->private_data;
3268 substream = pcm_file->substream;
3269 if (PCM_RUNTIME_CHECK(substream))
3271 runtime = substream->runtime;
3272 err = fasync_helper(fd, file, on, &runtime->fasync);
3281 #ifdef CONFIG_COMPAT
3282 #include "pcm_compat.c"
3284 #define snd_pcm_ioctl_compat NULL
3288 * To be removed helpers to keep binary compatibility
3291 #ifdef CONFIG_SND_SUPPORT_OLD_API
3292 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3293 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3295 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3296 struct snd_pcm_hw_params_old *oparams)
3300 memset(params, 0, sizeof(*params));
3301 params->flags = oparams->flags;
3302 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3303 params->masks[i].bits[0] = oparams->masks[i];
3304 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3305 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3306 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3307 params->info = oparams->info;
3308 params->msbits = oparams->msbits;
3309 params->rate_num = oparams->rate_num;
3310 params->rate_den = oparams->rate_den;
3311 params->fifo_size = oparams->fifo_size;
3314 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3315 struct snd_pcm_hw_params *params)
3319 memset(oparams, 0, sizeof(*oparams));
3320 oparams->flags = params->flags;
3321 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3322 oparams->masks[i] = params->masks[i].bits[0];
3323 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3324 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3325 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3326 oparams->info = params->info;
3327 oparams->msbits = params->msbits;
3328 oparams->rate_num = params->rate_num;
3329 oparams->rate_den = params->rate_den;
3330 oparams->fifo_size = params->fifo_size;
3333 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3334 struct snd_pcm_hw_params_old __user * _oparams)
3336 struct snd_pcm_hw_params *params;
3337 struct snd_pcm_hw_params_old *oparams = NULL;
3340 params = kmalloc(sizeof(*params), GFP_KERNEL);
3344 oparams = memdup_user(_oparams, sizeof(*oparams));
3345 if (IS_ERR(oparams)) {
3346 err = PTR_ERR(oparams);
3349 snd_pcm_hw_convert_from_old_params(params, oparams);
3350 err = snd_pcm_hw_refine(substream, params);
3351 snd_pcm_hw_convert_to_old_params(oparams, params);
3352 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3363 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3364 struct snd_pcm_hw_params_old __user * _oparams)
3366 struct snd_pcm_hw_params *params;
3367 struct snd_pcm_hw_params_old *oparams = NULL;
3370 params = kmalloc(sizeof(*params), GFP_KERNEL);
3374 oparams = memdup_user(_oparams, sizeof(*oparams));
3375 if (IS_ERR(oparams)) {
3376 err = PTR_ERR(oparams);
3379 snd_pcm_hw_convert_from_old_params(params, oparams);
3380 err = snd_pcm_hw_params(substream, params);
3381 snd_pcm_hw_convert_to_old_params(oparams, params);
3382 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3392 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3395 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3396 unsigned long len, unsigned long pgoff,
3397 unsigned long flags)
3402 # define dummy_get_unmapped_area NULL
3409 const struct file_operations snd_pcm_f_ops[2] = {
3411 .owner = THIS_MODULE,
3412 .write = snd_pcm_write,
3413 .aio_write = snd_pcm_aio_write,
3414 .open = snd_pcm_playback_open,
3415 .release = snd_pcm_release,
3416 .poll = snd_pcm_playback_poll,
3417 .unlocked_ioctl = snd_pcm_playback_ioctl,
3418 .compat_ioctl = snd_pcm_ioctl_compat,
3419 .mmap = snd_pcm_mmap,
3420 .fasync = snd_pcm_fasync,
3421 .get_unmapped_area = dummy_get_unmapped_area,
3424 .owner = THIS_MODULE,
3425 .read = snd_pcm_read,
3426 .aio_read = snd_pcm_aio_read,
3427 .open = snd_pcm_capture_open,
3428 .release = snd_pcm_release,
3429 .poll = snd_pcm_capture_poll,
3430 .unlocked_ioctl = snd_pcm_capture_ioctl,
3431 .compat_ioctl = snd_pcm_ioctl_compat,
3432 .mmap = snd_pcm_mmap,
3433 .fasync = snd_pcm_fasync,
3434 .get_unmapped_area = dummy_get_unmapped_area,