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/sched/signal.h>
27 #include <linux/time.h>
28 #include <linux/pm_qos.h>
30 #include <linux/dma-mapping.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/info.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/timer.h>
37 #include <sound/minors.h>
38 #include <linux/uio.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 * spin until it gets the lock.
84 static inline void down_write_nonblock(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;
217 /* AB: FIXME!!! This is definitely nonsense */
219 info->sync = runtime->sync;
220 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
225 int snd_pcm_info_user(struct snd_pcm_substream *substream,
226 struct snd_pcm_info __user * _info)
228 struct snd_pcm_info *info;
231 info = kmalloc(sizeof(*info), GFP_KERNEL);
234 err = snd_pcm_info(substream, info);
236 if (copy_to_user(_info, info, sizeof(*info)))
243 static bool hw_support_mmap(struct snd_pcm_substream *substream)
245 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
247 /* check architectures that return -EINVAL from dma_mmap_coherent() */
248 /* FIXME: this should be some global flag */
249 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
250 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
251 if (!substream->ops->mmap &&
252 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
261 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
262 static const char * const snd_pcm_hw_param_names[] = {
266 HW_PARAM(SAMPLE_BITS),
267 HW_PARAM(FRAME_BITS),
270 HW_PARAM(PERIOD_TIME),
271 HW_PARAM(PERIOD_SIZE),
272 HW_PARAM(PERIOD_BYTES),
274 HW_PARAM(BUFFER_TIME),
275 HW_PARAM(BUFFER_SIZE),
276 HW_PARAM(BUFFER_BYTES),
281 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
282 struct snd_pcm_hw_params *params)
285 struct snd_pcm_hardware *hw;
286 struct snd_interval *i = NULL;
287 struct snd_mask *m = NULL;
288 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
289 unsigned int rstamps[constrs->rules_num];
290 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
291 unsigned int stamp = 2;
295 params->fifo_size = 0;
296 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
298 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
299 params->rate_num = 0;
300 params->rate_den = 0;
303 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
304 m = hw_param_mask(params, k);
305 if (snd_mask_empty(m))
307 if (!(params->rmask & (1 << k)))
310 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
311 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
313 changed = snd_mask_refine(m, constrs_mask(constrs, k));
315 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
318 params->cmask |= 1 << k;
323 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
324 i = hw_param_interval(params, k);
325 if (snd_interval_empty(i))
327 if (!(params->rmask & (1 << k)))
330 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
335 i->openmin ? '(' : '[', i->min,
336 i->max, i->openmax ? ')' : ']');
339 changed = snd_interval_refine(i, constrs_interval(constrs, k));
344 pr_cont("%c%u %u%c\n",
345 i->openmin ? '(' : '[', i->min,
346 i->max, i->openmax ? ')' : ']');
349 params->cmask |= 1 << k;
354 for (k = 0; k < constrs->rules_num; k++)
356 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
357 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
360 for (k = 0; k < constrs->rules_num; k++) {
361 struct snd_pcm_hw_rule *r = &constrs->rules[k];
364 if (r->cond && !(r->cond & params->flags))
366 for (d = 0; r->deps[d] >= 0; d++) {
367 if (vstamps[r->deps[d]] > rstamps[k]) {
375 pr_debug("Rule %d [%p]: ", k, r->func);
377 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
378 if (hw_is_mask(r->var)) {
379 m = hw_param_mask(params, r->var);
380 pr_cont("%x", *m->bits);
382 i = hw_param_interval(params, r->var);
387 i->openmin ? '(' : '[', i->min,
388 i->max, i->openmax ? ')' : ']');
392 changed = r->func(params, r);
396 if (hw_is_mask(r->var))
397 pr_cont("%x", *m->bits);
403 i->openmin ? '(' : '[', i->min,
404 i->max, i->openmax ? ')' : ']');
410 if (changed && r->var >= 0) {
411 params->cmask |= (1 << r->var);
412 vstamps[r->var] = stamp;
420 if (!params->msbits) {
421 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
422 if (snd_interval_single(i))
423 params->msbits = snd_interval_value(i);
426 if (!params->rate_den) {
427 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
428 if (snd_interval_single(i)) {
429 params->rate_num = snd_interval_value(i);
430 params->rate_den = 1;
434 hw = &substream->runtime->hw;
436 params->info = hw->info & ~(SNDRV_PCM_INFO_FIFO_IN_FRAMES |
437 SNDRV_PCM_INFO_DRAIN_TRIGGER);
438 if (!hw_support_mmap(substream))
439 params->info &= ~(SNDRV_PCM_INFO_MMAP |
440 SNDRV_PCM_INFO_MMAP_VALID);
442 if (!params->fifo_size) {
443 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
444 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
445 if (snd_mask_min(m) == snd_mask_max(m) &&
446 snd_interval_min(i) == snd_interval_max(i)) {
447 changed = substream->ops->ioctl(substream,
448 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
457 EXPORT_SYMBOL(snd_pcm_hw_refine);
459 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
460 struct snd_pcm_hw_params __user * _params)
462 struct snd_pcm_hw_params *params;
465 params = memdup_user(_params, sizeof(*params));
467 return PTR_ERR(params);
469 err = snd_pcm_hw_refine(substream, params);
470 if (copy_to_user(_params, params, sizeof(*params))) {
479 static int period_to_usecs(struct snd_pcm_runtime *runtime)
484 return -1; /* invalid */
486 /* take 75% of period time as the deadline */
487 usecs = (750000 / runtime->rate) * runtime->period_size;
488 usecs += ((750000 % runtime->rate) * runtime->period_size) /
494 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
496 snd_pcm_stream_lock_irq(substream);
497 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
498 substream->runtime->status->state = state;
499 snd_pcm_stream_unlock_irq(substream);
502 static inline void snd_pcm_timer_notify(struct snd_pcm_substream *substream,
505 #ifdef CONFIG_SND_PCM_TIMER
506 if (substream->timer)
507 snd_timer_notify(substream->timer, event,
508 &substream->runtime->trigger_tstamp);
512 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
513 struct snd_pcm_hw_params *params)
515 struct snd_pcm_runtime *runtime;
518 snd_pcm_uframes_t frames;
520 if (PCM_RUNTIME_CHECK(substream))
522 runtime = substream->runtime;
523 snd_pcm_stream_lock_irq(substream);
524 switch (runtime->status->state) {
525 case SNDRV_PCM_STATE_OPEN:
526 case SNDRV_PCM_STATE_SETUP:
527 case SNDRV_PCM_STATE_PREPARED:
530 snd_pcm_stream_unlock_irq(substream);
533 snd_pcm_stream_unlock_irq(substream);
534 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
535 if (!substream->oss.oss)
537 if (atomic_read(&substream->mmap_count))
541 err = snd_pcm_hw_refine(substream, params);
545 err = snd_pcm_hw_params_choose(substream, params);
549 if (substream->ops->hw_params != NULL) {
550 err = substream->ops->hw_params(substream, params);
555 runtime->access = params_access(params);
556 runtime->format = params_format(params);
557 runtime->subformat = params_subformat(params);
558 runtime->channels = params_channels(params);
559 runtime->rate = params_rate(params);
560 runtime->period_size = params_period_size(params);
561 runtime->periods = params_periods(params);
562 runtime->buffer_size = params_buffer_size(params);
563 runtime->info = params->info;
564 runtime->rate_num = params->rate_num;
565 runtime->rate_den = params->rate_den;
566 runtime->no_period_wakeup =
567 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
568 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
570 bits = snd_pcm_format_physical_width(runtime->format);
571 runtime->sample_bits = bits;
572 bits *= runtime->channels;
573 runtime->frame_bits = bits;
575 while (bits % 8 != 0) {
579 runtime->byte_align = bits / 8;
580 runtime->min_align = frames;
582 /* Default sw params */
583 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
584 runtime->period_step = 1;
585 runtime->control->avail_min = runtime->period_size;
586 runtime->start_threshold = 1;
587 runtime->stop_threshold = runtime->buffer_size;
588 runtime->silence_threshold = 0;
589 runtime->silence_size = 0;
590 runtime->boundary = runtime->buffer_size;
591 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
592 runtime->boundary *= 2;
594 snd_pcm_timer_resolution_change(substream);
595 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
597 if (pm_qos_request_active(&substream->latency_pm_qos_req))
598 pm_qos_remove_request(&substream->latency_pm_qos_req);
599 if ((usecs = period_to_usecs(runtime)) >= 0)
600 pm_qos_add_request(&substream->latency_pm_qos_req,
601 PM_QOS_CPU_DMA_LATENCY, usecs);
604 /* hardware might be unusable from this time,
605 so we force application to retry to set
606 the correct hardware parameter settings */
607 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
608 if (substream->ops->hw_free != NULL)
609 substream->ops->hw_free(substream);
613 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
614 struct snd_pcm_hw_params __user * _params)
616 struct snd_pcm_hw_params *params;
619 params = memdup_user(_params, sizeof(*params));
621 return PTR_ERR(params);
623 err = snd_pcm_hw_params(substream, params);
624 if (copy_to_user(_params, params, sizeof(*params))) {
633 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
635 struct snd_pcm_runtime *runtime;
638 if (PCM_RUNTIME_CHECK(substream))
640 runtime = substream->runtime;
641 snd_pcm_stream_lock_irq(substream);
642 switch (runtime->status->state) {
643 case SNDRV_PCM_STATE_SETUP:
644 case SNDRV_PCM_STATE_PREPARED:
647 snd_pcm_stream_unlock_irq(substream);
650 snd_pcm_stream_unlock_irq(substream);
651 if (atomic_read(&substream->mmap_count))
653 if (substream->ops->hw_free)
654 result = substream->ops->hw_free(substream);
655 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
656 pm_qos_remove_request(&substream->latency_pm_qos_req);
660 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
661 struct snd_pcm_sw_params *params)
663 struct snd_pcm_runtime *runtime;
666 if (PCM_RUNTIME_CHECK(substream))
668 runtime = substream->runtime;
669 snd_pcm_stream_lock_irq(substream);
670 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
671 snd_pcm_stream_unlock_irq(substream);
674 snd_pcm_stream_unlock_irq(substream);
676 if (params->tstamp_mode < 0 ||
677 params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
679 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
680 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
682 if (params->avail_min == 0)
684 if (params->silence_size >= runtime->boundary) {
685 if (params->silence_threshold != 0)
688 if (params->silence_size > params->silence_threshold)
690 if (params->silence_threshold > runtime->buffer_size)
694 snd_pcm_stream_lock_irq(substream);
695 runtime->tstamp_mode = params->tstamp_mode;
696 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
697 runtime->tstamp_type = params->tstamp_type;
698 runtime->period_step = params->period_step;
699 runtime->control->avail_min = params->avail_min;
700 runtime->start_threshold = params->start_threshold;
701 runtime->stop_threshold = params->stop_threshold;
702 runtime->silence_threshold = params->silence_threshold;
703 runtime->silence_size = params->silence_size;
704 params->boundary = runtime->boundary;
705 if (snd_pcm_running(substream)) {
706 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
707 runtime->silence_size > 0)
708 snd_pcm_playback_silence(substream, ULONG_MAX);
709 err = snd_pcm_update_state(substream, runtime);
711 snd_pcm_stream_unlock_irq(substream);
715 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
716 struct snd_pcm_sw_params __user * _params)
718 struct snd_pcm_sw_params params;
720 if (copy_from_user(¶ms, _params, sizeof(params)))
722 err = snd_pcm_sw_params(substream, ¶ms);
723 if (copy_to_user(_params, ¶ms, sizeof(params)))
728 int snd_pcm_status(struct snd_pcm_substream *substream,
729 struct snd_pcm_status *status)
731 struct snd_pcm_runtime *runtime = substream->runtime;
733 snd_pcm_stream_lock_irq(substream);
735 snd_pcm_unpack_audio_tstamp_config(status->audio_tstamp_data,
736 &runtime->audio_tstamp_config);
738 /* backwards compatible behavior */
739 if (runtime->audio_tstamp_config.type_requested ==
740 SNDRV_PCM_AUDIO_TSTAMP_TYPE_COMPAT) {
741 if (runtime->hw.info & SNDRV_PCM_INFO_HAS_WALL_CLOCK)
742 runtime->audio_tstamp_config.type_requested =
743 SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
745 runtime->audio_tstamp_config.type_requested =
746 SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
747 runtime->audio_tstamp_report.valid = 0;
749 runtime->audio_tstamp_report.valid = 1;
751 status->state = runtime->status->state;
752 status->suspended_state = runtime->status->suspended_state;
753 if (status->state == SNDRV_PCM_STATE_OPEN)
755 status->trigger_tstamp = runtime->trigger_tstamp;
756 if (snd_pcm_running(substream)) {
757 snd_pcm_update_hw_ptr(substream);
758 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
759 status->tstamp = runtime->status->tstamp;
760 status->driver_tstamp = runtime->driver_tstamp;
761 status->audio_tstamp =
762 runtime->status->audio_tstamp;
763 if (runtime->audio_tstamp_report.valid == 1)
764 /* backwards compatibility, no report provided in COMPAT mode */
765 snd_pcm_pack_audio_tstamp_report(&status->audio_tstamp_data,
766 &status->audio_tstamp_accuracy,
767 &runtime->audio_tstamp_report);
772 /* get tstamp only in fallback mode and only if enabled */
773 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
774 snd_pcm_gettime(runtime, &status->tstamp);
777 status->appl_ptr = runtime->control->appl_ptr;
778 status->hw_ptr = runtime->status->hw_ptr;
779 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
780 status->avail = snd_pcm_playback_avail(runtime);
781 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
782 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
783 status->delay = runtime->buffer_size - status->avail;
784 status->delay += runtime->delay;
788 status->avail = snd_pcm_capture_avail(runtime);
789 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
790 status->delay = status->avail + runtime->delay;
794 status->avail_max = runtime->avail_max;
795 status->overrange = runtime->overrange;
796 runtime->avail_max = 0;
797 runtime->overrange = 0;
799 snd_pcm_stream_unlock_irq(substream);
803 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
804 struct snd_pcm_status __user * _status,
807 struct snd_pcm_status status;
810 memset(&status, 0, sizeof(status));
812 * with extension, parameters are read/write,
813 * get audio_tstamp_data from user,
814 * ignore rest of status structure
816 if (ext && get_user(status.audio_tstamp_data,
817 (u32 __user *)(&_status->audio_tstamp_data)))
819 res = snd_pcm_status(substream, &status);
822 if (copy_to_user(_status, &status, sizeof(status)))
827 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
828 struct snd_pcm_channel_info * info)
830 struct snd_pcm_runtime *runtime;
831 unsigned int channel;
833 channel = info->channel;
834 runtime = substream->runtime;
835 snd_pcm_stream_lock_irq(substream);
836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
837 snd_pcm_stream_unlock_irq(substream);
840 snd_pcm_stream_unlock_irq(substream);
841 if (channel >= runtime->channels)
843 memset(info, 0, sizeof(*info));
844 info->channel = channel;
845 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
848 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
849 struct snd_pcm_channel_info __user * _info)
851 struct snd_pcm_channel_info info;
854 if (copy_from_user(&info, _info, sizeof(info)))
856 res = snd_pcm_channel_info(substream, &info);
859 if (copy_to_user(_info, &info, sizeof(info)))
864 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
866 struct snd_pcm_runtime *runtime = substream->runtime;
867 if (runtime->trigger_master == NULL)
869 if (runtime->trigger_master == substream) {
870 if (!runtime->trigger_tstamp_latched)
871 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
873 snd_pcm_trigger_tstamp(runtime->trigger_master);
874 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
876 runtime->trigger_master = NULL;
880 int (*pre_action)(struct snd_pcm_substream *substream, int state);
881 int (*do_action)(struct snd_pcm_substream *substream, int state);
882 void (*undo_action)(struct snd_pcm_substream *substream, int state);
883 void (*post_action)(struct snd_pcm_substream *substream, int state);
887 * this functions is core for handling of linked stream
888 * Note: the stream state might be changed also on failure
889 * Note2: call with calling stream lock + link lock
891 static int snd_pcm_action_group(const struct action_ops *ops,
892 struct snd_pcm_substream *substream,
893 int state, int do_lock)
895 struct snd_pcm_substream *s = NULL;
896 struct snd_pcm_substream *s1;
897 int res = 0, depth = 1;
899 snd_pcm_group_for_each_entry(s, substream) {
900 if (do_lock && s != substream) {
901 if (s->pcm->nonatomic)
902 mutex_lock_nested(&s->self_group.mutex, depth);
904 spin_lock_nested(&s->self_group.lock, depth);
907 res = ops->pre_action(s, state);
911 snd_pcm_group_for_each_entry(s, substream) {
912 res = ops->do_action(s, state);
914 if (ops->undo_action) {
915 snd_pcm_group_for_each_entry(s1, substream) {
916 if (s1 == s) /* failed stream */
918 ops->undo_action(s1, state);
921 s = NULL; /* unlock all */
925 snd_pcm_group_for_each_entry(s, substream) {
926 ops->post_action(s, state);
931 snd_pcm_group_for_each_entry(s1, substream) {
932 if (s1 != substream) {
933 if (s1->pcm->nonatomic)
934 mutex_unlock(&s1->self_group.mutex);
936 spin_unlock(&s1->self_group.lock);
938 if (s1 == s) /* end */
946 * Note: call with stream lock
948 static int snd_pcm_action_single(const struct action_ops *ops,
949 struct snd_pcm_substream *substream,
954 res = ops->pre_action(substream, state);
957 res = ops->do_action(substream, state);
959 ops->post_action(substream, state);
960 else if (ops->undo_action)
961 ops->undo_action(substream, state);
966 * Note: call with stream lock
968 static int snd_pcm_action(const struct action_ops *ops,
969 struct snd_pcm_substream *substream,
974 if (!snd_pcm_stream_linked(substream))
975 return snd_pcm_action_single(ops, substream, state);
977 if (substream->pcm->nonatomic) {
978 if (!mutex_trylock(&substream->group->mutex)) {
979 mutex_unlock(&substream->self_group.mutex);
980 mutex_lock(&substream->group->mutex);
981 mutex_lock(&substream->self_group.mutex);
983 res = snd_pcm_action_group(ops, substream, state, 1);
984 mutex_unlock(&substream->group->mutex);
986 if (!spin_trylock(&substream->group->lock)) {
987 spin_unlock(&substream->self_group.lock);
988 spin_lock(&substream->group->lock);
989 spin_lock(&substream->self_group.lock);
991 res = snd_pcm_action_group(ops, substream, state, 1);
992 spin_unlock(&substream->group->lock);
998 * Note: don't use any locks before
1000 static int snd_pcm_action_lock_irq(const struct action_ops *ops,
1001 struct snd_pcm_substream *substream,
1006 snd_pcm_stream_lock_irq(substream);
1007 res = snd_pcm_action(ops, substream, state);
1008 snd_pcm_stream_unlock_irq(substream);
1014 static int snd_pcm_action_nonatomic(const struct action_ops *ops,
1015 struct snd_pcm_substream *substream,
1020 down_read(&snd_pcm_link_rwsem);
1021 if (snd_pcm_stream_linked(substream))
1022 res = snd_pcm_action_group(ops, substream, state, 0);
1024 res = snd_pcm_action_single(ops, substream, state);
1025 up_read(&snd_pcm_link_rwsem);
1032 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1034 struct snd_pcm_runtime *runtime = substream->runtime;
1035 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
1037 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1038 !snd_pcm_playback_data(substream))
1040 runtime->trigger_tstamp_latched = false;
1041 runtime->trigger_master = substream;
1045 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1047 if (substream->runtime->trigger_master != substream)
1049 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1052 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1054 if (substream->runtime->trigger_master == substream)
1055 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1058 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1060 struct snd_pcm_runtime *runtime = substream->runtime;
1061 snd_pcm_trigger_tstamp(substream);
1062 runtime->hw_ptr_jiffies = jiffies;
1063 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1065 runtime->status->state = state;
1066 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1067 runtime->silence_size > 0)
1068 snd_pcm_playback_silence(substream, ULONG_MAX);
1069 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTART);
1072 static const struct action_ops snd_pcm_action_start = {
1073 .pre_action = snd_pcm_pre_start,
1074 .do_action = snd_pcm_do_start,
1075 .undo_action = snd_pcm_undo_start,
1076 .post_action = snd_pcm_post_start
1080 * snd_pcm_start - start all linked streams
1081 * @substream: the PCM substream instance
1083 * Return: Zero if successful, or a negative error code.
1085 int snd_pcm_start(struct snd_pcm_substream *substream)
1087 return snd_pcm_action(&snd_pcm_action_start, substream,
1088 SNDRV_PCM_STATE_RUNNING);
1094 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1096 struct snd_pcm_runtime *runtime = substream->runtime;
1097 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1099 runtime->trigger_master = substream;
1103 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1105 if (substream->runtime->trigger_master == substream &&
1106 snd_pcm_running(substream))
1107 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1108 return 0; /* unconditonally stop all substreams */
1111 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1113 struct snd_pcm_runtime *runtime = substream->runtime;
1114 if (runtime->status->state != state) {
1115 snd_pcm_trigger_tstamp(substream);
1116 runtime->status->state = state;
1117 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSTOP);
1119 wake_up(&runtime->sleep);
1120 wake_up(&runtime->tsleep);
1123 static const struct action_ops snd_pcm_action_stop = {
1124 .pre_action = snd_pcm_pre_stop,
1125 .do_action = snd_pcm_do_stop,
1126 .post_action = snd_pcm_post_stop
1130 * snd_pcm_stop - try to stop all running streams in the substream group
1131 * @substream: the PCM substream instance
1132 * @state: PCM state after stopping the stream
1134 * The state of each stream is then changed to the given state unconditionally.
1136 * Return: Zero if successful, or a negative error code.
1138 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1140 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1143 EXPORT_SYMBOL(snd_pcm_stop);
1146 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1147 * @substream: the PCM substream
1149 * After stopping, the state is changed to SETUP.
1150 * Unlike snd_pcm_stop(), this affects only the given stream.
1152 * Return: Zero if succesful, or a negative error code.
1154 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1156 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1157 SNDRV_PCM_STATE_SETUP);
1161 * snd_pcm_stop_xrun - stop the running streams as XRUN
1162 * @substream: the PCM substream instance
1164 * This stops the given running substream (and all linked substreams) as XRUN.
1165 * Unlike snd_pcm_stop(), this function takes the substream lock by itself.
1167 * Return: Zero if successful, or a negative error code.
1169 int snd_pcm_stop_xrun(struct snd_pcm_substream *substream)
1171 unsigned long flags;
1174 snd_pcm_stream_lock_irqsave(substream, flags);
1175 if (snd_pcm_running(substream))
1176 ret = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1177 snd_pcm_stream_unlock_irqrestore(substream, flags);
1180 EXPORT_SYMBOL_GPL(snd_pcm_stop_xrun);
1185 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1187 struct snd_pcm_runtime *runtime = substream->runtime;
1188 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1191 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1193 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1195 runtime->trigger_master = substream;
1199 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1201 if (substream->runtime->trigger_master != substream)
1203 /* some drivers might use hw_ptr to recover from the pause -
1204 update the hw_ptr now */
1206 snd_pcm_update_hw_ptr(substream);
1207 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1208 * a delta between the current jiffies, this gives a large enough
1209 * delta, effectively to skip the check once.
1211 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1212 return substream->ops->trigger(substream,
1213 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1214 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1217 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1219 if (substream->runtime->trigger_master == substream)
1220 substream->ops->trigger(substream,
1221 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1222 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1225 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1227 struct snd_pcm_runtime *runtime = substream->runtime;
1228 snd_pcm_trigger_tstamp(substream);
1230 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1231 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MPAUSE);
1232 wake_up(&runtime->sleep);
1233 wake_up(&runtime->tsleep);
1235 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1236 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MCONTINUE);
1240 static const struct action_ops snd_pcm_action_pause = {
1241 .pre_action = snd_pcm_pre_pause,
1242 .do_action = snd_pcm_do_pause,
1243 .undo_action = snd_pcm_undo_pause,
1244 .post_action = snd_pcm_post_pause
1248 * Push/release the pause for all linked streams.
1250 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1252 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1258 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1260 struct snd_pcm_runtime *runtime = substream->runtime;
1261 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1263 runtime->trigger_master = substream;
1267 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1269 struct snd_pcm_runtime *runtime = substream->runtime;
1270 if (runtime->trigger_master != substream)
1272 if (! snd_pcm_running(substream))
1274 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1275 return 0; /* suspend unconditionally */
1278 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1280 struct snd_pcm_runtime *runtime = substream->runtime;
1281 snd_pcm_trigger_tstamp(substream);
1282 runtime->status->suspended_state = runtime->status->state;
1283 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1284 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MSUSPEND);
1285 wake_up(&runtime->sleep);
1286 wake_up(&runtime->tsleep);
1289 static const struct action_ops snd_pcm_action_suspend = {
1290 .pre_action = snd_pcm_pre_suspend,
1291 .do_action = snd_pcm_do_suspend,
1292 .post_action = snd_pcm_post_suspend
1296 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1297 * @substream: the PCM substream
1299 * After this call, all streams are changed to SUSPENDED state.
1301 * Return: Zero if successful (or @substream is %NULL), or a negative error
1304 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1307 unsigned long flags;
1312 snd_pcm_stream_lock_irqsave(substream, flags);
1313 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1314 snd_pcm_stream_unlock_irqrestore(substream, flags);
1318 EXPORT_SYMBOL(snd_pcm_suspend);
1321 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1322 * @pcm: the PCM instance
1324 * After this call, all streams are changed to SUSPENDED state.
1326 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1328 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1330 struct snd_pcm_substream *substream;
1331 int stream, err = 0;
1336 for (stream = 0; stream < 2; stream++) {
1337 for (substream = pcm->streams[stream].substream;
1338 substream; substream = substream->next) {
1339 /* FIXME: the open/close code should lock this as well */
1340 if (substream->runtime == NULL)
1342 err = snd_pcm_suspend(substream);
1343 if (err < 0 && err != -EBUSY)
1350 EXPORT_SYMBOL(snd_pcm_suspend_all);
1354 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1356 struct snd_pcm_runtime *runtime = substream->runtime;
1357 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1359 runtime->trigger_master = substream;
1363 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1365 struct snd_pcm_runtime *runtime = substream->runtime;
1366 if (runtime->trigger_master != substream)
1368 /* DMA not running previously? */
1369 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1370 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1371 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1373 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1376 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1378 if (substream->runtime->trigger_master == substream &&
1379 snd_pcm_running(substream))
1380 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1383 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1385 struct snd_pcm_runtime *runtime = substream->runtime;
1386 snd_pcm_trigger_tstamp(substream);
1387 runtime->status->state = runtime->status->suspended_state;
1388 snd_pcm_timer_notify(substream, SNDRV_TIMER_EVENT_MRESUME);
1391 static const struct action_ops snd_pcm_action_resume = {
1392 .pre_action = snd_pcm_pre_resume,
1393 .do_action = snd_pcm_do_resume,
1394 .undo_action = snd_pcm_undo_resume,
1395 .post_action = snd_pcm_post_resume
1398 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1400 struct snd_card *card = substream->pcm->card;
1403 snd_power_lock(card);
1404 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1405 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1406 snd_power_unlock(card);
1412 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1417 #endif /* CONFIG_PM */
1422 * Change the RUNNING stream(s) to XRUN state.
1424 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1426 struct snd_card *card = substream->pcm->card;
1427 struct snd_pcm_runtime *runtime = substream->runtime;
1430 snd_power_lock(card);
1431 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1432 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1437 snd_pcm_stream_lock_irq(substream);
1438 switch (runtime->status->state) {
1439 case SNDRV_PCM_STATE_XRUN:
1440 result = 0; /* already there */
1442 case SNDRV_PCM_STATE_RUNNING:
1443 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1448 snd_pcm_stream_unlock_irq(substream);
1450 snd_power_unlock(card);
1457 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1459 struct snd_pcm_runtime *runtime = substream->runtime;
1460 switch (runtime->status->state) {
1461 case SNDRV_PCM_STATE_RUNNING:
1462 case SNDRV_PCM_STATE_PREPARED:
1463 case SNDRV_PCM_STATE_PAUSED:
1464 case SNDRV_PCM_STATE_SUSPENDED:
1471 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1473 struct snd_pcm_runtime *runtime = substream->runtime;
1474 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1477 runtime->hw_ptr_base = 0;
1478 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1479 runtime->status->hw_ptr % runtime->period_size;
1480 runtime->silence_start = runtime->status->hw_ptr;
1481 runtime->silence_filled = 0;
1485 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1487 struct snd_pcm_runtime *runtime = substream->runtime;
1488 runtime->control->appl_ptr = runtime->status->hw_ptr;
1489 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1490 runtime->silence_size > 0)
1491 snd_pcm_playback_silence(substream, ULONG_MAX);
1494 static const struct action_ops snd_pcm_action_reset = {
1495 .pre_action = snd_pcm_pre_reset,
1496 .do_action = snd_pcm_do_reset,
1497 .post_action = snd_pcm_post_reset
1500 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1502 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1508 /* we use the second argument for updating f_flags */
1509 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1512 struct snd_pcm_runtime *runtime = substream->runtime;
1513 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1514 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1516 if (snd_pcm_running(substream))
1518 substream->f_flags = f_flags;
1522 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1525 err = substream->ops->prepare(substream);
1528 return snd_pcm_do_reset(substream, 0);
1531 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1533 struct snd_pcm_runtime *runtime = substream->runtime;
1534 runtime->control->appl_ptr = runtime->status->hw_ptr;
1535 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1538 static const struct action_ops snd_pcm_action_prepare = {
1539 .pre_action = snd_pcm_pre_prepare,
1540 .do_action = snd_pcm_do_prepare,
1541 .post_action = snd_pcm_post_prepare
1545 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1546 * @substream: the PCM substream instance
1547 * @file: file to refer f_flags
1549 * Return: Zero if successful, or a negative error code.
1551 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1555 struct snd_card *card = substream->pcm->card;
1559 f_flags = file->f_flags;
1561 f_flags = substream->f_flags;
1563 snd_power_lock(card);
1564 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1565 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1566 substream, f_flags);
1567 snd_power_unlock(card);
1575 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1577 struct snd_pcm_runtime *runtime = substream->runtime;
1578 switch (runtime->status->state) {
1579 case SNDRV_PCM_STATE_OPEN:
1580 case SNDRV_PCM_STATE_DISCONNECTED:
1581 case SNDRV_PCM_STATE_SUSPENDED:
1584 runtime->trigger_master = substream;
1588 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1590 struct snd_pcm_runtime *runtime = substream->runtime;
1591 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1592 switch (runtime->status->state) {
1593 case SNDRV_PCM_STATE_PREPARED:
1594 /* start playback stream if possible */
1595 if (! snd_pcm_playback_empty(substream)) {
1596 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1597 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1599 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1602 case SNDRV_PCM_STATE_RUNNING:
1603 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1605 case SNDRV_PCM_STATE_XRUN:
1606 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1612 /* stop running stream */
1613 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1614 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1615 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1616 snd_pcm_do_stop(substream, new_state);
1617 snd_pcm_post_stop(substream, new_state);
1621 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
1622 runtime->trigger_master == substream &&
1623 (runtime->hw.info & SNDRV_PCM_INFO_DRAIN_TRIGGER))
1624 return substream->ops->trigger(substream,
1625 SNDRV_PCM_TRIGGER_DRAIN);
1630 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1634 static const struct action_ops snd_pcm_action_drain_init = {
1635 .pre_action = snd_pcm_pre_drain_init,
1636 .do_action = snd_pcm_do_drain_init,
1637 .post_action = snd_pcm_post_drain_init
1640 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1643 * Drain the stream(s).
1644 * When the substream is linked, sync until the draining of all playback streams
1646 * After this call, all streams are supposed to be either SETUP or DRAINING
1647 * (capture only) state.
1649 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1652 struct snd_card *card;
1653 struct snd_pcm_runtime *runtime;
1654 struct snd_pcm_substream *s;
1659 card = substream->pcm->card;
1660 runtime = substream->runtime;
1662 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1665 snd_power_lock(card);
1666 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1667 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1669 snd_power_unlock(card);
1675 if (file->f_flags & O_NONBLOCK)
1677 } else if (substream->f_flags & O_NONBLOCK)
1680 down_read(&snd_pcm_link_rwsem);
1681 snd_pcm_stream_lock_irq(substream);
1683 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1684 snd_pcm_pause(substream, 0);
1686 /* pre-start/stop - all running streams are changed to DRAINING state */
1687 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1690 /* in non-blocking, we don't wait in ioctl but let caller poll */
1698 struct snd_pcm_runtime *to_check;
1699 if (signal_pending(current)) {
1700 result = -ERESTARTSYS;
1703 /* find a substream to drain */
1705 snd_pcm_group_for_each_entry(s, substream) {
1706 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1708 runtime = s->runtime;
1709 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1715 break; /* all drained */
1716 init_waitqueue_entry(&wait, current);
1717 add_wait_queue(&to_check->sleep, &wait);
1718 snd_pcm_stream_unlock_irq(substream);
1719 up_read(&snd_pcm_link_rwsem);
1720 snd_power_unlock(card);
1721 if (runtime->no_period_wakeup)
1722 tout = MAX_SCHEDULE_TIMEOUT;
1725 if (runtime->rate) {
1726 long t = runtime->period_size * 2 / runtime->rate;
1727 tout = max(t, tout);
1729 tout = msecs_to_jiffies(tout * 1000);
1731 tout = schedule_timeout_interruptible(tout);
1732 snd_power_lock(card);
1733 down_read(&snd_pcm_link_rwsem);
1734 snd_pcm_stream_lock_irq(substream);
1735 remove_wait_queue(&to_check->sleep, &wait);
1736 if (card->shutdown) {
1741 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1744 dev_dbg(substream->pcm->card->dev,
1745 "playback drain error (DMA or IRQ trouble?)\n");
1746 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1754 snd_pcm_stream_unlock_irq(substream);
1755 up_read(&snd_pcm_link_rwsem);
1756 snd_power_unlock(card);
1764 * Immediately put all linked substreams into SETUP state.
1766 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1768 struct snd_pcm_runtime *runtime;
1771 if (PCM_RUNTIME_CHECK(substream))
1773 runtime = substream->runtime;
1775 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1776 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1777 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1780 snd_pcm_stream_lock_irq(substream);
1782 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1783 snd_pcm_pause(substream, 0);
1785 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1786 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1787 snd_pcm_stream_unlock_irq(substream);
1793 static bool is_pcm_file(struct file *file)
1795 struct inode *inode = file_inode(file);
1798 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1800 minor = iminor(inode);
1801 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1802 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1808 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1811 struct snd_pcm_file *pcm_file;
1812 struct snd_pcm_substream *substream1;
1813 struct snd_pcm_group *group;
1814 struct fd f = fdget(fd);
1818 if (!is_pcm_file(f.file)) {
1822 pcm_file = f.file->private_data;
1823 substream1 = pcm_file->substream;
1824 group = kmalloc(sizeof(*group), GFP_KERNEL);
1829 down_write_nonblock(&snd_pcm_link_rwsem);
1830 write_lock_irq(&snd_pcm_link_rwlock);
1831 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1832 substream->runtime->status->state != substream1->runtime->status->state ||
1833 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1837 if (snd_pcm_stream_linked(substream1)) {
1841 if (!snd_pcm_stream_linked(substream)) {
1842 substream->group = group;
1844 spin_lock_init(&substream->group->lock);
1845 mutex_init(&substream->group->mutex);
1846 INIT_LIST_HEAD(&substream->group->substreams);
1847 list_add_tail(&substream->link_list, &substream->group->substreams);
1848 substream->group->count = 1;
1850 list_add_tail(&substream1->link_list, &substream->group->substreams);
1851 substream->group->count++;
1852 substream1->group = substream->group;
1854 write_unlock_irq(&snd_pcm_link_rwlock);
1855 up_write(&snd_pcm_link_rwsem);
1857 snd_card_unref(substream1->pcm->card);
1864 static void relink_to_local(struct snd_pcm_substream *substream)
1866 substream->group = &substream->self_group;
1867 INIT_LIST_HEAD(&substream->self_group.substreams);
1868 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1871 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1873 struct snd_pcm_substream *s;
1876 down_write_nonblock(&snd_pcm_link_rwsem);
1877 write_lock_irq(&snd_pcm_link_rwlock);
1878 if (!snd_pcm_stream_linked(substream)) {
1882 list_del(&substream->link_list);
1883 substream->group->count--;
1884 if (substream->group->count == 1) { /* detach the last stream, too */
1885 snd_pcm_group_for_each_entry(s, substream) {
1889 kfree(substream->group);
1891 relink_to_local(substream);
1893 write_unlock_irq(&snd_pcm_link_rwlock);
1894 up_write(&snd_pcm_link_rwsem);
1901 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1902 struct snd_pcm_hw_rule *rule)
1904 struct snd_interval t;
1905 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1906 hw_param_interval_c(params, rule->deps[1]), &t);
1907 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1910 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1911 struct snd_pcm_hw_rule *rule)
1913 struct snd_interval t;
1914 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1915 hw_param_interval_c(params, rule->deps[1]), &t);
1916 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1919 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1920 struct snd_pcm_hw_rule *rule)
1922 struct snd_interval t;
1923 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1924 hw_param_interval_c(params, rule->deps[1]),
1925 (unsigned long) rule->private, &t);
1926 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1929 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1930 struct snd_pcm_hw_rule *rule)
1932 struct snd_interval t;
1933 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1934 (unsigned long) rule->private,
1935 hw_param_interval_c(params, rule->deps[1]), &t);
1936 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1939 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1940 struct snd_pcm_hw_rule *rule)
1943 const struct snd_interval *i =
1944 hw_param_interval_c(params, rule->deps[0]);
1946 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1948 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1950 if (! snd_mask_test(mask, k))
1952 bits = snd_pcm_format_physical_width(k);
1954 continue; /* ignore invalid formats */
1955 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1956 snd_mask_reset(&m, k);
1958 return snd_mask_refine(mask, &m);
1961 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1962 struct snd_pcm_hw_rule *rule)
1964 struct snd_interval t;
1970 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1972 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1974 bits = snd_pcm_format_physical_width(k);
1976 continue; /* ignore invalid formats */
1977 if (t.min > (unsigned)bits)
1979 if (t.max < (unsigned)bits)
1983 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1986 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1987 #error "Change this table"
1990 static const unsigned int rates[] = {
1991 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1992 48000, 64000, 88200, 96000, 176400, 192000
1995 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1996 .count = ARRAY_SIZE(rates),
2000 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
2001 struct snd_pcm_hw_rule *rule)
2003 struct snd_pcm_hardware *hw = rule->private;
2004 return snd_interval_list(hw_param_interval(params, rule->var),
2005 snd_pcm_known_rates.count,
2006 snd_pcm_known_rates.list, hw->rates);
2009 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
2010 struct snd_pcm_hw_rule *rule)
2012 struct snd_interval t;
2013 struct snd_pcm_substream *substream = rule->private;
2015 t.max = substream->buffer_bytes_max;
2019 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
2022 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
2024 struct snd_pcm_runtime *runtime = substream->runtime;
2025 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
2028 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
2029 snd_mask_any(constrs_mask(constrs, k));
2032 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
2033 snd_interval_any(constrs_interval(constrs, k));
2036 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
2037 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
2038 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
2039 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
2040 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
2042 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
2043 snd_pcm_hw_rule_format, NULL,
2044 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2047 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2048 snd_pcm_hw_rule_sample_bits, NULL,
2049 SNDRV_PCM_HW_PARAM_FORMAT,
2050 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2053 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
2054 snd_pcm_hw_rule_div, NULL,
2055 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2058 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2059 snd_pcm_hw_rule_mul, NULL,
2060 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
2063 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2064 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2065 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2068 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2069 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2070 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2073 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2074 snd_pcm_hw_rule_div, NULL,
2075 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2078 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2079 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2080 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2083 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2084 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2085 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2088 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2089 snd_pcm_hw_rule_div, NULL,
2090 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2093 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2094 snd_pcm_hw_rule_div, NULL,
2095 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2098 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2099 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2100 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2103 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2104 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2105 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2108 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2109 snd_pcm_hw_rule_mul, NULL,
2110 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2113 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2114 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2115 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2118 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2119 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2120 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2123 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2124 snd_pcm_hw_rule_muldivk, (void*) 8,
2125 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2128 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2129 snd_pcm_hw_rule_muldivk, (void*) 8,
2130 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2133 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2134 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2135 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2138 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2139 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2140 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2146 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2148 struct snd_pcm_runtime *runtime = substream->runtime;
2149 struct snd_pcm_hardware *hw = &runtime->hw;
2151 unsigned int mask = 0;
2153 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2154 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2155 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2156 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2157 if (hw_support_mmap(substream)) {
2158 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2159 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2160 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2161 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2162 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2163 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2165 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2169 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2173 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2177 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2178 hw->channels_min, hw->channels_max);
2182 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2183 hw->rate_min, hw->rate_max);
2187 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2188 hw->period_bytes_min, hw->period_bytes_max);
2192 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2193 hw->periods_min, hw->periods_max);
2197 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2198 hw->period_bytes_min, hw->buffer_bytes_max);
2202 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2203 snd_pcm_hw_rule_buffer_bytes_max, substream,
2204 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2209 if (runtime->dma_bytes) {
2210 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2215 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2216 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2217 snd_pcm_hw_rule_rate, hw,
2218 SNDRV_PCM_HW_PARAM_RATE, -1);
2223 /* FIXME: this belong to lowlevel */
2224 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2229 static void pcm_release_private(struct snd_pcm_substream *substream)
2231 snd_pcm_unlink(substream);
2234 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2236 substream->ref_count--;
2237 if (substream->ref_count > 0)
2240 snd_pcm_drop(substream);
2241 if (substream->hw_opened) {
2242 if (substream->ops->hw_free &&
2243 substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
2244 substream->ops->hw_free(substream);
2245 substream->ops->close(substream);
2246 substream->hw_opened = 0;
2248 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2249 pm_qos_remove_request(&substream->latency_pm_qos_req);
2250 if (substream->pcm_release) {
2251 substream->pcm_release(substream);
2252 substream->pcm_release = NULL;
2254 snd_pcm_detach_substream(substream);
2257 EXPORT_SYMBOL(snd_pcm_release_substream);
2259 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2261 struct snd_pcm_substream **rsubstream)
2263 struct snd_pcm_substream *substream;
2266 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2269 if (substream->ref_count > 1) {
2270 *rsubstream = substream;
2274 err = snd_pcm_hw_constraints_init(substream);
2276 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2280 if ((err = substream->ops->open(substream)) < 0)
2283 substream->hw_opened = 1;
2285 err = snd_pcm_hw_constraints_complete(substream);
2287 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2291 *rsubstream = substream;
2295 snd_pcm_release_substream(substream);
2299 EXPORT_SYMBOL(snd_pcm_open_substream);
2301 static int snd_pcm_open_file(struct file *file,
2302 struct snd_pcm *pcm,
2305 struct snd_pcm_file *pcm_file;
2306 struct snd_pcm_substream *substream;
2309 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2313 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2314 if (pcm_file == NULL) {
2315 snd_pcm_release_substream(substream);
2318 pcm_file->substream = substream;
2319 if (substream->ref_count == 1) {
2320 substream->file = pcm_file;
2321 substream->pcm_release = pcm_release_private;
2323 file->private_data = pcm_file;
2328 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2330 struct snd_pcm *pcm;
2331 int err = nonseekable_open(inode, file);
2334 pcm = snd_lookup_minor_data(iminor(inode),
2335 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2336 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2338 snd_card_unref(pcm->card);
2342 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2344 struct snd_pcm *pcm;
2345 int err = nonseekable_open(inode, file);
2348 pcm = snd_lookup_minor_data(iminor(inode),
2349 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2350 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2352 snd_card_unref(pcm->card);
2356 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2365 err = snd_card_file_add(pcm->card, file);
2368 if (!try_module_get(pcm->card->module)) {
2372 init_waitqueue_entry(&wait, current);
2373 add_wait_queue(&pcm->open_wait, &wait);
2374 mutex_lock(&pcm->open_mutex);
2376 err = snd_pcm_open_file(file, pcm, stream);
2379 if (err == -EAGAIN) {
2380 if (file->f_flags & O_NONBLOCK) {
2386 set_current_state(TASK_INTERRUPTIBLE);
2387 mutex_unlock(&pcm->open_mutex);
2389 mutex_lock(&pcm->open_mutex);
2390 if (pcm->card->shutdown) {
2394 if (signal_pending(current)) {
2399 remove_wait_queue(&pcm->open_wait, &wait);
2400 mutex_unlock(&pcm->open_mutex);
2406 module_put(pcm->card->module);
2408 snd_card_file_remove(pcm->card, file);
2413 static int snd_pcm_release(struct inode *inode, struct file *file)
2415 struct snd_pcm *pcm;
2416 struct snd_pcm_substream *substream;
2417 struct snd_pcm_file *pcm_file;
2419 pcm_file = file->private_data;
2420 substream = pcm_file->substream;
2421 if (snd_BUG_ON(!substream))
2423 pcm = substream->pcm;
2424 mutex_lock(&pcm->open_mutex);
2425 snd_pcm_release_substream(substream);
2427 mutex_unlock(&pcm->open_mutex);
2428 wake_up(&pcm->open_wait);
2429 module_put(pcm->card->module);
2430 snd_card_file_remove(pcm->card, file);
2434 /* check and update PCM state; return 0 or a negative error
2435 * call this inside PCM lock
2437 static int do_pcm_hwsync(struct snd_pcm_substream *substream)
2439 switch (substream->runtime->status->state) {
2440 case SNDRV_PCM_STATE_DRAINING:
2441 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2444 case SNDRV_PCM_STATE_RUNNING:
2445 return snd_pcm_update_hw_ptr(substream);
2446 case SNDRV_PCM_STATE_PREPARED:
2447 case SNDRV_PCM_STATE_PAUSED:
2449 case SNDRV_PCM_STATE_SUSPENDED:
2451 case SNDRV_PCM_STATE_XRUN:
2458 /* increase the appl_ptr; returns the processed frames */
2459 static snd_pcm_sframes_t forward_appl_ptr(struct snd_pcm_substream *substream,
2460 snd_pcm_uframes_t frames,
2461 snd_pcm_sframes_t avail)
2463 struct snd_pcm_runtime *runtime = substream->runtime;
2464 snd_pcm_sframes_t appl_ptr;
2468 if (frames > (snd_pcm_uframes_t)avail)
2470 appl_ptr = runtime->control->appl_ptr + frames;
2471 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2472 appl_ptr -= runtime->boundary;
2473 runtime->control->appl_ptr = appl_ptr;
2477 /* decrease the appl_ptr; returns the processed frames */
2478 static snd_pcm_sframes_t rewind_appl_ptr(struct snd_pcm_substream *substream,
2479 snd_pcm_uframes_t frames,
2480 snd_pcm_sframes_t avail)
2482 struct snd_pcm_runtime *runtime = substream->runtime;
2483 snd_pcm_sframes_t appl_ptr;
2487 if (frames > (snd_pcm_uframes_t)avail)
2489 appl_ptr = runtime->control->appl_ptr - frames;
2491 appl_ptr += runtime->boundary;
2492 runtime->control->appl_ptr = appl_ptr;
2496 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2497 snd_pcm_uframes_t frames)
2499 struct snd_pcm_runtime *runtime = substream->runtime;
2500 snd_pcm_sframes_t ret;
2505 snd_pcm_stream_lock_irq(substream);
2506 ret = do_pcm_hwsync(substream);
2508 ret = rewind_appl_ptr(substream, frames,
2509 snd_pcm_playback_hw_avail(runtime));
2510 snd_pcm_stream_unlock_irq(substream);
2514 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2515 snd_pcm_uframes_t frames)
2517 struct snd_pcm_runtime *runtime = substream->runtime;
2518 snd_pcm_sframes_t ret;
2523 snd_pcm_stream_lock_irq(substream);
2524 ret = do_pcm_hwsync(substream);
2526 ret = rewind_appl_ptr(substream, frames,
2527 snd_pcm_capture_hw_avail(runtime));
2528 snd_pcm_stream_unlock_irq(substream);
2532 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2533 snd_pcm_uframes_t frames)
2535 struct snd_pcm_runtime *runtime = substream->runtime;
2536 snd_pcm_sframes_t ret;
2541 snd_pcm_stream_lock_irq(substream);
2542 ret = do_pcm_hwsync(substream);
2544 ret = forward_appl_ptr(substream, frames,
2545 snd_pcm_playback_avail(runtime));
2546 snd_pcm_stream_unlock_irq(substream);
2550 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2551 snd_pcm_uframes_t frames)
2553 struct snd_pcm_runtime *runtime = substream->runtime;
2554 snd_pcm_sframes_t ret;
2559 snd_pcm_stream_lock_irq(substream);
2560 ret = do_pcm_hwsync(substream);
2562 ret = forward_appl_ptr(substream, frames,
2563 snd_pcm_capture_avail(runtime));
2564 snd_pcm_stream_unlock_irq(substream);
2568 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2572 snd_pcm_stream_lock_irq(substream);
2573 err = do_pcm_hwsync(substream);
2574 snd_pcm_stream_unlock_irq(substream);
2578 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2579 snd_pcm_sframes_t __user *res)
2581 struct snd_pcm_runtime *runtime = substream->runtime;
2583 snd_pcm_sframes_t n = 0;
2585 snd_pcm_stream_lock_irq(substream);
2586 err = do_pcm_hwsync(substream);
2588 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2589 n = snd_pcm_playback_hw_avail(runtime);
2591 n = snd_pcm_capture_avail(runtime);
2592 n += runtime->delay;
2594 snd_pcm_stream_unlock_irq(substream);
2596 if (put_user(n, res))
2601 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2602 struct snd_pcm_sync_ptr __user *_sync_ptr)
2604 struct snd_pcm_runtime *runtime = substream->runtime;
2605 struct snd_pcm_sync_ptr sync_ptr;
2606 volatile struct snd_pcm_mmap_status *status;
2607 volatile struct snd_pcm_mmap_control *control;
2610 memset(&sync_ptr, 0, sizeof(sync_ptr));
2611 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2613 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2615 status = runtime->status;
2616 control = runtime->control;
2617 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2618 err = snd_pcm_hwsync(substream);
2622 snd_pcm_stream_lock_irq(substream);
2623 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2624 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2626 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2627 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2628 control->avail_min = sync_ptr.c.control.avail_min;
2630 sync_ptr.c.control.avail_min = control->avail_min;
2631 sync_ptr.s.status.state = status->state;
2632 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2633 sync_ptr.s.status.tstamp = status->tstamp;
2634 sync_ptr.s.status.suspended_state = status->suspended_state;
2635 snd_pcm_stream_unlock_irq(substream);
2636 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2641 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2643 struct snd_pcm_runtime *runtime = substream->runtime;
2646 if (get_user(arg, _arg))
2648 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2650 runtime->tstamp_type = arg;
2654 static int snd_pcm_common_ioctl1(struct file *file,
2655 struct snd_pcm_substream *substream,
2656 unsigned int cmd, void __user *arg)
2659 case SNDRV_PCM_IOCTL_PVERSION:
2660 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2661 case SNDRV_PCM_IOCTL_INFO:
2662 return snd_pcm_info_user(substream, arg);
2663 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2665 case SNDRV_PCM_IOCTL_TTSTAMP:
2666 return snd_pcm_tstamp(substream, arg);
2667 case SNDRV_PCM_IOCTL_HW_REFINE:
2668 return snd_pcm_hw_refine_user(substream, arg);
2669 case SNDRV_PCM_IOCTL_HW_PARAMS:
2670 return snd_pcm_hw_params_user(substream, arg);
2671 case SNDRV_PCM_IOCTL_HW_FREE:
2672 return snd_pcm_hw_free(substream);
2673 case SNDRV_PCM_IOCTL_SW_PARAMS:
2674 return snd_pcm_sw_params_user(substream, arg);
2675 case SNDRV_PCM_IOCTL_STATUS:
2676 return snd_pcm_status_user(substream, arg, false);
2677 case SNDRV_PCM_IOCTL_STATUS_EXT:
2678 return snd_pcm_status_user(substream, arg, true);
2679 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2680 return snd_pcm_channel_info_user(substream, arg);
2681 case SNDRV_PCM_IOCTL_PREPARE:
2682 return snd_pcm_prepare(substream, file);
2683 case SNDRV_PCM_IOCTL_RESET:
2684 return snd_pcm_reset(substream);
2685 case SNDRV_PCM_IOCTL_START:
2686 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2687 case SNDRV_PCM_IOCTL_LINK:
2688 return snd_pcm_link(substream, (int)(unsigned long) arg);
2689 case SNDRV_PCM_IOCTL_UNLINK:
2690 return snd_pcm_unlink(substream);
2691 case SNDRV_PCM_IOCTL_RESUME:
2692 return snd_pcm_resume(substream);
2693 case SNDRV_PCM_IOCTL_XRUN:
2694 return snd_pcm_xrun(substream);
2695 case SNDRV_PCM_IOCTL_HWSYNC:
2696 return snd_pcm_hwsync(substream);
2697 case SNDRV_PCM_IOCTL_DELAY:
2698 return snd_pcm_delay(substream, arg);
2699 case SNDRV_PCM_IOCTL_SYNC_PTR:
2700 return snd_pcm_sync_ptr(substream, arg);
2701 #ifdef CONFIG_SND_SUPPORT_OLD_API
2702 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2703 return snd_pcm_hw_refine_old_user(substream, arg);
2704 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2705 return snd_pcm_hw_params_old_user(substream, arg);
2707 case SNDRV_PCM_IOCTL_DRAIN:
2708 return snd_pcm_drain(substream, file);
2709 case SNDRV_PCM_IOCTL_DROP:
2710 return snd_pcm_drop(substream);
2711 case SNDRV_PCM_IOCTL_PAUSE:
2714 snd_pcm_stream_lock_irq(substream);
2715 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2716 snd_pcm_stream_unlock_irq(substream);
2720 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2724 static int snd_pcm_playback_ioctl1(struct file *file,
2725 struct snd_pcm_substream *substream,
2726 unsigned int cmd, void __user *arg)
2728 if (snd_BUG_ON(!substream))
2730 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2733 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2735 struct snd_xferi xferi;
2736 struct snd_xferi __user *_xferi = arg;
2737 struct snd_pcm_runtime *runtime = substream->runtime;
2738 snd_pcm_sframes_t result;
2739 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2741 if (put_user(0, &_xferi->result))
2743 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2745 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2746 __put_user(result, &_xferi->result);
2747 return result < 0 ? result : 0;
2749 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2751 struct snd_xfern xfern;
2752 struct snd_xfern __user *_xfern = arg;
2753 struct snd_pcm_runtime *runtime = substream->runtime;
2755 snd_pcm_sframes_t result;
2756 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2758 if (runtime->channels > 128)
2760 if (put_user(0, &_xfern->result))
2762 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2765 bufs = memdup_user(xfern.bufs,
2766 sizeof(void *) * runtime->channels);
2768 return PTR_ERR(bufs);
2769 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2771 __put_user(result, &_xfern->result);
2772 return result < 0 ? result : 0;
2774 case SNDRV_PCM_IOCTL_REWIND:
2776 snd_pcm_uframes_t frames;
2777 snd_pcm_uframes_t __user *_frames = arg;
2778 snd_pcm_sframes_t result;
2779 if (get_user(frames, _frames))
2781 if (put_user(0, _frames))
2783 result = snd_pcm_playback_rewind(substream, frames);
2784 __put_user(result, _frames);
2785 return result < 0 ? result : 0;
2787 case SNDRV_PCM_IOCTL_FORWARD:
2789 snd_pcm_uframes_t frames;
2790 snd_pcm_uframes_t __user *_frames = arg;
2791 snd_pcm_sframes_t result;
2792 if (get_user(frames, _frames))
2794 if (put_user(0, _frames))
2796 result = snd_pcm_playback_forward(substream, frames);
2797 __put_user(result, _frames);
2798 return result < 0 ? result : 0;
2801 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2804 static int snd_pcm_capture_ioctl1(struct file *file,
2805 struct snd_pcm_substream *substream,
2806 unsigned int cmd, void __user *arg)
2808 if (snd_BUG_ON(!substream))
2810 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2813 case SNDRV_PCM_IOCTL_READI_FRAMES:
2815 struct snd_xferi xferi;
2816 struct snd_xferi __user *_xferi = arg;
2817 struct snd_pcm_runtime *runtime = substream->runtime;
2818 snd_pcm_sframes_t result;
2819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 if (put_user(0, &_xferi->result))
2823 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2825 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2826 __put_user(result, &_xferi->result);
2827 return result < 0 ? result : 0;
2829 case SNDRV_PCM_IOCTL_READN_FRAMES:
2831 struct snd_xfern xfern;
2832 struct snd_xfern __user *_xfern = arg;
2833 struct snd_pcm_runtime *runtime = substream->runtime;
2835 snd_pcm_sframes_t result;
2836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2838 if (runtime->channels > 128)
2840 if (put_user(0, &_xfern->result))
2842 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2845 bufs = memdup_user(xfern.bufs,
2846 sizeof(void *) * runtime->channels);
2848 return PTR_ERR(bufs);
2849 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2851 __put_user(result, &_xfern->result);
2852 return result < 0 ? result : 0;
2854 case SNDRV_PCM_IOCTL_REWIND:
2856 snd_pcm_uframes_t frames;
2857 snd_pcm_uframes_t __user *_frames = arg;
2858 snd_pcm_sframes_t result;
2859 if (get_user(frames, _frames))
2861 if (put_user(0, _frames))
2863 result = snd_pcm_capture_rewind(substream, frames);
2864 __put_user(result, _frames);
2865 return result < 0 ? result : 0;
2867 case SNDRV_PCM_IOCTL_FORWARD:
2869 snd_pcm_uframes_t frames;
2870 snd_pcm_uframes_t __user *_frames = arg;
2871 snd_pcm_sframes_t result;
2872 if (get_user(frames, _frames))
2874 if (put_user(0, _frames))
2876 result = snd_pcm_capture_forward(substream, frames);
2877 __put_user(result, _frames);
2878 return result < 0 ? result : 0;
2881 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2884 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2887 struct snd_pcm_file *pcm_file;
2889 pcm_file = file->private_data;
2891 if (((cmd >> 8) & 0xff) != 'A')
2894 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2895 (void __user *)arg);
2898 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2901 struct snd_pcm_file *pcm_file;
2903 pcm_file = file->private_data;
2905 if (((cmd >> 8) & 0xff) != 'A')
2908 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2909 (void __user *)arg);
2912 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2913 unsigned int cmd, void *arg)
2918 fs = snd_enter_user();
2919 switch (substream->stream) {
2920 case SNDRV_PCM_STREAM_PLAYBACK:
2921 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2922 (void __user *)arg);
2924 case SNDRV_PCM_STREAM_CAPTURE:
2925 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2926 (void __user *)arg);
2936 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2938 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2941 struct snd_pcm_file *pcm_file;
2942 struct snd_pcm_substream *substream;
2943 struct snd_pcm_runtime *runtime;
2944 snd_pcm_sframes_t result;
2946 pcm_file = file->private_data;
2947 substream = pcm_file->substream;
2948 if (PCM_RUNTIME_CHECK(substream))
2950 runtime = substream->runtime;
2951 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2953 if (!frame_aligned(runtime, count))
2955 count = bytes_to_frames(runtime, count);
2956 result = snd_pcm_lib_read(substream, buf, count);
2958 result = frames_to_bytes(runtime, result);
2962 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2963 size_t count, loff_t * offset)
2965 struct snd_pcm_file *pcm_file;
2966 struct snd_pcm_substream *substream;
2967 struct snd_pcm_runtime *runtime;
2968 snd_pcm_sframes_t result;
2970 pcm_file = file->private_data;
2971 substream = pcm_file->substream;
2972 if (PCM_RUNTIME_CHECK(substream))
2974 runtime = substream->runtime;
2975 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2977 if (!frame_aligned(runtime, count))
2979 count = bytes_to_frames(runtime, count);
2980 result = snd_pcm_lib_write(substream, buf, count);
2982 result = frames_to_bytes(runtime, result);
2986 static ssize_t snd_pcm_readv(struct kiocb *iocb, struct iov_iter *to)
2988 struct snd_pcm_file *pcm_file;
2989 struct snd_pcm_substream *substream;
2990 struct snd_pcm_runtime *runtime;
2991 snd_pcm_sframes_t result;
2994 snd_pcm_uframes_t frames;
2996 pcm_file = iocb->ki_filp->private_data;
2997 substream = pcm_file->substream;
2998 if (PCM_RUNTIME_CHECK(substream))
3000 runtime = substream->runtime;
3001 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3003 if (!iter_is_iovec(to))
3005 if (to->nr_segs > 1024 || to->nr_segs != runtime->channels)
3007 if (!frame_aligned(runtime, to->iov->iov_len))
3009 frames = bytes_to_samples(runtime, to->iov->iov_len);
3010 bufs = kmalloc(sizeof(void *) * to->nr_segs, GFP_KERNEL);
3013 for (i = 0; i < to->nr_segs; ++i)
3014 bufs[i] = to->iov[i].iov_base;
3015 result = snd_pcm_lib_readv(substream, bufs, frames);
3017 result = frames_to_bytes(runtime, result);
3022 static ssize_t snd_pcm_writev(struct kiocb *iocb, struct iov_iter *from)
3024 struct snd_pcm_file *pcm_file;
3025 struct snd_pcm_substream *substream;
3026 struct snd_pcm_runtime *runtime;
3027 snd_pcm_sframes_t result;
3030 snd_pcm_uframes_t frames;
3032 pcm_file = iocb->ki_filp->private_data;
3033 substream = pcm_file->substream;
3034 if (PCM_RUNTIME_CHECK(substream))
3036 runtime = substream->runtime;
3037 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3039 if (!iter_is_iovec(from))
3041 if (from->nr_segs > 128 || from->nr_segs != runtime->channels ||
3042 !frame_aligned(runtime, from->iov->iov_len))
3044 frames = bytes_to_samples(runtime, from->iov->iov_len);
3045 bufs = kmalloc(sizeof(void *) * from->nr_segs, GFP_KERNEL);
3048 for (i = 0; i < from->nr_segs; ++i)
3049 bufs[i] = from->iov[i].iov_base;
3050 result = snd_pcm_lib_writev(substream, bufs, frames);
3052 result = frames_to_bytes(runtime, result);
3057 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3059 struct snd_pcm_file *pcm_file;
3060 struct snd_pcm_substream *substream;
3061 struct snd_pcm_runtime *runtime;
3063 snd_pcm_uframes_t avail;
3065 pcm_file = file->private_data;
3067 substream = pcm_file->substream;
3068 if (PCM_RUNTIME_CHECK(substream))
3069 return POLLOUT | POLLWRNORM | POLLERR;
3070 runtime = substream->runtime;
3072 poll_wait(file, &runtime->sleep, wait);
3074 snd_pcm_stream_lock_irq(substream);
3075 avail = snd_pcm_playback_avail(runtime);
3076 switch (runtime->status->state) {
3077 case SNDRV_PCM_STATE_RUNNING:
3078 case SNDRV_PCM_STATE_PREPARED:
3079 case SNDRV_PCM_STATE_PAUSED:
3080 if (avail >= runtime->control->avail_min) {
3081 mask = POLLOUT | POLLWRNORM;
3085 case SNDRV_PCM_STATE_DRAINING:
3089 mask = POLLOUT | POLLWRNORM | POLLERR;
3092 snd_pcm_stream_unlock_irq(substream);
3096 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3098 struct snd_pcm_file *pcm_file;
3099 struct snd_pcm_substream *substream;
3100 struct snd_pcm_runtime *runtime;
3102 snd_pcm_uframes_t avail;
3104 pcm_file = file->private_data;
3106 substream = pcm_file->substream;
3107 if (PCM_RUNTIME_CHECK(substream))
3108 return POLLIN | POLLRDNORM | POLLERR;
3109 runtime = substream->runtime;
3111 poll_wait(file, &runtime->sleep, wait);
3113 snd_pcm_stream_lock_irq(substream);
3114 avail = snd_pcm_capture_avail(runtime);
3115 switch (runtime->status->state) {
3116 case SNDRV_PCM_STATE_RUNNING:
3117 case SNDRV_PCM_STATE_PREPARED:
3118 case SNDRV_PCM_STATE_PAUSED:
3119 if (avail >= runtime->control->avail_min) {
3120 mask = POLLIN | POLLRDNORM;
3125 case SNDRV_PCM_STATE_DRAINING:
3127 mask = POLLIN | POLLRDNORM;
3132 mask = POLLIN | POLLRDNORM | POLLERR;
3135 snd_pcm_stream_unlock_irq(substream);
3144 * Only on coherent architectures, we can mmap the status and the control records
3145 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3147 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3149 * mmap status record
3151 static int snd_pcm_mmap_status_fault(struct vm_fault *vmf)
3153 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3154 struct snd_pcm_runtime *runtime;
3156 if (substream == NULL)
3157 return VM_FAULT_SIGBUS;
3158 runtime = substream->runtime;
3159 vmf->page = virt_to_page(runtime->status);
3160 get_page(vmf->page);
3164 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3166 .fault = snd_pcm_mmap_status_fault,
3169 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3170 struct vm_area_struct *area)
3173 if (!(area->vm_flags & VM_READ))
3175 size = area->vm_end - area->vm_start;
3176 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3178 area->vm_ops = &snd_pcm_vm_ops_status;
3179 area->vm_private_data = substream;
3180 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3185 * mmap control record
3187 static int snd_pcm_mmap_control_fault(struct vm_fault *vmf)
3189 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3190 struct snd_pcm_runtime *runtime;
3192 if (substream == NULL)
3193 return VM_FAULT_SIGBUS;
3194 runtime = substream->runtime;
3195 vmf->page = virt_to_page(runtime->control);
3196 get_page(vmf->page);
3200 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3202 .fault = snd_pcm_mmap_control_fault,
3205 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3206 struct vm_area_struct *area)
3209 if (!(area->vm_flags & VM_READ))
3211 size = area->vm_end - area->vm_start;
3212 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3214 area->vm_ops = &snd_pcm_vm_ops_control;
3215 area->vm_private_data = substream;
3216 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3219 #else /* ! coherent mmap */
3221 * don't support mmap for status and control records.
3223 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3224 struct vm_area_struct *area)
3228 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3229 struct vm_area_struct *area)
3233 #endif /* coherent mmap */
3235 static inline struct page *
3236 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3238 void *vaddr = substream->runtime->dma_area + ofs;
3239 return virt_to_page(vaddr);
3243 * fault callback for mmapping a RAM page
3245 static int snd_pcm_mmap_data_fault(struct vm_fault *vmf)
3247 struct snd_pcm_substream *substream = vmf->vma->vm_private_data;
3248 struct snd_pcm_runtime *runtime;
3249 unsigned long offset;
3253 if (substream == NULL)
3254 return VM_FAULT_SIGBUS;
3255 runtime = substream->runtime;
3256 offset = vmf->pgoff << PAGE_SHIFT;
3257 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3258 if (offset > dma_bytes - PAGE_SIZE)
3259 return VM_FAULT_SIGBUS;
3260 if (substream->ops->page)
3261 page = substream->ops->page(substream, offset);
3263 page = snd_pcm_default_page_ops(substream, offset);
3265 return VM_FAULT_SIGBUS;
3271 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3272 .open = snd_pcm_mmap_data_open,
3273 .close = snd_pcm_mmap_data_close,
3276 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3277 .open = snd_pcm_mmap_data_open,
3278 .close = snd_pcm_mmap_data_close,
3279 .fault = snd_pcm_mmap_data_fault,
3283 * mmap the DMA buffer on RAM
3287 * snd_pcm_lib_default_mmap - Default PCM data mmap function
3288 * @substream: PCM substream
3291 * This is the default mmap handler for PCM data. When mmap pcm_ops is NULL,
3292 * this function is invoked implicitly.
3294 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3295 struct vm_area_struct *area)
3297 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3298 #ifdef CONFIG_GENERIC_ALLOCATOR
3299 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3300 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3301 return remap_pfn_range(area, area->vm_start,
3302 substream->dma_buffer.addr >> PAGE_SHIFT,
3303 area->vm_end - area->vm_start, area->vm_page_prot);
3305 #endif /* CONFIG_GENERIC_ALLOCATOR */
3306 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3307 if (!substream->ops->page &&
3308 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3309 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3311 substream->runtime->dma_area,
3312 substream->runtime->dma_addr,
3313 area->vm_end - area->vm_start);
3314 #endif /* CONFIG_X86 */
3315 /* mmap with fault handler */
3316 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3319 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3322 * mmap the DMA buffer on I/O memory area
3324 #if SNDRV_PCM_INFO_MMAP_IOMEM
3326 * snd_pcm_lib_mmap_iomem - Default PCM data mmap function for I/O mem
3327 * @substream: PCM substream
3330 * When your hardware uses the iomapped pages as the hardware buffer and
3331 * wants to mmap it, pass this function as mmap pcm_ops. Note that this
3332 * is supposed to work only on limited architectures.
3334 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3335 struct vm_area_struct *area)
3337 struct snd_pcm_runtime *runtime = substream->runtime;;
3339 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3340 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3343 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3344 #endif /* SNDRV_PCM_INFO_MMAP */
3349 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3350 struct vm_area_struct *area)
3352 struct snd_pcm_runtime *runtime;
3354 unsigned long offset;
3358 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3359 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3362 if (!(area->vm_flags & VM_READ))
3365 runtime = substream->runtime;
3366 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3368 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3370 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3371 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3373 size = area->vm_end - area->vm_start;
3374 offset = area->vm_pgoff << PAGE_SHIFT;
3375 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3376 if ((size_t)size > dma_bytes)
3378 if (offset > dma_bytes - size)
3381 area->vm_ops = &snd_pcm_vm_ops_data;
3382 area->vm_private_data = substream;
3383 if (substream->ops->mmap)
3384 err = substream->ops->mmap(substream, area);
3386 err = snd_pcm_lib_default_mmap(substream, area);
3388 atomic_inc(&substream->mmap_count);
3392 EXPORT_SYMBOL(snd_pcm_mmap_data);
3394 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3396 struct snd_pcm_file * pcm_file;
3397 struct snd_pcm_substream *substream;
3398 unsigned long offset;
3400 pcm_file = file->private_data;
3401 substream = pcm_file->substream;
3402 if (PCM_RUNTIME_CHECK(substream))
3405 offset = area->vm_pgoff << PAGE_SHIFT;
3407 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3408 if (pcm_file->no_compat_mmap)
3410 return snd_pcm_mmap_status(substream, file, area);
3411 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3412 if (pcm_file->no_compat_mmap)
3414 return snd_pcm_mmap_control(substream, file, area);
3416 return snd_pcm_mmap_data(substream, file, area);
3421 static int snd_pcm_fasync(int fd, struct file * file, int on)
3423 struct snd_pcm_file * pcm_file;
3424 struct snd_pcm_substream *substream;
3425 struct snd_pcm_runtime *runtime;
3427 pcm_file = file->private_data;
3428 substream = pcm_file->substream;
3429 if (PCM_RUNTIME_CHECK(substream))
3431 runtime = substream->runtime;
3432 return fasync_helper(fd, file, on, &runtime->fasync);
3438 #ifdef CONFIG_COMPAT
3439 #include "pcm_compat.c"
3441 #define snd_pcm_ioctl_compat NULL
3445 * To be removed helpers to keep binary compatibility
3448 #ifdef CONFIG_SND_SUPPORT_OLD_API
3449 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3450 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3452 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3453 struct snd_pcm_hw_params_old *oparams)
3457 memset(params, 0, sizeof(*params));
3458 params->flags = oparams->flags;
3459 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3460 params->masks[i].bits[0] = oparams->masks[i];
3461 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3462 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3463 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3464 params->info = oparams->info;
3465 params->msbits = oparams->msbits;
3466 params->rate_num = oparams->rate_num;
3467 params->rate_den = oparams->rate_den;
3468 params->fifo_size = oparams->fifo_size;
3471 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3472 struct snd_pcm_hw_params *params)
3476 memset(oparams, 0, sizeof(*oparams));
3477 oparams->flags = params->flags;
3478 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3479 oparams->masks[i] = params->masks[i].bits[0];
3480 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3481 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3482 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3483 oparams->info = params->info;
3484 oparams->msbits = params->msbits;
3485 oparams->rate_num = params->rate_num;
3486 oparams->rate_den = params->rate_den;
3487 oparams->fifo_size = params->fifo_size;
3490 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3491 struct snd_pcm_hw_params_old __user * _oparams)
3493 struct snd_pcm_hw_params *params;
3494 struct snd_pcm_hw_params_old *oparams = NULL;
3497 params = kmalloc(sizeof(*params), GFP_KERNEL);
3501 oparams = memdup_user(_oparams, sizeof(*oparams));
3502 if (IS_ERR(oparams)) {
3503 err = PTR_ERR(oparams);
3506 snd_pcm_hw_convert_from_old_params(params, oparams);
3507 err = snd_pcm_hw_refine(substream, params);
3508 snd_pcm_hw_convert_to_old_params(oparams, params);
3509 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3520 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3521 struct snd_pcm_hw_params_old __user * _oparams)
3523 struct snd_pcm_hw_params *params;
3524 struct snd_pcm_hw_params_old *oparams = NULL;
3527 params = kmalloc(sizeof(*params), GFP_KERNEL);
3531 oparams = memdup_user(_oparams, sizeof(*oparams));
3532 if (IS_ERR(oparams)) {
3533 err = PTR_ERR(oparams);
3536 snd_pcm_hw_convert_from_old_params(params, oparams);
3537 err = snd_pcm_hw_params(substream, params);
3538 snd_pcm_hw_convert_to_old_params(oparams, params);
3539 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3549 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3552 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3555 unsigned long pgoff,
3556 unsigned long flags)
3558 struct snd_pcm_file *pcm_file = file->private_data;
3559 struct snd_pcm_substream *substream = pcm_file->substream;
3560 struct snd_pcm_runtime *runtime = substream->runtime;
3561 unsigned long offset = pgoff << PAGE_SHIFT;
3564 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3565 return (unsigned long)runtime->status;
3566 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3567 return (unsigned long)runtime->control;
3569 return (unsigned long)runtime->dma_area + offset;
3573 # define snd_pcm_get_unmapped_area NULL
3580 const struct file_operations snd_pcm_f_ops[2] = {
3582 .owner = THIS_MODULE,
3583 .write = snd_pcm_write,
3584 .write_iter = snd_pcm_writev,
3585 .open = snd_pcm_playback_open,
3586 .release = snd_pcm_release,
3587 .llseek = no_llseek,
3588 .poll = snd_pcm_playback_poll,
3589 .unlocked_ioctl = snd_pcm_playback_ioctl,
3590 .compat_ioctl = snd_pcm_ioctl_compat,
3591 .mmap = snd_pcm_mmap,
3592 .fasync = snd_pcm_fasync,
3593 .get_unmapped_area = snd_pcm_get_unmapped_area,
3596 .owner = THIS_MODULE,
3597 .read = snd_pcm_read,
3598 .read_iter = snd_pcm_readv,
3599 .open = snd_pcm_capture_open,
3600 .release = snd_pcm_release,
3601 .llseek = no_llseek,
3602 .poll = snd_pcm_capture_poll,
3603 .unlocked_ioctl = snd_pcm_capture_ioctl,
3604 .compat_ioctl = snd_pcm_ioctl_compat,
3605 .mmap = snd_pcm_mmap,
3606 .fasync = snd_pcm_fasync,
3607 .get_unmapped_area = snd_pcm_get_unmapped_area,