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/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos.h>
28 #include <linux/aio.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
38 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39 #include <dma-coherence.h>
46 struct snd_pcm_hw_params_old {
48 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
49 SNDRV_PCM_HW_PARAM_ACCESS + 1];
50 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
51 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
56 unsigned int rate_num;
57 unsigned int rate_den;
58 snd_pcm_uframes_t fifo_size;
59 unsigned char reserved[64];
62 #ifdef CONFIG_SND_SUPPORT_OLD_API
63 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
66 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params_old __user * _oparams);
71 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
77 DEFINE_RWLOCK(snd_pcm_link_rwlock);
78 EXPORT_SYMBOL(snd_pcm_link_rwlock);
80 DECLARE_RWSEM(snd_pcm_link_rwsem);
81 EXPORT_SYMBOL(snd_pcm_link_rwsem);
83 static inline mm_segment_t snd_enter_user(void)
85 mm_segment_t fs = get_fs();
90 static inline void snd_leave_user(mm_segment_t fs)
97 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
99 struct snd_pcm_runtime *runtime;
100 struct snd_pcm *pcm = substream->pcm;
101 struct snd_pcm_str *pstr = substream->pstr;
103 memset(info, 0, sizeof(*info));
104 info->card = pcm->card->number;
105 info->device = pcm->device;
106 info->stream = substream->stream;
107 info->subdevice = substream->number;
108 strlcpy(info->id, pcm->id, sizeof(info->id));
109 strlcpy(info->name, pcm->name, sizeof(info->name));
110 info->dev_class = pcm->dev_class;
111 info->dev_subclass = pcm->dev_subclass;
112 info->subdevices_count = pstr->substream_count;
113 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
114 strlcpy(info->subname, substream->name, sizeof(info->subname));
115 runtime = substream->runtime;
116 /* AB: FIXME!!! This is definitely nonsense */
118 info->sync = runtime->sync;
119 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
124 int snd_pcm_info_user(struct snd_pcm_substream *substream,
125 struct snd_pcm_info __user * _info)
127 struct snd_pcm_info *info;
130 info = kmalloc(sizeof(*info), GFP_KERNEL);
133 err = snd_pcm_info(substream, info);
135 if (copy_to_user(_info, info, sizeof(*info)))
145 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
146 static const char * const snd_pcm_hw_param_names[] = {
150 HW_PARAM(SAMPLE_BITS),
151 HW_PARAM(FRAME_BITS),
154 HW_PARAM(PERIOD_TIME),
155 HW_PARAM(PERIOD_SIZE),
156 HW_PARAM(PERIOD_BYTES),
158 HW_PARAM(BUFFER_TIME),
159 HW_PARAM(BUFFER_SIZE),
160 HW_PARAM(BUFFER_BYTES),
165 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
166 struct snd_pcm_hw_params *params)
169 struct snd_pcm_hardware *hw;
170 struct snd_interval *i = NULL;
171 struct snd_mask *m = NULL;
172 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
173 unsigned int rstamps[constrs->rules_num];
174 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
175 unsigned int stamp = 2;
179 params->fifo_size = 0;
180 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
182 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
183 params->rate_num = 0;
184 params->rate_den = 0;
187 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
188 m = hw_param_mask(params, k);
189 if (snd_mask_empty(m))
191 if (!(params->rmask & (1 << k)))
194 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
195 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197 changed = snd_mask_refine(m, constrs_mask(constrs, k));
199 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
202 params->cmask |= 1 << k;
207 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
208 i = hw_param_interval(params, k);
209 if (snd_interval_empty(i))
211 if (!(params->rmask & (1 << k)))
214 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
219 i->openmin ? '(' : '[', i->min,
220 i->max, i->openmax ? ')' : ']');
223 changed = snd_interval_refine(i, constrs_interval(constrs, k));
228 pr_cont("%c%u %u%c\n",
229 i->openmin ? '(' : '[', i->min,
230 i->max, i->openmax ? ')' : ']');
233 params->cmask |= 1 << k;
238 for (k = 0; k < constrs->rules_num; k++)
240 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
241 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
244 for (k = 0; k < constrs->rules_num; k++) {
245 struct snd_pcm_hw_rule *r = &constrs->rules[k];
248 if (r->cond && !(r->cond & params->flags))
250 for (d = 0; r->deps[d] >= 0; d++) {
251 if (vstamps[r->deps[d]] > rstamps[k]) {
259 pr_debug("Rule %d [%p]: ", k, r->func);
261 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
262 if (hw_is_mask(r->var)) {
263 m = hw_param_mask(params, r->var);
264 pr_cont("%x", *m->bits);
266 i = hw_param_interval(params, r->var);
271 i->openmin ? '(' : '[', i->min,
272 i->max, i->openmax ? ')' : ']');
276 changed = r->func(params, r);
280 if (hw_is_mask(r->var))
281 pr_cont("%x", *m->bits);
287 i->openmin ? '(' : '[', i->min,
288 i->max, i->openmax ? ')' : ']');
294 if (changed && r->var >= 0) {
295 params->cmask |= (1 << r->var);
296 vstamps[r->var] = stamp;
304 if (!params->msbits) {
305 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
306 if (snd_interval_single(i))
307 params->msbits = snd_interval_value(i);
310 if (!params->rate_den) {
311 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
312 if (snd_interval_single(i)) {
313 params->rate_num = snd_interval_value(i);
314 params->rate_den = 1;
318 hw = &substream->runtime->hw;
320 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
321 if (!params->fifo_size) {
322 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
323 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
324 if (snd_mask_min(m) == snd_mask_max(m) &&
325 snd_interval_min(i) == snd_interval_max(i)) {
326 changed = substream->ops->ioctl(substream,
327 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
336 EXPORT_SYMBOL(snd_pcm_hw_refine);
338 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
339 struct snd_pcm_hw_params __user * _params)
341 struct snd_pcm_hw_params *params;
344 params = memdup_user(_params, sizeof(*params));
346 return PTR_ERR(params);
348 err = snd_pcm_hw_refine(substream, params);
349 if (copy_to_user(_params, params, sizeof(*params))) {
358 static int period_to_usecs(struct snd_pcm_runtime *runtime)
363 return -1; /* invalid */
365 /* take 75% of period time as the deadline */
366 usecs = (750000 / runtime->rate) * runtime->period_size;
367 usecs += ((750000 % runtime->rate) * runtime->period_size) /
373 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
375 snd_pcm_stream_lock_irq(substream);
376 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
377 substream->runtime->status->state = state;
378 snd_pcm_stream_unlock_irq(substream);
381 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
382 struct snd_pcm_hw_params *params)
384 struct snd_pcm_runtime *runtime;
387 snd_pcm_uframes_t frames;
389 if (PCM_RUNTIME_CHECK(substream))
391 runtime = substream->runtime;
392 snd_pcm_stream_lock_irq(substream);
393 switch (runtime->status->state) {
394 case SNDRV_PCM_STATE_OPEN:
395 case SNDRV_PCM_STATE_SETUP:
396 case SNDRV_PCM_STATE_PREPARED:
399 snd_pcm_stream_unlock_irq(substream);
402 snd_pcm_stream_unlock_irq(substream);
403 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
404 if (!substream->oss.oss)
406 if (atomic_read(&substream->mmap_count))
410 err = snd_pcm_hw_refine(substream, params);
414 err = snd_pcm_hw_params_choose(substream, params);
418 if (substream->ops->hw_params != NULL) {
419 err = substream->ops->hw_params(substream, params);
424 runtime->access = params_access(params);
425 runtime->format = params_format(params);
426 runtime->subformat = params_subformat(params);
427 runtime->channels = params_channels(params);
428 runtime->rate = params_rate(params);
429 runtime->period_size = params_period_size(params);
430 runtime->periods = params_periods(params);
431 runtime->buffer_size = params_buffer_size(params);
432 runtime->info = params->info;
433 runtime->rate_num = params->rate_num;
434 runtime->rate_den = params->rate_den;
435 runtime->no_period_wakeup =
436 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
437 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
439 bits = snd_pcm_format_physical_width(runtime->format);
440 runtime->sample_bits = bits;
441 bits *= runtime->channels;
442 runtime->frame_bits = bits;
444 while (bits % 8 != 0) {
448 runtime->byte_align = bits / 8;
449 runtime->min_align = frames;
451 /* Default sw params */
452 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
453 runtime->period_step = 1;
454 runtime->control->avail_min = runtime->period_size;
455 runtime->start_threshold = 1;
456 runtime->stop_threshold = runtime->buffer_size;
457 runtime->silence_threshold = 0;
458 runtime->silence_size = 0;
459 runtime->boundary = runtime->buffer_size;
460 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
461 runtime->boundary *= 2;
463 snd_pcm_timer_resolution_change(substream);
464 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
466 if (pm_qos_request_active(&substream->latency_pm_qos_req))
467 pm_qos_remove_request(&substream->latency_pm_qos_req);
468 if ((usecs = period_to_usecs(runtime)) >= 0)
469 pm_qos_add_request(&substream->latency_pm_qos_req,
470 PM_QOS_CPU_DMA_LATENCY, usecs);
473 /* hardware might be unusable from this time,
474 so we force application to retry to set
475 the correct hardware parameter settings */
476 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
477 if (substream->ops->hw_free != NULL)
478 substream->ops->hw_free(substream);
482 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
483 struct snd_pcm_hw_params __user * _params)
485 struct snd_pcm_hw_params *params;
488 params = memdup_user(_params, sizeof(*params));
490 return PTR_ERR(params);
492 err = snd_pcm_hw_params(substream, params);
493 if (copy_to_user(_params, params, sizeof(*params))) {
502 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
504 struct snd_pcm_runtime *runtime;
507 if (PCM_RUNTIME_CHECK(substream))
509 runtime = substream->runtime;
510 snd_pcm_stream_lock_irq(substream);
511 switch (runtime->status->state) {
512 case SNDRV_PCM_STATE_SETUP:
513 case SNDRV_PCM_STATE_PREPARED:
516 snd_pcm_stream_unlock_irq(substream);
519 snd_pcm_stream_unlock_irq(substream);
520 if (atomic_read(&substream->mmap_count))
522 if (substream->ops->hw_free)
523 result = substream->ops->hw_free(substream);
524 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
525 pm_qos_remove_request(&substream->latency_pm_qos_req);
529 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
530 struct snd_pcm_sw_params *params)
532 struct snd_pcm_runtime *runtime;
535 if (PCM_RUNTIME_CHECK(substream))
537 runtime = substream->runtime;
538 snd_pcm_stream_lock_irq(substream);
539 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
540 snd_pcm_stream_unlock_irq(substream);
543 snd_pcm_stream_unlock_irq(substream);
545 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
547 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
548 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
550 if (params->avail_min == 0)
552 if (params->silence_size >= runtime->boundary) {
553 if (params->silence_threshold != 0)
556 if (params->silence_size > params->silence_threshold)
558 if (params->silence_threshold > runtime->buffer_size)
562 snd_pcm_stream_lock_irq(substream);
563 runtime->tstamp_mode = params->tstamp_mode;
564 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
565 runtime->tstamp_type = params->tstamp_type;
566 runtime->period_step = params->period_step;
567 runtime->control->avail_min = params->avail_min;
568 runtime->start_threshold = params->start_threshold;
569 runtime->stop_threshold = params->stop_threshold;
570 runtime->silence_threshold = params->silence_threshold;
571 runtime->silence_size = params->silence_size;
572 params->boundary = runtime->boundary;
573 if (snd_pcm_running(substream)) {
574 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
575 runtime->silence_size > 0)
576 snd_pcm_playback_silence(substream, ULONG_MAX);
577 err = snd_pcm_update_state(substream, runtime);
579 snd_pcm_stream_unlock_irq(substream);
583 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
584 struct snd_pcm_sw_params __user * _params)
586 struct snd_pcm_sw_params params;
588 if (copy_from_user(¶ms, _params, sizeof(params)))
590 err = snd_pcm_sw_params(substream, ¶ms);
591 if (copy_to_user(_params, ¶ms, sizeof(params)))
596 int snd_pcm_status(struct snd_pcm_substream *substream,
597 struct snd_pcm_status *status)
599 struct snd_pcm_runtime *runtime = substream->runtime;
601 snd_pcm_stream_lock_irq(substream);
602 status->state = runtime->status->state;
603 status->suspended_state = runtime->status->suspended_state;
604 if (status->state == SNDRV_PCM_STATE_OPEN)
606 status->trigger_tstamp = runtime->trigger_tstamp;
607 if (snd_pcm_running(substream)) {
608 snd_pcm_update_hw_ptr(substream);
609 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
610 status->tstamp = runtime->status->tstamp;
611 status->audio_tstamp =
612 runtime->status->audio_tstamp;
616 snd_pcm_gettime(runtime, &status->tstamp);
618 status->appl_ptr = runtime->control->appl_ptr;
619 status->hw_ptr = runtime->status->hw_ptr;
620 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
621 status->avail = snd_pcm_playback_avail(runtime);
622 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
623 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
624 status->delay = runtime->buffer_size - status->avail;
625 status->delay += runtime->delay;
629 status->avail = snd_pcm_capture_avail(runtime);
630 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
631 status->delay = status->avail + runtime->delay;
635 status->avail_max = runtime->avail_max;
636 status->overrange = runtime->overrange;
637 runtime->avail_max = 0;
638 runtime->overrange = 0;
640 snd_pcm_stream_unlock_irq(substream);
644 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
645 struct snd_pcm_status __user * _status)
647 struct snd_pcm_status status;
650 memset(&status, 0, sizeof(status));
651 res = snd_pcm_status(substream, &status);
654 if (copy_to_user(_status, &status, sizeof(status)))
659 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
660 struct snd_pcm_channel_info * info)
662 struct snd_pcm_runtime *runtime;
663 unsigned int channel;
665 channel = info->channel;
666 runtime = substream->runtime;
667 snd_pcm_stream_lock_irq(substream);
668 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
669 snd_pcm_stream_unlock_irq(substream);
672 snd_pcm_stream_unlock_irq(substream);
673 if (channel >= runtime->channels)
675 memset(info, 0, sizeof(*info));
676 info->channel = channel;
677 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
680 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
681 struct snd_pcm_channel_info __user * _info)
683 struct snd_pcm_channel_info info;
686 if (copy_from_user(&info, _info, sizeof(info)))
688 res = snd_pcm_channel_info(substream, &info);
691 if (copy_to_user(_info, &info, sizeof(info)))
696 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
698 struct snd_pcm_runtime *runtime = substream->runtime;
699 if (runtime->trigger_master == NULL)
701 if (runtime->trigger_master == substream) {
702 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
704 snd_pcm_trigger_tstamp(runtime->trigger_master);
705 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
707 runtime->trigger_master = NULL;
711 int (*pre_action)(struct snd_pcm_substream *substream, int state);
712 int (*do_action)(struct snd_pcm_substream *substream, int state);
713 void (*undo_action)(struct snd_pcm_substream *substream, int state);
714 void (*post_action)(struct snd_pcm_substream *substream, int state);
718 * this functions is core for handling of linked stream
719 * Note: the stream state might be changed also on failure
720 * Note2: call with calling stream lock + link lock
722 static int snd_pcm_action_group(struct action_ops *ops,
723 struct snd_pcm_substream *substream,
724 int state, int do_lock)
726 struct snd_pcm_substream *s = NULL;
727 struct snd_pcm_substream *s1;
730 snd_pcm_group_for_each_entry(s, substream) {
731 if (do_lock && s != substream) {
732 if (s->pcm->nonatomic)
733 mutex_lock_nested(&s->self_group.mutex,
734 SINGLE_DEPTH_NESTING);
736 spin_lock_nested(&s->self_group.lock,
737 SINGLE_DEPTH_NESTING);
739 res = ops->pre_action(s, state);
743 snd_pcm_group_for_each_entry(s, substream) {
744 res = ops->do_action(s, state);
746 if (ops->undo_action) {
747 snd_pcm_group_for_each_entry(s1, substream) {
748 if (s1 == s) /* failed stream */
750 ops->undo_action(s1, state);
753 s = NULL; /* unlock all */
757 snd_pcm_group_for_each_entry(s, substream) {
758 ops->post_action(s, state);
763 snd_pcm_group_for_each_entry(s1, substream) {
764 if (s1 != substream) {
765 if (s->pcm->nonatomic)
766 mutex_unlock(&s1->self_group.mutex);
768 spin_unlock(&s1->self_group.lock);
770 if (s1 == s) /* end */
778 * Note: call with stream lock
780 static int snd_pcm_action_single(struct action_ops *ops,
781 struct snd_pcm_substream *substream,
786 res = ops->pre_action(substream, state);
789 res = ops->do_action(substream, state);
791 ops->post_action(substream, state);
792 else if (ops->undo_action)
793 ops->undo_action(substream, state);
797 /* call in mutex-protected context */
798 static int snd_pcm_action_mutex(struct action_ops *ops,
799 struct snd_pcm_substream *substream,
804 if (snd_pcm_stream_linked(substream)) {
805 if (!mutex_trylock(&substream->group->mutex)) {
806 mutex_unlock(&substream->self_group.mutex);
807 mutex_lock(&substream->group->mutex);
808 mutex_lock(&substream->self_group.mutex);
810 res = snd_pcm_action_group(ops, substream, state, 1);
811 mutex_unlock(&substream->group->mutex);
813 res = snd_pcm_action_single(ops, substream, state);
819 * Note: call with stream lock
821 static int snd_pcm_action(struct action_ops *ops,
822 struct snd_pcm_substream *substream,
827 if (substream->pcm->nonatomic)
828 return snd_pcm_action_mutex(ops, substream, state);
830 if (snd_pcm_stream_linked(substream)) {
831 if (!spin_trylock(&substream->group->lock)) {
832 spin_unlock(&substream->self_group.lock);
833 spin_lock(&substream->group->lock);
834 spin_lock(&substream->self_group.lock);
836 res = snd_pcm_action_group(ops, substream, state, 1);
837 spin_unlock(&substream->group->lock);
839 res = snd_pcm_action_single(ops, substream, state);
844 static int snd_pcm_action_lock_mutex(struct action_ops *ops,
845 struct snd_pcm_substream *substream,
850 down_read(&snd_pcm_link_rwsem);
851 if (snd_pcm_stream_linked(substream)) {
852 mutex_lock(&substream->group->mutex);
853 mutex_lock_nested(&substream->self_group.mutex,
854 SINGLE_DEPTH_NESTING);
855 res = snd_pcm_action_group(ops, substream, state, 1);
856 mutex_unlock(&substream->self_group.mutex);
857 mutex_unlock(&substream->group->mutex);
859 mutex_lock(&substream->self_group.mutex);
860 res = snd_pcm_action_single(ops, substream, state);
861 mutex_unlock(&substream->self_group.mutex);
863 up_read(&snd_pcm_link_rwsem);
868 * Note: don't use any locks before
870 static int snd_pcm_action_lock_irq(struct action_ops *ops,
871 struct snd_pcm_substream *substream,
876 if (substream->pcm->nonatomic)
877 return snd_pcm_action_lock_mutex(ops, substream, state);
879 read_lock_irq(&snd_pcm_link_rwlock);
880 if (snd_pcm_stream_linked(substream)) {
881 spin_lock(&substream->group->lock);
882 spin_lock(&substream->self_group.lock);
883 res = snd_pcm_action_group(ops, substream, state, 1);
884 spin_unlock(&substream->self_group.lock);
885 spin_unlock(&substream->group->lock);
887 spin_lock(&substream->self_group.lock);
888 res = snd_pcm_action_single(ops, substream, state);
889 spin_unlock(&substream->self_group.lock);
891 read_unlock_irq(&snd_pcm_link_rwlock);
897 static int snd_pcm_action_nonatomic(struct action_ops *ops,
898 struct snd_pcm_substream *substream,
903 down_read(&snd_pcm_link_rwsem);
904 if (snd_pcm_stream_linked(substream))
905 res = snd_pcm_action_group(ops, substream, state, 0);
907 res = snd_pcm_action_single(ops, substream, state);
908 up_read(&snd_pcm_link_rwsem);
915 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
917 struct snd_pcm_runtime *runtime = substream->runtime;
918 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
920 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
921 !snd_pcm_playback_data(substream))
923 runtime->trigger_master = substream;
927 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
929 if (substream->runtime->trigger_master != substream)
931 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
934 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
936 if (substream->runtime->trigger_master == substream)
937 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
940 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 snd_pcm_trigger_tstamp(substream);
944 runtime->hw_ptr_jiffies = jiffies;
945 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
947 runtime->status->state = state;
948 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
949 runtime->silence_size > 0)
950 snd_pcm_playback_silence(substream, ULONG_MAX);
951 if (substream->timer)
952 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
953 &runtime->trigger_tstamp);
956 static struct action_ops snd_pcm_action_start = {
957 .pre_action = snd_pcm_pre_start,
958 .do_action = snd_pcm_do_start,
959 .undo_action = snd_pcm_undo_start,
960 .post_action = snd_pcm_post_start
964 * snd_pcm_start - start all linked streams
965 * @substream: the PCM substream instance
967 * Return: Zero if successful, or a negative error code.
969 int snd_pcm_start(struct snd_pcm_substream *substream)
971 return snd_pcm_action(&snd_pcm_action_start, substream,
972 SNDRV_PCM_STATE_RUNNING);
978 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
980 struct snd_pcm_runtime *runtime = substream->runtime;
981 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
983 runtime->trigger_master = substream;
987 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
989 if (substream->runtime->trigger_master == substream &&
990 snd_pcm_running(substream))
991 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
992 return 0; /* unconditonally stop all substreams */
995 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
997 struct snd_pcm_runtime *runtime = substream->runtime;
998 if (runtime->status->state != state) {
999 snd_pcm_trigger_tstamp(substream);
1000 if (substream->timer)
1001 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1002 &runtime->trigger_tstamp);
1003 runtime->status->state = state;
1005 wake_up(&runtime->sleep);
1006 wake_up(&runtime->tsleep);
1009 static struct action_ops snd_pcm_action_stop = {
1010 .pre_action = snd_pcm_pre_stop,
1011 .do_action = snd_pcm_do_stop,
1012 .post_action = snd_pcm_post_stop
1016 * snd_pcm_stop - try to stop all running streams in the substream group
1017 * @substream: the PCM substream instance
1018 * @state: PCM state after stopping the stream
1020 * The state of each stream is then changed to the given state unconditionally.
1022 * Return: Zero if successful, or a negative error code.
1024 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1026 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1029 EXPORT_SYMBOL(snd_pcm_stop);
1032 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1033 * @substream: the PCM substream
1035 * After stopping, the state is changed to SETUP.
1036 * Unlike snd_pcm_stop(), this affects only the given stream.
1038 * Return: Zero if succesful, or a negative error code.
1040 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1042 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1043 SNDRV_PCM_STATE_SETUP);
1049 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1051 struct snd_pcm_runtime *runtime = substream->runtime;
1052 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1055 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1057 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1059 runtime->trigger_master = substream;
1063 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1065 if (substream->runtime->trigger_master != substream)
1067 /* some drivers might use hw_ptr to recover from the pause -
1068 update the hw_ptr now */
1070 snd_pcm_update_hw_ptr(substream);
1071 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1072 * a delta between the current jiffies, this gives a large enough
1073 * delta, effectively to skip the check once.
1075 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1076 return substream->ops->trigger(substream,
1077 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1078 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1081 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1083 if (substream->runtime->trigger_master == substream)
1084 substream->ops->trigger(substream,
1085 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1086 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1089 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1091 struct snd_pcm_runtime *runtime = substream->runtime;
1092 snd_pcm_trigger_tstamp(substream);
1094 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1095 if (substream->timer)
1096 snd_timer_notify(substream->timer,
1097 SNDRV_TIMER_EVENT_MPAUSE,
1098 &runtime->trigger_tstamp);
1099 wake_up(&runtime->sleep);
1100 wake_up(&runtime->tsleep);
1102 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1103 if (substream->timer)
1104 snd_timer_notify(substream->timer,
1105 SNDRV_TIMER_EVENT_MCONTINUE,
1106 &runtime->trigger_tstamp);
1110 static struct action_ops snd_pcm_action_pause = {
1111 .pre_action = snd_pcm_pre_pause,
1112 .do_action = snd_pcm_do_pause,
1113 .undo_action = snd_pcm_undo_pause,
1114 .post_action = snd_pcm_post_pause
1118 * Push/release the pause for all linked streams.
1120 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1122 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1128 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1130 struct snd_pcm_runtime *runtime = substream->runtime;
1131 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1133 runtime->trigger_master = substream;
1137 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1139 struct snd_pcm_runtime *runtime = substream->runtime;
1140 if (runtime->trigger_master != substream)
1142 if (! snd_pcm_running(substream))
1144 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1145 return 0; /* suspend unconditionally */
1148 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1150 struct snd_pcm_runtime *runtime = substream->runtime;
1151 snd_pcm_trigger_tstamp(substream);
1152 if (substream->timer)
1153 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1154 &runtime->trigger_tstamp);
1155 runtime->status->suspended_state = runtime->status->state;
1156 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1157 wake_up(&runtime->sleep);
1158 wake_up(&runtime->tsleep);
1161 static struct action_ops snd_pcm_action_suspend = {
1162 .pre_action = snd_pcm_pre_suspend,
1163 .do_action = snd_pcm_do_suspend,
1164 .post_action = snd_pcm_post_suspend
1168 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1169 * @substream: the PCM substream
1171 * After this call, all streams are changed to SUSPENDED state.
1173 * Return: Zero if successful (or @substream is %NULL), or a negative error
1176 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1179 unsigned long flags;
1184 snd_pcm_stream_lock_irqsave(substream, flags);
1185 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1186 snd_pcm_stream_unlock_irqrestore(substream, flags);
1190 EXPORT_SYMBOL(snd_pcm_suspend);
1193 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1194 * @pcm: the PCM instance
1196 * After this call, all streams are changed to SUSPENDED state.
1198 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1200 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1202 struct snd_pcm_substream *substream;
1203 int stream, err = 0;
1208 for (stream = 0; stream < 2; stream++) {
1209 for (substream = pcm->streams[stream].substream;
1210 substream; substream = substream->next) {
1211 /* FIXME: the open/close code should lock this as well */
1212 if (substream->runtime == NULL)
1214 err = snd_pcm_suspend(substream);
1215 if (err < 0 && err != -EBUSY)
1222 EXPORT_SYMBOL(snd_pcm_suspend_all);
1226 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1228 struct snd_pcm_runtime *runtime = substream->runtime;
1229 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1231 runtime->trigger_master = substream;
1235 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1237 struct snd_pcm_runtime *runtime = substream->runtime;
1238 if (runtime->trigger_master != substream)
1240 /* DMA not running previously? */
1241 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1242 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1243 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1245 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1248 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1250 if (substream->runtime->trigger_master == substream &&
1251 snd_pcm_running(substream))
1252 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1255 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1257 struct snd_pcm_runtime *runtime = substream->runtime;
1258 snd_pcm_trigger_tstamp(substream);
1259 if (substream->timer)
1260 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1261 &runtime->trigger_tstamp);
1262 runtime->status->state = runtime->status->suspended_state;
1265 static struct action_ops snd_pcm_action_resume = {
1266 .pre_action = snd_pcm_pre_resume,
1267 .do_action = snd_pcm_do_resume,
1268 .undo_action = snd_pcm_undo_resume,
1269 .post_action = snd_pcm_post_resume
1272 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1274 struct snd_card *card = substream->pcm->card;
1277 snd_power_lock(card);
1278 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1279 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1280 snd_power_unlock(card);
1286 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1291 #endif /* CONFIG_PM */
1296 * Change the RUNNING stream(s) to XRUN state.
1298 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1300 struct snd_card *card = substream->pcm->card;
1301 struct snd_pcm_runtime *runtime = substream->runtime;
1304 snd_power_lock(card);
1305 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1306 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1311 snd_pcm_stream_lock_irq(substream);
1312 switch (runtime->status->state) {
1313 case SNDRV_PCM_STATE_XRUN:
1314 result = 0; /* already there */
1316 case SNDRV_PCM_STATE_RUNNING:
1317 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1322 snd_pcm_stream_unlock_irq(substream);
1324 snd_power_unlock(card);
1331 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1333 struct snd_pcm_runtime *runtime = substream->runtime;
1334 switch (runtime->status->state) {
1335 case SNDRV_PCM_STATE_RUNNING:
1336 case SNDRV_PCM_STATE_PREPARED:
1337 case SNDRV_PCM_STATE_PAUSED:
1338 case SNDRV_PCM_STATE_SUSPENDED:
1345 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1347 struct snd_pcm_runtime *runtime = substream->runtime;
1348 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1351 runtime->hw_ptr_base = 0;
1352 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1353 runtime->status->hw_ptr % runtime->period_size;
1354 runtime->silence_start = runtime->status->hw_ptr;
1355 runtime->silence_filled = 0;
1359 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 runtime->control->appl_ptr = runtime->status->hw_ptr;
1363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1364 runtime->silence_size > 0)
1365 snd_pcm_playback_silence(substream, ULONG_MAX);
1368 static struct action_ops snd_pcm_action_reset = {
1369 .pre_action = snd_pcm_pre_reset,
1370 .do_action = snd_pcm_do_reset,
1371 .post_action = snd_pcm_post_reset
1374 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1376 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1382 /* we use the second argument for updating f_flags */
1383 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1386 struct snd_pcm_runtime *runtime = substream->runtime;
1387 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1388 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1390 if (snd_pcm_running(substream))
1392 substream->f_flags = f_flags;
1396 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1399 err = substream->ops->prepare(substream);
1402 return snd_pcm_do_reset(substream, 0);
1405 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1407 struct snd_pcm_runtime *runtime = substream->runtime;
1408 runtime->control->appl_ptr = runtime->status->hw_ptr;
1409 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1412 static struct action_ops snd_pcm_action_prepare = {
1413 .pre_action = snd_pcm_pre_prepare,
1414 .do_action = snd_pcm_do_prepare,
1415 .post_action = snd_pcm_post_prepare
1419 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1420 * @substream: the PCM substream instance
1421 * @file: file to refer f_flags
1423 * Return: Zero if successful, or a negative error code.
1425 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1429 struct snd_card *card = substream->pcm->card;
1433 f_flags = file->f_flags;
1435 f_flags = substream->f_flags;
1437 snd_power_lock(card);
1438 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1439 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1440 substream, f_flags);
1441 snd_power_unlock(card);
1449 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1451 struct snd_pcm_runtime *runtime = substream->runtime;
1452 switch (runtime->status->state) {
1453 case SNDRV_PCM_STATE_OPEN:
1454 case SNDRV_PCM_STATE_DISCONNECTED:
1455 case SNDRV_PCM_STATE_SUSPENDED:
1458 runtime->trigger_master = substream;
1462 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1464 struct snd_pcm_runtime *runtime = substream->runtime;
1465 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1466 switch (runtime->status->state) {
1467 case SNDRV_PCM_STATE_PREPARED:
1468 /* start playback stream if possible */
1469 if (! snd_pcm_playback_empty(substream)) {
1470 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1471 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1474 case SNDRV_PCM_STATE_RUNNING:
1475 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1477 case SNDRV_PCM_STATE_XRUN:
1478 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1484 /* stop running stream */
1485 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1486 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1487 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1488 snd_pcm_do_stop(substream, new_state);
1489 snd_pcm_post_stop(substream, new_state);
1495 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1499 static struct action_ops snd_pcm_action_drain_init = {
1500 .pre_action = snd_pcm_pre_drain_init,
1501 .do_action = snd_pcm_do_drain_init,
1502 .post_action = snd_pcm_post_drain_init
1505 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1508 * Drain the stream(s).
1509 * When the substream is linked, sync until the draining of all playback streams
1511 * After this call, all streams are supposed to be either SETUP or DRAINING
1512 * (capture only) state.
1514 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1517 struct snd_card *card;
1518 struct snd_pcm_runtime *runtime;
1519 struct snd_pcm_substream *s;
1524 card = substream->pcm->card;
1525 runtime = substream->runtime;
1527 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1530 snd_power_lock(card);
1531 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1532 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1534 snd_power_unlock(card);
1540 if (file->f_flags & O_NONBLOCK)
1542 } else if (substream->f_flags & O_NONBLOCK)
1545 down_read(&snd_pcm_link_rwsem);
1546 snd_pcm_stream_lock_irq(substream);
1548 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1549 snd_pcm_pause(substream, 0);
1551 /* pre-start/stop - all running streams are changed to DRAINING state */
1552 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1555 /* in non-blocking, we don't wait in ioctl but let caller poll */
1563 struct snd_pcm_runtime *to_check;
1564 if (signal_pending(current)) {
1565 result = -ERESTARTSYS;
1568 /* find a substream to drain */
1570 snd_pcm_group_for_each_entry(s, substream) {
1571 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1573 runtime = s->runtime;
1574 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1580 break; /* all drained */
1581 init_waitqueue_entry(&wait, current);
1582 add_wait_queue(&to_check->sleep, &wait);
1583 snd_pcm_stream_unlock_irq(substream);
1584 up_read(&snd_pcm_link_rwsem);
1585 snd_power_unlock(card);
1586 if (runtime->no_period_wakeup)
1587 tout = MAX_SCHEDULE_TIMEOUT;
1590 if (runtime->rate) {
1591 long t = runtime->period_size * 2 / runtime->rate;
1592 tout = max(t, tout);
1594 tout = msecs_to_jiffies(tout * 1000);
1596 tout = schedule_timeout_interruptible(tout);
1597 snd_power_lock(card);
1598 down_read(&snd_pcm_link_rwsem);
1599 snd_pcm_stream_lock_irq(substream);
1600 remove_wait_queue(&to_check->sleep, &wait);
1601 if (card->shutdown) {
1606 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1609 dev_dbg(substream->pcm->card->dev,
1610 "playback drain error (DMA or IRQ trouble?)\n");
1611 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1619 snd_pcm_stream_unlock_irq(substream);
1620 up_read(&snd_pcm_link_rwsem);
1621 snd_power_unlock(card);
1629 * Immediately put all linked substreams into SETUP state.
1631 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1633 struct snd_pcm_runtime *runtime;
1636 if (PCM_RUNTIME_CHECK(substream))
1638 runtime = substream->runtime;
1640 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1641 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1642 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1645 snd_pcm_stream_lock_irq(substream);
1647 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1648 snd_pcm_pause(substream, 0);
1650 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1651 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1652 snd_pcm_stream_unlock_irq(substream);
1658 static bool is_pcm_file(struct file *file)
1660 struct inode *inode = file_inode(file);
1663 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1665 minor = iminor(inode);
1666 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1667 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1673 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1676 struct snd_pcm_file *pcm_file;
1677 struct snd_pcm_substream *substream1;
1678 struct snd_pcm_group *group;
1679 struct fd f = fdget(fd);
1683 if (!is_pcm_file(f.file)) {
1687 pcm_file = f.file->private_data;
1688 substream1 = pcm_file->substream;
1689 group = kmalloc(sizeof(*group), GFP_KERNEL);
1694 down_write(&snd_pcm_link_rwsem);
1695 write_lock_irq(&snd_pcm_link_rwlock);
1696 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1697 substream->runtime->status->state != substream1->runtime->status->state ||
1698 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1702 if (snd_pcm_stream_linked(substream1)) {
1706 if (!snd_pcm_stream_linked(substream)) {
1707 substream->group = group;
1709 spin_lock_init(&substream->group->lock);
1710 mutex_init(&substream->group->mutex);
1711 INIT_LIST_HEAD(&substream->group->substreams);
1712 list_add_tail(&substream->link_list, &substream->group->substreams);
1713 substream->group->count = 1;
1715 list_add_tail(&substream1->link_list, &substream->group->substreams);
1716 substream->group->count++;
1717 substream1->group = substream->group;
1719 write_unlock_irq(&snd_pcm_link_rwlock);
1720 up_write(&snd_pcm_link_rwsem);
1722 snd_card_unref(substream1->pcm->card);
1729 static void relink_to_local(struct snd_pcm_substream *substream)
1731 substream->group = &substream->self_group;
1732 INIT_LIST_HEAD(&substream->self_group.substreams);
1733 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1736 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1738 struct snd_pcm_substream *s;
1741 down_write(&snd_pcm_link_rwsem);
1742 write_lock_irq(&snd_pcm_link_rwlock);
1743 if (!snd_pcm_stream_linked(substream)) {
1747 list_del(&substream->link_list);
1748 substream->group->count--;
1749 if (substream->group->count == 1) { /* detach the last stream, too */
1750 snd_pcm_group_for_each_entry(s, substream) {
1754 kfree(substream->group);
1756 relink_to_local(substream);
1758 write_unlock_irq(&snd_pcm_link_rwlock);
1759 up_write(&snd_pcm_link_rwsem);
1766 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1767 struct snd_pcm_hw_rule *rule)
1769 struct snd_interval t;
1770 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1771 hw_param_interval_c(params, rule->deps[1]), &t);
1772 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1775 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1776 struct snd_pcm_hw_rule *rule)
1778 struct snd_interval t;
1779 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1780 hw_param_interval_c(params, rule->deps[1]), &t);
1781 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1784 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1785 struct snd_pcm_hw_rule *rule)
1787 struct snd_interval t;
1788 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1789 hw_param_interval_c(params, rule->deps[1]),
1790 (unsigned long) rule->private, &t);
1791 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1794 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1795 struct snd_pcm_hw_rule *rule)
1797 struct snd_interval t;
1798 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1799 (unsigned long) rule->private,
1800 hw_param_interval_c(params, rule->deps[1]), &t);
1801 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1804 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1805 struct snd_pcm_hw_rule *rule)
1808 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1810 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1812 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1814 if (! snd_mask_test(mask, k))
1816 bits = snd_pcm_format_physical_width(k);
1818 continue; /* ignore invalid formats */
1819 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1820 snd_mask_reset(&m, k);
1822 return snd_mask_refine(mask, &m);
1825 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1826 struct snd_pcm_hw_rule *rule)
1828 struct snd_interval t;
1834 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1836 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1838 bits = snd_pcm_format_physical_width(k);
1840 continue; /* ignore invalid formats */
1841 if (t.min > (unsigned)bits)
1843 if (t.max < (unsigned)bits)
1847 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1850 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1851 #error "Change this table"
1854 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1855 48000, 64000, 88200, 96000, 176400, 192000 };
1857 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1858 .count = ARRAY_SIZE(rates),
1862 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1863 struct snd_pcm_hw_rule *rule)
1865 struct snd_pcm_hardware *hw = rule->private;
1866 return snd_interval_list(hw_param_interval(params, rule->var),
1867 snd_pcm_known_rates.count,
1868 snd_pcm_known_rates.list, hw->rates);
1871 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1872 struct snd_pcm_hw_rule *rule)
1874 struct snd_interval t;
1875 struct snd_pcm_substream *substream = rule->private;
1877 t.max = substream->buffer_bytes_max;
1881 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1884 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1886 struct snd_pcm_runtime *runtime = substream->runtime;
1887 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1890 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1891 snd_mask_any(constrs_mask(constrs, k));
1894 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1895 snd_interval_any(constrs_interval(constrs, k));
1898 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1899 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1900 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1901 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1902 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1904 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1905 snd_pcm_hw_rule_format, NULL,
1906 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1909 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1910 snd_pcm_hw_rule_sample_bits, NULL,
1911 SNDRV_PCM_HW_PARAM_FORMAT,
1912 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1915 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1916 snd_pcm_hw_rule_div, NULL,
1917 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1920 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1921 snd_pcm_hw_rule_mul, NULL,
1922 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1925 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1926 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1927 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1930 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1931 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1932 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1935 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1936 snd_pcm_hw_rule_div, NULL,
1937 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1940 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1941 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1942 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1945 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1946 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1947 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1950 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1951 snd_pcm_hw_rule_div, NULL,
1952 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1955 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1956 snd_pcm_hw_rule_div, NULL,
1957 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1960 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1961 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1962 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1965 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1966 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1967 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1970 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1971 snd_pcm_hw_rule_mul, NULL,
1972 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1975 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1976 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1977 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1980 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1981 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1982 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1985 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1986 snd_pcm_hw_rule_muldivk, (void*) 8,
1987 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1990 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1991 snd_pcm_hw_rule_muldivk, (void*) 8,
1992 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1996 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1997 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2000 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2001 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2002 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2008 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2010 struct snd_pcm_runtime *runtime = substream->runtime;
2011 struct snd_pcm_hardware *hw = &runtime->hw;
2013 unsigned int mask = 0;
2015 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2016 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2017 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2018 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2019 if (hw->info & SNDRV_PCM_INFO_MMAP) {
2020 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2021 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2022 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2023 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2024 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2025 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2027 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2031 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2035 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2039 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2040 hw->channels_min, hw->channels_max);
2044 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2045 hw->rate_min, hw->rate_max);
2049 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2050 hw->period_bytes_min, hw->period_bytes_max);
2054 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2055 hw->periods_min, hw->periods_max);
2059 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2060 hw->period_bytes_min, hw->buffer_bytes_max);
2064 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2065 snd_pcm_hw_rule_buffer_bytes_max, substream,
2066 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2071 if (runtime->dma_bytes) {
2072 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2077 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2078 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2079 snd_pcm_hw_rule_rate, hw,
2080 SNDRV_PCM_HW_PARAM_RATE, -1);
2085 /* FIXME: this belong to lowlevel */
2086 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2091 static void pcm_release_private(struct snd_pcm_substream *substream)
2093 snd_pcm_unlink(substream);
2096 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2098 substream->ref_count--;
2099 if (substream->ref_count > 0)
2102 snd_pcm_drop(substream);
2103 if (substream->hw_opened) {
2104 if (substream->ops->hw_free != NULL)
2105 substream->ops->hw_free(substream);
2106 substream->ops->close(substream);
2107 substream->hw_opened = 0;
2109 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2110 pm_qos_remove_request(&substream->latency_pm_qos_req);
2111 if (substream->pcm_release) {
2112 substream->pcm_release(substream);
2113 substream->pcm_release = NULL;
2115 snd_pcm_detach_substream(substream);
2118 EXPORT_SYMBOL(snd_pcm_release_substream);
2120 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2122 struct snd_pcm_substream **rsubstream)
2124 struct snd_pcm_substream *substream;
2127 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2130 if (substream->ref_count > 1) {
2131 *rsubstream = substream;
2135 err = snd_pcm_hw_constraints_init(substream);
2137 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2141 if ((err = substream->ops->open(substream)) < 0)
2144 substream->hw_opened = 1;
2146 err = snd_pcm_hw_constraints_complete(substream);
2148 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2152 *rsubstream = substream;
2156 snd_pcm_release_substream(substream);
2160 EXPORT_SYMBOL(snd_pcm_open_substream);
2162 static int snd_pcm_open_file(struct file *file,
2163 struct snd_pcm *pcm,
2166 struct snd_pcm_file *pcm_file;
2167 struct snd_pcm_substream *substream;
2170 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2174 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2175 if (pcm_file == NULL) {
2176 snd_pcm_release_substream(substream);
2179 pcm_file->substream = substream;
2180 if (substream->ref_count == 1) {
2181 substream->file = pcm_file;
2182 substream->pcm_release = pcm_release_private;
2184 file->private_data = pcm_file;
2189 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2191 struct snd_pcm *pcm;
2192 int err = nonseekable_open(inode, file);
2195 pcm = snd_lookup_minor_data(iminor(inode),
2196 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2197 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2199 snd_card_unref(pcm->card);
2203 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2205 struct snd_pcm *pcm;
2206 int err = nonseekable_open(inode, file);
2209 pcm = snd_lookup_minor_data(iminor(inode),
2210 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2211 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2213 snd_card_unref(pcm->card);
2217 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2226 err = snd_card_file_add(pcm->card, file);
2229 if (!try_module_get(pcm->card->module)) {
2233 init_waitqueue_entry(&wait, current);
2234 add_wait_queue(&pcm->open_wait, &wait);
2235 mutex_lock(&pcm->open_mutex);
2237 err = snd_pcm_open_file(file, pcm, stream);
2240 if (err == -EAGAIN) {
2241 if (file->f_flags & O_NONBLOCK) {
2247 set_current_state(TASK_INTERRUPTIBLE);
2248 mutex_unlock(&pcm->open_mutex);
2250 mutex_lock(&pcm->open_mutex);
2251 if (pcm->card->shutdown) {
2255 if (signal_pending(current)) {
2260 remove_wait_queue(&pcm->open_wait, &wait);
2261 mutex_unlock(&pcm->open_mutex);
2267 module_put(pcm->card->module);
2269 snd_card_file_remove(pcm->card, file);
2274 static int snd_pcm_release(struct inode *inode, struct file *file)
2276 struct snd_pcm *pcm;
2277 struct snd_pcm_substream *substream;
2278 struct snd_pcm_file *pcm_file;
2280 pcm_file = file->private_data;
2281 substream = pcm_file->substream;
2282 if (snd_BUG_ON(!substream))
2284 pcm = substream->pcm;
2285 mutex_lock(&pcm->open_mutex);
2286 snd_pcm_release_substream(substream);
2288 mutex_unlock(&pcm->open_mutex);
2289 wake_up(&pcm->open_wait);
2290 module_put(pcm->card->module);
2291 snd_card_file_remove(pcm->card, file);
2295 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2296 snd_pcm_uframes_t frames)
2298 struct snd_pcm_runtime *runtime = substream->runtime;
2299 snd_pcm_sframes_t appl_ptr;
2300 snd_pcm_sframes_t ret;
2301 snd_pcm_sframes_t hw_avail;
2306 snd_pcm_stream_lock_irq(substream);
2307 switch (runtime->status->state) {
2308 case SNDRV_PCM_STATE_PREPARED:
2310 case SNDRV_PCM_STATE_DRAINING:
2311 case SNDRV_PCM_STATE_RUNNING:
2312 if (snd_pcm_update_hw_ptr(substream) >= 0)
2315 case SNDRV_PCM_STATE_XRUN:
2318 case SNDRV_PCM_STATE_SUSPENDED:
2326 hw_avail = snd_pcm_playback_hw_avail(runtime);
2327 if (hw_avail <= 0) {
2331 if (frames > (snd_pcm_uframes_t)hw_avail)
2333 appl_ptr = runtime->control->appl_ptr - frames;
2335 appl_ptr += runtime->boundary;
2336 runtime->control->appl_ptr = appl_ptr;
2339 snd_pcm_stream_unlock_irq(substream);
2343 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2344 snd_pcm_uframes_t frames)
2346 struct snd_pcm_runtime *runtime = substream->runtime;
2347 snd_pcm_sframes_t appl_ptr;
2348 snd_pcm_sframes_t ret;
2349 snd_pcm_sframes_t hw_avail;
2354 snd_pcm_stream_lock_irq(substream);
2355 switch (runtime->status->state) {
2356 case SNDRV_PCM_STATE_PREPARED:
2357 case SNDRV_PCM_STATE_DRAINING:
2359 case SNDRV_PCM_STATE_RUNNING:
2360 if (snd_pcm_update_hw_ptr(substream) >= 0)
2363 case SNDRV_PCM_STATE_XRUN:
2366 case SNDRV_PCM_STATE_SUSPENDED:
2374 hw_avail = snd_pcm_capture_hw_avail(runtime);
2375 if (hw_avail <= 0) {
2379 if (frames > (snd_pcm_uframes_t)hw_avail)
2381 appl_ptr = runtime->control->appl_ptr - frames;
2383 appl_ptr += runtime->boundary;
2384 runtime->control->appl_ptr = appl_ptr;
2387 snd_pcm_stream_unlock_irq(substream);
2391 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2392 snd_pcm_uframes_t frames)
2394 struct snd_pcm_runtime *runtime = substream->runtime;
2395 snd_pcm_sframes_t appl_ptr;
2396 snd_pcm_sframes_t ret;
2397 snd_pcm_sframes_t avail;
2402 snd_pcm_stream_lock_irq(substream);
2403 switch (runtime->status->state) {
2404 case SNDRV_PCM_STATE_PREPARED:
2405 case SNDRV_PCM_STATE_PAUSED:
2407 case SNDRV_PCM_STATE_DRAINING:
2408 case SNDRV_PCM_STATE_RUNNING:
2409 if (snd_pcm_update_hw_ptr(substream) >= 0)
2412 case SNDRV_PCM_STATE_XRUN:
2415 case SNDRV_PCM_STATE_SUSPENDED:
2423 avail = snd_pcm_playback_avail(runtime);
2428 if (frames > (snd_pcm_uframes_t)avail)
2430 appl_ptr = runtime->control->appl_ptr + frames;
2431 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2432 appl_ptr -= runtime->boundary;
2433 runtime->control->appl_ptr = appl_ptr;
2436 snd_pcm_stream_unlock_irq(substream);
2440 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2441 snd_pcm_uframes_t frames)
2443 struct snd_pcm_runtime *runtime = substream->runtime;
2444 snd_pcm_sframes_t appl_ptr;
2445 snd_pcm_sframes_t ret;
2446 snd_pcm_sframes_t avail;
2451 snd_pcm_stream_lock_irq(substream);
2452 switch (runtime->status->state) {
2453 case SNDRV_PCM_STATE_PREPARED:
2454 case SNDRV_PCM_STATE_DRAINING:
2455 case SNDRV_PCM_STATE_PAUSED:
2457 case SNDRV_PCM_STATE_RUNNING:
2458 if (snd_pcm_update_hw_ptr(substream) >= 0)
2461 case SNDRV_PCM_STATE_XRUN:
2464 case SNDRV_PCM_STATE_SUSPENDED:
2472 avail = snd_pcm_capture_avail(runtime);
2477 if (frames > (snd_pcm_uframes_t)avail)
2479 appl_ptr = runtime->control->appl_ptr + frames;
2480 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2481 appl_ptr -= runtime->boundary;
2482 runtime->control->appl_ptr = appl_ptr;
2485 snd_pcm_stream_unlock_irq(substream);
2489 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2491 struct snd_pcm_runtime *runtime = substream->runtime;
2494 snd_pcm_stream_lock_irq(substream);
2495 switch (runtime->status->state) {
2496 case SNDRV_PCM_STATE_DRAINING:
2497 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2500 case SNDRV_PCM_STATE_RUNNING:
2501 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2504 case SNDRV_PCM_STATE_PREPARED:
2505 case SNDRV_PCM_STATE_SUSPENDED:
2508 case SNDRV_PCM_STATE_XRUN:
2516 snd_pcm_stream_unlock_irq(substream);
2520 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2521 snd_pcm_sframes_t __user *res)
2523 struct snd_pcm_runtime *runtime = substream->runtime;
2525 snd_pcm_sframes_t n = 0;
2527 snd_pcm_stream_lock_irq(substream);
2528 switch (runtime->status->state) {
2529 case SNDRV_PCM_STATE_DRAINING:
2530 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2533 case SNDRV_PCM_STATE_RUNNING:
2534 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2537 case SNDRV_PCM_STATE_PREPARED:
2538 case SNDRV_PCM_STATE_SUSPENDED:
2540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2541 n = snd_pcm_playback_hw_avail(runtime);
2543 n = snd_pcm_capture_avail(runtime);
2544 n += runtime->delay;
2546 case SNDRV_PCM_STATE_XRUN:
2554 snd_pcm_stream_unlock_irq(substream);
2556 if (put_user(n, res))
2561 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2562 struct snd_pcm_sync_ptr __user *_sync_ptr)
2564 struct snd_pcm_runtime *runtime = substream->runtime;
2565 struct snd_pcm_sync_ptr sync_ptr;
2566 volatile struct snd_pcm_mmap_status *status;
2567 volatile struct snd_pcm_mmap_control *control;
2570 memset(&sync_ptr, 0, sizeof(sync_ptr));
2571 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2573 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2575 status = runtime->status;
2576 control = runtime->control;
2577 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2578 err = snd_pcm_hwsync(substream);
2582 snd_pcm_stream_lock_irq(substream);
2583 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2584 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2586 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2587 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2588 control->avail_min = sync_ptr.c.control.avail_min;
2590 sync_ptr.c.control.avail_min = control->avail_min;
2591 sync_ptr.s.status.state = status->state;
2592 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2593 sync_ptr.s.status.tstamp = status->tstamp;
2594 sync_ptr.s.status.suspended_state = status->suspended_state;
2595 snd_pcm_stream_unlock_irq(substream);
2596 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2601 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2603 struct snd_pcm_runtime *runtime = substream->runtime;
2606 if (get_user(arg, _arg))
2608 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2610 runtime->tstamp_type = arg;
2614 static int snd_pcm_common_ioctl1(struct file *file,
2615 struct snd_pcm_substream *substream,
2616 unsigned int cmd, void __user *arg)
2619 case SNDRV_PCM_IOCTL_PVERSION:
2620 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2621 case SNDRV_PCM_IOCTL_INFO:
2622 return snd_pcm_info_user(substream, arg);
2623 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2625 case SNDRV_PCM_IOCTL_TTSTAMP:
2626 return snd_pcm_tstamp(substream, arg);
2627 case SNDRV_PCM_IOCTL_HW_REFINE:
2628 return snd_pcm_hw_refine_user(substream, arg);
2629 case SNDRV_PCM_IOCTL_HW_PARAMS:
2630 return snd_pcm_hw_params_user(substream, arg);
2631 case SNDRV_PCM_IOCTL_HW_FREE:
2632 return snd_pcm_hw_free(substream);
2633 case SNDRV_PCM_IOCTL_SW_PARAMS:
2634 return snd_pcm_sw_params_user(substream, arg);
2635 case SNDRV_PCM_IOCTL_STATUS:
2636 return snd_pcm_status_user(substream, arg);
2637 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2638 return snd_pcm_channel_info_user(substream, arg);
2639 case SNDRV_PCM_IOCTL_PREPARE:
2640 return snd_pcm_prepare(substream, file);
2641 case SNDRV_PCM_IOCTL_RESET:
2642 return snd_pcm_reset(substream);
2643 case SNDRV_PCM_IOCTL_START:
2644 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2645 case SNDRV_PCM_IOCTL_LINK:
2646 return snd_pcm_link(substream, (int)(unsigned long) arg);
2647 case SNDRV_PCM_IOCTL_UNLINK:
2648 return snd_pcm_unlink(substream);
2649 case SNDRV_PCM_IOCTL_RESUME:
2650 return snd_pcm_resume(substream);
2651 case SNDRV_PCM_IOCTL_XRUN:
2652 return snd_pcm_xrun(substream);
2653 case SNDRV_PCM_IOCTL_HWSYNC:
2654 return snd_pcm_hwsync(substream);
2655 case SNDRV_PCM_IOCTL_DELAY:
2656 return snd_pcm_delay(substream, arg);
2657 case SNDRV_PCM_IOCTL_SYNC_PTR:
2658 return snd_pcm_sync_ptr(substream, arg);
2659 #ifdef CONFIG_SND_SUPPORT_OLD_API
2660 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2661 return snd_pcm_hw_refine_old_user(substream, arg);
2662 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2663 return snd_pcm_hw_params_old_user(substream, arg);
2665 case SNDRV_PCM_IOCTL_DRAIN:
2666 return snd_pcm_drain(substream, file);
2667 case SNDRV_PCM_IOCTL_DROP:
2668 return snd_pcm_drop(substream);
2669 case SNDRV_PCM_IOCTL_PAUSE:
2672 snd_pcm_stream_lock_irq(substream);
2673 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2674 snd_pcm_stream_unlock_irq(substream);
2678 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2682 static int snd_pcm_playback_ioctl1(struct file *file,
2683 struct snd_pcm_substream *substream,
2684 unsigned int cmd, void __user *arg)
2686 if (snd_BUG_ON(!substream))
2688 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2691 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2693 struct snd_xferi xferi;
2694 struct snd_xferi __user *_xferi = arg;
2695 struct snd_pcm_runtime *runtime = substream->runtime;
2696 snd_pcm_sframes_t result;
2697 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2699 if (put_user(0, &_xferi->result))
2701 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2703 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2704 __put_user(result, &_xferi->result);
2705 return result < 0 ? result : 0;
2707 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2709 struct snd_xfern xfern;
2710 struct snd_xfern __user *_xfern = arg;
2711 struct snd_pcm_runtime *runtime = substream->runtime;
2713 snd_pcm_sframes_t result;
2714 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2716 if (runtime->channels > 128)
2718 if (put_user(0, &_xfern->result))
2720 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2723 bufs = memdup_user(xfern.bufs,
2724 sizeof(void *) * runtime->channels);
2726 return PTR_ERR(bufs);
2727 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2729 __put_user(result, &_xfern->result);
2730 return result < 0 ? result : 0;
2732 case SNDRV_PCM_IOCTL_REWIND:
2734 snd_pcm_uframes_t frames;
2735 snd_pcm_uframes_t __user *_frames = arg;
2736 snd_pcm_sframes_t result;
2737 if (get_user(frames, _frames))
2739 if (put_user(0, _frames))
2741 result = snd_pcm_playback_rewind(substream, frames);
2742 __put_user(result, _frames);
2743 return result < 0 ? result : 0;
2745 case SNDRV_PCM_IOCTL_FORWARD:
2747 snd_pcm_uframes_t frames;
2748 snd_pcm_uframes_t __user *_frames = arg;
2749 snd_pcm_sframes_t result;
2750 if (get_user(frames, _frames))
2752 if (put_user(0, _frames))
2754 result = snd_pcm_playback_forward(substream, frames);
2755 __put_user(result, _frames);
2756 return result < 0 ? result : 0;
2759 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2762 static int snd_pcm_capture_ioctl1(struct file *file,
2763 struct snd_pcm_substream *substream,
2764 unsigned int cmd, void __user *arg)
2766 if (snd_BUG_ON(!substream))
2768 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2771 case SNDRV_PCM_IOCTL_READI_FRAMES:
2773 struct snd_xferi xferi;
2774 struct snd_xferi __user *_xferi = arg;
2775 struct snd_pcm_runtime *runtime = substream->runtime;
2776 snd_pcm_sframes_t result;
2777 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2779 if (put_user(0, &_xferi->result))
2781 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2783 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2784 __put_user(result, &_xferi->result);
2785 return result < 0 ? result : 0;
2787 case SNDRV_PCM_IOCTL_READN_FRAMES:
2789 struct snd_xfern xfern;
2790 struct snd_xfern __user *_xfern = arg;
2791 struct snd_pcm_runtime *runtime = substream->runtime;
2793 snd_pcm_sframes_t result;
2794 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2796 if (runtime->channels > 128)
2798 if (put_user(0, &_xfern->result))
2800 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2803 bufs = memdup_user(xfern.bufs,
2804 sizeof(void *) * runtime->channels);
2806 return PTR_ERR(bufs);
2807 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2809 __put_user(result, &_xfern->result);
2810 return result < 0 ? result : 0;
2812 case SNDRV_PCM_IOCTL_REWIND:
2814 snd_pcm_uframes_t frames;
2815 snd_pcm_uframes_t __user *_frames = arg;
2816 snd_pcm_sframes_t result;
2817 if (get_user(frames, _frames))
2819 if (put_user(0, _frames))
2821 result = snd_pcm_capture_rewind(substream, frames);
2822 __put_user(result, _frames);
2823 return result < 0 ? result : 0;
2825 case SNDRV_PCM_IOCTL_FORWARD:
2827 snd_pcm_uframes_t frames;
2828 snd_pcm_uframes_t __user *_frames = arg;
2829 snd_pcm_sframes_t result;
2830 if (get_user(frames, _frames))
2832 if (put_user(0, _frames))
2834 result = snd_pcm_capture_forward(substream, frames);
2835 __put_user(result, _frames);
2836 return result < 0 ? result : 0;
2839 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2842 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2845 struct snd_pcm_file *pcm_file;
2847 pcm_file = file->private_data;
2849 if (((cmd >> 8) & 0xff) != 'A')
2852 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2853 (void __user *)arg);
2856 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2859 struct snd_pcm_file *pcm_file;
2861 pcm_file = file->private_data;
2863 if (((cmd >> 8) & 0xff) != 'A')
2866 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2867 (void __user *)arg);
2870 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2871 unsigned int cmd, void *arg)
2876 fs = snd_enter_user();
2877 switch (substream->stream) {
2878 case SNDRV_PCM_STREAM_PLAYBACK:
2879 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2880 (void __user *)arg);
2882 case SNDRV_PCM_STREAM_CAPTURE:
2883 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2884 (void __user *)arg);
2894 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2896 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2899 struct snd_pcm_file *pcm_file;
2900 struct snd_pcm_substream *substream;
2901 struct snd_pcm_runtime *runtime;
2902 snd_pcm_sframes_t result;
2904 pcm_file = file->private_data;
2905 substream = pcm_file->substream;
2906 if (PCM_RUNTIME_CHECK(substream))
2908 runtime = substream->runtime;
2909 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2911 if (!frame_aligned(runtime, count))
2913 count = bytes_to_frames(runtime, count);
2914 result = snd_pcm_lib_read(substream, buf, count);
2916 result = frames_to_bytes(runtime, result);
2920 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2921 size_t count, loff_t * offset)
2923 struct snd_pcm_file *pcm_file;
2924 struct snd_pcm_substream *substream;
2925 struct snd_pcm_runtime *runtime;
2926 snd_pcm_sframes_t result;
2928 pcm_file = file->private_data;
2929 substream = pcm_file->substream;
2930 if (PCM_RUNTIME_CHECK(substream))
2932 runtime = substream->runtime;
2933 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2935 if (!frame_aligned(runtime, count))
2937 count = bytes_to_frames(runtime, count);
2938 result = snd_pcm_lib_write(substream, buf, count);
2940 result = frames_to_bytes(runtime, result);
2944 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2945 unsigned long nr_segs, loff_t pos)
2948 struct snd_pcm_file *pcm_file;
2949 struct snd_pcm_substream *substream;
2950 struct snd_pcm_runtime *runtime;
2951 snd_pcm_sframes_t result;
2954 snd_pcm_uframes_t frames;
2956 pcm_file = iocb->ki_filp->private_data;
2957 substream = pcm_file->substream;
2958 if (PCM_RUNTIME_CHECK(substream))
2960 runtime = substream->runtime;
2961 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2963 if (nr_segs > 1024 || nr_segs != runtime->channels)
2965 if (!frame_aligned(runtime, iov->iov_len))
2967 frames = bytes_to_samples(runtime, iov->iov_len);
2968 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2971 for (i = 0; i < nr_segs; ++i)
2972 bufs[i] = iov[i].iov_base;
2973 result = snd_pcm_lib_readv(substream, bufs, frames);
2975 result = frames_to_bytes(runtime, result);
2980 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2981 unsigned long nr_segs, loff_t pos)
2983 struct snd_pcm_file *pcm_file;
2984 struct snd_pcm_substream *substream;
2985 struct snd_pcm_runtime *runtime;
2986 snd_pcm_sframes_t result;
2989 snd_pcm_uframes_t frames;
2991 pcm_file = iocb->ki_filp->private_data;
2992 substream = pcm_file->substream;
2993 if (PCM_RUNTIME_CHECK(substream))
2995 runtime = substream->runtime;
2996 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2998 if (nr_segs > 128 || nr_segs != runtime->channels ||
2999 !frame_aligned(runtime, iov->iov_len))
3001 frames = bytes_to_samples(runtime, iov->iov_len);
3002 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3005 for (i = 0; i < nr_segs; ++i)
3006 bufs[i] = iov[i].iov_base;
3007 result = snd_pcm_lib_writev(substream, bufs, frames);
3009 result = frames_to_bytes(runtime, result);
3014 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3016 struct snd_pcm_file *pcm_file;
3017 struct snd_pcm_substream *substream;
3018 struct snd_pcm_runtime *runtime;
3020 snd_pcm_uframes_t avail;
3022 pcm_file = file->private_data;
3024 substream = pcm_file->substream;
3025 if (PCM_RUNTIME_CHECK(substream))
3027 runtime = substream->runtime;
3029 poll_wait(file, &runtime->sleep, wait);
3031 snd_pcm_stream_lock_irq(substream);
3032 avail = snd_pcm_playback_avail(runtime);
3033 switch (runtime->status->state) {
3034 case SNDRV_PCM_STATE_RUNNING:
3035 case SNDRV_PCM_STATE_PREPARED:
3036 case SNDRV_PCM_STATE_PAUSED:
3037 if (avail >= runtime->control->avail_min) {
3038 mask = POLLOUT | POLLWRNORM;
3042 case SNDRV_PCM_STATE_DRAINING:
3046 mask = POLLOUT | POLLWRNORM | POLLERR;
3049 snd_pcm_stream_unlock_irq(substream);
3053 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3055 struct snd_pcm_file *pcm_file;
3056 struct snd_pcm_substream *substream;
3057 struct snd_pcm_runtime *runtime;
3059 snd_pcm_uframes_t avail;
3061 pcm_file = file->private_data;
3063 substream = pcm_file->substream;
3064 if (PCM_RUNTIME_CHECK(substream))
3066 runtime = substream->runtime;
3068 poll_wait(file, &runtime->sleep, wait);
3070 snd_pcm_stream_lock_irq(substream);
3071 avail = snd_pcm_capture_avail(runtime);
3072 switch (runtime->status->state) {
3073 case SNDRV_PCM_STATE_RUNNING:
3074 case SNDRV_PCM_STATE_PREPARED:
3075 case SNDRV_PCM_STATE_PAUSED:
3076 if (avail >= runtime->control->avail_min) {
3077 mask = POLLIN | POLLRDNORM;
3082 case SNDRV_PCM_STATE_DRAINING:
3084 mask = POLLIN | POLLRDNORM;
3089 mask = POLLIN | POLLRDNORM | POLLERR;
3092 snd_pcm_stream_unlock_irq(substream);
3101 * Only on coherent architectures, we can mmap the status and the control records
3102 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3104 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3106 * mmap status record
3108 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3109 struct vm_fault *vmf)
3111 struct snd_pcm_substream *substream = area->vm_private_data;
3112 struct snd_pcm_runtime *runtime;
3114 if (substream == NULL)
3115 return VM_FAULT_SIGBUS;
3116 runtime = substream->runtime;
3117 vmf->page = virt_to_page(runtime->status);
3118 get_page(vmf->page);
3122 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3124 .fault = snd_pcm_mmap_status_fault,
3127 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3128 struct vm_area_struct *area)
3131 if (!(area->vm_flags & VM_READ))
3133 size = area->vm_end - area->vm_start;
3134 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3136 area->vm_ops = &snd_pcm_vm_ops_status;
3137 area->vm_private_data = substream;
3138 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3143 * mmap control record
3145 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3146 struct vm_fault *vmf)
3148 struct snd_pcm_substream *substream = area->vm_private_data;
3149 struct snd_pcm_runtime *runtime;
3151 if (substream == NULL)
3152 return VM_FAULT_SIGBUS;
3153 runtime = substream->runtime;
3154 vmf->page = virt_to_page(runtime->control);
3155 get_page(vmf->page);
3159 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3161 .fault = snd_pcm_mmap_control_fault,
3164 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3165 struct vm_area_struct *area)
3168 if (!(area->vm_flags & VM_READ))
3170 size = area->vm_end - area->vm_start;
3171 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3173 area->vm_ops = &snd_pcm_vm_ops_control;
3174 area->vm_private_data = substream;
3175 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3178 #else /* ! coherent mmap */
3180 * don't support mmap for status and control records.
3182 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3183 struct vm_area_struct *area)
3187 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3188 struct vm_area_struct *area)
3192 #endif /* coherent mmap */
3194 static inline struct page *
3195 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3197 void *vaddr = substream->runtime->dma_area + ofs;
3198 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3199 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3200 return virt_to_page(CAC_ADDR(vaddr));
3202 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3203 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3204 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3205 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3206 /* assume dma_handle set via pfn_to_phys() in
3207 * mm/dma-noncoherent.c
3209 return pfn_to_page(addr >> PAGE_SHIFT);
3212 return virt_to_page(vaddr);
3216 * fault callback for mmapping a RAM page
3218 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3219 struct vm_fault *vmf)
3221 struct snd_pcm_substream *substream = area->vm_private_data;
3222 struct snd_pcm_runtime *runtime;
3223 unsigned long offset;
3227 if (substream == NULL)
3228 return VM_FAULT_SIGBUS;
3229 runtime = substream->runtime;
3230 offset = vmf->pgoff << PAGE_SHIFT;
3231 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3232 if (offset > dma_bytes - PAGE_SIZE)
3233 return VM_FAULT_SIGBUS;
3234 if (substream->ops->page)
3235 page = substream->ops->page(substream, offset);
3237 page = snd_pcm_default_page_ops(substream, offset);
3239 return VM_FAULT_SIGBUS;
3245 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3246 .open = snd_pcm_mmap_data_open,
3247 .close = snd_pcm_mmap_data_close,
3250 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3251 .open = snd_pcm_mmap_data_open,
3252 .close = snd_pcm_mmap_data_close,
3253 .fault = snd_pcm_mmap_data_fault,
3256 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3257 /* This should be defined / handled globally! */
3259 #define ARCH_HAS_DMA_MMAP_COHERENT
3264 * mmap the DMA buffer on RAM
3266 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3267 struct vm_area_struct *area)
3269 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3270 #ifdef CONFIG_GENERIC_ALLOCATOR
3271 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3272 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3273 return remap_pfn_range(area, area->vm_start,
3274 substream->dma_buffer.addr >> PAGE_SHIFT,
3275 area->vm_end - area->vm_start, area->vm_page_prot);
3277 #endif /* CONFIG_GENERIC_ALLOCATOR */
3278 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3279 if (!substream->ops->page &&
3280 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3281 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3283 substream->runtime->dma_area,
3284 substream->runtime->dma_addr,
3285 area->vm_end - area->vm_start);
3286 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3287 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3288 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3289 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3290 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3291 /* mmap with fault handler */
3292 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3295 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3298 * mmap the DMA buffer on I/O memory area
3300 #if SNDRV_PCM_INFO_MMAP_IOMEM
3301 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3302 struct vm_area_struct *area)
3304 struct snd_pcm_runtime *runtime = substream->runtime;;
3306 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3307 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3310 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3311 #endif /* SNDRV_PCM_INFO_MMAP */
3316 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3317 struct vm_area_struct *area)
3319 struct snd_pcm_runtime *runtime;
3321 unsigned long offset;
3325 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3326 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3329 if (!(area->vm_flags & VM_READ))
3332 runtime = substream->runtime;
3333 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3335 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3337 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3338 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3340 size = area->vm_end - area->vm_start;
3341 offset = area->vm_pgoff << PAGE_SHIFT;
3342 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3343 if ((size_t)size > dma_bytes)
3345 if (offset > dma_bytes - size)
3348 area->vm_ops = &snd_pcm_vm_ops_data;
3349 area->vm_private_data = substream;
3350 if (substream->ops->mmap)
3351 err = substream->ops->mmap(substream, area);
3353 err = snd_pcm_lib_default_mmap(substream, area);
3355 atomic_inc(&substream->mmap_count);
3359 EXPORT_SYMBOL(snd_pcm_mmap_data);
3361 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3363 struct snd_pcm_file * pcm_file;
3364 struct snd_pcm_substream *substream;
3365 unsigned long offset;
3367 pcm_file = file->private_data;
3368 substream = pcm_file->substream;
3369 if (PCM_RUNTIME_CHECK(substream))
3372 offset = area->vm_pgoff << PAGE_SHIFT;
3374 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3375 if (pcm_file->no_compat_mmap)
3377 return snd_pcm_mmap_status(substream, file, area);
3378 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3379 if (pcm_file->no_compat_mmap)
3381 return snd_pcm_mmap_control(substream, file, area);
3383 return snd_pcm_mmap_data(substream, file, area);
3388 static int snd_pcm_fasync(int fd, struct file * file, int on)
3390 struct snd_pcm_file * pcm_file;
3391 struct snd_pcm_substream *substream;
3392 struct snd_pcm_runtime *runtime;
3394 pcm_file = file->private_data;
3395 substream = pcm_file->substream;
3396 if (PCM_RUNTIME_CHECK(substream))
3398 runtime = substream->runtime;
3399 return fasync_helper(fd, file, on, &runtime->fasync);
3405 #ifdef CONFIG_COMPAT
3406 #include "pcm_compat.c"
3408 #define snd_pcm_ioctl_compat NULL
3412 * To be removed helpers to keep binary compatibility
3415 #ifdef CONFIG_SND_SUPPORT_OLD_API
3416 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3417 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3419 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3420 struct snd_pcm_hw_params_old *oparams)
3424 memset(params, 0, sizeof(*params));
3425 params->flags = oparams->flags;
3426 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3427 params->masks[i].bits[0] = oparams->masks[i];
3428 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3429 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3430 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3431 params->info = oparams->info;
3432 params->msbits = oparams->msbits;
3433 params->rate_num = oparams->rate_num;
3434 params->rate_den = oparams->rate_den;
3435 params->fifo_size = oparams->fifo_size;
3438 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3439 struct snd_pcm_hw_params *params)
3443 memset(oparams, 0, sizeof(*oparams));
3444 oparams->flags = params->flags;
3445 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3446 oparams->masks[i] = params->masks[i].bits[0];
3447 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3448 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3449 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3450 oparams->info = params->info;
3451 oparams->msbits = params->msbits;
3452 oparams->rate_num = params->rate_num;
3453 oparams->rate_den = params->rate_den;
3454 oparams->fifo_size = params->fifo_size;
3457 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3458 struct snd_pcm_hw_params_old __user * _oparams)
3460 struct snd_pcm_hw_params *params;
3461 struct snd_pcm_hw_params_old *oparams = NULL;
3464 params = kmalloc(sizeof(*params), GFP_KERNEL);
3468 oparams = memdup_user(_oparams, sizeof(*oparams));
3469 if (IS_ERR(oparams)) {
3470 err = PTR_ERR(oparams);
3473 snd_pcm_hw_convert_from_old_params(params, oparams);
3474 err = snd_pcm_hw_refine(substream, params);
3475 snd_pcm_hw_convert_to_old_params(oparams, params);
3476 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3487 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3488 struct snd_pcm_hw_params_old __user * _oparams)
3490 struct snd_pcm_hw_params *params;
3491 struct snd_pcm_hw_params_old *oparams = NULL;
3494 params = kmalloc(sizeof(*params), GFP_KERNEL);
3498 oparams = memdup_user(_oparams, sizeof(*oparams));
3499 if (IS_ERR(oparams)) {
3500 err = PTR_ERR(oparams);
3503 snd_pcm_hw_convert_from_old_params(params, oparams);
3504 err = snd_pcm_hw_params(substream, params);
3505 snd_pcm_hw_convert_to_old_params(oparams, params);
3506 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3516 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3519 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3522 unsigned long pgoff,
3523 unsigned long flags)
3525 struct snd_pcm_file *pcm_file = file->private_data;
3526 struct snd_pcm_substream *substream = pcm_file->substream;
3527 struct snd_pcm_runtime *runtime = substream->runtime;
3528 unsigned long offset = pgoff << PAGE_SHIFT;
3531 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3532 return (unsigned long)runtime->status;
3533 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3534 return (unsigned long)runtime->control;
3536 return (unsigned long)runtime->dma_area + offset;
3540 # define snd_pcm_get_unmapped_area NULL
3547 const struct file_operations snd_pcm_f_ops[2] = {
3549 .owner = THIS_MODULE,
3550 .write = snd_pcm_write,
3551 .aio_write = snd_pcm_aio_write,
3552 .open = snd_pcm_playback_open,
3553 .release = snd_pcm_release,
3554 .llseek = no_llseek,
3555 .poll = snd_pcm_playback_poll,
3556 .unlocked_ioctl = snd_pcm_playback_ioctl,
3557 .compat_ioctl = snd_pcm_ioctl_compat,
3558 .mmap = snd_pcm_mmap,
3559 .fasync = snd_pcm_fasync,
3560 .get_unmapped_area = snd_pcm_get_unmapped_area,
3563 .owner = THIS_MODULE,
3564 .read = snd_pcm_read,
3565 .aio_read = snd_pcm_aio_read,
3566 .open = snd_pcm_capture_open,
3567 .release = snd_pcm_release,
3568 .llseek = no_llseek,
3569 .poll = snd_pcm_capture_poll,
3570 .unlocked_ioctl = snd_pcm_capture_ioctl,
3571 .compat_ioctl = snd_pcm_ioctl_compat,
3572 .mmap = snd_pcm_mmap,
3573 .fasync = snd_pcm_fasync,
3574 .get_unmapped_area = snd_pcm_get_unmapped_area,