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>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
78 * snd_pcm_stream_lock - Lock the PCM stream
79 * @substream: PCM substream
81 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
82 * flag of the given substream. This also takes the global link rw lock
83 * (or rw sem), too, for avoiding the race with linked streams.
85 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
87 if (substream->pcm->nonatomic) {
88 down_read(&snd_pcm_link_rwsem);
89 mutex_lock(&substream->self_group.mutex);
91 read_lock(&snd_pcm_link_rwlock);
92 spin_lock(&substream->self_group.lock);
95 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
98 * snd_pcm_stream_lock - Unlock the PCM stream
99 * @substream: PCM substream
101 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
103 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
105 if (substream->pcm->nonatomic) {
106 mutex_unlock(&substream->self_group.mutex);
107 up_read(&snd_pcm_link_rwsem);
109 spin_unlock(&substream->self_group.lock);
110 read_unlock(&snd_pcm_link_rwlock);
113 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
116 * snd_pcm_stream_lock_irq - Lock the PCM stream
117 * @substream: PCM substream
119 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
120 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
121 * as snd_pcm_stream_lock().
123 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
125 if (!substream->pcm->nonatomic)
127 snd_pcm_stream_lock(substream);
129 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
132 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
133 * @substream: PCM substream
135 * This is a counter-part of snd_pcm_stream_lock_irq().
137 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
139 snd_pcm_stream_unlock(substream);
140 if (!substream->pcm->nonatomic)
143 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
145 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
147 unsigned long flags = 0;
148 if (!substream->pcm->nonatomic)
149 local_irq_save(flags);
150 snd_pcm_stream_lock(substream);
153 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
156 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
157 * @substream: PCM substream
160 * This is a counter-part of snd_pcm_stream_lock_irqsave().
162 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
165 snd_pcm_stream_unlock(substream);
166 if (!substream->pcm->nonatomic)
167 local_irq_restore(flags);
169 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
171 static inline mm_segment_t snd_enter_user(void)
173 mm_segment_t fs = get_fs();
178 static inline void snd_leave_user(mm_segment_t fs)
185 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
187 struct snd_pcm_runtime *runtime;
188 struct snd_pcm *pcm = substream->pcm;
189 struct snd_pcm_str *pstr = substream->pstr;
191 memset(info, 0, sizeof(*info));
192 info->card = pcm->card->number;
193 info->device = pcm->device;
194 info->stream = substream->stream;
195 info->subdevice = substream->number;
196 strlcpy(info->id, pcm->id, sizeof(info->id));
197 strlcpy(info->name, pcm->name, sizeof(info->name));
198 info->dev_class = pcm->dev_class;
199 info->dev_subclass = pcm->dev_subclass;
200 info->subdevices_count = pstr->substream_count;
201 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
202 strlcpy(info->subname, substream->name, sizeof(info->subname));
203 runtime = substream->runtime;
204 /* AB: FIXME!!! This is definitely nonsense */
206 info->sync = runtime->sync;
207 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
212 int snd_pcm_info_user(struct snd_pcm_substream *substream,
213 struct snd_pcm_info __user * _info)
215 struct snd_pcm_info *info;
218 info = kmalloc(sizeof(*info), GFP_KERNEL);
221 err = snd_pcm_info(substream, info);
223 if (copy_to_user(_info, info, sizeof(*info)))
230 static bool hw_support_mmap(struct snd_pcm_substream *substream)
232 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
234 /* check architectures that return -EINVAL from dma_mmap_coherent() */
235 /* FIXME: this should be some global flag */
236 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
237 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
238 if (!substream->ops->mmap &&
239 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
248 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
249 static const char * const snd_pcm_hw_param_names[] = {
253 HW_PARAM(SAMPLE_BITS),
254 HW_PARAM(FRAME_BITS),
257 HW_PARAM(PERIOD_TIME),
258 HW_PARAM(PERIOD_SIZE),
259 HW_PARAM(PERIOD_BYTES),
261 HW_PARAM(BUFFER_TIME),
262 HW_PARAM(BUFFER_SIZE),
263 HW_PARAM(BUFFER_BYTES),
268 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
269 struct snd_pcm_hw_params *params)
272 struct snd_pcm_hardware *hw;
273 struct snd_interval *i = NULL;
274 struct snd_mask *m = NULL;
275 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
276 unsigned int rstamps[constrs->rules_num];
277 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
278 unsigned int stamp = 2;
282 params->fifo_size = 0;
283 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
285 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
286 params->rate_num = 0;
287 params->rate_den = 0;
290 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
291 m = hw_param_mask(params, k);
292 if (snd_mask_empty(m))
294 if (!(params->rmask & (1 << k)))
297 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
298 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
300 changed = snd_mask_refine(m, constrs_mask(constrs, k));
302 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
305 params->cmask |= 1 << k;
310 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
311 i = hw_param_interval(params, k);
312 if (snd_interval_empty(i))
314 if (!(params->rmask & (1 << k)))
317 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
322 i->openmin ? '(' : '[', i->min,
323 i->max, i->openmax ? ')' : ']');
326 changed = snd_interval_refine(i, constrs_interval(constrs, k));
331 pr_cont("%c%u %u%c\n",
332 i->openmin ? '(' : '[', i->min,
333 i->max, i->openmax ? ')' : ']');
336 params->cmask |= 1 << k;
341 for (k = 0; k < constrs->rules_num; k++)
343 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
344 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
347 for (k = 0; k < constrs->rules_num; k++) {
348 struct snd_pcm_hw_rule *r = &constrs->rules[k];
351 if (r->cond && !(r->cond & params->flags))
353 for (d = 0; r->deps[d] >= 0; d++) {
354 if (vstamps[r->deps[d]] > rstamps[k]) {
362 pr_debug("Rule %d [%p]: ", k, r->func);
364 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
365 if (hw_is_mask(r->var)) {
366 m = hw_param_mask(params, r->var);
367 pr_cont("%x", *m->bits);
369 i = hw_param_interval(params, r->var);
374 i->openmin ? '(' : '[', i->min,
375 i->max, i->openmax ? ')' : ']');
379 changed = r->func(params, r);
383 if (hw_is_mask(r->var))
384 pr_cont("%x", *m->bits);
390 i->openmin ? '(' : '[', i->min,
391 i->max, i->openmax ? ')' : ']');
397 if (changed && r->var >= 0) {
398 params->cmask |= (1 << r->var);
399 vstamps[r->var] = stamp;
407 if (!params->msbits) {
408 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
409 if (snd_interval_single(i))
410 params->msbits = snd_interval_value(i);
413 if (!params->rate_den) {
414 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
415 if (snd_interval_single(i)) {
416 params->rate_num = snd_interval_value(i);
417 params->rate_den = 1;
421 hw = &substream->runtime->hw;
423 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
424 SNDRV_PCM_INFO_DRAIN_TRIGGER);
425 if (!hw_support_mmap(substream))
426 params->info &= ~(SNDRV_PCM_INFO_MMAP |
427 SNDRV_PCM_INFO_MMAP_VALID);
429 if (!params->fifo_size) {
430 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
431 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
432 if (snd_mask_min(m) == snd_mask_max(m) &&
433 snd_interval_min(i) == snd_interval_max(i)) {
434 changed = substream->ops->ioctl(substream,
435 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
444 EXPORT_SYMBOL(snd_pcm_hw_refine);
446 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params __user * _params)
449 struct snd_pcm_hw_params *params;
452 params = memdup_user(_params, sizeof(*params));
454 return PTR_ERR(params);
456 err = snd_pcm_hw_refine(substream, params);
457 if (copy_to_user(_params, params, sizeof(*params))) {
466 static int period_to_usecs(struct snd_pcm_runtime *runtime)
471 return -1; /* invalid */
473 /* take 75% of period time as the deadline */
474 usecs = (750000 / runtime->rate) * runtime->period_size;
475 usecs += ((750000 % runtime->rate) * runtime->period_size) /
481 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
483 snd_pcm_stream_lock_irq(substream);
484 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
485 substream->runtime->status->state = state;
486 snd_pcm_stream_unlock_irq(substream);
489 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
490 struct snd_pcm_hw_params *params)
492 struct snd_pcm_runtime *runtime;
495 snd_pcm_uframes_t frames;
497 if (PCM_RUNTIME_CHECK(substream))
499 runtime = substream->runtime;
500 snd_pcm_stream_lock_irq(substream);
501 switch (runtime->status->state) {
502 case SNDRV_PCM_STATE_OPEN:
503 case SNDRV_PCM_STATE_SETUP:
504 case SNDRV_PCM_STATE_PREPARED:
507 snd_pcm_stream_unlock_irq(substream);
510 snd_pcm_stream_unlock_irq(substream);
511 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
512 if (!substream->oss.oss)
514 if (atomic_read(&substream->mmap_count))
518 err = snd_pcm_hw_refine(substream, params);
522 err = snd_pcm_hw_params_choose(substream, params);
526 if (substream->ops->hw_params != NULL) {
527 err = substream->ops->hw_params(substream, params);
532 runtime->access = params_access(params);
533 runtime->format = params_format(params);
534 runtime->subformat = params_subformat(params);
535 runtime->channels = params_channels(params);
536 runtime->rate = params_rate(params);
537 runtime->period_size = params_period_size(params);
538 runtime->periods = params_periods(params);
539 runtime->buffer_size = params_buffer_size(params);
540 runtime->info = params->info;
541 runtime->rate_num = params->rate_num;
542 runtime->rate_den = params->rate_den;
543 runtime->no_period_wakeup =
544 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
545 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
547 bits = snd_pcm_format_physical_width(runtime->format);
548 runtime->sample_bits = bits;
549 bits *= runtime->channels;
550 runtime->frame_bits = bits;
552 while (bits % 8 != 0) {
556 runtime->byte_align = bits / 8;
557 runtime->min_align = frames;
559 /* Default sw params */
560 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
561 runtime->period_step = 1;
562 runtime->control->avail_min = runtime->period_size;
563 runtime->start_threshold = 1;
564 runtime->stop_threshold = runtime->buffer_size;
565 runtime->silence_threshold = 0;
566 runtime->silence_size = 0;
567 runtime->boundary = runtime->buffer_size;
568 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
569 runtime->boundary *= 2;
571 snd_pcm_timer_resolution_change(substream);
572 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
574 if (pm_qos_request_active(&substream->latency_pm_qos_req))
575 pm_qos_remove_request(&substream->latency_pm_qos_req);
576 if ((usecs = period_to_usecs(runtime)) >= 0)
577 pm_qos_add_request(&substream->latency_pm_qos_req,
578 PM_QOS_CPU_DMA_LATENCY, usecs);
581 /* hardware might be unusable from this time,
582 so we force application to retry to set
583 the correct hardware parameter settings */
584 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
585 if (substream->ops->hw_free != NULL)
586 substream->ops->hw_free(substream);
590 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
591 struct snd_pcm_hw_params __user * _params)
593 struct snd_pcm_hw_params *params;
596 params = memdup_user(_params, sizeof(*params));
598 return PTR_ERR(params);
600 err = snd_pcm_hw_params(substream, params);
601 if (copy_to_user(_params, params, sizeof(*params))) {
610 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
612 struct snd_pcm_runtime *runtime;
615 if (PCM_RUNTIME_CHECK(substream))
617 runtime = substream->runtime;
618 snd_pcm_stream_lock_irq(substream);
619 switch (runtime->status->state) {
620 case SNDRV_PCM_STATE_SETUP:
621 case SNDRV_PCM_STATE_PREPARED:
624 snd_pcm_stream_unlock_irq(substream);
627 snd_pcm_stream_unlock_irq(substream);
628 if (atomic_read(&substream->mmap_count))
630 if (substream->ops->hw_free)
631 result = substream->ops->hw_free(substream);
632 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
633 pm_qos_remove_request(&substream->latency_pm_qos_req);
637 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
638 struct snd_pcm_sw_params *params)
640 struct snd_pcm_runtime *runtime;
643 if (PCM_RUNTIME_CHECK(substream))
645 runtime = substream->runtime;
646 snd_pcm_stream_lock_irq(substream);
647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
648 snd_pcm_stream_unlock_irq(substream);
651 snd_pcm_stream_unlock_irq(substream);
653 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
655 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
656 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
658 if (params->avail_min == 0)
660 if (params->silence_size >= runtime->boundary) {
661 if (params->silence_threshold != 0)
664 if (params->silence_size > params->silence_threshold)
666 if (params->silence_threshold > runtime->buffer_size)
670 snd_pcm_stream_lock_irq(substream);
671 runtime->tstamp_mode = params->tstamp_mode;
672 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
673 runtime->tstamp_type = params->tstamp_type;
674 runtime->period_step = params->period_step;
675 runtime->control->avail_min = params->avail_min;
676 runtime->start_threshold = params->start_threshold;
677 runtime->stop_threshold = params->stop_threshold;
678 runtime->silence_threshold = params->silence_threshold;
679 runtime->silence_size = params->silence_size;
680 params->boundary = runtime->boundary;
681 if (snd_pcm_running(substream)) {
682 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
683 runtime->silence_size > 0)
684 snd_pcm_playback_silence(substream, ULONG_MAX);
685 err = snd_pcm_update_state(substream, runtime);
687 snd_pcm_stream_unlock_irq(substream);
691 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
692 struct snd_pcm_sw_params __user * _params)
694 struct snd_pcm_sw_params params;
696 if (copy_from_user(¶ms, _params, sizeof(params)))
698 err = snd_pcm_sw_params(substream, ¶ms);
699 if (copy_to_user(_params, ¶ms, sizeof(params)))
704 int snd_pcm_status(struct snd_pcm_substream *substream,
705 struct snd_pcm_status *status)
707 struct snd_pcm_runtime *runtime = substream->runtime;
709 snd_pcm_stream_lock_irq(substream);
710 status->state = runtime->status->state;
711 status->suspended_state = runtime->status->suspended_state;
712 if (status->state == SNDRV_PCM_STATE_OPEN)
714 status->trigger_tstamp = runtime->trigger_tstamp;
715 if (snd_pcm_running(substream)) {
716 snd_pcm_update_hw_ptr(substream);
717 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
718 status->tstamp = runtime->status->tstamp;
719 status->audio_tstamp =
720 runtime->status->audio_tstamp;
724 /* get tstamp only in fallback mode and only if enabled */
725 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
726 snd_pcm_gettime(runtime, &status->tstamp);
729 status->appl_ptr = runtime->control->appl_ptr;
730 status->hw_ptr = runtime->status->hw_ptr;
731 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
732 status->avail = snd_pcm_playback_avail(runtime);
733 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
734 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
735 status->delay = runtime->buffer_size - status->avail;
736 status->delay += runtime->delay;
740 status->avail = snd_pcm_capture_avail(runtime);
741 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
742 status->delay = status->avail + runtime->delay;
746 status->avail_max = runtime->avail_max;
747 status->overrange = runtime->overrange;
748 runtime->avail_max = 0;
749 runtime->overrange = 0;
751 snd_pcm_stream_unlock_irq(substream);
755 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
756 struct snd_pcm_status __user * _status)
758 struct snd_pcm_status status;
761 memset(&status, 0, sizeof(status));
762 res = snd_pcm_status(substream, &status);
765 if (copy_to_user(_status, &status, sizeof(status)))
770 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
771 struct snd_pcm_channel_info * info)
773 struct snd_pcm_runtime *runtime;
774 unsigned int channel;
776 channel = info->channel;
777 runtime = substream->runtime;
778 snd_pcm_stream_lock_irq(substream);
779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
780 snd_pcm_stream_unlock_irq(substream);
783 snd_pcm_stream_unlock_irq(substream);
784 if (channel >= runtime->channels)
786 memset(info, 0, sizeof(*info));
787 info->channel = channel;
788 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
791 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
792 struct snd_pcm_channel_info __user * _info)
794 struct snd_pcm_channel_info info;
797 if (copy_from_user(&info, _info, sizeof(info)))
799 res = snd_pcm_channel_info(substream, &info);
802 if (copy_to_user(_info, &info, sizeof(info)))
807 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
809 struct snd_pcm_runtime *runtime = substream->runtime;
810 if (runtime->trigger_master == NULL)
812 if (runtime->trigger_master == substream) {
813 if (!runtime->trigger_tstamp_latched)
814 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
816 snd_pcm_trigger_tstamp(runtime->trigger_master);
817 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
819 runtime->trigger_master = NULL;
823 int (*pre_action)(struct snd_pcm_substream *substream, int state);
824 int (*do_action)(struct snd_pcm_substream *substream, int state);
825 void (*undo_action)(struct snd_pcm_substream *substream, int state);
826 void (*post_action)(struct snd_pcm_substream *substream, int state);
830 * this functions is core for handling of linked stream
831 * Note: the stream state might be changed also on failure
832 * Note2: call with calling stream lock + link lock
834 static int snd_pcm_action_group(struct action_ops *ops,
835 struct snd_pcm_substream *substream,
836 int state, int do_lock)
838 struct snd_pcm_substream *s = NULL;
839 struct snd_pcm_substream *s1;
840 int res = 0, depth = 1;
842 snd_pcm_group_for_each_entry(s, substream) {
843 if (do_lock && s != substream) {
844 if (s->pcm->nonatomic)
845 mutex_lock_nested(&s->self_group.mutex, depth);
847 spin_lock_nested(&s->self_group.lock, depth);
850 res = ops->pre_action(s, state);
854 snd_pcm_group_for_each_entry(s, substream) {
855 res = ops->do_action(s, state);
857 if (ops->undo_action) {
858 snd_pcm_group_for_each_entry(s1, substream) {
859 if (s1 == s) /* failed stream */
861 ops->undo_action(s1, state);
864 s = NULL; /* unlock all */
868 snd_pcm_group_for_each_entry(s, substream) {
869 ops->post_action(s, state);
874 snd_pcm_group_for_each_entry(s1, substream) {
875 if (s1 != substream) {
876 if (s1->pcm->nonatomic)
877 mutex_unlock(&s1->self_group.mutex);
879 spin_unlock(&s1->self_group.lock);
881 if (s1 == s) /* end */
889 * Note: call with stream lock
891 static int snd_pcm_action_single(struct action_ops *ops,
892 struct snd_pcm_substream *substream,
897 res = ops->pre_action(substream, state);
900 res = ops->do_action(substream, state);
902 ops->post_action(substream, state);
903 else if (ops->undo_action)
904 ops->undo_action(substream, state);
909 * Note: call with stream lock
911 static int snd_pcm_action(struct action_ops *ops,
912 struct snd_pcm_substream *substream,
917 if (!snd_pcm_stream_linked(substream))
918 return snd_pcm_action_single(ops, substream, state);
920 if (substream->pcm->nonatomic) {
921 if (!mutex_trylock(&substream->group->mutex)) {
922 mutex_unlock(&substream->self_group.mutex);
923 mutex_lock(&substream->group->mutex);
924 mutex_lock(&substream->self_group.mutex);
926 res = snd_pcm_action_group(ops, substream, state, 1);
927 mutex_unlock(&substream->group->mutex);
929 if (!spin_trylock(&substream->group->lock)) {
930 spin_unlock(&substream->self_group.lock);
931 spin_lock(&substream->group->lock);
932 spin_lock(&substream->self_group.lock);
934 res = snd_pcm_action_group(ops, substream, state, 1);
935 spin_unlock(&substream->group->lock);
941 * Note: don't use any locks before
943 static int snd_pcm_action_lock_irq(struct action_ops *ops,
944 struct snd_pcm_substream *substream,
949 snd_pcm_stream_lock_irq(substream);
950 res = snd_pcm_action(ops, substream, state);
951 snd_pcm_stream_unlock_irq(substream);
957 static int snd_pcm_action_nonatomic(struct action_ops *ops,
958 struct snd_pcm_substream *substream,
963 down_read(&snd_pcm_link_rwsem);
964 if (snd_pcm_stream_linked(substream))
965 res = snd_pcm_action_group(ops, substream, state, 0);
967 res = snd_pcm_action_single(ops, substream, state);
968 up_read(&snd_pcm_link_rwsem);
975 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
977 struct snd_pcm_runtime *runtime = substream->runtime;
978 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
980 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
981 !snd_pcm_playback_data(substream))
983 runtime->trigger_tstamp_latched = false;
984 runtime->trigger_master = substream;
988 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
990 if (substream->runtime->trigger_master != substream)
992 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
995 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
997 if (substream->runtime->trigger_master == substream)
998 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1001 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1003 struct snd_pcm_runtime *runtime = substream->runtime;
1004 snd_pcm_trigger_tstamp(substream);
1005 runtime->hw_ptr_jiffies = jiffies;
1006 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1008 runtime->status->state = state;
1009 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1010 runtime->silence_size > 0)
1011 snd_pcm_playback_silence(substream, ULONG_MAX);
1012 if (substream->timer)
1013 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1014 &runtime->trigger_tstamp);
1017 static struct action_ops snd_pcm_action_start = {
1018 .pre_action = snd_pcm_pre_start,
1019 .do_action = snd_pcm_do_start,
1020 .undo_action = snd_pcm_undo_start,
1021 .post_action = snd_pcm_post_start
1025 * snd_pcm_start - start all linked streams
1026 * @substream: the PCM substream instance
1028 * Return: Zero if successful, or a negative error code.
1030 int snd_pcm_start(struct snd_pcm_substream *substream)
1032 return snd_pcm_action(&snd_pcm_action_start, substream,
1033 SNDRV_PCM_STATE_RUNNING);
1039 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1041 struct snd_pcm_runtime *runtime = substream->runtime;
1042 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1044 runtime->trigger_master = substream;
1048 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1050 if (substream->runtime->trigger_master == substream &&
1051 snd_pcm_running(substream))
1052 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1053 return 0; /* unconditonally stop all substreams */
1056 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1058 struct snd_pcm_runtime *runtime = substream->runtime;
1059 if (runtime->status->state != state) {
1060 snd_pcm_trigger_tstamp(substream);
1061 runtime->status->state = state;
1062 if (substream->timer)
1063 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1064 &runtime->trigger_tstamp);
1066 wake_up(&runtime->sleep);
1067 wake_up(&runtime->tsleep);
1070 static struct action_ops snd_pcm_action_stop = {
1071 .pre_action = snd_pcm_pre_stop,
1072 .do_action = snd_pcm_do_stop,
1073 .post_action = snd_pcm_post_stop
1077 * snd_pcm_stop - try to stop all running streams in the substream group
1078 * @substream: the PCM substream instance
1079 * @state: PCM state after stopping the stream
1081 * The state of each stream is then changed to the given state unconditionally.
1083 * Return: Zero if successful, or a negative error code.
1085 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1087 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1090 EXPORT_SYMBOL(snd_pcm_stop);
1093 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1094 * @substream: the PCM substream
1096 * After stopping, the state is changed to SETUP.
1097 * Unlike snd_pcm_stop(), this affects only the given stream.
1099 * Return: Zero if succesful, or a negative error code.
1101 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1103 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1104 SNDRV_PCM_STATE_SETUP);
1108 * snd_pcm_stop_xrun - stop the running streams as XRUN
1109 * @substream: the PCM substream instance
1111 * This stops the given running substream (and all linked substreams) as XRUN.
1112 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1114 * Return: Zero if successful, or a negative error code.
1116 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1118 unsigned long flags;
1121 snd_pcm_stream_lock_irqsave(substream, flags);
1122 if (snd_pcm_running(substream))
1123 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1124 snd_pcm_stream_unlock_irqrestore(substream, flags);
1127 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1132 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1134 struct snd_pcm_runtime *runtime = substream->runtime;
1135 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1138 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1140 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1142 runtime->trigger_master = substream;
1146 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1148 if (substream->runtime->trigger_master != substream)
1150 /* some drivers might use hw_ptr to recover from the pause -
1151 update the hw_ptr now */
1153 snd_pcm_update_hw_ptr(substream);
1154 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1155 * a delta between the current jiffies, this gives a large enough
1156 * delta, effectively to skip the check once.
1158 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1159 return substream->ops->trigger(substream,
1160 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1161 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1164 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1166 if (substream->runtime->trigger_master == substream)
1167 substream->ops->trigger(substream,
1168 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1169 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1172 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1174 struct snd_pcm_runtime *runtime = substream->runtime;
1175 snd_pcm_trigger_tstamp(substream);
1177 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1178 if (substream->timer)
1179 snd_timer_notify(substream->timer,
1180 SNDRV_TIMER_EVENT_MPAUSE,
1181 &runtime->trigger_tstamp);
1182 wake_up(&runtime->sleep);
1183 wake_up(&runtime->tsleep);
1185 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1186 if (substream->timer)
1187 snd_timer_notify(substream->timer,
1188 SNDRV_TIMER_EVENT_MCONTINUE,
1189 &runtime->trigger_tstamp);
1193 static struct action_ops snd_pcm_action_pause = {
1194 .pre_action = snd_pcm_pre_pause,
1195 .do_action = snd_pcm_do_pause,
1196 .undo_action = snd_pcm_undo_pause,
1197 .post_action = snd_pcm_post_pause
1201 * Push/release the pause for all linked streams.
1203 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1205 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1211 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1213 struct snd_pcm_runtime *runtime = substream->runtime;
1214 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1216 runtime->trigger_master = substream;
1220 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1222 struct snd_pcm_runtime *runtime = substream->runtime;
1223 if (runtime->trigger_master != substream)
1225 if (! snd_pcm_running(substream))
1227 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1228 return 0; /* suspend unconditionally */
1231 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1233 struct snd_pcm_runtime *runtime = substream->runtime;
1234 snd_pcm_trigger_tstamp(substream);
1235 runtime->status->suspended_state = runtime->status->state;
1236 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1237 if (substream->timer)
1238 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1239 &runtime->trigger_tstamp);
1240 wake_up(&runtime->sleep);
1241 wake_up(&runtime->tsleep);
1244 static struct action_ops snd_pcm_action_suspend = {
1245 .pre_action = snd_pcm_pre_suspend,
1246 .do_action = snd_pcm_do_suspend,
1247 .post_action = snd_pcm_post_suspend
1251 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1252 * @substream: the PCM substream
1254 * After this call, all streams are changed to SUSPENDED state.
1256 * Return: Zero if successful (or @substream is %NULL), or a negative error
1259 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1262 unsigned long flags;
1267 snd_pcm_stream_lock_irqsave(substream, flags);
1268 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1269 snd_pcm_stream_unlock_irqrestore(substream, flags);
1273 EXPORT_SYMBOL(snd_pcm_suspend);
1276 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1277 * @pcm: the PCM instance
1279 * After this call, all streams are changed to SUSPENDED state.
1281 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1283 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1285 struct snd_pcm_substream *substream;
1286 int stream, err = 0;
1291 for (stream = 0; stream < 2; stream++) {
1292 for (substream = pcm->streams[stream].substream;
1293 substream; substream = substream->next) {
1294 /* FIXME: the open/close code should lock this as well */
1295 if (substream->runtime == NULL)
1297 err = snd_pcm_suspend(substream);
1298 if (err < 0 && err != -EBUSY)
1305 EXPORT_SYMBOL(snd_pcm_suspend_all);
1309 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1311 struct snd_pcm_runtime *runtime = substream->runtime;
1312 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1314 runtime->trigger_master = substream;
1318 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1320 struct snd_pcm_runtime *runtime = substream->runtime;
1321 if (runtime->trigger_master != substream)
1323 /* DMA not running previously? */
1324 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1325 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1326 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1328 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1331 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1333 if (substream->runtime->trigger_master == substream &&
1334 snd_pcm_running(substream))
1335 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1338 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1340 struct snd_pcm_runtime *runtime = substream->runtime;
1341 snd_pcm_trigger_tstamp(substream);
1342 runtime->status->state = runtime->status->suspended_state;
1343 if (substream->timer)
1344 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1345 &runtime->trigger_tstamp);
1348 static struct action_ops snd_pcm_action_resume = {
1349 .pre_action = snd_pcm_pre_resume,
1350 .do_action = snd_pcm_do_resume,
1351 .undo_action = snd_pcm_undo_resume,
1352 .post_action = snd_pcm_post_resume
1355 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1357 struct snd_card *card = substream->pcm->card;
1360 snd_power_lock(card);
1361 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1362 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1363 snd_power_unlock(card);
1369 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1374 #endif /* CONFIG_PM */
1379 * Change the RUNNING stream(s) to XRUN state.
1381 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1383 struct snd_card *card = substream->pcm->card;
1384 struct snd_pcm_runtime *runtime = substream->runtime;
1387 snd_power_lock(card);
1388 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1389 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1394 snd_pcm_stream_lock_irq(substream);
1395 switch (runtime->status->state) {
1396 case SNDRV_PCM_STATE_XRUN:
1397 result = 0; /* already there */
1399 case SNDRV_PCM_STATE_RUNNING:
1400 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1405 snd_pcm_stream_unlock_irq(substream);
1407 snd_power_unlock(card);
1414 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1416 struct snd_pcm_runtime *runtime = substream->runtime;
1417 switch (runtime->status->state) {
1418 case SNDRV_PCM_STATE_RUNNING:
1419 case SNDRV_PCM_STATE_PREPARED:
1420 case SNDRV_PCM_STATE_PAUSED:
1421 case SNDRV_PCM_STATE_SUSPENDED:
1428 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1430 struct snd_pcm_runtime *runtime = substream->runtime;
1431 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1434 runtime->hw_ptr_base = 0;
1435 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1436 runtime->status->hw_ptr % runtime->period_size;
1437 runtime->silence_start = runtime->status->hw_ptr;
1438 runtime->silence_filled = 0;
1442 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1444 struct snd_pcm_runtime *runtime = substream->runtime;
1445 runtime->control->appl_ptr = runtime->status->hw_ptr;
1446 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1447 runtime->silence_size > 0)
1448 snd_pcm_playback_silence(substream, ULONG_MAX);
1451 static struct action_ops snd_pcm_action_reset = {
1452 .pre_action = snd_pcm_pre_reset,
1453 .do_action = snd_pcm_do_reset,
1454 .post_action = snd_pcm_post_reset
1457 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1459 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1465 /* we use the second argument for updating f_flags */
1466 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1469 struct snd_pcm_runtime *runtime = substream->runtime;
1470 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1471 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1473 if (snd_pcm_running(substream))
1475 substream->f_flags = f_flags;
1479 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1482 err = substream->ops->prepare(substream);
1485 return snd_pcm_do_reset(substream, 0);
1488 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1490 struct snd_pcm_runtime *runtime = substream->runtime;
1491 runtime->control->appl_ptr = runtime->status->hw_ptr;
1492 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1495 static struct action_ops snd_pcm_action_prepare = {
1496 .pre_action = snd_pcm_pre_prepare,
1497 .do_action = snd_pcm_do_prepare,
1498 .post_action = snd_pcm_post_prepare
1502 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1503 * @substream: the PCM substream instance
1504 * @file: file to refer f_flags
1506 * Return: Zero if successful, or a negative error code.
1508 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1512 struct snd_card *card = substream->pcm->card;
1516 f_flags = file->f_flags;
1518 f_flags = substream->f_flags;
1520 snd_power_lock(card);
1521 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1522 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1523 substream, f_flags);
1524 snd_power_unlock(card);
1532 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1534 struct snd_pcm_runtime *runtime = substream->runtime;
1535 switch (runtime->status->state) {
1536 case SNDRV_PCM_STATE_OPEN:
1537 case SNDRV_PCM_STATE_DISCONNECTED:
1538 case SNDRV_PCM_STATE_SUSPENDED:
1541 runtime->trigger_master = substream;
1545 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1547 struct snd_pcm_runtime *runtime = substream->runtime;
1548 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1549 switch (runtime->status->state) {
1550 case SNDRV_PCM_STATE_PREPARED:
1551 /* start playback stream if possible */
1552 if (! snd_pcm_playback_empty(substream)) {
1553 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1554 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1557 case SNDRV_PCM_STATE_RUNNING:
1558 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1560 case SNDRV_PCM_STATE_XRUN:
1561 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1567 /* stop running stream */
1568 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1569 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1570 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1571 snd_pcm_do_stop(substream, new_state);
1572 snd_pcm_post_stop(substream, new_state);
1576 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1577 runtime->trigger_master == substream &&
1578 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1579 return substream->ops->trigger(substream,
1580 SNDRV_PCM_TRIGGER_DRAIN);
1585 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1589 static struct action_ops snd_pcm_action_drain_init = {
1590 .pre_action = snd_pcm_pre_drain_init,
1591 .do_action = snd_pcm_do_drain_init,
1592 .post_action = snd_pcm_post_drain_init
1595 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1598 * Drain the stream(s).
1599 * When the substream is linked, sync until the draining of all playback streams
1601 * After this call, all streams are supposed to be either SETUP or DRAINING
1602 * (capture only) state.
1604 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1607 struct snd_card *card;
1608 struct snd_pcm_runtime *runtime;
1609 struct snd_pcm_substream *s;
1614 card = substream->pcm->card;
1615 runtime = substream->runtime;
1617 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1620 snd_power_lock(card);
1621 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1622 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1624 snd_power_unlock(card);
1630 if (file->f_flags & O_NONBLOCK)
1632 } else if (substream->f_flags & O_NONBLOCK)
1635 down_read(&snd_pcm_link_rwsem);
1636 snd_pcm_stream_lock_irq(substream);
1638 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1639 snd_pcm_pause(substream, 0);
1641 /* pre-start/stop - all running streams are changed to DRAINING state */
1642 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1645 /* in non-blocking, we don't wait in ioctl but let caller poll */
1653 struct snd_pcm_runtime *to_check;
1654 if (signal_pending(current)) {
1655 result = -ERESTARTSYS;
1658 /* find a substream to drain */
1660 snd_pcm_group_for_each_entry(s, substream) {
1661 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1663 runtime = s->runtime;
1664 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1670 break; /* all drained */
1671 init_waitqueue_entry(&wait, current);
1672 add_wait_queue(&to_check->sleep, &wait);
1673 snd_pcm_stream_unlock_irq(substream);
1674 up_read(&snd_pcm_link_rwsem);
1675 snd_power_unlock(card);
1676 if (runtime->no_period_wakeup)
1677 tout = MAX_SCHEDULE_TIMEOUT;
1680 if (runtime->rate) {
1681 long t = runtime->period_size * 2 / runtime->rate;
1682 tout = max(t, tout);
1684 tout = msecs_to_jiffies(tout * 1000);
1686 tout = schedule_timeout_interruptible(tout);
1687 snd_power_lock(card);
1688 down_read(&snd_pcm_link_rwsem);
1689 snd_pcm_stream_lock_irq(substream);
1690 remove_wait_queue(&to_check->sleep, &wait);
1691 if (card->shutdown) {
1696 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1699 dev_dbg(substream->pcm->card->dev,
1700 "playback drain error (DMA or IRQ trouble?)\n");
1701 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1709 snd_pcm_stream_unlock_irq(substream);
1710 up_read(&snd_pcm_link_rwsem);
1711 snd_power_unlock(card);
1719 * Immediately put all linked substreams into SETUP state.
1721 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1723 struct snd_pcm_runtime *runtime;
1726 if (PCM_RUNTIME_CHECK(substream))
1728 runtime = substream->runtime;
1730 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1731 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1732 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1735 snd_pcm_stream_lock_irq(substream);
1737 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1738 snd_pcm_pause(substream, 0);
1740 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1741 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1742 snd_pcm_stream_unlock_irq(substream);
1748 static bool is_pcm_file(struct file *file)
1750 struct inode *inode = file_inode(file);
1753 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1755 minor = iminor(inode);
1756 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1757 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1763 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1766 struct snd_pcm_file *pcm_file;
1767 struct snd_pcm_substream *substream1;
1768 struct snd_pcm_group *group;
1769 struct fd f = fdget(fd);
1773 if (!is_pcm_file(f.file)) {
1777 pcm_file = f.file->private_data;
1778 substream1 = pcm_file->substream;
1779 group = kmalloc(sizeof(*group), GFP_KERNEL);
1784 down_write(&snd_pcm_link_rwsem);
1785 write_lock_irq(&snd_pcm_link_rwlock);
1786 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1787 substream->runtime->status->state != substream1->runtime->status->state ||
1788 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1792 if (snd_pcm_stream_linked(substream1)) {
1796 if (!snd_pcm_stream_linked(substream)) {
1797 substream->group = group;
1799 spin_lock_init(&substream->group->lock);
1800 mutex_init(&substream->group->mutex);
1801 INIT_LIST_HEAD(&substream->group->substreams);
1802 list_add_tail(&substream->link_list, &substream->group->substreams);
1803 substream->group->count = 1;
1805 list_add_tail(&substream1->link_list, &substream->group->substreams);
1806 substream->group->count++;
1807 substream1->group = substream->group;
1809 write_unlock_irq(&snd_pcm_link_rwlock);
1810 up_write(&snd_pcm_link_rwsem);
1812 snd_card_unref(substream1->pcm->card);
1819 static void relink_to_local(struct snd_pcm_substream *substream)
1821 substream->group = &substream->self_group;
1822 INIT_LIST_HEAD(&substream->self_group.substreams);
1823 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1826 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1828 struct snd_pcm_substream *s;
1831 down_write(&snd_pcm_link_rwsem);
1832 write_lock_irq(&snd_pcm_link_rwlock);
1833 if (!snd_pcm_stream_linked(substream)) {
1837 list_del(&substream->link_list);
1838 substream->group->count--;
1839 if (substream->group->count == 1) { /* detach the last stream, too */
1840 snd_pcm_group_for_each_entry(s, substream) {
1844 kfree(substream->group);
1846 relink_to_local(substream);
1848 write_unlock_irq(&snd_pcm_link_rwlock);
1849 up_write(&snd_pcm_link_rwsem);
1856 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1857 struct snd_pcm_hw_rule *rule)
1859 struct snd_interval t;
1860 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1861 hw_param_interval_c(params, rule->deps[1]), &t);
1862 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1865 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1866 struct snd_pcm_hw_rule *rule)
1868 struct snd_interval t;
1869 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1870 hw_param_interval_c(params, rule->deps[1]), &t);
1871 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1874 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1875 struct snd_pcm_hw_rule *rule)
1877 struct snd_interval t;
1878 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1879 hw_param_interval_c(params, rule->deps[1]),
1880 (unsigned long) rule->private, &t);
1881 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1884 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1885 struct snd_pcm_hw_rule *rule)
1887 struct snd_interval t;
1888 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1889 (unsigned long) rule->private,
1890 hw_param_interval_c(params, rule->deps[1]), &t);
1891 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1894 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1895 struct snd_pcm_hw_rule *rule)
1898 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1900 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1902 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1904 if (! snd_mask_test(mask, k))
1906 bits = snd_pcm_format_physical_width(k);
1908 continue; /* ignore invalid formats */
1909 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1910 snd_mask_reset(&m, k);
1912 return snd_mask_refine(mask, &m);
1915 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1916 struct snd_pcm_hw_rule *rule)
1918 struct snd_interval t;
1924 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1926 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1928 bits = snd_pcm_format_physical_width(k);
1930 continue; /* ignore invalid formats */
1931 if (t.min > (unsigned)bits)
1933 if (t.max < (unsigned)bits)
1937 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1940 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1941 #error "Change this table"
1944 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1945 48000, 64000, 88200, 96000, 176400, 192000 };
1947 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1948 .count = ARRAY_SIZE(rates),
1952 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1953 struct snd_pcm_hw_rule *rule)
1955 struct snd_pcm_hardware *hw = rule->private;
1956 return snd_interval_list(hw_param_interval(params, rule->var),
1957 snd_pcm_known_rates.count,
1958 snd_pcm_known_rates.list, hw->rates);
1961 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1962 struct snd_pcm_hw_rule *rule)
1964 struct snd_interval t;
1965 struct snd_pcm_substream *substream = rule->private;
1967 t.max = substream->buffer_bytes_max;
1971 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1974 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1976 struct snd_pcm_runtime *runtime = substream->runtime;
1977 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1980 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1981 snd_mask_any(constrs_mask(constrs, k));
1984 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1985 snd_interval_any(constrs_interval(constrs, k));
1988 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1989 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1990 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1991 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1992 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1994 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1995 snd_pcm_hw_rule_format, NULL,
1996 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1999 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2000 snd_pcm_hw_rule_sample_bits, NULL,
2001 SNDRV_PCM_HW_PARAM_FORMAT,
2002 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2005 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2006 snd_pcm_hw_rule_div, NULL,
2007 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2010 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2011 snd_pcm_hw_rule_mul, NULL,
2012 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2015 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2016 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2017 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2020 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2021 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2022 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2025 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2026 snd_pcm_hw_rule_div, NULL,
2027 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2030 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2031 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2032 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2035 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2036 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2037 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2041 snd_pcm_hw_rule_div, NULL,
2042 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2046 snd_pcm_hw_rule_div, NULL,
2047 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2051 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2052 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2056 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2057 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2061 snd_pcm_hw_rule_mul, NULL,
2062 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2067 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2071 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2076 snd_pcm_hw_rule_muldivk, (void*) 8,
2077 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2081 snd_pcm_hw_rule_muldivk, (void*) 8,
2082 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2086 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2087 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2091 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2092 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2098 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2100 struct snd_pcm_runtime *runtime = substream->runtime;
2101 struct snd_pcm_hardware *hw = &runtime->hw;
2103 unsigned int mask = 0;
2105 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2106 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2107 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2108 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2109 if (hw_support_mmap(substream)) {
2110 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2111 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2112 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2113 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2114 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2115 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2117 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2121 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2125 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2129 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2130 hw->channels_min, hw->channels_max);
2134 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2135 hw->rate_min, hw->rate_max);
2139 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2140 hw->period_bytes_min, hw->period_bytes_max);
2144 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2145 hw->periods_min, hw->periods_max);
2149 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2150 hw->period_bytes_min, hw->buffer_bytes_max);
2154 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2155 snd_pcm_hw_rule_buffer_bytes_max, substream,
2156 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2161 if (runtime->dma_bytes) {
2162 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2167 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2168 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2169 snd_pcm_hw_rule_rate, hw,
2170 SNDRV_PCM_HW_PARAM_RATE, -1);
2175 /* FIXME: this belong to lowlevel */
2176 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2181 static void pcm_release_private(struct snd_pcm_substream *substream)
2183 snd_pcm_unlink(substream);
2186 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2188 substream->ref_count--;
2189 if (substream->ref_count > 0)
2192 snd_pcm_drop(substream);
2193 if (substream->hw_opened) {
2194 if (substream->ops->hw_free != NULL)
2195 substream->ops->hw_free(substream);
2196 substream->ops->close(substream);
2197 substream->hw_opened = 0;
2199 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2200 pm_qos_remove_request(&substream->latency_pm_qos_req);
2201 if (substream->pcm_release) {
2202 substream->pcm_release(substream);
2203 substream->pcm_release = NULL;
2205 snd_pcm_detach_substream(substream);
2208 EXPORT_SYMBOL(snd_pcm_release_substream);
2210 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2212 struct snd_pcm_substream **rsubstream)
2214 struct snd_pcm_substream *substream;
2217 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2220 if (substream->ref_count > 1) {
2221 *rsubstream = substream;
2225 err = snd_pcm_hw_constraints_init(substream);
2227 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2231 if ((err = substream->ops->open(substream)) < 0)
2234 substream->hw_opened = 1;
2236 err = snd_pcm_hw_constraints_complete(substream);
2238 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2242 *rsubstream = substream;
2246 snd_pcm_release_substream(substream);
2250 EXPORT_SYMBOL(snd_pcm_open_substream);
2252 static int snd_pcm_open_file(struct file *file,
2253 struct snd_pcm *pcm,
2256 struct snd_pcm_file *pcm_file;
2257 struct snd_pcm_substream *substream;
2260 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2264 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2265 if (pcm_file == NULL) {
2266 snd_pcm_release_substream(substream);
2269 pcm_file->substream = substream;
2270 if (substream->ref_count == 1) {
2271 substream->file = pcm_file;
2272 substream->pcm_release = pcm_release_private;
2274 file->private_data = pcm_file;
2279 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2281 struct snd_pcm *pcm;
2282 int err = nonseekable_open(inode, file);
2285 pcm = snd_lookup_minor_data(iminor(inode),
2286 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2287 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2289 snd_card_unref(pcm->card);
2293 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2295 struct snd_pcm *pcm;
2296 int err = nonseekable_open(inode, file);
2299 pcm = snd_lookup_minor_data(iminor(inode),
2300 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2301 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2303 snd_card_unref(pcm->card);
2307 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2316 err = snd_card_file_add(pcm->card, file);
2319 if (!try_module_get(pcm->card->module)) {
2323 init_waitqueue_entry(&wait, current);
2324 add_wait_queue(&pcm->open_wait, &wait);
2325 mutex_lock(&pcm->open_mutex);
2327 err = snd_pcm_open_file(file, pcm, stream);
2330 if (err == -EAGAIN) {
2331 if (file->f_flags & O_NONBLOCK) {
2337 set_current_state(TASK_INTERRUPTIBLE);
2338 mutex_unlock(&pcm->open_mutex);
2340 mutex_lock(&pcm->open_mutex);
2341 if (pcm->card->shutdown) {
2345 if (signal_pending(current)) {
2350 remove_wait_queue(&pcm->open_wait, &wait);
2351 mutex_unlock(&pcm->open_mutex);
2357 module_put(pcm->card->module);
2359 snd_card_file_remove(pcm->card, file);
2364 static int snd_pcm_release(struct inode *inode, struct file *file)
2366 struct snd_pcm *pcm;
2367 struct snd_pcm_substream *substream;
2368 struct snd_pcm_file *pcm_file;
2370 pcm_file = file->private_data;
2371 substream = pcm_file->substream;
2372 if (snd_BUG_ON(!substream))
2374 pcm = substream->pcm;
2375 mutex_lock(&pcm->open_mutex);
2376 snd_pcm_release_substream(substream);
2378 mutex_unlock(&pcm->open_mutex);
2379 wake_up(&pcm->open_wait);
2380 module_put(pcm->card->module);
2381 snd_card_file_remove(pcm->card, file);
2385 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2386 snd_pcm_uframes_t frames)
2388 struct snd_pcm_runtime *runtime = substream->runtime;
2389 snd_pcm_sframes_t appl_ptr;
2390 snd_pcm_sframes_t ret;
2391 snd_pcm_sframes_t hw_avail;
2396 snd_pcm_stream_lock_irq(substream);
2397 switch (runtime->status->state) {
2398 case SNDRV_PCM_STATE_PREPARED:
2400 case SNDRV_PCM_STATE_DRAINING:
2401 case SNDRV_PCM_STATE_RUNNING:
2402 if (snd_pcm_update_hw_ptr(substream) >= 0)
2405 case SNDRV_PCM_STATE_XRUN:
2408 case SNDRV_PCM_STATE_SUSPENDED:
2416 hw_avail = snd_pcm_playback_hw_avail(runtime);
2417 if (hw_avail <= 0) {
2421 if (frames > (snd_pcm_uframes_t)hw_avail)
2423 appl_ptr = runtime->control->appl_ptr - frames;
2425 appl_ptr += runtime->boundary;
2426 runtime->control->appl_ptr = appl_ptr;
2429 snd_pcm_stream_unlock_irq(substream);
2433 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2434 snd_pcm_uframes_t frames)
2436 struct snd_pcm_runtime *runtime = substream->runtime;
2437 snd_pcm_sframes_t appl_ptr;
2438 snd_pcm_sframes_t ret;
2439 snd_pcm_sframes_t hw_avail;
2444 snd_pcm_stream_lock_irq(substream);
2445 switch (runtime->status->state) {
2446 case SNDRV_PCM_STATE_PREPARED:
2447 case SNDRV_PCM_STATE_DRAINING:
2449 case SNDRV_PCM_STATE_RUNNING:
2450 if (snd_pcm_update_hw_ptr(substream) >= 0)
2453 case SNDRV_PCM_STATE_XRUN:
2456 case SNDRV_PCM_STATE_SUSPENDED:
2464 hw_avail = snd_pcm_capture_hw_avail(runtime);
2465 if (hw_avail <= 0) {
2469 if (frames > (snd_pcm_uframes_t)hw_avail)
2471 appl_ptr = runtime->control->appl_ptr - frames;
2473 appl_ptr += runtime->boundary;
2474 runtime->control->appl_ptr = appl_ptr;
2477 snd_pcm_stream_unlock_irq(substream);
2481 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2482 snd_pcm_uframes_t frames)
2484 struct snd_pcm_runtime *runtime = substream->runtime;
2485 snd_pcm_sframes_t appl_ptr;
2486 snd_pcm_sframes_t ret;
2487 snd_pcm_sframes_t avail;
2492 snd_pcm_stream_lock_irq(substream);
2493 switch (runtime->status->state) {
2494 case SNDRV_PCM_STATE_PREPARED:
2495 case SNDRV_PCM_STATE_PAUSED:
2497 case SNDRV_PCM_STATE_DRAINING:
2498 case SNDRV_PCM_STATE_RUNNING:
2499 if (snd_pcm_update_hw_ptr(substream) >= 0)
2502 case SNDRV_PCM_STATE_XRUN:
2505 case SNDRV_PCM_STATE_SUSPENDED:
2513 avail = snd_pcm_playback_avail(runtime);
2518 if (frames > (snd_pcm_uframes_t)avail)
2520 appl_ptr = runtime->control->appl_ptr + frames;
2521 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2522 appl_ptr -= runtime->boundary;
2523 runtime->control->appl_ptr = appl_ptr;
2526 snd_pcm_stream_unlock_irq(substream);
2530 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2531 snd_pcm_uframes_t frames)
2533 struct snd_pcm_runtime *runtime = substream->runtime;
2534 snd_pcm_sframes_t appl_ptr;
2535 snd_pcm_sframes_t ret;
2536 snd_pcm_sframes_t avail;
2541 snd_pcm_stream_lock_irq(substream);
2542 switch (runtime->status->state) {
2543 case SNDRV_PCM_STATE_PREPARED:
2544 case SNDRV_PCM_STATE_DRAINING:
2545 case SNDRV_PCM_STATE_PAUSED:
2547 case SNDRV_PCM_STATE_RUNNING:
2548 if (snd_pcm_update_hw_ptr(substream) >= 0)
2551 case SNDRV_PCM_STATE_XRUN:
2554 case SNDRV_PCM_STATE_SUSPENDED:
2562 avail = snd_pcm_capture_avail(runtime);
2567 if (frames > (snd_pcm_uframes_t)avail)
2569 appl_ptr = runtime->control->appl_ptr + frames;
2570 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2571 appl_ptr -= runtime->boundary;
2572 runtime->control->appl_ptr = appl_ptr;
2575 snd_pcm_stream_unlock_irq(substream);
2579 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2581 struct snd_pcm_runtime *runtime = substream->runtime;
2584 snd_pcm_stream_lock_irq(substream);
2585 switch (runtime->status->state) {
2586 case SNDRV_PCM_STATE_DRAINING:
2587 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2590 case SNDRV_PCM_STATE_RUNNING:
2591 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2594 case SNDRV_PCM_STATE_PREPARED:
2595 case SNDRV_PCM_STATE_SUSPENDED:
2598 case SNDRV_PCM_STATE_XRUN:
2606 snd_pcm_stream_unlock_irq(substream);
2610 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2611 snd_pcm_sframes_t __user *res)
2613 struct snd_pcm_runtime *runtime = substream->runtime;
2615 snd_pcm_sframes_t n = 0;
2617 snd_pcm_stream_lock_irq(substream);
2618 switch (runtime->status->state) {
2619 case SNDRV_PCM_STATE_DRAINING:
2620 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2623 case SNDRV_PCM_STATE_RUNNING:
2624 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2627 case SNDRV_PCM_STATE_PREPARED:
2628 case SNDRV_PCM_STATE_SUSPENDED:
2630 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2631 n = snd_pcm_playback_hw_avail(runtime);
2633 n = snd_pcm_capture_avail(runtime);
2634 n += runtime->delay;
2636 case SNDRV_PCM_STATE_XRUN:
2644 snd_pcm_stream_unlock_irq(substream);
2646 if (put_user(n, res))
2651 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2652 struct snd_pcm_sync_ptr __user *_sync_ptr)
2654 struct snd_pcm_runtime *runtime = substream->runtime;
2655 struct snd_pcm_sync_ptr sync_ptr;
2656 volatile struct snd_pcm_mmap_status *status;
2657 volatile struct snd_pcm_mmap_control *control;
2660 memset(&sync_ptr, 0, sizeof(sync_ptr));
2661 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2663 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2665 status = runtime->status;
2666 control = runtime->control;
2667 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2668 err = snd_pcm_hwsync(substream);
2672 snd_pcm_stream_lock_irq(substream);
2673 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2674 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2676 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2677 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2678 control->avail_min = sync_ptr.c.control.avail_min;
2680 sync_ptr.c.control.avail_min = control->avail_min;
2681 sync_ptr.s.status.state = status->state;
2682 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2683 sync_ptr.s.status.tstamp = status->tstamp;
2684 sync_ptr.s.status.suspended_state = status->suspended_state;
2685 snd_pcm_stream_unlock_irq(substream);
2686 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2691 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2693 struct snd_pcm_runtime *runtime = substream->runtime;
2696 if (get_user(arg, _arg))
2698 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2700 runtime->tstamp_type = arg;
2704 static int snd_pcm_common_ioctl1(struct file *file,
2705 struct snd_pcm_substream *substream,
2706 unsigned int cmd, void __user *arg)
2709 case SNDRV_PCM_IOCTL_PVERSION:
2710 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2711 case SNDRV_PCM_IOCTL_INFO:
2712 return snd_pcm_info_user(substream, arg);
2713 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2715 case SNDRV_PCM_IOCTL_TTSTAMP:
2716 return snd_pcm_tstamp(substream, arg);
2717 case SNDRV_PCM_IOCTL_HW_REFINE:
2718 return snd_pcm_hw_refine_user(substream, arg);
2719 case SNDRV_PCM_IOCTL_HW_PARAMS:
2720 return snd_pcm_hw_params_user(substream, arg);
2721 case SNDRV_PCM_IOCTL_HW_FREE:
2722 return snd_pcm_hw_free(substream);
2723 case SNDRV_PCM_IOCTL_SW_PARAMS:
2724 return snd_pcm_sw_params_user(substream, arg);
2725 case SNDRV_PCM_IOCTL_STATUS:
2726 return snd_pcm_status_user(substream, arg);
2727 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2728 return snd_pcm_channel_info_user(substream, arg);
2729 case SNDRV_PCM_IOCTL_PREPARE:
2730 return snd_pcm_prepare(substream, file);
2731 case SNDRV_PCM_IOCTL_RESET:
2732 return snd_pcm_reset(substream);
2733 case SNDRV_PCM_IOCTL_START:
2734 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2735 case SNDRV_PCM_IOCTL_LINK:
2736 return snd_pcm_link(substream, (int)(unsigned long) arg);
2737 case SNDRV_PCM_IOCTL_UNLINK:
2738 return snd_pcm_unlink(substream);
2739 case SNDRV_PCM_IOCTL_RESUME:
2740 return snd_pcm_resume(substream);
2741 case SNDRV_PCM_IOCTL_XRUN:
2742 return snd_pcm_xrun(substream);
2743 case SNDRV_PCM_IOCTL_HWSYNC:
2744 return snd_pcm_hwsync(substream);
2745 case SNDRV_PCM_IOCTL_DELAY:
2746 return snd_pcm_delay(substream, arg);
2747 case SNDRV_PCM_IOCTL_SYNC_PTR:
2748 return snd_pcm_sync_ptr(substream, arg);
2749 #ifdef CONFIG_SND_SUPPORT_OLD_API
2750 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2751 return snd_pcm_hw_refine_old_user(substream, arg);
2752 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2753 return snd_pcm_hw_params_old_user(substream, arg);
2755 case SNDRV_PCM_IOCTL_DRAIN:
2756 return snd_pcm_drain(substream, file);
2757 case SNDRV_PCM_IOCTL_DROP:
2758 return snd_pcm_drop(substream);
2759 case SNDRV_PCM_IOCTL_PAUSE:
2762 snd_pcm_stream_lock_irq(substream);
2763 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2764 snd_pcm_stream_unlock_irq(substream);
2768 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2772 static int snd_pcm_playback_ioctl1(struct file *file,
2773 struct snd_pcm_substream *substream,
2774 unsigned int cmd, void __user *arg)
2776 if (snd_BUG_ON(!substream))
2778 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2781 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2783 struct snd_xferi xferi;
2784 struct snd_xferi __user *_xferi = arg;
2785 struct snd_pcm_runtime *runtime = substream->runtime;
2786 snd_pcm_sframes_t result;
2787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2789 if (put_user(0, &_xferi->result))
2791 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2793 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2794 __put_user(result, &_xferi->result);
2795 return result < 0 ? result : 0;
2797 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2799 struct snd_xfern xfern;
2800 struct snd_xfern __user *_xfern = arg;
2801 struct snd_pcm_runtime *runtime = substream->runtime;
2803 snd_pcm_sframes_t result;
2804 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2806 if (runtime->channels > 128)
2808 if (put_user(0, &_xfern->result))
2810 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2813 bufs = memdup_user(xfern.bufs,
2814 sizeof(void *) * runtime->channels);
2816 return PTR_ERR(bufs);
2817 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2819 __put_user(result, &_xfern->result);
2820 return result < 0 ? result : 0;
2822 case SNDRV_PCM_IOCTL_REWIND:
2824 snd_pcm_uframes_t frames;
2825 snd_pcm_uframes_t __user *_frames = arg;
2826 snd_pcm_sframes_t result;
2827 if (get_user(frames, _frames))
2829 if (put_user(0, _frames))
2831 result = snd_pcm_playback_rewind(substream, frames);
2832 __put_user(result, _frames);
2833 return result < 0 ? result : 0;
2835 case SNDRV_PCM_IOCTL_FORWARD:
2837 snd_pcm_uframes_t frames;
2838 snd_pcm_uframes_t __user *_frames = arg;
2839 snd_pcm_sframes_t result;
2840 if (get_user(frames, _frames))
2842 if (put_user(0, _frames))
2844 result = snd_pcm_playback_forward(substream, frames);
2845 __put_user(result, _frames);
2846 return result < 0 ? result : 0;
2849 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2852 static int snd_pcm_capture_ioctl1(struct file *file,
2853 struct snd_pcm_substream *substream,
2854 unsigned int cmd, void __user *arg)
2856 if (snd_BUG_ON(!substream))
2858 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2861 case SNDRV_PCM_IOCTL_READI_FRAMES:
2863 struct snd_xferi xferi;
2864 struct snd_xferi __user *_xferi = arg;
2865 struct snd_pcm_runtime *runtime = substream->runtime;
2866 snd_pcm_sframes_t result;
2867 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2869 if (put_user(0, &_xferi->result))
2871 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2873 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2874 __put_user(result, &_xferi->result);
2875 return result < 0 ? result : 0;
2877 case SNDRV_PCM_IOCTL_READN_FRAMES:
2879 struct snd_xfern xfern;
2880 struct snd_xfern __user *_xfern = arg;
2881 struct snd_pcm_runtime *runtime = substream->runtime;
2883 snd_pcm_sframes_t result;
2884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 if (runtime->channels > 128)
2888 if (put_user(0, &_xfern->result))
2890 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2893 bufs = memdup_user(xfern.bufs,
2894 sizeof(void *) * runtime->channels);
2896 return PTR_ERR(bufs);
2897 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2899 __put_user(result, &_xfern->result);
2900 return result < 0 ? result : 0;
2902 case SNDRV_PCM_IOCTL_REWIND:
2904 snd_pcm_uframes_t frames;
2905 snd_pcm_uframes_t __user *_frames = arg;
2906 snd_pcm_sframes_t result;
2907 if (get_user(frames, _frames))
2909 if (put_user(0, _frames))
2911 result = snd_pcm_capture_rewind(substream, frames);
2912 __put_user(result, _frames);
2913 return result < 0 ? result : 0;
2915 case SNDRV_PCM_IOCTL_FORWARD:
2917 snd_pcm_uframes_t frames;
2918 snd_pcm_uframes_t __user *_frames = arg;
2919 snd_pcm_sframes_t result;
2920 if (get_user(frames, _frames))
2922 if (put_user(0, _frames))
2924 result = snd_pcm_capture_forward(substream, frames);
2925 __put_user(result, _frames);
2926 return result < 0 ? result : 0;
2929 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2932 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2935 struct snd_pcm_file *pcm_file;
2937 pcm_file = file->private_data;
2939 if (((cmd >> 8) & 0xff) != 'A')
2942 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2943 (void __user *)arg);
2946 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2949 struct snd_pcm_file *pcm_file;
2951 pcm_file = file->private_data;
2953 if (((cmd >> 8) & 0xff) != 'A')
2956 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2957 (void __user *)arg);
2960 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2961 unsigned int cmd, void *arg)
2966 fs = snd_enter_user();
2967 switch (substream->stream) {
2968 case SNDRV_PCM_STREAM_PLAYBACK:
2969 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2970 (void __user *)arg);
2972 case SNDRV_PCM_STREAM_CAPTURE:
2973 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2974 (void __user *)arg);
2984 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2986 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2989 struct snd_pcm_file *pcm_file;
2990 struct snd_pcm_substream *substream;
2991 struct snd_pcm_runtime *runtime;
2992 snd_pcm_sframes_t result;
2994 pcm_file = file->private_data;
2995 substream = pcm_file->substream;
2996 if (PCM_RUNTIME_CHECK(substream))
2998 runtime = substream->runtime;
2999 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3001 if (!frame_aligned(runtime, count))
3003 count = bytes_to_frames(runtime, count);
3004 result = snd_pcm_lib_read(substream, buf, count);
3006 result = frames_to_bytes(runtime, result);
3010 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3011 size_t count, loff_t * offset)
3013 struct snd_pcm_file *pcm_file;
3014 struct snd_pcm_substream *substream;
3015 struct snd_pcm_runtime *runtime;
3016 snd_pcm_sframes_t result;
3018 pcm_file = file->private_data;
3019 substream = pcm_file->substream;
3020 if (PCM_RUNTIME_CHECK(substream))
3022 runtime = substream->runtime;
3023 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3025 if (!frame_aligned(runtime, count))
3027 count = bytes_to_frames(runtime, count);
3028 result = snd_pcm_lib_write(substream, buf, count);
3030 result = frames_to_bytes(runtime, result);
3034 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
3035 unsigned long nr_segs, loff_t pos)
3038 struct snd_pcm_file *pcm_file;
3039 struct snd_pcm_substream *substream;
3040 struct snd_pcm_runtime *runtime;
3041 snd_pcm_sframes_t result;
3044 snd_pcm_uframes_t frames;
3046 pcm_file = iocb->ki_filp->private_data;
3047 substream = pcm_file->substream;
3048 if (PCM_RUNTIME_CHECK(substream))
3050 runtime = substream->runtime;
3051 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3053 if (nr_segs > 1024 || nr_segs != runtime->channels)
3055 if (!frame_aligned(runtime, iov->iov_len))
3057 frames = bytes_to_samples(runtime, iov->iov_len);
3058 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3061 for (i = 0; i < nr_segs; ++i)
3062 bufs[i] = iov[i].iov_base;
3063 result = snd_pcm_lib_readv(substream, bufs, frames);
3065 result = frames_to_bytes(runtime, result);
3070 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
3071 unsigned long nr_segs, loff_t pos)
3073 struct snd_pcm_file *pcm_file;
3074 struct snd_pcm_substream *substream;
3075 struct snd_pcm_runtime *runtime;
3076 snd_pcm_sframes_t result;
3079 snd_pcm_uframes_t frames;
3081 pcm_file = iocb->ki_filp->private_data;
3082 substream = pcm_file->substream;
3083 if (PCM_RUNTIME_CHECK(substream))
3085 runtime = substream->runtime;
3086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3088 if (nr_segs > 128 || nr_segs != runtime->channels ||
3089 !frame_aligned(runtime, iov->iov_len))
3091 frames = bytes_to_samples(runtime, iov->iov_len);
3092 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3095 for (i = 0; i < nr_segs; ++i)
3096 bufs[i] = iov[i].iov_base;
3097 result = snd_pcm_lib_writev(substream, bufs, frames);
3099 result = frames_to_bytes(runtime, result);
3104 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3106 struct snd_pcm_file *pcm_file;
3107 struct snd_pcm_substream *substream;
3108 struct snd_pcm_runtime *runtime;
3110 snd_pcm_uframes_t avail;
3112 pcm_file = file->private_data;
3114 substream = pcm_file->substream;
3115 if (PCM_RUNTIME_CHECK(substream))
3117 runtime = substream->runtime;
3119 poll_wait(file, &runtime->sleep, wait);
3121 snd_pcm_stream_lock_irq(substream);
3122 avail = snd_pcm_playback_avail(runtime);
3123 switch (runtime->status->state) {
3124 case SNDRV_PCM_STATE_RUNNING:
3125 case SNDRV_PCM_STATE_PREPARED:
3126 case SNDRV_PCM_STATE_PAUSED:
3127 if (avail >= runtime->control->avail_min) {
3128 mask = POLLOUT | POLLWRNORM;
3132 case SNDRV_PCM_STATE_DRAINING:
3136 mask = POLLOUT | POLLWRNORM | POLLERR;
3139 snd_pcm_stream_unlock_irq(substream);
3143 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3145 struct snd_pcm_file *pcm_file;
3146 struct snd_pcm_substream *substream;
3147 struct snd_pcm_runtime *runtime;
3149 snd_pcm_uframes_t avail;
3151 pcm_file = file->private_data;
3153 substream = pcm_file->substream;
3154 if (PCM_RUNTIME_CHECK(substream))
3156 runtime = substream->runtime;
3158 poll_wait(file, &runtime->sleep, wait);
3160 snd_pcm_stream_lock_irq(substream);
3161 avail = snd_pcm_capture_avail(runtime);
3162 switch (runtime->status->state) {
3163 case SNDRV_PCM_STATE_RUNNING:
3164 case SNDRV_PCM_STATE_PREPARED:
3165 case SNDRV_PCM_STATE_PAUSED:
3166 if (avail >= runtime->control->avail_min) {
3167 mask = POLLIN | POLLRDNORM;
3172 case SNDRV_PCM_STATE_DRAINING:
3174 mask = POLLIN | POLLRDNORM;
3179 mask = POLLIN | POLLRDNORM | POLLERR;
3182 snd_pcm_stream_unlock_irq(substream);
3191 * Only on coherent architectures, we can mmap the status and the control records
3192 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3194 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3196 * mmap status record
3198 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3199 struct vm_fault *vmf)
3201 struct snd_pcm_substream *substream = area->vm_private_data;
3202 struct snd_pcm_runtime *runtime;
3204 if (substream == NULL)
3205 return VM_FAULT_SIGBUS;
3206 runtime = substream->runtime;
3207 vmf->page = virt_to_page(runtime->status);
3208 get_page(vmf->page);
3212 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3214 .fault = snd_pcm_mmap_status_fault,
3217 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3218 struct vm_area_struct *area)
3221 if (!(area->vm_flags & VM_READ))
3223 size = area->vm_end - area->vm_start;
3224 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3226 area->vm_ops = &snd_pcm_vm_ops_status;
3227 area->vm_private_data = substream;
3228 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3233 * mmap control record
3235 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3236 struct vm_fault *vmf)
3238 struct snd_pcm_substream *substream = area->vm_private_data;
3239 struct snd_pcm_runtime *runtime;
3241 if (substream == NULL)
3242 return VM_FAULT_SIGBUS;
3243 runtime = substream->runtime;
3244 vmf->page = virt_to_page(runtime->control);
3245 get_page(vmf->page);
3249 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3251 .fault = snd_pcm_mmap_control_fault,
3254 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3255 struct vm_area_struct *area)
3258 if (!(area->vm_flags & VM_READ))
3260 size = area->vm_end - area->vm_start;
3261 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3263 area->vm_ops = &snd_pcm_vm_ops_control;
3264 area->vm_private_data = substream;
3265 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3268 #else /* ! coherent mmap */
3270 * don't support mmap for status and control records.
3272 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3273 struct vm_area_struct *area)
3277 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3278 struct vm_area_struct *area)
3282 #endif /* coherent mmap */
3284 static inline struct page *
3285 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3287 void *vaddr = substream->runtime->dma_area + ofs;
3288 return virt_to_page(vaddr);
3292 * fault callback for mmapping a RAM page
3294 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3295 struct vm_fault *vmf)
3297 struct snd_pcm_substream *substream = area->vm_private_data;
3298 struct snd_pcm_runtime *runtime;
3299 unsigned long offset;
3303 if (substream == NULL)
3304 return VM_FAULT_SIGBUS;
3305 runtime = substream->runtime;
3306 offset = vmf->pgoff << PAGE_SHIFT;
3307 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3308 if (offset > dma_bytes - PAGE_SIZE)
3309 return VM_FAULT_SIGBUS;
3310 if (substream->ops->page)
3311 page = substream->ops->page(substream, offset);
3313 page = snd_pcm_default_page_ops(substream, offset);
3315 return VM_FAULT_SIGBUS;
3321 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3322 .open = snd_pcm_mmap_data_open,
3323 .close = snd_pcm_mmap_data_close,
3326 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3327 .open = snd_pcm_mmap_data_open,
3328 .close = snd_pcm_mmap_data_close,
3329 .fault = snd_pcm_mmap_data_fault,
3333 * mmap the DMA buffer on RAM
3337 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3338 * @substream: PCM substream
3341 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3342 * this function is invoked implicitly.
3344 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3345 struct vm_area_struct *area)
3347 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3348 #ifdef CONFIG_GENERIC_ALLOCATOR
3349 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3350 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3351 return remap_pfn_range(area, area->vm_start,
3352 substream->dma_buffer.addr >> PAGE_SHIFT,
3353 area->vm_end - area->vm_start, area->vm_page_prot);
3355 #endif /* CONFIG_GENERIC_ALLOCATOR */
3356 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3357 if (!substream->ops->page &&
3358 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3359 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3361 substream->runtime->dma_area,
3362 substream->runtime->dma_addr,
3363 area->vm_end - area->vm_start);
3364 #endif /* CONFIG_X86 */
3365 /* mmap with fault handler */
3366 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3369 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3372 * mmap the DMA buffer on I/O memory area
3374 #if SNDRV_PCM_INFO_MMAP_IOMEM
3376 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3377 * @substream: PCM substream
3380 * When your hardware uses the iomapped pages as the hardware buffer and
3381 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3382 * is supposed to work only on limited architectures.
3384 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3385 struct vm_area_struct *area)
3387 struct snd_pcm_runtime *runtime = substream->runtime;;
3389 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3390 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3393 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3394 #endif /* SNDRV_PCM_INFO_MMAP */
3399 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3400 struct vm_area_struct *area)
3402 struct snd_pcm_runtime *runtime;
3404 unsigned long offset;
3408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3409 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3412 if (!(area->vm_flags & VM_READ))
3415 runtime = substream->runtime;
3416 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3418 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3420 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3421 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3423 size = area->vm_end - area->vm_start;
3424 offset = area->vm_pgoff << PAGE_SHIFT;
3425 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3426 if ((size_t)size > dma_bytes)
3428 if (offset > dma_bytes - size)
3431 area->vm_ops = &snd_pcm_vm_ops_data;
3432 area->vm_private_data = substream;
3433 if (substream->ops->mmap)
3434 err = substream->ops->mmap(substream, area);
3436 err = snd_pcm_lib_default_mmap(substream, area);
3438 atomic_inc(&substream->mmap_count);
3442 EXPORT_SYMBOL(snd_pcm_mmap_data);
3444 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3446 struct snd_pcm_file * pcm_file;
3447 struct snd_pcm_substream *substream;
3448 unsigned long offset;
3450 pcm_file = file->private_data;
3451 substream = pcm_file->substream;
3452 if (PCM_RUNTIME_CHECK(substream))
3455 offset = area->vm_pgoff << PAGE_SHIFT;
3457 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3458 if (pcm_file->no_compat_mmap)
3460 return snd_pcm_mmap_status(substream, file, area);
3461 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3462 if (pcm_file->no_compat_mmap)
3464 return snd_pcm_mmap_control(substream, file, area);
3466 return snd_pcm_mmap_data(substream, file, area);
3471 static int snd_pcm_fasync(int fd, struct file * file, int on)
3473 struct snd_pcm_file * pcm_file;
3474 struct snd_pcm_substream *substream;
3475 struct snd_pcm_runtime *runtime;
3477 pcm_file = file->private_data;
3478 substream = pcm_file->substream;
3479 if (PCM_RUNTIME_CHECK(substream))
3481 runtime = substream->runtime;
3482 return fasync_helper(fd, file, on, &runtime->fasync);
3488 #ifdef CONFIG_COMPAT
3489 #include "pcm_compat.c"
3491 #define snd_pcm_ioctl_compat NULL
3495 * To be removed helpers to keep binary compatibility
3498 #ifdef CONFIG_SND_SUPPORT_OLD_API
3499 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3500 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3502 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3503 struct snd_pcm_hw_params_old *oparams)
3507 memset(params, 0, sizeof(*params));
3508 params->flags = oparams->flags;
3509 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3510 params->masks[i].bits[0] = oparams->masks[i];
3511 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3512 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3513 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3514 params->info = oparams->info;
3515 params->msbits = oparams->msbits;
3516 params->rate_num = oparams->rate_num;
3517 params->rate_den = oparams->rate_den;
3518 params->fifo_size = oparams->fifo_size;
3521 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3522 struct snd_pcm_hw_params *params)
3526 memset(oparams, 0, sizeof(*oparams));
3527 oparams->flags = params->flags;
3528 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3529 oparams->masks[i] = params->masks[i].bits[0];
3530 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3531 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3532 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3533 oparams->info = params->info;
3534 oparams->msbits = params->msbits;
3535 oparams->rate_num = params->rate_num;
3536 oparams->rate_den = params->rate_den;
3537 oparams->fifo_size = params->fifo_size;
3540 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3541 struct snd_pcm_hw_params_old __user * _oparams)
3543 struct snd_pcm_hw_params *params;
3544 struct snd_pcm_hw_params_old *oparams = NULL;
3547 params = kmalloc(sizeof(*params), GFP_KERNEL);
3551 oparams = memdup_user(_oparams, sizeof(*oparams));
3552 if (IS_ERR(oparams)) {
3553 err = PTR_ERR(oparams);
3556 snd_pcm_hw_convert_from_old_params(params, oparams);
3557 err = snd_pcm_hw_refine(substream, params);
3558 snd_pcm_hw_convert_to_old_params(oparams, params);
3559 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3570 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3571 struct snd_pcm_hw_params_old __user * _oparams)
3573 struct snd_pcm_hw_params *params;
3574 struct snd_pcm_hw_params_old *oparams = NULL;
3577 params = kmalloc(sizeof(*params), GFP_KERNEL);
3581 oparams = memdup_user(_oparams, sizeof(*oparams));
3582 if (IS_ERR(oparams)) {
3583 err = PTR_ERR(oparams);
3586 snd_pcm_hw_convert_from_old_params(params, oparams);
3587 err = snd_pcm_hw_params(substream, params);
3588 snd_pcm_hw_convert_to_old_params(oparams, params);
3589 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3599 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3602 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3605 unsigned long pgoff,
3606 unsigned long flags)
3608 struct snd_pcm_file *pcm_file = file->private_data;
3609 struct snd_pcm_substream *substream = pcm_file->substream;
3610 struct snd_pcm_runtime *runtime = substream->runtime;
3611 unsigned long offset = pgoff << PAGE_SHIFT;
3614 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3615 return (unsigned long)runtime->status;
3616 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3617 return (unsigned long)runtime->control;
3619 return (unsigned long)runtime->dma_area + offset;
3623 # define snd_pcm_get_unmapped_area NULL
3630 const struct file_operations snd_pcm_f_ops[2] = {
3632 .owner = THIS_MODULE,
3633 .write = snd_pcm_write,
3634 .aio_write = snd_pcm_aio_write,
3635 .open = snd_pcm_playback_open,
3636 .release = snd_pcm_release,
3637 .llseek = no_llseek,
3638 .poll = snd_pcm_playback_poll,
3639 .unlocked_ioctl = snd_pcm_playback_ioctl,
3640 .compat_ioctl = snd_pcm_ioctl_compat,
3641 .mmap = snd_pcm_mmap,
3642 .fasync = snd_pcm_fasync,
3643 .get_unmapped_area = snd_pcm_get_unmapped_area,
3646 .owner = THIS_MODULE,
3647 .read = snd_pcm_read,
3648 .aio_read = snd_pcm_aio_read,
3649 .open = snd_pcm_capture_open,
3650 .release = snd_pcm_release,
3651 .llseek = no_llseek,
3652 .poll = snd_pcm_capture_poll,
3653 .unlocked_ioctl = snd_pcm_capture_ioctl,
3654 .compat_ioctl = snd_pcm_ioctl_compat,
3655 .mmap = snd_pcm_mmap,
3656 .fasync = snd_pcm_fasync,
3657 .get_unmapped_area = snd_pcm_get_unmapped_area,