2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos.h>
28 #include <linux/aio.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
43 struct snd_pcm_hw_params_old {
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
47 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
48 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rate_num;
54 unsigned int rate_den;
55 snd_pcm_uframes_t fifo_size;
56 unsigned char reserved[64];
59 #ifdef CONFIG_SND_SUPPORT_OLD_API
60 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
74 static DEFINE_RWLOCK(snd_pcm_link_rwlock);
75 static DECLARE_RWSEM(snd_pcm_link_rwsem);
77 void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
79 if (substream->pcm->nonatomic) {
80 down_read(&snd_pcm_link_rwsem);
81 mutex_lock(&substream->self_group.mutex);
83 read_lock(&snd_pcm_link_rwlock);
84 spin_lock(&substream->self_group.lock);
87 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock);
89 void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
91 if (substream->pcm->nonatomic) {
92 mutex_unlock(&substream->self_group.mutex);
93 up_read(&snd_pcm_link_rwsem);
95 spin_unlock(&substream->self_group.lock);
96 read_unlock(&snd_pcm_link_rwlock);
99 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock);
101 void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
103 if (!substream->pcm->nonatomic)
105 snd_pcm_stream_lock(substream);
107 EXPORT_SYMBOL_GPL(snd_pcm_stream_lock_irq);
109 void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
111 snd_pcm_stream_unlock(substream);
112 if (!substream->pcm->nonatomic)
115 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irq);
117 unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream)
119 unsigned long flags = 0;
120 if (!substream->pcm->nonatomic)
121 local_irq_save(flags);
122 snd_pcm_stream_lock(substream);
125 EXPORT_SYMBOL_GPL(_snd_pcm_stream_lock_irqsave);
127 void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
130 snd_pcm_stream_unlock(substream);
131 if (!substream->pcm->nonatomic)
132 local_irq_restore(flags);
134 EXPORT_SYMBOL_GPL(snd_pcm_stream_unlock_irqrestore);
136 static inline mm_segment_t snd_enter_user(void)
138 mm_segment_t fs = get_fs();
143 static inline void snd_leave_user(mm_segment_t fs)
150 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
152 struct snd_pcm_runtime *runtime;
153 struct snd_pcm *pcm = substream->pcm;
154 struct snd_pcm_str *pstr = substream->pstr;
156 memset(info, 0, sizeof(*info));
157 info->card = pcm->card->number;
158 info->device = pcm->device;
159 info->stream = substream->stream;
160 info->subdevice = substream->number;
161 strlcpy(info->id, pcm->id, sizeof(info->id));
162 strlcpy(info->name, pcm->name, sizeof(info->name));
163 info->dev_class = pcm->dev_class;
164 info->dev_subclass = pcm->dev_subclass;
165 info->subdevices_count = pstr->substream_count;
166 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
167 strlcpy(info->subname, substream->name, sizeof(info->subname));
168 runtime = substream->runtime;
169 /* AB: FIXME!!! This is definitely nonsense */
171 info->sync = runtime->sync;
172 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
177 int snd_pcm_info_user(struct snd_pcm_substream *substream,
178 struct snd_pcm_info __user * _info)
180 struct snd_pcm_info *info;
183 info = kmalloc(sizeof(*info), GFP_KERNEL);
186 err = snd_pcm_info(substream, info);
188 if (copy_to_user(_info, info, sizeof(*info)))
195 static bool hw_support_mmap(struct snd_pcm_substream *substream)
197 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_MMAP))
199 /* check architectures that return -EINVAL from dma_mmap_coherent() */
200 /* FIXME: this should be some global flag */
201 #if defined(CONFIG_C6X) || defined(CONFIG_FRV) || defined(CONFIG_MN10300) ||\
202 defined(CONFIG_PARISC) || defined(CONFIG_XTENSA)
203 if (!substream->ops->mmap &&
204 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
213 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
214 static const char * const snd_pcm_hw_param_names[] = {
218 HW_PARAM(SAMPLE_BITS),
219 HW_PARAM(FRAME_BITS),
222 HW_PARAM(PERIOD_TIME),
223 HW_PARAM(PERIOD_SIZE),
224 HW_PARAM(PERIOD_BYTES),
226 HW_PARAM(BUFFER_TIME),
227 HW_PARAM(BUFFER_SIZE),
228 HW_PARAM(BUFFER_BYTES),
233 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
234 struct snd_pcm_hw_params *params)
237 struct snd_pcm_hardware *hw;
238 struct snd_interval *i = NULL;
239 struct snd_mask *m = NULL;
240 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
241 unsigned int rstamps[constrs->rules_num];
242 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
243 unsigned int stamp = 2;
247 params->fifo_size = 0;
248 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
250 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
251 params->rate_num = 0;
252 params->rate_den = 0;
255 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
256 m = hw_param_mask(params, k);
257 if (snd_mask_empty(m))
259 if (!(params->rmask & (1 << k)))
262 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
263 pr_cont("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
265 changed = snd_mask_refine(m, constrs_mask(constrs, k));
267 pr_cont("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
270 params->cmask |= 1 << k;
275 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
276 i = hw_param_interval(params, k);
277 if (snd_interval_empty(i))
279 if (!(params->rmask & (1 << k)))
282 pr_debug("%s = ", snd_pcm_hw_param_names[k]);
287 i->openmin ? '(' : '[', i->min,
288 i->max, i->openmax ? ')' : ']');
291 changed = snd_interval_refine(i, constrs_interval(constrs, k));
296 pr_cont("%c%u %u%c\n",
297 i->openmin ? '(' : '[', i->min,
298 i->max, i->openmax ? ')' : ']');
301 params->cmask |= 1 << k;
306 for (k = 0; k < constrs->rules_num; k++)
308 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
309 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
312 for (k = 0; k < constrs->rules_num; k++) {
313 struct snd_pcm_hw_rule *r = &constrs->rules[k];
316 if (r->cond && !(r->cond & params->flags))
318 for (d = 0; r->deps[d] >= 0; d++) {
319 if (vstamps[r->deps[d]] > rstamps[k]) {
327 pr_debug("Rule %d [%p]: ", k, r->func);
329 pr_cont("%s = ", snd_pcm_hw_param_names[r->var]);
330 if (hw_is_mask(r->var)) {
331 m = hw_param_mask(params, r->var);
332 pr_cont("%x", *m->bits);
334 i = hw_param_interval(params, r->var);
339 i->openmin ? '(' : '[', i->min,
340 i->max, i->openmax ? ')' : ']');
344 changed = r->func(params, r);
348 if (hw_is_mask(r->var))
349 pr_cont("%x", *m->bits);
355 i->openmin ? '(' : '[', i->min,
356 i->max, i->openmax ? ')' : ']');
362 if (changed && r->var >= 0) {
363 params->cmask |= (1 << r->var);
364 vstamps[r->var] = stamp;
372 if (!params->msbits) {
373 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
374 if (snd_interval_single(i))
375 params->msbits = snd_interval_value(i);
378 if (!params->rate_den) {
379 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
380 if (snd_interval_single(i)) {
381 params->rate_num = snd_interval_value(i);
382 params->rate_den = 1;
386 hw = &substream->runtime->hw;
388 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
389 if (!hw_support_mmap(substream))
390 params->info &= ~(SNDRV_PCM_INFO_MMAP |
391 SNDRV_PCM_INFO_MMAP_VALID);
393 if (!params->fifo_size) {
394 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
395 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
396 if (snd_mask_min(m) == snd_mask_max(m) &&
397 snd_interval_min(i) == snd_interval_max(i)) {
398 changed = substream->ops->ioctl(substream,
399 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
408 EXPORT_SYMBOL(snd_pcm_hw_refine);
410 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
411 struct snd_pcm_hw_params __user * _params)
413 struct snd_pcm_hw_params *params;
416 params = memdup_user(_params, sizeof(*params));
418 return PTR_ERR(params);
420 err = snd_pcm_hw_refine(substream, params);
421 if (copy_to_user(_params, params, sizeof(*params))) {
430 static int period_to_usecs(struct snd_pcm_runtime *runtime)
435 return -1; /* invalid */
437 /* take 75% of period time as the deadline */
438 usecs = (750000 / runtime->rate) * runtime->period_size;
439 usecs += ((750000 % runtime->rate) * runtime->period_size) /
445 static void snd_pcm_set_state(struct snd_pcm_substream *substream, int state)
447 snd_pcm_stream_lock_irq(substream);
448 if (substream->runtime->status->state != SNDRV_PCM_STATE_DISCONNECTED)
449 substream->runtime->status->state = state;
450 snd_pcm_stream_unlock_irq(substream);
453 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
454 struct snd_pcm_hw_params *params)
456 struct snd_pcm_runtime *runtime;
459 snd_pcm_uframes_t frames;
461 if (PCM_RUNTIME_CHECK(substream))
463 runtime = substream->runtime;
464 snd_pcm_stream_lock_irq(substream);
465 switch (runtime->status->state) {
466 case SNDRV_PCM_STATE_OPEN:
467 case SNDRV_PCM_STATE_SETUP:
468 case SNDRV_PCM_STATE_PREPARED:
471 snd_pcm_stream_unlock_irq(substream);
474 snd_pcm_stream_unlock_irq(substream);
475 #if IS_ENABLED(CONFIG_SND_PCM_OSS)
476 if (!substream->oss.oss)
478 if (atomic_read(&substream->mmap_count))
482 err = snd_pcm_hw_refine(substream, params);
486 err = snd_pcm_hw_params_choose(substream, params);
490 if (substream->ops->hw_params != NULL) {
491 err = substream->ops->hw_params(substream, params);
496 runtime->access = params_access(params);
497 runtime->format = params_format(params);
498 runtime->subformat = params_subformat(params);
499 runtime->channels = params_channels(params);
500 runtime->rate = params_rate(params);
501 runtime->period_size = params_period_size(params);
502 runtime->periods = params_periods(params);
503 runtime->buffer_size = params_buffer_size(params);
504 runtime->info = params->info;
505 runtime->rate_num = params->rate_num;
506 runtime->rate_den = params->rate_den;
507 runtime->no_period_wakeup =
508 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
509 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
511 bits = snd_pcm_format_physical_width(runtime->format);
512 runtime->sample_bits = bits;
513 bits *= runtime->channels;
514 runtime->frame_bits = bits;
516 while (bits % 8 != 0) {
520 runtime->byte_align = bits / 8;
521 runtime->min_align = frames;
523 /* Default sw params */
524 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
525 runtime->period_step = 1;
526 runtime->control->avail_min = runtime->period_size;
527 runtime->start_threshold = 1;
528 runtime->stop_threshold = runtime->buffer_size;
529 runtime->silence_threshold = 0;
530 runtime->silence_size = 0;
531 runtime->boundary = runtime->buffer_size;
532 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
533 runtime->boundary *= 2;
535 snd_pcm_timer_resolution_change(substream);
536 snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);
538 if (pm_qos_request_active(&substream->latency_pm_qos_req))
539 pm_qos_remove_request(&substream->latency_pm_qos_req);
540 if ((usecs = period_to_usecs(runtime)) >= 0)
541 pm_qos_add_request(&substream->latency_pm_qos_req,
542 PM_QOS_CPU_DMA_LATENCY, usecs);
545 /* hardware might be unusable from this time,
546 so we force application to retry to set
547 the correct hardware parameter settings */
548 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
549 if (substream->ops->hw_free != NULL)
550 substream->ops->hw_free(substream);
554 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
555 struct snd_pcm_hw_params __user * _params)
557 struct snd_pcm_hw_params *params;
560 params = memdup_user(_params, sizeof(*params));
562 return PTR_ERR(params);
564 err = snd_pcm_hw_params(substream, params);
565 if (copy_to_user(_params, params, sizeof(*params))) {
574 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
576 struct snd_pcm_runtime *runtime;
579 if (PCM_RUNTIME_CHECK(substream))
581 runtime = substream->runtime;
582 snd_pcm_stream_lock_irq(substream);
583 switch (runtime->status->state) {
584 case SNDRV_PCM_STATE_SETUP:
585 case SNDRV_PCM_STATE_PREPARED:
588 snd_pcm_stream_unlock_irq(substream);
591 snd_pcm_stream_unlock_irq(substream);
592 if (atomic_read(&substream->mmap_count))
594 if (substream->ops->hw_free)
595 result = substream->ops->hw_free(substream);
596 snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
597 pm_qos_remove_request(&substream->latency_pm_qos_req);
601 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
602 struct snd_pcm_sw_params *params)
604 struct snd_pcm_runtime *runtime;
607 if (PCM_RUNTIME_CHECK(substream))
609 runtime = substream->runtime;
610 snd_pcm_stream_lock_irq(substream);
611 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
612 snd_pcm_stream_unlock_irq(substream);
615 snd_pcm_stream_unlock_irq(substream);
617 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
619 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12) &&
620 params->tstamp_type > SNDRV_PCM_TSTAMP_TYPE_LAST)
622 if (params->avail_min == 0)
624 if (params->silence_size >= runtime->boundary) {
625 if (params->silence_threshold != 0)
628 if (params->silence_size > params->silence_threshold)
630 if (params->silence_threshold > runtime->buffer_size)
634 snd_pcm_stream_lock_irq(substream);
635 runtime->tstamp_mode = params->tstamp_mode;
636 if (params->proto >= SNDRV_PROTOCOL_VERSION(2, 0, 12))
637 runtime->tstamp_type = params->tstamp_type;
638 runtime->period_step = params->period_step;
639 runtime->control->avail_min = params->avail_min;
640 runtime->start_threshold = params->start_threshold;
641 runtime->stop_threshold = params->stop_threshold;
642 runtime->silence_threshold = params->silence_threshold;
643 runtime->silence_size = params->silence_size;
644 params->boundary = runtime->boundary;
645 if (snd_pcm_running(substream)) {
646 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
647 runtime->silence_size > 0)
648 snd_pcm_playback_silence(substream, ULONG_MAX);
649 err = snd_pcm_update_state(substream, runtime);
651 snd_pcm_stream_unlock_irq(substream);
655 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
656 struct snd_pcm_sw_params __user * _params)
658 struct snd_pcm_sw_params params;
660 if (copy_from_user(¶ms, _params, sizeof(params)))
662 err = snd_pcm_sw_params(substream, ¶ms);
663 if (copy_to_user(_params, ¶ms, sizeof(params)))
668 int snd_pcm_status(struct snd_pcm_substream *substream,
669 struct snd_pcm_status *status)
671 struct snd_pcm_runtime *runtime = substream->runtime;
673 snd_pcm_stream_lock_irq(substream);
674 status->state = runtime->status->state;
675 status->suspended_state = runtime->status->suspended_state;
676 if (status->state == SNDRV_PCM_STATE_OPEN)
678 status->trigger_tstamp = runtime->trigger_tstamp;
679 if (snd_pcm_running(substream)) {
680 snd_pcm_update_hw_ptr(substream);
681 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
682 status->tstamp = runtime->status->tstamp;
683 status->audio_tstamp =
684 runtime->status->audio_tstamp;
688 snd_pcm_gettime(runtime, &status->tstamp);
690 status->appl_ptr = runtime->control->appl_ptr;
691 status->hw_ptr = runtime->status->hw_ptr;
692 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
693 status->avail = snd_pcm_playback_avail(runtime);
694 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
695 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
696 status->delay = runtime->buffer_size - status->avail;
697 status->delay += runtime->delay;
701 status->avail = snd_pcm_capture_avail(runtime);
702 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
703 status->delay = status->avail + runtime->delay;
707 status->avail_max = runtime->avail_max;
708 status->overrange = runtime->overrange;
709 runtime->avail_max = 0;
710 runtime->overrange = 0;
712 snd_pcm_stream_unlock_irq(substream);
716 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
717 struct snd_pcm_status __user * _status)
719 struct snd_pcm_status status;
722 memset(&status, 0, sizeof(status));
723 res = snd_pcm_status(substream, &status);
726 if (copy_to_user(_status, &status, sizeof(status)))
731 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
732 struct snd_pcm_channel_info * info)
734 struct snd_pcm_runtime *runtime;
735 unsigned int channel;
737 channel = info->channel;
738 runtime = substream->runtime;
739 snd_pcm_stream_lock_irq(substream);
740 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
741 snd_pcm_stream_unlock_irq(substream);
744 snd_pcm_stream_unlock_irq(substream);
745 if (channel >= runtime->channels)
747 memset(info, 0, sizeof(*info));
748 info->channel = channel;
749 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
752 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
753 struct snd_pcm_channel_info __user * _info)
755 struct snd_pcm_channel_info info;
758 if (copy_from_user(&info, _info, sizeof(info)))
760 res = snd_pcm_channel_info(substream, &info);
763 if (copy_to_user(_info, &info, sizeof(info)))
768 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
770 struct snd_pcm_runtime *runtime = substream->runtime;
771 if (runtime->trigger_master == NULL)
773 if (runtime->trigger_master == substream) {
774 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
776 snd_pcm_trigger_tstamp(runtime->trigger_master);
777 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
779 runtime->trigger_master = NULL;
783 int (*pre_action)(struct snd_pcm_substream *substream, int state);
784 int (*do_action)(struct snd_pcm_substream *substream, int state);
785 void (*undo_action)(struct snd_pcm_substream *substream, int state);
786 void (*post_action)(struct snd_pcm_substream *substream, int state);
790 * this functions is core for handling of linked stream
791 * Note: the stream state might be changed also on failure
792 * Note2: call with calling stream lock + link lock
794 static int snd_pcm_action_group(struct action_ops *ops,
795 struct snd_pcm_substream *substream,
796 int state, int do_lock)
798 struct snd_pcm_substream *s = NULL;
799 struct snd_pcm_substream *s1;
802 snd_pcm_group_for_each_entry(s, substream) {
803 if (do_lock && s != substream) {
804 if (s->pcm->nonatomic)
805 mutex_lock_nested(&s->self_group.mutex,
806 SINGLE_DEPTH_NESTING);
808 spin_lock_nested(&s->self_group.lock,
809 SINGLE_DEPTH_NESTING);
811 res = ops->pre_action(s, state);
815 snd_pcm_group_for_each_entry(s, substream) {
816 res = ops->do_action(s, state);
818 if (ops->undo_action) {
819 snd_pcm_group_for_each_entry(s1, substream) {
820 if (s1 == s) /* failed stream */
822 ops->undo_action(s1, state);
825 s = NULL; /* unlock all */
829 snd_pcm_group_for_each_entry(s, substream) {
830 ops->post_action(s, state);
835 snd_pcm_group_for_each_entry(s1, substream) {
836 if (s1 != substream) {
837 if (s1->pcm->nonatomic)
838 mutex_unlock(&s1->self_group.mutex);
840 spin_unlock(&s1->self_group.lock);
842 if (s1 == s) /* end */
850 * Note: call with stream lock
852 static int snd_pcm_action_single(struct action_ops *ops,
853 struct snd_pcm_substream *substream,
858 res = ops->pre_action(substream, state);
861 res = ops->do_action(substream, state);
863 ops->post_action(substream, state);
864 else if (ops->undo_action)
865 ops->undo_action(substream, state);
869 /* call in mutex-protected context */
870 static int snd_pcm_action_mutex(struct action_ops *ops,
871 struct snd_pcm_substream *substream,
876 if (snd_pcm_stream_linked(substream)) {
877 if (!mutex_trylock(&substream->group->mutex)) {
878 mutex_unlock(&substream->self_group.mutex);
879 mutex_lock(&substream->group->mutex);
880 mutex_lock(&substream->self_group.mutex);
882 res = snd_pcm_action_group(ops, substream, state, 1);
883 mutex_unlock(&substream->group->mutex);
885 res = snd_pcm_action_single(ops, substream, state);
891 * Note: call with stream lock
893 static int snd_pcm_action(struct action_ops *ops,
894 struct snd_pcm_substream *substream,
899 if (substream->pcm->nonatomic)
900 return snd_pcm_action_mutex(ops, substream, state);
902 if (snd_pcm_stream_linked(substream)) {
903 if (!spin_trylock(&substream->group->lock)) {
904 spin_unlock(&substream->self_group.lock);
905 spin_lock(&substream->group->lock);
906 spin_lock(&substream->self_group.lock);
908 res = snd_pcm_action_group(ops, substream, state, 1);
909 spin_unlock(&substream->group->lock);
911 res = snd_pcm_action_single(ops, substream, state);
916 static int snd_pcm_action_lock_mutex(struct action_ops *ops,
917 struct snd_pcm_substream *substream,
922 down_read(&snd_pcm_link_rwsem);
923 if (snd_pcm_stream_linked(substream)) {
924 mutex_lock(&substream->group->mutex);
925 mutex_lock_nested(&substream->self_group.mutex,
926 SINGLE_DEPTH_NESTING);
927 res = snd_pcm_action_group(ops, substream, state, 1);
928 mutex_unlock(&substream->self_group.mutex);
929 mutex_unlock(&substream->group->mutex);
931 mutex_lock(&substream->self_group.mutex);
932 res = snd_pcm_action_single(ops, substream, state);
933 mutex_unlock(&substream->self_group.mutex);
935 up_read(&snd_pcm_link_rwsem);
940 * Note: don't use any locks before
942 static int snd_pcm_action_lock_irq(struct action_ops *ops,
943 struct snd_pcm_substream *substream,
948 if (substream->pcm->nonatomic)
949 return snd_pcm_action_lock_mutex(ops, substream, state);
951 read_lock_irq(&snd_pcm_link_rwlock);
952 if (snd_pcm_stream_linked(substream)) {
953 spin_lock(&substream->group->lock);
954 spin_lock(&substream->self_group.lock);
955 res = snd_pcm_action_group(ops, substream, state, 1);
956 spin_unlock(&substream->self_group.lock);
957 spin_unlock(&substream->group->lock);
959 spin_lock(&substream->self_group.lock);
960 res = snd_pcm_action_single(ops, substream, state);
961 spin_unlock(&substream->self_group.lock);
963 read_unlock_irq(&snd_pcm_link_rwlock);
969 static int snd_pcm_action_nonatomic(struct action_ops *ops,
970 struct snd_pcm_substream *substream,
975 down_read(&snd_pcm_link_rwsem);
976 if (snd_pcm_stream_linked(substream))
977 res = snd_pcm_action_group(ops, substream, state, 0);
979 res = snd_pcm_action_single(ops, substream, state);
980 up_read(&snd_pcm_link_rwsem);
987 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
989 struct snd_pcm_runtime *runtime = substream->runtime;
990 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
992 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
993 !snd_pcm_playback_data(substream))
995 runtime->trigger_master = substream;
999 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1001 if (substream->runtime->trigger_master != substream)
1003 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
1006 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1008 if (substream->runtime->trigger_master == substream)
1009 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1012 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1014 struct snd_pcm_runtime *runtime = substream->runtime;
1015 snd_pcm_trigger_tstamp(substream);
1016 runtime->hw_ptr_jiffies = jiffies;
1017 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
1019 runtime->status->state = state;
1020 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1021 runtime->silence_size > 0)
1022 snd_pcm_playback_silence(substream, ULONG_MAX);
1023 if (substream->timer)
1024 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
1025 &runtime->trigger_tstamp);
1028 static struct action_ops snd_pcm_action_start = {
1029 .pre_action = snd_pcm_pre_start,
1030 .do_action = snd_pcm_do_start,
1031 .undo_action = snd_pcm_undo_start,
1032 .post_action = snd_pcm_post_start
1036 * snd_pcm_start - start all linked streams
1037 * @substream: the PCM substream instance
1039 * Return: Zero if successful, or a negative error code.
1041 int snd_pcm_start(struct snd_pcm_substream *substream)
1043 return snd_pcm_action(&snd_pcm_action_start, substream,
1044 SNDRV_PCM_STATE_RUNNING);
1050 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1052 struct snd_pcm_runtime *runtime = substream->runtime;
1053 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1055 runtime->trigger_master = substream;
1059 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1061 if (substream->runtime->trigger_master == substream &&
1062 snd_pcm_running(substream))
1063 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
1064 return 0; /* unconditonally stop all substreams */
1067 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1069 struct snd_pcm_runtime *runtime = substream->runtime;
1070 if (runtime->status->state != state) {
1071 snd_pcm_trigger_tstamp(substream);
1072 if (substream->timer)
1073 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
1074 &runtime->trigger_tstamp);
1075 runtime->status->state = state;
1077 wake_up(&runtime->sleep);
1078 wake_up(&runtime->tsleep);
1081 static struct action_ops snd_pcm_action_stop = {
1082 .pre_action = snd_pcm_pre_stop,
1083 .do_action = snd_pcm_do_stop,
1084 .post_action = snd_pcm_post_stop
1088 * snd_pcm_stop - try to stop all running streams in the substream group
1089 * @substream: the PCM substream instance
1090 * @state: PCM state after stopping the stream
1092 * The state of each stream is then changed to the given state unconditionally.
1094 * Return: Zero if successful, or a negative error code.
1096 int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
1098 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
1101 EXPORT_SYMBOL(snd_pcm_stop);
1104 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
1105 * @substream: the PCM substream
1107 * After stopping, the state is changed to SETUP.
1108 * Unlike snd_pcm_stop(), this affects only the given stream.
1110 * Return: Zero if succesful, or a negative error code.
1112 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1114 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
1115 SNDRV_PCM_STATE_SETUP);
1121 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1123 struct snd_pcm_runtime *runtime = substream->runtime;
1124 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1127 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1129 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1131 runtime->trigger_master = substream;
1135 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1137 if (substream->runtime->trigger_master != substream)
1139 /* some drivers might use hw_ptr to recover from the pause -
1140 update the hw_ptr now */
1142 snd_pcm_update_hw_ptr(substream);
1143 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1144 * a delta between the current jiffies, this gives a large enough
1145 * delta, effectively to skip the check once.
1147 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
1148 return substream->ops->trigger(substream,
1149 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1150 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1153 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1155 if (substream->runtime->trigger_master == substream)
1156 substream->ops->trigger(substream,
1157 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1158 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1161 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1163 struct snd_pcm_runtime *runtime = substream->runtime;
1164 snd_pcm_trigger_tstamp(substream);
1166 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1167 if (substream->timer)
1168 snd_timer_notify(substream->timer,
1169 SNDRV_TIMER_EVENT_MPAUSE,
1170 &runtime->trigger_tstamp);
1171 wake_up(&runtime->sleep);
1172 wake_up(&runtime->tsleep);
1174 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1175 if (substream->timer)
1176 snd_timer_notify(substream->timer,
1177 SNDRV_TIMER_EVENT_MCONTINUE,
1178 &runtime->trigger_tstamp);
1182 static struct action_ops snd_pcm_action_pause = {
1183 .pre_action = snd_pcm_pre_pause,
1184 .do_action = snd_pcm_do_pause,
1185 .undo_action = snd_pcm_undo_pause,
1186 .post_action = snd_pcm_post_pause
1190 * Push/release the pause for all linked streams.
1192 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1194 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1200 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1205 runtime->trigger_master = substream;
1209 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1211 struct snd_pcm_runtime *runtime = substream->runtime;
1212 if (runtime->trigger_master != substream)
1214 if (! snd_pcm_running(substream))
1216 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1217 return 0; /* suspend unconditionally */
1220 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1222 struct snd_pcm_runtime *runtime = substream->runtime;
1223 snd_pcm_trigger_tstamp(substream);
1224 if (substream->timer)
1225 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1226 &runtime->trigger_tstamp);
1227 runtime->status->suspended_state = runtime->status->state;
1228 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1229 wake_up(&runtime->sleep);
1230 wake_up(&runtime->tsleep);
1233 static struct action_ops snd_pcm_action_suspend = {
1234 .pre_action = snd_pcm_pre_suspend,
1235 .do_action = snd_pcm_do_suspend,
1236 .post_action = snd_pcm_post_suspend
1240 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1241 * @substream: the PCM substream
1243 * After this call, all streams are changed to SUSPENDED state.
1245 * Return: Zero if successful (or @substream is %NULL), or a negative error
1248 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1251 unsigned long flags;
1256 snd_pcm_stream_lock_irqsave(substream, flags);
1257 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1258 snd_pcm_stream_unlock_irqrestore(substream, flags);
1262 EXPORT_SYMBOL(snd_pcm_suspend);
1265 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1266 * @pcm: the PCM instance
1268 * After this call, all streams are changed to SUSPENDED state.
1270 * Return: Zero if successful (or @pcm is %NULL), or a negative error code.
1272 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1274 struct snd_pcm_substream *substream;
1275 int stream, err = 0;
1280 for (stream = 0; stream < 2; stream++) {
1281 for (substream = pcm->streams[stream].substream;
1282 substream; substream = substream->next) {
1283 /* FIXME: the open/close code should lock this as well */
1284 if (substream->runtime == NULL)
1286 err = snd_pcm_suspend(substream);
1287 if (err < 0 && err != -EBUSY)
1294 EXPORT_SYMBOL(snd_pcm_suspend_all);
1298 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1300 struct snd_pcm_runtime *runtime = substream->runtime;
1301 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1303 runtime->trigger_master = substream;
1307 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1309 struct snd_pcm_runtime *runtime = substream->runtime;
1310 if (runtime->trigger_master != substream)
1312 /* DMA not running previously? */
1313 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1314 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1315 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1317 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1320 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1322 if (substream->runtime->trigger_master == substream &&
1323 snd_pcm_running(substream))
1324 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1327 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1329 struct snd_pcm_runtime *runtime = substream->runtime;
1330 snd_pcm_trigger_tstamp(substream);
1331 if (substream->timer)
1332 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1333 &runtime->trigger_tstamp);
1334 runtime->status->state = runtime->status->suspended_state;
1337 static struct action_ops snd_pcm_action_resume = {
1338 .pre_action = snd_pcm_pre_resume,
1339 .do_action = snd_pcm_do_resume,
1340 .undo_action = snd_pcm_undo_resume,
1341 .post_action = snd_pcm_post_resume
1344 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1346 struct snd_card *card = substream->pcm->card;
1349 snd_power_lock(card);
1350 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1351 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1352 snd_power_unlock(card);
1358 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1363 #endif /* CONFIG_PM */
1368 * Change the RUNNING stream(s) to XRUN state.
1370 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1372 struct snd_card *card = substream->pcm->card;
1373 struct snd_pcm_runtime *runtime = substream->runtime;
1376 snd_power_lock(card);
1377 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1378 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1383 snd_pcm_stream_lock_irq(substream);
1384 switch (runtime->status->state) {
1385 case SNDRV_PCM_STATE_XRUN:
1386 result = 0; /* already there */
1388 case SNDRV_PCM_STATE_RUNNING:
1389 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1394 snd_pcm_stream_unlock_irq(substream);
1396 snd_power_unlock(card);
1403 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1405 struct snd_pcm_runtime *runtime = substream->runtime;
1406 switch (runtime->status->state) {
1407 case SNDRV_PCM_STATE_RUNNING:
1408 case SNDRV_PCM_STATE_PREPARED:
1409 case SNDRV_PCM_STATE_PAUSED:
1410 case SNDRV_PCM_STATE_SUSPENDED:
1417 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1419 struct snd_pcm_runtime *runtime = substream->runtime;
1420 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1423 runtime->hw_ptr_base = 0;
1424 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1425 runtime->status->hw_ptr % runtime->period_size;
1426 runtime->silence_start = runtime->status->hw_ptr;
1427 runtime->silence_filled = 0;
1431 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1433 struct snd_pcm_runtime *runtime = substream->runtime;
1434 runtime->control->appl_ptr = runtime->status->hw_ptr;
1435 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1436 runtime->silence_size > 0)
1437 snd_pcm_playback_silence(substream, ULONG_MAX);
1440 static struct action_ops snd_pcm_action_reset = {
1441 .pre_action = snd_pcm_pre_reset,
1442 .do_action = snd_pcm_do_reset,
1443 .post_action = snd_pcm_post_reset
1446 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1448 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1454 /* we use the second argument for updating f_flags */
1455 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1458 struct snd_pcm_runtime *runtime = substream->runtime;
1459 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1460 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1462 if (snd_pcm_running(substream))
1464 substream->f_flags = f_flags;
1468 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1471 err = substream->ops->prepare(substream);
1474 return snd_pcm_do_reset(substream, 0);
1477 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1479 struct snd_pcm_runtime *runtime = substream->runtime;
1480 runtime->control->appl_ptr = runtime->status->hw_ptr;
1481 snd_pcm_set_state(substream, SNDRV_PCM_STATE_PREPARED);
1484 static struct action_ops snd_pcm_action_prepare = {
1485 .pre_action = snd_pcm_pre_prepare,
1486 .do_action = snd_pcm_do_prepare,
1487 .post_action = snd_pcm_post_prepare
1491 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1492 * @substream: the PCM substream instance
1493 * @file: file to refer f_flags
1495 * Return: Zero if successful, or a negative error code.
1497 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1501 struct snd_card *card = substream->pcm->card;
1505 f_flags = file->f_flags;
1507 f_flags = substream->f_flags;
1509 snd_power_lock(card);
1510 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1511 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1512 substream, f_flags);
1513 snd_power_unlock(card);
1521 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1523 struct snd_pcm_runtime *runtime = substream->runtime;
1524 switch (runtime->status->state) {
1525 case SNDRV_PCM_STATE_OPEN:
1526 case SNDRV_PCM_STATE_DISCONNECTED:
1527 case SNDRV_PCM_STATE_SUSPENDED:
1530 runtime->trigger_master = substream;
1534 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1536 struct snd_pcm_runtime *runtime = substream->runtime;
1537 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1538 switch (runtime->status->state) {
1539 case SNDRV_PCM_STATE_PREPARED:
1540 /* start playback stream if possible */
1541 if (! snd_pcm_playback_empty(substream)) {
1542 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1543 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1546 case SNDRV_PCM_STATE_RUNNING:
1547 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1549 case SNDRV_PCM_STATE_XRUN:
1550 runtime->status->state = SNDRV_PCM_STATE_SETUP;
1556 /* stop running stream */
1557 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1558 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1559 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1560 snd_pcm_do_stop(substream, new_state);
1561 snd_pcm_post_stop(substream, new_state);
1567 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1571 static struct action_ops snd_pcm_action_drain_init = {
1572 .pre_action = snd_pcm_pre_drain_init,
1573 .do_action = snd_pcm_do_drain_init,
1574 .post_action = snd_pcm_post_drain_init
1577 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1580 * Drain the stream(s).
1581 * When the substream is linked, sync until the draining of all playback streams
1583 * After this call, all streams are supposed to be either SETUP or DRAINING
1584 * (capture only) state.
1586 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1589 struct snd_card *card;
1590 struct snd_pcm_runtime *runtime;
1591 struct snd_pcm_substream *s;
1596 card = substream->pcm->card;
1597 runtime = substream->runtime;
1599 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1602 snd_power_lock(card);
1603 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1604 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1606 snd_power_unlock(card);
1612 if (file->f_flags & O_NONBLOCK)
1614 } else if (substream->f_flags & O_NONBLOCK)
1617 down_read(&snd_pcm_link_rwsem);
1618 snd_pcm_stream_lock_irq(substream);
1620 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1621 snd_pcm_pause(substream, 0);
1623 /* pre-start/stop - all running streams are changed to DRAINING state */
1624 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1627 /* in non-blocking, we don't wait in ioctl but let caller poll */
1635 struct snd_pcm_runtime *to_check;
1636 if (signal_pending(current)) {
1637 result = -ERESTARTSYS;
1640 /* find a substream to drain */
1642 snd_pcm_group_for_each_entry(s, substream) {
1643 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1645 runtime = s->runtime;
1646 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1652 break; /* all drained */
1653 init_waitqueue_entry(&wait, current);
1654 add_wait_queue(&to_check->sleep, &wait);
1655 snd_pcm_stream_unlock_irq(substream);
1656 up_read(&snd_pcm_link_rwsem);
1657 snd_power_unlock(card);
1658 if (runtime->no_period_wakeup)
1659 tout = MAX_SCHEDULE_TIMEOUT;
1662 if (runtime->rate) {
1663 long t = runtime->period_size * 2 / runtime->rate;
1664 tout = max(t, tout);
1666 tout = msecs_to_jiffies(tout * 1000);
1668 tout = schedule_timeout_interruptible(tout);
1669 snd_power_lock(card);
1670 down_read(&snd_pcm_link_rwsem);
1671 snd_pcm_stream_lock_irq(substream);
1672 remove_wait_queue(&to_check->sleep, &wait);
1673 if (card->shutdown) {
1678 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1681 dev_dbg(substream->pcm->card->dev,
1682 "playback drain error (DMA or IRQ trouble?)\n");
1683 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1691 snd_pcm_stream_unlock_irq(substream);
1692 up_read(&snd_pcm_link_rwsem);
1693 snd_power_unlock(card);
1701 * Immediately put all linked substreams into SETUP state.
1703 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1705 struct snd_pcm_runtime *runtime;
1708 if (PCM_RUNTIME_CHECK(substream))
1710 runtime = substream->runtime;
1712 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1713 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1714 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1717 snd_pcm_stream_lock_irq(substream);
1719 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1720 snd_pcm_pause(substream, 0);
1722 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1723 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1724 snd_pcm_stream_unlock_irq(substream);
1730 static bool is_pcm_file(struct file *file)
1732 struct inode *inode = file_inode(file);
1735 if (!S_ISCHR(inode->i_mode) || imajor(inode) != snd_major)
1737 minor = iminor(inode);
1738 return snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) ||
1739 snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE);
1745 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1748 struct snd_pcm_file *pcm_file;
1749 struct snd_pcm_substream *substream1;
1750 struct snd_pcm_group *group;
1751 struct fd f = fdget(fd);
1755 if (!is_pcm_file(f.file)) {
1759 pcm_file = f.file->private_data;
1760 substream1 = pcm_file->substream;
1761 group = kmalloc(sizeof(*group), GFP_KERNEL);
1766 down_write(&snd_pcm_link_rwsem);
1767 write_lock_irq(&snd_pcm_link_rwlock);
1768 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1769 substream->runtime->status->state != substream1->runtime->status->state ||
1770 substream->pcm->nonatomic != substream1->pcm->nonatomic) {
1774 if (snd_pcm_stream_linked(substream1)) {
1778 if (!snd_pcm_stream_linked(substream)) {
1779 substream->group = group;
1781 spin_lock_init(&substream->group->lock);
1782 mutex_init(&substream->group->mutex);
1783 INIT_LIST_HEAD(&substream->group->substreams);
1784 list_add_tail(&substream->link_list, &substream->group->substreams);
1785 substream->group->count = 1;
1787 list_add_tail(&substream1->link_list, &substream->group->substreams);
1788 substream->group->count++;
1789 substream1->group = substream->group;
1791 write_unlock_irq(&snd_pcm_link_rwlock);
1792 up_write(&snd_pcm_link_rwsem);
1794 snd_card_unref(substream1->pcm->card);
1801 static void relink_to_local(struct snd_pcm_substream *substream)
1803 substream->group = &substream->self_group;
1804 INIT_LIST_HEAD(&substream->self_group.substreams);
1805 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1808 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1810 struct snd_pcm_substream *s;
1813 down_write(&snd_pcm_link_rwsem);
1814 write_lock_irq(&snd_pcm_link_rwlock);
1815 if (!snd_pcm_stream_linked(substream)) {
1819 list_del(&substream->link_list);
1820 substream->group->count--;
1821 if (substream->group->count == 1) { /* detach the last stream, too */
1822 snd_pcm_group_for_each_entry(s, substream) {
1826 kfree(substream->group);
1828 relink_to_local(substream);
1830 write_unlock_irq(&snd_pcm_link_rwlock);
1831 up_write(&snd_pcm_link_rwsem);
1838 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1839 struct snd_pcm_hw_rule *rule)
1841 struct snd_interval t;
1842 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1843 hw_param_interval_c(params, rule->deps[1]), &t);
1844 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1847 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1848 struct snd_pcm_hw_rule *rule)
1850 struct snd_interval t;
1851 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1852 hw_param_interval_c(params, rule->deps[1]), &t);
1853 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1856 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1857 struct snd_pcm_hw_rule *rule)
1859 struct snd_interval t;
1860 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1861 hw_param_interval_c(params, rule->deps[1]),
1862 (unsigned long) rule->private, &t);
1863 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1866 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1867 struct snd_pcm_hw_rule *rule)
1869 struct snd_interval t;
1870 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1871 (unsigned long) rule->private,
1872 hw_param_interval_c(params, rule->deps[1]), &t);
1873 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1876 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1877 struct snd_pcm_hw_rule *rule)
1880 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1882 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1884 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1886 if (! snd_mask_test(mask, k))
1888 bits = snd_pcm_format_physical_width(k);
1890 continue; /* ignore invalid formats */
1891 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1892 snd_mask_reset(&m, k);
1894 return snd_mask_refine(mask, &m);
1897 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1898 struct snd_pcm_hw_rule *rule)
1900 struct snd_interval t;
1906 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1908 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1910 bits = snd_pcm_format_physical_width(k);
1912 continue; /* ignore invalid formats */
1913 if (t.min > (unsigned)bits)
1915 if (t.max < (unsigned)bits)
1919 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1922 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1923 #error "Change this table"
1926 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1927 48000, 64000, 88200, 96000, 176400, 192000 };
1929 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1930 .count = ARRAY_SIZE(rates),
1934 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1935 struct snd_pcm_hw_rule *rule)
1937 struct snd_pcm_hardware *hw = rule->private;
1938 return snd_interval_list(hw_param_interval(params, rule->var),
1939 snd_pcm_known_rates.count,
1940 snd_pcm_known_rates.list, hw->rates);
1943 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1944 struct snd_pcm_hw_rule *rule)
1946 struct snd_interval t;
1947 struct snd_pcm_substream *substream = rule->private;
1949 t.max = substream->buffer_bytes_max;
1953 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1956 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1958 struct snd_pcm_runtime *runtime = substream->runtime;
1959 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1962 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1963 snd_mask_any(constrs_mask(constrs, k));
1966 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1967 snd_interval_any(constrs_interval(constrs, k));
1970 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1971 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1972 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1973 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1974 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1976 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1977 snd_pcm_hw_rule_format, NULL,
1978 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1981 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1982 snd_pcm_hw_rule_sample_bits, NULL,
1983 SNDRV_PCM_HW_PARAM_FORMAT,
1984 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1987 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1988 snd_pcm_hw_rule_div, NULL,
1989 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1992 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1993 snd_pcm_hw_rule_mul, NULL,
1994 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1997 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1998 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1999 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2002 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
2003 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2004 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
2007 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2008 snd_pcm_hw_rule_div, NULL,
2009 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
2012 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2013 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2014 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
2017 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2018 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2019 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
2022 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
2023 snd_pcm_hw_rule_div, NULL,
2024 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
2027 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2028 snd_pcm_hw_rule_div, NULL,
2029 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2032 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2033 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2034 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2037 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
2038 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2039 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2042 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2043 snd_pcm_hw_rule_mul, NULL,
2044 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
2047 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2048 snd_pcm_hw_rule_mulkdiv, (void*) 8,
2049 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2052 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
2053 snd_pcm_hw_rule_muldivk, (void*) 1000000,
2054 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
2057 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2058 snd_pcm_hw_rule_muldivk, (void*) 8,
2059 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2062 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2063 snd_pcm_hw_rule_muldivk, (void*) 8,
2064 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
2067 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
2068 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2069 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2072 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
2073 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
2074 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
2080 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
2082 struct snd_pcm_runtime *runtime = substream->runtime;
2083 struct snd_pcm_hardware *hw = &runtime->hw;
2085 unsigned int mask = 0;
2087 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2088 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
2089 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2090 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
2091 if (hw_support_mmap(substream)) {
2092 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
2093 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
2094 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
2095 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
2096 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
2097 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
2099 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
2103 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
2107 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
2111 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
2112 hw->channels_min, hw->channels_max);
2116 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
2117 hw->rate_min, hw->rate_max);
2121 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
2122 hw->period_bytes_min, hw->period_bytes_max);
2126 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
2127 hw->periods_min, hw->periods_max);
2131 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2132 hw->period_bytes_min, hw->buffer_bytes_max);
2136 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
2137 snd_pcm_hw_rule_buffer_bytes_max, substream,
2138 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
2143 if (runtime->dma_bytes) {
2144 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2149 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2150 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2151 snd_pcm_hw_rule_rate, hw,
2152 SNDRV_PCM_HW_PARAM_RATE, -1);
2157 /* FIXME: this belong to lowlevel */
2158 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2163 static void pcm_release_private(struct snd_pcm_substream *substream)
2165 snd_pcm_unlink(substream);
2168 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2170 substream->ref_count--;
2171 if (substream->ref_count > 0)
2174 snd_pcm_drop(substream);
2175 if (substream->hw_opened) {
2176 if (substream->ops->hw_free != NULL)
2177 substream->ops->hw_free(substream);
2178 substream->ops->close(substream);
2179 substream->hw_opened = 0;
2181 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2182 pm_qos_remove_request(&substream->latency_pm_qos_req);
2183 if (substream->pcm_release) {
2184 substream->pcm_release(substream);
2185 substream->pcm_release = NULL;
2187 snd_pcm_detach_substream(substream);
2190 EXPORT_SYMBOL(snd_pcm_release_substream);
2192 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2194 struct snd_pcm_substream **rsubstream)
2196 struct snd_pcm_substream *substream;
2199 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2202 if (substream->ref_count > 1) {
2203 *rsubstream = substream;
2207 err = snd_pcm_hw_constraints_init(substream);
2209 pcm_dbg(pcm, "snd_pcm_hw_constraints_init failed\n");
2213 if ((err = substream->ops->open(substream)) < 0)
2216 substream->hw_opened = 1;
2218 err = snd_pcm_hw_constraints_complete(substream);
2220 pcm_dbg(pcm, "snd_pcm_hw_constraints_complete failed\n");
2224 *rsubstream = substream;
2228 snd_pcm_release_substream(substream);
2232 EXPORT_SYMBOL(snd_pcm_open_substream);
2234 static int snd_pcm_open_file(struct file *file,
2235 struct snd_pcm *pcm,
2238 struct snd_pcm_file *pcm_file;
2239 struct snd_pcm_substream *substream;
2242 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2246 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2247 if (pcm_file == NULL) {
2248 snd_pcm_release_substream(substream);
2251 pcm_file->substream = substream;
2252 if (substream->ref_count == 1) {
2253 substream->file = pcm_file;
2254 substream->pcm_release = pcm_release_private;
2256 file->private_data = pcm_file;
2261 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2263 struct snd_pcm *pcm;
2264 int err = nonseekable_open(inode, file);
2267 pcm = snd_lookup_minor_data(iminor(inode),
2268 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2269 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2271 snd_card_unref(pcm->card);
2275 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2277 struct snd_pcm *pcm;
2278 int err = nonseekable_open(inode, file);
2281 pcm = snd_lookup_minor_data(iminor(inode),
2282 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2283 err = snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2285 snd_card_unref(pcm->card);
2289 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2298 err = snd_card_file_add(pcm->card, file);
2301 if (!try_module_get(pcm->card->module)) {
2305 init_waitqueue_entry(&wait, current);
2306 add_wait_queue(&pcm->open_wait, &wait);
2307 mutex_lock(&pcm->open_mutex);
2309 err = snd_pcm_open_file(file, pcm, stream);
2312 if (err == -EAGAIN) {
2313 if (file->f_flags & O_NONBLOCK) {
2319 set_current_state(TASK_INTERRUPTIBLE);
2320 mutex_unlock(&pcm->open_mutex);
2322 mutex_lock(&pcm->open_mutex);
2323 if (pcm->card->shutdown) {
2327 if (signal_pending(current)) {
2332 remove_wait_queue(&pcm->open_wait, &wait);
2333 mutex_unlock(&pcm->open_mutex);
2339 module_put(pcm->card->module);
2341 snd_card_file_remove(pcm->card, file);
2346 static int snd_pcm_release(struct inode *inode, struct file *file)
2348 struct snd_pcm *pcm;
2349 struct snd_pcm_substream *substream;
2350 struct snd_pcm_file *pcm_file;
2352 pcm_file = file->private_data;
2353 substream = pcm_file->substream;
2354 if (snd_BUG_ON(!substream))
2356 pcm = substream->pcm;
2357 mutex_lock(&pcm->open_mutex);
2358 snd_pcm_release_substream(substream);
2360 mutex_unlock(&pcm->open_mutex);
2361 wake_up(&pcm->open_wait);
2362 module_put(pcm->card->module);
2363 snd_card_file_remove(pcm->card, file);
2367 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2368 snd_pcm_uframes_t frames)
2370 struct snd_pcm_runtime *runtime = substream->runtime;
2371 snd_pcm_sframes_t appl_ptr;
2372 snd_pcm_sframes_t ret;
2373 snd_pcm_sframes_t hw_avail;
2378 snd_pcm_stream_lock_irq(substream);
2379 switch (runtime->status->state) {
2380 case SNDRV_PCM_STATE_PREPARED:
2382 case SNDRV_PCM_STATE_DRAINING:
2383 case SNDRV_PCM_STATE_RUNNING:
2384 if (snd_pcm_update_hw_ptr(substream) >= 0)
2387 case SNDRV_PCM_STATE_XRUN:
2390 case SNDRV_PCM_STATE_SUSPENDED:
2398 hw_avail = snd_pcm_playback_hw_avail(runtime);
2399 if (hw_avail <= 0) {
2403 if (frames > (snd_pcm_uframes_t)hw_avail)
2405 appl_ptr = runtime->control->appl_ptr - frames;
2407 appl_ptr += runtime->boundary;
2408 runtime->control->appl_ptr = appl_ptr;
2411 snd_pcm_stream_unlock_irq(substream);
2415 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2416 snd_pcm_uframes_t frames)
2418 struct snd_pcm_runtime *runtime = substream->runtime;
2419 snd_pcm_sframes_t appl_ptr;
2420 snd_pcm_sframes_t ret;
2421 snd_pcm_sframes_t hw_avail;
2426 snd_pcm_stream_lock_irq(substream);
2427 switch (runtime->status->state) {
2428 case SNDRV_PCM_STATE_PREPARED:
2429 case SNDRV_PCM_STATE_DRAINING:
2431 case SNDRV_PCM_STATE_RUNNING:
2432 if (snd_pcm_update_hw_ptr(substream) >= 0)
2435 case SNDRV_PCM_STATE_XRUN:
2438 case SNDRV_PCM_STATE_SUSPENDED:
2446 hw_avail = snd_pcm_capture_hw_avail(runtime);
2447 if (hw_avail <= 0) {
2451 if (frames > (snd_pcm_uframes_t)hw_avail)
2453 appl_ptr = runtime->control->appl_ptr - frames;
2455 appl_ptr += runtime->boundary;
2456 runtime->control->appl_ptr = appl_ptr;
2459 snd_pcm_stream_unlock_irq(substream);
2463 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2464 snd_pcm_uframes_t frames)
2466 struct snd_pcm_runtime *runtime = substream->runtime;
2467 snd_pcm_sframes_t appl_ptr;
2468 snd_pcm_sframes_t ret;
2469 snd_pcm_sframes_t avail;
2474 snd_pcm_stream_lock_irq(substream);
2475 switch (runtime->status->state) {
2476 case SNDRV_PCM_STATE_PREPARED:
2477 case SNDRV_PCM_STATE_PAUSED:
2479 case SNDRV_PCM_STATE_DRAINING:
2480 case SNDRV_PCM_STATE_RUNNING:
2481 if (snd_pcm_update_hw_ptr(substream) >= 0)
2484 case SNDRV_PCM_STATE_XRUN:
2487 case SNDRV_PCM_STATE_SUSPENDED:
2495 avail = snd_pcm_playback_avail(runtime);
2500 if (frames > (snd_pcm_uframes_t)avail)
2502 appl_ptr = runtime->control->appl_ptr + frames;
2503 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2504 appl_ptr -= runtime->boundary;
2505 runtime->control->appl_ptr = appl_ptr;
2508 snd_pcm_stream_unlock_irq(substream);
2512 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2513 snd_pcm_uframes_t frames)
2515 struct snd_pcm_runtime *runtime = substream->runtime;
2516 snd_pcm_sframes_t appl_ptr;
2517 snd_pcm_sframes_t ret;
2518 snd_pcm_sframes_t avail;
2523 snd_pcm_stream_lock_irq(substream);
2524 switch (runtime->status->state) {
2525 case SNDRV_PCM_STATE_PREPARED:
2526 case SNDRV_PCM_STATE_DRAINING:
2527 case SNDRV_PCM_STATE_PAUSED:
2529 case SNDRV_PCM_STATE_RUNNING:
2530 if (snd_pcm_update_hw_ptr(substream) >= 0)
2533 case SNDRV_PCM_STATE_XRUN:
2536 case SNDRV_PCM_STATE_SUSPENDED:
2544 avail = snd_pcm_capture_avail(runtime);
2549 if (frames > (snd_pcm_uframes_t)avail)
2551 appl_ptr = runtime->control->appl_ptr + frames;
2552 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2553 appl_ptr -= runtime->boundary;
2554 runtime->control->appl_ptr = appl_ptr;
2557 snd_pcm_stream_unlock_irq(substream);
2561 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2563 struct snd_pcm_runtime *runtime = substream->runtime;
2566 snd_pcm_stream_lock_irq(substream);
2567 switch (runtime->status->state) {
2568 case SNDRV_PCM_STATE_DRAINING:
2569 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2572 case SNDRV_PCM_STATE_RUNNING:
2573 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2576 case SNDRV_PCM_STATE_PREPARED:
2577 case SNDRV_PCM_STATE_SUSPENDED:
2580 case SNDRV_PCM_STATE_XRUN:
2588 snd_pcm_stream_unlock_irq(substream);
2592 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2593 snd_pcm_sframes_t __user *res)
2595 struct snd_pcm_runtime *runtime = substream->runtime;
2597 snd_pcm_sframes_t n = 0;
2599 snd_pcm_stream_lock_irq(substream);
2600 switch (runtime->status->state) {
2601 case SNDRV_PCM_STATE_DRAINING:
2602 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2605 case SNDRV_PCM_STATE_RUNNING:
2606 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2609 case SNDRV_PCM_STATE_PREPARED:
2610 case SNDRV_PCM_STATE_SUSPENDED:
2612 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2613 n = snd_pcm_playback_hw_avail(runtime);
2615 n = snd_pcm_capture_avail(runtime);
2616 n += runtime->delay;
2618 case SNDRV_PCM_STATE_XRUN:
2626 snd_pcm_stream_unlock_irq(substream);
2628 if (put_user(n, res))
2633 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2634 struct snd_pcm_sync_ptr __user *_sync_ptr)
2636 struct snd_pcm_runtime *runtime = substream->runtime;
2637 struct snd_pcm_sync_ptr sync_ptr;
2638 volatile struct snd_pcm_mmap_status *status;
2639 volatile struct snd_pcm_mmap_control *control;
2642 memset(&sync_ptr, 0, sizeof(sync_ptr));
2643 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2645 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2647 status = runtime->status;
2648 control = runtime->control;
2649 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2650 err = snd_pcm_hwsync(substream);
2654 snd_pcm_stream_lock_irq(substream);
2655 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2656 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2658 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2659 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2660 control->avail_min = sync_ptr.c.control.avail_min;
2662 sync_ptr.c.control.avail_min = control->avail_min;
2663 sync_ptr.s.status.state = status->state;
2664 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2665 sync_ptr.s.status.tstamp = status->tstamp;
2666 sync_ptr.s.status.suspended_state = status->suspended_state;
2667 snd_pcm_stream_unlock_irq(substream);
2668 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2673 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2675 struct snd_pcm_runtime *runtime = substream->runtime;
2678 if (get_user(arg, _arg))
2680 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2682 runtime->tstamp_type = arg;
2686 static int snd_pcm_common_ioctl1(struct file *file,
2687 struct snd_pcm_substream *substream,
2688 unsigned int cmd, void __user *arg)
2691 case SNDRV_PCM_IOCTL_PVERSION:
2692 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2693 case SNDRV_PCM_IOCTL_INFO:
2694 return snd_pcm_info_user(substream, arg);
2695 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2697 case SNDRV_PCM_IOCTL_TTSTAMP:
2698 return snd_pcm_tstamp(substream, arg);
2699 case SNDRV_PCM_IOCTL_HW_REFINE:
2700 return snd_pcm_hw_refine_user(substream, arg);
2701 case SNDRV_PCM_IOCTL_HW_PARAMS:
2702 return snd_pcm_hw_params_user(substream, arg);
2703 case SNDRV_PCM_IOCTL_HW_FREE:
2704 return snd_pcm_hw_free(substream);
2705 case SNDRV_PCM_IOCTL_SW_PARAMS:
2706 return snd_pcm_sw_params_user(substream, arg);
2707 case SNDRV_PCM_IOCTL_STATUS:
2708 return snd_pcm_status_user(substream, arg);
2709 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2710 return snd_pcm_channel_info_user(substream, arg);
2711 case SNDRV_PCM_IOCTL_PREPARE:
2712 return snd_pcm_prepare(substream, file);
2713 case SNDRV_PCM_IOCTL_RESET:
2714 return snd_pcm_reset(substream);
2715 case SNDRV_PCM_IOCTL_START:
2716 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2717 case SNDRV_PCM_IOCTL_LINK:
2718 return snd_pcm_link(substream, (int)(unsigned long) arg);
2719 case SNDRV_PCM_IOCTL_UNLINK:
2720 return snd_pcm_unlink(substream);
2721 case SNDRV_PCM_IOCTL_RESUME:
2722 return snd_pcm_resume(substream);
2723 case SNDRV_PCM_IOCTL_XRUN:
2724 return snd_pcm_xrun(substream);
2725 case SNDRV_PCM_IOCTL_HWSYNC:
2726 return snd_pcm_hwsync(substream);
2727 case SNDRV_PCM_IOCTL_DELAY:
2728 return snd_pcm_delay(substream, arg);
2729 case SNDRV_PCM_IOCTL_SYNC_PTR:
2730 return snd_pcm_sync_ptr(substream, arg);
2731 #ifdef CONFIG_SND_SUPPORT_OLD_API
2732 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2733 return snd_pcm_hw_refine_old_user(substream, arg);
2734 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2735 return snd_pcm_hw_params_old_user(substream, arg);
2737 case SNDRV_PCM_IOCTL_DRAIN:
2738 return snd_pcm_drain(substream, file);
2739 case SNDRV_PCM_IOCTL_DROP:
2740 return snd_pcm_drop(substream);
2741 case SNDRV_PCM_IOCTL_PAUSE:
2744 snd_pcm_stream_lock_irq(substream);
2745 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2746 snd_pcm_stream_unlock_irq(substream);
2750 pcm_dbg(substream->pcm, "unknown ioctl = 0x%x\n", cmd);
2754 static int snd_pcm_playback_ioctl1(struct file *file,
2755 struct snd_pcm_substream *substream,
2756 unsigned int cmd, void __user *arg)
2758 if (snd_BUG_ON(!substream))
2760 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2763 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2765 struct snd_xferi xferi;
2766 struct snd_xferi __user *_xferi = arg;
2767 struct snd_pcm_runtime *runtime = substream->runtime;
2768 snd_pcm_sframes_t result;
2769 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2771 if (put_user(0, &_xferi->result))
2773 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2775 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2776 __put_user(result, &_xferi->result);
2777 return result < 0 ? result : 0;
2779 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2781 struct snd_xfern xfern;
2782 struct snd_xfern __user *_xfern = arg;
2783 struct snd_pcm_runtime *runtime = substream->runtime;
2785 snd_pcm_sframes_t result;
2786 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2788 if (runtime->channels > 128)
2790 if (put_user(0, &_xfern->result))
2792 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2795 bufs = memdup_user(xfern.bufs,
2796 sizeof(void *) * runtime->channels);
2798 return PTR_ERR(bufs);
2799 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2801 __put_user(result, &_xfern->result);
2802 return result < 0 ? result : 0;
2804 case SNDRV_PCM_IOCTL_REWIND:
2806 snd_pcm_uframes_t frames;
2807 snd_pcm_uframes_t __user *_frames = arg;
2808 snd_pcm_sframes_t result;
2809 if (get_user(frames, _frames))
2811 if (put_user(0, _frames))
2813 result = snd_pcm_playback_rewind(substream, frames);
2814 __put_user(result, _frames);
2815 return result < 0 ? result : 0;
2817 case SNDRV_PCM_IOCTL_FORWARD:
2819 snd_pcm_uframes_t frames;
2820 snd_pcm_uframes_t __user *_frames = arg;
2821 snd_pcm_sframes_t result;
2822 if (get_user(frames, _frames))
2824 if (put_user(0, _frames))
2826 result = snd_pcm_playback_forward(substream, frames);
2827 __put_user(result, _frames);
2828 return result < 0 ? result : 0;
2831 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2834 static int snd_pcm_capture_ioctl1(struct file *file,
2835 struct snd_pcm_substream *substream,
2836 unsigned int cmd, void __user *arg)
2838 if (snd_BUG_ON(!substream))
2840 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2843 case SNDRV_PCM_IOCTL_READI_FRAMES:
2845 struct snd_xferi xferi;
2846 struct snd_xferi __user *_xferi = arg;
2847 struct snd_pcm_runtime *runtime = substream->runtime;
2848 snd_pcm_sframes_t result;
2849 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2851 if (put_user(0, &_xferi->result))
2853 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2855 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2856 __put_user(result, &_xferi->result);
2857 return result < 0 ? result : 0;
2859 case SNDRV_PCM_IOCTL_READN_FRAMES:
2861 struct snd_xfern xfern;
2862 struct snd_xfern __user *_xfern = arg;
2863 struct snd_pcm_runtime *runtime = substream->runtime;
2865 snd_pcm_sframes_t result;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2868 if (runtime->channels > 128)
2870 if (put_user(0, &_xfern->result))
2872 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2875 bufs = memdup_user(xfern.bufs,
2876 sizeof(void *) * runtime->channels);
2878 return PTR_ERR(bufs);
2879 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2881 __put_user(result, &_xfern->result);
2882 return result < 0 ? result : 0;
2884 case SNDRV_PCM_IOCTL_REWIND:
2886 snd_pcm_uframes_t frames;
2887 snd_pcm_uframes_t __user *_frames = arg;
2888 snd_pcm_sframes_t result;
2889 if (get_user(frames, _frames))
2891 if (put_user(0, _frames))
2893 result = snd_pcm_capture_rewind(substream, frames);
2894 __put_user(result, _frames);
2895 return result < 0 ? result : 0;
2897 case SNDRV_PCM_IOCTL_FORWARD:
2899 snd_pcm_uframes_t frames;
2900 snd_pcm_uframes_t __user *_frames = arg;
2901 snd_pcm_sframes_t result;
2902 if (get_user(frames, _frames))
2904 if (put_user(0, _frames))
2906 result = snd_pcm_capture_forward(substream, frames);
2907 __put_user(result, _frames);
2908 return result < 0 ? result : 0;
2911 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2914 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2917 struct snd_pcm_file *pcm_file;
2919 pcm_file = file->private_data;
2921 if (((cmd >> 8) & 0xff) != 'A')
2924 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2925 (void __user *)arg);
2928 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2931 struct snd_pcm_file *pcm_file;
2933 pcm_file = file->private_data;
2935 if (((cmd >> 8) & 0xff) != 'A')
2938 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2939 (void __user *)arg);
2942 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2943 unsigned int cmd, void *arg)
2948 fs = snd_enter_user();
2949 switch (substream->stream) {
2950 case SNDRV_PCM_STREAM_PLAYBACK:
2951 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2952 (void __user *)arg);
2954 case SNDRV_PCM_STREAM_CAPTURE:
2955 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2956 (void __user *)arg);
2966 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2968 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2971 struct snd_pcm_file *pcm_file;
2972 struct snd_pcm_substream *substream;
2973 struct snd_pcm_runtime *runtime;
2974 snd_pcm_sframes_t result;
2976 pcm_file = file->private_data;
2977 substream = pcm_file->substream;
2978 if (PCM_RUNTIME_CHECK(substream))
2980 runtime = substream->runtime;
2981 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2983 if (!frame_aligned(runtime, count))
2985 count = bytes_to_frames(runtime, count);
2986 result = snd_pcm_lib_read(substream, buf, count);
2988 result = frames_to_bytes(runtime, result);
2992 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2993 size_t count, loff_t * offset)
2995 struct snd_pcm_file *pcm_file;
2996 struct snd_pcm_substream *substream;
2997 struct snd_pcm_runtime *runtime;
2998 snd_pcm_sframes_t result;
3000 pcm_file = file->private_data;
3001 substream = pcm_file->substream;
3002 if (PCM_RUNTIME_CHECK(substream))
3004 runtime = substream->runtime;
3005 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3007 if (!frame_aligned(runtime, count))
3009 count = bytes_to_frames(runtime, count);
3010 result = snd_pcm_lib_write(substream, buf, count);
3012 result = frames_to_bytes(runtime, result);
3016 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
3017 unsigned long nr_segs, loff_t pos)
3020 struct snd_pcm_file *pcm_file;
3021 struct snd_pcm_substream *substream;
3022 struct snd_pcm_runtime *runtime;
3023 snd_pcm_sframes_t result;
3026 snd_pcm_uframes_t frames;
3028 pcm_file = iocb->ki_filp->private_data;
3029 substream = pcm_file->substream;
3030 if (PCM_RUNTIME_CHECK(substream))
3032 runtime = substream->runtime;
3033 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3035 if (nr_segs > 1024 || nr_segs != runtime->channels)
3037 if (!frame_aligned(runtime, iov->iov_len))
3039 frames = bytes_to_samples(runtime, iov->iov_len);
3040 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3043 for (i = 0; i < nr_segs; ++i)
3044 bufs[i] = iov[i].iov_base;
3045 result = snd_pcm_lib_readv(substream, bufs, frames);
3047 result = frames_to_bytes(runtime, result);
3052 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
3053 unsigned long nr_segs, loff_t pos)
3055 struct snd_pcm_file *pcm_file;
3056 struct snd_pcm_substream *substream;
3057 struct snd_pcm_runtime *runtime;
3058 snd_pcm_sframes_t result;
3061 snd_pcm_uframes_t frames;
3063 pcm_file = iocb->ki_filp->private_data;
3064 substream = pcm_file->substream;
3065 if (PCM_RUNTIME_CHECK(substream))
3067 runtime = substream->runtime;
3068 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3070 if (nr_segs > 128 || nr_segs != runtime->channels ||
3071 !frame_aligned(runtime, iov->iov_len))
3073 frames = bytes_to_samples(runtime, iov->iov_len);
3074 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
3077 for (i = 0; i < nr_segs; ++i)
3078 bufs[i] = iov[i].iov_base;
3079 result = snd_pcm_lib_writev(substream, bufs, frames);
3081 result = frames_to_bytes(runtime, result);
3086 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
3088 struct snd_pcm_file *pcm_file;
3089 struct snd_pcm_substream *substream;
3090 struct snd_pcm_runtime *runtime;
3092 snd_pcm_uframes_t avail;
3094 pcm_file = file->private_data;
3096 substream = pcm_file->substream;
3097 if (PCM_RUNTIME_CHECK(substream))
3099 runtime = substream->runtime;
3101 poll_wait(file, &runtime->sleep, wait);
3103 snd_pcm_stream_lock_irq(substream);
3104 avail = snd_pcm_playback_avail(runtime);
3105 switch (runtime->status->state) {
3106 case SNDRV_PCM_STATE_RUNNING:
3107 case SNDRV_PCM_STATE_PREPARED:
3108 case SNDRV_PCM_STATE_PAUSED:
3109 if (avail >= runtime->control->avail_min) {
3110 mask = POLLOUT | POLLWRNORM;
3114 case SNDRV_PCM_STATE_DRAINING:
3118 mask = POLLOUT | POLLWRNORM | POLLERR;
3121 snd_pcm_stream_unlock_irq(substream);
3125 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3127 struct snd_pcm_file *pcm_file;
3128 struct snd_pcm_substream *substream;
3129 struct snd_pcm_runtime *runtime;
3131 snd_pcm_uframes_t avail;
3133 pcm_file = file->private_data;
3135 substream = pcm_file->substream;
3136 if (PCM_RUNTIME_CHECK(substream))
3138 runtime = substream->runtime;
3140 poll_wait(file, &runtime->sleep, wait);
3142 snd_pcm_stream_lock_irq(substream);
3143 avail = snd_pcm_capture_avail(runtime);
3144 switch (runtime->status->state) {
3145 case SNDRV_PCM_STATE_RUNNING:
3146 case SNDRV_PCM_STATE_PREPARED:
3147 case SNDRV_PCM_STATE_PAUSED:
3148 if (avail >= runtime->control->avail_min) {
3149 mask = POLLIN | POLLRDNORM;
3154 case SNDRV_PCM_STATE_DRAINING:
3156 mask = POLLIN | POLLRDNORM;
3161 mask = POLLIN | POLLRDNORM | POLLERR;
3164 snd_pcm_stream_unlock_irq(substream);
3173 * Only on coherent architectures, we can mmap the status and the control records
3174 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3176 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3178 * mmap status record
3180 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3181 struct vm_fault *vmf)
3183 struct snd_pcm_substream *substream = area->vm_private_data;
3184 struct snd_pcm_runtime *runtime;
3186 if (substream == NULL)
3187 return VM_FAULT_SIGBUS;
3188 runtime = substream->runtime;
3189 vmf->page = virt_to_page(runtime->status);
3190 get_page(vmf->page);
3194 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3196 .fault = snd_pcm_mmap_status_fault,
3199 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3200 struct vm_area_struct *area)
3203 if (!(area->vm_flags & VM_READ))
3205 size = area->vm_end - area->vm_start;
3206 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3208 area->vm_ops = &snd_pcm_vm_ops_status;
3209 area->vm_private_data = substream;
3210 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3215 * mmap control record
3217 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3218 struct vm_fault *vmf)
3220 struct snd_pcm_substream *substream = area->vm_private_data;
3221 struct snd_pcm_runtime *runtime;
3223 if (substream == NULL)
3224 return VM_FAULT_SIGBUS;
3225 runtime = substream->runtime;
3226 vmf->page = virt_to_page(runtime->control);
3227 get_page(vmf->page);
3231 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3233 .fault = snd_pcm_mmap_control_fault,
3236 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3237 struct vm_area_struct *area)
3240 if (!(area->vm_flags & VM_READ))
3242 size = area->vm_end - area->vm_start;
3243 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3245 area->vm_ops = &snd_pcm_vm_ops_control;
3246 area->vm_private_data = substream;
3247 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3250 #else /* ! coherent mmap */
3252 * don't support mmap for status and control records.
3254 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3255 struct vm_area_struct *area)
3259 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3260 struct vm_area_struct *area)
3264 #endif /* coherent mmap */
3266 static inline struct page *
3267 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3269 void *vaddr = substream->runtime->dma_area + ofs;
3270 return virt_to_page(vaddr);
3274 * fault callback for mmapping a RAM page
3276 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3277 struct vm_fault *vmf)
3279 struct snd_pcm_substream *substream = area->vm_private_data;
3280 struct snd_pcm_runtime *runtime;
3281 unsigned long offset;
3285 if (substream == NULL)
3286 return VM_FAULT_SIGBUS;
3287 runtime = substream->runtime;
3288 offset = vmf->pgoff << PAGE_SHIFT;
3289 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3290 if (offset > dma_bytes - PAGE_SIZE)
3291 return VM_FAULT_SIGBUS;
3292 if (substream->ops->page)
3293 page = substream->ops->page(substream, offset);
3295 page = snd_pcm_default_page_ops(substream, offset);
3297 return VM_FAULT_SIGBUS;
3303 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3304 .open = snd_pcm_mmap_data_open,
3305 .close = snd_pcm_mmap_data_close,
3308 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3309 .open = snd_pcm_mmap_data_open,
3310 .close = snd_pcm_mmap_data_close,
3311 .fault = snd_pcm_mmap_data_fault,
3315 * mmap the DMA buffer on RAM
3317 int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3318 struct vm_area_struct *area)
3320 area->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
3321 #ifdef CONFIG_GENERIC_ALLOCATOR
3322 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV_IRAM) {
3323 area->vm_page_prot = pgprot_writecombine(area->vm_page_prot);
3324 return remap_pfn_range(area, area->vm_start,
3325 substream->dma_buffer.addr >> PAGE_SHIFT,
3326 area->vm_end - area->vm_start, area->vm_page_prot);
3328 #endif /* CONFIG_GENERIC_ALLOCATOR */
3329 #ifndef CONFIG_X86 /* for avoiding warnings arch/x86/mm/pat.c */
3330 if (!substream->ops->page &&
3331 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3332 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3334 substream->runtime->dma_area,
3335 substream->runtime->dma_addr,
3336 area->vm_end - area->vm_start);
3337 #endif /* CONFIG_X86 */
3338 /* mmap with fault handler */
3339 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3342 EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
3345 * mmap the DMA buffer on I/O memory area
3347 #if SNDRV_PCM_INFO_MMAP_IOMEM
3348 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3349 struct vm_area_struct *area)
3351 struct snd_pcm_runtime *runtime = substream->runtime;;
3353 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3354 return vm_iomap_memory(area, runtime->dma_addr, runtime->dma_bytes);
3357 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3358 #endif /* SNDRV_PCM_INFO_MMAP */
3363 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3364 struct vm_area_struct *area)
3366 struct snd_pcm_runtime *runtime;
3368 unsigned long offset;
3372 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3373 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3376 if (!(area->vm_flags & VM_READ))
3379 runtime = substream->runtime;
3380 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3382 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3384 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3385 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3387 size = area->vm_end - area->vm_start;
3388 offset = area->vm_pgoff << PAGE_SHIFT;
3389 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3390 if ((size_t)size > dma_bytes)
3392 if (offset > dma_bytes - size)
3395 area->vm_ops = &snd_pcm_vm_ops_data;
3396 area->vm_private_data = substream;
3397 if (substream->ops->mmap)
3398 err = substream->ops->mmap(substream, area);
3400 err = snd_pcm_lib_default_mmap(substream, area);
3402 atomic_inc(&substream->mmap_count);
3406 EXPORT_SYMBOL(snd_pcm_mmap_data);
3408 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3410 struct snd_pcm_file * pcm_file;
3411 struct snd_pcm_substream *substream;
3412 unsigned long offset;
3414 pcm_file = file->private_data;
3415 substream = pcm_file->substream;
3416 if (PCM_RUNTIME_CHECK(substream))
3419 offset = area->vm_pgoff << PAGE_SHIFT;
3421 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3422 if (pcm_file->no_compat_mmap)
3424 return snd_pcm_mmap_status(substream, file, area);
3425 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3426 if (pcm_file->no_compat_mmap)
3428 return snd_pcm_mmap_control(substream, file, area);
3430 return snd_pcm_mmap_data(substream, file, area);
3435 static int snd_pcm_fasync(int fd, struct file * file, int on)
3437 struct snd_pcm_file * pcm_file;
3438 struct snd_pcm_substream *substream;
3439 struct snd_pcm_runtime *runtime;
3441 pcm_file = file->private_data;
3442 substream = pcm_file->substream;
3443 if (PCM_RUNTIME_CHECK(substream))
3445 runtime = substream->runtime;
3446 return fasync_helper(fd, file, on, &runtime->fasync);
3452 #ifdef CONFIG_COMPAT
3453 #include "pcm_compat.c"
3455 #define snd_pcm_ioctl_compat NULL
3459 * To be removed helpers to keep binary compatibility
3462 #ifdef CONFIG_SND_SUPPORT_OLD_API
3463 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3464 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3466 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3467 struct snd_pcm_hw_params_old *oparams)
3471 memset(params, 0, sizeof(*params));
3472 params->flags = oparams->flags;
3473 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3474 params->masks[i].bits[0] = oparams->masks[i];
3475 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3476 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3477 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3478 params->info = oparams->info;
3479 params->msbits = oparams->msbits;
3480 params->rate_num = oparams->rate_num;
3481 params->rate_den = oparams->rate_den;
3482 params->fifo_size = oparams->fifo_size;
3485 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3486 struct snd_pcm_hw_params *params)
3490 memset(oparams, 0, sizeof(*oparams));
3491 oparams->flags = params->flags;
3492 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3493 oparams->masks[i] = params->masks[i].bits[0];
3494 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3495 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3496 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3497 oparams->info = params->info;
3498 oparams->msbits = params->msbits;
3499 oparams->rate_num = params->rate_num;
3500 oparams->rate_den = params->rate_den;
3501 oparams->fifo_size = params->fifo_size;
3504 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3505 struct snd_pcm_hw_params_old __user * _oparams)
3507 struct snd_pcm_hw_params *params;
3508 struct snd_pcm_hw_params_old *oparams = NULL;
3511 params = kmalloc(sizeof(*params), GFP_KERNEL);
3515 oparams = memdup_user(_oparams, sizeof(*oparams));
3516 if (IS_ERR(oparams)) {
3517 err = PTR_ERR(oparams);
3520 snd_pcm_hw_convert_from_old_params(params, oparams);
3521 err = snd_pcm_hw_refine(substream, params);
3522 snd_pcm_hw_convert_to_old_params(oparams, params);
3523 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3534 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3535 struct snd_pcm_hw_params_old __user * _oparams)
3537 struct snd_pcm_hw_params *params;
3538 struct snd_pcm_hw_params_old *oparams = NULL;
3541 params = kmalloc(sizeof(*params), GFP_KERNEL);
3545 oparams = memdup_user(_oparams, sizeof(*oparams));
3546 if (IS_ERR(oparams)) {
3547 err = PTR_ERR(oparams);
3550 snd_pcm_hw_convert_from_old_params(params, oparams);
3551 err = snd_pcm_hw_params(substream, params);
3552 snd_pcm_hw_convert_to_old_params(oparams, params);
3553 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3563 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3566 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3569 unsigned long pgoff,
3570 unsigned long flags)
3572 struct snd_pcm_file *pcm_file = file->private_data;
3573 struct snd_pcm_substream *substream = pcm_file->substream;
3574 struct snd_pcm_runtime *runtime = substream->runtime;
3575 unsigned long offset = pgoff << PAGE_SHIFT;
3578 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3579 return (unsigned long)runtime->status;
3580 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3581 return (unsigned long)runtime->control;
3583 return (unsigned long)runtime->dma_area + offset;
3587 # define snd_pcm_get_unmapped_area NULL
3594 const struct file_operations snd_pcm_f_ops[2] = {
3596 .owner = THIS_MODULE,
3597 .write = snd_pcm_write,
3598 .aio_write = snd_pcm_aio_write,
3599 .open = snd_pcm_playback_open,
3600 .release = snd_pcm_release,
3601 .llseek = no_llseek,
3602 .poll = snd_pcm_playback_poll,
3603 .unlocked_ioctl = snd_pcm_playback_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,
3610 .owner = THIS_MODULE,
3611 .read = snd_pcm_read,
3612 .aio_read = snd_pcm_aio_read,
3613 .open = snd_pcm_capture_open,
3614 .release = snd_pcm_release,
3615 .llseek = no_llseek,
3616 .poll = snd_pcm_capture_poll,
3617 .unlocked_ioctl = snd_pcm_capture_ioctl,
3618 .compat_ioctl = snd_pcm_ioctl_compat,
3619 .mmap = snd_pcm_mmap,
3620 .fasync = snd_pcm_fasync,
3621 .get_unmapped_area = snd_pcm_get_unmapped_area,