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);
950 static int snd_pcm_action_lock_mutex(struct action_ops *ops,
951 struct snd_pcm_substream *substream,
956 down_read(&snd_pcm_link_rwsem);
957 if (snd_pcm_stream_linked(substream)) {
958 mutex_lock(&substream->group->mutex);
959 mutex_lock(&substream->self_group.mutex);
960 res = snd_pcm_action_group(ops, substream, state, 1);
961 mutex_unlock(&substream->self_group.mutex);
962 mutex_unlock(&substream->group->mutex);
964 mutex_lock(&substream->self_group.mutex);
965 res = snd_pcm_action_single(ops, substream, state);
966 mutex_unlock(&substream->self_group.mutex);
968 up_read(&snd_pcm_link_rwsem);
973 * Note: don't use any locks before
975 static int snd_pcm_action_lock_irq(struct action_ops *ops,
976 struct snd_pcm_substream *substream,
981 if (substream->pcm->nonatomic)
982 return snd_pcm_action_lock_mutex(ops, substream, state);
984 read_lock_irq(&snd_pcm_link_rwlock);
985 if (snd_pcm_stream_linked(substream)) {
986 spin_lock(&substream->group->lock);
987 spin_lock(&substream->self_group.lock);
988 res = snd_pcm_action_group(ops, substream, state, 1);
989 spin_unlock(&substream->self_group.lock);
990 spin_unlock(&substream->group->lock);
992 spin_lock(&substream->self_group.lock);
993 res = snd_pcm_action_single(ops, substream, state);
994 spin_unlock(&substream->self_group.lock);
996 read_unlock_irq(&snd_pcm_link_rwlock);
1002 static int snd_pcm_action_nonatomic(struct action_ops *ops,
1003 struct snd_pcm_substream *substream,
1008 down_read(&snd_pcm_link_rwsem);
1009 if (snd_pcm_stream_linked(substream))
1010 res = snd_pcm_action_group(ops, substream, state, 0);
1012 res = snd_pcm_action_single(ops, substream, state);
1013 up_read(&snd_pcm_link_rwsem);
1020 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1022 struct snd_pcm_runtime *runtime = substream->runtime;
1023 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1025 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1026 !snd_pcm_playback_data(substream))
1028 runtime->trigger_master = substream;
1032 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1034 if (substream->runtime->trigger_master != substream)
1036 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1039 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1041 if (substream->runtime->trigger_master == substream)
1042 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1045 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1047 struct snd_pcm_runtime *runtime = substream->runtime;
1048 snd_pcm_trigger_tstamp(substream);
1049 runtime->hw_ptr_jiffies = jiffies;
1050 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1052 runtime->status->state = state;
1053 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1054 runtime->silence_size > 0)
1055 snd_pcm_playback_silence(substream, ULONG_MAX);
1056 if (substream->timer)
1057 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1058 &runtime->trigger_tstamp);
1061 static struct action_ops snd_pcm_action_start = {
1062 .pre_action = snd_pcm_pre_start,
1063 .do_action = snd_pcm_do_start,
1064 .undo_action = snd_pcm_undo_start,
1065 .post_action = snd_pcm_post_start
1069 * snd_pcm_start - start all linked streams
1070 * @substream: the PCM substream instance
1072 * Return: Zero if successful, or a negative error code.
1074 int snd_pcm_start(struct snd_pcm_substream *substream)
1076 return snd_pcm_action(&snd_pcm_action_start, substream,
1077 SNDRV_PCM_STATE_RUNNING);
1083 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1085 struct snd_pcm_runtime *runtime = substream->runtime;
1086 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1088 runtime->trigger_master = substream;
1092 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1094 if (substream->runtime->trigger_master == substream &&
1095 snd_pcm_running(substream))
1096 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1097 return 0; /* unconditonally stop all substreams */
1100 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1102 struct snd_pcm_runtime *runtime = substream->runtime;
1103 if (runtime->status->state != state) {
1104 snd_pcm_trigger_tstamp(substream);
1105 if (substream->timer)
1106 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1107 &runtime->trigger_tstamp);
1108 runtime->status->state = state;
1110 wake_up(&runtime->sleep);
1111 wake_up(&runtime->tsleep);
1114 static struct action_ops snd_pcm_action_stop = {
1115 .pre_action = snd_pcm_pre_stop,
1116 .do_action = snd_pcm_do_stop,
1117 .post_action = snd_pcm_post_stop
1121 * snd_pcm_stop - try to stop all running streams in the substream group
1122 * @substream: the PCM substream instance
1123 * @state: PCM state after stopping the stream
1125 * The state of each stream is then changed to the given state unconditionally.
1127 * Return: Zero if successful, or a negative error code.
1129 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1131 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1134 EXPORT_SYMBOL(snd_pcm_stop);
1137 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1138 * @substream: the PCM substream
1140 * After stopping, the state is changed to SETUP.
1141 * Unlike snd_pcm_stop(), this affects only the given stream.
1143 * Return: Zero if succesful, or a negative error code.
1145 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1147 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1148 SNDRV_PCM_STATE_SETUP);
1154 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1156 struct snd_pcm_runtime *runtime = substream->runtime;
1157 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1160 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1162 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1164 runtime->trigger_master = substream;
1168 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1170 if (substream->runtime->trigger_master != substream)
1172 /* some drivers might use hw_ptr to recover from the pause -
1173 update the hw_ptr now */
1175 snd_pcm_update_hw_ptr(substream);
1176 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1177 * a delta between the current jiffies, this gives a large enough
1178 * delta, effectively to skip the check once.
1180 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1181 return substream->ops->trigger(substream,
1182 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1183 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1186 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1188 if (substream->runtime->trigger_master == substream)
1189 substream->ops->trigger(substream,
1190 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1191 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1194 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1196 struct snd_pcm_runtime *runtime = substream->runtime;
1197 snd_pcm_trigger_tstamp(substream);
1199 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1200 if (substream->timer)
1201 snd_timer_notify(substream->timer,
1202 SNDRV_TIMER_EVENT_MPAUSE,
1203 &runtime->trigger_tstamp);
1204 wake_up(&runtime->sleep);
1205 wake_up(&runtime->tsleep);
1207 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1208 if (substream->timer)
1209 snd_timer_notify(substream->timer,
1210 SNDRV_TIMER_EVENT_MCONTINUE,
1211 &runtime->trigger_tstamp);
1215 static struct action_ops snd_pcm_action_pause = {
1216 .pre_action = snd_pcm_pre_pause,
1217 .do_action = snd_pcm_do_pause,
1218 .undo_action = snd_pcm_undo_pause,
1219 .post_action = snd_pcm_post_pause
1223 * Push/release the pause for all linked streams.
1225 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1227 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1233 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1235 struct snd_pcm_runtime *runtime = substream->runtime;
1236 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1238 runtime->trigger_master = substream;
1242 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1244 struct snd_pcm_runtime *runtime = substream->runtime;
1245 if (runtime->trigger_master != substream)
1247 if (! snd_pcm_running(substream))
1249 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1250 return 0; /* suspend unconditionally */
1253 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1255 struct snd_pcm_runtime *runtime = substream->runtime;
1256 snd_pcm_trigger_tstamp(substream);
1257 if (substream->timer)
1258 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1259 &runtime->trigger_tstamp);
1260 runtime->status->suspended_state = runtime->status->state;
1261 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1262 wake_up(&runtime->sleep);
1263 wake_up(&runtime->tsleep);
1266 static struct action_ops snd_pcm_action_suspend = {
1267 .pre_action = snd_pcm_pre_suspend,
1268 .do_action = snd_pcm_do_suspend,
1269 .post_action = snd_pcm_post_suspend
1273 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1274 * @substream: the PCM substream
1276 * After this call, all streams are changed to SUSPENDED state.
1278 * Return: Zero if successful (or @substream is %NULL), or a negative error
1281 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1284 unsigned long flags;
1289 snd_pcm_stream_lock_irqsave(substream, flags);
1290 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1291 snd_pcm_stream_unlock_irqrestore(substream, flags);
1295 EXPORT_SYMBOL(snd_pcm_suspend);
1298 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1299 * @pcm: the PCM instance
1301 * After this call, all streams are changed to SUSPENDED state.
1303 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1305 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1307 struct snd_pcm_substream *substream;
1308 int stream, err = 0;
1313 for (stream = 0; stream < 2; stream++) {
1314 for (substream = pcm->streams[stream].substream;
1315 substream; substream = substream->next) {
1316 /* FIXME: the open/close code should lock this as well */
1317 if (substream->runtime == NULL)
1319 err = snd_pcm_suspend(substream);
1320 if (err < 0 && err != -EBUSY)
1327 EXPORT_SYMBOL(snd_pcm_suspend_all);
1331 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1333 struct snd_pcm_runtime *runtime = substream->runtime;
1334 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1336 runtime->trigger_master = substream;
1340 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1342 struct snd_pcm_runtime *runtime = substream->runtime;
1343 if (runtime->trigger_master != substream)
1345 /* DMA not running previously? */
1346 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1347 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1348 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1350 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1353 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1355 if (substream->runtime->trigger_master == substream &&
1356 snd_pcm_running(substream))
1357 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1360 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1362 struct snd_pcm_runtime *runtime = substream->runtime;
1363 snd_pcm_trigger_tstamp(substream);
1364 if (substream->timer)
1365 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1366 &runtime->trigger_tstamp);
1367 runtime->status->state = runtime->status->suspended_state;
1370 static struct action_ops snd_pcm_action_resume = {
1371 .pre_action = snd_pcm_pre_resume,
1372 .do_action = snd_pcm_do_resume,
1373 .undo_action = snd_pcm_undo_resume,
1374 .post_action = snd_pcm_post_resume
1377 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1379 struct snd_card *card = substream->pcm->card;
1382 snd_power_lock(card);
1383 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1384 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1385 snd_power_unlock(card);
1391 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1396 #endif /* CONFIG_PM */
1401 * Change the RUNNING stream(s) to XRUN state.
1403 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1405 struct snd_card *card = substream->pcm->card;
1406 struct snd_pcm_runtime *runtime = substream->runtime;
1409 snd_power_lock(card);
1410 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1411 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1416 snd_pcm_stream_lock_irq(substream);
1417 switch (runtime->status->state) {
1418 case SNDRV_PCM_STATE_XRUN:
1419 result = 0; /* already there */
1421 case SNDRV_PCM_STATE_RUNNING:
1422 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1427 snd_pcm_stream_unlock_irq(substream);
1429 snd_power_unlock(card);
1436 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1438 struct snd_pcm_runtime *runtime = substream->runtime;
1439 switch (runtime->status->state) {
1440 case SNDRV_PCM_STATE_RUNNING:
1441 case SNDRV_PCM_STATE_PREPARED:
1442 case SNDRV_PCM_STATE_PAUSED:
1443 case SNDRV_PCM_STATE_SUSPENDED:
1450 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1452 struct snd_pcm_runtime *runtime = substream->runtime;
1453 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1456 runtime->hw_ptr_base = 0;
1457 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1458 runtime->status->hw_ptr % runtime->period_size;
1459 runtime->silence_start = runtime->status->hw_ptr;
1460 runtime->silence_filled = 0;
1464 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1466 struct snd_pcm_runtime *runtime = substream->runtime;
1467 runtime->control->appl_ptr = runtime->status->hw_ptr;
1468 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1469 runtime->silence_size > 0)
1470 snd_pcm_playback_silence(substream, ULONG_MAX);
1473 static struct action_ops snd_pcm_action_reset = {
1474 .pre_action = snd_pcm_pre_reset,
1475 .do_action = snd_pcm_do_reset,
1476 .post_action = snd_pcm_post_reset
1479 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1481 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1487 /* we use the second argument for updating f_flags */
1488 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1491 struct snd_pcm_runtime *runtime = substream->runtime;
1492 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1493 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1495 if (snd_pcm_running(substream))
1497 substream->f_flags = f_flags;
1501 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1504 err = substream->ops->prepare(substream);
1507 return snd_pcm_do_reset(substream, 0);
1510 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1512 struct snd_pcm_runtime *runtime = substream->runtime;
1513 runtime->control->appl_ptr = runtime->status->hw_ptr;
1514 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1517 static struct action_ops snd_pcm_action_prepare = {
1518 .pre_action = snd_pcm_pre_prepare,
1519 .do_action = snd_pcm_do_prepare,
1520 .post_action = snd_pcm_post_prepare
1524 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1525 * @substream: the PCM substream instance
1526 * @file: file to refer f_flags
1528 * Return: Zero if successful, or a negative error code.
1530 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1534 struct snd_card *card = substream->pcm->card;
1538 f_flags = file->f_flags;
1540 f_flags = substream->f_flags;
1542 snd_power_lock(card);
1543 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1544 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1545 substream, f_flags);
1546 snd_power_unlock(card);
1554 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1556 struct snd_pcm_runtime *runtime = substream->runtime;
1557 switch (runtime->status->state) {
1558 case SNDRV_PCM_STATE_OPEN:
1559 case SNDRV_PCM_STATE_DISCONNECTED:
1560 case SNDRV_PCM_STATE_SUSPENDED:
1563 runtime->trigger_master = substream;
1567 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1569 struct snd_pcm_runtime *runtime = substream->runtime;
1570 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1571 switch (runtime->status->state) {
1572 case SNDRV_PCM_STATE_PREPARED:
1573 /* start playback stream if possible */
1574 if (! snd_pcm_playback_empty(substream)) {
1575 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1576 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1579 case SNDRV_PCM_STATE_RUNNING:
1580 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1582 case SNDRV_PCM_STATE_XRUN:
1583 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1589 /* stop running stream */
1590 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1591 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1592 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1593 snd_pcm_do_stop(substream, new_state);
1594 snd_pcm_post_stop(substream, new_state);
1600 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1604 static struct action_ops snd_pcm_action_drain_init = {
1605 .pre_action = snd_pcm_pre_drain_init,
1606 .do_action = snd_pcm_do_drain_init,
1607 .post_action = snd_pcm_post_drain_init
1610 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1613 * Drain the stream(s).
1614 * When the substream is linked, sync until the draining of all playback streams
1616 * After this call, all streams are supposed to be either SETUP or DRAINING
1617 * (capture only) state.
1619 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1622 struct snd_card *card;
1623 struct snd_pcm_runtime *runtime;
1624 struct snd_pcm_substream *s;
1629 card = substream->pcm->card;
1630 runtime = substream->runtime;
1632 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1635 snd_power_lock(card);
1636 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1637 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1639 snd_power_unlock(card);
1645 if (file->f_flags & O_NONBLOCK)
1647 } else if (substream->f_flags & O_NONBLOCK)
1650 down_read(&snd_pcm_link_rwsem);
1651 snd_pcm_stream_lock_irq(substream);
1653 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1654 snd_pcm_pause(substream, 0);
1656 /* pre-start/stop - all running streams are changed to DRAINING state */
1657 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1660 /* in non-blocking, we don't wait in ioctl but let caller poll */
1668 struct snd_pcm_runtime *to_check;
1669 if (signal_pending(current)) {
1670 result = -ERESTARTSYS;
1673 /* find a substream to drain */
1675 snd_pcm_group_for_each_entry(s, substream) {
1676 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1678 runtime = s->runtime;
1679 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1685 break; /* all drained */
1686 init_waitqueue_entry(&wait, current);
1687 add_wait_queue(&to_check->sleep, &wait);
1688 snd_pcm_stream_unlock_irq(substream);
1689 up_read(&snd_pcm_link_rwsem);
1690 snd_power_unlock(card);
1691 if (runtime->no_period_wakeup)
1692 tout = MAX_SCHEDULE_TIMEOUT;
1695 if (runtime->rate) {
1696 long t = runtime->period_size * 2 / runtime->rate;
1697 tout = max(t, tout);
1699 tout = msecs_to_jiffies(tout * 1000);
1701 tout = schedule_timeout_interruptible(tout);
1702 snd_power_lock(card);
1703 down_read(&snd_pcm_link_rwsem);
1704 snd_pcm_stream_lock_irq(substream);
1705 remove_wait_queue(&to_check->sleep, &wait);
1706 if (card->shutdown) {
1711 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1714 dev_dbg(substream->pcm->card->dev,
1715 "playback drain error (DMA or IRQ trouble?)\n");
1716 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1724 snd_pcm_stream_unlock_irq(substream);
1725 up_read(&snd_pcm_link_rwsem);
1726 snd_power_unlock(card);
1734 * Immediately put all linked substreams into SETUP state.
1736 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1738 struct snd_pcm_runtime *runtime;
1741 if (PCM_RUNTIME_CHECK(substream))
1743 runtime = substream->runtime;
1745 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1746 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1747 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1750 snd_pcm_stream_lock_irq(substream);
1752 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1753 snd_pcm_pause(substream, 0);
1755 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1756 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1757 snd_pcm_stream_unlock_irq(substream);
1763 static bool is_pcm_file(struct file *file)
1765 struct inode *inode = file_inode(file);
1768 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1770 minor = iminor(inode);
1771 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1772 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1778 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1781 struct snd_pcm_file *pcm_file;
1782 struct snd_pcm_substream *substream1;
1783 struct snd_pcm_group *group;
1784 struct fd f = fdget(fd);
1788 if (!is_pcm_file(f.file)) {
1792 pcm_file = f.file->private_data;
1793 substream1 = pcm_file->substream;
1794 group = kmalloc(sizeof(*group), GFP_KERNEL);
1799 down_write(&snd_pcm_link_rwsem);
1800 write_lock_irq(&snd_pcm_link_rwlock);
1801 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1802 substream->runtime->status->state != substream1->runtime->status->state ||
1803 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1807 if (snd_pcm_stream_linked(substream1)) {
1811 if (!snd_pcm_stream_linked(substream)) {
1812 substream->group = group;
1814 spin_lock_init(&substream->group->lock);
1815 mutex_init(&substream->group->mutex);
1816 INIT_LIST_HEAD(&substream->group->substreams);
1817 list_add_tail(&substream->link_list, &substream->group->substreams);
1818 substream->group->count = 1;
1820 list_add_tail(&substream1->link_list, &substream->group->substreams);
1821 substream->group->count++;
1822 substream1->group = substream->group;
1824 write_unlock_irq(&snd_pcm_link_rwlock);
1825 up_write(&snd_pcm_link_rwsem);
1827 snd_card_unref(substream1->pcm->card);
1834 static void relink_to_local(struct snd_pcm_substream *substream)
1836 substream->group = &substream->self_group;
1837 INIT_LIST_HEAD(&substream->self_group.substreams);
1838 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1841 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1843 struct snd_pcm_substream *s;
1846 down_write(&snd_pcm_link_rwsem);
1847 write_lock_irq(&snd_pcm_link_rwlock);
1848 if (!snd_pcm_stream_linked(substream)) {
1852 list_del(&substream->link_list);
1853 substream->group->count--;
1854 if (substream->group->count == 1) { /* detach the last stream, too */
1855 snd_pcm_group_for_each_entry(s, substream) {
1859 kfree(substream->group);
1861 relink_to_local(substream);
1863 write_unlock_irq(&snd_pcm_link_rwlock);
1864 up_write(&snd_pcm_link_rwsem);
1871 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1872 struct snd_pcm_hw_rule *rule)
1874 struct snd_interval t;
1875 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1876 hw_param_interval_c(params, rule->deps[1]), &t);
1877 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1880 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1881 struct snd_pcm_hw_rule *rule)
1883 struct snd_interval t;
1884 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1885 hw_param_interval_c(params, rule->deps[1]), &t);
1886 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1889 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1890 struct snd_pcm_hw_rule *rule)
1892 struct snd_interval t;
1893 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1894 hw_param_interval_c(params, rule->deps[1]),
1895 (unsigned long) rule->private, &t);
1896 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1899 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1900 struct snd_pcm_hw_rule *rule)
1902 struct snd_interval t;
1903 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1904 (unsigned long) rule->private,
1905 hw_param_interval_c(params, rule->deps[1]), &t);
1906 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1909 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1910 struct snd_pcm_hw_rule *rule)
1913 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1915 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1917 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1919 if (! snd_mask_test(mask, k))
1921 bits = snd_pcm_format_physical_width(k);
1923 continue; /* ignore invalid formats */
1924 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1925 snd_mask_reset(&m, k);
1927 return snd_mask_refine(mask, &m);
1930 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1931 struct snd_pcm_hw_rule *rule)
1933 struct snd_interval t;
1939 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1941 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1943 bits = snd_pcm_format_physical_width(k);
1945 continue; /* ignore invalid formats */
1946 if (t.min > (unsigned)bits)
1948 if (t.max < (unsigned)bits)
1952 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1955 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1956 #error "Change this table"
1959 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1960 48000, 64000, 88200, 96000, 176400, 192000 };
1962 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1963 .count = ARRAY_SIZE(rates),
1967 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1968 struct snd_pcm_hw_rule *rule)
1970 struct snd_pcm_hardware *hw = rule->private;
1971 return snd_interval_list(hw_param_interval(params, rule->var),
1972 snd_pcm_known_rates.count,
1973 snd_pcm_known_rates.list, hw->rates);
1976 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1977 struct snd_pcm_hw_rule *rule)
1979 struct snd_interval t;
1980 struct snd_pcm_substream *substream = rule->private;
1982 t.max = substream->buffer_bytes_max;
1986 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1989 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1991 struct snd_pcm_runtime *runtime = substream->runtime;
1992 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1995 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1996 snd_mask_any(constrs_mask(constrs, k));
1999 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2000 snd_interval_any(constrs_interval(constrs, k));
2003 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2004 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2005 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2006 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2007 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2009 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2010 snd_pcm_hw_rule_format, NULL,
2011 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2014 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2015 snd_pcm_hw_rule_sample_bits, NULL,
2016 SNDRV_PCM_HW_PARAM_FORMAT,
2017 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2020 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2021 snd_pcm_hw_rule_div, NULL,
2022 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2025 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2026 snd_pcm_hw_rule_mul, NULL,
2027 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2030 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2031 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2032 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2035 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2036 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2037 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2040 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2041 snd_pcm_hw_rule_div, NULL,
2042 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2045 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2046 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2047 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2051 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2052 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2056 snd_pcm_hw_rule_div, NULL,
2057 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2060 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2061 snd_pcm_hw_rule_div, NULL,
2062 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2065 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2066 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2067 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2070 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2071 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2072 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2075 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2076 snd_pcm_hw_rule_mul, NULL,
2077 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2080 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2081 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2082 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2085 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2086 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2087 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2090 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2091 snd_pcm_hw_rule_muldivk, (void*) 8,
2092 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2095 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2096 snd_pcm_hw_rule_muldivk, (void*) 8,
2097 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2100 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2101 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2102 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2105 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2106 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2107 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2113 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2115 struct snd_pcm_runtime *runtime = substream->runtime;
2116 struct snd_pcm_hardware *hw = &runtime->hw;
2118 unsigned int mask = 0;
2120 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2121 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2122 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2123 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2124 if (hw_support_mmap(substream)) {
2125 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2126 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2127 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2128 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2129 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2130 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2132 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2136 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2140 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2144 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2145 hw->channels_min, hw->channels_max);
2149 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2150 hw->rate_min, hw->rate_max);
2154 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2155 hw->period_bytes_min, hw->period_bytes_max);
2159 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2160 hw->periods_min, hw->periods_max);
2164 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2165 hw->period_bytes_min, hw->buffer_bytes_max);
2169 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2170 snd_pcm_hw_rule_buffer_bytes_max, substream,
2171 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2176 if (runtime->dma_bytes) {
2177 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2182 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2183 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2184 snd_pcm_hw_rule_rate, hw,
2185 SNDRV_PCM_HW_PARAM_RATE, -1);
2190 /* FIXME: this belong to lowlevel */
2191 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2196 static void pcm_release_private(struct snd_pcm_substream *substream)
2198 snd_pcm_unlink(substream);
2201 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2203 substream->ref_count--;
2204 if (substream->ref_count > 0)
2207 snd_pcm_drop(substream);
2208 if (substream->hw_opened) {
2209 if (substream->ops->hw_free != NULL)
2210 substream->ops->hw_free(substream);
2211 substream->ops->close(substream);
2212 substream->hw_opened = 0;
2214 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2215 pm_qos_remove_request(&substream->latency_pm_qos_req);
2216 if (substream->pcm_release) {
2217 substream->pcm_release(substream);
2218 substream->pcm_release = NULL;
2220 snd_pcm_detach_substream(substream);
2223 EXPORT_SYMBOL(snd_pcm_release_substream);
2225 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2227 struct snd_pcm_substream **rsubstream)
2229 struct snd_pcm_substream *substream;
2232 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2235 if (substream->ref_count > 1) {
2236 *rsubstream = substream;
2240 err = snd_pcm_hw_constraints_init(substream);
2242 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2246 if ((err = substream->ops->open(substream)) < 0)
2249 substream->hw_opened = 1;
2251 err = snd_pcm_hw_constraints_complete(substream);
2253 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2257 *rsubstream = substream;
2261 snd_pcm_release_substream(substream);
2265 EXPORT_SYMBOL(snd_pcm_open_substream);
2267 static int snd_pcm_open_file(struct file *file,
2268 struct snd_pcm *pcm,
2271 struct snd_pcm_file *pcm_file;
2272 struct snd_pcm_substream *substream;
2275 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2279 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2280 if (pcm_file == NULL) {
2281 snd_pcm_release_substream(substream);
2284 pcm_file->substream = substream;
2285 if (substream->ref_count == 1) {
2286 substream->file = pcm_file;
2287 substream->pcm_release = pcm_release_private;
2289 file->private_data = pcm_file;
2294 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2296 struct snd_pcm *pcm;
2297 int err = nonseekable_open(inode, file);
2300 pcm = snd_lookup_minor_data(iminor(inode),
2301 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2302 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2304 snd_card_unref(pcm->card);
2308 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2310 struct snd_pcm *pcm;
2311 int err = nonseekable_open(inode, file);
2314 pcm = snd_lookup_minor_data(iminor(inode),
2315 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2316 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2318 snd_card_unref(pcm->card);
2322 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2331 err = snd_card_file_add(pcm->card, file);
2334 if (!try_module_get(pcm->card->module)) {
2338 init_waitqueue_entry(&wait, current);
2339 add_wait_queue(&pcm->open_wait, &wait);
2340 mutex_lock(&pcm->open_mutex);
2342 err = snd_pcm_open_file(file, pcm, stream);
2345 if (err == -EAGAIN) {
2346 if (file->f_flags & O_NONBLOCK) {
2352 set_current_state(TASK_INTERRUPTIBLE);
2353 mutex_unlock(&pcm->open_mutex);
2355 mutex_lock(&pcm->open_mutex);
2356 if (pcm->card->shutdown) {
2360 if (signal_pending(current)) {
2365 remove_wait_queue(&pcm->open_wait, &wait);
2366 mutex_unlock(&pcm->open_mutex);
2372 module_put(pcm->card->module);
2374 snd_card_file_remove(pcm->card, file);
2379 static int snd_pcm_release(struct inode *inode, struct file *file)
2381 struct snd_pcm *pcm;
2382 struct snd_pcm_substream *substream;
2383 struct snd_pcm_file *pcm_file;
2385 pcm_file = file->private_data;
2386 substream = pcm_file->substream;
2387 if (snd_BUG_ON(!substream))
2389 pcm = substream->pcm;
2390 mutex_lock(&pcm->open_mutex);
2391 snd_pcm_release_substream(substream);
2393 mutex_unlock(&pcm->open_mutex);
2394 wake_up(&pcm->open_wait);
2395 module_put(pcm->card->module);
2396 snd_card_file_remove(pcm->card, file);
2400 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2401 snd_pcm_uframes_t frames)
2403 struct snd_pcm_runtime *runtime = substream->runtime;
2404 snd_pcm_sframes_t appl_ptr;
2405 snd_pcm_sframes_t ret;
2406 snd_pcm_sframes_t hw_avail;
2411 snd_pcm_stream_lock_irq(substream);
2412 switch (runtime->status->state) {
2413 case SNDRV_PCM_STATE_PREPARED:
2415 case SNDRV_PCM_STATE_DRAINING:
2416 case SNDRV_PCM_STATE_RUNNING:
2417 if (snd_pcm_update_hw_ptr(substream) >= 0)
2420 case SNDRV_PCM_STATE_XRUN:
2423 case SNDRV_PCM_STATE_SUSPENDED:
2431 hw_avail = snd_pcm_playback_hw_avail(runtime);
2432 if (hw_avail <= 0) {
2436 if (frames > (snd_pcm_uframes_t)hw_avail)
2438 appl_ptr = runtime->control->appl_ptr - frames;
2440 appl_ptr += runtime->boundary;
2441 runtime->control->appl_ptr = appl_ptr;
2444 snd_pcm_stream_unlock_irq(substream);
2448 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2449 snd_pcm_uframes_t frames)
2451 struct snd_pcm_runtime *runtime = substream->runtime;
2452 snd_pcm_sframes_t appl_ptr;
2453 snd_pcm_sframes_t ret;
2454 snd_pcm_sframes_t hw_avail;
2459 snd_pcm_stream_lock_irq(substream);
2460 switch (runtime->status->state) {
2461 case SNDRV_PCM_STATE_PREPARED:
2462 case SNDRV_PCM_STATE_DRAINING:
2464 case SNDRV_PCM_STATE_RUNNING:
2465 if (snd_pcm_update_hw_ptr(substream) >= 0)
2468 case SNDRV_PCM_STATE_XRUN:
2471 case SNDRV_PCM_STATE_SUSPENDED:
2479 hw_avail = snd_pcm_capture_hw_avail(runtime);
2480 if (hw_avail <= 0) {
2484 if (frames > (snd_pcm_uframes_t)hw_avail)
2486 appl_ptr = runtime->control->appl_ptr - frames;
2488 appl_ptr += runtime->boundary;
2489 runtime->control->appl_ptr = appl_ptr;
2492 snd_pcm_stream_unlock_irq(substream);
2496 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2497 snd_pcm_uframes_t frames)
2499 struct snd_pcm_runtime *runtime = substream->runtime;
2500 snd_pcm_sframes_t appl_ptr;
2501 snd_pcm_sframes_t ret;
2502 snd_pcm_sframes_t avail;
2507 snd_pcm_stream_lock_irq(substream);
2508 switch (runtime->status->state) {
2509 case SNDRV_PCM_STATE_PREPARED:
2510 case SNDRV_PCM_STATE_PAUSED:
2512 case SNDRV_PCM_STATE_DRAINING:
2513 case SNDRV_PCM_STATE_RUNNING:
2514 if (snd_pcm_update_hw_ptr(substream) >= 0)
2517 case SNDRV_PCM_STATE_XRUN:
2520 case SNDRV_PCM_STATE_SUSPENDED:
2528 avail = snd_pcm_playback_avail(runtime);
2533 if (frames > (snd_pcm_uframes_t)avail)
2535 appl_ptr = runtime->control->appl_ptr + frames;
2536 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2537 appl_ptr -= runtime->boundary;
2538 runtime->control->appl_ptr = appl_ptr;
2541 snd_pcm_stream_unlock_irq(substream);
2545 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2546 snd_pcm_uframes_t frames)
2548 struct snd_pcm_runtime *runtime = substream->runtime;
2549 snd_pcm_sframes_t appl_ptr;
2550 snd_pcm_sframes_t ret;
2551 snd_pcm_sframes_t avail;
2556 snd_pcm_stream_lock_irq(substream);
2557 switch (runtime->status->state) {
2558 case SNDRV_PCM_STATE_PREPARED:
2559 case SNDRV_PCM_STATE_DRAINING:
2560 case SNDRV_PCM_STATE_PAUSED:
2562 case SNDRV_PCM_STATE_RUNNING:
2563 if (snd_pcm_update_hw_ptr(substream) >= 0)
2566 case SNDRV_PCM_STATE_XRUN:
2569 case SNDRV_PCM_STATE_SUSPENDED:
2577 avail = snd_pcm_capture_avail(runtime);
2582 if (frames > (snd_pcm_uframes_t)avail)
2584 appl_ptr = runtime->control->appl_ptr + frames;
2585 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2586 appl_ptr -= runtime->boundary;
2587 runtime->control->appl_ptr = appl_ptr;
2590 snd_pcm_stream_unlock_irq(substream);
2594 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2596 struct snd_pcm_runtime *runtime = substream->runtime;
2599 snd_pcm_stream_lock_irq(substream);
2600 switch (runtime->status->state) {
2601 case SNDRV_PCM_STATE_DRAINING:
2602 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2605 case SNDRV_PCM_STATE_RUNNING:
2606 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2609 case SNDRV_PCM_STATE_PREPARED:
2610 case SNDRV_PCM_STATE_SUSPENDED:
2613 case SNDRV_PCM_STATE_XRUN:
2621 snd_pcm_stream_unlock_irq(substream);
2625 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2626 snd_pcm_sframes_t __user *res)
2628 struct snd_pcm_runtime *runtime = substream->runtime;
2630 snd_pcm_sframes_t n = 0;
2632 snd_pcm_stream_lock_irq(substream);
2633 switch (runtime->status->state) {
2634 case SNDRV_PCM_STATE_DRAINING:
2635 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2638 case SNDRV_PCM_STATE_RUNNING:
2639 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2642 case SNDRV_PCM_STATE_PREPARED:
2643 case SNDRV_PCM_STATE_SUSPENDED:
2645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2646 n = snd_pcm_playback_hw_avail(runtime);
2648 n = snd_pcm_capture_avail(runtime);
2649 n += runtime->delay;
2651 case SNDRV_PCM_STATE_XRUN:
2659 snd_pcm_stream_unlock_irq(substream);
2661 if (put_user(n, res))
2666 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2667 struct snd_pcm_sync_ptr __user *_sync_ptr)
2669 struct snd_pcm_runtime *runtime = substream->runtime;
2670 struct snd_pcm_sync_ptr sync_ptr;
2671 volatile struct snd_pcm_mmap_status *status;
2672 volatile struct snd_pcm_mmap_control *control;
2675 memset(&sync_ptr, 0, sizeof(sync_ptr));
2676 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2678 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2680 status = runtime->status;
2681 control = runtime->control;
2682 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2683 err = snd_pcm_hwsync(substream);
2687 snd_pcm_stream_lock_irq(substream);
2688 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2689 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2691 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2692 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2693 control->avail_min = sync_ptr.c.control.avail_min;
2695 sync_ptr.c.control.avail_min = control->avail_min;
2696 sync_ptr.s.status.state = status->state;
2697 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2698 sync_ptr.s.status.tstamp = status->tstamp;
2699 sync_ptr.s.status.suspended_state = status->suspended_state;
2700 snd_pcm_stream_unlock_irq(substream);
2701 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2706 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2708 struct snd_pcm_runtime *runtime = substream->runtime;
2711 if (get_user(arg, _arg))
2713 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2715 runtime->tstamp_type = arg;
2719 static int snd_pcm_common_ioctl1(struct file *file,
2720 struct snd_pcm_substream *substream,
2721 unsigned int cmd, void __user *arg)
2724 case SNDRV_PCM_IOCTL_PVERSION:
2725 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2726 case SNDRV_PCM_IOCTL_INFO:
2727 return snd_pcm_info_user(substream, arg);
2728 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2730 case SNDRV_PCM_IOCTL_TTSTAMP:
2731 return snd_pcm_tstamp(substream, arg);
2732 case SNDRV_PCM_IOCTL_HW_REFINE:
2733 return snd_pcm_hw_refine_user(substream, arg);
2734 case SNDRV_PCM_IOCTL_HW_PARAMS:
2735 return snd_pcm_hw_params_user(substream, arg);
2736 case SNDRV_PCM_IOCTL_HW_FREE:
2737 return snd_pcm_hw_free(substream);
2738 case SNDRV_PCM_IOCTL_SW_PARAMS:
2739 return snd_pcm_sw_params_user(substream, arg);
2740 case SNDRV_PCM_IOCTL_STATUS:
2741 return snd_pcm_status_user(substream, arg);
2742 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2743 return snd_pcm_channel_info_user(substream, arg);
2744 case SNDRV_PCM_IOCTL_PREPARE:
2745 return snd_pcm_prepare(substream, file);
2746 case SNDRV_PCM_IOCTL_RESET:
2747 return snd_pcm_reset(substream);
2748 case SNDRV_PCM_IOCTL_START:
2749 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2750 case SNDRV_PCM_IOCTL_LINK:
2751 return snd_pcm_link(substream, (int)(unsigned long) arg);
2752 case SNDRV_PCM_IOCTL_UNLINK:
2753 return snd_pcm_unlink(substream);
2754 case SNDRV_PCM_IOCTL_RESUME:
2755 return snd_pcm_resume(substream);
2756 case SNDRV_PCM_IOCTL_XRUN:
2757 return snd_pcm_xrun(substream);
2758 case SNDRV_PCM_IOCTL_HWSYNC:
2759 return snd_pcm_hwsync(substream);
2760 case SNDRV_PCM_IOCTL_DELAY:
2761 return snd_pcm_delay(substream, arg);
2762 case SNDRV_PCM_IOCTL_SYNC_PTR:
2763 return snd_pcm_sync_ptr(substream, arg);
2764 #ifdef CONFIG_SND_SUPPORT_OLD_API
2765 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2766 return snd_pcm_hw_refine_old_user(substream, arg);
2767 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2768 return snd_pcm_hw_params_old_user(substream, arg);
2770 case SNDRV_PCM_IOCTL_DRAIN:
2771 return snd_pcm_drain(substream, file);
2772 case SNDRV_PCM_IOCTL_DROP:
2773 return snd_pcm_drop(substream);
2774 case SNDRV_PCM_IOCTL_PAUSE:
2777 snd_pcm_stream_lock_irq(substream);
2778 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2779 snd_pcm_stream_unlock_irq(substream);
2783 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2787 static int snd_pcm_playback_ioctl1(struct file *file,
2788 struct snd_pcm_substream *substream,
2789 unsigned int cmd, void __user *arg)
2791 if (snd_BUG_ON(!substream))
2793 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2796 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2798 struct snd_xferi xferi;
2799 struct snd_xferi __user *_xferi = arg;
2800 struct snd_pcm_runtime *runtime = substream->runtime;
2801 snd_pcm_sframes_t result;
2802 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2804 if (put_user(0, &_xferi->result))
2806 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2808 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2809 __put_user(result, &_xferi->result);
2810 return result < 0 ? result : 0;
2812 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2814 struct snd_xfern xfern;
2815 struct snd_xfern __user *_xfern = arg;
2816 struct snd_pcm_runtime *runtime = substream->runtime;
2818 snd_pcm_sframes_t result;
2819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 if (runtime->channels > 128)
2823 if (put_user(0, &_xfern->result))
2825 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2828 bufs = memdup_user(xfern.bufs,
2829 sizeof(void *) * runtime->channels);
2831 return PTR_ERR(bufs);
2832 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2834 __put_user(result, &_xfern->result);
2835 return result < 0 ? result : 0;
2837 case SNDRV_PCM_IOCTL_REWIND:
2839 snd_pcm_uframes_t frames;
2840 snd_pcm_uframes_t __user *_frames = arg;
2841 snd_pcm_sframes_t result;
2842 if (get_user(frames, _frames))
2844 if (put_user(0, _frames))
2846 result = snd_pcm_playback_rewind(substream, frames);
2847 __put_user(result, _frames);
2848 return result < 0 ? result : 0;
2850 case SNDRV_PCM_IOCTL_FORWARD:
2852 snd_pcm_uframes_t frames;
2853 snd_pcm_uframes_t __user *_frames = arg;
2854 snd_pcm_sframes_t result;
2855 if (get_user(frames, _frames))
2857 if (put_user(0, _frames))
2859 result = snd_pcm_playback_forward(substream, frames);
2860 __put_user(result, _frames);
2861 return result < 0 ? result : 0;
2864 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2867 static int snd_pcm_capture_ioctl1(struct file *file,
2868 struct snd_pcm_substream *substream,
2869 unsigned int cmd, void __user *arg)
2871 if (snd_BUG_ON(!substream))
2873 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2876 case SNDRV_PCM_IOCTL_READI_FRAMES:
2878 struct snd_xferi xferi;
2879 struct snd_xferi __user *_xferi = arg;
2880 struct snd_pcm_runtime *runtime = substream->runtime;
2881 snd_pcm_sframes_t result;
2882 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2884 if (put_user(0, &_xferi->result))
2886 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2888 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2889 __put_user(result, &_xferi->result);
2890 return result < 0 ? result : 0;
2892 case SNDRV_PCM_IOCTL_READN_FRAMES:
2894 struct snd_xfern xfern;
2895 struct snd_xfern __user *_xfern = arg;
2896 struct snd_pcm_runtime *runtime = substream->runtime;
2898 snd_pcm_sframes_t result;
2899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2901 if (runtime->channels > 128)
2903 if (put_user(0, &_xfern->result))
2905 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2908 bufs = memdup_user(xfern.bufs,
2909 sizeof(void *) * runtime->channels);
2911 return PTR_ERR(bufs);
2912 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2914 __put_user(result, &_xfern->result);
2915 return result < 0 ? result : 0;
2917 case SNDRV_PCM_IOCTL_REWIND:
2919 snd_pcm_uframes_t frames;
2920 snd_pcm_uframes_t __user *_frames = arg;
2921 snd_pcm_sframes_t result;
2922 if (get_user(frames, _frames))
2924 if (put_user(0, _frames))
2926 result = snd_pcm_capture_rewind(substream, frames);
2927 __put_user(result, _frames);
2928 return result < 0 ? result : 0;
2930 case SNDRV_PCM_IOCTL_FORWARD:
2932 snd_pcm_uframes_t frames;
2933 snd_pcm_uframes_t __user *_frames = arg;
2934 snd_pcm_sframes_t result;
2935 if (get_user(frames, _frames))
2937 if (put_user(0, _frames))
2939 result = snd_pcm_capture_forward(substream, frames);
2940 __put_user(result, _frames);
2941 return result < 0 ? result : 0;
2944 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2947 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2950 struct snd_pcm_file *pcm_file;
2952 pcm_file = file->private_data;
2954 if (((cmd >> 8) & 0xff) != 'A')
2957 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2958 (void __user *)arg);
2961 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2964 struct snd_pcm_file *pcm_file;
2966 pcm_file = file->private_data;
2968 if (((cmd >> 8) & 0xff) != 'A')
2971 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2972 (void __user *)arg);
2975 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2976 unsigned int cmd, void *arg)
2981 fs = snd_enter_user();
2982 switch (substream->stream) {
2983 case SNDRV_PCM_STREAM_PLAYBACK:
2984 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2985 (void __user *)arg);
2987 case SNDRV_PCM_STREAM_CAPTURE:
2988 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2989 (void __user *)arg);
2999 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3001 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3004 struct snd_pcm_file *pcm_file;
3005 struct snd_pcm_substream *substream;
3006 struct snd_pcm_runtime *runtime;
3007 snd_pcm_sframes_t result;
3009 pcm_file = file->private_data;
3010 substream = pcm_file->substream;
3011 if (PCM_RUNTIME_CHECK(substream))
3013 runtime = substream->runtime;
3014 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3016 if (!frame_aligned(runtime, count))
3018 count = bytes_to_frames(runtime, count);
3019 result = snd_pcm_lib_read(substream, buf, count);
3021 result = frames_to_bytes(runtime, result);
3025 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3026 size_t count, loff_t * offset)
3028 struct snd_pcm_file *pcm_file;
3029 struct snd_pcm_substream *substream;
3030 struct snd_pcm_runtime *runtime;
3031 snd_pcm_sframes_t result;
3033 pcm_file = file->private_data;
3034 substream = pcm_file->substream;
3035 if (PCM_RUNTIME_CHECK(substream))
3037 runtime = substream->runtime;
3038 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3040 if (!frame_aligned(runtime, count))
3042 count = bytes_to_frames(runtime, count);
3043 result = snd_pcm_lib_write(substream, buf, count);
3045 result = frames_to_bytes(runtime, result);
3049 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
3050 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 > 1024 || nr_segs != runtime->channels)
3070 if (!frame_aligned(runtime, iov->iov_len))
3072 frames = bytes_to_samples(runtime, iov->iov_len);
3073 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3076 for (i = 0; i < nr_segs; ++i)
3077 bufs[i] = iov[i].iov_base;
3078 result = snd_pcm_lib_readv(substream, bufs, frames);
3080 result = frames_to_bytes(runtime, result);
3085 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
3086 unsigned long nr_segs, loff_t pos)
3088 struct snd_pcm_file *pcm_file;
3089 struct snd_pcm_substream *substream;
3090 struct snd_pcm_runtime *runtime;
3091 snd_pcm_sframes_t result;
3094 snd_pcm_uframes_t frames;
3096 pcm_file = iocb->ki_filp->private_data;
3097 substream = pcm_file->substream;
3098 if (PCM_RUNTIME_CHECK(substream))
3100 runtime = substream->runtime;
3101 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3103 if (nr_segs > 128 || nr_segs != runtime->channels ||
3104 !frame_aligned(runtime, iov->iov_len))
3106 frames = bytes_to_samples(runtime, iov->iov_len);
3107 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3110 for (i = 0; i < nr_segs; ++i)
3111 bufs[i] = iov[i].iov_base;
3112 result = snd_pcm_lib_writev(substream, bufs, frames);
3114 result = frames_to_bytes(runtime, result);
3119 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3121 struct snd_pcm_file *pcm_file;
3122 struct snd_pcm_substream *substream;
3123 struct snd_pcm_runtime *runtime;
3125 snd_pcm_uframes_t avail;
3127 pcm_file = file->private_data;
3129 substream = pcm_file->substream;
3130 if (PCM_RUNTIME_CHECK(substream))
3132 runtime = substream->runtime;
3134 poll_wait(file, &runtime->sleep, wait);
3136 snd_pcm_stream_lock_irq(substream);
3137 avail = snd_pcm_playback_avail(runtime);
3138 switch (runtime->status->state) {
3139 case SNDRV_PCM_STATE_RUNNING:
3140 case SNDRV_PCM_STATE_PREPARED:
3141 case SNDRV_PCM_STATE_PAUSED:
3142 if (avail >= runtime->control->avail_min) {
3143 mask = POLLOUT | POLLWRNORM;
3147 case SNDRV_PCM_STATE_DRAINING:
3151 mask = POLLOUT | POLLWRNORM | POLLERR;
3154 snd_pcm_stream_unlock_irq(substream);
3158 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3160 struct snd_pcm_file *pcm_file;
3161 struct snd_pcm_substream *substream;
3162 struct snd_pcm_runtime *runtime;
3164 snd_pcm_uframes_t avail;
3166 pcm_file = file->private_data;
3168 substream = pcm_file->substream;
3169 if (PCM_RUNTIME_CHECK(substream))
3171 runtime = substream->runtime;
3173 poll_wait(file, &runtime->sleep, wait);
3175 snd_pcm_stream_lock_irq(substream);
3176 avail = snd_pcm_capture_avail(runtime);
3177 switch (runtime->status->state) {
3178 case SNDRV_PCM_STATE_RUNNING:
3179 case SNDRV_PCM_STATE_PREPARED:
3180 case SNDRV_PCM_STATE_PAUSED:
3181 if (avail >= runtime->control->avail_min) {
3182 mask = POLLIN | POLLRDNORM;
3187 case SNDRV_PCM_STATE_DRAINING:
3189 mask = POLLIN | POLLRDNORM;
3194 mask = POLLIN | POLLRDNORM | POLLERR;
3197 snd_pcm_stream_unlock_irq(substream);
3206 * Only on coherent architectures, we can mmap the status and the control records
3207 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3209 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3211 * mmap status record
3213 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3214 struct vm_fault *vmf)
3216 struct snd_pcm_substream *substream = area->vm_private_data;
3217 struct snd_pcm_runtime *runtime;
3219 if (substream == NULL)
3220 return VM_FAULT_SIGBUS;
3221 runtime = substream->runtime;
3222 vmf->page = virt_to_page(runtime->status);
3223 get_page(vmf->page);
3227 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3229 .fault = snd_pcm_mmap_status_fault,
3232 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3233 struct vm_area_struct *area)
3236 if (!(area->vm_flags & VM_READ))
3238 size = area->vm_end - area->vm_start;
3239 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3241 area->vm_ops = &snd_pcm_vm_ops_status;
3242 area->vm_private_data = substream;
3243 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3248 * mmap control record
3250 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3251 struct vm_fault *vmf)
3253 struct snd_pcm_substream *substream = area->vm_private_data;
3254 struct snd_pcm_runtime *runtime;
3256 if (substream == NULL)
3257 return VM_FAULT_SIGBUS;
3258 runtime = substream->runtime;
3259 vmf->page = virt_to_page(runtime->control);
3260 get_page(vmf->page);
3264 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3266 .fault = snd_pcm_mmap_control_fault,
3269 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3270 struct vm_area_struct *area)
3273 if (!(area->vm_flags & VM_READ))
3275 size = area->vm_end - area->vm_start;
3276 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3278 area->vm_ops = &snd_pcm_vm_ops_control;
3279 area->vm_private_data = substream;
3280 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3283 #else /* ! coherent mmap */
3285 * don't support mmap for status and control records.
3287 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3288 struct vm_area_struct *area)
3292 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3293 struct vm_area_struct *area)
3297 #endif /* coherent mmap */
3299 static inline struct page *
3300 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3302 void *vaddr = substream->runtime->dma_area + ofs;
3303 return virt_to_page(vaddr);
3307 * fault callback for mmapping a RAM page
3309 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3310 struct vm_fault *vmf)
3312 struct snd_pcm_substream *substream = area->vm_private_data;
3313 struct snd_pcm_runtime *runtime;
3314 unsigned long offset;
3318 if (substream == NULL)
3319 return VM_FAULT_SIGBUS;
3320 runtime = substream->runtime;
3321 offset = vmf->pgoff << PAGE_SHIFT;
3322 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3323 if (offset > dma_bytes - PAGE_SIZE)
3324 return VM_FAULT_SIGBUS;
3325 if (substream->ops->page)
3326 page = substream->ops->page(substream, offset);
3328 page = snd_pcm_default_page_ops(substream, offset);
3330 return VM_FAULT_SIGBUS;
3336 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3337 .open = snd_pcm_mmap_data_open,
3338 .close = snd_pcm_mmap_data_close,
3341 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3342 .open = snd_pcm_mmap_data_open,
3343 .close = snd_pcm_mmap_data_close,
3344 .fault = snd_pcm_mmap_data_fault,
3348 * mmap the DMA buffer on RAM
3352 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3353 * @substream: PCM substream
3356 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3357 * this function is invoked implicitly.
3359 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3360 struct vm_area_struct *area)
3362 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3363 #ifdef CONFIG_GENERIC_ALLOCATOR
3364 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3365 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3366 return remap_pfn_range(area, area->vm_start,
3367 substream->dma_buffer.addr >> PAGE_SHIFT,
3368 area->vm_end - area->vm_start, area->vm_page_prot);
3370 #endif /* CONFIG_GENERIC_ALLOCATOR */
3371 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3372 if (!substream->ops->page &&
3373 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3374 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3376 substream->runtime->dma_area,
3377 substream->runtime->dma_addr,
3378 area->vm_end - area->vm_start);
3379 #endif /* CONFIG_X86 */
3380 /* mmap with fault handler */
3381 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3384 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3387 * mmap the DMA buffer on I/O memory area
3389 #if SNDRV_PCM_INFO_MMAP_IOMEM
3391 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3392 * @substream: PCM substream
3395 * When your hardware uses the iomapped pages as the hardware buffer and
3396 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3397 * is supposed to work only on limited architectures.
3399 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3400 struct vm_area_struct *area)
3402 struct snd_pcm_runtime *runtime = substream->runtime;;
3404 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3405 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3408 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3409 #endif /* SNDRV_PCM_INFO_MMAP */
3414 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3415 struct vm_area_struct *area)
3417 struct snd_pcm_runtime *runtime;
3419 unsigned long offset;
3423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3424 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3427 if (!(area->vm_flags & VM_READ))
3430 runtime = substream->runtime;
3431 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3433 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3435 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3436 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3438 size = area->vm_end - area->vm_start;
3439 offset = area->vm_pgoff << PAGE_SHIFT;
3440 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3441 if ((size_t)size > dma_bytes)
3443 if (offset > dma_bytes - size)
3446 area->vm_ops = &snd_pcm_vm_ops_data;
3447 area->vm_private_data = substream;
3448 if (substream->ops->mmap)
3449 err = substream->ops->mmap(substream, area);
3451 err = snd_pcm_lib_default_mmap(substream, area);
3453 atomic_inc(&substream->mmap_count);
3457 EXPORT_SYMBOL(snd_pcm_mmap_data);
3459 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3461 struct snd_pcm_file * pcm_file;
3462 struct snd_pcm_substream *substream;
3463 unsigned long offset;
3465 pcm_file = file->private_data;
3466 substream = pcm_file->substream;
3467 if (PCM_RUNTIME_CHECK(substream))
3470 offset = area->vm_pgoff << PAGE_SHIFT;
3472 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3473 if (pcm_file->no_compat_mmap)
3475 return snd_pcm_mmap_status(substream, file, area);
3476 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3477 if (pcm_file->no_compat_mmap)
3479 return snd_pcm_mmap_control(substream, file, area);
3481 return snd_pcm_mmap_data(substream, file, area);
3486 static int snd_pcm_fasync(int fd, struct file * file, int on)
3488 struct snd_pcm_file * pcm_file;
3489 struct snd_pcm_substream *substream;
3490 struct snd_pcm_runtime *runtime;
3492 pcm_file = file->private_data;
3493 substream = pcm_file->substream;
3494 if (PCM_RUNTIME_CHECK(substream))
3496 runtime = substream->runtime;
3497 return fasync_helper(fd, file, on, &runtime->fasync);
3503 #ifdef CONFIG_COMPAT
3504 #include "pcm_compat.c"
3506 #define snd_pcm_ioctl_compat NULL
3510 * To be removed helpers to keep binary compatibility
3513 #ifdef CONFIG_SND_SUPPORT_OLD_API
3514 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3515 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3517 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3518 struct snd_pcm_hw_params_old *oparams)
3522 memset(params, 0, sizeof(*params));
3523 params->flags = oparams->flags;
3524 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3525 params->masks[i].bits[0] = oparams->masks[i];
3526 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3527 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3528 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3529 params->info = oparams->info;
3530 params->msbits = oparams->msbits;
3531 params->rate_num = oparams->rate_num;
3532 params->rate_den = oparams->rate_den;
3533 params->fifo_size = oparams->fifo_size;
3536 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3537 struct snd_pcm_hw_params *params)
3541 memset(oparams, 0, sizeof(*oparams));
3542 oparams->flags = params->flags;
3543 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3544 oparams->masks[i] = params->masks[i].bits[0];
3545 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3546 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3547 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3548 oparams->info = params->info;
3549 oparams->msbits = params->msbits;
3550 oparams->rate_num = params->rate_num;
3551 oparams->rate_den = params->rate_den;
3552 oparams->fifo_size = params->fifo_size;
3555 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3556 struct snd_pcm_hw_params_old __user * _oparams)
3558 struct snd_pcm_hw_params *params;
3559 struct snd_pcm_hw_params_old *oparams = NULL;
3562 params = kmalloc(sizeof(*params), GFP_KERNEL);
3566 oparams = memdup_user(_oparams, sizeof(*oparams));
3567 if (IS_ERR(oparams)) {
3568 err = PTR_ERR(oparams);
3571 snd_pcm_hw_convert_from_old_params(params, oparams);
3572 err = snd_pcm_hw_refine(substream, params);
3573 snd_pcm_hw_convert_to_old_params(oparams, params);
3574 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3585 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3586 struct snd_pcm_hw_params_old __user * _oparams)
3588 struct snd_pcm_hw_params *params;
3589 struct snd_pcm_hw_params_old *oparams = NULL;
3592 params = kmalloc(sizeof(*params), GFP_KERNEL);
3596 oparams = memdup_user(_oparams, sizeof(*oparams));
3597 if (IS_ERR(oparams)) {
3598 err = PTR_ERR(oparams);
3601 snd_pcm_hw_convert_from_old_params(params, oparams);
3602 err = snd_pcm_hw_params(substream, params);
3603 snd_pcm_hw_convert_to_old_params(oparams, params);
3604 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3614 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3617 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3620 unsigned long pgoff,
3621 unsigned long flags)
3623 struct snd_pcm_file *pcm_file = file->private_data;
3624 struct snd_pcm_substream *substream = pcm_file->substream;
3625 struct snd_pcm_runtime *runtime = substream->runtime;
3626 unsigned long offset = pgoff << PAGE_SHIFT;
3629 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3630 return (unsigned long)runtime->status;
3631 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3632 return (unsigned long)runtime->control;
3634 return (unsigned long)runtime->dma_area + offset;
3638 # define snd_pcm_get_unmapped_area NULL
3645 const struct file_operations snd_pcm_f_ops[2] = {
3647 .owner = THIS_MODULE,
3648 .write = snd_pcm_write,
3649 .aio_write = snd_pcm_aio_write,
3650 .open = snd_pcm_playback_open,
3651 .release = snd_pcm_release,
3652 .llseek = no_llseek,
3653 .poll = snd_pcm_playback_poll,
3654 .unlocked_ioctl = snd_pcm_playback_ioctl,
3655 .compat_ioctl = snd_pcm_ioctl_compat,
3656 .mmap = snd_pcm_mmap,
3657 .fasync = snd_pcm_fasync,
3658 .get_unmapped_area = snd_pcm_get_unmapped_area,
3661 .owner = THIS_MODULE,
3662 .read = snd_pcm_read,
3663 .aio_read = snd_pcm_aio_read,
3664 .open = snd_pcm_capture_open,
3665 .release = snd_pcm_release,
3666 .llseek = no_llseek,
3667 .poll = snd_pcm_capture_poll,
3668 .unlocked_ioctl = snd_pcm_capture_ioctl,
3669 .compat_ioctl = snd_pcm_ioctl_compat,
3670 .mmap = snd_pcm_mmap,
3671 .fasync = snd_pcm_fasync,
3672 .get_unmapped_area = snd_pcm_get_unmapped_area,