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>
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>
37 #include <linux/uio.h>
38 #include <linux/delay.h>
44 struct snd_pcm_hw_params_old {
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
60 #ifdef CONFIG_SND_SUPPORT_OLD_API
61 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
64 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
69 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
75 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem);
78 /* Writer in rwsem may block readers even during its waiting in queue,
79 * and this may lead to a deadlock when the code path takes read sem
80 * twice (e.g. one in snd_pcm_action_nonatomic() and another in
81 * snd_pcm_stream_lock()). As a (suboptimal) workaround, let writer to
82 * sleep until all the readers are completed without blocking by writer.
84 static inline void down_write_nonfifo(struct rw_semaphore *lock)
86 while (!down_write_trylock(lock))
91 * snd_pcm_stream_lock - Lock the PCM stream
92 * @substream: PCM substream
94 * This locks the PCM stream's spinlock or mutex depending on the nonatomic
95 * flag of the given substream. This also takes the global link rw lock
96 * (or rw sem), too, for avoiding the race with linked streams.
98 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
100 if (substream->pcm->nonatomic) {
101 down_read_nested(&snd_pcm_link_rwsem, SINGLE_DEPTH_NESTING);
102 mutex_lock(&substream->self_group.mutex);
104 read_lock(&snd_pcm_link_rwlock);
105 spin_lock(&substream->self_group.lock);
108 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
111 * snd_pcm_stream_lock - Unlock the PCM stream
112 * @substream: PCM substream
114 * This unlocks the PCM stream that has been locked via snd_pcm_stream_lock().
116 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
118 if (substream->pcm->nonatomic) {
119 mutex_unlock(&substream->self_group.mutex);
120 up_read(&snd_pcm_link_rwsem);
122 spin_unlock(&substream->self_group.lock);
123 read_unlock(&snd_pcm_link_rwlock);
126 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
129 * snd_pcm_stream_lock_irq - Lock the PCM stream
130 * @substream: PCM substream
132 * This locks the PCM stream like snd_pcm_stream_lock() and disables the local
133 * IRQ (only when nonatomic is false). In nonatomic case, this is identical
134 * as snd_pcm_stream_lock().
136 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
138 if (!substream->pcm->nonatomic)
140 snd_pcm_stream_lock(substream);
142 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
145 * snd_pcm_stream_unlock_irq - Unlock the PCM stream
146 * @substream: PCM substream
148 * This is a counter-part of snd_pcm_stream_lock_irq().
150 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
152 snd_pcm_stream_unlock(substream);
153 if (!substream->pcm->nonatomic)
156 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
158 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
160 unsigned long flags = 0;
161 if (!substream->pcm->nonatomic)
162 local_irq_save(flags);
163 snd_pcm_stream_lock(substream);
166 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
169 * snd_pcm_stream_unlock_irqrestore - Unlock the PCM stream
170 * @substream: PCM substream
173 * This is a counter-part of snd_pcm_stream_lock_irqsave().
175 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
178 snd_pcm_stream_unlock(substream);
179 if (!substream->pcm->nonatomic)
180 local_irq_restore(flags);
182 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
184 static inline mm_segment_t snd_enter_user(void)
186 mm_segment_t fs = get_fs();
191 static inline void snd_leave_user(mm_segment_t fs)
198 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
200 struct snd_pcm_runtime *runtime;
201 struct snd_pcm *pcm = substream->pcm;
202 struct snd_pcm_str *pstr = substream->pstr;
204 memset(info, 0, sizeof(*info));
205 info->card = pcm->card->number;
206 info->device = pcm->device;
207 info->stream = substream->stream;
208 info->subdevice = substream->number;
209 strlcpy(info->id, pcm->id, sizeof(info->id));
210 strlcpy(info->name, pcm->name, sizeof(info->name));
211 info->dev_class = pcm->dev_class;
212 info->dev_subclass = pcm->dev_subclass;
213 info->subdevices_count = pstr->substream_count;
214 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
215 strlcpy(info->subname, substream->name, sizeof(info->subname));
216 runtime = substream->runtime;
221 int snd_pcm_info_user(struct snd_pcm_substream *substream,
222 struct snd_pcm_info __user * _info)
224 struct snd_pcm_info *info;
227 info = kmalloc(sizeof(*info), GFP_KERNEL);
230 err = snd_pcm_info(substream, info);
232 if (copy_to_user(_info, info, sizeof(*info)))
239 static bool hw_support_mmap(struct snd_pcm_substream *substream)
241 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
243 /* check architectures that return -EINVAL from dma_mmap_coherent() */
244 /* FIXME: this should be some global flag */
245 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
246 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
247 if (!substream->ops->mmap &&
248 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
257 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
258 static const char * const snd_pcm_hw_param_names[] = {
262 HW_PARAM(SAMPLE_BITS),
263 HW_PARAM(FRAME_BITS),
266 HW_PARAM(PERIOD_TIME),
267 HW_PARAM(PERIOD_SIZE),
268 HW_PARAM(PERIOD_BYTES),
270 HW_PARAM(BUFFER_TIME),
271 HW_PARAM(BUFFER_SIZE),
272 HW_PARAM(BUFFER_BYTES),
277 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
278 struct snd_pcm_hw_params *params)
281 struct snd_pcm_hardware *hw;
282 struct snd_interval *i = NULL;
283 struct snd_mask *m = NULL;
284 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
285 unsigned int rstamps[constrs->rules_num];
286 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
287 unsigned int stamp = 2;
291 params->fifo_size = 0;
292 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
294 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
295 params->rate_num = 0;
296 params->rate_den = 0;
299 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
300 m = hw_param_mask(params, k);
301 if (snd_mask_empty(m))
303 if (!(params->rmask & (1 << k)))
306 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
307 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
309 changed = snd_mask_refine(m, constrs_mask(constrs, k));
311 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
314 params->cmask |= 1 << k;
319 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
320 i = hw_param_interval(params, k);
321 if (snd_interval_empty(i))
323 if (!(params->rmask & (1 << k)))
326 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
331 i->openmin ? '(' : '[', i->min,
332 i->max, i->openmax ? ')' : ']');
335 changed = snd_interval_refine(i, constrs_interval(constrs, k));
340 pr_cont("%c%u %u%c\n",
341 i->openmin ? '(' : '[', i->min,
342 i->max, i->openmax ? ')' : ']');
345 params->cmask |= 1 << k;
350 for (k = 0; k < constrs->rules_num; k++)
352 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
353 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
356 for (k = 0; k < constrs->rules_num; k++) {
357 struct snd_pcm_hw_rule *r = &constrs->rules[k];
360 if (r->cond && !(r->cond & params->flags))
362 for (d = 0; r->deps[d] >= 0; d++) {
363 if (vstamps[r->deps[d]] > rstamps[k]) {
371 pr_debug("Rule %d [%p]: ", k, r->func);
373 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
374 if (hw_is_mask(r->var)) {
375 m = hw_param_mask(params, r->var);
376 pr_cont("%x", *m->bits);
378 i = hw_param_interval(params, r->var);
383 i->openmin ? '(' : '[', i->min,
384 i->max, i->openmax ? ')' : ']');
388 changed = r->func(params, r);
392 if (hw_is_mask(r->var))
393 pr_cont("%x", *m->bits);
399 i->openmin ? '(' : '[', i->min,
400 i->max, i->openmax ? ')' : ']');
406 if (changed && r->var >= 0) {
407 params->cmask |= (1 << r->var);
408 vstamps[r->var] = stamp;
416 if (!params->msbits) {
417 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
418 if (snd_interval_single(i))
419 params->msbits = snd_interval_value(i);
422 if (!params->rate_den) {
423 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
424 if (snd_interval_single(i)) {
425 params->rate_num = snd_interval_value(i);
426 params->rate_den = 1;
430 hw = &substream->runtime->hw;
432 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
433 SNDRV_PCM_INFO_DRAIN_TRIGGER);
434 if (!hw_support_mmap(substream))
435 params->info &= ~(SNDRV_PCM_INFO_MMAP |
436 SNDRV_PCM_INFO_MMAP_VALID);
438 if (!params->fifo_size) {
439 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
440 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
441 if (snd_mask_min(m) == snd_mask_max(m) &&
442 snd_interval_min(i) == snd_interval_max(i)) {
443 changed = substream->ops->ioctl(substream,
444 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
453 EXPORT_SYMBOL(snd_pcm_hw_refine);
455 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
456 struct snd_pcm_hw_params __user * _params)
458 struct snd_pcm_hw_params *params;
461 params = memdup_user(_params, sizeof(*params));
463 return PTR_ERR(params);
465 err = snd_pcm_hw_refine(substream, params);
466 if (copy_to_user(_params, params, sizeof(*params))) {
475 static int period_to_usecs(struct snd_pcm_runtime *runtime)
480 return -1; /* invalid */
482 /* take 75% of period time as the deadline */
483 usecs = (750000 / runtime->rate) * runtime->period_size;
484 usecs += ((750000 % runtime->rate) * runtime->period_size) /
490 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
492 snd_pcm_stream_lock_irq(substream);
493 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
494 substream->runtime->status->state = state;
495 snd_pcm_stream_unlock_irq(substream);
498 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
501 #ifdef CONFIG_SND_PCM_TIMER
502 if (substream->timer)
503 snd_timer_notify(substream->timer, event,
504 &substream->runtime->trigger_tstamp);
508 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
509 struct snd_pcm_hw_params *params)
511 struct snd_pcm_runtime *runtime;
514 snd_pcm_uframes_t frames;
516 if (PCM_RUNTIME_CHECK(substream))
518 runtime = substream->runtime;
519 snd_pcm_stream_lock_irq(substream);
520 switch (runtime->status->state) {
521 case SNDRV_PCM_STATE_OPEN:
522 case SNDRV_PCM_STATE_SETUP:
523 case SNDRV_PCM_STATE_PREPARED:
526 snd_pcm_stream_unlock_irq(substream);
529 snd_pcm_stream_unlock_irq(substream);
530 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
531 if (!substream->oss.oss)
533 if (atomic_read(&substream->mmap_count))
537 err = snd_pcm_hw_refine(substream, params);
541 err = snd_pcm_hw_params_choose(substream, params);
545 if (substream->ops->hw_params != NULL) {
546 err = substream->ops->hw_params(substream, params);
551 runtime->access = params_access(params);
552 runtime->format = params_format(params);
553 runtime->subformat = params_subformat(params);
554 runtime->channels = params_channels(params);
555 runtime->rate = params_rate(params);
556 runtime->period_size = params_period_size(params);
557 runtime->periods = params_periods(params);
558 runtime->buffer_size = params_buffer_size(params);
559 runtime->info = params->info;
560 runtime->rate_num = params->rate_num;
561 runtime->rate_den = params->rate_den;
562 runtime->no_period_wakeup =
563 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
564 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
566 bits = snd_pcm_format_physical_width(runtime->format);
567 runtime->sample_bits = bits;
568 bits *= runtime->channels;
569 runtime->frame_bits = bits;
571 while (bits % 8 != 0) {
575 runtime->byte_align = bits / 8;
576 runtime->min_align = frames;
578 /* Default sw params */
579 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
580 runtime->period_step = 1;
581 runtime->control->avail_min = runtime->period_size;
582 runtime->start_threshold = 1;
583 runtime->stop_threshold = runtime->buffer_size;
584 runtime->silence_threshold = 0;
585 runtime->silence_size = 0;
586 runtime->boundary = runtime->buffer_size;
587 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
588 runtime->boundary *= 2;
590 snd_pcm_timer_resolution_change(substream);
591 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
593 if (pm_qos_request_active(&substream->latency_pm_qos_req))
594 pm_qos_remove_request(&substream->latency_pm_qos_req);
595 if ((usecs = period_to_usecs(runtime)) >= 0)
596 pm_qos_add_request(&substream->latency_pm_qos_req,
597 PM_QOS_CPU_DMA_LATENCY, usecs);
600 /* hardware might be unusable from this time,
601 so we force application to retry to set
602 the correct hardware parameter settings */
603 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
604 if (substream->ops->hw_free != NULL)
605 substream->ops->hw_free(substream);
609 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
610 struct snd_pcm_hw_params __user * _params)
612 struct snd_pcm_hw_params *params;
615 params = memdup_user(_params, sizeof(*params));
617 return PTR_ERR(params);
619 err = snd_pcm_hw_params(substream, params);
620 if (copy_to_user(_params, params, sizeof(*params))) {
629 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
631 struct snd_pcm_runtime *runtime;
634 if (PCM_RUNTIME_CHECK(substream))
636 runtime = substream->runtime;
637 snd_pcm_stream_lock_irq(substream);
638 switch (runtime->status->state) {
639 case SNDRV_PCM_STATE_SETUP:
640 case SNDRV_PCM_STATE_PREPARED:
643 snd_pcm_stream_unlock_irq(substream);
646 snd_pcm_stream_unlock_irq(substream);
647 if (atomic_read(&substream->mmap_count))
649 if (substream->ops->hw_free)
650 result = substream->ops->hw_free(substream);
651 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
652 pm_qos_remove_request(&substream->latency_pm_qos_req);
656 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
657 struct snd_pcm_sw_params *params)
659 struct snd_pcm_runtime *runtime;
662 if (PCM_RUNTIME_CHECK(substream))
664 runtime = substream->runtime;
665 snd_pcm_stream_lock_irq(substream);
666 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
667 snd_pcm_stream_unlock_irq(substream);
670 snd_pcm_stream_unlock_irq(substream);
672 if (params->tstamp_mode < 0 ||
673 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
675 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
676 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
678 if (params->avail_min == 0)
680 if (params->silence_size >= runtime->boundary) {
681 if (params->silence_threshold != 0)
684 if (params->silence_size > params->silence_threshold)
686 if (params->silence_threshold > runtime->buffer_size)
690 snd_pcm_stream_lock_irq(substream);
691 runtime->tstamp_mode = params->tstamp_mode;
692 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
693 runtime->tstamp_type = params->tstamp_type;
694 runtime->period_step = params->period_step;
695 runtime->control->avail_min = params->avail_min;
696 runtime->start_threshold = params->start_threshold;
697 runtime->stop_threshold = params->stop_threshold;
698 runtime->silence_threshold = params->silence_threshold;
699 runtime->silence_size = params->silence_size;
700 params->boundary = runtime->boundary;
701 if (snd_pcm_running(substream)) {
702 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
703 runtime->silence_size > 0)
704 snd_pcm_playback_silence(substream, ULONG_MAX);
705 err = snd_pcm_update_state(substream, runtime);
707 snd_pcm_stream_unlock_irq(substream);
711 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
712 struct snd_pcm_sw_params __user * _params)
714 struct snd_pcm_sw_params params;
716 if (copy_from_user(¶ms, _params, sizeof(params)))
718 err = snd_pcm_sw_params(substream, ¶ms);
719 if (copy_to_user(_params, ¶ms, sizeof(params)))
724 int snd_pcm_status(struct snd_pcm_substream *substream,
725 struct snd_pcm_status *status)
727 struct snd_pcm_runtime *runtime = substream->runtime;
729 snd_pcm_stream_lock_irq(substream);
731 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
732 &runtime->audio_tstamp_config);
734 /* backwards compatible behavior */
735 if (runtime->audio_tstamp_config.type_requested ==
736 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
737 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
738 runtime->audio_tstamp_config.type_requested =
739 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
741 runtime->audio_tstamp_config.type_requested =
742 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
743 runtime->audio_tstamp_report.valid = 0;
745 runtime->audio_tstamp_report.valid = 1;
747 status->state = runtime->status->state;
748 status->suspended_state = runtime->status->suspended_state;
749 if (status->state == SNDRV_PCM_STATE_OPEN)
751 status->trigger_tstamp = runtime->trigger_tstamp;
752 if (snd_pcm_running(substream)) {
753 snd_pcm_update_hw_ptr(substream);
754 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
755 status->tstamp = runtime->status->tstamp;
756 status->driver_tstamp = runtime->driver_tstamp;
757 status->audio_tstamp =
758 runtime->status->audio_tstamp;
759 if (runtime->audio_tstamp_report.valid == 1)
760 /* backwards compatibility, no report provided in COMPAT mode */
761 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
762 &status->audio_tstamp_accuracy,
763 &runtime->audio_tstamp_report);
768 /* get tstamp only in fallback mode and only if enabled */
769 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
770 snd_pcm_gettime(runtime, &status->tstamp);
773 status->appl_ptr = runtime->control->appl_ptr;
774 status->hw_ptr = runtime->status->hw_ptr;
775 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
776 status->avail = snd_pcm_playback_avail(runtime);
777 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
778 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
779 status->delay = runtime->buffer_size - status->avail;
780 status->delay += runtime->delay;
784 status->avail = snd_pcm_capture_avail(runtime);
785 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
786 status->delay = status->avail + runtime->delay;
790 status->avail_max = runtime->avail_max;
791 status->overrange = runtime->overrange;
792 runtime->avail_max = 0;
793 runtime->overrange = 0;
795 snd_pcm_stream_unlock_irq(substream);
799 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
800 struct snd_pcm_status __user * _status,
803 struct snd_pcm_status status;
806 memset(&status, 0, sizeof(status));
808 * with extension, parameters are read/write,
809 * get audio_tstamp_data from user,
810 * ignore rest of status structure
812 if (ext && get_user(status.audio_tstamp_data,
813 (u32 __user *)(&_status->audio_tstamp_data)))
815 res = snd_pcm_status(substream, &status);
818 if (copy_to_user(_status, &status, sizeof(status)))
823 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
824 struct snd_pcm_channel_info * info)
826 struct snd_pcm_runtime *runtime;
827 unsigned int channel;
829 channel = info->channel;
830 runtime = substream->runtime;
831 snd_pcm_stream_lock_irq(substream);
832 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
833 snd_pcm_stream_unlock_irq(substream);
836 snd_pcm_stream_unlock_irq(substream);
837 if (channel >= runtime->channels)
839 memset(info, 0, sizeof(*info));
840 info->channel = channel;
841 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
844 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
845 struct snd_pcm_channel_info __user * _info)
847 struct snd_pcm_channel_info info;
850 if (copy_from_user(&info, _info, sizeof(info)))
852 res = snd_pcm_channel_info(substream, &info);
855 if (copy_to_user(_info, &info, sizeof(info)))
860 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
862 struct snd_pcm_runtime *runtime = substream->runtime;
863 if (runtime->trigger_master == NULL)
865 if (runtime->trigger_master == substream) {
866 if (!runtime->trigger_tstamp_latched)
867 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
869 snd_pcm_trigger_tstamp(runtime->trigger_master);
870 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
872 runtime->trigger_master = NULL;
876 int (*pre_action)(struct snd_pcm_substream *substream, int state);
877 int (*do_action)(struct snd_pcm_substream *substream, int state);
878 void (*undo_action)(struct snd_pcm_substream *substream, int state);
879 void (*post_action)(struct snd_pcm_substream *substream, int state);
883 * this functions is core for handling of linked stream
884 * Note: the stream state might be changed also on failure
885 * Note2: call with calling stream lock + link lock
887 static int snd_pcm_action_group(struct action_ops *ops,
888 struct snd_pcm_substream *substream,
889 int state, int do_lock)
891 struct snd_pcm_substream *s = NULL;
892 struct snd_pcm_substream *s1;
893 int res = 0, depth = 1;
895 snd_pcm_group_for_each_entry(s, substream) {
896 if (do_lock && s != substream) {
897 if (s->pcm->nonatomic)
898 mutex_lock_nested(&s->self_group.mutex, depth);
900 spin_lock_nested(&s->self_group.lock, depth);
903 res = ops->pre_action(s, state);
907 snd_pcm_group_for_each_entry(s, substream) {
908 res = ops->do_action(s, state);
910 if (ops->undo_action) {
911 snd_pcm_group_for_each_entry(s1, substream) {
912 if (s1 == s) /* failed stream */
914 ops->undo_action(s1, state);
917 s = NULL; /* unlock all */
921 snd_pcm_group_for_each_entry(s, substream) {
922 ops->post_action(s, state);
927 snd_pcm_group_for_each_entry(s1, substream) {
928 if (s1 != substream) {
929 if (s1->pcm->nonatomic)
930 mutex_unlock(&s1->self_group.mutex);
932 spin_unlock(&s1->self_group.lock);
934 if (s1 == s) /* end */
942 * Note: call with stream lock
944 static int snd_pcm_action_single(struct action_ops *ops,
945 struct snd_pcm_substream *substream,
950 res = ops->pre_action(substream, state);
953 res = ops->do_action(substream, state);
955 ops->post_action(substream, state);
956 else if (ops->undo_action)
957 ops->undo_action(substream, state);
962 * Note: call with stream lock
964 static int snd_pcm_action(struct action_ops *ops,
965 struct snd_pcm_substream *substream,
970 if (!snd_pcm_stream_linked(substream))
971 return snd_pcm_action_single(ops, substream, state);
973 if (substream->pcm->nonatomic) {
974 if (!mutex_trylock(&substream->group->mutex)) {
975 mutex_unlock(&substream->self_group.mutex);
976 mutex_lock(&substream->group->mutex);
977 mutex_lock(&substream->self_group.mutex);
979 res = snd_pcm_action_group(ops, substream, state, 1);
980 mutex_unlock(&substream->group->mutex);
982 if (!spin_trylock(&substream->group->lock)) {
983 spin_unlock(&substream->self_group.lock);
984 spin_lock(&substream->group->lock);
985 spin_lock(&substream->self_group.lock);
987 res = snd_pcm_action_group(ops, substream, state, 1);
988 spin_unlock(&substream->group->lock);
994 * Note: don't use any locks before
996 static int snd_pcm_action_lock_irq(struct action_ops *ops,
997 struct snd_pcm_substream *substream,
1002 snd_pcm_stream_lock_irq(substream);
1003 res = snd_pcm_action(ops, substream, state);
1004 snd_pcm_stream_unlock_irq(substream);
1010 static int snd_pcm_action_nonatomic(struct action_ops *ops,
1011 struct snd_pcm_substream *substream,
1016 down_read(&snd_pcm_link_rwsem);
1017 if (snd_pcm_stream_linked(substream))
1018 res = snd_pcm_action_group(ops, substream, state, 0);
1020 res = snd_pcm_action_single(ops, substream, state);
1021 up_read(&snd_pcm_link_rwsem);
1028 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1030 struct snd_pcm_runtime *runtime = substream->runtime;
1031 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1033 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1034 !snd_pcm_playback_data(substream))
1036 runtime->trigger_tstamp_latched = false;
1037 runtime->trigger_master = substream;
1041 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1043 if (substream->runtime->trigger_master != substream)
1045 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1048 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1050 if (substream->runtime->trigger_master == substream)
1051 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1054 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1056 struct snd_pcm_runtime *runtime = substream->runtime;
1057 snd_pcm_trigger_tstamp(substream);
1058 runtime->hw_ptr_jiffies = jiffies;
1059 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1061 runtime->status->state = state;
1062 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1063 runtime->silence_size > 0)
1064 snd_pcm_playback_silence(substream, ULONG_MAX);
1065 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1068 static struct action_ops snd_pcm_action_start = {
1069 .pre_action = snd_pcm_pre_start,
1070 .do_action = snd_pcm_do_start,
1071 .undo_action = snd_pcm_undo_start,
1072 .post_action = snd_pcm_post_start
1076 * snd_pcm_start - start all linked streams
1077 * @substream: the PCM substream instance
1079 * Return: Zero if successful, or a negative error code.
1081 int snd_pcm_start(struct snd_pcm_substream *substream)
1083 return snd_pcm_action(&snd_pcm_action_start, substream,
1084 SNDRV_PCM_STATE_RUNNING);
1090 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1092 struct snd_pcm_runtime *runtime = substream->runtime;
1093 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1095 runtime->trigger_master = substream;
1099 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1101 if (substream->runtime->trigger_master == substream &&
1102 snd_pcm_running(substream))
1103 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1104 return 0; /* unconditonally stop all substreams */
1107 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 if (runtime->status->state != state) {
1111 snd_pcm_trigger_tstamp(substream);
1112 runtime->status->state = state;
1113 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1115 wake_up(&runtime->sleep);
1116 wake_up(&runtime->tsleep);
1119 static struct action_ops snd_pcm_action_stop = {
1120 .pre_action = snd_pcm_pre_stop,
1121 .do_action = snd_pcm_do_stop,
1122 .post_action = snd_pcm_post_stop
1126 * snd_pcm_stop - try to stop all running streams in the substream group
1127 * @substream: the PCM substream instance
1128 * @state: PCM state after stopping the stream
1130 * The state of each stream is then changed to the given state unconditionally.
1132 * Return: Zero if successful, or a negative error code.
1134 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1136 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1139 EXPORT_SYMBOL(snd_pcm_stop);
1142 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1143 * @substream: the PCM substream
1145 * After stopping, the state is changed to SETUP.
1146 * Unlike snd_pcm_stop(), this affects only the given stream.
1148 * Return: Zero if succesful, or a negative error code.
1150 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1152 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1153 SNDRV_PCM_STATE_SETUP);
1157 * snd_pcm_stop_xrun - stop the running streams as XRUN
1158 * @substream: the PCM substream instance
1160 * This stops the given running substream (and all linked substreams) as XRUN.
1161 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1163 * Return: Zero if successful, or a negative error code.
1165 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1167 unsigned long flags;
1170 snd_pcm_stream_lock_irqsave(substream, flags);
1171 if (snd_pcm_running(substream))
1172 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1173 snd_pcm_stream_unlock_irqrestore(substream, flags);
1176 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1181 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1183 struct snd_pcm_runtime *runtime = substream->runtime;
1184 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1187 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1189 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1191 runtime->trigger_master = substream;
1195 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1197 if (substream->runtime->trigger_master != substream)
1199 /* some drivers might use hw_ptr to recover from the pause -
1200 update the hw_ptr now */
1202 snd_pcm_update_hw_ptr(substream);
1203 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1204 * a delta between the current jiffies, this gives a large enough
1205 * delta, effectively to skip the check once.
1207 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1208 return substream->ops->trigger(substream,
1209 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1210 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1213 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1215 if (substream->runtime->trigger_master == substream)
1216 substream->ops->trigger(substream,
1217 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1218 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1221 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1223 struct snd_pcm_runtime *runtime = substream->runtime;
1224 snd_pcm_trigger_tstamp(substream);
1226 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1227 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1228 wake_up(&runtime->sleep);
1229 wake_up(&runtime->tsleep);
1231 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1232 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1236 static struct action_ops snd_pcm_action_pause = {
1237 .pre_action = snd_pcm_pre_pause,
1238 .do_action = snd_pcm_do_pause,
1239 .undo_action = snd_pcm_undo_pause,
1240 .post_action = snd_pcm_post_pause
1244 * Push/release the pause for all linked streams.
1246 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1248 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1254 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1256 struct snd_pcm_runtime *runtime = substream->runtime;
1257 switch (runtime->status->state) {
1258 case SNDRV_PCM_STATE_SUSPENDED:
1260 /* unresumable PCM state; return -EBUSY for skipping suspend */
1261 case SNDRV_PCM_STATE_OPEN:
1262 case SNDRV_PCM_STATE_SETUP:
1263 case SNDRV_PCM_STATE_DISCONNECTED:
1266 runtime->trigger_master = substream;
1270 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1272 struct snd_pcm_runtime *runtime = substream->runtime;
1273 if (runtime->trigger_master != substream)
1275 if (! snd_pcm_running(substream))
1277 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1278 return 0; /* suspend unconditionally */
1281 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1283 struct snd_pcm_runtime *runtime = substream->runtime;
1284 snd_pcm_trigger_tstamp(substream);
1285 runtime->status->suspended_state = runtime->status->state;
1286 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1287 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1288 wake_up(&runtime->sleep);
1289 wake_up(&runtime->tsleep);
1292 static struct action_ops snd_pcm_action_suspend = {
1293 .pre_action = snd_pcm_pre_suspend,
1294 .do_action = snd_pcm_do_suspend,
1295 .post_action = snd_pcm_post_suspend
1299 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1300 * @substream: the PCM substream
1302 * After this call, all streams are changed to SUSPENDED state.
1304 * Return: Zero if successful (or @substream is %NULL), or a negative error
1307 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1310 unsigned long flags;
1315 snd_pcm_stream_lock_irqsave(substream, flags);
1316 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1317 snd_pcm_stream_unlock_irqrestore(substream, flags);
1321 EXPORT_SYMBOL(snd_pcm_suspend);
1324 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1325 * @pcm: the PCM instance
1327 * After this call, all streams are changed to SUSPENDED state.
1329 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1331 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1333 struct snd_pcm_substream *substream;
1334 int stream, err = 0;
1339 for (stream = 0; stream < 2; stream++) {
1340 for (substream = pcm->streams[stream].substream;
1341 substream; substream = substream->next) {
1342 /* FIXME: the open/close code should lock this as well */
1343 if (substream->runtime == NULL)
1347 * Skip BE dai link PCM's that are internal and may
1348 * not have their substream ops set.
1350 if (!substream->ops)
1353 err = snd_pcm_suspend(substream);
1354 if (err < 0 && err != -EBUSY)
1361 EXPORT_SYMBOL(snd_pcm_suspend_all);
1365 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1367 struct snd_pcm_runtime *runtime = substream->runtime;
1368 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1370 runtime->trigger_master = substream;
1374 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1376 struct snd_pcm_runtime *runtime = substream->runtime;
1377 if (runtime->trigger_master != substream)
1379 /* DMA not running previously? */
1380 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1381 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1382 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1384 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1387 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1389 if (substream->runtime->trigger_master == substream &&
1390 snd_pcm_running(substream))
1391 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1394 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1396 struct snd_pcm_runtime *runtime = substream->runtime;
1397 snd_pcm_trigger_tstamp(substream);
1398 runtime->status->state = runtime->status->suspended_state;
1399 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1402 static struct action_ops snd_pcm_action_resume = {
1403 .pre_action = snd_pcm_pre_resume,
1404 .do_action = snd_pcm_do_resume,
1405 .undo_action = snd_pcm_undo_resume,
1406 .post_action = snd_pcm_post_resume
1409 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1411 struct snd_card *card = substream->pcm->card;
1414 snd_power_lock(card);
1415 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1416 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1417 snd_power_unlock(card);
1423 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1428 #endif /* CONFIG_PM */
1433 * Change the RUNNING stream(s) to XRUN state.
1435 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1437 struct snd_card *card = substream->pcm->card;
1438 struct snd_pcm_runtime *runtime = substream->runtime;
1441 snd_power_lock(card);
1442 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1443 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1448 snd_pcm_stream_lock_irq(substream);
1449 switch (runtime->status->state) {
1450 case SNDRV_PCM_STATE_XRUN:
1451 result = 0; /* already there */
1453 case SNDRV_PCM_STATE_RUNNING:
1454 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1459 snd_pcm_stream_unlock_irq(substream);
1461 snd_power_unlock(card);
1468 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1470 struct snd_pcm_runtime *runtime = substream->runtime;
1471 switch (runtime->status->state) {
1472 case SNDRV_PCM_STATE_RUNNING:
1473 case SNDRV_PCM_STATE_PREPARED:
1474 case SNDRV_PCM_STATE_PAUSED:
1475 case SNDRV_PCM_STATE_SUSPENDED:
1482 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1484 struct snd_pcm_runtime *runtime = substream->runtime;
1485 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1488 runtime->hw_ptr_base = 0;
1489 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1490 runtime->status->hw_ptr % runtime->period_size;
1491 runtime->silence_start = runtime->status->hw_ptr;
1492 runtime->silence_filled = 0;
1496 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1498 struct snd_pcm_runtime *runtime = substream->runtime;
1499 runtime->control->appl_ptr = runtime->status->hw_ptr;
1500 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1501 runtime->silence_size > 0)
1502 snd_pcm_playback_silence(substream, ULONG_MAX);
1505 static struct action_ops snd_pcm_action_reset = {
1506 .pre_action = snd_pcm_pre_reset,
1507 .do_action = snd_pcm_do_reset,
1508 .post_action = snd_pcm_post_reset
1511 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1513 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1519 /* we use the second argument for updating f_flags */
1520 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1523 struct snd_pcm_runtime *runtime = substream->runtime;
1524 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1525 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1527 if (snd_pcm_running(substream))
1529 substream->f_flags = f_flags;
1533 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1536 err = substream->ops->prepare(substream);
1539 return snd_pcm_do_reset(substream, 0);
1542 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1544 struct snd_pcm_runtime *runtime = substream->runtime;
1545 runtime->control->appl_ptr = runtime->status->hw_ptr;
1546 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1549 static struct action_ops snd_pcm_action_prepare = {
1550 .pre_action = snd_pcm_pre_prepare,
1551 .do_action = snd_pcm_do_prepare,
1552 .post_action = snd_pcm_post_prepare
1556 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1557 * @substream: the PCM substream instance
1558 * @file: file to refer f_flags
1560 * Return: Zero if successful, or a negative error code.
1562 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1566 struct snd_card *card = substream->pcm->card;
1570 f_flags = file->f_flags;
1572 f_flags = substream->f_flags;
1574 snd_power_lock(card);
1575 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1576 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1577 substream, f_flags);
1578 snd_power_unlock(card);
1586 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1588 struct snd_pcm_runtime *runtime = substream->runtime;
1589 switch (runtime->status->state) {
1590 case SNDRV_PCM_STATE_OPEN:
1591 case SNDRV_PCM_STATE_DISCONNECTED:
1592 case SNDRV_PCM_STATE_SUSPENDED:
1595 runtime->trigger_master = substream;
1599 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1601 struct snd_pcm_runtime *runtime = substream->runtime;
1602 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1603 switch (runtime->status->state) {
1604 case SNDRV_PCM_STATE_PREPARED:
1605 /* start playback stream if possible */
1606 if (! snd_pcm_playback_empty(substream)) {
1607 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1608 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1610 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1613 case SNDRV_PCM_STATE_RUNNING:
1614 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1616 case SNDRV_PCM_STATE_XRUN:
1617 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1623 /* stop running stream */
1624 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1625 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1626 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1627 snd_pcm_do_stop(substream, new_state);
1628 snd_pcm_post_stop(substream, new_state);
1632 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1633 runtime->trigger_master == substream &&
1634 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1635 return substream->ops->trigger(substream,
1636 SNDRV_PCM_TRIGGER_DRAIN);
1641 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1645 static struct action_ops snd_pcm_action_drain_init = {
1646 .pre_action = snd_pcm_pre_drain_init,
1647 .do_action = snd_pcm_do_drain_init,
1648 .post_action = snd_pcm_post_drain_init
1651 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1654 * Drain the stream(s).
1655 * When the substream is linked, sync until the draining of all playback streams
1657 * After this call, all streams are supposed to be either SETUP or DRAINING
1658 * (capture only) state.
1660 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1663 struct snd_card *card;
1664 struct snd_pcm_runtime *runtime;
1665 struct snd_pcm_substream *s;
1670 card = substream->pcm->card;
1671 runtime = substream->runtime;
1673 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1676 snd_power_lock(card);
1677 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1678 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1680 snd_power_unlock(card);
1686 if (file->f_flags & O_NONBLOCK)
1688 } else if (substream->f_flags & O_NONBLOCK)
1691 down_read(&snd_pcm_link_rwsem);
1692 snd_pcm_stream_lock_irq(substream);
1694 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1695 snd_pcm_pause(substream, 0);
1697 /* pre-start/stop - all running streams are changed to DRAINING state */
1698 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1701 /* in non-blocking, we don't wait in ioctl but let caller poll */
1709 struct snd_pcm_runtime *to_check;
1710 if (signal_pending(current)) {
1711 result = -ERESTARTSYS;
1714 /* find a substream to drain */
1716 snd_pcm_group_for_each_entry(s, substream) {
1717 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1719 runtime = s->runtime;
1720 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1726 break; /* all drained */
1727 init_waitqueue_entry(&wait, current);
1728 add_wait_queue(&to_check->sleep, &wait);
1729 snd_pcm_stream_unlock_irq(substream);
1730 up_read(&snd_pcm_link_rwsem);
1731 snd_power_unlock(card);
1732 if (runtime->no_period_wakeup)
1733 tout = MAX_SCHEDULE_TIMEOUT;
1736 if (runtime->rate) {
1737 long t = runtime->period_size * 2 / runtime->rate;
1738 tout = max(t, tout);
1740 tout = msecs_to_jiffies(tout * 1000);
1742 tout = schedule_timeout_interruptible(tout);
1743 snd_power_lock(card);
1744 down_read(&snd_pcm_link_rwsem);
1745 snd_pcm_stream_lock_irq(substream);
1746 remove_wait_queue(&to_check->sleep, &wait);
1747 if (card->shutdown) {
1752 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1755 dev_dbg(substream->pcm->card->dev,
1756 "playback drain error (DMA or IRQ trouble?)\n");
1757 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1765 snd_pcm_stream_unlock_irq(substream);
1766 up_read(&snd_pcm_link_rwsem);
1767 snd_power_unlock(card);
1775 * Immediately put all linked substreams into SETUP state.
1777 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1779 struct snd_pcm_runtime *runtime;
1782 if (PCM_RUNTIME_CHECK(substream))
1784 runtime = substream->runtime;
1786 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1787 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1788 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1791 snd_pcm_stream_lock_irq(substream);
1793 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1794 snd_pcm_pause(substream, 0);
1796 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1797 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1798 snd_pcm_stream_unlock_irq(substream);
1804 static bool is_pcm_file(struct file *file)
1806 struct inode *inode = file_inode(file);
1809 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1811 minor = iminor(inode);
1812 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1813 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1819 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1822 struct snd_pcm_file *pcm_file;
1823 struct snd_pcm_substream *substream1;
1824 struct snd_pcm_group *group;
1825 struct fd f = fdget(fd);
1829 if (!is_pcm_file(f.file)) {
1833 pcm_file = f.file->private_data;
1834 substream1 = pcm_file->substream;
1835 group = kmalloc(sizeof(*group), GFP_KERNEL);
1840 down_write_nonfifo(&snd_pcm_link_rwsem);
1841 write_lock_irq(&snd_pcm_link_rwlock);
1842 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1843 substream->runtime->status->state != substream1->runtime->status->state ||
1844 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1848 if (snd_pcm_stream_linked(substream1)) {
1852 if (!snd_pcm_stream_linked(substream)) {
1853 substream->group = group;
1855 spin_lock_init(&substream->group->lock);
1856 mutex_init(&substream->group->mutex);
1857 INIT_LIST_HEAD(&substream->group->substreams);
1858 list_add_tail(&substream->link_list, &substream->group->substreams);
1859 substream->group->count = 1;
1861 list_add_tail(&substream1->link_list, &substream->group->substreams);
1862 substream->group->count++;
1863 substream1->group = substream->group;
1865 write_unlock_irq(&snd_pcm_link_rwlock);
1866 up_write(&snd_pcm_link_rwsem);
1868 snd_card_unref(substream1->pcm->card);
1875 static void relink_to_local(struct snd_pcm_substream *substream)
1877 substream->group = &substream->self_group;
1878 INIT_LIST_HEAD(&substream->self_group.substreams);
1879 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1882 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1884 struct snd_pcm_substream *s;
1887 down_write_nonfifo(&snd_pcm_link_rwsem);
1888 write_lock_irq(&snd_pcm_link_rwlock);
1889 if (!snd_pcm_stream_linked(substream)) {
1893 list_del(&substream->link_list);
1894 substream->group->count--;
1895 if (substream->group->count == 1) { /* detach the last stream, too */
1896 snd_pcm_group_for_each_entry(s, substream) {
1900 kfree(substream->group);
1902 relink_to_local(substream);
1904 write_unlock_irq(&snd_pcm_link_rwlock);
1905 up_write(&snd_pcm_link_rwsem);
1912 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1913 struct snd_pcm_hw_rule *rule)
1915 struct snd_interval t;
1916 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1917 hw_param_interval_c(params, rule->deps[1]), &t);
1918 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1921 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1922 struct snd_pcm_hw_rule *rule)
1924 struct snd_interval t;
1925 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1926 hw_param_interval_c(params, rule->deps[1]), &t);
1927 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1930 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1931 struct snd_pcm_hw_rule *rule)
1933 struct snd_interval t;
1934 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1935 hw_param_interval_c(params, rule->deps[1]),
1936 (unsigned long) rule->private, &t);
1937 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1940 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1941 struct snd_pcm_hw_rule *rule)
1943 struct snd_interval t;
1944 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1945 (unsigned long) rule->private,
1946 hw_param_interval_c(params, rule->deps[1]), &t);
1947 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1950 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1951 struct snd_pcm_hw_rule *rule)
1954 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1956 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1958 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1960 if (! snd_mask_test(mask, k))
1962 bits = snd_pcm_format_physical_width(k);
1964 continue; /* ignore invalid formats */
1965 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1966 snd_mask_reset(&m, k);
1968 return snd_mask_refine(mask, &m);
1971 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1972 struct snd_pcm_hw_rule *rule)
1974 struct snd_interval t;
1980 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1982 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1984 bits = snd_pcm_format_physical_width(k);
1986 continue; /* ignore invalid formats */
1987 if (t.min > (unsigned)bits)
1989 if (t.max < (unsigned)bits)
1993 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1996 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1997 #error "Change this table"
2000 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
2001 48000, 64000, 88200, 96000, 176400, 192000 };
2003 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
2004 .count = ARRAY_SIZE(rates),
2008 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2009 struct snd_pcm_hw_rule *rule)
2011 struct snd_pcm_hardware *hw = rule->private;
2012 return snd_interval_list(hw_param_interval(params, rule->var),
2013 snd_pcm_known_rates.count,
2014 snd_pcm_known_rates.list, hw->rates);
2017 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2018 struct snd_pcm_hw_rule *rule)
2020 struct snd_interval t;
2021 struct snd_pcm_substream *substream = rule->private;
2023 t.max = substream->buffer_bytes_max;
2027 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2030 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2032 struct snd_pcm_runtime *runtime = substream->runtime;
2033 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2036 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2037 snd_mask_any(constrs_mask(constrs, k));
2040 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2041 snd_interval_any(constrs_interval(constrs, k));
2044 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2045 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2046 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2047 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2048 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2050 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2051 snd_pcm_hw_rule_format, NULL,
2052 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2055 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2056 snd_pcm_hw_rule_sample_bits, NULL,
2057 SNDRV_PCM_HW_PARAM_FORMAT,
2058 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2061 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2062 snd_pcm_hw_rule_div, NULL,
2063 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2066 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2067 snd_pcm_hw_rule_mul, NULL,
2068 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2071 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2072 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2073 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2076 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2077 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2078 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2081 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2082 snd_pcm_hw_rule_div, NULL,
2083 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2086 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2087 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2088 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2091 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2092 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2093 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2096 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2097 snd_pcm_hw_rule_div, NULL,
2098 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2101 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2102 snd_pcm_hw_rule_div, NULL,
2103 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2106 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2107 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2108 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2111 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2112 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2113 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2116 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2117 snd_pcm_hw_rule_mul, NULL,
2118 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2121 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2122 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2123 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2126 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2127 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2128 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2131 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2132 snd_pcm_hw_rule_muldivk, (void*) 8,
2133 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2136 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2137 snd_pcm_hw_rule_muldivk, (void*) 8,
2138 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2141 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2142 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2143 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2146 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2147 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2148 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2154 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2156 struct snd_pcm_runtime *runtime = substream->runtime;
2157 struct snd_pcm_hardware *hw = &runtime->hw;
2159 unsigned int mask = 0;
2161 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2162 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2163 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2164 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2165 if (hw_support_mmap(substream)) {
2166 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2167 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2168 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2169 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2170 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2171 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2173 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2177 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2181 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2185 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2186 hw->channels_min, hw->channels_max);
2190 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2191 hw->rate_min, hw->rate_max);
2195 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2196 hw->period_bytes_min, hw->period_bytes_max);
2200 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2201 hw->periods_min, hw->periods_max);
2205 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2206 hw->period_bytes_min, hw->buffer_bytes_max);
2210 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2211 snd_pcm_hw_rule_buffer_bytes_max, substream,
2212 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2217 if (runtime->dma_bytes) {
2218 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2223 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2224 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2225 snd_pcm_hw_rule_rate, hw,
2226 SNDRV_PCM_HW_PARAM_RATE, -1);
2231 /* FIXME: this belong to lowlevel */
2232 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2237 static void pcm_release_private(struct snd_pcm_substream *substream)
2239 if (snd_pcm_stream_linked(substream))
2240 snd_pcm_unlink(substream);
2243 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2245 substream->ref_count--;
2246 if (substream->ref_count > 0)
2249 snd_pcm_drop(substream);
2250 if (substream->hw_opened) {
2251 if (substream->ops->hw_free &&
2252 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2253 substream->ops->hw_free(substream);
2254 substream->ops->close(substream);
2255 substream->hw_opened = 0;
2257 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2258 pm_qos_remove_request(&substream->latency_pm_qos_req);
2259 if (substream->pcm_release) {
2260 substream->pcm_release(substream);
2261 substream->pcm_release = NULL;
2263 snd_pcm_detach_substream(substream);
2266 EXPORT_SYMBOL(snd_pcm_release_substream);
2268 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2270 struct snd_pcm_substream **rsubstream)
2272 struct snd_pcm_substream *substream;
2275 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2278 if (substream->ref_count > 1) {
2279 *rsubstream = substream;
2283 err = snd_pcm_hw_constraints_init(substream);
2285 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2289 if ((err = substream->ops->open(substream)) < 0)
2292 substream->hw_opened = 1;
2294 err = snd_pcm_hw_constraints_complete(substream);
2296 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2300 *rsubstream = substream;
2304 snd_pcm_release_substream(substream);
2308 EXPORT_SYMBOL(snd_pcm_open_substream);
2310 static int snd_pcm_open_file(struct file *file,
2311 struct snd_pcm *pcm,
2314 struct snd_pcm_file *pcm_file;
2315 struct snd_pcm_substream *substream;
2318 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2322 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2323 if (pcm_file == NULL) {
2324 snd_pcm_release_substream(substream);
2327 pcm_file->substream = substream;
2328 if (substream->ref_count == 1) {
2329 substream->file = pcm_file;
2330 substream->pcm_release = pcm_release_private;
2332 file->private_data = pcm_file;
2337 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2339 struct snd_pcm *pcm;
2340 int err = nonseekable_open(inode, file);
2343 pcm = snd_lookup_minor_data(iminor(inode),
2344 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2345 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2347 snd_card_unref(pcm->card);
2351 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2353 struct snd_pcm *pcm;
2354 int err = nonseekable_open(inode, file);
2357 pcm = snd_lookup_minor_data(iminor(inode),
2358 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2359 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2361 snd_card_unref(pcm->card);
2365 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2374 err = snd_card_file_add(pcm->card, file);
2377 if (!try_module_get(pcm->card->module)) {
2381 init_waitqueue_entry(&wait, current);
2382 add_wait_queue(&pcm->open_wait, &wait);
2383 mutex_lock(&pcm->open_mutex);
2385 err = snd_pcm_open_file(file, pcm, stream);
2388 if (err == -EAGAIN) {
2389 if (file->f_flags & O_NONBLOCK) {
2395 set_current_state(TASK_INTERRUPTIBLE);
2396 mutex_unlock(&pcm->open_mutex);
2398 mutex_lock(&pcm->open_mutex);
2399 if (pcm->card->shutdown) {
2403 if (signal_pending(current)) {
2408 remove_wait_queue(&pcm->open_wait, &wait);
2409 mutex_unlock(&pcm->open_mutex);
2415 module_put(pcm->card->module);
2417 snd_card_file_remove(pcm->card, file);
2422 static int snd_pcm_release(struct inode *inode, struct file *file)
2424 struct snd_pcm *pcm;
2425 struct snd_pcm_substream *substream;
2426 struct snd_pcm_file *pcm_file;
2428 pcm_file = file->private_data;
2429 substream = pcm_file->substream;
2430 if (snd_BUG_ON(!substream))
2432 pcm = substream->pcm;
2433 mutex_lock(&pcm->open_mutex);
2434 snd_pcm_release_substream(substream);
2436 mutex_unlock(&pcm->open_mutex);
2437 wake_up(&pcm->open_wait);
2438 module_put(pcm->card->module);
2439 snd_card_file_remove(pcm->card, file);
2443 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2444 snd_pcm_uframes_t frames)
2446 struct snd_pcm_runtime *runtime = substream->runtime;
2447 snd_pcm_sframes_t appl_ptr;
2448 snd_pcm_sframes_t ret;
2449 snd_pcm_sframes_t hw_avail;
2454 snd_pcm_stream_lock_irq(substream);
2455 switch (runtime->status->state) {
2456 case SNDRV_PCM_STATE_PREPARED:
2458 case SNDRV_PCM_STATE_DRAINING:
2459 case SNDRV_PCM_STATE_RUNNING:
2460 if (snd_pcm_update_hw_ptr(substream) >= 0)
2463 case SNDRV_PCM_STATE_XRUN:
2466 case SNDRV_PCM_STATE_SUSPENDED:
2474 hw_avail = snd_pcm_playback_hw_avail(runtime);
2475 if (hw_avail <= 0) {
2479 if (frames > (snd_pcm_uframes_t)hw_avail)
2481 appl_ptr = runtime->control->appl_ptr - frames;
2483 appl_ptr += runtime->boundary;
2484 runtime->control->appl_ptr = appl_ptr;
2487 snd_pcm_stream_unlock_irq(substream);
2491 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2492 snd_pcm_uframes_t frames)
2494 struct snd_pcm_runtime *runtime = substream->runtime;
2495 snd_pcm_sframes_t appl_ptr;
2496 snd_pcm_sframes_t ret;
2497 snd_pcm_sframes_t hw_avail;
2502 snd_pcm_stream_lock_irq(substream);
2503 switch (runtime->status->state) {
2504 case SNDRV_PCM_STATE_PREPARED:
2505 case SNDRV_PCM_STATE_DRAINING:
2507 case SNDRV_PCM_STATE_RUNNING:
2508 if (snd_pcm_update_hw_ptr(substream) >= 0)
2511 case SNDRV_PCM_STATE_XRUN:
2514 case SNDRV_PCM_STATE_SUSPENDED:
2522 hw_avail = snd_pcm_capture_hw_avail(runtime);
2523 if (hw_avail <= 0) {
2527 if (frames > (snd_pcm_uframes_t)hw_avail)
2529 appl_ptr = runtime->control->appl_ptr - frames;
2531 appl_ptr += runtime->boundary;
2532 runtime->control->appl_ptr = appl_ptr;
2535 snd_pcm_stream_unlock_irq(substream);
2539 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2540 snd_pcm_uframes_t frames)
2542 struct snd_pcm_runtime *runtime = substream->runtime;
2543 snd_pcm_sframes_t appl_ptr;
2544 snd_pcm_sframes_t ret;
2545 snd_pcm_sframes_t avail;
2550 snd_pcm_stream_lock_irq(substream);
2551 switch (runtime->status->state) {
2552 case SNDRV_PCM_STATE_PREPARED:
2553 case SNDRV_PCM_STATE_PAUSED:
2555 case SNDRV_PCM_STATE_DRAINING:
2556 case SNDRV_PCM_STATE_RUNNING:
2557 if (snd_pcm_update_hw_ptr(substream) >= 0)
2560 case SNDRV_PCM_STATE_XRUN:
2563 case SNDRV_PCM_STATE_SUSPENDED:
2571 avail = snd_pcm_playback_avail(runtime);
2576 if (frames > (snd_pcm_uframes_t)avail)
2578 appl_ptr = runtime->control->appl_ptr + frames;
2579 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2580 appl_ptr -= runtime->boundary;
2581 runtime->control->appl_ptr = appl_ptr;
2584 snd_pcm_stream_unlock_irq(substream);
2588 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2589 snd_pcm_uframes_t frames)
2591 struct snd_pcm_runtime *runtime = substream->runtime;
2592 snd_pcm_sframes_t appl_ptr;
2593 snd_pcm_sframes_t ret;
2594 snd_pcm_sframes_t avail;
2599 snd_pcm_stream_lock_irq(substream);
2600 switch (runtime->status->state) {
2601 case SNDRV_PCM_STATE_PREPARED:
2602 case SNDRV_PCM_STATE_DRAINING:
2603 case SNDRV_PCM_STATE_PAUSED:
2605 case SNDRV_PCM_STATE_RUNNING:
2606 if (snd_pcm_update_hw_ptr(substream) >= 0)
2609 case SNDRV_PCM_STATE_XRUN:
2612 case SNDRV_PCM_STATE_SUSPENDED:
2620 avail = snd_pcm_capture_avail(runtime);
2625 if (frames > (snd_pcm_uframes_t)avail)
2627 appl_ptr = runtime->control->appl_ptr + frames;
2628 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2629 appl_ptr -= runtime->boundary;
2630 runtime->control->appl_ptr = appl_ptr;
2633 snd_pcm_stream_unlock_irq(substream);
2637 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2639 struct snd_pcm_runtime *runtime = substream->runtime;
2642 snd_pcm_stream_lock_irq(substream);
2643 switch (runtime->status->state) {
2644 case SNDRV_PCM_STATE_DRAINING:
2645 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2648 case SNDRV_PCM_STATE_RUNNING:
2649 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2652 case SNDRV_PCM_STATE_PREPARED:
2653 case SNDRV_PCM_STATE_SUSPENDED:
2656 case SNDRV_PCM_STATE_XRUN:
2664 snd_pcm_stream_unlock_irq(substream);
2668 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2669 snd_pcm_sframes_t __user *res)
2671 struct snd_pcm_runtime *runtime = substream->runtime;
2673 snd_pcm_sframes_t n = 0;
2675 snd_pcm_stream_lock_irq(substream);
2676 switch (runtime->status->state) {
2677 case SNDRV_PCM_STATE_DRAINING:
2678 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2681 case SNDRV_PCM_STATE_RUNNING:
2682 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2685 case SNDRV_PCM_STATE_PREPARED:
2686 case SNDRV_PCM_STATE_SUSPENDED:
2688 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2689 n = snd_pcm_playback_hw_avail(runtime);
2691 n = snd_pcm_capture_avail(runtime);
2692 n += runtime->delay;
2694 case SNDRV_PCM_STATE_XRUN:
2702 snd_pcm_stream_unlock_irq(substream);
2704 if (put_user(n, res))
2709 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2710 struct snd_pcm_sync_ptr __user *_sync_ptr)
2712 struct snd_pcm_runtime *runtime = substream->runtime;
2713 struct snd_pcm_sync_ptr sync_ptr;
2714 volatile struct snd_pcm_mmap_status *status;
2715 volatile struct snd_pcm_mmap_control *control;
2718 memset(&sync_ptr, 0, sizeof(sync_ptr));
2719 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2721 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2723 status = runtime->status;
2724 control = runtime->control;
2725 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2726 err = snd_pcm_hwsync(substream);
2730 snd_pcm_stream_lock_irq(substream);
2731 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2732 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2734 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2735 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2736 control->avail_min = sync_ptr.c.control.avail_min;
2738 sync_ptr.c.control.avail_min = control->avail_min;
2739 sync_ptr.s.status.state = status->state;
2740 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2741 sync_ptr.s.status.tstamp = status->tstamp;
2742 sync_ptr.s.status.suspended_state = status->suspended_state;
2743 sync_ptr.s.status.audio_tstamp = status->audio_tstamp;
2744 snd_pcm_stream_unlock_irq(substream);
2745 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2750 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2752 struct snd_pcm_runtime *runtime = substream->runtime;
2755 if (get_user(arg, _arg))
2757 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2759 runtime->tstamp_type = arg;
2763 static int snd_pcm_common_ioctl1(struct file *file,
2764 struct snd_pcm_substream *substream,
2765 unsigned int cmd, void __user *arg)
2768 case SNDRV_PCM_IOCTL_PVERSION:
2769 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2770 case SNDRV_PCM_IOCTL_INFO:
2771 return snd_pcm_info_user(substream, arg);
2772 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2774 case SNDRV_PCM_IOCTL_TTSTAMP:
2775 return snd_pcm_tstamp(substream, arg);
2776 case SNDRV_PCM_IOCTL_HW_REFINE:
2777 return snd_pcm_hw_refine_user(substream, arg);
2778 case SNDRV_PCM_IOCTL_HW_PARAMS:
2779 return snd_pcm_hw_params_user(substream, arg);
2780 case SNDRV_PCM_IOCTL_HW_FREE:
2781 return snd_pcm_hw_free(substream);
2782 case SNDRV_PCM_IOCTL_SW_PARAMS:
2783 return snd_pcm_sw_params_user(substream, arg);
2784 case SNDRV_PCM_IOCTL_STATUS:
2785 return snd_pcm_status_user(substream, arg, false);
2786 case SNDRV_PCM_IOCTL_STATUS_EXT:
2787 return snd_pcm_status_user(substream, arg, true);
2788 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2789 return snd_pcm_channel_info_user(substream, arg);
2790 case SNDRV_PCM_IOCTL_PREPARE:
2791 return snd_pcm_prepare(substream, file);
2792 case SNDRV_PCM_IOCTL_RESET:
2793 return snd_pcm_reset(substream);
2794 case SNDRV_PCM_IOCTL_START:
2795 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2796 case SNDRV_PCM_IOCTL_LINK:
2797 return snd_pcm_link(substream, (int)(unsigned long) arg);
2798 case SNDRV_PCM_IOCTL_UNLINK:
2799 return snd_pcm_unlink(substream);
2800 case SNDRV_PCM_IOCTL_RESUME:
2801 return snd_pcm_resume(substream);
2802 case SNDRV_PCM_IOCTL_XRUN:
2803 return snd_pcm_xrun(substream);
2804 case SNDRV_PCM_IOCTL_HWSYNC:
2805 return snd_pcm_hwsync(substream);
2806 case SNDRV_PCM_IOCTL_DELAY:
2807 return snd_pcm_delay(substream, arg);
2808 case SNDRV_PCM_IOCTL_SYNC_PTR:
2809 return snd_pcm_sync_ptr(substream, arg);
2810 #ifdef CONFIG_SND_SUPPORT_OLD_API
2811 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2812 return snd_pcm_hw_refine_old_user(substream, arg);
2813 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2814 return snd_pcm_hw_params_old_user(substream, arg);
2816 case SNDRV_PCM_IOCTL_DRAIN:
2817 return snd_pcm_drain(substream, file);
2818 case SNDRV_PCM_IOCTL_DROP:
2819 return snd_pcm_drop(substream);
2820 case SNDRV_PCM_IOCTL_PAUSE:
2823 snd_pcm_stream_lock_irq(substream);
2824 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2825 snd_pcm_stream_unlock_irq(substream);
2829 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2833 static int snd_pcm_playback_ioctl1(struct file *file,
2834 struct snd_pcm_substream *substream,
2835 unsigned int cmd, void __user *arg)
2837 if (snd_BUG_ON(!substream))
2839 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2842 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2844 struct snd_xferi xferi;
2845 struct snd_xferi __user *_xferi = arg;
2846 struct snd_pcm_runtime *runtime = substream->runtime;
2847 snd_pcm_sframes_t result;
2848 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2850 if (put_user(0, &_xferi->result))
2852 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2854 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2855 __put_user(result, &_xferi->result);
2856 return result < 0 ? result : 0;
2858 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2860 struct snd_xfern xfern;
2861 struct snd_xfern __user *_xfern = arg;
2862 struct snd_pcm_runtime *runtime = substream->runtime;
2864 snd_pcm_sframes_t result;
2865 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 if (runtime->channels > 128)
2869 if (put_user(0, &_xfern->result))
2871 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2874 bufs = memdup_user(xfern.bufs,
2875 sizeof(void *) * runtime->channels);
2877 return PTR_ERR(bufs);
2878 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2880 __put_user(result, &_xfern->result);
2881 return result < 0 ? result : 0;
2883 case SNDRV_PCM_IOCTL_REWIND:
2885 snd_pcm_uframes_t frames;
2886 snd_pcm_uframes_t __user *_frames = arg;
2887 snd_pcm_sframes_t result;
2888 if (get_user(frames, _frames))
2890 if (put_user(0, _frames))
2892 result = snd_pcm_playback_rewind(substream, frames);
2893 __put_user(result, _frames);
2894 return result < 0 ? result : 0;
2896 case SNDRV_PCM_IOCTL_FORWARD:
2898 snd_pcm_uframes_t frames;
2899 snd_pcm_uframes_t __user *_frames = arg;
2900 snd_pcm_sframes_t result;
2901 if (get_user(frames, _frames))
2903 if (put_user(0, _frames))
2905 result = snd_pcm_playback_forward(substream, frames);
2906 __put_user(result, _frames);
2907 return result < 0 ? result : 0;
2910 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2913 static int snd_pcm_capture_ioctl1(struct file *file,
2914 struct snd_pcm_substream *substream,
2915 unsigned int cmd, void __user *arg)
2917 if (snd_BUG_ON(!substream))
2919 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2922 case SNDRV_PCM_IOCTL_READI_FRAMES:
2924 struct snd_xferi xferi;
2925 struct snd_xferi __user *_xferi = arg;
2926 struct snd_pcm_runtime *runtime = substream->runtime;
2927 snd_pcm_sframes_t result;
2928 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2930 if (put_user(0, &_xferi->result))
2932 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2934 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2935 __put_user(result, &_xferi->result);
2936 return result < 0 ? result : 0;
2938 case SNDRV_PCM_IOCTL_READN_FRAMES:
2940 struct snd_xfern xfern;
2941 struct snd_xfern __user *_xfern = arg;
2942 struct snd_pcm_runtime *runtime = substream->runtime;
2944 snd_pcm_sframes_t result;
2945 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2947 if (runtime->channels > 128)
2949 if (put_user(0, &_xfern->result))
2951 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2954 bufs = memdup_user(xfern.bufs,
2955 sizeof(void *) * runtime->channels);
2957 return PTR_ERR(bufs);
2958 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2960 __put_user(result, &_xfern->result);
2961 return result < 0 ? result : 0;
2963 case SNDRV_PCM_IOCTL_REWIND:
2965 snd_pcm_uframes_t frames;
2966 snd_pcm_uframes_t __user *_frames = arg;
2967 snd_pcm_sframes_t result;
2968 if (get_user(frames, _frames))
2970 if (put_user(0, _frames))
2972 result = snd_pcm_capture_rewind(substream, frames);
2973 __put_user(result, _frames);
2974 return result < 0 ? result : 0;
2976 case SNDRV_PCM_IOCTL_FORWARD:
2978 snd_pcm_uframes_t frames;
2979 snd_pcm_uframes_t __user *_frames = arg;
2980 snd_pcm_sframes_t result;
2981 if (get_user(frames, _frames))
2983 if (put_user(0, _frames))
2985 result = snd_pcm_capture_forward(substream, frames);
2986 __put_user(result, _frames);
2987 return result < 0 ? result : 0;
2990 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2993 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2996 struct snd_pcm_file *pcm_file;
2998 pcm_file = file->private_data;
3000 if (((cmd >> 8) & 0xff) != 'A')
3003 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
3004 (void __user *)arg);
3007 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
3010 struct snd_pcm_file *pcm_file;
3012 pcm_file = file->private_data;
3014 if (((cmd >> 8) & 0xff) != 'A')
3017 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
3018 (void __user *)arg);
3021 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
3022 unsigned int cmd, void *arg)
3027 fs = snd_enter_user();
3028 switch (substream->stream) {
3029 case SNDRV_PCM_STREAM_PLAYBACK:
3030 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
3031 (void __user *)arg);
3033 case SNDRV_PCM_STREAM_CAPTURE:
3034 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
3035 (void __user *)arg);
3045 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
3047 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
3050 struct snd_pcm_file *pcm_file;
3051 struct snd_pcm_substream *substream;
3052 struct snd_pcm_runtime *runtime;
3053 snd_pcm_sframes_t result;
3055 pcm_file = file->private_data;
3056 substream = pcm_file->substream;
3057 if (PCM_RUNTIME_CHECK(substream))
3059 runtime = substream->runtime;
3060 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3062 if (!frame_aligned(runtime, count))
3064 count = bytes_to_frames(runtime, count);
3065 result = snd_pcm_lib_read(substream, buf, count);
3067 result = frames_to_bytes(runtime, result);
3071 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
3072 size_t count, loff_t * offset)
3074 struct snd_pcm_file *pcm_file;
3075 struct snd_pcm_substream *substream;
3076 struct snd_pcm_runtime *runtime;
3077 snd_pcm_sframes_t result;
3079 pcm_file = file->private_data;
3080 substream = pcm_file->substream;
3081 if (PCM_RUNTIME_CHECK(substream))
3083 runtime = substream->runtime;
3084 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3086 if (!frame_aligned(runtime, count))
3088 count = bytes_to_frames(runtime, count);
3089 result = snd_pcm_lib_write(substream, buf, count);
3091 result = frames_to_bytes(runtime, result);
3095 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
3097 struct snd_pcm_file *pcm_file;
3098 struct snd_pcm_substream *substream;
3099 struct snd_pcm_runtime *runtime;
3100 snd_pcm_sframes_t result;
3103 snd_pcm_uframes_t frames;
3105 pcm_file = iocb->ki_filp->private_data;
3106 substream = pcm_file->substream;
3107 if (PCM_RUNTIME_CHECK(substream))
3109 runtime = substream->runtime;
3110 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3112 if (!iter_is_iovec(to))
3114 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3116 if (!frame_aligned(runtime, to->iov->iov_len))
3118 frames = bytes_to_samples(runtime, to->iov->iov_len);
3119 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3122 for (i = 0; i < to->nr_segs; ++i)
3123 bufs[i] = to->iov[i].iov_base;
3124 result = snd_pcm_lib_readv(substream, bufs, frames);
3126 result = frames_to_bytes(runtime, result);
3131 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3133 struct snd_pcm_file *pcm_file;
3134 struct snd_pcm_substream *substream;
3135 struct snd_pcm_runtime *runtime;
3136 snd_pcm_sframes_t result;
3139 snd_pcm_uframes_t frames;
3141 pcm_file = iocb->ki_filp->private_data;
3142 substream = pcm_file->substream;
3143 if (PCM_RUNTIME_CHECK(substream))
3145 runtime = substream->runtime;
3146 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3148 if (!iter_is_iovec(from))
3150 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3151 !frame_aligned(runtime, from->iov->iov_len))
3153 frames = bytes_to_samples(runtime, from->iov->iov_len);
3154 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3157 for (i = 0; i < from->nr_segs; ++i)
3158 bufs[i] = from->iov[i].iov_base;
3159 result = snd_pcm_lib_writev(substream, bufs, frames);
3161 result = frames_to_bytes(runtime, result);
3166 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3168 struct snd_pcm_file *pcm_file;
3169 struct snd_pcm_substream *substream;
3170 struct snd_pcm_runtime *runtime;
3172 snd_pcm_uframes_t avail;
3174 pcm_file = file->private_data;
3176 substream = pcm_file->substream;
3177 if (PCM_RUNTIME_CHECK(substream))
3179 runtime = substream->runtime;
3181 poll_wait(file, &runtime->sleep, wait);
3183 snd_pcm_stream_lock_irq(substream);
3184 avail = snd_pcm_playback_avail(runtime);
3185 switch (runtime->status->state) {
3186 case SNDRV_PCM_STATE_RUNNING:
3187 case SNDRV_PCM_STATE_PREPARED:
3188 case SNDRV_PCM_STATE_PAUSED:
3189 if (avail >= runtime->control->avail_min) {
3190 mask = POLLOUT | POLLWRNORM;
3194 case SNDRV_PCM_STATE_DRAINING:
3198 mask = POLLOUT | POLLWRNORM | POLLERR;
3201 snd_pcm_stream_unlock_irq(substream);
3205 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3207 struct snd_pcm_file *pcm_file;
3208 struct snd_pcm_substream *substream;
3209 struct snd_pcm_runtime *runtime;
3211 snd_pcm_uframes_t avail;
3213 pcm_file = file->private_data;
3215 substream = pcm_file->substream;
3216 if (PCM_RUNTIME_CHECK(substream))
3218 runtime = substream->runtime;
3220 poll_wait(file, &runtime->sleep, wait);
3222 snd_pcm_stream_lock_irq(substream);
3223 avail = snd_pcm_capture_avail(runtime);
3224 switch (runtime->status->state) {
3225 case SNDRV_PCM_STATE_RUNNING:
3226 case SNDRV_PCM_STATE_PREPARED:
3227 case SNDRV_PCM_STATE_PAUSED:
3228 if (avail >= runtime->control->avail_min) {
3229 mask = POLLIN | POLLRDNORM;
3234 case SNDRV_PCM_STATE_DRAINING:
3236 mask = POLLIN | POLLRDNORM;
3241 mask = POLLIN | POLLRDNORM | POLLERR;
3244 snd_pcm_stream_unlock_irq(substream);
3253 * Only on coherent architectures, we can mmap the status and the control records
3254 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3256 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3258 * mmap status record
3260 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3261 struct vm_fault *vmf)
3263 struct snd_pcm_substream *substream = area->vm_private_data;
3264 struct snd_pcm_runtime *runtime;
3266 if (substream == NULL)
3267 return VM_FAULT_SIGBUS;
3268 runtime = substream->runtime;
3269 vmf->page = virt_to_page(runtime->status);
3270 get_page(vmf->page);
3274 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3276 .fault = snd_pcm_mmap_status_fault,
3279 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3280 struct vm_area_struct *area)
3283 if (!(area->vm_flags & VM_READ))
3285 size = area->vm_end - area->vm_start;
3286 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3288 area->vm_ops = &snd_pcm_vm_ops_status;
3289 area->vm_private_data = substream;
3290 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3295 * mmap control record
3297 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3298 struct vm_fault *vmf)
3300 struct snd_pcm_substream *substream = area->vm_private_data;
3301 struct snd_pcm_runtime *runtime;
3303 if (substream == NULL)
3304 return VM_FAULT_SIGBUS;
3305 runtime = substream->runtime;
3306 vmf->page = virt_to_page(runtime->control);
3307 get_page(vmf->page);
3311 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3313 .fault = snd_pcm_mmap_control_fault,
3316 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3317 struct vm_area_struct *area)
3320 if (!(area->vm_flags & VM_READ))
3322 size = area->vm_end - area->vm_start;
3323 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3325 area->vm_ops = &snd_pcm_vm_ops_control;
3326 area->vm_private_data = substream;
3327 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3330 #else /* ! coherent mmap */
3332 * don't support mmap for status and control records.
3334 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3335 struct vm_area_struct *area)
3339 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3340 struct vm_area_struct *area)
3344 #endif /* coherent mmap */
3346 static inline struct page *
3347 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3349 void *vaddr = substream->runtime->dma_area + ofs;
3350 return virt_to_page(vaddr);
3354 * fault callback for mmapping a RAM page
3356 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3357 struct vm_fault *vmf)
3359 struct snd_pcm_substream *substream = area->vm_private_data;
3360 struct snd_pcm_runtime *runtime;
3361 unsigned long offset;
3365 if (substream == NULL)
3366 return VM_FAULT_SIGBUS;
3367 runtime = substream->runtime;
3368 offset = vmf->pgoff << PAGE_SHIFT;
3369 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3370 if (offset > dma_bytes - PAGE_SIZE)
3371 return VM_FAULT_SIGBUS;
3372 if (substream->ops->page)
3373 page = substream->ops->page(substream, offset);
3375 page = snd_pcm_default_page_ops(substream, offset);
3377 return VM_FAULT_SIGBUS;
3383 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3384 .open = snd_pcm_mmap_data_open,
3385 .close = snd_pcm_mmap_data_close,
3388 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3389 .open = snd_pcm_mmap_data_open,
3390 .close = snd_pcm_mmap_data_close,
3391 .fault = snd_pcm_mmap_data_fault,
3395 * mmap the DMA buffer on RAM
3399 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3400 * @substream: PCM substream
3403 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3404 * this function is invoked implicitly.
3406 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3407 struct vm_area_struct *area)
3409 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3410 #ifdef CONFIG_GENERIC_ALLOCATOR
3411 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3412 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3413 return remap_pfn_range(area, area->vm_start,
3414 substream->dma_buffer.addr >> PAGE_SHIFT,
3415 area->vm_end - area->vm_start, area->vm_page_prot);
3417 #endif /* CONFIG_GENERIC_ALLOCATOR */
3418 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3419 if (!substream->ops->page &&
3420 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3421 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3423 substream->runtime->dma_area,
3424 substream->runtime->dma_addr,
3425 substream->runtime->dma_bytes);
3426 #endif /* CONFIG_X86 */
3427 /* mmap with fault handler */
3428 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3431 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3434 * mmap the DMA buffer on I/O memory area
3436 #if SNDRV_PCM_INFO_MMAP_IOMEM
3438 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3439 * @substream: PCM substream
3442 * When your hardware uses the iomapped pages as the hardware buffer and
3443 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3444 * is supposed to work only on limited architectures.
3446 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3447 struct vm_area_struct *area)
3449 struct snd_pcm_runtime *runtime = substream->runtime;;
3451 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3452 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3455 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3456 #endif /* SNDRV_PCM_INFO_MMAP */
3461 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3462 struct vm_area_struct *area)
3464 struct snd_pcm_runtime *runtime;
3466 unsigned long offset;
3470 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3471 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3474 if (!(area->vm_flags & VM_READ))
3477 runtime = substream->runtime;
3478 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3480 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3482 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3483 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3485 size = area->vm_end - area->vm_start;
3486 offset = area->vm_pgoff << PAGE_SHIFT;
3487 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3488 if ((size_t)size > dma_bytes)
3490 if (offset > dma_bytes - size)
3493 area->vm_ops = &snd_pcm_vm_ops_data;
3494 area->vm_private_data = substream;
3495 if (substream->ops->mmap)
3496 err = substream->ops->mmap(substream, area);
3498 err = snd_pcm_lib_default_mmap(substream, area);
3500 atomic_inc(&substream->mmap_count);
3504 EXPORT_SYMBOL(snd_pcm_mmap_data);
3506 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3508 struct snd_pcm_file * pcm_file;
3509 struct snd_pcm_substream *substream;
3510 unsigned long offset;
3512 pcm_file = file->private_data;
3513 substream = pcm_file->substream;
3514 if (PCM_RUNTIME_CHECK(substream))
3517 offset = area->vm_pgoff << PAGE_SHIFT;
3519 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3520 if (pcm_file->no_compat_mmap)
3522 return snd_pcm_mmap_status(substream, file, area);
3523 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3524 if (pcm_file->no_compat_mmap)
3526 return snd_pcm_mmap_control(substream, file, area);
3528 return snd_pcm_mmap_data(substream, file, area);
3533 static int snd_pcm_fasync(int fd, struct file * file, int on)
3535 struct snd_pcm_file * pcm_file;
3536 struct snd_pcm_substream *substream;
3537 struct snd_pcm_runtime *runtime;
3539 pcm_file = file->private_data;
3540 substream = pcm_file->substream;
3541 if (PCM_RUNTIME_CHECK(substream))
3543 runtime = substream->runtime;
3544 return fasync_helper(fd, file, on, &runtime->fasync);
3550 #ifdef CONFIG_COMPAT
3551 #include "pcm_compat.c"
3553 #define snd_pcm_ioctl_compat NULL
3557 * To be removed helpers to keep binary compatibility
3560 #ifdef CONFIG_SND_SUPPORT_OLD_API
3561 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3562 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3564 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3565 struct snd_pcm_hw_params_old *oparams)
3569 memset(params, 0, sizeof(*params));
3570 params->flags = oparams->flags;
3571 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3572 params->masks[i].bits[0] = oparams->masks[i];
3573 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3574 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3575 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3576 params->info = oparams->info;
3577 params->msbits = oparams->msbits;
3578 params->rate_num = oparams->rate_num;
3579 params->rate_den = oparams->rate_den;
3580 params->fifo_size = oparams->fifo_size;
3583 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3584 struct snd_pcm_hw_params *params)
3588 memset(oparams, 0, sizeof(*oparams));
3589 oparams->flags = params->flags;
3590 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3591 oparams->masks[i] = params->masks[i].bits[0];
3592 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3593 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3594 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3595 oparams->info = params->info;
3596 oparams->msbits = params->msbits;
3597 oparams->rate_num = params->rate_num;
3598 oparams->rate_den = params->rate_den;
3599 oparams->fifo_size = params->fifo_size;
3602 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3603 struct snd_pcm_hw_params_old __user * _oparams)
3605 struct snd_pcm_hw_params *params;
3606 struct snd_pcm_hw_params_old *oparams = NULL;
3609 params = kmalloc(sizeof(*params), GFP_KERNEL);
3613 oparams = memdup_user(_oparams, sizeof(*oparams));
3614 if (IS_ERR(oparams)) {
3615 err = PTR_ERR(oparams);
3618 snd_pcm_hw_convert_from_old_params(params, oparams);
3619 err = snd_pcm_hw_refine(substream, params);
3620 snd_pcm_hw_convert_to_old_params(oparams, params);
3621 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3632 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3633 struct snd_pcm_hw_params_old __user * _oparams)
3635 struct snd_pcm_hw_params *params;
3636 struct snd_pcm_hw_params_old *oparams = NULL;
3639 params = kmalloc(sizeof(*params), GFP_KERNEL);
3643 oparams = memdup_user(_oparams, sizeof(*oparams));
3644 if (IS_ERR(oparams)) {
3645 err = PTR_ERR(oparams);
3648 snd_pcm_hw_convert_from_old_params(params, oparams);
3649 err = snd_pcm_hw_params(substream, params);
3650 snd_pcm_hw_convert_to_old_params(oparams, params);
3651 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3661 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3664 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3667 unsigned long pgoff,
3668 unsigned long flags)
3670 struct snd_pcm_file *pcm_file = file->private_data;
3671 struct snd_pcm_substream *substream = pcm_file->substream;
3672 struct snd_pcm_runtime *runtime = substream->runtime;
3673 unsigned long offset = pgoff << PAGE_SHIFT;
3676 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3677 return (unsigned long)runtime->status;
3678 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3679 return (unsigned long)runtime->control;
3681 return (unsigned long)runtime->dma_area + offset;
3685 # define snd_pcm_get_unmapped_area NULL
3692 const struct file_operations snd_pcm_f_ops[2] = {
3694 .owner = THIS_MODULE,
3695 .write = snd_pcm_write,
3696 .write_iter = snd_pcm_writev,
3697 .open = snd_pcm_playback_open,
3698 .release = snd_pcm_release,
3699 .llseek = no_llseek,
3700 .poll = snd_pcm_playback_poll,
3701 .unlocked_ioctl = snd_pcm_playback_ioctl,
3702 .compat_ioctl = snd_pcm_ioctl_compat,
3703 .mmap = snd_pcm_mmap,
3704 .fasync = snd_pcm_fasync,
3705 .get_unmapped_area = snd_pcm_get_unmapped_area,
3708 .owner = THIS_MODULE,
3709 .read = snd_pcm_read,
3710 .read_iter = snd_pcm_readv,
3711 .open = snd_pcm_capture_open,
3712 .release = snd_pcm_release,
3713 .llseek = no_llseek,
3714 .poll = snd_pcm_capture_poll,
3715 .unlocked_ioctl = snd_pcm_capture_ioctl,
3716 .compat_ioctl = snd_pcm_ioctl_compat,
3717 .mmap = snd_pcm_mmap,
3718 .fasync = snd_pcm_fasync,
3719 .get_unmapped_area = snd_pcm_get_unmapped_area,