2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos.h>
28 #include <linux/aio.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
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 if (!hw_support_mmap(substream))
425 params->info &= ~(SNDRV_PCM_INFO_MMAP |
426 SNDRV_PCM_INFO_MMAP_VALID);
428 if (!params->fifo_size) {
429 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
430 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
431 if (snd_mask_min(m) == snd_mask_max(m) &&
432 snd_interval_min(i) == snd_interval_max(i)) {
433 changed = substream->ops->ioctl(substream,
434 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
443 EXPORT_SYMBOL(snd_pcm_hw_refine);
445 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
446 struct snd_pcm_hw_params __user * _params)
448 struct snd_pcm_hw_params *params;
451 params = memdup_user(_params, sizeof(*params));
453 return PTR_ERR(params);
455 err = snd_pcm_hw_refine(substream, params);
456 if (copy_to_user(_params, params, sizeof(*params))) {
465 static int period_to_usecs(struct snd_pcm_runtime *runtime)
470 return -1; /* invalid */
472 /* take 75% of period time as the deadline */
473 usecs = (750000 / runtime->rate) * runtime->period_size;
474 usecs += ((750000 % runtime->rate) * runtime->period_size) /
480 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
482 snd_pcm_stream_lock_irq(substream);
483 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
484 substream->runtime->status->state = state;
485 snd_pcm_stream_unlock_irq(substream);
488 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
489 struct snd_pcm_hw_params *params)
491 struct snd_pcm_runtime *runtime;
494 snd_pcm_uframes_t frames;
496 if (PCM_RUNTIME_CHECK(substream))
498 runtime = substream->runtime;
499 snd_pcm_stream_lock_irq(substream);
500 switch (runtime->status->state) {
501 case SNDRV_PCM_STATE_OPEN:
502 case SNDRV_PCM_STATE_SETUP:
503 case SNDRV_PCM_STATE_PREPARED:
506 snd_pcm_stream_unlock_irq(substream);
509 snd_pcm_stream_unlock_irq(substream);
510 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
511 if (!substream->oss.oss)
513 if (atomic_read(&substream->mmap_count))
517 err = snd_pcm_hw_refine(substream, params);
521 err = snd_pcm_hw_params_choose(substream, params);
525 if (substream->ops->hw_params != NULL) {
526 err = substream->ops->hw_params(substream, params);
531 runtime->access = params_access(params);
532 runtime->format = params_format(params);
533 runtime->subformat = params_subformat(params);
534 runtime->channels = params_channels(params);
535 runtime->rate = params_rate(params);
536 runtime->period_size = params_period_size(params);
537 runtime->periods = params_periods(params);
538 runtime->buffer_size = params_buffer_size(params);
539 runtime->info = params->info;
540 runtime->rate_num = params->rate_num;
541 runtime->rate_den = params->rate_den;
542 runtime->no_period_wakeup =
543 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
544 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
546 bits = snd_pcm_format_physical_width(runtime->format);
547 runtime->sample_bits = bits;
548 bits *= runtime->channels;
549 runtime->frame_bits = bits;
551 while (bits % 8 != 0) {
555 runtime->byte_align = bits / 8;
556 runtime->min_align = frames;
558 /* Default sw params */
559 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
560 runtime->period_step = 1;
561 runtime->control->avail_min = runtime->period_size;
562 runtime->start_threshold = 1;
563 runtime->stop_threshold = runtime->buffer_size;
564 runtime->silence_threshold = 0;
565 runtime->silence_size = 0;
566 runtime->boundary = runtime->buffer_size;
567 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
568 runtime->boundary *= 2;
570 snd_pcm_timer_resolution_change(substream);
571 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
573 if (pm_qos_request_active(&substream->latency_pm_qos_req))
574 pm_qos_remove_request(&substream->latency_pm_qos_req);
575 if ((usecs = period_to_usecs(runtime)) >= 0)
576 pm_qos_add_request(&substream->latency_pm_qos_req,
577 PM_QOS_CPU_DMA_LATENCY, usecs);
580 /* hardware might be unusable from this time,
581 so we force application to retry to set
582 the correct hardware parameter settings */
583 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
584 if (substream->ops->hw_free != NULL)
585 substream->ops->hw_free(substream);
589 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
590 struct snd_pcm_hw_params __user * _params)
592 struct snd_pcm_hw_params *params;
595 params = memdup_user(_params, sizeof(*params));
597 return PTR_ERR(params);
599 err = snd_pcm_hw_params(substream, params);
600 if (copy_to_user(_params, params, sizeof(*params))) {
609 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
611 struct snd_pcm_runtime *runtime;
614 if (PCM_RUNTIME_CHECK(substream))
616 runtime = substream->runtime;
617 snd_pcm_stream_lock_irq(substream);
618 switch (runtime->status->state) {
619 case SNDRV_PCM_STATE_SETUP:
620 case SNDRV_PCM_STATE_PREPARED:
623 snd_pcm_stream_unlock_irq(substream);
626 snd_pcm_stream_unlock_irq(substream);
627 if (atomic_read(&substream->mmap_count))
629 if (substream->ops->hw_free)
630 result = substream->ops->hw_free(substream);
631 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
632 pm_qos_remove_request(&substream->latency_pm_qos_req);
636 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
637 struct snd_pcm_sw_params *params)
639 struct snd_pcm_runtime *runtime;
642 if (PCM_RUNTIME_CHECK(substream))
644 runtime = substream->runtime;
645 snd_pcm_stream_lock_irq(substream);
646 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
647 snd_pcm_stream_unlock_irq(substream);
650 snd_pcm_stream_unlock_irq(substream);
652 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
654 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
655 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
657 if (params->avail_min == 0)
659 if (params->silence_size >= runtime->boundary) {
660 if (params->silence_threshold != 0)
663 if (params->silence_size > params->silence_threshold)
665 if (params->silence_threshold > runtime->buffer_size)
669 snd_pcm_stream_lock_irq(substream);
670 runtime->tstamp_mode = params->tstamp_mode;
671 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
672 runtime->tstamp_type = params->tstamp_type;
673 runtime->period_step = params->period_step;
674 runtime->control->avail_min = params->avail_min;
675 runtime->start_threshold = params->start_threshold;
676 runtime->stop_threshold = params->stop_threshold;
677 runtime->silence_threshold = params->silence_threshold;
678 runtime->silence_size = params->silence_size;
679 params->boundary = runtime->boundary;
680 if (snd_pcm_running(substream)) {
681 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
682 runtime->silence_size > 0)
683 snd_pcm_playback_silence(substream, ULONG_MAX);
684 err = snd_pcm_update_state(substream, runtime);
686 snd_pcm_stream_unlock_irq(substream);
690 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
691 struct snd_pcm_sw_params __user * _params)
693 struct snd_pcm_sw_params params;
695 if (copy_from_user(¶ms, _params, sizeof(params)))
697 err = snd_pcm_sw_params(substream, ¶ms);
698 if (copy_to_user(_params, ¶ms, sizeof(params)))
703 int snd_pcm_status(struct snd_pcm_substream *substream,
704 struct snd_pcm_status *status)
706 struct snd_pcm_runtime *runtime = substream->runtime;
708 snd_pcm_stream_lock_irq(substream);
709 status->state = runtime->status->state;
710 status->suspended_state = runtime->status->suspended_state;
711 if (status->state == SNDRV_PCM_STATE_OPEN)
713 status->trigger_tstamp = runtime->trigger_tstamp;
714 if (snd_pcm_running(substream)) {
715 snd_pcm_update_hw_ptr(substream);
716 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
717 status->tstamp = runtime->status->tstamp;
718 status->audio_tstamp =
719 runtime->status->audio_tstamp;
723 snd_pcm_gettime(runtime, &status->tstamp);
725 status->appl_ptr = runtime->control->appl_ptr;
726 status->hw_ptr = runtime->status->hw_ptr;
727 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
728 status->avail = snd_pcm_playback_avail(runtime);
729 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
730 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
731 status->delay = runtime->buffer_size - status->avail;
732 status->delay += runtime->delay;
736 status->avail = snd_pcm_capture_avail(runtime);
737 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
738 status->delay = status->avail + runtime->delay;
742 status->avail_max = runtime->avail_max;
743 status->overrange = runtime->overrange;
744 runtime->avail_max = 0;
745 runtime->overrange = 0;
747 snd_pcm_stream_unlock_irq(substream);
751 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
752 struct snd_pcm_status __user * _status)
754 struct snd_pcm_status status;
757 memset(&status, 0, sizeof(status));
758 res = snd_pcm_status(substream, &status);
761 if (copy_to_user(_status, &status, sizeof(status)))
766 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
767 struct snd_pcm_channel_info * info)
769 struct snd_pcm_runtime *runtime;
770 unsigned int channel;
772 channel = info->channel;
773 runtime = substream->runtime;
774 snd_pcm_stream_lock_irq(substream);
775 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
776 snd_pcm_stream_unlock_irq(substream);
779 snd_pcm_stream_unlock_irq(substream);
780 if (channel >= runtime->channels)
782 memset(info, 0, sizeof(*info));
783 info->channel = channel;
784 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
787 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
788 struct snd_pcm_channel_info __user * _info)
790 struct snd_pcm_channel_info info;
793 if (copy_from_user(&info, _info, sizeof(info)))
795 res = snd_pcm_channel_info(substream, &info);
798 if (copy_to_user(_info, &info, sizeof(info)))
803 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
805 struct snd_pcm_runtime *runtime = substream->runtime;
806 if (runtime->trigger_master == NULL)
808 if (runtime->trigger_master == substream) {
809 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
811 snd_pcm_trigger_tstamp(runtime->trigger_master);
812 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
814 runtime->trigger_master = NULL;
818 int (*pre_action)(struct snd_pcm_substream *substream, int state);
819 int (*do_action)(struct snd_pcm_substream *substream, int state);
820 void (*undo_action)(struct snd_pcm_substream *substream, int state);
821 void (*post_action)(struct snd_pcm_substream *substream, int state);
825 * this functions is core for handling of linked stream
826 * Note: the stream state might be changed also on failure
827 * Note2: call with calling stream lock + link lock
829 static int snd_pcm_action_group(struct action_ops *ops,
830 struct snd_pcm_substream *substream,
831 int state, int do_lock)
833 struct snd_pcm_substream *s = NULL;
834 struct snd_pcm_substream *s1;
835 int res = 0, depth = 1;
837 snd_pcm_group_for_each_entry(s, substream) {
838 if (do_lock && s != substream) {
839 if (s->pcm->nonatomic)
840 mutex_lock_nested(&s->self_group.mutex, depth);
842 spin_lock_nested(&s->self_group.lock, depth);
845 res = ops->pre_action(s, state);
849 snd_pcm_group_for_each_entry(s, substream) {
850 res = ops->do_action(s, state);
852 if (ops->undo_action) {
853 snd_pcm_group_for_each_entry(s1, substream) {
854 if (s1 == s) /* failed stream */
856 ops->undo_action(s1, state);
859 s = NULL; /* unlock all */
863 snd_pcm_group_for_each_entry(s, substream) {
864 ops->post_action(s, state);
869 snd_pcm_group_for_each_entry(s1, substream) {
870 if (s1 != substream) {
871 if (s1->pcm->nonatomic)
872 mutex_unlock(&s1->self_group.mutex);
874 spin_unlock(&s1->self_group.lock);
876 if (s1 == s) /* end */
884 * Note: call with stream lock
886 static int snd_pcm_action_single(struct action_ops *ops,
887 struct snd_pcm_substream *substream,
892 res = ops->pre_action(substream, state);
895 res = ops->do_action(substream, state);
897 ops->post_action(substream, state);
898 else if (ops->undo_action)
899 ops->undo_action(substream, state);
903 /* call in mutex-protected context */
904 static int snd_pcm_action_mutex(struct action_ops *ops,
905 struct snd_pcm_substream *substream,
910 if (snd_pcm_stream_linked(substream)) {
911 if (!mutex_trylock(&substream->group->mutex)) {
912 mutex_unlock(&substream->self_group.mutex);
913 mutex_lock(&substream->group->mutex);
914 mutex_lock(&substream->self_group.mutex);
916 res = snd_pcm_action_group(ops, substream, state, 1);
917 mutex_unlock(&substream->group->mutex);
919 res = snd_pcm_action_single(ops, substream, state);
925 * Note: call with stream lock
927 static int snd_pcm_action(struct action_ops *ops,
928 struct snd_pcm_substream *substream,
933 if (substream->pcm->nonatomic)
934 return snd_pcm_action_mutex(ops, substream, state);
936 if (snd_pcm_stream_linked(substream)) {
937 if (!spin_trylock(&substream->group->lock)) {
938 spin_unlock(&substream->self_group.lock);
939 spin_lock(&substream->group->lock);
940 spin_lock(&substream->self_group.lock);
942 res = snd_pcm_action_group(ops, substream, state, 1);
943 spin_unlock(&substream->group->lock);
945 res = snd_pcm_action_single(ops, substream, state);
951 * Note: don't use any locks before
953 static int snd_pcm_action_lock_irq(struct action_ops *ops,
954 struct snd_pcm_substream *substream,
959 snd_pcm_stream_lock_irq(substream);
960 res = snd_pcm_action(ops, substream, state);
961 snd_pcm_stream_unlock_irq(substream);
967 static int snd_pcm_action_nonatomic(struct action_ops *ops,
968 struct snd_pcm_substream *substream,
973 down_read(&snd_pcm_link_rwsem);
974 if (snd_pcm_stream_linked(substream))
975 res = snd_pcm_action_group(ops, substream, state, 0);
977 res = snd_pcm_action_single(ops, substream, state);
978 up_read(&snd_pcm_link_rwsem);
985 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
987 struct snd_pcm_runtime *runtime = substream->runtime;
988 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
990 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
991 !snd_pcm_playback_data(substream))
993 runtime->trigger_master = substream;
997 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
999 if (substream->runtime->trigger_master != substream)
1001 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1004 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1006 if (substream->runtime->trigger_master == substream)
1007 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1010 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1012 struct snd_pcm_runtime *runtime = substream->runtime;
1013 snd_pcm_trigger_tstamp(substream);
1014 runtime->hw_ptr_jiffies = jiffies;
1015 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1017 runtime->status->state = state;
1018 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1019 runtime->silence_size > 0)
1020 snd_pcm_playback_silence(substream, ULONG_MAX);
1021 if (substream->timer)
1022 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1023 &runtime->trigger_tstamp);
1026 static struct action_ops snd_pcm_action_start = {
1027 .pre_action = snd_pcm_pre_start,
1028 .do_action = snd_pcm_do_start,
1029 .undo_action = snd_pcm_undo_start,
1030 .post_action = snd_pcm_post_start
1034 * snd_pcm_start - start all linked streams
1035 * @substream: the PCM substream instance
1037 * Return: Zero if successful, or a negative error code.
1039 int snd_pcm_start(struct snd_pcm_substream *substream)
1041 return snd_pcm_action(&snd_pcm_action_start, substream,
1042 SNDRV_PCM_STATE_RUNNING);
1048 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1050 struct snd_pcm_runtime *runtime = substream->runtime;
1051 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1053 runtime->trigger_master = substream;
1057 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1059 if (substream->runtime->trigger_master == substream &&
1060 snd_pcm_running(substream))
1061 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1062 return 0; /* unconditonally stop all substreams */
1065 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1067 struct snd_pcm_runtime *runtime = substream->runtime;
1068 if (runtime->status->state != state) {
1069 snd_pcm_trigger_tstamp(substream);
1070 if (substream->timer)
1071 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1072 &runtime->trigger_tstamp);
1073 runtime->status->state = state;
1075 wake_up(&runtime->sleep);
1076 wake_up(&runtime->tsleep);
1079 static struct action_ops snd_pcm_action_stop = {
1080 .pre_action = snd_pcm_pre_stop,
1081 .do_action = snd_pcm_do_stop,
1082 .post_action = snd_pcm_post_stop
1086 * snd_pcm_stop - try to stop all running streams in the substream group
1087 * @substream: the PCM substream instance
1088 * @state: PCM state after stopping the stream
1090 * The state of each stream is then changed to the given state unconditionally.
1092 * Return: Zero if successful, or a negative error code.
1094 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1096 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1099 EXPORT_SYMBOL(snd_pcm_stop);
1102 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1103 * @substream: the PCM substream
1105 * After stopping, the state is changed to SETUP.
1106 * Unlike snd_pcm_stop(), this affects only the given stream.
1108 * Return: Zero if succesful, or a negative error code.
1110 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1112 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1113 SNDRV_PCM_STATE_SETUP);
1119 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1121 struct snd_pcm_runtime *runtime = substream->runtime;
1122 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1125 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1127 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1129 runtime->trigger_master = substream;
1133 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1135 if (substream->runtime->trigger_master != substream)
1137 /* some drivers might use hw_ptr to recover from the pause -
1138 update the hw_ptr now */
1140 snd_pcm_update_hw_ptr(substream);
1141 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1142 * a delta between the current jiffies, this gives a large enough
1143 * delta, effectively to skip the check once.
1145 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1146 return substream->ops->trigger(substream,
1147 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1148 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1151 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1153 if (substream->runtime->trigger_master == substream)
1154 substream->ops->trigger(substream,
1155 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1156 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1159 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1161 struct snd_pcm_runtime *runtime = substream->runtime;
1162 snd_pcm_trigger_tstamp(substream);
1164 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1165 if (substream->timer)
1166 snd_timer_notify(substream->timer,
1167 SNDRV_TIMER_EVENT_MPAUSE,
1168 &runtime->trigger_tstamp);
1169 wake_up(&runtime->sleep);
1170 wake_up(&runtime->tsleep);
1172 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1173 if (substream->timer)
1174 snd_timer_notify(substream->timer,
1175 SNDRV_TIMER_EVENT_MCONTINUE,
1176 &runtime->trigger_tstamp);
1180 static struct action_ops snd_pcm_action_pause = {
1181 .pre_action = snd_pcm_pre_pause,
1182 .do_action = snd_pcm_do_pause,
1183 .undo_action = snd_pcm_undo_pause,
1184 .post_action = snd_pcm_post_pause
1188 * Push/release the pause for all linked streams.
1190 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1192 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1198 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1200 struct snd_pcm_runtime *runtime = substream->runtime;
1201 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1203 runtime->trigger_master = substream;
1207 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1209 struct snd_pcm_runtime *runtime = substream->runtime;
1210 if (runtime->trigger_master != substream)
1212 if (! snd_pcm_running(substream))
1214 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1215 return 0; /* suspend unconditionally */
1218 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1220 struct snd_pcm_runtime *runtime = substream->runtime;
1221 snd_pcm_trigger_tstamp(substream);
1222 if (substream->timer)
1223 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1224 &runtime->trigger_tstamp);
1225 runtime->status->suspended_state = runtime->status->state;
1226 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1227 wake_up(&runtime->sleep);
1228 wake_up(&runtime->tsleep);
1231 static struct action_ops snd_pcm_action_suspend = {
1232 .pre_action = snd_pcm_pre_suspend,
1233 .do_action = snd_pcm_do_suspend,
1234 .post_action = snd_pcm_post_suspend
1238 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1239 * @substream: the PCM substream
1241 * After this call, all streams are changed to SUSPENDED state.
1243 * Return: Zero if successful (or @substream is %NULL), or a negative error
1246 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1249 unsigned long flags;
1254 snd_pcm_stream_lock_irqsave(substream, flags);
1255 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1256 snd_pcm_stream_unlock_irqrestore(substream, flags);
1260 EXPORT_SYMBOL(snd_pcm_suspend);
1263 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1264 * @pcm: the PCM instance
1266 * After this call, all streams are changed to SUSPENDED state.
1268 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1270 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1272 struct snd_pcm_substream *substream;
1273 int stream, err = 0;
1278 for (stream = 0; stream < 2; stream++) {
1279 for (substream = pcm->streams[stream].substream;
1280 substream; substream = substream->next) {
1281 /* FIXME: the open/close code should lock this as well */
1282 if (substream->runtime == NULL)
1284 err = snd_pcm_suspend(substream);
1285 if (err < 0 && err != -EBUSY)
1292 EXPORT_SYMBOL(snd_pcm_suspend_all);
1296 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1298 struct snd_pcm_runtime *runtime = substream->runtime;
1299 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1301 runtime->trigger_master = substream;
1305 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1307 struct snd_pcm_runtime *runtime = substream->runtime;
1308 if (runtime->trigger_master != substream)
1310 /* DMA not running previously? */
1311 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1312 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1313 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1315 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1318 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1320 if (substream->runtime->trigger_master == substream &&
1321 snd_pcm_running(substream))
1322 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1325 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1327 struct snd_pcm_runtime *runtime = substream->runtime;
1328 snd_pcm_trigger_tstamp(substream);
1329 if (substream->timer)
1330 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1331 &runtime->trigger_tstamp);
1332 runtime->status->state = runtime->status->suspended_state;
1335 static struct action_ops snd_pcm_action_resume = {
1336 .pre_action = snd_pcm_pre_resume,
1337 .do_action = snd_pcm_do_resume,
1338 .undo_action = snd_pcm_undo_resume,
1339 .post_action = snd_pcm_post_resume
1342 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1344 struct snd_card *card = substream->pcm->card;
1347 snd_power_lock(card);
1348 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1349 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1350 snd_power_unlock(card);
1356 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1361 #endif /* CONFIG_PM */
1366 * Change the RUNNING stream(s) to XRUN state.
1368 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1370 struct snd_card *card = substream->pcm->card;
1371 struct snd_pcm_runtime *runtime = substream->runtime;
1374 snd_power_lock(card);
1375 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1376 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1381 snd_pcm_stream_lock_irq(substream);
1382 switch (runtime->status->state) {
1383 case SNDRV_PCM_STATE_XRUN:
1384 result = 0; /* already there */
1386 case SNDRV_PCM_STATE_RUNNING:
1387 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1392 snd_pcm_stream_unlock_irq(substream);
1394 snd_power_unlock(card);
1401 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1403 struct snd_pcm_runtime *runtime = substream->runtime;
1404 switch (runtime->status->state) {
1405 case SNDRV_PCM_STATE_RUNNING:
1406 case SNDRV_PCM_STATE_PREPARED:
1407 case SNDRV_PCM_STATE_PAUSED:
1408 case SNDRV_PCM_STATE_SUSPENDED:
1415 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1417 struct snd_pcm_runtime *runtime = substream->runtime;
1418 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1421 runtime->hw_ptr_base = 0;
1422 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1423 runtime->status->hw_ptr % runtime->period_size;
1424 runtime->silence_start = runtime->status->hw_ptr;
1425 runtime->silence_filled = 0;
1429 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1431 struct snd_pcm_runtime *runtime = substream->runtime;
1432 runtime->control->appl_ptr = runtime->status->hw_ptr;
1433 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1434 runtime->silence_size > 0)
1435 snd_pcm_playback_silence(substream, ULONG_MAX);
1438 static struct action_ops snd_pcm_action_reset = {
1439 .pre_action = snd_pcm_pre_reset,
1440 .do_action = snd_pcm_do_reset,
1441 .post_action = snd_pcm_post_reset
1444 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1446 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1452 /* we use the second argument for updating f_flags */
1453 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1456 struct snd_pcm_runtime *runtime = substream->runtime;
1457 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1458 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1460 if (snd_pcm_running(substream))
1462 substream->f_flags = f_flags;
1466 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1469 err = substream->ops->prepare(substream);
1472 return snd_pcm_do_reset(substream, 0);
1475 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1477 struct snd_pcm_runtime *runtime = substream->runtime;
1478 runtime->control->appl_ptr = runtime->status->hw_ptr;
1479 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1482 static struct action_ops snd_pcm_action_prepare = {
1483 .pre_action = snd_pcm_pre_prepare,
1484 .do_action = snd_pcm_do_prepare,
1485 .post_action = snd_pcm_post_prepare
1489 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1490 * @substream: the PCM substream instance
1491 * @file: file to refer f_flags
1493 * Return: Zero if successful, or a negative error code.
1495 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1499 struct snd_card *card = substream->pcm->card;
1503 f_flags = file->f_flags;
1505 f_flags = substream->f_flags;
1507 snd_power_lock(card);
1508 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1509 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1510 substream, f_flags);
1511 snd_power_unlock(card);
1519 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1521 struct snd_pcm_runtime *runtime = substream->runtime;
1522 switch (runtime->status->state) {
1523 case SNDRV_PCM_STATE_OPEN:
1524 case SNDRV_PCM_STATE_DISCONNECTED:
1525 case SNDRV_PCM_STATE_SUSPENDED:
1528 runtime->trigger_master = substream;
1532 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1534 struct snd_pcm_runtime *runtime = substream->runtime;
1535 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1536 switch (runtime->status->state) {
1537 case SNDRV_PCM_STATE_PREPARED:
1538 /* start playback stream if possible */
1539 if (! snd_pcm_playback_empty(substream)) {
1540 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1541 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1544 case SNDRV_PCM_STATE_RUNNING:
1545 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1547 case SNDRV_PCM_STATE_XRUN:
1548 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1554 /* stop running stream */
1555 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1556 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1557 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1558 snd_pcm_do_stop(substream, new_state);
1559 snd_pcm_post_stop(substream, new_state);
1565 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1569 static struct action_ops snd_pcm_action_drain_init = {
1570 .pre_action = snd_pcm_pre_drain_init,
1571 .do_action = snd_pcm_do_drain_init,
1572 .post_action = snd_pcm_post_drain_init
1575 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1578 * Drain the stream(s).
1579 * When the substream is linked, sync until the draining of all playback streams
1581 * After this call, all streams are supposed to be either SETUP or DRAINING
1582 * (capture only) state.
1584 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1587 struct snd_card *card;
1588 struct snd_pcm_runtime *runtime;
1589 struct snd_pcm_substream *s;
1594 card = substream->pcm->card;
1595 runtime = substream->runtime;
1597 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1600 snd_power_lock(card);
1601 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1602 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1604 snd_power_unlock(card);
1610 if (file->f_flags & O_NONBLOCK)
1612 } else if (substream->f_flags & O_NONBLOCK)
1615 down_read(&snd_pcm_link_rwsem);
1616 snd_pcm_stream_lock_irq(substream);
1618 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1619 snd_pcm_pause(substream, 0);
1621 /* pre-start/stop - all running streams are changed to DRAINING state */
1622 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1625 /* in non-blocking, we don't wait in ioctl but let caller poll */
1633 struct snd_pcm_runtime *to_check;
1634 if (signal_pending(current)) {
1635 result = -ERESTARTSYS;
1638 /* find a substream to drain */
1640 snd_pcm_group_for_each_entry(s, substream) {
1641 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1643 runtime = s->runtime;
1644 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1650 break; /* all drained */
1651 init_waitqueue_entry(&wait, current);
1652 add_wait_queue(&to_check->sleep, &wait);
1653 snd_pcm_stream_unlock_irq(substream);
1654 up_read(&snd_pcm_link_rwsem);
1655 snd_power_unlock(card);
1656 if (runtime->no_period_wakeup)
1657 tout = MAX_SCHEDULE_TIMEOUT;
1660 if (runtime->rate) {
1661 long t = runtime->period_size * 2 / runtime->rate;
1662 tout = max(t, tout);
1664 tout = msecs_to_jiffies(tout * 1000);
1666 tout = schedule_timeout_interruptible(tout);
1667 snd_power_lock(card);
1668 down_read(&snd_pcm_link_rwsem);
1669 snd_pcm_stream_lock_irq(substream);
1670 remove_wait_queue(&to_check->sleep, &wait);
1671 if (card->shutdown) {
1676 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1679 dev_dbg(substream->pcm->card->dev,
1680 "playback drain error (DMA or IRQ trouble?)\n");
1681 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1689 snd_pcm_stream_unlock_irq(substream);
1690 up_read(&snd_pcm_link_rwsem);
1691 snd_power_unlock(card);
1699 * Immediately put all linked substreams into SETUP state.
1701 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1703 struct snd_pcm_runtime *runtime;
1706 if (PCM_RUNTIME_CHECK(substream))
1708 runtime = substream->runtime;
1710 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1711 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1712 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1715 snd_pcm_stream_lock_irq(substream);
1717 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1718 snd_pcm_pause(substream, 0);
1720 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1721 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1722 snd_pcm_stream_unlock_irq(substream);
1728 static bool is_pcm_file(struct file *file)
1730 struct inode *inode = file_inode(file);
1733 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1735 minor = iminor(inode);
1736 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1737 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1743 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1746 struct snd_pcm_file *pcm_file;
1747 struct snd_pcm_substream *substream1;
1748 struct snd_pcm_group *group;
1749 struct fd f = fdget(fd);
1753 if (!is_pcm_file(f.file)) {
1757 pcm_file = f.file->private_data;
1758 substream1 = pcm_file->substream;
1759 group = kmalloc(sizeof(*group), GFP_KERNEL);
1764 down_write(&snd_pcm_link_rwsem);
1765 write_lock_irq(&snd_pcm_link_rwlock);
1766 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1767 substream->runtime->status->state != substream1->runtime->status->state ||
1768 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1772 if (snd_pcm_stream_linked(substream1)) {
1776 if (!snd_pcm_stream_linked(substream)) {
1777 substream->group = group;
1779 spin_lock_init(&substream->group->lock);
1780 mutex_init(&substream->group->mutex);
1781 INIT_LIST_HEAD(&substream->group->substreams);
1782 list_add_tail(&substream->link_list, &substream->group->substreams);
1783 substream->group->count = 1;
1785 list_add_tail(&substream1->link_list, &substream->group->substreams);
1786 substream->group->count++;
1787 substream1->group = substream->group;
1789 write_unlock_irq(&snd_pcm_link_rwlock);
1790 up_write(&snd_pcm_link_rwsem);
1792 snd_card_unref(substream1->pcm->card);
1799 static void relink_to_local(struct snd_pcm_substream *substream)
1801 substream->group = &substream->self_group;
1802 INIT_LIST_HEAD(&substream->self_group.substreams);
1803 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1806 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1808 struct snd_pcm_substream *s;
1811 down_write(&snd_pcm_link_rwsem);
1812 write_lock_irq(&snd_pcm_link_rwlock);
1813 if (!snd_pcm_stream_linked(substream)) {
1817 list_del(&substream->link_list);
1818 substream->group->count--;
1819 if (substream->group->count == 1) { /* detach the last stream, too */
1820 snd_pcm_group_for_each_entry(s, substream) {
1824 kfree(substream->group);
1826 relink_to_local(substream);
1828 write_unlock_irq(&snd_pcm_link_rwlock);
1829 up_write(&snd_pcm_link_rwsem);
1836 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1837 struct snd_pcm_hw_rule *rule)
1839 struct snd_interval t;
1840 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1841 hw_param_interval_c(params, rule->deps[1]), &t);
1842 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1845 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1846 struct snd_pcm_hw_rule *rule)
1848 struct snd_interval t;
1849 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1850 hw_param_interval_c(params, rule->deps[1]), &t);
1851 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1854 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1855 struct snd_pcm_hw_rule *rule)
1857 struct snd_interval t;
1858 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1859 hw_param_interval_c(params, rule->deps[1]),
1860 (unsigned long) rule->private, &t);
1861 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1864 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1865 struct snd_pcm_hw_rule *rule)
1867 struct snd_interval t;
1868 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1869 (unsigned long) rule->private,
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_format(struct snd_pcm_hw_params *params,
1875 struct snd_pcm_hw_rule *rule)
1878 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1880 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1882 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1884 if (! snd_mask_test(mask, k))
1886 bits = snd_pcm_format_physical_width(k);
1888 continue; /* ignore invalid formats */
1889 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1890 snd_mask_reset(&m, k);
1892 return snd_mask_refine(mask, &m);
1895 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1896 struct snd_pcm_hw_rule *rule)
1898 struct snd_interval t;
1904 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1906 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1908 bits = snd_pcm_format_physical_width(k);
1910 continue; /* ignore invalid formats */
1911 if (t.min > (unsigned)bits)
1913 if (t.max < (unsigned)bits)
1917 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1920 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1921 #error "Change this table"
1924 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1925 48000, 64000, 88200, 96000, 176400, 192000 };
1927 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1928 .count = ARRAY_SIZE(rates),
1932 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1933 struct snd_pcm_hw_rule *rule)
1935 struct snd_pcm_hardware *hw = rule->private;
1936 return snd_interval_list(hw_param_interval(params, rule->var),
1937 snd_pcm_known_rates.count,
1938 snd_pcm_known_rates.list, hw->rates);
1941 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1942 struct snd_pcm_hw_rule *rule)
1944 struct snd_interval t;
1945 struct snd_pcm_substream *substream = rule->private;
1947 t.max = substream->buffer_bytes_max;
1951 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1954 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1956 struct snd_pcm_runtime *runtime = substream->runtime;
1957 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1960 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1961 snd_mask_any(constrs_mask(constrs, k));
1964 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1965 snd_interval_any(constrs_interval(constrs, k));
1968 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1969 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1970 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1971 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1972 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1974 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1975 snd_pcm_hw_rule_format, NULL,
1976 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1979 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1980 snd_pcm_hw_rule_sample_bits, NULL,
1981 SNDRV_PCM_HW_PARAM_FORMAT,
1982 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1985 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1986 snd_pcm_hw_rule_div, NULL,
1987 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1990 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1991 snd_pcm_hw_rule_mul, NULL,
1992 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1996 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1997 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2000 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2001 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2002 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2005 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2006 snd_pcm_hw_rule_div, NULL,
2007 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2010 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2011 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2012 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2015 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2016 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2017 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2020 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2021 snd_pcm_hw_rule_div, NULL,
2022 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2025 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2026 snd_pcm_hw_rule_div, NULL,
2027 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2030 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2031 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2032 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2035 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2036 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2037 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2041 snd_pcm_hw_rule_mul, NULL,
2042 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2046 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2047 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2051 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2052 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2056 snd_pcm_hw_rule_muldivk, (void*) 8,
2057 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2061 snd_pcm_hw_rule_muldivk, (void*) 8,
2062 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2067 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2071 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2078 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2080 struct snd_pcm_runtime *runtime = substream->runtime;
2081 struct snd_pcm_hardware *hw = &runtime->hw;
2083 unsigned int mask = 0;
2085 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2086 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2087 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2088 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2089 if (hw_support_mmap(substream)) {
2090 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2091 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2092 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2093 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2094 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2095 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2097 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2101 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2105 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2109 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2110 hw->channels_min, hw->channels_max);
2114 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2115 hw->rate_min, hw->rate_max);
2119 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2120 hw->period_bytes_min, hw->period_bytes_max);
2124 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2125 hw->periods_min, hw->periods_max);
2129 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2130 hw->period_bytes_min, hw->buffer_bytes_max);
2134 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2135 snd_pcm_hw_rule_buffer_bytes_max, substream,
2136 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2141 if (runtime->dma_bytes) {
2142 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2147 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2148 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2149 snd_pcm_hw_rule_rate, hw,
2150 SNDRV_PCM_HW_PARAM_RATE, -1);
2155 /* FIXME: this belong to lowlevel */
2156 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2161 static void pcm_release_private(struct snd_pcm_substream *substream)
2163 snd_pcm_unlink(substream);
2166 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2168 substream->ref_count--;
2169 if (substream->ref_count > 0)
2172 snd_pcm_drop(substream);
2173 if (substream->hw_opened) {
2174 if (substream->ops->hw_free != NULL)
2175 substream->ops->hw_free(substream);
2176 substream->ops->close(substream);
2177 substream->hw_opened = 0;
2179 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2180 pm_qos_remove_request(&substream->latency_pm_qos_req);
2181 if (substream->pcm_release) {
2182 substream->pcm_release(substream);
2183 substream->pcm_release = NULL;
2185 snd_pcm_detach_substream(substream);
2188 EXPORT_SYMBOL(snd_pcm_release_substream);
2190 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2192 struct snd_pcm_substream **rsubstream)
2194 struct snd_pcm_substream *substream;
2197 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2200 if (substream->ref_count > 1) {
2201 *rsubstream = substream;
2205 err = snd_pcm_hw_constraints_init(substream);
2207 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2211 if ((err = substream->ops->open(substream)) < 0)
2214 substream->hw_opened = 1;
2216 err = snd_pcm_hw_constraints_complete(substream);
2218 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2222 *rsubstream = substream;
2226 snd_pcm_release_substream(substream);
2230 EXPORT_SYMBOL(snd_pcm_open_substream);
2232 static int snd_pcm_open_file(struct file *file,
2233 struct snd_pcm *pcm,
2236 struct snd_pcm_file *pcm_file;
2237 struct snd_pcm_substream *substream;
2240 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2244 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2245 if (pcm_file == NULL) {
2246 snd_pcm_release_substream(substream);
2249 pcm_file->substream = substream;
2250 if (substream->ref_count == 1) {
2251 substream->file = pcm_file;
2252 substream->pcm_release = pcm_release_private;
2254 file->private_data = pcm_file;
2259 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2261 struct snd_pcm *pcm;
2262 int err = nonseekable_open(inode, file);
2265 pcm = snd_lookup_minor_data(iminor(inode),
2266 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2267 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2269 snd_card_unref(pcm->card);
2273 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2275 struct snd_pcm *pcm;
2276 int err = nonseekable_open(inode, file);
2279 pcm = snd_lookup_minor_data(iminor(inode),
2280 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2281 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2283 snd_card_unref(pcm->card);
2287 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2296 err = snd_card_file_add(pcm->card, file);
2299 if (!try_module_get(pcm->card->module)) {
2303 init_waitqueue_entry(&wait, current);
2304 add_wait_queue(&pcm->open_wait, &wait);
2305 mutex_lock(&pcm->open_mutex);
2307 err = snd_pcm_open_file(file, pcm, stream);
2310 if (err == -EAGAIN) {
2311 if (file->f_flags & O_NONBLOCK) {
2317 set_current_state(TASK_INTERRUPTIBLE);
2318 mutex_unlock(&pcm->open_mutex);
2320 mutex_lock(&pcm->open_mutex);
2321 if (pcm->card->shutdown) {
2325 if (signal_pending(current)) {
2330 remove_wait_queue(&pcm->open_wait, &wait);
2331 mutex_unlock(&pcm->open_mutex);
2337 module_put(pcm->card->module);
2339 snd_card_file_remove(pcm->card, file);
2344 static int snd_pcm_release(struct inode *inode, struct file *file)
2346 struct snd_pcm *pcm;
2347 struct snd_pcm_substream *substream;
2348 struct snd_pcm_file *pcm_file;
2350 pcm_file = file->private_data;
2351 substream = pcm_file->substream;
2352 if (snd_BUG_ON(!substream))
2354 pcm = substream->pcm;
2355 mutex_lock(&pcm->open_mutex);
2356 snd_pcm_release_substream(substream);
2358 mutex_unlock(&pcm->open_mutex);
2359 wake_up(&pcm->open_wait);
2360 module_put(pcm->card->module);
2361 snd_card_file_remove(pcm->card, file);
2365 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2366 snd_pcm_uframes_t frames)
2368 struct snd_pcm_runtime *runtime = substream->runtime;
2369 snd_pcm_sframes_t appl_ptr;
2370 snd_pcm_sframes_t ret;
2371 snd_pcm_sframes_t hw_avail;
2376 snd_pcm_stream_lock_irq(substream);
2377 switch (runtime->status->state) {
2378 case SNDRV_PCM_STATE_PREPARED:
2380 case SNDRV_PCM_STATE_DRAINING:
2381 case SNDRV_PCM_STATE_RUNNING:
2382 if (snd_pcm_update_hw_ptr(substream) >= 0)
2385 case SNDRV_PCM_STATE_XRUN:
2388 case SNDRV_PCM_STATE_SUSPENDED:
2396 hw_avail = snd_pcm_playback_hw_avail(runtime);
2397 if (hw_avail <= 0) {
2401 if (frames > (snd_pcm_uframes_t)hw_avail)
2403 appl_ptr = runtime->control->appl_ptr - frames;
2405 appl_ptr += runtime->boundary;
2406 runtime->control->appl_ptr = appl_ptr;
2409 snd_pcm_stream_unlock_irq(substream);
2413 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2414 snd_pcm_uframes_t frames)
2416 struct snd_pcm_runtime *runtime = substream->runtime;
2417 snd_pcm_sframes_t appl_ptr;
2418 snd_pcm_sframes_t ret;
2419 snd_pcm_sframes_t hw_avail;
2424 snd_pcm_stream_lock_irq(substream);
2425 switch (runtime->status->state) {
2426 case SNDRV_PCM_STATE_PREPARED:
2427 case SNDRV_PCM_STATE_DRAINING:
2429 case SNDRV_PCM_STATE_RUNNING:
2430 if (snd_pcm_update_hw_ptr(substream) >= 0)
2433 case SNDRV_PCM_STATE_XRUN:
2436 case SNDRV_PCM_STATE_SUSPENDED:
2444 hw_avail = snd_pcm_capture_hw_avail(runtime);
2445 if (hw_avail <= 0) {
2449 if (frames > (snd_pcm_uframes_t)hw_avail)
2451 appl_ptr = runtime->control->appl_ptr - frames;
2453 appl_ptr += runtime->boundary;
2454 runtime->control->appl_ptr = appl_ptr;
2457 snd_pcm_stream_unlock_irq(substream);
2461 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2462 snd_pcm_uframes_t frames)
2464 struct snd_pcm_runtime *runtime = substream->runtime;
2465 snd_pcm_sframes_t appl_ptr;
2466 snd_pcm_sframes_t ret;
2467 snd_pcm_sframes_t avail;
2472 snd_pcm_stream_lock_irq(substream);
2473 switch (runtime->status->state) {
2474 case SNDRV_PCM_STATE_PREPARED:
2475 case SNDRV_PCM_STATE_PAUSED:
2477 case SNDRV_PCM_STATE_DRAINING:
2478 case SNDRV_PCM_STATE_RUNNING:
2479 if (snd_pcm_update_hw_ptr(substream) >= 0)
2482 case SNDRV_PCM_STATE_XRUN:
2485 case SNDRV_PCM_STATE_SUSPENDED:
2493 avail = snd_pcm_playback_avail(runtime);
2498 if (frames > (snd_pcm_uframes_t)avail)
2500 appl_ptr = runtime->control->appl_ptr + frames;
2501 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2502 appl_ptr -= runtime->boundary;
2503 runtime->control->appl_ptr = appl_ptr;
2506 snd_pcm_stream_unlock_irq(substream);
2510 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2511 snd_pcm_uframes_t frames)
2513 struct snd_pcm_runtime *runtime = substream->runtime;
2514 snd_pcm_sframes_t appl_ptr;
2515 snd_pcm_sframes_t ret;
2516 snd_pcm_sframes_t avail;
2521 snd_pcm_stream_lock_irq(substream);
2522 switch (runtime->status->state) {
2523 case SNDRV_PCM_STATE_PREPARED:
2524 case SNDRV_PCM_STATE_DRAINING:
2525 case SNDRV_PCM_STATE_PAUSED:
2527 case SNDRV_PCM_STATE_RUNNING:
2528 if (snd_pcm_update_hw_ptr(substream) >= 0)
2531 case SNDRV_PCM_STATE_XRUN:
2534 case SNDRV_PCM_STATE_SUSPENDED:
2542 avail = snd_pcm_capture_avail(runtime);
2547 if (frames > (snd_pcm_uframes_t)avail)
2549 appl_ptr = runtime->control->appl_ptr + frames;
2550 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2551 appl_ptr -= runtime->boundary;
2552 runtime->control->appl_ptr = appl_ptr;
2555 snd_pcm_stream_unlock_irq(substream);
2559 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2561 struct snd_pcm_runtime *runtime = substream->runtime;
2564 snd_pcm_stream_lock_irq(substream);
2565 switch (runtime->status->state) {
2566 case SNDRV_PCM_STATE_DRAINING:
2567 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2570 case SNDRV_PCM_STATE_RUNNING:
2571 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2574 case SNDRV_PCM_STATE_PREPARED:
2575 case SNDRV_PCM_STATE_SUSPENDED:
2578 case SNDRV_PCM_STATE_XRUN:
2586 snd_pcm_stream_unlock_irq(substream);
2590 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2591 snd_pcm_sframes_t __user *res)
2593 struct snd_pcm_runtime *runtime = substream->runtime;
2595 snd_pcm_sframes_t n = 0;
2597 snd_pcm_stream_lock_irq(substream);
2598 switch (runtime->status->state) {
2599 case SNDRV_PCM_STATE_DRAINING:
2600 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2603 case SNDRV_PCM_STATE_RUNNING:
2604 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2607 case SNDRV_PCM_STATE_PREPARED:
2608 case SNDRV_PCM_STATE_SUSPENDED:
2610 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2611 n = snd_pcm_playback_hw_avail(runtime);
2613 n = snd_pcm_capture_avail(runtime);
2614 n += runtime->delay;
2616 case SNDRV_PCM_STATE_XRUN:
2624 snd_pcm_stream_unlock_irq(substream);
2626 if (put_user(n, res))
2631 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2632 struct snd_pcm_sync_ptr __user *_sync_ptr)
2634 struct snd_pcm_runtime *runtime = substream->runtime;
2635 struct snd_pcm_sync_ptr sync_ptr;
2636 volatile struct snd_pcm_mmap_status *status;
2637 volatile struct snd_pcm_mmap_control *control;
2640 memset(&sync_ptr, 0, sizeof(sync_ptr));
2641 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2643 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2645 status = runtime->status;
2646 control = runtime->control;
2647 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2648 err = snd_pcm_hwsync(substream);
2652 snd_pcm_stream_lock_irq(substream);
2653 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2654 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2656 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2657 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2658 control->avail_min = sync_ptr.c.control.avail_min;
2660 sync_ptr.c.control.avail_min = control->avail_min;
2661 sync_ptr.s.status.state = status->state;
2662 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2663 sync_ptr.s.status.tstamp = status->tstamp;
2664 sync_ptr.s.status.suspended_state = status->suspended_state;
2665 snd_pcm_stream_unlock_irq(substream);
2666 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2671 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2673 struct snd_pcm_runtime *runtime = substream->runtime;
2676 if (get_user(arg, _arg))
2678 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2680 runtime->tstamp_type = arg;
2684 static int snd_pcm_common_ioctl1(struct file *file,
2685 struct snd_pcm_substream *substream,
2686 unsigned int cmd, void __user *arg)
2689 case SNDRV_PCM_IOCTL_PVERSION:
2690 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2691 case SNDRV_PCM_IOCTL_INFO:
2692 return snd_pcm_info_user(substream, arg);
2693 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2695 case SNDRV_PCM_IOCTL_TTSTAMP:
2696 return snd_pcm_tstamp(substream, arg);
2697 case SNDRV_PCM_IOCTL_HW_REFINE:
2698 return snd_pcm_hw_refine_user(substream, arg);
2699 case SNDRV_PCM_IOCTL_HW_PARAMS:
2700 return snd_pcm_hw_params_user(substream, arg);
2701 case SNDRV_PCM_IOCTL_HW_FREE:
2702 return snd_pcm_hw_free(substream);
2703 case SNDRV_PCM_IOCTL_SW_PARAMS:
2704 return snd_pcm_sw_params_user(substream, arg);
2705 case SNDRV_PCM_IOCTL_STATUS:
2706 return snd_pcm_status_user(substream, arg);
2707 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2708 return snd_pcm_channel_info_user(substream, arg);
2709 case SNDRV_PCM_IOCTL_PREPARE:
2710 return snd_pcm_prepare(substream, file);
2711 case SNDRV_PCM_IOCTL_RESET:
2712 return snd_pcm_reset(substream);
2713 case SNDRV_PCM_IOCTL_START:
2714 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2715 case SNDRV_PCM_IOCTL_LINK:
2716 return snd_pcm_link(substream, (int)(unsigned long) arg);
2717 case SNDRV_PCM_IOCTL_UNLINK:
2718 return snd_pcm_unlink(substream);
2719 case SNDRV_PCM_IOCTL_RESUME:
2720 return snd_pcm_resume(substream);
2721 case SNDRV_PCM_IOCTL_XRUN:
2722 return snd_pcm_xrun(substream);
2723 case SNDRV_PCM_IOCTL_HWSYNC:
2724 return snd_pcm_hwsync(substream);
2725 case SNDRV_PCM_IOCTL_DELAY:
2726 return snd_pcm_delay(substream, arg);
2727 case SNDRV_PCM_IOCTL_SYNC_PTR:
2728 return snd_pcm_sync_ptr(substream, arg);
2729 #ifdef CONFIG_SND_SUPPORT_OLD_API
2730 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2731 return snd_pcm_hw_refine_old_user(substream, arg);
2732 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2733 return snd_pcm_hw_params_old_user(substream, arg);
2735 case SNDRV_PCM_IOCTL_DRAIN:
2736 return snd_pcm_drain(substream, file);
2737 case SNDRV_PCM_IOCTL_DROP:
2738 return snd_pcm_drop(substream);
2739 case SNDRV_PCM_IOCTL_PAUSE:
2742 snd_pcm_stream_lock_irq(substream);
2743 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2744 snd_pcm_stream_unlock_irq(substream);
2748 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2752 static int snd_pcm_playback_ioctl1(struct file *file,
2753 struct snd_pcm_substream *substream,
2754 unsigned int cmd, void __user *arg)
2756 if (snd_BUG_ON(!substream))
2758 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2761 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2763 struct snd_xferi xferi;
2764 struct snd_xferi __user *_xferi = arg;
2765 struct snd_pcm_runtime *runtime = substream->runtime;
2766 snd_pcm_sframes_t result;
2767 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2769 if (put_user(0, &_xferi->result))
2771 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2773 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2774 __put_user(result, &_xferi->result);
2775 return result < 0 ? result : 0;
2777 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2779 struct snd_xfern xfern;
2780 struct snd_xfern __user *_xfern = arg;
2781 struct snd_pcm_runtime *runtime = substream->runtime;
2783 snd_pcm_sframes_t result;
2784 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2786 if (runtime->channels > 128)
2788 if (put_user(0, &_xfern->result))
2790 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2793 bufs = memdup_user(xfern.bufs,
2794 sizeof(void *) * runtime->channels);
2796 return PTR_ERR(bufs);
2797 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2799 __put_user(result, &_xfern->result);
2800 return result < 0 ? result : 0;
2802 case SNDRV_PCM_IOCTL_REWIND:
2804 snd_pcm_uframes_t frames;
2805 snd_pcm_uframes_t __user *_frames = arg;
2806 snd_pcm_sframes_t result;
2807 if (get_user(frames, _frames))
2809 if (put_user(0, _frames))
2811 result = snd_pcm_playback_rewind(substream, frames);
2812 __put_user(result, _frames);
2813 return result < 0 ? result : 0;
2815 case SNDRV_PCM_IOCTL_FORWARD:
2817 snd_pcm_uframes_t frames;
2818 snd_pcm_uframes_t __user *_frames = arg;
2819 snd_pcm_sframes_t result;
2820 if (get_user(frames, _frames))
2822 if (put_user(0, _frames))
2824 result = snd_pcm_playback_forward(substream, frames);
2825 __put_user(result, _frames);
2826 return result < 0 ? result : 0;
2829 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2832 static int snd_pcm_capture_ioctl1(struct file *file,
2833 struct snd_pcm_substream *substream,
2834 unsigned int cmd, void __user *arg)
2836 if (snd_BUG_ON(!substream))
2838 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2841 case SNDRV_PCM_IOCTL_READI_FRAMES:
2843 struct snd_xferi xferi;
2844 struct snd_xferi __user *_xferi = arg;
2845 struct snd_pcm_runtime *runtime = substream->runtime;
2846 snd_pcm_sframes_t result;
2847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2849 if (put_user(0, &_xferi->result))
2851 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2853 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2854 __put_user(result, &_xferi->result);
2855 return result < 0 ? result : 0;
2857 case SNDRV_PCM_IOCTL_READN_FRAMES:
2859 struct snd_xfern xfern;
2860 struct snd_xfern __user *_xfern = arg;
2861 struct snd_pcm_runtime *runtime = substream->runtime;
2863 snd_pcm_sframes_t result;
2864 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2866 if (runtime->channels > 128)
2868 if (put_user(0, &_xfern->result))
2870 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2873 bufs = memdup_user(xfern.bufs,
2874 sizeof(void *) * runtime->channels);
2876 return PTR_ERR(bufs);
2877 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2879 __put_user(result, &_xfern->result);
2880 return result < 0 ? result : 0;
2882 case SNDRV_PCM_IOCTL_REWIND:
2884 snd_pcm_uframes_t frames;
2885 snd_pcm_uframes_t __user *_frames = arg;
2886 snd_pcm_sframes_t result;
2887 if (get_user(frames, _frames))
2889 if (put_user(0, _frames))
2891 result = snd_pcm_capture_rewind(substream, frames);
2892 __put_user(result, _frames);
2893 return result < 0 ? result : 0;
2895 case SNDRV_PCM_IOCTL_FORWARD:
2897 snd_pcm_uframes_t frames;
2898 snd_pcm_uframes_t __user *_frames = arg;
2899 snd_pcm_sframes_t result;
2900 if (get_user(frames, _frames))
2902 if (put_user(0, _frames))
2904 result = snd_pcm_capture_forward(substream, frames);
2905 __put_user(result, _frames);
2906 return result < 0 ? result : 0;
2909 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2912 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2915 struct snd_pcm_file *pcm_file;
2917 pcm_file = file->private_data;
2919 if (((cmd >> 8) & 0xff) != 'A')
2922 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2923 (void __user *)arg);
2926 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2929 struct snd_pcm_file *pcm_file;
2931 pcm_file = file->private_data;
2933 if (((cmd >> 8) & 0xff) != 'A')
2936 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2937 (void __user *)arg);
2940 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2941 unsigned int cmd, void *arg)
2946 fs = snd_enter_user();
2947 switch (substream->stream) {
2948 case SNDRV_PCM_STREAM_PLAYBACK:
2949 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2950 (void __user *)arg);
2952 case SNDRV_PCM_STREAM_CAPTURE:
2953 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2954 (void __user *)arg);
2964 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2966 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2969 struct snd_pcm_file *pcm_file;
2970 struct snd_pcm_substream *substream;
2971 struct snd_pcm_runtime *runtime;
2972 snd_pcm_sframes_t result;
2974 pcm_file = file->private_data;
2975 substream = pcm_file->substream;
2976 if (PCM_RUNTIME_CHECK(substream))
2978 runtime = substream->runtime;
2979 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2981 if (!frame_aligned(runtime, count))
2983 count = bytes_to_frames(runtime, count);
2984 result = snd_pcm_lib_read(substream, buf, count);
2986 result = frames_to_bytes(runtime, result);
2990 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2991 size_t count, loff_t * offset)
2993 struct snd_pcm_file *pcm_file;
2994 struct snd_pcm_substream *substream;
2995 struct snd_pcm_runtime *runtime;
2996 snd_pcm_sframes_t result;
2998 pcm_file = file->private_data;
2999 substream = pcm_file->substream;
3000 if (PCM_RUNTIME_CHECK(substream))
3002 runtime = substream->runtime;
3003 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3005 if (!frame_aligned(runtime, count))
3007 count = bytes_to_frames(runtime, count);
3008 result = snd_pcm_lib_write(substream, buf, count);
3010 result = frames_to_bytes(runtime, result);
3014 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
3015 unsigned long nr_segs, loff_t pos)
3018 struct snd_pcm_file *pcm_file;
3019 struct snd_pcm_substream *substream;
3020 struct snd_pcm_runtime *runtime;
3021 snd_pcm_sframes_t result;
3024 snd_pcm_uframes_t frames;
3026 pcm_file = iocb->ki_filp->private_data;
3027 substream = pcm_file->substream;
3028 if (PCM_RUNTIME_CHECK(substream))
3030 runtime = substream->runtime;
3031 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3033 if (nr_segs > 1024 || nr_segs != runtime->channels)
3035 if (!frame_aligned(runtime, iov->iov_len))
3037 frames = bytes_to_samples(runtime, iov->iov_len);
3038 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3041 for (i = 0; i < nr_segs; ++i)
3042 bufs[i] = iov[i].iov_base;
3043 result = snd_pcm_lib_readv(substream, bufs, frames);
3045 result = frames_to_bytes(runtime, result);
3050 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
3051 unsigned long nr_segs, loff_t pos)
3053 struct snd_pcm_file *pcm_file;
3054 struct snd_pcm_substream *substream;
3055 struct snd_pcm_runtime *runtime;
3056 snd_pcm_sframes_t result;
3059 snd_pcm_uframes_t frames;
3061 pcm_file = iocb->ki_filp->private_data;
3062 substream = pcm_file->substream;
3063 if (PCM_RUNTIME_CHECK(substream))
3065 runtime = substream->runtime;
3066 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3068 if (nr_segs > 128 || nr_segs != runtime->channels ||
3069 !frame_aligned(runtime, iov->iov_len))
3071 frames = bytes_to_samples(runtime, iov->iov_len);
3072 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3075 for (i = 0; i < nr_segs; ++i)
3076 bufs[i] = iov[i].iov_base;
3077 result = snd_pcm_lib_writev(substream, bufs, frames);
3079 result = frames_to_bytes(runtime, result);
3084 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3086 struct snd_pcm_file *pcm_file;
3087 struct snd_pcm_substream *substream;
3088 struct snd_pcm_runtime *runtime;
3090 snd_pcm_uframes_t avail;
3092 pcm_file = file->private_data;
3094 substream = pcm_file->substream;
3095 if (PCM_RUNTIME_CHECK(substream))
3097 runtime = substream->runtime;
3099 poll_wait(file, &runtime->sleep, wait);
3101 snd_pcm_stream_lock_irq(substream);
3102 avail = snd_pcm_playback_avail(runtime);
3103 switch (runtime->status->state) {
3104 case SNDRV_PCM_STATE_RUNNING:
3105 case SNDRV_PCM_STATE_PREPARED:
3106 case SNDRV_PCM_STATE_PAUSED:
3107 if (avail >= runtime->control->avail_min) {
3108 mask = POLLOUT | POLLWRNORM;
3112 case SNDRV_PCM_STATE_DRAINING:
3116 mask = POLLOUT | POLLWRNORM | POLLERR;
3119 snd_pcm_stream_unlock_irq(substream);
3123 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3125 struct snd_pcm_file *pcm_file;
3126 struct snd_pcm_substream *substream;
3127 struct snd_pcm_runtime *runtime;
3129 snd_pcm_uframes_t avail;
3131 pcm_file = file->private_data;
3133 substream = pcm_file->substream;
3134 if (PCM_RUNTIME_CHECK(substream))
3136 runtime = substream->runtime;
3138 poll_wait(file, &runtime->sleep, wait);
3140 snd_pcm_stream_lock_irq(substream);
3141 avail = snd_pcm_capture_avail(runtime);
3142 switch (runtime->status->state) {
3143 case SNDRV_PCM_STATE_RUNNING:
3144 case SNDRV_PCM_STATE_PREPARED:
3145 case SNDRV_PCM_STATE_PAUSED:
3146 if (avail >= runtime->control->avail_min) {
3147 mask = POLLIN | POLLRDNORM;
3152 case SNDRV_PCM_STATE_DRAINING:
3154 mask = POLLIN | POLLRDNORM;
3159 mask = POLLIN | POLLRDNORM | POLLERR;
3162 snd_pcm_stream_unlock_irq(substream);
3171 * Only on coherent architectures, we can mmap the status and the control records
3172 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3174 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3176 * mmap status record
3178 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3179 struct vm_fault *vmf)
3181 struct snd_pcm_substream *substream = area->vm_private_data;
3182 struct snd_pcm_runtime *runtime;
3184 if (substream == NULL)
3185 return VM_FAULT_SIGBUS;
3186 runtime = substream->runtime;
3187 vmf->page = virt_to_page(runtime->status);
3188 get_page(vmf->page);
3192 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3194 .fault = snd_pcm_mmap_status_fault,
3197 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3198 struct vm_area_struct *area)
3201 if (!(area->vm_flags & VM_READ))
3203 size = area->vm_end - area->vm_start;
3204 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3206 area->vm_ops = &snd_pcm_vm_ops_status;
3207 area->vm_private_data = substream;
3208 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3213 * mmap control record
3215 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3216 struct vm_fault *vmf)
3218 struct snd_pcm_substream *substream = area->vm_private_data;
3219 struct snd_pcm_runtime *runtime;
3221 if (substream == NULL)
3222 return VM_FAULT_SIGBUS;
3223 runtime = substream->runtime;
3224 vmf->page = virt_to_page(runtime->control);
3225 get_page(vmf->page);
3229 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3231 .fault = snd_pcm_mmap_control_fault,
3234 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3235 struct vm_area_struct *area)
3238 if (!(area->vm_flags & VM_READ))
3240 size = area->vm_end - area->vm_start;
3241 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3243 area->vm_ops = &snd_pcm_vm_ops_control;
3244 area->vm_private_data = substream;
3245 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3248 #else /* ! coherent mmap */
3250 * don't support mmap for status and control records.
3252 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3253 struct vm_area_struct *area)
3257 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3258 struct vm_area_struct *area)
3262 #endif /* coherent mmap */
3264 static inline struct page *
3265 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3267 void *vaddr = substream->runtime->dma_area + ofs;
3268 return virt_to_page(vaddr);
3272 * fault callback for mmapping a RAM page
3274 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3275 struct vm_fault *vmf)
3277 struct snd_pcm_substream *substream = area->vm_private_data;
3278 struct snd_pcm_runtime *runtime;
3279 unsigned long offset;
3283 if (substream == NULL)
3284 return VM_FAULT_SIGBUS;
3285 runtime = substream->runtime;
3286 offset = vmf->pgoff << PAGE_SHIFT;
3287 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3288 if (offset > dma_bytes - PAGE_SIZE)
3289 return VM_FAULT_SIGBUS;
3290 if (substream->ops->page)
3291 page = substream->ops->page(substream, offset);
3293 page = snd_pcm_default_page_ops(substream, offset);
3295 return VM_FAULT_SIGBUS;
3301 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3302 .open = snd_pcm_mmap_data_open,
3303 .close = snd_pcm_mmap_data_close,
3306 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3307 .open = snd_pcm_mmap_data_open,
3308 .close = snd_pcm_mmap_data_close,
3309 .fault = snd_pcm_mmap_data_fault,
3313 * mmap the DMA buffer on RAM
3317 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3318 * @substream: PCM substream
3321 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3322 * this function is invoked implicitly.
3324 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3325 struct vm_area_struct *area)
3327 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3328 #ifdef CONFIG_GENERIC_ALLOCATOR
3329 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3330 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3331 return remap_pfn_range(area, area->vm_start,
3332 substream->dma_buffer.addr >> PAGE_SHIFT,
3333 area->vm_end - area->vm_start, area->vm_page_prot);
3335 #endif /* CONFIG_GENERIC_ALLOCATOR */
3336 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3337 if (!substream->ops->page &&
3338 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3339 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3341 substream->runtime->dma_area,
3342 substream->runtime->dma_addr,
3343 area->vm_end - area->vm_start);
3344 #endif /* CONFIG_X86 */
3345 /* mmap with fault handler */
3346 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3349 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3352 * mmap the DMA buffer on I/O memory area
3354 #if SNDRV_PCM_INFO_MMAP_IOMEM
3356 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3357 * @substream: PCM substream
3360 * When your hardware uses the iomapped pages as the hardware buffer and
3361 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3362 * is supposed to work only on limited architectures.
3364 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3365 struct vm_area_struct *area)
3367 struct snd_pcm_runtime *runtime = substream->runtime;;
3369 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3370 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3373 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3374 #endif /* SNDRV_PCM_INFO_MMAP */
3379 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3380 struct vm_area_struct *area)
3382 struct snd_pcm_runtime *runtime;
3384 unsigned long offset;
3388 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3389 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3392 if (!(area->vm_flags & VM_READ))
3395 runtime = substream->runtime;
3396 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3398 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3400 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3401 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3403 size = area->vm_end - area->vm_start;
3404 offset = area->vm_pgoff << PAGE_SHIFT;
3405 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3406 if ((size_t)size > dma_bytes)
3408 if (offset > dma_bytes - size)
3411 area->vm_ops = &snd_pcm_vm_ops_data;
3412 area->vm_private_data = substream;
3413 if (substream->ops->mmap)
3414 err = substream->ops->mmap(substream, area);
3416 err = snd_pcm_lib_default_mmap(substream, area);
3418 atomic_inc(&substream->mmap_count);
3422 EXPORT_SYMBOL(snd_pcm_mmap_data);
3424 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3426 struct snd_pcm_file * pcm_file;
3427 struct snd_pcm_substream *substream;
3428 unsigned long offset;
3430 pcm_file = file->private_data;
3431 substream = pcm_file->substream;
3432 if (PCM_RUNTIME_CHECK(substream))
3435 offset = area->vm_pgoff << PAGE_SHIFT;
3437 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3438 if (pcm_file->no_compat_mmap)
3440 return snd_pcm_mmap_status(substream, file, area);
3441 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3442 if (pcm_file->no_compat_mmap)
3444 return snd_pcm_mmap_control(substream, file, area);
3446 return snd_pcm_mmap_data(substream, file, area);
3451 static int snd_pcm_fasync(int fd, struct file * file, int on)
3453 struct snd_pcm_file * pcm_file;
3454 struct snd_pcm_substream *substream;
3455 struct snd_pcm_runtime *runtime;
3457 pcm_file = file->private_data;
3458 substream = pcm_file->substream;
3459 if (PCM_RUNTIME_CHECK(substream))
3461 runtime = substream->runtime;
3462 return fasync_helper(fd, file, on, &runtime->fasync);
3468 #ifdef CONFIG_COMPAT
3469 #include "pcm_compat.c"
3471 #define snd_pcm_ioctl_compat NULL
3475 * To be removed helpers to keep binary compatibility
3478 #ifdef CONFIG_SND_SUPPORT_OLD_API
3479 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3480 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3482 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3483 struct snd_pcm_hw_params_old *oparams)
3487 memset(params, 0, sizeof(*params));
3488 params->flags = oparams->flags;
3489 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3490 params->masks[i].bits[0] = oparams->masks[i];
3491 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3492 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3493 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3494 params->info = oparams->info;
3495 params->msbits = oparams->msbits;
3496 params->rate_num = oparams->rate_num;
3497 params->rate_den = oparams->rate_den;
3498 params->fifo_size = oparams->fifo_size;
3501 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3502 struct snd_pcm_hw_params *params)
3506 memset(oparams, 0, sizeof(*oparams));
3507 oparams->flags = params->flags;
3508 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3509 oparams->masks[i] = params->masks[i].bits[0];
3510 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3511 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3512 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3513 oparams->info = params->info;
3514 oparams->msbits = params->msbits;
3515 oparams->rate_num = params->rate_num;
3516 oparams->rate_den = params->rate_den;
3517 oparams->fifo_size = params->fifo_size;
3520 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3521 struct snd_pcm_hw_params_old __user * _oparams)
3523 struct snd_pcm_hw_params *params;
3524 struct snd_pcm_hw_params_old *oparams = NULL;
3527 params = kmalloc(sizeof(*params), GFP_KERNEL);
3531 oparams = memdup_user(_oparams, sizeof(*oparams));
3532 if (IS_ERR(oparams)) {
3533 err = PTR_ERR(oparams);
3536 snd_pcm_hw_convert_from_old_params(params, oparams);
3537 err = snd_pcm_hw_refine(substream, params);
3538 snd_pcm_hw_convert_to_old_params(oparams, params);
3539 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3550 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3551 struct snd_pcm_hw_params_old __user * _oparams)
3553 struct snd_pcm_hw_params *params;
3554 struct snd_pcm_hw_params_old *oparams = NULL;
3557 params = kmalloc(sizeof(*params), GFP_KERNEL);
3561 oparams = memdup_user(_oparams, sizeof(*oparams));
3562 if (IS_ERR(oparams)) {
3563 err = PTR_ERR(oparams);
3566 snd_pcm_hw_convert_from_old_params(params, oparams);
3567 err = snd_pcm_hw_params(substream, params);
3568 snd_pcm_hw_convert_to_old_params(oparams, params);
3569 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3579 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3582 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3585 unsigned long pgoff,
3586 unsigned long flags)
3588 struct snd_pcm_file *pcm_file = file->private_data;
3589 struct snd_pcm_substream *substream = pcm_file->substream;
3590 struct snd_pcm_runtime *runtime = substream->runtime;
3591 unsigned long offset = pgoff << PAGE_SHIFT;
3594 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3595 return (unsigned long)runtime->status;
3596 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3597 return (unsigned long)runtime->control;
3599 return (unsigned long)runtime->dma_area + offset;
3603 # define snd_pcm_get_unmapped_area NULL
3610 const struct file_operations snd_pcm_f_ops[2] = {
3612 .owner = THIS_MODULE,
3613 .write = snd_pcm_write,
3614 .aio_write = snd_pcm_aio_write,
3615 .open = snd_pcm_playback_open,
3616 .release = snd_pcm_release,
3617 .llseek = no_llseek,
3618 .poll = snd_pcm_playback_poll,
3619 .unlocked_ioctl = snd_pcm_playback_ioctl,
3620 .compat_ioctl = snd_pcm_ioctl_compat,
3621 .mmap = snd_pcm_mmap,
3622 .fasync = snd_pcm_fasync,
3623 .get_unmapped_area = snd_pcm_get_unmapped_area,
3626 .owner = THIS_MODULE,
3627 .read = snd_pcm_read,
3628 .aio_read = snd_pcm_aio_read,
3629 .open = snd_pcm_capture_open,
3630 .release = snd_pcm_release,
3631 .llseek = no_llseek,
3632 .poll = snd_pcm_capture_poll,
3633 .unlocked_ioctl = snd_pcm_capture_ioctl,
3634 .compat_ioctl = snd_pcm_ioctl_compat,
3635 .mmap = snd_pcm_mmap,
3636 .fasync = snd_pcm_fasync,
3637 .get_unmapped_area = snd_pcm_get_unmapped_area,