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 <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
42 struct snd_pcm_hw_params_old {
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
46 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rate_num;
53 unsigned int rate_den;
54 snd_pcm_uframes_t fifo_size;
55 unsigned char reserved[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
67 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem);
78 static inline mm_segment_t snd_enter_user(void)
80 mm_segment_t fs = get_fs();
85 static inline void snd_leave_user(mm_segment_t fs)
92 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
94 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
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(KERN_DEBUG "%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(KERN_DEBUG "%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(KERN_DEBUG "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 = memdup_user(_params, sizeof(*params));
332 return PTR_ERR(params);
334 err = snd_pcm_hw_refine(substream, params);
335 if (copy_to_user(_params, params, sizeof(*params))) {
344 static int period_to_usecs(struct snd_pcm_runtime *runtime)
349 return -1; /* invalid */
351 /* take 75% of period time as the deadline */
352 usecs = (750000 / runtime->rate) * runtime->period_size;
353 usecs += ((750000 % runtime->rate) * runtime->period_size) /
359 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
360 struct snd_pcm_hw_params *params)
362 struct snd_pcm_runtime *runtime;
365 snd_pcm_uframes_t frames;
367 if (PCM_RUNTIME_CHECK(substream))
369 runtime = substream->runtime;
370 snd_pcm_stream_lock_irq(substream);
371 switch (runtime->status->state) {
372 case SNDRV_PCM_STATE_OPEN:
373 case SNDRV_PCM_STATE_SETUP:
374 case SNDRV_PCM_STATE_PREPARED:
377 snd_pcm_stream_unlock_irq(substream);
380 snd_pcm_stream_unlock_irq(substream);
381 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
382 if (!substream->oss.oss)
384 if (atomic_read(&substream->mmap_count))
388 err = snd_pcm_hw_refine(substream, params);
392 err = snd_pcm_hw_params_choose(substream, params);
396 if (substream->ops->hw_params != NULL) {
397 err = substream->ops->hw_params(substream, params);
402 runtime->access = params_access(params);
403 runtime->format = params_format(params);
404 runtime->subformat = params_subformat(params);
405 runtime->channels = params_channels(params);
406 runtime->rate = params_rate(params);
407 runtime->period_size = params_period_size(params);
408 runtime->periods = params_periods(params);
409 runtime->buffer_size = params_buffer_size(params);
410 runtime->info = params->info;
411 runtime->rate_num = params->rate_num;
412 runtime->rate_den = params->rate_den;
414 bits = snd_pcm_format_physical_width(runtime->format);
415 runtime->sample_bits = bits;
416 bits *= runtime->channels;
417 runtime->frame_bits = bits;
419 while (bits % 8 != 0) {
423 runtime->byte_align = bits / 8;
424 runtime->min_align = frames;
426 /* Default sw params */
427 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
428 runtime->period_step = 1;
429 runtime->control->avail_min = runtime->period_size;
430 runtime->start_threshold = 1;
431 runtime->stop_threshold = runtime->buffer_size;
432 runtime->silence_threshold = 0;
433 runtime->silence_size = 0;
434 runtime->boundary = runtime->buffer_size;
435 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
436 runtime->boundary *= 2;
438 snd_pcm_timer_resolution_change(substream);
439 runtime->status->state = SNDRV_PCM_STATE_SETUP;
441 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
442 substream->latency_id);
443 if ((usecs = period_to_usecs(runtime)) >= 0)
444 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
445 substream->latency_id, usecs);
448 /* hardware might be unuseable from this time,
449 so we force application to retry to set
450 the correct hardware parameter settings */
451 runtime->status->state = SNDRV_PCM_STATE_OPEN;
452 if (substream->ops->hw_free != NULL)
453 substream->ops->hw_free(substream);
457 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
458 struct snd_pcm_hw_params __user * _params)
460 struct snd_pcm_hw_params *params;
463 params = memdup_user(_params, sizeof(*params));
465 return PTR_ERR(params);
467 err = snd_pcm_hw_params(substream, params);
468 if (copy_to_user(_params, params, sizeof(*params))) {
477 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
479 struct snd_pcm_runtime *runtime;
482 if (PCM_RUNTIME_CHECK(substream))
484 runtime = substream->runtime;
485 snd_pcm_stream_lock_irq(substream);
486 switch (runtime->status->state) {
487 case SNDRV_PCM_STATE_SETUP:
488 case SNDRV_PCM_STATE_PREPARED:
491 snd_pcm_stream_unlock_irq(substream);
494 snd_pcm_stream_unlock_irq(substream);
495 if (atomic_read(&substream->mmap_count))
497 if (substream->ops->hw_free)
498 result = substream->ops->hw_free(substream);
499 runtime->status->state = SNDRV_PCM_STATE_OPEN;
500 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
501 substream->latency_id);
505 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_sw_params *params)
508 struct snd_pcm_runtime *runtime;
510 if (PCM_RUNTIME_CHECK(substream))
512 runtime = substream->runtime;
513 snd_pcm_stream_lock_irq(substream);
514 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
515 snd_pcm_stream_unlock_irq(substream);
518 snd_pcm_stream_unlock_irq(substream);
520 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
522 if (params->avail_min == 0)
524 if (params->silence_size >= runtime->boundary) {
525 if (params->silence_threshold != 0)
528 if (params->silence_size > params->silence_threshold)
530 if (params->silence_threshold > runtime->buffer_size)
533 snd_pcm_stream_lock_irq(substream);
534 runtime->tstamp_mode = params->tstamp_mode;
535 runtime->period_step = params->period_step;
536 runtime->control->avail_min = params->avail_min;
537 runtime->start_threshold = params->start_threshold;
538 runtime->stop_threshold = params->stop_threshold;
539 runtime->silence_threshold = params->silence_threshold;
540 runtime->silence_size = params->silence_size;
541 params->boundary = runtime->boundary;
542 if (snd_pcm_running(substream)) {
543 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
544 runtime->silence_size > 0)
545 snd_pcm_playback_silence(substream, ULONG_MAX);
546 wake_up(&runtime->sleep);
548 snd_pcm_stream_unlock_irq(substream);
552 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
553 struct snd_pcm_sw_params __user * _params)
555 struct snd_pcm_sw_params params;
557 if (copy_from_user(¶ms, _params, sizeof(params)))
559 err = snd_pcm_sw_params(substream, ¶ms);
560 if (copy_to_user(_params, ¶ms, sizeof(params)))
565 int snd_pcm_status(struct snd_pcm_substream *substream,
566 struct snd_pcm_status *status)
568 struct snd_pcm_runtime *runtime = substream->runtime;
570 snd_pcm_stream_lock_irq(substream);
571 status->state = runtime->status->state;
572 status->suspended_state = runtime->status->suspended_state;
573 if (status->state == SNDRV_PCM_STATE_OPEN)
575 status->trigger_tstamp = runtime->trigger_tstamp;
576 if (snd_pcm_running(substream)) {
577 snd_pcm_update_hw_ptr(substream);
578 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
579 status->tstamp = runtime->status->tstamp;
583 snd_pcm_gettime(runtime, &status->tstamp);
585 status->appl_ptr = runtime->control->appl_ptr;
586 status->hw_ptr = runtime->status->hw_ptr;
587 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
588 status->avail = snd_pcm_playback_avail(runtime);
589 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
590 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
591 status->delay = runtime->buffer_size - status->avail;
595 status->avail = snd_pcm_capture_avail(runtime);
596 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
597 status->delay = status->avail;
601 status->avail_max = runtime->avail_max;
602 status->overrange = runtime->overrange;
603 runtime->avail_max = 0;
604 runtime->overrange = 0;
606 snd_pcm_stream_unlock_irq(substream);
610 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
611 struct snd_pcm_status __user * _status)
613 struct snd_pcm_status status;
616 memset(&status, 0, sizeof(status));
617 res = snd_pcm_status(substream, &status);
620 if (copy_to_user(_status, &status, sizeof(status)))
625 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
626 struct snd_pcm_channel_info * info)
628 struct snd_pcm_runtime *runtime;
629 unsigned int channel;
631 channel = info->channel;
632 runtime = substream->runtime;
633 snd_pcm_stream_lock_irq(substream);
634 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
635 snd_pcm_stream_unlock_irq(substream);
638 snd_pcm_stream_unlock_irq(substream);
639 if (channel >= runtime->channels)
641 memset(info, 0, sizeof(*info));
642 info->channel = channel;
643 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
646 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
647 struct snd_pcm_channel_info __user * _info)
649 struct snd_pcm_channel_info info;
652 if (copy_from_user(&info, _info, sizeof(info)))
654 res = snd_pcm_channel_info(substream, &info);
657 if (copy_to_user(_info, &info, sizeof(info)))
662 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 if (runtime->trigger_master == NULL)
667 if (runtime->trigger_master == substream) {
668 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
670 snd_pcm_trigger_tstamp(runtime->trigger_master);
671 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
673 runtime->trigger_master = NULL;
677 int (*pre_action)(struct snd_pcm_substream *substream, int state);
678 int (*do_action)(struct snd_pcm_substream *substream, int state);
679 void (*undo_action)(struct snd_pcm_substream *substream, int state);
680 void (*post_action)(struct snd_pcm_substream *substream, int state);
684 * this functions is core for handling of linked stream
685 * Note: the stream state might be changed also on failure
686 * Note2: call with calling stream lock + link lock
688 static int snd_pcm_action_group(struct action_ops *ops,
689 struct snd_pcm_substream *substream,
690 int state, int do_lock)
692 struct snd_pcm_substream *s = NULL;
693 struct snd_pcm_substream *s1;
696 snd_pcm_group_for_each_entry(s, substream) {
697 if (do_lock && s != substream)
698 spin_lock_nested(&s->self_group.lock,
699 SINGLE_DEPTH_NESTING);
700 res = ops->pre_action(s, state);
704 snd_pcm_group_for_each_entry(s, substream) {
705 res = ops->do_action(s, state);
707 if (ops->undo_action) {
708 snd_pcm_group_for_each_entry(s1, substream) {
709 if (s1 == s) /* failed stream */
711 ops->undo_action(s1, state);
714 s = NULL; /* unlock all */
718 snd_pcm_group_for_each_entry(s, substream) {
719 ops->post_action(s, state);
724 snd_pcm_group_for_each_entry(s1, substream) {
726 spin_unlock(&s1->self_group.lock);
727 if (s1 == s) /* end */
735 * Note: call with stream lock
737 static int snd_pcm_action_single(struct action_ops *ops,
738 struct snd_pcm_substream *substream,
743 res = ops->pre_action(substream, state);
746 res = ops->do_action(substream, state);
748 ops->post_action(substream, state);
749 else if (ops->undo_action)
750 ops->undo_action(substream, state);
755 * Note: call with stream lock
757 static int snd_pcm_action(struct action_ops *ops,
758 struct snd_pcm_substream *substream,
763 if (snd_pcm_stream_linked(substream)) {
764 if (!spin_trylock(&substream->group->lock)) {
765 spin_unlock(&substream->self_group.lock);
766 spin_lock(&substream->group->lock);
767 spin_lock(&substream->self_group.lock);
769 res = snd_pcm_action_group(ops, substream, state, 1);
770 spin_unlock(&substream->group->lock);
772 res = snd_pcm_action_single(ops, substream, state);
778 * Note: don't use any locks before
780 static int snd_pcm_action_lock_irq(struct action_ops *ops,
781 struct snd_pcm_substream *substream,
786 read_lock_irq(&snd_pcm_link_rwlock);
787 if (snd_pcm_stream_linked(substream)) {
788 spin_lock(&substream->group->lock);
789 spin_lock(&substream->self_group.lock);
790 res = snd_pcm_action_group(ops, substream, state, 1);
791 spin_unlock(&substream->self_group.lock);
792 spin_unlock(&substream->group->lock);
794 spin_lock(&substream->self_group.lock);
795 res = snd_pcm_action_single(ops, substream, state);
796 spin_unlock(&substream->self_group.lock);
798 read_unlock_irq(&snd_pcm_link_rwlock);
804 static int snd_pcm_action_nonatomic(struct action_ops *ops,
805 struct snd_pcm_substream *substream,
810 down_read(&snd_pcm_link_rwsem);
811 if (snd_pcm_stream_linked(substream))
812 res = snd_pcm_action_group(ops, substream, state, 0);
814 res = snd_pcm_action_single(ops, substream, state);
815 up_read(&snd_pcm_link_rwsem);
822 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
824 struct snd_pcm_runtime *runtime = substream->runtime;
825 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
827 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
828 !snd_pcm_playback_data(substream))
830 runtime->trigger_master = substream;
834 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
836 if (substream->runtime->trigger_master != substream)
838 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
841 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
843 if (substream->runtime->trigger_master == substream)
844 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
847 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
849 struct snd_pcm_runtime *runtime = substream->runtime;
850 snd_pcm_trigger_tstamp(substream);
851 runtime->hw_ptr_jiffies = jiffies;
852 runtime->status->state = state;
853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
854 runtime->silence_size > 0)
855 snd_pcm_playback_silence(substream, ULONG_MAX);
856 if (substream->timer)
857 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
858 &runtime->trigger_tstamp);
861 static struct action_ops snd_pcm_action_start = {
862 .pre_action = snd_pcm_pre_start,
863 .do_action = snd_pcm_do_start,
864 .undo_action = snd_pcm_undo_start,
865 .post_action = snd_pcm_post_start
869 * snd_pcm_start - start all linked streams
870 * @substream: the PCM substream instance
872 int snd_pcm_start(struct snd_pcm_substream *substream)
874 return snd_pcm_action(&snd_pcm_action_start, substream,
875 SNDRV_PCM_STATE_RUNNING);
881 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
886 runtime->trigger_master = substream;
890 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
892 if (substream->runtime->trigger_master == substream &&
893 snd_pcm_running(substream))
894 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
895 return 0; /* unconditonally stop all substreams */
898 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
900 struct snd_pcm_runtime *runtime = substream->runtime;
901 if (runtime->status->state != state) {
902 snd_pcm_trigger_tstamp(substream);
903 if (substream->timer)
904 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
905 &runtime->trigger_tstamp);
906 runtime->status->state = state;
908 wake_up(&runtime->sleep);
911 static struct action_ops snd_pcm_action_stop = {
912 .pre_action = snd_pcm_pre_stop,
913 .do_action = snd_pcm_do_stop,
914 .post_action = snd_pcm_post_stop
918 * snd_pcm_stop - try to stop all running streams in the substream group
919 * @substream: the PCM substream instance
920 * @state: PCM state after stopping the stream
922 * The state of each stream is then changed to the given state unconditionally.
924 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
926 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
929 EXPORT_SYMBOL(snd_pcm_stop);
932 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
933 * @substream: the PCM substream
935 * After stopping, the state is changed to SETUP.
936 * Unlike snd_pcm_stop(), this affects only the given stream.
938 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
940 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
941 SNDRV_PCM_STATE_SETUP);
947 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
949 struct snd_pcm_runtime *runtime = substream->runtime;
950 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
953 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
955 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
957 runtime->trigger_master = substream;
961 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
963 if (substream->runtime->trigger_master != substream)
965 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
966 * a delta betwen the current jiffies, this gives a large enough
967 * delta, effectively to skip the check once.
969 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
970 return substream->ops->trigger(substream,
971 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
972 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
975 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
977 if (substream->runtime->trigger_master == substream)
978 substream->ops->trigger(substream,
979 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
980 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
983 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
985 struct snd_pcm_runtime *runtime = substream->runtime;
986 snd_pcm_trigger_tstamp(substream);
988 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
989 if (substream->timer)
990 snd_timer_notify(substream->timer,
991 SNDRV_TIMER_EVENT_MPAUSE,
992 &runtime->trigger_tstamp);
993 wake_up(&runtime->sleep);
995 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
996 if (substream->timer)
997 snd_timer_notify(substream->timer,
998 SNDRV_TIMER_EVENT_MCONTINUE,
999 &runtime->trigger_tstamp);
1003 static struct action_ops snd_pcm_action_pause = {
1004 .pre_action = snd_pcm_pre_pause,
1005 .do_action = snd_pcm_do_pause,
1006 .undo_action = snd_pcm_undo_pause,
1007 .post_action = snd_pcm_post_pause
1011 * Push/release the pause for all linked streams.
1013 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1015 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1021 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1023 struct snd_pcm_runtime *runtime = substream->runtime;
1024 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1026 runtime->trigger_master = substream;
1030 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1032 struct snd_pcm_runtime *runtime = substream->runtime;
1033 if (runtime->trigger_master != substream)
1035 if (! snd_pcm_running(substream))
1037 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1038 return 0; /* suspend unconditionally */
1041 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1043 struct snd_pcm_runtime *runtime = substream->runtime;
1044 snd_pcm_trigger_tstamp(substream);
1045 if (substream->timer)
1046 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1047 &runtime->trigger_tstamp);
1048 runtime->status->suspended_state = runtime->status->state;
1049 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1050 wake_up(&runtime->sleep);
1053 static struct action_ops snd_pcm_action_suspend = {
1054 .pre_action = snd_pcm_pre_suspend,
1055 .do_action = snd_pcm_do_suspend,
1056 .post_action = snd_pcm_post_suspend
1060 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1061 * @substream: the PCM substream
1063 * After this call, all streams are changed to SUSPENDED state.
1065 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1068 unsigned long flags;
1073 snd_pcm_stream_lock_irqsave(substream, flags);
1074 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1075 snd_pcm_stream_unlock_irqrestore(substream, flags);
1079 EXPORT_SYMBOL(snd_pcm_suspend);
1082 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1083 * @pcm: the PCM instance
1085 * After this call, all streams are changed to SUSPENDED state.
1087 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1089 struct snd_pcm_substream *substream;
1090 int stream, err = 0;
1095 for (stream = 0; stream < 2; stream++) {
1096 for (substream = pcm->streams[stream].substream;
1097 substream; substream = substream->next) {
1098 /* FIXME: the open/close code should lock this as well */
1099 if (substream->runtime == NULL)
1101 err = snd_pcm_suspend(substream);
1102 if (err < 0 && err != -EBUSY)
1109 EXPORT_SYMBOL(snd_pcm_suspend_all);
1113 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1115 struct snd_pcm_runtime *runtime = substream->runtime;
1116 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1118 runtime->trigger_master = substream;
1122 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 if (runtime->trigger_master != substream)
1127 /* DMA not running previously? */
1128 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1129 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1130 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1132 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1135 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1137 if (substream->runtime->trigger_master == substream &&
1138 snd_pcm_running(substream))
1139 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1142 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1144 struct snd_pcm_runtime *runtime = substream->runtime;
1145 snd_pcm_trigger_tstamp(substream);
1146 if (substream->timer)
1147 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1148 &runtime->trigger_tstamp);
1149 runtime->status->state = runtime->status->suspended_state;
1152 static struct action_ops snd_pcm_action_resume = {
1153 .pre_action = snd_pcm_pre_resume,
1154 .do_action = snd_pcm_do_resume,
1155 .undo_action = snd_pcm_undo_resume,
1156 .post_action = snd_pcm_post_resume
1159 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1161 struct snd_card *card = substream->pcm->card;
1164 snd_power_lock(card);
1165 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1166 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1167 snd_power_unlock(card);
1173 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1178 #endif /* CONFIG_PM */
1183 * Change the RUNNING stream(s) to XRUN state.
1185 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1187 struct snd_card *card = substream->pcm->card;
1188 struct snd_pcm_runtime *runtime = substream->runtime;
1191 snd_power_lock(card);
1192 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1193 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1198 snd_pcm_stream_lock_irq(substream);
1199 switch (runtime->status->state) {
1200 case SNDRV_PCM_STATE_XRUN:
1201 result = 0; /* already there */
1203 case SNDRV_PCM_STATE_RUNNING:
1204 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1209 snd_pcm_stream_unlock_irq(substream);
1211 snd_power_unlock(card);
1218 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1220 struct snd_pcm_runtime *runtime = substream->runtime;
1221 switch (runtime->status->state) {
1222 case SNDRV_PCM_STATE_RUNNING:
1223 case SNDRV_PCM_STATE_PREPARED:
1224 case SNDRV_PCM_STATE_PAUSED:
1225 case SNDRV_PCM_STATE_SUSPENDED:
1232 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1234 struct snd_pcm_runtime *runtime = substream->runtime;
1235 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1238 runtime->hw_ptr_base = 0;
1239 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1240 runtime->status->hw_ptr % runtime->period_size;
1241 runtime->silence_start = runtime->status->hw_ptr;
1242 runtime->silence_filled = 0;
1246 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 runtime->control->appl_ptr = runtime->status->hw_ptr;
1250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1251 runtime->silence_size > 0)
1252 snd_pcm_playback_silence(substream, ULONG_MAX);
1255 static struct action_ops snd_pcm_action_reset = {
1256 .pre_action = snd_pcm_pre_reset,
1257 .do_action = snd_pcm_do_reset,
1258 .post_action = snd_pcm_post_reset
1261 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1263 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1269 /* we use the second argument for updating f_flags */
1270 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1273 struct snd_pcm_runtime *runtime = substream->runtime;
1274 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1275 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1277 if (snd_pcm_running(substream))
1279 substream->f_flags = f_flags;
1283 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1286 err = substream->ops->prepare(substream);
1289 return snd_pcm_do_reset(substream, 0);
1292 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1294 struct snd_pcm_runtime *runtime = substream->runtime;
1295 runtime->control->appl_ptr = runtime->status->hw_ptr;
1296 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1299 static struct action_ops snd_pcm_action_prepare = {
1300 .pre_action = snd_pcm_pre_prepare,
1301 .do_action = snd_pcm_do_prepare,
1302 .post_action = snd_pcm_post_prepare
1306 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1307 * @substream: the PCM substream instance
1308 * @file: file to refer f_flags
1310 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1314 struct snd_card *card = substream->pcm->card;
1318 f_flags = file->f_flags;
1320 f_flags = substream->f_flags;
1322 snd_power_lock(card);
1323 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1324 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1325 substream, f_flags);
1326 snd_power_unlock(card);
1334 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1336 if (substream->f_flags & O_NONBLOCK)
1338 substream->runtime->trigger_master = substream;
1342 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1344 struct snd_pcm_runtime *runtime = substream->runtime;
1345 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1346 switch (runtime->status->state) {
1347 case SNDRV_PCM_STATE_PREPARED:
1348 /* start playback stream if possible */
1349 if (! snd_pcm_playback_empty(substream)) {
1350 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1351 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1354 case SNDRV_PCM_STATE_RUNNING:
1355 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1361 /* stop running stream */
1362 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1363 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1364 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1365 snd_pcm_do_stop(substream, new_state);
1366 snd_pcm_post_stop(substream, new_state);
1372 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1376 static struct action_ops snd_pcm_action_drain_init = {
1377 .pre_action = snd_pcm_pre_drain_init,
1378 .do_action = snd_pcm_do_drain_init,
1379 .post_action = snd_pcm_post_drain_init
1383 struct snd_pcm_substream *substream;
1385 snd_pcm_uframes_t stop_threshold;
1388 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1391 * Drain the stream(s).
1392 * When the substream is linked, sync until the draining of all playback streams
1394 * After this call, all streams are supposed to be either SETUP or DRAINING
1395 * (capture only) state.
1397 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1399 struct snd_card *card;
1400 struct snd_pcm_runtime *runtime;
1401 struct snd_pcm_substream *s;
1404 struct drain_rec *drec, drec_tmp, *d;
1406 card = substream->pcm->card;
1407 runtime = substream->runtime;
1409 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1412 snd_power_lock(card);
1413 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1414 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1416 snd_power_unlock(card);
1421 /* allocate temporary record for drain sync */
1422 down_read(&snd_pcm_link_rwsem);
1423 if (snd_pcm_stream_linked(substream)) {
1424 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1426 up_read(&snd_pcm_link_rwsem);
1427 snd_power_unlock(card);
1433 /* count only playback streams */
1435 snd_pcm_group_for_each_entry(s, substream) {
1436 runtime = s->runtime;
1437 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1438 d = &drec[num_drecs++];
1440 init_waitqueue_entry(&d->wait, current);
1441 add_wait_queue(&runtime->sleep, &d->wait);
1442 /* stop_threshold fixup to avoid endless loop when
1443 * stop_threshold > buffer_size
1445 d->stop_threshold = runtime->stop_threshold;
1446 if (runtime->stop_threshold > runtime->buffer_size)
1447 runtime->stop_threshold = runtime->buffer_size;
1450 up_read(&snd_pcm_link_rwsem);
1452 snd_pcm_stream_lock_irq(substream);
1454 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1455 snd_pcm_pause(substream, 0);
1457 /* pre-start/stop - all running streams are changed to DRAINING state */
1458 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1460 snd_pcm_stream_unlock_irq(substream);
1466 if (signal_pending(current)) {
1467 result = -ERESTARTSYS;
1471 for (i = 0; i < num_drecs; i++) {
1472 runtime = drec[i].substream->runtime;
1473 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1477 break; /* yes, all drained */
1479 set_current_state(TASK_INTERRUPTIBLE);
1480 snd_pcm_stream_unlock_irq(substream);
1481 snd_power_unlock(card);
1482 tout = schedule_timeout(10 * HZ);
1483 snd_power_lock(card);
1484 snd_pcm_stream_lock_irq(substream);
1486 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1489 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1490 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1497 snd_pcm_stream_unlock_irq(substream);
1500 for (i = 0; i < num_drecs; i++) {
1502 runtime = d->substream->runtime;
1503 remove_wait_queue(&runtime->sleep, &d->wait);
1504 runtime->stop_threshold = d->stop_threshold;
1507 if (drec != &drec_tmp)
1509 snd_power_unlock(card);
1517 * Immediately put all linked substreams into SETUP state.
1519 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1521 struct snd_pcm_runtime *runtime;
1522 struct snd_card *card;
1525 if (PCM_RUNTIME_CHECK(substream))
1527 runtime = substream->runtime;
1528 card = substream->pcm->card;
1530 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1531 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1532 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1535 snd_pcm_stream_lock_irq(substream);
1537 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1538 snd_pcm_pause(substream, 0);
1540 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1541 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1542 snd_pcm_stream_unlock_irq(substream);
1548 /* WARNING: Don't forget to fput back the file */
1549 static struct file *snd_pcm_file_fd(int fd)
1552 struct inode *inode;
1558 inode = file->f_path.dentry->d_inode;
1559 if (!S_ISCHR(inode->i_mode) ||
1560 imajor(inode) != snd_major) {
1564 minor = iminor(inode);
1565 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1566 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1576 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1580 struct snd_pcm_file *pcm_file;
1581 struct snd_pcm_substream *substream1;
1583 file = snd_pcm_file_fd(fd);
1586 pcm_file = file->private_data;
1587 substream1 = pcm_file->substream;
1588 down_write(&snd_pcm_link_rwsem);
1589 write_lock_irq(&snd_pcm_link_rwlock);
1590 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1591 substream->runtime->status->state != substream1->runtime->status->state) {
1595 if (snd_pcm_stream_linked(substream1)) {
1599 if (!snd_pcm_stream_linked(substream)) {
1600 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1601 if (substream->group == NULL) {
1605 spin_lock_init(&substream->group->lock);
1606 INIT_LIST_HEAD(&substream->group->substreams);
1607 list_add_tail(&substream->link_list, &substream->group->substreams);
1608 substream->group->count = 1;
1610 list_add_tail(&substream1->link_list, &substream->group->substreams);
1611 substream->group->count++;
1612 substream1->group = substream->group;
1614 write_unlock_irq(&snd_pcm_link_rwlock);
1615 up_write(&snd_pcm_link_rwsem);
1620 static void relink_to_local(struct snd_pcm_substream *substream)
1622 substream->group = &substream->self_group;
1623 INIT_LIST_HEAD(&substream->self_group.substreams);
1624 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1627 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1629 struct snd_pcm_substream *s;
1632 down_write(&snd_pcm_link_rwsem);
1633 write_lock_irq(&snd_pcm_link_rwlock);
1634 if (!snd_pcm_stream_linked(substream)) {
1638 list_del(&substream->link_list);
1639 substream->group->count--;
1640 if (substream->group->count == 1) { /* detach the last stream, too */
1641 snd_pcm_group_for_each_entry(s, substream) {
1645 kfree(substream->group);
1647 relink_to_local(substream);
1649 write_unlock_irq(&snd_pcm_link_rwlock);
1650 up_write(&snd_pcm_link_rwsem);
1657 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1658 struct snd_pcm_hw_rule *rule)
1660 struct snd_interval t;
1661 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1662 hw_param_interval_c(params, rule->deps[1]), &t);
1663 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1666 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1667 struct snd_pcm_hw_rule *rule)
1669 struct snd_interval t;
1670 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1671 hw_param_interval_c(params, rule->deps[1]), &t);
1672 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1675 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1676 struct snd_pcm_hw_rule *rule)
1678 struct snd_interval t;
1679 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1680 hw_param_interval_c(params, rule->deps[1]),
1681 (unsigned long) rule->private, &t);
1682 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1685 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1686 struct snd_pcm_hw_rule *rule)
1688 struct snd_interval t;
1689 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1690 (unsigned long) rule->private,
1691 hw_param_interval_c(params, rule->deps[1]), &t);
1692 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1695 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1696 struct snd_pcm_hw_rule *rule)
1699 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1701 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1703 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1705 if (! snd_mask_test(mask, k))
1707 bits = snd_pcm_format_physical_width(k);
1709 continue; /* ignore invalid formats */
1710 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1711 snd_mask_reset(&m, k);
1713 return snd_mask_refine(mask, &m);
1716 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1717 struct snd_pcm_hw_rule *rule)
1719 struct snd_interval t;
1725 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1727 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1729 bits = snd_pcm_format_physical_width(k);
1731 continue; /* ignore invalid formats */
1732 if (t.min > (unsigned)bits)
1734 if (t.max < (unsigned)bits)
1738 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1741 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1742 #error "Change this table"
1745 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1746 48000, 64000, 88200, 96000, 176400, 192000 };
1748 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1749 .count = ARRAY_SIZE(rates),
1753 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
1756 struct snd_pcm_hardware *hw = rule->private;
1757 return snd_interval_list(hw_param_interval(params, rule->var),
1758 snd_pcm_known_rates.count,
1759 snd_pcm_known_rates.list, hw->rates);
1762 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1763 struct snd_pcm_hw_rule *rule)
1765 struct snd_interval t;
1766 struct snd_pcm_substream *substream = rule->private;
1768 t.max = substream->buffer_bytes_max;
1772 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1775 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1777 struct snd_pcm_runtime *runtime = substream->runtime;
1778 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1781 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1782 snd_mask_any(constrs_mask(constrs, k));
1785 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1786 snd_interval_any(constrs_interval(constrs, k));
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1796 snd_pcm_hw_rule_format, NULL,
1797 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1800 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1801 snd_pcm_hw_rule_sample_bits, NULL,
1802 SNDRV_PCM_HW_PARAM_FORMAT,
1803 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1807 snd_pcm_hw_rule_div, NULL,
1808 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1811 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1812 snd_pcm_hw_rule_mul, NULL,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1817 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1818 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1822 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1823 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1827 snd_pcm_hw_rule_div, NULL,
1828 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1833 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1837 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1838 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1853 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1857 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1862 snd_pcm_hw_rule_mul, NULL,
1863 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1872 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1877 snd_pcm_hw_rule_muldivk, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1882 snd_pcm_hw_rule_muldivk, (void*) 8,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1899 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1901 struct snd_pcm_runtime *runtime = substream->runtime;
1902 struct snd_pcm_hardware *hw = &runtime->hw;
1904 unsigned int mask = 0;
1906 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1911 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1912 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1913 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1914 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1915 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1916 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1918 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1922 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1926 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1931 hw->channels_min, hw->channels_max);
1935 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1936 hw->rate_min, hw->rate_max);
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1941 hw->period_bytes_min, hw->period_bytes_max);
1945 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1946 hw->periods_min, hw->periods_max);
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1951 hw->period_bytes_min, hw->buffer_bytes_max);
1955 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1956 snd_pcm_hw_rule_buffer_bytes_max, substream,
1957 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1962 if (runtime->dma_bytes) {
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1968 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1969 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1970 snd_pcm_hw_rule_rate, hw,
1971 SNDRV_PCM_HW_PARAM_RATE, -1);
1976 /* FIXME: this belong to lowlevel */
1977 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1982 static void pcm_release_private(struct snd_pcm_substream *substream)
1984 snd_pcm_unlink(substream);
1987 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1989 substream->ref_count--;
1990 if (substream->ref_count > 0)
1993 snd_pcm_drop(substream);
1994 if (substream->hw_opened) {
1995 if (substream->ops->hw_free != NULL)
1996 substream->ops->hw_free(substream);
1997 substream->ops->close(substream);
1998 substream->hw_opened = 0;
2000 if (substream->pcm_release) {
2001 substream->pcm_release(substream);
2002 substream->pcm_release = NULL;
2004 snd_pcm_detach_substream(substream);
2007 EXPORT_SYMBOL(snd_pcm_release_substream);
2009 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2011 struct snd_pcm_substream **rsubstream)
2013 struct snd_pcm_substream *substream;
2016 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2019 if (substream->ref_count > 1) {
2020 *rsubstream = substream;
2024 err = snd_pcm_hw_constraints_init(substream);
2026 snd_printd("snd_pcm_hw_constraints_init failed\n");
2030 if ((err = substream->ops->open(substream)) < 0)
2033 substream->hw_opened = 1;
2035 err = snd_pcm_hw_constraints_complete(substream);
2037 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2041 *rsubstream = substream;
2045 snd_pcm_release_substream(substream);
2049 EXPORT_SYMBOL(snd_pcm_open_substream);
2051 static int snd_pcm_open_file(struct file *file,
2052 struct snd_pcm *pcm,
2054 struct snd_pcm_file **rpcm_file)
2056 struct snd_pcm_file *pcm_file;
2057 struct snd_pcm_substream *substream;
2058 struct snd_pcm_str *str;
2064 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2068 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2069 if (pcm_file == NULL) {
2070 snd_pcm_release_substream(substream);
2073 pcm_file->substream = substream;
2074 if (substream->ref_count == 1) {
2075 str = substream->pstr;
2076 substream->file = pcm_file;
2077 substream->pcm_release = pcm_release_private;
2079 file->private_data = pcm_file;
2081 *rpcm_file = pcm_file;
2085 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2087 struct snd_pcm *pcm;
2089 pcm = snd_lookup_minor_data(iminor(inode),
2090 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2091 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2094 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2096 struct snd_pcm *pcm;
2098 pcm = snd_lookup_minor_data(iminor(inode),
2099 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2100 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2103 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2106 struct snd_pcm_file *pcm_file;
2113 err = snd_card_file_add(pcm->card, file);
2116 if (!try_module_get(pcm->card->module)) {
2120 init_waitqueue_entry(&wait, current);
2121 add_wait_queue(&pcm->open_wait, &wait);
2122 mutex_lock(&pcm->open_mutex);
2124 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2127 if (err == -EAGAIN) {
2128 if (file->f_flags & O_NONBLOCK) {
2134 set_current_state(TASK_INTERRUPTIBLE);
2135 mutex_unlock(&pcm->open_mutex);
2137 mutex_lock(&pcm->open_mutex);
2138 if (signal_pending(current)) {
2143 remove_wait_queue(&pcm->open_wait, &wait);
2144 mutex_unlock(&pcm->open_mutex);
2150 module_put(pcm->card->module);
2152 snd_card_file_remove(pcm->card, file);
2157 static int snd_pcm_release(struct inode *inode, struct file *file)
2159 struct snd_pcm *pcm;
2160 struct snd_pcm_substream *substream;
2161 struct snd_pcm_file *pcm_file;
2163 pcm_file = file->private_data;
2164 substream = pcm_file->substream;
2165 if (snd_BUG_ON(!substream))
2167 pcm = substream->pcm;
2168 mutex_lock(&pcm->open_mutex);
2169 snd_pcm_release_substream(substream);
2171 mutex_unlock(&pcm->open_mutex);
2172 wake_up(&pcm->open_wait);
2173 module_put(pcm->card->module);
2174 snd_card_file_remove(pcm->card, file);
2178 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2179 snd_pcm_uframes_t frames)
2181 struct snd_pcm_runtime *runtime = substream->runtime;
2182 snd_pcm_sframes_t appl_ptr;
2183 snd_pcm_sframes_t ret;
2184 snd_pcm_sframes_t hw_avail;
2189 snd_pcm_stream_lock_irq(substream);
2190 switch (runtime->status->state) {
2191 case SNDRV_PCM_STATE_PREPARED:
2193 case SNDRV_PCM_STATE_DRAINING:
2194 case SNDRV_PCM_STATE_RUNNING:
2195 if (snd_pcm_update_hw_ptr(substream) >= 0)
2198 case SNDRV_PCM_STATE_XRUN:
2206 hw_avail = snd_pcm_playback_hw_avail(runtime);
2207 if (hw_avail <= 0) {
2211 if (frames > (snd_pcm_uframes_t)hw_avail)
2213 appl_ptr = runtime->control->appl_ptr - frames;
2215 appl_ptr += runtime->boundary;
2216 runtime->control->appl_ptr = appl_ptr;
2219 snd_pcm_stream_unlock_irq(substream);
2223 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2224 snd_pcm_uframes_t frames)
2226 struct snd_pcm_runtime *runtime = substream->runtime;
2227 snd_pcm_sframes_t appl_ptr;
2228 snd_pcm_sframes_t ret;
2229 snd_pcm_sframes_t hw_avail;
2234 snd_pcm_stream_lock_irq(substream);
2235 switch (runtime->status->state) {
2236 case SNDRV_PCM_STATE_PREPARED:
2237 case SNDRV_PCM_STATE_DRAINING:
2239 case SNDRV_PCM_STATE_RUNNING:
2240 if (snd_pcm_update_hw_ptr(substream) >= 0)
2243 case SNDRV_PCM_STATE_XRUN:
2251 hw_avail = snd_pcm_capture_hw_avail(runtime);
2252 if (hw_avail <= 0) {
2256 if (frames > (snd_pcm_uframes_t)hw_avail)
2258 appl_ptr = runtime->control->appl_ptr - frames;
2260 appl_ptr += runtime->boundary;
2261 runtime->control->appl_ptr = appl_ptr;
2264 snd_pcm_stream_unlock_irq(substream);
2268 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2269 snd_pcm_uframes_t frames)
2271 struct snd_pcm_runtime *runtime = substream->runtime;
2272 snd_pcm_sframes_t appl_ptr;
2273 snd_pcm_sframes_t ret;
2274 snd_pcm_sframes_t avail;
2279 snd_pcm_stream_lock_irq(substream);
2280 switch (runtime->status->state) {
2281 case SNDRV_PCM_STATE_PREPARED:
2282 case SNDRV_PCM_STATE_PAUSED:
2284 case SNDRV_PCM_STATE_DRAINING:
2285 case SNDRV_PCM_STATE_RUNNING:
2286 if (snd_pcm_update_hw_ptr(substream) >= 0)
2289 case SNDRV_PCM_STATE_XRUN:
2297 avail = snd_pcm_playback_avail(runtime);
2302 if (frames > (snd_pcm_uframes_t)avail)
2304 appl_ptr = runtime->control->appl_ptr + frames;
2305 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2306 appl_ptr -= runtime->boundary;
2307 runtime->control->appl_ptr = appl_ptr;
2310 snd_pcm_stream_unlock_irq(substream);
2314 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2315 snd_pcm_uframes_t frames)
2317 struct snd_pcm_runtime *runtime = substream->runtime;
2318 snd_pcm_sframes_t appl_ptr;
2319 snd_pcm_sframes_t ret;
2320 snd_pcm_sframes_t avail;
2325 snd_pcm_stream_lock_irq(substream);
2326 switch (runtime->status->state) {
2327 case SNDRV_PCM_STATE_PREPARED:
2328 case SNDRV_PCM_STATE_DRAINING:
2329 case SNDRV_PCM_STATE_PAUSED:
2331 case SNDRV_PCM_STATE_RUNNING:
2332 if (snd_pcm_update_hw_ptr(substream) >= 0)
2335 case SNDRV_PCM_STATE_XRUN:
2343 avail = snd_pcm_capture_avail(runtime);
2348 if (frames > (snd_pcm_uframes_t)avail)
2350 appl_ptr = runtime->control->appl_ptr + frames;
2351 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2352 appl_ptr -= runtime->boundary;
2353 runtime->control->appl_ptr = appl_ptr;
2356 snd_pcm_stream_unlock_irq(substream);
2360 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2362 struct snd_pcm_runtime *runtime = substream->runtime;
2365 snd_pcm_stream_lock_irq(substream);
2366 switch (runtime->status->state) {
2367 case SNDRV_PCM_STATE_DRAINING:
2368 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2370 case SNDRV_PCM_STATE_RUNNING:
2371 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2374 case SNDRV_PCM_STATE_PREPARED:
2375 case SNDRV_PCM_STATE_SUSPENDED:
2378 case SNDRV_PCM_STATE_XRUN:
2386 snd_pcm_stream_unlock_irq(substream);
2390 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2391 snd_pcm_sframes_t __user *res)
2393 struct snd_pcm_runtime *runtime = substream->runtime;
2395 snd_pcm_sframes_t n = 0;
2397 snd_pcm_stream_lock_irq(substream);
2398 switch (runtime->status->state) {
2399 case SNDRV_PCM_STATE_DRAINING:
2400 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2402 case SNDRV_PCM_STATE_RUNNING:
2403 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2406 case SNDRV_PCM_STATE_PREPARED:
2407 case SNDRV_PCM_STATE_SUSPENDED:
2409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2410 n = snd_pcm_playback_hw_avail(runtime);
2412 n = snd_pcm_capture_avail(runtime);
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);
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 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 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 */
3069 * fault callback for mmapping a RAM page
3071 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3072 struct vm_fault *vmf)
3074 struct snd_pcm_substream *substream = area->vm_private_data;
3075 struct snd_pcm_runtime *runtime;
3076 unsigned long offset;
3081 if (substream == NULL)
3082 return VM_FAULT_SIGBUS;
3083 runtime = substream->runtime;
3084 offset = vmf->pgoff << PAGE_SHIFT;
3085 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3086 if (offset > dma_bytes - PAGE_SIZE)
3087 return VM_FAULT_SIGBUS;
3088 if (substream->ops->page) {
3089 page = substream->ops->page(substream, offset);
3091 return VM_FAULT_SIGBUS;
3093 vaddr = runtime->dma_area + offset;
3094 page = virt_to_page(vaddr);
3101 static struct vm_operations_struct snd_pcm_vm_ops_data =
3103 .open = snd_pcm_mmap_data_open,
3104 .close = snd_pcm_mmap_data_close,
3105 .fault = snd_pcm_mmap_data_fault,
3109 * mmap the DMA buffer on RAM
3111 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3112 struct vm_area_struct *area)
3114 area->vm_ops = &snd_pcm_vm_ops_data;
3115 area->vm_private_data = substream;
3116 area->vm_flags |= VM_RESERVED;
3117 atomic_inc(&substream->mmap_count);
3122 * mmap the DMA buffer on I/O memory area
3124 #if SNDRV_PCM_INFO_MMAP_IOMEM
3125 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3127 .open = snd_pcm_mmap_data_open,
3128 .close = snd_pcm_mmap_data_close,
3131 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3132 struct vm_area_struct *area)
3135 unsigned long offset;
3137 #ifdef pgprot_noncached
3138 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3140 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3141 area->vm_private_data = substream;
3142 area->vm_flags |= VM_IO;
3143 size = area->vm_end - area->vm_start;
3144 offset = area->vm_pgoff << PAGE_SHIFT;
3145 if (io_remap_pfn_range(area, area->vm_start,
3146 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3147 size, area->vm_page_prot))
3149 atomic_inc(&substream->mmap_count);
3153 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3154 #endif /* SNDRV_PCM_INFO_MMAP */
3159 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3160 struct vm_area_struct *area)
3162 struct snd_pcm_runtime *runtime;
3164 unsigned long offset;
3167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3168 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3171 if (!(area->vm_flags & VM_READ))
3174 runtime = substream->runtime;
3175 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3177 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3179 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3180 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3182 size = area->vm_end - area->vm_start;
3183 offset = area->vm_pgoff << PAGE_SHIFT;
3184 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3185 if ((size_t)size > dma_bytes)
3187 if (offset > dma_bytes - size)
3190 if (substream->ops->mmap)
3191 return substream->ops->mmap(substream, area);
3193 return snd_pcm_default_mmap(substream, area);
3196 EXPORT_SYMBOL(snd_pcm_mmap_data);
3198 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3200 struct snd_pcm_file * pcm_file;
3201 struct snd_pcm_substream *substream;
3202 unsigned long offset;
3204 pcm_file = file->private_data;
3205 substream = pcm_file->substream;
3206 if (PCM_RUNTIME_CHECK(substream))
3209 offset = area->vm_pgoff << PAGE_SHIFT;
3211 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3212 if (pcm_file->no_compat_mmap)
3214 return snd_pcm_mmap_status(substream, file, area);
3215 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3216 if (pcm_file->no_compat_mmap)
3218 return snd_pcm_mmap_control(substream, file, area);
3220 return snd_pcm_mmap_data(substream, file, area);
3225 static int snd_pcm_fasync(int fd, struct file * file, int on)
3227 struct snd_pcm_file * pcm_file;
3228 struct snd_pcm_substream *substream;
3229 struct snd_pcm_runtime *runtime;
3233 pcm_file = file->private_data;
3234 substream = pcm_file->substream;
3235 if (PCM_RUNTIME_CHECK(substream))
3237 runtime = substream->runtime;
3238 err = fasync_helper(fd, file, on, &runtime->fasync);
3247 #ifdef CONFIG_COMPAT
3248 #include "pcm_compat.c"
3250 #define snd_pcm_ioctl_compat NULL
3254 * To be removed helpers to keep binary compatibility
3257 #ifdef CONFIG_SND_SUPPORT_OLD_API
3258 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3259 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3261 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3262 struct snd_pcm_hw_params_old *oparams)
3266 memset(params, 0, sizeof(*params));
3267 params->flags = oparams->flags;
3268 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3269 params->masks[i].bits[0] = oparams->masks[i];
3270 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3271 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3272 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3273 params->info = oparams->info;
3274 params->msbits = oparams->msbits;
3275 params->rate_num = oparams->rate_num;
3276 params->rate_den = oparams->rate_den;
3277 params->fifo_size = oparams->fifo_size;
3280 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3281 struct snd_pcm_hw_params *params)
3285 memset(oparams, 0, sizeof(*oparams));
3286 oparams->flags = params->flags;
3287 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3288 oparams->masks[i] = params->masks[i].bits[0];
3289 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3290 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3291 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3292 oparams->info = params->info;
3293 oparams->msbits = params->msbits;
3294 oparams->rate_num = params->rate_num;
3295 oparams->rate_den = params->rate_den;
3296 oparams->fifo_size = params->fifo_size;
3299 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3300 struct snd_pcm_hw_params_old __user * _oparams)
3302 struct snd_pcm_hw_params *params;
3303 struct snd_pcm_hw_params_old *oparams = NULL;
3306 params = kmalloc(sizeof(*params), GFP_KERNEL);
3310 oparams = memdup_user(_oparams, sizeof(*oparams));
3311 if (IS_ERR(oparams)) {
3312 err = PTR_ERR(oparams);
3315 snd_pcm_hw_convert_from_old_params(params, oparams);
3316 err = snd_pcm_hw_refine(substream, params);
3317 snd_pcm_hw_convert_to_old_params(oparams, params);
3318 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3329 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3330 struct snd_pcm_hw_params_old __user * _oparams)
3332 struct snd_pcm_hw_params *params;
3333 struct snd_pcm_hw_params_old *oparams = NULL;
3336 params = kmalloc(sizeof(*params), GFP_KERNEL);
3340 oparams = memdup_user(_oparams, sizeof(*oparams));
3341 if (IS_ERR(oparams)) {
3342 err = PTR_ERR(oparams);
3345 snd_pcm_hw_convert_from_old_params(params, oparams);
3346 err = snd_pcm_hw_params(substream, params);
3347 snd_pcm_hw_convert_to_old_params(oparams, params);
3348 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3358 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3361 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3362 unsigned long len, unsigned long pgoff,
3363 unsigned long flags)
3368 # define dummy_get_unmapped_area NULL
3375 const struct file_operations snd_pcm_f_ops[2] = {
3377 .owner = THIS_MODULE,
3378 .write = snd_pcm_write,
3379 .aio_write = snd_pcm_aio_write,
3380 .open = snd_pcm_playback_open,
3381 .release = snd_pcm_release,
3382 .poll = snd_pcm_playback_poll,
3383 .unlocked_ioctl = snd_pcm_playback_ioctl,
3384 .compat_ioctl = snd_pcm_ioctl_compat,
3385 .mmap = snd_pcm_mmap,
3386 .fasync = snd_pcm_fasync,
3387 .get_unmapped_area = dummy_get_unmapped_area,
3390 .owner = THIS_MODULE,
3391 .read = snd_pcm_read,
3392 .aio_read = snd_pcm_aio_read,
3393 .open = snd_pcm_capture_open,
3394 .release = snd_pcm_release,
3395 .poll = snd_pcm_capture_poll,
3396 .unlocked_ioctl = snd_pcm_capture_ioctl,
3397 .compat_ioctl = snd_pcm_ioctl_compat,
3398 .mmap = snd_pcm_mmap,
3399 .fasync = snd_pcm_fasync,
3400 .get_unmapped_area = dummy_get_unmapped_area,