2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <linux/pm_qos_params.h>
27 #include <linux/uio.h>
28 #include <sound/core.h>
29 #include <sound/control.h>
30 #include <sound/info.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/timer.h>
34 #include <sound/minors.h>
41 struct snd_pcm_hw_params_old {
43 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
44 SNDRV_PCM_HW_PARAM_ACCESS + 1];
45 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
46 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
51 unsigned int rate_num;
52 unsigned int rate_den;
53 snd_pcm_uframes_t fifo_size;
54 unsigned char reserved[64];
57 #ifdef CONFIG_SND_SUPPORT_OLD_API
58 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
59 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
61 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params_old __user * _oparams);
63 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
72 DEFINE_RWLOCK(snd_pcm_link_rwlock);
73 EXPORT_SYMBOL(snd_pcm_link_rwlock);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 static inline mm_segment_t snd_enter_user(void)
79 mm_segment_t fs = get_fs();
84 static inline void snd_leave_user(mm_segment_t fs)
91 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
93 struct snd_pcm_runtime *runtime;
94 struct snd_pcm *pcm = substream->pcm;
95 struct snd_pcm_str *pstr = substream->pstr;
97 snd_assert(substream != NULL, return -ENXIO);
98 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
122 struct snd_pcm_info *info;
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
128 err = snd_pcm_info(substream, info);
130 if (copy_to_user(_info, info, sizeof(*info)))
140 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141 char *snd_pcm_hw_param_names[] = {
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
160 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
186 if (!(params->rmask & (1 << k)))
189 printk("%s = ", snd_pcm_hw_param_names[k]);
190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197 params->cmask |= 1 << k;
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
206 if (!(params->rmask & (1 << k)))
209 printk("%s = ", snd_pcm_hw_param_names[k]);
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
228 params->cmask |= 1 << k;
233 for (k = 0; k < constrs->rules_num; k++)
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
239 for (k = 0; k < constrs->rules_num; k++) {
240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
243 if (r->cond && !(r->cond & params->flags))
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
254 printk("Rule %d [%p]: ", k, r->func);
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
261 i = hw_param_interval(params, r->var);
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
271 changed = r->func(params, r);
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
313 hw = &substream->runtime->hw;
315 params->info = hw->info;
316 if (!params->fifo_size)
317 params->fifo_size = hw->fifo_size;
322 EXPORT_SYMBOL(snd_pcm_hw_refine);
324 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
325 struct snd_pcm_hw_params __user * _params)
327 struct snd_pcm_hw_params *params;
330 params = kmalloc(sizeof(*params), GFP_KERNEL);
335 if (copy_from_user(params, _params, sizeof(*params))) {
339 err = snd_pcm_hw_refine(substream, params);
340 if (copy_to_user(_params, params, sizeof(*params))) {
349 static int period_to_usecs(struct snd_pcm_runtime *runtime)
354 return -1; /* invalid */
356 /* take 75% of period time as the deadline */
357 usecs = (750000 / runtime->rate) * runtime->period_size;
358 usecs += ((750000 % runtime->rate) * runtime->period_size) /
364 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
365 struct snd_pcm_hw_params *params)
367 struct snd_pcm_runtime *runtime;
370 snd_pcm_uframes_t frames;
372 snd_assert(substream != NULL, return -ENXIO);
373 runtime = substream->runtime;
374 snd_assert(runtime != NULL, return -ENXIO);
375 snd_pcm_stream_lock_irq(substream);
376 switch (runtime->status->state) {
377 case SNDRV_PCM_STATE_OPEN:
378 case SNDRV_PCM_STATE_SETUP:
379 case SNDRV_PCM_STATE_PREPARED:
382 snd_pcm_stream_unlock_irq(substream);
385 snd_pcm_stream_unlock_irq(substream);
386 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
387 if (!substream->oss.oss)
389 if (atomic_read(&substream->mmap_count))
393 err = snd_pcm_hw_refine(substream, params);
397 err = snd_pcm_hw_params_choose(substream, params);
401 if (substream->ops->hw_params != NULL) {
402 err = substream->ops->hw_params(substream, params);
407 runtime->access = params_access(params);
408 runtime->format = params_format(params);
409 runtime->subformat = params_subformat(params);
410 runtime->channels = params_channels(params);
411 runtime->rate = params_rate(params);
412 runtime->period_size = params_period_size(params);
413 runtime->periods = params_periods(params);
414 runtime->buffer_size = params_buffer_size(params);
415 runtime->info = params->info;
416 runtime->rate_num = params->rate_num;
417 runtime->rate_den = params->rate_den;
419 bits = snd_pcm_format_physical_width(runtime->format);
420 runtime->sample_bits = bits;
421 bits *= runtime->channels;
422 runtime->frame_bits = bits;
424 while (bits % 8 != 0) {
428 runtime->byte_align = bits / 8;
429 runtime->min_align = frames;
431 /* Default sw params */
432 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
433 runtime->period_step = 1;
434 runtime->control->avail_min = runtime->period_size;
435 runtime->start_threshold = 1;
436 runtime->stop_threshold = runtime->buffer_size;
437 runtime->silence_threshold = 0;
438 runtime->silence_size = 0;
439 runtime->boundary = runtime->buffer_size;
440 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
441 runtime->boundary *= 2;
443 snd_pcm_timer_resolution_change(substream);
444 runtime->status->state = SNDRV_PCM_STATE_SETUP;
446 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
447 substream->latency_id);
448 if ((usecs = period_to_usecs(runtime)) >= 0)
449 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
450 substream->latency_id, usecs);
453 /* hardware might be unuseable from this time,
454 so we force application to retry to set
455 the correct hardware parameter settings */
456 runtime->status->state = SNDRV_PCM_STATE_OPEN;
457 if (substream->ops->hw_free != NULL)
458 substream->ops->hw_free(substream);
462 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params __user * _params)
465 struct snd_pcm_hw_params *params;
468 params = kmalloc(sizeof(*params), GFP_KERNEL);
473 if (copy_from_user(params, _params, sizeof(*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 snd_assert(substream != NULL, return -ENXIO);
493 runtime = substream->runtime;
494 snd_assert(runtime != NULL, return -ENXIO);
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;
520 snd_assert(substream != NULL, return -ENXIO);
521 runtime = substream->runtime;
522 snd_assert(runtime != NULL, return -ENXIO);
523 snd_pcm_stream_lock_irq(substream);
524 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
525 snd_pcm_stream_unlock_irq(substream);
528 snd_pcm_stream_unlock_irq(substream);
530 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
532 if (params->avail_min == 0)
534 if (params->silence_size >= runtime->boundary) {
535 if (params->silence_threshold != 0)
538 if (params->silence_size > params->silence_threshold)
540 if (params->silence_threshold > runtime->buffer_size)
543 snd_pcm_stream_lock_irq(substream);
544 runtime->tstamp_mode = params->tstamp_mode;
545 runtime->period_step = params->period_step;
546 runtime->control->avail_min = params->avail_min;
547 runtime->start_threshold = params->start_threshold;
548 runtime->stop_threshold = params->stop_threshold;
549 runtime->silence_threshold = params->silence_threshold;
550 runtime->silence_size = params->silence_size;
551 params->boundary = runtime->boundary;
552 if (snd_pcm_running(substream)) {
553 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
554 runtime->silence_size > 0)
555 snd_pcm_playback_silence(substream, ULONG_MAX);
556 wake_up(&runtime->sleep);
558 snd_pcm_stream_unlock_irq(substream);
562 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
563 struct snd_pcm_sw_params __user * _params)
565 struct snd_pcm_sw_params params;
567 if (copy_from_user(¶ms, _params, sizeof(params)))
569 err = snd_pcm_sw_params(substream, ¶ms);
570 if (copy_to_user(_params, ¶ms, sizeof(params)))
575 int snd_pcm_status(struct snd_pcm_substream *substream,
576 struct snd_pcm_status *status)
578 struct snd_pcm_runtime *runtime = substream->runtime;
580 snd_pcm_stream_lock_irq(substream);
581 status->state = runtime->status->state;
582 status->suspended_state = runtime->status->suspended_state;
583 if (status->state == SNDRV_PCM_STATE_OPEN)
585 status->trigger_tstamp = runtime->trigger_tstamp;
586 if (snd_pcm_running(substream)) {
587 snd_pcm_update_hw_ptr(substream);
588 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
589 status->tstamp = runtime->status->tstamp;
593 snd_pcm_gettime(runtime, &status->tstamp);
595 status->appl_ptr = runtime->control->appl_ptr;
596 status->hw_ptr = runtime->status->hw_ptr;
597 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
598 status->avail = snd_pcm_playback_avail(runtime);
599 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
600 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
601 status->delay = runtime->buffer_size - status->avail;
605 status->avail = snd_pcm_capture_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
607 status->delay = status->avail;
611 status->avail_max = runtime->avail_max;
612 status->overrange = runtime->overrange;
613 runtime->avail_max = 0;
614 runtime->overrange = 0;
616 snd_pcm_stream_unlock_irq(substream);
620 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
621 struct snd_pcm_status __user * _status)
623 struct snd_pcm_status status;
624 struct snd_pcm_runtime *runtime;
627 snd_assert(substream != NULL, return -ENXIO);
628 runtime = substream->runtime;
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 snd_assert(substream != NULL, return -ENXIO);
645 channel = info->channel;
646 runtime = substream->runtime;
647 snd_pcm_stream_lock_irq(substream);
648 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
649 snd_pcm_stream_unlock_irq(substream);
652 snd_pcm_stream_unlock_irq(substream);
653 if (channel >= runtime->channels)
655 memset(info, 0, sizeof(*info));
656 info->channel = channel;
657 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
660 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
661 struct snd_pcm_channel_info __user * _info)
663 struct snd_pcm_channel_info info;
666 if (copy_from_user(&info, _info, sizeof(info)))
668 res = snd_pcm_channel_info(substream, &info);
671 if (copy_to_user(_info, &info, sizeof(info)))
676 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
678 struct snd_pcm_runtime *runtime = substream->runtime;
679 if (runtime->trigger_master == NULL)
681 if (runtime->trigger_master == substream) {
682 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
684 snd_pcm_trigger_tstamp(runtime->trigger_master);
685 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
687 runtime->trigger_master = NULL;
691 int (*pre_action)(struct snd_pcm_substream *substream, int state);
692 int (*do_action)(struct snd_pcm_substream *substream, int state);
693 void (*undo_action)(struct snd_pcm_substream *substream, int state);
694 void (*post_action)(struct snd_pcm_substream *substream, int state);
698 * this functions is core for handling of linked stream
699 * Note: the stream state might be changed also on failure
700 * Note2: call with calling stream lock + link lock
702 static int snd_pcm_action_group(struct action_ops *ops,
703 struct snd_pcm_substream *substream,
704 int state, int do_lock)
706 struct snd_pcm_substream *s = NULL;
707 struct snd_pcm_substream *s1;
710 snd_pcm_group_for_each_entry(s, substream) {
711 if (do_lock && s != substream)
712 spin_lock_nested(&s->self_group.lock,
713 SINGLE_DEPTH_NESTING);
714 res = ops->pre_action(s, state);
718 snd_pcm_group_for_each_entry(s, substream) {
719 res = ops->do_action(s, state);
721 if (ops->undo_action) {
722 snd_pcm_group_for_each_entry(s1, substream) {
723 if (s1 == s) /* failed stream */
725 ops->undo_action(s1, state);
728 s = NULL; /* unlock all */
732 snd_pcm_group_for_each_entry(s, substream) {
733 ops->post_action(s, state);
738 snd_pcm_group_for_each_entry(s1, substream) {
740 spin_unlock(&s1->self_group.lock);
741 if (s1 == s) /* end */
749 * Note: call with stream lock
751 static int snd_pcm_action_single(struct action_ops *ops,
752 struct snd_pcm_substream *substream,
757 res = ops->pre_action(substream, state);
760 res = ops->do_action(substream, state);
762 ops->post_action(substream, state);
763 else if (ops->undo_action)
764 ops->undo_action(substream, state);
769 * Note: call with stream lock
771 static int snd_pcm_action(struct action_ops *ops,
772 struct snd_pcm_substream *substream,
777 if (snd_pcm_stream_linked(substream)) {
778 if (!spin_trylock(&substream->group->lock)) {
779 spin_unlock(&substream->self_group.lock);
780 spin_lock(&substream->group->lock);
781 spin_lock(&substream->self_group.lock);
783 res = snd_pcm_action_group(ops, substream, state, 1);
784 spin_unlock(&substream->group->lock);
786 res = snd_pcm_action_single(ops, substream, state);
792 * Note: don't use any locks before
794 static int snd_pcm_action_lock_irq(struct action_ops *ops,
795 struct snd_pcm_substream *substream,
800 read_lock_irq(&snd_pcm_link_rwlock);
801 if (snd_pcm_stream_linked(substream)) {
802 spin_lock(&substream->group->lock);
803 spin_lock(&substream->self_group.lock);
804 res = snd_pcm_action_group(ops, substream, state, 1);
805 spin_unlock(&substream->self_group.lock);
806 spin_unlock(&substream->group->lock);
808 spin_lock(&substream->self_group.lock);
809 res = snd_pcm_action_single(ops, substream, state);
810 spin_unlock(&substream->self_group.lock);
812 read_unlock_irq(&snd_pcm_link_rwlock);
818 static int snd_pcm_action_nonatomic(struct action_ops *ops,
819 struct snd_pcm_substream *substream,
824 down_read(&snd_pcm_link_rwsem);
825 if (snd_pcm_stream_linked(substream))
826 res = snd_pcm_action_group(ops, substream, state, 0);
828 res = snd_pcm_action_single(ops, substream, state);
829 up_read(&snd_pcm_link_rwsem);
836 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
838 struct snd_pcm_runtime *runtime = substream->runtime;
839 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
841 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
842 !snd_pcm_playback_data(substream))
844 runtime->trigger_master = substream;
848 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
850 if (substream->runtime->trigger_master != substream)
852 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
855 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
857 if (substream->runtime->trigger_master == substream)
858 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
861 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
863 struct snd_pcm_runtime *runtime = substream->runtime;
864 snd_pcm_trigger_tstamp(substream);
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
883 * @substream: the PCM substream instance
885 * Start all linked streams.
887 int snd_pcm_start(struct snd_pcm_substream *substream)
889 return snd_pcm_action(&snd_pcm_action_start, substream,
890 SNDRV_PCM_STATE_RUNNING);
896 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
898 struct snd_pcm_runtime *runtime = substream->runtime;
899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
901 runtime->trigger_master = substream;
905 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
907 if (substream->runtime->trigger_master == substream &&
908 snd_pcm_running(substream))
909 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
910 return 0; /* unconditonally stop all substreams */
913 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
915 struct snd_pcm_runtime *runtime = substream->runtime;
916 if (runtime->status->state != state) {
917 snd_pcm_trigger_tstamp(substream);
918 if (substream->timer)
919 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
920 &runtime->trigger_tstamp);
921 runtime->status->state = state;
923 wake_up(&runtime->sleep);
926 static struct action_ops snd_pcm_action_stop = {
927 .pre_action = snd_pcm_pre_stop,
928 .do_action = snd_pcm_do_stop,
929 .post_action = snd_pcm_post_stop
934 * @substream: the PCM substream instance
935 * @state: PCM state after stopping the stream
937 * Try to stop all running streams in the substream group.
938 * The state of each stream is changed to the given value after that unconditionally.
940 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
942 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
945 EXPORT_SYMBOL(snd_pcm_stop);
949 * @substream: the PCM substream
951 * Stop the DMA only when the given stream is playback.
952 * The state is changed to SETUP.
953 * Unlike snd_pcm_stop(), this affects only the given stream.
955 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
957 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
958 SNDRV_PCM_STATE_SETUP);
964 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
966 struct snd_pcm_runtime *runtime = substream->runtime;
967 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
970 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
972 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
974 runtime->trigger_master = substream;
978 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
980 if (substream->runtime->trigger_master != substream)
982 return substream->ops->trigger(substream,
983 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
984 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
987 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
989 if (substream->runtime->trigger_master == substream)
990 substream->ops->trigger(substream,
991 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
992 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
995 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
997 struct snd_pcm_runtime *runtime = substream->runtime;
998 snd_pcm_trigger_tstamp(substream);
1000 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1001 if (substream->timer)
1002 snd_timer_notify(substream->timer,
1003 SNDRV_TIMER_EVENT_MPAUSE,
1004 &runtime->trigger_tstamp);
1005 wake_up(&runtime->sleep);
1007 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1008 if (substream->timer)
1009 snd_timer_notify(substream->timer,
1010 SNDRV_TIMER_EVENT_MCONTINUE,
1011 &runtime->trigger_tstamp);
1015 static struct action_ops snd_pcm_action_pause = {
1016 .pre_action = snd_pcm_pre_pause,
1017 .do_action = snd_pcm_do_pause,
1018 .undo_action = snd_pcm_undo_pause,
1019 .post_action = snd_pcm_post_pause
1023 * Push/release the pause for all linked streams.
1025 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1027 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1033 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1035 struct snd_pcm_runtime *runtime = substream->runtime;
1036 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1038 runtime->trigger_master = substream;
1042 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1044 struct snd_pcm_runtime *runtime = substream->runtime;
1045 if (runtime->trigger_master != substream)
1047 if (! snd_pcm_running(substream))
1049 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1050 return 0; /* suspend unconditionally */
1053 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1055 struct snd_pcm_runtime *runtime = substream->runtime;
1056 snd_pcm_trigger_tstamp(substream);
1057 if (substream->timer)
1058 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1059 &runtime->trigger_tstamp);
1060 runtime->status->suspended_state = runtime->status->state;
1061 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1062 wake_up(&runtime->sleep);
1065 static struct action_ops snd_pcm_action_suspend = {
1066 .pre_action = snd_pcm_pre_suspend,
1067 .do_action = snd_pcm_do_suspend,
1068 .post_action = snd_pcm_post_suspend
1073 * @substream: the PCM substream
1075 * Trigger SUSPEND to all linked streams.
1076 * After this call, all streams are changed to SUSPENDED state.
1078 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1081 unsigned long flags;
1086 snd_pcm_stream_lock_irqsave(substream, flags);
1087 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1088 snd_pcm_stream_unlock_irqrestore(substream, flags);
1092 EXPORT_SYMBOL(snd_pcm_suspend);
1095 * snd_pcm_suspend_all
1096 * @pcm: the PCM instance
1098 * Trigger SUSPEND to all substreams in the given pcm.
1099 * After this call, all streams are changed to SUSPENDED state.
1101 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1103 struct snd_pcm_substream *substream;
1104 int stream, err = 0;
1109 for (stream = 0; stream < 2; stream++) {
1110 for (substream = pcm->streams[stream].substream;
1111 substream; substream = substream->next) {
1112 /* FIXME: the open/close code should lock this as well */
1113 if (substream->runtime == NULL)
1115 err = snd_pcm_suspend(substream);
1116 if (err < 0 && err != -EBUSY)
1123 EXPORT_SYMBOL(snd_pcm_suspend_all);
1127 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1132 runtime->trigger_master = substream;
1136 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1139 if (runtime->trigger_master != substream)
1141 /* DMA not running previously? */
1142 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1143 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1144 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1146 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1149 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1151 if (substream->runtime->trigger_master == substream &&
1152 snd_pcm_running(substream))
1153 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1156 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1158 struct snd_pcm_runtime *runtime = substream->runtime;
1159 snd_pcm_trigger_tstamp(substream);
1160 if (substream->timer)
1161 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1162 &runtime->trigger_tstamp);
1163 runtime->status->state = runtime->status->suspended_state;
1166 static struct action_ops snd_pcm_action_resume = {
1167 .pre_action = snd_pcm_pre_resume,
1168 .do_action = snd_pcm_do_resume,
1169 .undo_action = snd_pcm_undo_resume,
1170 .post_action = snd_pcm_post_resume
1173 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1175 struct snd_card *card = substream->pcm->card;
1178 snd_power_lock(card);
1179 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1180 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1181 snd_power_unlock(card);
1187 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1192 #endif /* CONFIG_PM */
1197 * Change the RUNNING stream(s) to XRUN state.
1199 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1201 struct snd_card *card = substream->pcm->card;
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1205 snd_power_lock(card);
1206 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1207 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1212 snd_pcm_stream_lock_irq(substream);
1213 switch (runtime->status->state) {
1214 case SNDRV_PCM_STATE_XRUN:
1215 result = 0; /* already there */
1217 case SNDRV_PCM_STATE_RUNNING:
1218 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1223 snd_pcm_stream_unlock_irq(substream);
1225 snd_power_unlock(card);
1232 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1234 struct snd_pcm_runtime *runtime = substream->runtime;
1235 switch (runtime->status->state) {
1236 case SNDRV_PCM_STATE_RUNNING:
1237 case SNDRV_PCM_STATE_PREPARED:
1238 case SNDRV_PCM_STATE_PAUSED:
1239 case SNDRV_PCM_STATE_SUSPENDED:
1246 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1252 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1253 runtime->hw_ptr_base = 0;
1254 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1255 runtime->status->hw_ptr % runtime->period_size;
1256 runtime->silence_start = runtime->status->hw_ptr;
1257 runtime->silence_filled = 0;
1261 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 runtime->control->appl_ptr = runtime->status->hw_ptr;
1265 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1266 runtime->silence_size > 0)
1267 snd_pcm_playback_silence(substream, ULONG_MAX);
1270 static struct action_ops snd_pcm_action_reset = {
1271 .pre_action = snd_pcm_pre_reset,
1272 .do_action = snd_pcm_do_reset,
1273 .post_action = snd_pcm_post_reset
1276 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1278 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1284 /* we use the second argument for updating f_flags */
1285 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1288 struct snd_pcm_runtime *runtime = substream->runtime;
1289 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1290 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1292 if (snd_pcm_running(substream))
1294 substream->f_flags = f_flags;
1298 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1301 err = substream->ops->prepare(substream);
1304 return snd_pcm_do_reset(substream, 0);
1307 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1309 struct snd_pcm_runtime *runtime = substream->runtime;
1310 runtime->control->appl_ptr = runtime->status->hw_ptr;
1311 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1314 static struct action_ops snd_pcm_action_prepare = {
1315 .pre_action = snd_pcm_pre_prepare,
1316 .do_action = snd_pcm_do_prepare,
1317 .post_action = snd_pcm_post_prepare
1322 * @substream: the PCM substream instance
1323 * @file: file to refer f_flags
1325 * Prepare the PCM substream to be triggerable.
1327 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1331 struct snd_card *card = substream->pcm->card;
1335 f_flags = file->f_flags;
1337 f_flags = substream->f_flags;
1339 snd_power_lock(card);
1340 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1341 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1342 substream, f_flags);
1343 snd_power_unlock(card);
1351 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1353 if (substream->f_flags & O_NONBLOCK)
1355 substream->runtime->trigger_master = substream;
1359 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1363 switch (runtime->status->state) {
1364 case SNDRV_PCM_STATE_PREPARED:
1365 /* start playback stream if possible */
1366 if (! snd_pcm_playback_empty(substream)) {
1367 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1368 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1371 case SNDRV_PCM_STATE_RUNNING:
1372 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1378 /* stop running stream */
1379 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1380 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1381 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1382 snd_pcm_do_stop(substream, new_state);
1383 snd_pcm_post_stop(substream, new_state);
1389 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1393 static struct action_ops snd_pcm_action_drain_init = {
1394 .pre_action = snd_pcm_pre_drain_init,
1395 .do_action = snd_pcm_do_drain_init,
1396 .post_action = snd_pcm_post_drain_init
1400 struct snd_pcm_substream *substream;
1402 snd_pcm_uframes_t stop_threshold;
1405 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1408 * Drain the stream(s).
1409 * When the substream is linked, sync until the draining of all playback streams
1411 * After this call, all streams are supposed to be either SETUP or DRAINING
1412 * (capture only) state.
1414 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1416 struct snd_card *card;
1417 struct snd_pcm_runtime *runtime;
1418 struct snd_pcm_substream *s;
1421 struct drain_rec *drec, drec_tmp, *d;
1423 snd_assert(substream != NULL, return -ENXIO);
1424 card = substream->pcm->card;
1425 runtime = substream->runtime;
1427 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1430 snd_power_lock(card);
1431 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1432 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1434 snd_power_unlock(card);
1439 /* allocate temporary record for drain sync */
1440 down_read(&snd_pcm_link_rwsem);
1441 if (snd_pcm_stream_linked(substream)) {
1442 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1444 up_read(&snd_pcm_link_rwsem);
1445 snd_power_unlock(card);
1451 /* count only playback streams */
1453 snd_pcm_group_for_each_entry(s, substream) {
1454 runtime = s->runtime;
1455 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1456 d = &drec[num_drecs++];
1458 init_waitqueue_entry(&d->wait, current);
1459 add_wait_queue(&runtime->sleep, &d->wait);
1460 /* stop_threshold fixup to avoid endless loop when
1461 * stop_threshold > buffer_size
1463 d->stop_threshold = runtime->stop_threshold;
1464 if (runtime->stop_threshold > runtime->buffer_size)
1465 runtime->stop_threshold = runtime->buffer_size;
1468 up_read(&snd_pcm_link_rwsem);
1470 snd_pcm_stream_lock_irq(substream);
1472 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1473 snd_pcm_pause(substream, 0);
1475 /* pre-start/stop - all running streams are changed to DRAINING state */
1476 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1478 snd_pcm_stream_unlock_irq(substream);
1484 if (signal_pending(current)) {
1485 result = -ERESTARTSYS;
1489 for (i = 0; i < num_drecs; i++) {
1490 runtime = drec[i].substream->runtime;
1491 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1495 break; /* yes, all drained */
1497 set_current_state(TASK_INTERRUPTIBLE);
1498 snd_pcm_stream_unlock_irq(substream);
1499 snd_power_unlock(card);
1500 tout = schedule_timeout(10 * HZ);
1501 snd_power_lock(card);
1502 snd_pcm_stream_lock_irq(substream);
1504 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1507 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1508 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1515 snd_pcm_stream_unlock_irq(substream);
1518 for (i = 0; i < num_drecs; i++) {
1520 runtime = d->substream->runtime;
1521 remove_wait_queue(&runtime->sleep, &d->wait);
1522 runtime->stop_threshold = d->stop_threshold;
1525 if (drec != &drec_tmp)
1527 snd_power_unlock(card);
1535 * Immediately put all linked substreams into SETUP state.
1537 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1539 struct snd_pcm_runtime *runtime;
1540 struct snd_card *card;
1543 snd_assert(substream != NULL, return -ENXIO);
1544 runtime = substream->runtime;
1545 card = substream->pcm->card;
1547 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1548 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1551 snd_power_lock(card);
1552 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1553 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1558 snd_pcm_stream_lock_irq(substream);
1560 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1561 snd_pcm_pause(substream, 0);
1563 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1564 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1565 snd_pcm_stream_unlock_irq(substream);
1567 snd_power_unlock(card);
1572 /* WARNING: Don't forget to fput back the file */
1573 static struct file *snd_pcm_file_fd(int fd)
1576 struct inode *inode;
1582 inode = file->f_path.dentry->d_inode;
1583 if (!S_ISCHR(inode->i_mode) ||
1584 imajor(inode) != snd_major) {
1588 minor = iminor(inode);
1589 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1590 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1600 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1604 struct snd_pcm_file *pcm_file;
1605 struct snd_pcm_substream *substream1;
1607 file = snd_pcm_file_fd(fd);
1610 pcm_file = file->private_data;
1611 substream1 = pcm_file->substream;
1612 down_write(&snd_pcm_link_rwsem);
1613 write_lock_irq(&snd_pcm_link_rwlock);
1614 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1615 substream->runtime->status->state != substream1->runtime->status->state) {
1619 if (snd_pcm_stream_linked(substream1)) {
1623 if (!snd_pcm_stream_linked(substream)) {
1624 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1625 if (substream->group == NULL) {
1629 spin_lock_init(&substream->group->lock);
1630 INIT_LIST_HEAD(&substream->group->substreams);
1631 list_add_tail(&substream->link_list, &substream->group->substreams);
1632 substream->group->count = 1;
1634 list_add_tail(&substream1->link_list, &substream->group->substreams);
1635 substream->group->count++;
1636 substream1->group = substream->group;
1638 write_unlock_irq(&snd_pcm_link_rwlock);
1639 up_write(&snd_pcm_link_rwsem);
1644 static void relink_to_local(struct snd_pcm_substream *substream)
1646 substream->group = &substream->self_group;
1647 INIT_LIST_HEAD(&substream->self_group.substreams);
1648 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1651 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1653 struct snd_pcm_substream *s;
1656 down_write(&snd_pcm_link_rwsem);
1657 write_lock_irq(&snd_pcm_link_rwlock);
1658 if (!snd_pcm_stream_linked(substream)) {
1662 list_del(&substream->link_list);
1663 substream->group->count--;
1664 if (substream->group->count == 1) { /* detach the last stream, too */
1665 snd_pcm_group_for_each_entry(s, substream) {
1669 kfree(substream->group);
1671 relink_to_local(substream);
1673 write_unlock_irq(&snd_pcm_link_rwlock);
1674 up_write(&snd_pcm_link_rwsem);
1681 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1682 struct snd_pcm_hw_rule *rule)
1684 struct snd_interval t;
1685 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
1693 struct snd_interval t;
1694 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1695 hw_param_interval_c(params, rule->deps[1]), &t);
1696 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1699 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
1702 struct snd_interval t;
1703 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1704 hw_param_interval_c(params, rule->deps[1]),
1705 (unsigned long) rule->private, &t);
1706 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1709 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1710 struct snd_pcm_hw_rule *rule)
1712 struct snd_interval t;
1713 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1714 (unsigned long) rule->private,
1715 hw_param_interval_c(params, rule->deps[1]), &t);
1716 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1719 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1720 struct snd_pcm_hw_rule *rule)
1723 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1725 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1727 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1729 if (! snd_mask_test(mask, k))
1731 bits = snd_pcm_format_physical_width(k);
1733 continue; /* ignore invalid formats */
1734 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1735 snd_mask_reset(&m, k);
1737 return snd_mask_refine(mask, &m);
1740 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
1743 struct snd_interval t;
1749 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1751 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1753 bits = snd_pcm_format_physical_width(k);
1755 continue; /* ignore invalid formats */
1756 if (t.min > (unsigned)bits)
1758 if (t.max < (unsigned)bits)
1762 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1765 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1766 #error "Change this table"
1769 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1770 48000, 64000, 88200, 96000, 176400, 192000 };
1772 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1773 .count = ARRAY_SIZE(rates),
1777 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1778 struct snd_pcm_hw_rule *rule)
1780 struct snd_pcm_hardware *hw = rule->private;
1781 return snd_interval_list(hw_param_interval(params, rule->var),
1782 snd_pcm_known_rates.count,
1783 snd_pcm_known_rates.list, hw->rates);
1786 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1787 struct snd_pcm_hw_rule *rule)
1789 struct snd_interval t;
1790 struct snd_pcm_substream *substream = rule->private;
1792 t.max = substream->buffer_bytes_max;
1796 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1799 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1801 struct snd_pcm_runtime *runtime = substream->runtime;
1802 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1805 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1806 snd_mask_any(constrs_mask(constrs, k));
1809 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1810 snd_interval_any(constrs_interval(constrs, k));
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1820 snd_pcm_hw_rule_format, NULL,
1821 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1825 snd_pcm_hw_rule_sample_bits, NULL,
1826 SNDRV_PCM_HW_PARAM_FORMAT,
1827 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1831 snd_pcm_hw_rule_div, NULL,
1832 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1836 snd_pcm_hw_rule_mul, NULL,
1837 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1841 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1842 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1846 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1847 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1851 snd_pcm_hw_rule_div, NULL,
1852 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1856 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1861 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1862 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1866 snd_pcm_hw_rule_div, NULL,
1867 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1871 snd_pcm_hw_rule_div, NULL,
1872 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1876 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1877 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1880 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1881 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1882 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1885 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1886 snd_pcm_hw_rule_mul, NULL,
1887 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1890 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1891 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1892 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1895 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1896 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1897 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1900 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1901 snd_pcm_hw_rule_muldivk, (void*) 8,
1902 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1905 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1906 snd_pcm_hw_rule_muldivk, (void*) 8,
1907 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1910 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1911 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1912 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1915 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1916 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1917 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1923 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1925 struct snd_pcm_runtime *runtime = substream->runtime;
1926 struct snd_pcm_hardware *hw = &runtime->hw;
1928 unsigned int mask = 0;
1930 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1931 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1932 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1933 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1934 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1935 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1936 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1937 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1938 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1939 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1940 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1942 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1943 snd_assert(err >= 0, return -EINVAL);
1945 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1946 snd_assert(err >= 0, return -EINVAL);
1948 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1949 snd_assert(err >= 0, return -EINVAL);
1951 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1952 hw->channels_min, hw->channels_max);
1953 snd_assert(err >= 0, return -EINVAL);
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1956 hw->rate_min, hw->rate_max);
1957 snd_assert(err >= 0, return -EINVAL);
1959 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1960 hw->period_bytes_min, hw->period_bytes_max);
1961 snd_assert(err >= 0, return -EINVAL);
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1964 hw->periods_min, hw->periods_max);
1965 snd_assert(err >= 0, return -EINVAL);
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1968 hw->period_bytes_min, hw->buffer_bytes_max);
1969 snd_assert(err >= 0, return -EINVAL);
1971 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1972 snd_pcm_hw_rule_buffer_bytes_max, substream,
1973 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1978 if (runtime->dma_bytes) {
1979 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1980 snd_assert(err >= 0, return -EINVAL);
1983 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1984 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1985 snd_pcm_hw_rule_rate, hw,
1986 SNDRV_PCM_HW_PARAM_RATE, -1);
1991 /* FIXME: this belong to lowlevel */
1992 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1997 static void pcm_release_private(struct snd_pcm_substream *substream)
1999 snd_pcm_unlink(substream);
2002 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2004 substream->ref_count--;
2005 if (substream->ref_count > 0)
2008 snd_pcm_drop(substream);
2009 if (substream->hw_opened) {
2010 if (substream->ops->hw_free != NULL)
2011 substream->ops->hw_free(substream);
2012 substream->ops->close(substream);
2013 substream->hw_opened = 0;
2015 if (substream->pcm_release) {
2016 substream->pcm_release(substream);
2017 substream->pcm_release = NULL;
2019 snd_pcm_detach_substream(substream);
2022 EXPORT_SYMBOL(snd_pcm_release_substream);
2024 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2026 struct snd_pcm_substream **rsubstream)
2028 struct snd_pcm_substream *substream;
2031 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2034 if (substream->ref_count > 1) {
2035 *rsubstream = substream;
2039 err = snd_pcm_hw_constraints_init(substream);
2041 snd_printd("snd_pcm_hw_constraints_init failed\n");
2045 if ((err = substream->ops->open(substream)) < 0)
2048 substream->hw_opened = 1;
2050 err = snd_pcm_hw_constraints_complete(substream);
2052 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2056 *rsubstream = substream;
2060 snd_pcm_release_substream(substream);
2064 EXPORT_SYMBOL(snd_pcm_open_substream);
2066 static int snd_pcm_open_file(struct file *file,
2067 struct snd_pcm *pcm,
2069 struct snd_pcm_file **rpcm_file)
2071 struct snd_pcm_file *pcm_file;
2072 struct snd_pcm_substream *substream;
2073 struct snd_pcm_str *str;
2076 snd_assert(rpcm_file != NULL, return -EINVAL);
2079 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2083 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2084 if (pcm_file == NULL) {
2085 snd_pcm_release_substream(substream);
2088 pcm_file->substream = substream;
2089 if (substream->ref_count == 1) {
2090 str = substream->pstr;
2091 substream->file = pcm_file;
2092 substream->pcm_release = pcm_release_private;
2094 file->private_data = pcm_file;
2095 *rpcm_file = pcm_file;
2099 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2101 struct snd_pcm *pcm;
2103 pcm = snd_lookup_minor_data(iminor(inode),
2104 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2105 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2108 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2110 struct snd_pcm *pcm;
2112 pcm = snd_lookup_minor_data(iminor(inode),
2113 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2114 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2117 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2120 struct snd_pcm_file *pcm_file;
2127 err = snd_card_file_add(pcm->card, file);
2130 if (!try_module_get(pcm->card->module)) {
2134 init_waitqueue_entry(&wait, current);
2135 add_wait_queue(&pcm->open_wait, &wait);
2136 mutex_lock(&pcm->open_mutex);
2138 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2141 if (err == -EAGAIN) {
2142 if (file->f_flags & O_NONBLOCK) {
2148 set_current_state(TASK_INTERRUPTIBLE);
2149 mutex_unlock(&pcm->open_mutex);
2151 mutex_lock(&pcm->open_mutex);
2152 if (signal_pending(current)) {
2157 remove_wait_queue(&pcm->open_wait, &wait);
2158 mutex_unlock(&pcm->open_mutex);
2164 module_put(pcm->card->module);
2166 snd_card_file_remove(pcm->card, file);
2171 static int snd_pcm_release(struct inode *inode, struct file *file)
2173 struct snd_pcm *pcm;
2174 struct snd_pcm_substream *substream;
2175 struct snd_pcm_file *pcm_file;
2177 pcm_file = file->private_data;
2178 substream = pcm_file->substream;
2179 snd_assert(substream != NULL, return -ENXIO);
2180 pcm = substream->pcm;
2181 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2182 mutex_lock(&pcm->open_mutex);
2183 snd_pcm_release_substream(substream);
2185 mutex_unlock(&pcm->open_mutex);
2186 wake_up(&pcm->open_wait);
2187 module_put(pcm->card->module);
2188 snd_card_file_remove(pcm->card, file);
2192 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2193 snd_pcm_uframes_t frames)
2195 struct snd_pcm_runtime *runtime = substream->runtime;
2196 snd_pcm_sframes_t appl_ptr;
2197 snd_pcm_sframes_t ret;
2198 snd_pcm_sframes_t hw_avail;
2203 snd_pcm_stream_lock_irq(substream);
2204 switch (runtime->status->state) {
2205 case SNDRV_PCM_STATE_PREPARED:
2207 case SNDRV_PCM_STATE_DRAINING:
2208 case SNDRV_PCM_STATE_RUNNING:
2209 if (snd_pcm_update_hw_ptr(substream) >= 0)
2212 case SNDRV_PCM_STATE_XRUN:
2220 hw_avail = snd_pcm_playback_hw_avail(runtime);
2221 if (hw_avail <= 0) {
2225 if (frames > (snd_pcm_uframes_t)hw_avail)
2227 appl_ptr = runtime->control->appl_ptr - frames;
2229 appl_ptr += runtime->boundary;
2230 runtime->control->appl_ptr = appl_ptr;
2233 snd_pcm_stream_unlock_irq(substream);
2237 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2238 snd_pcm_uframes_t frames)
2240 struct snd_pcm_runtime *runtime = substream->runtime;
2241 snd_pcm_sframes_t appl_ptr;
2242 snd_pcm_sframes_t ret;
2243 snd_pcm_sframes_t hw_avail;
2248 snd_pcm_stream_lock_irq(substream);
2249 switch (runtime->status->state) {
2250 case SNDRV_PCM_STATE_PREPARED:
2251 case SNDRV_PCM_STATE_DRAINING:
2253 case SNDRV_PCM_STATE_RUNNING:
2254 if (snd_pcm_update_hw_ptr(substream) >= 0)
2257 case SNDRV_PCM_STATE_XRUN:
2265 hw_avail = snd_pcm_capture_hw_avail(runtime);
2266 if (hw_avail <= 0) {
2270 if (frames > (snd_pcm_uframes_t)hw_avail)
2272 appl_ptr = runtime->control->appl_ptr - frames;
2274 appl_ptr += runtime->boundary;
2275 runtime->control->appl_ptr = appl_ptr;
2278 snd_pcm_stream_unlock_irq(substream);
2282 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2283 snd_pcm_uframes_t frames)
2285 struct snd_pcm_runtime *runtime = substream->runtime;
2286 snd_pcm_sframes_t appl_ptr;
2287 snd_pcm_sframes_t ret;
2288 snd_pcm_sframes_t avail;
2293 snd_pcm_stream_lock_irq(substream);
2294 switch (runtime->status->state) {
2295 case SNDRV_PCM_STATE_PREPARED:
2296 case SNDRV_PCM_STATE_PAUSED:
2298 case SNDRV_PCM_STATE_DRAINING:
2299 case SNDRV_PCM_STATE_RUNNING:
2300 if (snd_pcm_update_hw_ptr(substream) >= 0)
2303 case SNDRV_PCM_STATE_XRUN:
2311 avail = snd_pcm_playback_avail(runtime);
2316 if (frames > (snd_pcm_uframes_t)avail)
2318 appl_ptr = runtime->control->appl_ptr + frames;
2319 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2320 appl_ptr -= runtime->boundary;
2321 runtime->control->appl_ptr = appl_ptr;
2324 snd_pcm_stream_unlock_irq(substream);
2328 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2329 snd_pcm_uframes_t frames)
2331 struct snd_pcm_runtime *runtime = substream->runtime;
2332 snd_pcm_sframes_t appl_ptr;
2333 snd_pcm_sframes_t ret;
2334 snd_pcm_sframes_t avail;
2339 snd_pcm_stream_lock_irq(substream);
2340 switch (runtime->status->state) {
2341 case SNDRV_PCM_STATE_PREPARED:
2342 case SNDRV_PCM_STATE_DRAINING:
2343 case SNDRV_PCM_STATE_PAUSED:
2345 case SNDRV_PCM_STATE_RUNNING:
2346 if (snd_pcm_update_hw_ptr(substream) >= 0)
2349 case SNDRV_PCM_STATE_XRUN:
2357 avail = snd_pcm_capture_avail(runtime);
2362 if (frames > (snd_pcm_uframes_t)avail)
2364 appl_ptr = runtime->control->appl_ptr + frames;
2365 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2366 appl_ptr -= runtime->boundary;
2367 runtime->control->appl_ptr = appl_ptr;
2370 snd_pcm_stream_unlock_irq(substream);
2374 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2376 struct snd_pcm_runtime *runtime = substream->runtime;
2379 snd_pcm_stream_lock_irq(substream);
2380 switch (runtime->status->state) {
2381 case SNDRV_PCM_STATE_DRAINING:
2382 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2384 case SNDRV_PCM_STATE_RUNNING:
2385 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2388 case SNDRV_PCM_STATE_PREPARED:
2389 case SNDRV_PCM_STATE_SUSPENDED:
2392 case SNDRV_PCM_STATE_XRUN:
2400 snd_pcm_stream_unlock_irq(substream);
2404 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2405 snd_pcm_sframes_t __user *res)
2407 struct snd_pcm_runtime *runtime = substream->runtime;
2409 snd_pcm_sframes_t n = 0;
2411 snd_pcm_stream_lock_irq(substream);
2412 switch (runtime->status->state) {
2413 case SNDRV_PCM_STATE_DRAINING:
2414 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2416 case SNDRV_PCM_STATE_RUNNING:
2417 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2420 case SNDRV_PCM_STATE_PREPARED:
2421 case SNDRV_PCM_STATE_SUSPENDED:
2423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2424 n = snd_pcm_playback_hw_avail(runtime);
2426 n = snd_pcm_capture_avail(runtime);
2428 case SNDRV_PCM_STATE_XRUN:
2436 snd_pcm_stream_unlock_irq(substream);
2438 if (put_user(n, res))
2443 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2444 struct snd_pcm_sync_ptr __user *_sync_ptr)
2446 struct snd_pcm_runtime *runtime = substream->runtime;
2447 struct snd_pcm_sync_ptr sync_ptr;
2448 volatile struct snd_pcm_mmap_status *status;
2449 volatile struct snd_pcm_mmap_control *control;
2452 memset(&sync_ptr, 0, sizeof(sync_ptr));
2453 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2455 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2457 status = runtime->status;
2458 control = runtime->control;
2459 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2460 err = snd_pcm_hwsync(substream);
2464 snd_pcm_stream_lock_irq(substream);
2465 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2466 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2468 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2469 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2470 control->avail_min = sync_ptr.c.control.avail_min;
2472 sync_ptr.c.control.avail_min = control->avail_min;
2473 sync_ptr.s.status.state = status->state;
2474 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2475 sync_ptr.s.status.tstamp = status->tstamp;
2476 sync_ptr.s.status.suspended_state = status->suspended_state;
2477 snd_pcm_stream_unlock_irq(substream);
2478 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2483 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2485 struct snd_pcm_runtime *runtime = substream->runtime;
2488 if (get_user(arg, _arg))
2490 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2492 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2493 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2494 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2498 static int snd_pcm_common_ioctl1(struct file *file,
2499 struct snd_pcm_substream *substream,
2500 unsigned int cmd, void __user *arg)
2502 snd_assert(substream != NULL, return -ENXIO);
2505 case SNDRV_PCM_IOCTL_PVERSION:
2506 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2507 case SNDRV_PCM_IOCTL_INFO:
2508 return snd_pcm_info_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2511 case SNDRV_PCM_IOCTL_TTSTAMP:
2512 return snd_pcm_tstamp(substream, arg);
2513 case SNDRV_PCM_IOCTL_HW_REFINE:
2514 return snd_pcm_hw_refine_user(substream, arg);
2515 case SNDRV_PCM_IOCTL_HW_PARAMS:
2516 return snd_pcm_hw_params_user(substream, arg);
2517 case SNDRV_PCM_IOCTL_HW_FREE:
2518 return snd_pcm_hw_free(substream);
2519 case SNDRV_PCM_IOCTL_SW_PARAMS:
2520 return snd_pcm_sw_params_user(substream, arg);
2521 case SNDRV_PCM_IOCTL_STATUS:
2522 return snd_pcm_status_user(substream, arg);
2523 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2524 return snd_pcm_channel_info_user(substream, arg);
2525 case SNDRV_PCM_IOCTL_PREPARE:
2526 return snd_pcm_prepare(substream, file);
2527 case SNDRV_PCM_IOCTL_RESET:
2528 return snd_pcm_reset(substream);
2529 case SNDRV_PCM_IOCTL_START:
2530 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2531 case SNDRV_PCM_IOCTL_LINK:
2532 return snd_pcm_link(substream, (int)(unsigned long) arg);
2533 case SNDRV_PCM_IOCTL_UNLINK:
2534 return snd_pcm_unlink(substream);
2535 case SNDRV_PCM_IOCTL_RESUME:
2536 return snd_pcm_resume(substream);
2537 case SNDRV_PCM_IOCTL_XRUN:
2538 return snd_pcm_xrun(substream);
2539 case SNDRV_PCM_IOCTL_HWSYNC:
2540 return snd_pcm_hwsync(substream);
2541 case SNDRV_PCM_IOCTL_DELAY:
2542 return snd_pcm_delay(substream, arg);
2543 case SNDRV_PCM_IOCTL_SYNC_PTR:
2544 return snd_pcm_sync_ptr(substream, arg);
2545 #ifdef CONFIG_SND_SUPPORT_OLD_API
2546 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2547 return snd_pcm_hw_refine_old_user(substream, arg);
2548 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2549 return snd_pcm_hw_params_old_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_DRAIN:
2552 return snd_pcm_drain(substream);
2553 case SNDRV_PCM_IOCTL_DROP:
2554 return snd_pcm_drop(substream);
2555 case SNDRV_PCM_IOCTL_PAUSE:
2558 snd_pcm_stream_lock_irq(substream);
2559 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2560 snd_pcm_stream_unlock_irq(substream);
2564 snd_printd("unknown ioctl = 0x%x\n", cmd);
2568 static int snd_pcm_playback_ioctl1(struct file *file,
2569 struct snd_pcm_substream *substream,
2570 unsigned int cmd, void __user *arg)
2572 snd_assert(substream != NULL, return -ENXIO);
2573 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2575 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2577 struct snd_xferi xferi;
2578 struct snd_xferi __user *_xferi = arg;
2579 struct snd_pcm_runtime *runtime = substream->runtime;
2580 snd_pcm_sframes_t result;
2581 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2583 if (put_user(0, &_xferi->result))
2585 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2587 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2588 __put_user(result, &_xferi->result);
2589 return result < 0 ? result : 0;
2591 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2593 struct snd_xfern xfern;
2594 struct snd_xfern __user *_xfern = arg;
2595 struct snd_pcm_runtime *runtime = substream->runtime;
2597 snd_pcm_sframes_t result;
2598 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2600 if (runtime->channels > 128)
2602 if (put_user(0, &_xfern->result))
2604 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2606 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2609 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2613 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2615 __put_user(result, &_xfern->result);
2616 return result < 0 ? result : 0;
2618 case SNDRV_PCM_IOCTL_REWIND:
2620 snd_pcm_uframes_t frames;
2621 snd_pcm_uframes_t __user *_frames = arg;
2622 snd_pcm_sframes_t result;
2623 if (get_user(frames, _frames))
2625 if (put_user(0, _frames))
2627 result = snd_pcm_playback_rewind(substream, frames);
2628 __put_user(result, _frames);
2629 return result < 0 ? result : 0;
2631 case SNDRV_PCM_IOCTL_FORWARD:
2633 snd_pcm_uframes_t frames;
2634 snd_pcm_uframes_t __user *_frames = arg;
2635 snd_pcm_sframes_t result;
2636 if (get_user(frames, _frames))
2638 if (put_user(0, _frames))
2640 result = snd_pcm_playback_forward(substream, frames);
2641 __put_user(result, _frames);
2642 return result < 0 ? result : 0;
2645 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2648 static int snd_pcm_capture_ioctl1(struct file *file,
2649 struct snd_pcm_substream *substream,
2650 unsigned int cmd, void __user *arg)
2652 snd_assert(substream != NULL, return -ENXIO);
2653 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2655 case SNDRV_PCM_IOCTL_READI_FRAMES:
2657 struct snd_xferi xferi;
2658 struct snd_xferi __user *_xferi = arg;
2659 struct snd_pcm_runtime *runtime = substream->runtime;
2660 snd_pcm_sframes_t result;
2661 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2663 if (put_user(0, &_xferi->result))
2665 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2667 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2668 __put_user(result, &_xferi->result);
2669 return result < 0 ? result : 0;
2671 case SNDRV_PCM_IOCTL_READN_FRAMES:
2673 struct snd_xfern xfern;
2674 struct snd_xfern __user *_xfern = arg;
2675 struct snd_pcm_runtime *runtime = substream->runtime;
2677 snd_pcm_sframes_t result;
2678 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2680 if (runtime->channels > 128)
2682 if (put_user(0, &_xfern->result))
2684 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2686 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2689 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2693 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2695 __put_user(result, &_xfern->result);
2696 return result < 0 ? result : 0;
2698 case SNDRV_PCM_IOCTL_REWIND:
2700 snd_pcm_uframes_t frames;
2701 snd_pcm_uframes_t __user *_frames = arg;
2702 snd_pcm_sframes_t result;
2703 if (get_user(frames, _frames))
2705 if (put_user(0, _frames))
2707 result = snd_pcm_capture_rewind(substream, frames);
2708 __put_user(result, _frames);
2709 return result < 0 ? result : 0;
2711 case SNDRV_PCM_IOCTL_FORWARD:
2713 snd_pcm_uframes_t frames;
2714 snd_pcm_uframes_t __user *_frames = arg;
2715 snd_pcm_sframes_t result;
2716 if (get_user(frames, _frames))
2718 if (put_user(0, _frames))
2720 result = snd_pcm_capture_forward(substream, frames);
2721 __put_user(result, _frames);
2722 return result < 0 ? result : 0;
2725 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2728 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2731 struct snd_pcm_file *pcm_file;
2733 pcm_file = file->private_data;
2735 if (((cmd >> 8) & 0xff) != 'A')
2738 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2739 (void __user *)arg);
2742 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2745 struct snd_pcm_file *pcm_file;
2747 pcm_file = file->private_data;
2749 if (((cmd >> 8) & 0xff) != 'A')
2752 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2753 (void __user *)arg);
2756 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2757 unsigned int cmd, void *arg)
2762 fs = snd_enter_user();
2763 switch (substream->stream) {
2764 case SNDRV_PCM_STREAM_PLAYBACK:
2765 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2766 (void __user *)arg);
2768 case SNDRV_PCM_STREAM_CAPTURE:
2769 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2770 (void __user *)arg);
2780 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2782 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2785 struct snd_pcm_file *pcm_file;
2786 struct snd_pcm_substream *substream;
2787 struct snd_pcm_runtime *runtime;
2788 snd_pcm_sframes_t result;
2790 pcm_file = file->private_data;
2791 substream = pcm_file->substream;
2792 snd_assert(substream != NULL, return -ENXIO);
2793 runtime = substream->runtime;
2794 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2796 if (!frame_aligned(runtime, count))
2798 count = bytes_to_frames(runtime, count);
2799 result = snd_pcm_lib_read(substream, buf, count);
2801 result = frames_to_bytes(runtime, result);
2805 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2806 size_t count, loff_t * offset)
2808 struct snd_pcm_file *pcm_file;
2809 struct snd_pcm_substream *substream;
2810 struct snd_pcm_runtime *runtime;
2811 snd_pcm_sframes_t result;
2813 pcm_file = file->private_data;
2814 substream = pcm_file->substream;
2815 snd_assert(substream != NULL, result = -ENXIO; goto end);
2816 runtime = substream->runtime;
2817 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2821 if (!frame_aligned(runtime, count)) {
2825 count = bytes_to_frames(runtime, count);
2826 result = snd_pcm_lib_write(substream, buf, count);
2828 result = frames_to_bytes(runtime, result);
2833 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2834 unsigned long nr_segs, loff_t pos)
2837 struct snd_pcm_file *pcm_file;
2838 struct snd_pcm_substream *substream;
2839 struct snd_pcm_runtime *runtime;
2840 snd_pcm_sframes_t result;
2843 snd_pcm_uframes_t frames;
2845 pcm_file = iocb->ki_filp->private_data;
2846 substream = pcm_file->substream;
2847 snd_assert(substream != NULL, return -ENXIO);
2848 runtime = substream->runtime;
2849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2851 if (nr_segs > 1024 || nr_segs != runtime->channels)
2853 if (!frame_aligned(runtime, iov->iov_len))
2855 frames = bytes_to_samples(runtime, iov->iov_len);
2856 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2859 for (i = 0; i < nr_segs; ++i)
2860 bufs[i] = iov[i].iov_base;
2861 result = snd_pcm_lib_readv(substream, bufs, frames);
2863 result = frames_to_bytes(runtime, result);
2868 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2869 unsigned long nr_segs, loff_t pos)
2871 struct snd_pcm_file *pcm_file;
2872 struct snd_pcm_substream *substream;
2873 struct snd_pcm_runtime *runtime;
2874 snd_pcm_sframes_t result;
2877 snd_pcm_uframes_t frames;
2879 pcm_file = iocb->ki_filp->private_data;
2880 substream = pcm_file->substream;
2881 snd_assert(substream != NULL, result = -ENXIO; goto end);
2882 runtime = substream->runtime;
2883 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2887 if (nr_segs > 128 || nr_segs != runtime->channels ||
2888 !frame_aligned(runtime, iov->iov_len)) {
2892 frames = bytes_to_samples(runtime, iov->iov_len);
2893 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2896 for (i = 0; i < nr_segs; ++i)
2897 bufs[i] = iov[i].iov_base;
2898 result = snd_pcm_lib_writev(substream, bufs, frames);
2900 result = frames_to_bytes(runtime, result);
2906 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2908 struct snd_pcm_file *pcm_file;
2909 struct snd_pcm_substream *substream;
2910 struct snd_pcm_runtime *runtime;
2912 snd_pcm_uframes_t avail;
2914 pcm_file = file->private_data;
2916 substream = pcm_file->substream;
2917 snd_assert(substream != NULL, return -ENXIO);
2918 runtime = substream->runtime;
2920 poll_wait(file, &runtime->sleep, wait);
2922 snd_pcm_stream_lock_irq(substream);
2923 avail = snd_pcm_playback_avail(runtime);
2924 switch (runtime->status->state) {
2925 case SNDRV_PCM_STATE_RUNNING:
2926 case SNDRV_PCM_STATE_PREPARED:
2927 case SNDRV_PCM_STATE_PAUSED:
2928 if (avail >= runtime->control->avail_min) {
2929 mask = POLLOUT | POLLWRNORM;
2933 case SNDRV_PCM_STATE_DRAINING:
2937 mask = POLLOUT | POLLWRNORM | POLLERR;
2940 snd_pcm_stream_unlock_irq(substream);
2944 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2946 struct snd_pcm_file *pcm_file;
2947 struct snd_pcm_substream *substream;
2948 struct snd_pcm_runtime *runtime;
2950 snd_pcm_uframes_t avail;
2952 pcm_file = file->private_data;
2954 substream = pcm_file->substream;
2955 snd_assert(substream != NULL, return -ENXIO);
2956 runtime = substream->runtime;
2958 poll_wait(file, &runtime->sleep, wait);
2960 snd_pcm_stream_lock_irq(substream);
2961 avail = snd_pcm_capture_avail(runtime);
2962 switch (runtime->status->state) {
2963 case SNDRV_PCM_STATE_RUNNING:
2964 case SNDRV_PCM_STATE_PREPARED:
2965 case SNDRV_PCM_STATE_PAUSED:
2966 if (avail >= runtime->control->avail_min) {
2967 mask = POLLIN | POLLRDNORM;
2972 case SNDRV_PCM_STATE_DRAINING:
2974 mask = POLLIN | POLLRDNORM;
2979 mask = POLLIN | POLLRDNORM | POLLERR;
2982 snd_pcm_stream_unlock_irq(substream);
2991 * Only on coherent architectures, we can mmap the status and the control records
2992 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2994 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2996 * mmap status record
2998 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2999 struct vm_fault *vmf)
3001 struct snd_pcm_substream *substream = area->vm_private_data;
3002 struct snd_pcm_runtime *runtime;
3004 if (substream == NULL)
3005 return VM_FAULT_SIGBUS;
3006 runtime = substream->runtime;
3007 vmf->page = virt_to_page(runtime->status);
3008 get_page(vmf->page);
3012 static struct vm_operations_struct snd_pcm_vm_ops_status =
3014 .fault = snd_pcm_mmap_status_fault,
3017 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3018 struct vm_area_struct *area)
3020 struct snd_pcm_runtime *runtime;
3022 if (!(area->vm_flags & VM_READ))
3024 runtime = substream->runtime;
3025 snd_assert(runtime != NULL, return -EAGAIN);
3026 size = area->vm_end - area->vm_start;
3027 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3029 area->vm_ops = &snd_pcm_vm_ops_status;
3030 area->vm_private_data = substream;
3031 area->vm_flags |= VM_RESERVED;
3036 * mmap control record
3038 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3039 struct vm_fault *vmf)
3041 struct snd_pcm_substream *substream = area->vm_private_data;
3042 struct snd_pcm_runtime *runtime;
3044 if (substream == NULL)
3045 return VM_FAULT_SIGBUS;
3046 runtime = substream->runtime;
3047 vmf->page = virt_to_page(runtime->control);
3048 get_page(vmf->page);
3052 static struct vm_operations_struct snd_pcm_vm_ops_control =
3054 .fault = snd_pcm_mmap_control_fault,
3057 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3058 struct vm_area_struct *area)
3060 struct snd_pcm_runtime *runtime;
3062 if (!(area->vm_flags & VM_READ))
3064 runtime = substream->runtime;
3065 snd_assert(runtime != NULL, return -EAGAIN);
3066 size = area->vm_end - area->vm_start;
3067 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3069 area->vm_ops = &snd_pcm_vm_ops_control;
3070 area->vm_private_data = substream;
3071 area->vm_flags |= VM_RESERVED;
3074 #else /* ! coherent mmap */
3076 * don't support mmap for status and control records.
3078 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3079 struct vm_area_struct *area)
3083 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3084 struct vm_area_struct *area)
3088 #endif /* coherent mmap */
3091 * fault callback for mmapping a RAM page
3093 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3094 struct vm_fault *vmf)
3096 struct snd_pcm_substream *substream = area->vm_private_data;
3097 struct snd_pcm_runtime *runtime;
3098 unsigned long offset;
3103 if (substream == NULL)
3104 return VM_FAULT_SIGBUS;
3105 runtime = substream->runtime;
3106 offset = vmf->pgoff << PAGE_SHIFT;
3107 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3108 if (offset > dma_bytes - PAGE_SIZE)
3109 return VM_FAULT_SIGBUS;
3110 if (substream->ops->page) {
3111 page = substream->ops->page(substream, offset);
3113 return VM_FAULT_SIGBUS;
3115 vaddr = runtime->dma_area + offset;
3116 page = virt_to_page(vaddr);
3123 static struct vm_operations_struct snd_pcm_vm_ops_data =
3125 .open = snd_pcm_mmap_data_open,
3126 .close = snd_pcm_mmap_data_close,
3127 .fault = snd_pcm_mmap_data_fault,
3131 * mmap the DMA buffer on RAM
3133 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3134 struct vm_area_struct *area)
3136 area->vm_ops = &snd_pcm_vm_ops_data;
3137 area->vm_private_data = substream;
3138 area->vm_flags |= VM_RESERVED;
3139 atomic_inc(&substream->mmap_count);
3144 * mmap the DMA buffer on I/O memory area
3146 #if SNDRV_PCM_INFO_MMAP_IOMEM
3147 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3149 .open = snd_pcm_mmap_data_open,
3150 .close = snd_pcm_mmap_data_close,
3153 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3154 struct vm_area_struct *area)
3157 unsigned long offset;
3159 #ifdef pgprot_noncached
3160 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3162 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3163 area->vm_private_data = substream;
3164 area->vm_flags |= VM_IO;
3165 size = area->vm_end - area->vm_start;
3166 offset = area->vm_pgoff << PAGE_SHIFT;
3167 if (io_remap_pfn_range(area, area->vm_start,
3168 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3169 size, area->vm_page_prot))
3171 atomic_inc(&substream->mmap_count);
3175 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3176 #endif /* SNDRV_PCM_INFO_MMAP */
3181 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3182 struct vm_area_struct *area)
3184 struct snd_pcm_runtime *runtime;
3186 unsigned long offset;
3189 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3190 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3193 if (!(area->vm_flags & VM_READ))
3196 runtime = substream->runtime;
3197 snd_assert(runtime != NULL, return -EAGAIN);
3198 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3200 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3202 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3203 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3205 size = area->vm_end - area->vm_start;
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3207 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3208 if ((size_t)size > dma_bytes)
3210 if (offset > dma_bytes - size)
3213 if (substream->ops->mmap)
3214 return substream->ops->mmap(substream, area);
3216 return snd_pcm_default_mmap(substream, area);
3219 EXPORT_SYMBOL(snd_pcm_mmap_data);
3221 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3223 struct snd_pcm_file * pcm_file;
3224 struct snd_pcm_substream *substream;
3225 unsigned long offset;
3227 pcm_file = file->private_data;
3228 substream = pcm_file->substream;
3229 snd_assert(substream != NULL, return -ENXIO);
3231 offset = area->vm_pgoff << PAGE_SHIFT;
3233 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3234 if (pcm_file->no_compat_mmap)
3236 return snd_pcm_mmap_status(substream, file, area);
3237 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3238 if (pcm_file->no_compat_mmap)
3240 return snd_pcm_mmap_control(substream, file, area);
3242 return snd_pcm_mmap_data(substream, file, area);
3247 static int snd_pcm_fasync(int fd, struct file * file, int on)
3249 struct snd_pcm_file * pcm_file;
3250 struct snd_pcm_substream *substream;
3251 struct snd_pcm_runtime *runtime;
3254 pcm_file = file->private_data;
3255 substream = pcm_file->substream;
3256 snd_assert(substream != NULL, return -ENXIO);
3257 runtime = substream->runtime;
3259 err = fasync_helper(fd, file, on, &runtime->fasync);
3268 #ifdef CONFIG_COMPAT
3269 #include "pcm_compat.c"
3271 #define snd_pcm_ioctl_compat NULL
3275 * To be removed helpers to keep binary compatibility
3278 #ifdef CONFIG_SND_SUPPORT_OLD_API
3279 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3280 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3282 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3283 struct snd_pcm_hw_params_old *oparams)
3287 memset(params, 0, sizeof(*params));
3288 params->flags = oparams->flags;
3289 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3290 params->masks[i].bits[0] = oparams->masks[i];
3291 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3292 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3293 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3294 params->info = oparams->info;
3295 params->msbits = oparams->msbits;
3296 params->rate_num = oparams->rate_num;
3297 params->rate_den = oparams->rate_den;
3298 params->fifo_size = oparams->fifo_size;
3301 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3302 struct snd_pcm_hw_params *params)
3306 memset(oparams, 0, sizeof(*oparams));
3307 oparams->flags = params->flags;
3308 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3309 oparams->masks[i] = params->masks[i].bits[0];
3310 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3311 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3312 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3313 oparams->info = params->info;
3314 oparams->msbits = params->msbits;
3315 oparams->rate_num = params->rate_num;
3316 oparams->rate_den = params->rate_den;
3317 oparams->fifo_size = params->fifo_size;
3320 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3321 struct snd_pcm_hw_params_old __user * _oparams)
3323 struct snd_pcm_hw_params *params;
3324 struct snd_pcm_hw_params_old *oparams = NULL;
3327 params = kmalloc(sizeof(*params), GFP_KERNEL);
3332 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3338 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3342 snd_pcm_hw_convert_from_old_params(params, oparams);
3343 err = snd_pcm_hw_refine(substream, params);
3344 snd_pcm_hw_convert_to_old_params(oparams, params);
3345 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3355 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
3358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
3367 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3372 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3376 snd_pcm_hw_convert_from_old_params(params, oparams);
3377 err = snd_pcm_hw_params(substream, params);
3378 snd_pcm_hw_convert_to_old_params(oparams, params);
3379 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3388 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3394 const struct file_operations snd_pcm_f_ops[2] = {
3396 .owner = THIS_MODULE,
3397 .write = snd_pcm_write,
3398 .aio_write = snd_pcm_aio_write,
3399 .open = snd_pcm_playback_open,
3400 .release = snd_pcm_release,
3401 .poll = snd_pcm_playback_poll,
3402 .unlocked_ioctl = snd_pcm_playback_ioctl,
3403 .compat_ioctl = snd_pcm_ioctl_compat,
3404 .mmap = snd_pcm_mmap,
3405 .fasync = snd_pcm_fasync,
3408 .owner = THIS_MODULE,
3409 .read = snd_pcm_read,
3410 .aio_read = snd_pcm_aio_read,
3411 .open = snd_pcm_capture_open,
3412 .release = snd_pcm_release,
3413 .poll = snd_pcm_capture_poll,
3414 .unlocked_ioctl = snd_pcm_capture_ioctl,
3415 .compat_ioctl = snd_pcm_ioctl_compat,
3416 .mmap = snd_pcm_mmap,
3417 .fasync = snd_pcm_fasync,