GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / core / pcm_native.c
blob141ca540d16dfa8dfc14cca975abf18c71d453f1
1 /*
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
22 #include <linux/mm.h>
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <linux/dma-mapping.h>
30 #include <sound/core.h>
31 #include <sound/control.h>
32 #include <sound/info.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/timer.h>
36 #include <sound/minors.h>
37 #include <asm/io.h>
38 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39 #include <dma-coherence.h>
40 #endif
43 * Compatibility
46 struct snd_pcm_hw_params_old {
47 unsigned int flags;
48 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
49 SNDRV_PCM_HW_PARAM_ACCESS + 1];
50 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
51 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rmask;
53 unsigned int cmask;
54 unsigned int info;
55 unsigned int msbits;
56 unsigned int rate_num;
57 unsigned int rate_den;
58 snd_pcm_uframes_t fifo_size;
59 unsigned char reserved[64];
62 #ifdef CONFIG_SND_SUPPORT_OLD_API
63 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
66 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params_old __user * _oparams);
70 #endif
71 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
77 DEFINE_RWLOCK(snd_pcm_link_rwlock);
78 EXPORT_SYMBOL(snd_pcm_link_rwlock);
80 static DECLARE_RWSEM(snd_pcm_link_rwsem);
82 static inline mm_segment_t snd_enter_user(void)
84 mm_segment_t fs = get_fs();
85 set_fs(get_ds());
86 return fs;
89 static inline void snd_leave_user(mm_segment_t fs)
91 set_fs(fs);
96 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
98 struct snd_pcm_runtime *runtime;
99 struct snd_pcm *pcm = substream->pcm;
100 struct snd_pcm_str *pstr = substream->pstr;
102 memset(info, 0, sizeof(*info));
103 info->card = pcm->card->number;
104 info->device = pcm->device;
105 info->stream = substream->stream;
106 info->subdevice = substream->number;
107 strlcpy(info->id, pcm->id, sizeof(info->id));
108 strlcpy(info->name, pcm->name, sizeof(info->name));
109 info->dev_class = pcm->dev_class;
110 info->dev_subclass = pcm->dev_subclass;
111 info->subdevices_count = pstr->substream_count;
112 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
113 strlcpy(info->subname, substream->name, sizeof(info->subname));
114 runtime = substream->runtime;
115 if (runtime) {
116 info->sync = runtime->sync;
117 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 return 0;
122 int snd_pcm_info_user(struct snd_pcm_substream *substream,
123 struct snd_pcm_info __user * _info)
125 struct snd_pcm_info *info;
126 int err;
128 info = kmalloc(sizeof(*info), GFP_KERNEL);
129 if (! info)
130 return -ENOMEM;
131 err = snd_pcm_info(substream, info);
132 if (err >= 0) {
133 if (copy_to_user(_info, info, sizeof(*info)))
134 err = -EFAULT;
136 kfree(info);
137 return err;
140 #undef RULES_DEBUG
142 #ifdef RULES_DEBUG
143 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
144 char *snd_pcm_hw_param_names[] = {
145 HW_PARAM(ACCESS),
146 HW_PARAM(FORMAT),
147 HW_PARAM(SUBFORMAT),
148 HW_PARAM(SAMPLE_BITS),
149 HW_PARAM(FRAME_BITS),
150 HW_PARAM(CHANNELS),
151 HW_PARAM(RATE),
152 HW_PARAM(PERIOD_TIME),
153 HW_PARAM(PERIOD_SIZE),
154 HW_PARAM(PERIOD_BYTES),
155 HW_PARAM(PERIODS),
156 HW_PARAM(BUFFER_TIME),
157 HW_PARAM(BUFFER_SIZE),
158 HW_PARAM(BUFFER_BYTES),
159 HW_PARAM(TICK_TIME),
161 #endif
163 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
164 struct snd_pcm_hw_params *params)
166 unsigned int k;
167 struct snd_pcm_hardware *hw;
168 struct snd_interval *i = NULL;
169 struct snd_mask *m = NULL;
170 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
171 unsigned int rstamps[constrs->rules_num];
172 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
173 unsigned int stamp = 2;
174 int changed, again;
176 params->info = 0;
177 params->fifo_size = 0;
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
179 params->msbits = 0;
180 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
181 params->rate_num = 0;
182 params->rate_den = 0;
185 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
186 m = hw_param_mask(params, k);
187 if (snd_mask_empty(m))
188 return -EINVAL;
189 if (!(params->rmask & (1 << k)))
190 continue;
191 #ifdef RULES_DEBUG
192 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
193 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
194 #endif
195 changed = snd_mask_refine(m, constrs_mask(constrs, k));
196 #ifdef RULES_DEBUG
197 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
198 #endif
199 if (changed)
200 params->cmask |= 1 << k;
201 if (changed < 0)
202 return changed;
205 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
206 i = hw_param_interval(params, k);
207 if (snd_interval_empty(i))
208 return -EINVAL;
209 if (!(params->rmask & (1 << k)))
210 continue;
211 #ifdef RULES_DEBUG
212 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
213 if (i->empty)
214 printk("empty");
215 else
216 printk("%c%u %u%c",
217 i->openmin ? '(' : '[', i->min,
218 i->max, i->openmax ? ')' : ']');
219 printk(" -> ");
220 #endif
221 changed = snd_interval_refine(i, constrs_interval(constrs, k));
222 #ifdef RULES_DEBUG
223 if (i->empty)
224 printk("empty\n");
225 else
226 printk("%c%u %u%c\n",
227 i->openmin ? '(' : '[', i->min,
228 i->max, i->openmax ? ')' : ']');
229 #endif
230 if (changed)
231 params->cmask |= 1 << k;
232 if (changed < 0)
233 return changed;
236 for (k = 0; k < constrs->rules_num; k++)
237 rstamps[k] = 0;
238 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
239 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
240 do {
241 again = 0;
242 for (k = 0; k < constrs->rules_num; k++) {
243 struct snd_pcm_hw_rule *r = &constrs->rules[k];
244 unsigned int d;
245 int doit = 0;
246 if (r->cond && !(r->cond & params->flags))
247 continue;
248 for (d = 0; r->deps[d] >= 0; d++) {
249 if (vstamps[r->deps[d]] > rstamps[k]) {
250 doit = 1;
251 break;
254 if (!doit)
255 continue;
256 #ifdef RULES_DEBUG
257 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
258 if (r->var >= 0) {
259 printk("%s = ", snd_pcm_hw_param_names[r->var]);
260 if (hw_is_mask(r->var)) {
261 m = hw_param_mask(params, r->var);
262 printk("%x", *m->bits);
263 } else {
264 i = hw_param_interval(params, r->var);
265 if (i->empty)
266 printk("empty");
267 else
268 printk("%c%u %u%c",
269 i->openmin ? '(' : '[', i->min,
270 i->max, i->openmax ? ')' : ']');
273 #endif
274 changed = r->func(params, r);
275 #ifdef RULES_DEBUG
276 if (r->var >= 0) {
277 printk(" -> ");
278 if (hw_is_mask(r->var))
279 printk("%x", *m->bits);
280 else {
281 if (i->empty)
282 printk("empty");
283 else
284 printk("%c%u %u%c",
285 i->openmin ? '(' : '[', i->min,
286 i->max, i->openmax ? ')' : ']');
289 printk("\n");
290 #endif
291 rstamps[k] = stamp;
292 if (changed && r->var >= 0) {
293 params->cmask |= (1 << r->var);
294 vstamps[r->var] = stamp;
295 again = 1;
297 if (changed < 0)
298 return changed;
299 stamp++;
301 } while (again);
302 if (!params->msbits) {
303 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
304 if (snd_interval_single(i))
305 params->msbits = snd_interval_value(i);
308 if (!params->rate_den) {
309 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
310 if (snd_interval_single(i)) {
311 params->rate_num = snd_interval_value(i);
312 params->rate_den = 1;
316 hw = &substream->runtime->hw;
317 if (!params->info)
318 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
319 if (!params->fifo_size) {
320 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
321 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
322 if (snd_mask_min(m) == snd_mask_max(m) &&
323 snd_interval_min(i) == snd_interval_max(i)) {
324 changed = substream->ops->ioctl(substream,
325 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
326 if (changed < 0)
327 return changed;
330 params->rmask = 0;
331 return 0;
334 EXPORT_SYMBOL(snd_pcm_hw_refine);
336 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
337 struct snd_pcm_hw_params __user * _params)
339 struct snd_pcm_hw_params *params;
340 int err;
342 params = memdup_user(_params, sizeof(*params));
343 if (IS_ERR(params))
344 return PTR_ERR(params);
346 err = snd_pcm_hw_refine(substream, params);
347 if (copy_to_user(_params, params, sizeof(*params))) {
348 if (!err)
349 err = -EFAULT;
352 kfree(params);
353 return err;
356 static int period_to_usecs(struct snd_pcm_runtime *runtime)
358 int usecs;
360 if (! runtime->rate)
361 return -1; /* invalid */
363 /* take 75% of period time as the deadline */
364 usecs = (750000 / runtime->rate) * runtime->period_size;
365 usecs += ((750000 % runtime->rate) * runtime->period_size) /
366 runtime->rate;
368 return usecs;
371 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
372 struct snd_pcm_hw_params *params)
374 struct snd_pcm_runtime *runtime;
375 int err, usecs;
376 unsigned int bits;
377 snd_pcm_uframes_t frames;
379 if (PCM_RUNTIME_CHECK(substream))
380 return -ENXIO;
381 runtime = substream->runtime;
382 snd_pcm_stream_lock_irq(substream);
383 switch (runtime->status->state) {
384 case SNDRV_PCM_STATE_OPEN:
385 case SNDRV_PCM_STATE_SETUP:
386 case SNDRV_PCM_STATE_PREPARED:
387 break;
388 default:
389 snd_pcm_stream_unlock_irq(substream);
390 return -EBADFD;
392 snd_pcm_stream_unlock_irq(substream);
393 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
394 if (!substream->oss.oss)
395 #endif
396 if (atomic_read(&substream->mmap_count))
397 return -EBADFD;
399 params->rmask = ~0U;
400 err = snd_pcm_hw_refine(substream, params);
401 if (err < 0)
402 goto _error;
404 err = snd_pcm_hw_params_choose(substream, params);
405 if (err < 0)
406 goto _error;
408 if (substream->ops->hw_params != NULL) {
409 err = substream->ops->hw_params(substream, params);
410 if (err < 0)
411 goto _error;
414 runtime->access = params_access(params);
415 runtime->format = params_format(params);
416 runtime->subformat = params_subformat(params);
417 runtime->channels = params_channels(params);
418 runtime->rate = params_rate(params);
419 runtime->period_size = params_period_size(params);
420 runtime->periods = params_periods(params);
421 runtime->buffer_size = params_buffer_size(params);
422 runtime->info = params->info;
423 runtime->rate_num = params->rate_num;
424 runtime->rate_den = params->rate_den;
426 bits = snd_pcm_format_physical_width(runtime->format);
427 runtime->sample_bits = bits;
428 bits *= runtime->channels;
429 runtime->frame_bits = bits;
430 frames = 1;
431 while (bits % 8 != 0) {
432 bits *= 2;
433 frames *= 2;
435 runtime->byte_align = bits / 8;
436 runtime->min_align = frames;
438 /* Default sw params */
439 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
440 runtime->period_step = 1;
441 runtime->control->avail_min = runtime->period_size;
442 runtime->start_threshold = 1;
443 runtime->stop_threshold = runtime->buffer_size;
444 runtime->silence_threshold = 0;
445 runtime->silence_size = 0;
446 runtime->boundary = runtime->buffer_size;
447 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
448 runtime->boundary *= 2;
450 snd_pcm_timer_resolution_change(substream);
451 runtime->status->state = SNDRV_PCM_STATE_SETUP;
453 if (pm_qos_request_active(&substream->latency_pm_qos_req))
454 pm_qos_remove_request(&substream->latency_pm_qos_req);
455 if ((usecs = period_to_usecs(runtime)) >= 0)
456 pm_qos_add_request(&substream->latency_pm_qos_req,
457 PM_QOS_CPU_DMA_LATENCY, usecs);
458 return 0;
459 _error:
460 /* hardware might be unuseable from this time,
461 so we force application to retry to set
462 the correct hardware parameter settings */
463 runtime->status->state = SNDRV_PCM_STATE_OPEN;
464 if (substream->ops->hw_free != NULL)
465 substream->ops->hw_free(substream);
466 return err;
469 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
470 struct snd_pcm_hw_params __user * _params)
472 struct snd_pcm_hw_params *params;
473 int err;
475 params = memdup_user(_params, sizeof(*params));
476 if (IS_ERR(params))
477 return PTR_ERR(params);
479 err = snd_pcm_hw_params(substream, params);
480 if (copy_to_user(_params, params, sizeof(*params))) {
481 if (!err)
482 err = -EFAULT;
485 kfree(params);
486 return err;
489 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
491 struct snd_pcm_runtime *runtime;
492 int result = 0;
494 if (PCM_RUNTIME_CHECK(substream))
495 return -ENXIO;
496 runtime = substream->runtime;
497 snd_pcm_stream_lock_irq(substream);
498 switch (runtime->status->state) {
499 case SNDRV_PCM_STATE_SETUP:
500 case SNDRV_PCM_STATE_PREPARED:
501 break;
502 default:
503 snd_pcm_stream_unlock_irq(substream);
504 return -EBADFD;
506 snd_pcm_stream_unlock_irq(substream);
507 if (atomic_read(&substream->mmap_count))
508 return -EBADFD;
509 if (substream->ops->hw_free)
510 result = substream->ops->hw_free(substream);
511 runtime->status->state = SNDRV_PCM_STATE_OPEN;
512 pm_qos_remove_request(&substream->latency_pm_qos_req);
513 return result;
516 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
517 struct snd_pcm_sw_params *params)
519 struct snd_pcm_runtime *runtime;
520 int err;
522 if (PCM_RUNTIME_CHECK(substream))
523 return -ENXIO;
524 runtime = substream->runtime;
525 snd_pcm_stream_lock_irq(substream);
526 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
527 snd_pcm_stream_unlock_irq(substream);
528 return -EBADFD;
530 snd_pcm_stream_unlock_irq(substream);
532 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
533 return -EINVAL;
534 if (params->avail_min == 0)
535 return -EINVAL;
536 if (params->silence_size >= runtime->boundary) {
537 if (params->silence_threshold != 0)
538 return -EINVAL;
539 } else {
540 if (params->silence_size > params->silence_threshold)
541 return -EINVAL;
542 if (params->silence_threshold > runtime->buffer_size)
543 return -EINVAL;
545 err = 0;
546 snd_pcm_stream_lock_irq(substream);
547 runtime->tstamp_mode = params->tstamp_mode;
548 runtime->period_step = params->period_step;
549 runtime->control->avail_min = params->avail_min;
550 runtime->start_threshold = params->start_threshold;
551 runtime->stop_threshold = params->stop_threshold;
552 runtime->silence_threshold = params->silence_threshold;
553 runtime->silence_size = params->silence_size;
554 params->boundary = runtime->boundary;
555 if (snd_pcm_running(substream)) {
556 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
557 runtime->silence_size > 0)
558 snd_pcm_playback_silence(substream, ULONG_MAX);
559 err = snd_pcm_update_state(substream, runtime);
561 snd_pcm_stream_unlock_irq(substream);
562 return err;
565 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
566 struct snd_pcm_sw_params __user * _params)
568 struct snd_pcm_sw_params params;
569 int err;
570 if (copy_from_user(&params, _params, sizeof(params)))
571 return -EFAULT;
572 err = snd_pcm_sw_params(substream, &params);
573 if (copy_to_user(_params, &params, sizeof(params)))
574 return -EFAULT;
575 return err;
578 int snd_pcm_status(struct snd_pcm_substream *substream,
579 struct snd_pcm_status *status)
581 struct snd_pcm_runtime *runtime = substream->runtime;
583 snd_pcm_stream_lock_irq(substream);
584 status->state = runtime->status->state;
585 status->suspended_state = runtime->status->suspended_state;
586 if (status->state == SNDRV_PCM_STATE_OPEN)
587 goto _end;
588 status->trigger_tstamp = runtime->trigger_tstamp;
589 if (snd_pcm_running(substream)) {
590 snd_pcm_update_hw_ptr(substream);
591 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
592 status->tstamp = runtime->status->tstamp;
593 goto _tstamp_end;
596 snd_pcm_gettime(runtime, &status->tstamp);
597 _tstamp_end:
598 status->appl_ptr = runtime->control->appl_ptr;
599 status->hw_ptr = runtime->status->hw_ptr;
600 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
601 status->avail = snd_pcm_playback_avail(runtime);
602 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
603 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
604 status->delay = runtime->buffer_size - status->avail;
605 status->delay += runtime->delay;
606 } else
607 status->delay = 0;
608 } else {
609 status->avail = snd_pcm_capture_avail(runtime);
610 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
611 status->delay = status->avail + runtime->delay;
612 else
613 status->delay = 0;
615 status->avail_max = runtime->avail_max;
616 status->overrange = runtime->overrange;
617 runtime->avail_max = 0;
618 runtime->overrange = 0;
619 _end:
620 snd_pcm_stream_unlock_irq(substream);
621 return 0;
624 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
625 struct snd_pcm_status __user * _status)
627 struct snd_pcm_status status;
628 int res;
630 memset(&status, 0, sizeof(status));
631 res = snd_pcm_status(substream, &status);
632 if (res < 0)
633 return res;
634 if (copy_to_user(_status, &status, sizeof(status)))
635 return -EFAULT;
636 return 0;
639 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
640 struct snd_pcm_channel_info * info)
642 struct snd_pcm_runtime *runtime;
643 unsigned int channel;
645 channel = info->channel;
646 runtime = substream->runtime;
647 snd_pcm_stream_lock_irq(substream);
648 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
649 snd_pcm_stream_unlock_irq(substream);
650 return -EBADFD;
652 snd_pcm_stream_unlock_irq(substream);
653 if (channel >= runtime->channels)
654 return -EINVAL;
655 memset(info, 0, sizeof(*info));
656 info->channel = channel;
657 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
660 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
661 struct snd_pcm_channel_info __user * _info)
663 struct snd_pcm_channel_info info;
664 int res;
666 if (copy_from_user(&info, _info, sizeof(info)))
667 return -EFAULT;
668 res = snd_pcm_channel_info(substream, &info);
669 if (res < 0)
670 return res;
671 if (copy_to_user(_info, &info, sizeof(info)))
672 return -EFAULT;
673 return 0;
676 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
678 struct snd_pcm_runtime *runtime = substream->runtime;
679 if (runtime->trigger_master == NULL)
680 return;
681 if (runtime->trigger_master == substream) {
682 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
683 } else {
684 snd_pcm_trigger_tstamp(runtime->trigger_master);
685 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
687 runtime->trigger_master = NULL;
690 struct action_ops {
691 int (*pre_action)(struct snd_pcm_substream *substream, int state);
692 int (*do_action)(struct snd_pcm_substream *substream, int state);
693 void (*undo_action)(struct snd_pcm_substream *substream, int state);
694 void (*post_action)(struct snd_pcm_substream *substream, int state);
698 * this functions is core for handling of linked stream
699 * Note: the stream state might be changed also on failure
700 * Note2: call with calling stream lock + link lock
702 static int snd_pcm_action_group(struct action_ops *ops,
703 struct snd_pcm_substream *substream,
704 int state, int do_lock)
706 struct snd_pcm_substream *s = NULL;
707 struct snd_pcm_substream *s1;
708 int res = 0;
710 snd_pcm_group_for_each_entry(s, substream) {
711 if (do_lock && s != substream)
712 spin_lock_nested(&s->self_group.lock,
713 SINGLE_DEPTH_NESTING);
714 res = ops->pre_action(s, state);
715 if (res < 0)
716 goto _unlock;
718 snd_pcm_group_for_each_entry(s, substream) {
719 res = ops->do_action(s, state);
720 if (res < 0) {
721 if (ops->undo_action) {
722 snd_pcm_group_for_each_entry(s1, substream) {
723 if (s1 == s) /* failed stream */
724 break;
725 ops->undo_action(s1, state);
728 s = NULL; /* unlock all */
729 goto _unlock;
732 snd_pcm_group_for_each_entry(s, substream) {
733 ops->post_action(s, state);
735 _unlock:
736 if (do_lock) {
737 /* unlock streams */
738 snd_pcm_group_for_each_entry(s1, substream) {
739 if (s1 != substream)
740 spin_unlock(&s1->self_group.lock);
741 if (s1 == s) /* end */
742 break;
745 return res;
749 * Note: call with stream lock
751 static int snd_pcm_action_single(struct action_ops *ops,
752 struct snd_pcm_substream *substream,
753 int state)
755 int res;
757 res = ops->pre_action(substream, state);
758 if (res < 0)
759 return res;
760 res = ops->do_action(substream, state);
761 if (res == 0)
762 ops->post_action(substream, state);
763 else if (ops->undo_action)
764 ops->undo_action(substream, state);
765 return res;
769 * Note: call with stream lock
771 static int snd_pcm_action(struct action_ops *ops,
772 struct snd_pcm_substream *substream,
773 int state)
775 int res;
777 if (snd_pcm_stream_linked(substream)) {
778 if (!spin_trylock(&substream->group->lock)) {
779 spin_unlock(&substream->self_group.lock);
780 spin_lock(&substream->group->lock);
781 spin_lock(&substream->self_group.lock);
783 res = snd_pcm_action_group(ops, substream, state, 1);
784 spin_unlock(&substream->group->lock);
785 } else {
786 res = snd_pcm_action_single(ops, substream, state);
788 return res;
792 * Note: don't use any locks before
794 static int snd_pcm_action_lock_irq(struct action_ops *ops,
795 struct snd_pcm_substream *substream,
796 int state)
798 int res;
800 read_lock_irq(&snd_pcm_link_rwlock);
801 if (snd_pcm_stream_linked(substream)) {
802 spin_lock(&substream->group->lock);
803 spin_lock(&substream->self_group.lock);
804 res = snd_pcm_action_group(ops, substream, state, 1);
805 spin_unlock(&substream->self_group.lock);
806 spin_unlock(&substream->group->lock);
807 } else {
808 spin_lock(&substream->self_group.lock);
809 res = snd_pcm_action_single(ops, substream, state);
810 spin_unlock(&substream->self_group.lock);
812 read_unlock_irq(&snd_pcm_link_rwlock);
813 return res;
818 static int snd_pcm_action_nonatomic(struct action_ops *ops,
819 struct snd_pcm_substream *substream,
820 int state)
822 int res;
824 down_read(&snd_pcm_link_rwsem);
825 if (snd_pcm_stream_linked(substream))
826 res = snd_pcm_action_group(ops, substream, state, 0);
827 else
828 res = snd_pcm_action_single(ops, substream, state);
829 up_read(&snd_pcm_link_rwsem);
830 return res;
834 * start callbacks
836 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
838 struct snd_pcm_runtime *runtime = substream->runtime;
839 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
840 return -EBADFD;
841 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
842 !snd_pcm_playback_data(substream))
843 return -EPIPE;
844 runtime->trigger_master = substream;
845 return 0;
848 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
850 if (substream->runtime->trigger_master != substream)
851 return 0;
852 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
855 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
857 if (substream->runtime->trigger_master == substream)
858 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
861 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
863 struct snd_pcm_runtime *runtime = substream->runtime;
864 snd_pcm_trigger_tstamp(substream);
865 runtime->hw_ptr_jiffies = jiffies;
866 runtime->status->state = state;
867 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
868 runtime->silence_size > 0)
869 snd_pcm_playback_silence(substream, ULONG_MAX);
870 if (substream->timer)
871 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
872 &runtime->trigger_tstamp);
875 static struct action_ops snd_pcm_action_start = {
876 .pre_action = snd_pcm_pre_start,
877 .do_action = snd_pcm_do_start,
878 .undo_action = snd_pcm_undo_start,
879 .post_action = snd_pcm_post_start
883 * snd_pcm_start - start all linked streams
884 * @substream: the PCM substream instance
886 int snd_pcm_start(struct snd_pcm_substream *substream)
888 return snd_pcm_action(&snd_pcm_action_start, substream,
889 SNDRV_PCM_STATE_RUNNING);
893 * stop callbacks
895 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
897 struct snd_pcm_runtime *runtime = substream->runtime;
898 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
899 return -EBADFD;
900 runtime->trigger_master = substream;
901 return 0;
904 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
906 if (substream->runtime->trigger_master == substream &&
907 snd_pcm_running(substream))
908 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
909 return 0; /* unconditonally stop all substreams */
912 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
914 struct snd_pcm_runtime *runtime = substream->runtime;
915 if (runtime->status->state != state) {
916 snd_pcm_trigger_tstamp(substream);
917 if (substream->timer)
918 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
919 &runtime->trigger_tstamp);
920 runtime->status->state = state;
922 wake_up(&runtime->sleep);
923 wake_up(&runtime->tsleep);
926 static struct action_ops snd_pcm_action_stop = {
927 .pre_action = snd_pcm_pre_stop,
928 .do_action = snd_pcm_do_stop,
929 .post_action = snd_pcm_post_stop
933 * snd_pcm_stop - try to stop all running streams in the substream group
934 * @substream: the PCM substream instance
935 * @state: PCM state after stopping the stream
937 * The state of each stream is then changed to the given state unconditionally.
939 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
941 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
944 EXPORT_SYMBOL(snd_pcm_stop);
947 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
948 * @substream: the PCM substream
950 * After stopping, the state is changed to SETUP.
951 * Unlike snd_pcm_stop(), this affects only the given stream.
953 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
955 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
956 SNDRV_PCM_STATE_SETUP);
960 * pause callbacks
962 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
964 struct snd_pcm_runtime *runtime = substream->runtime;
965 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
966 return -ENOSYS;
967 if (push) {
968 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
969 return -EBADFD;
970 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
971 return -EBADFD;
972 runtime->trigger_master = substream;
973 return 0;
976 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
978 if (substream->runtime->trigger_master != substream)
979 return 0;
980 /* some drivers might use hw_ptr to recover from the pause -
981 update the hw_ptr now */
982 if (push)
983 snd_pcm_update_hw_ptr(substream);
984 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
985 * a delta betwen the current jiffies, this gives a large enough
986 * delta, effectively to skip the check once.
988 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
989 return substream->ops->trigger(substream,
990 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
991 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
994 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
996 if (substream->runtime->trigger_master == substream)
997 substream->ops->trigger(substream,
998 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
999 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1002 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 snd_pcm_trigger_tstamp(substream);
1006 if (push) {
1007 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1008 if (substream->timer)
1009 snd_timer_notify(substream->timer,
1010 SNDRV_TIMER_EVENT_MPAUSE,
1011 &runtime->trigger_tstamp);
1012 wake_up(&runtime->sleep);
1013 wake_up(&runtime->tsleep);
1014 } else {
1015 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1016 if (substream->timer)
1017 snd_timer_notify(substream->timer,
1018 SNDRV_TIMER_EVENT_MCONTINUE,
1019 &runtime->trigger_tstamp);
1023 static struct action_ops snd_pcm_action_pause = {
1024 .pre_action = snd_pcm_pre_pause,
1025 .do_action = snd_pcm_do_pause,
1026 .undo_action = snd_pcm_undo_pause,
1027 .post_action = snd_pcm_post_pause
1031 * Push/release the pause for all linked streams.
1033 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1035 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1038 #ifdef CONFIG_PM
1039 /* suspend */
1041 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1043 struct snd_pcm_runtime *runtime = substream->runtime;
1044 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1045 return -EBUSY;
1046 runtime->trigger_master = substream;
1047 return 0;
1050 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1052 struct snd_pcm_runtime *runtime = substream->runtime;
1053 if (runtime->trigger_master != substream)
1054 return 0;
1055 if (! snd_pcm_running(substream))
1056 return 0;
1057 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1058 return 0; /* suspend unconditionally */
1061 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1063 struct snd_pcm_runtime *runtime = substream->runtime;
1064 snd_pcm_trigger_tstamp(substream);
1065 if (substream->timer)
1066 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1067 &runtime->trigger_tstamp);
1068 runtime->status->suspended_state = runtime->status->state;
1069 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1070 wake_up(&runtime->sleep);
1071 wake_up(&runtime->tsleep);
1074 static struct action_ops snd_pcm_action_suspend = {
1075 .pre_action = snd_pcm_pre_suspend,
1076 .do_action = snd_pcm_do_suspend,
1077 .post_action = snd_pcm_post_suspend
1081 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1082 * @substream: the PCM substream
1084 * After this call, all streams are changed to SUSPENDED state.
1086 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1088 int err;
1089 unsigned long flags;
1091 if (! substream)
1092 return 0;
1094 snd_pcm_stream_lock_irqsave(substream, flags);
1095 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1096 snd_pcm_stream_unlock_irqrestore(substream, flags);
1097 return err;
1100 EXPORT_SYMBOL(snd_pcm_suspend);
1103 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1104 * @pcm: the PCM instance
1106 * After this call, all streams are changed to SUSPENDED state.
1108 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1110 struct snd_pcm_substream *substream;
1111 int stream, err = 0;
1113 if (! pcm)
1114 return 0;
1116 for (stream = 0; stream < 2; stream++) {
1117 for (substream = pcm->streams[stream].substream;
1118 substream; substream = substream->next) {
1119 if (substream->runtime == NULL)
1120 continue;
1121 err = snd_pcm_suspend(substream);
1122 if (err < 0 && err != -EBUSY)
1123 return err;
1126 return 0;
1129 EXPORT_SYMBOL(snd_pcm_suspend_all);
1131 /* resume */
1133 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1135 struct snd_pcm_runtime *runtime = substream->runtime;
1136 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1137 return -ENOSYS;
1138 runtime->trigger_master = substream;
1139 return 0;
1142 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1144 struct snd_pcm_runtime *runtime = substream->runtime;
1145 if (runtime->trigger_master != substream)
1146 return 0;
1147 /* DMA not running previously? */
1148 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1149 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1150 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1151 return 0;
1152 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1155 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1157 if (substream->runtime->trigger_master == substream &&
1158 snd_pcm_running(substream))
1159 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1162 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1164 struct snd_pcm_runtime *runtime = substream->runtime;
1165 snd_pcm_trigger_tstamp(substream);
1166 if (substream->timer)
1167 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1168 &runtime->trigger_tstamp);
1169 runtime->status->state = runtime->status->suspended_state;
1172 static struct action_ops snd_pcm_action_resume = {
1173 .pre_action = snd_pcm_pre_resume,
1174 .do_action = snd_pcm_do_resume,
1175 .undo_action = snd_pcm_undo_resume,
1176 .post_action = snd_pcm_post_resume
1179 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1181 struct snd_card *card = substream->pcm->card;
1182 int res;
1184 snd_power_lock(card);
1185 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1186 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1187 snd_power_unlock(card);
1188 return res;
1191 #else
1193 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1195 return -ENOSYS;
1198 #endif /* CONFIG_PM */
1201 * xrun ioctl
1203 * Change the RUNNING stream(s) to XRUN state.
1205 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1207 struct snd_card *card = substream->pcm->card;
1208 struct snd_pcm_runtime *runtime = substream->runtime;
1209 int result;
1211 snd_power_lock(card);
1212 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1213 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1214 if (result < 0)
1215 goto _unlock;
1218 snd_pcm_stream_lock_irq(substream);
1219 switch (runtime->status->state) {
1220 case SNDRV_PCM_STATE_XRUN:
1221 result = 0; /* already there */
1222 break;
1223 case SNDRV_PCM_STATE_RUNNING:
1224 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1225 break;
1226 default:
1227 result = -EBADFD;
1229 snd_pcm_stream_unlock_irq(substream);
1230 _unlock:
1231 snd_power_unlock(card);
1232 return result;
1236 * reset ioctl
1238 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1241 switch (runtime->status->state) {
1242 case SNDRV_PCM_STATE_RUNNING:
1243 case SNDRV_PCM_STATE_PREPARED:
1244 case SNDRV_PCM_STATE_PAUSED:
1245 case SNDRV_PCM_STATE_SUSPENDED:
1246 return 0;
1247 default:
1248 return -EBADFD;
1252 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1254 struct snd_pcm_runtime *runtime = substream->runtime;
1255 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1256 if (err < 0)
1257 return err;
1258 runtime->hw_ptr_base = 0;
1259 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1260 runtime->status->hw_ptr % runtime->period_size;
1261 runtime->silence_start = runtime->status->hw_ptr;
1262 runtime->silence_filled = 0;
1263 return 0;
1266 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1268 struct snd_pcm_runtime *runtime = substream->runtime;
1269 runtime->control->appl_ptr = runtime->status->hw_ptr;
1270 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1271 runtime->silence_size > 0)
1272 snd_pcm_playback_silence(substream, ULONG_MAX);
1275 static struct action_ops snd_pcm_action_reset = {
1276 .pre_action = snd_pcm_pre_reset,
1277 .do_action = snd_pcm_do_reset,
1278 .post_action = snd_pcm_post_reset
1281 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1283 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1287 * prepare ioctl
1289 /* we use the second argument for updating f_flags */
1290 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1291 int f_flags)
1293 struct snd_pcm_runtime *runtime = substream->runtime;
1294 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1295 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1296 return -EBADFD;
1297 if (snd_pcm_running(substream))
1298 return -EBUSY;
1299 substream->f_flags = f_flags;
1300 return 0;
1303 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1305 int err;
1306 err = substream->ops->prepare(substream);
1307 if (err < 0)
1308 return err;
1309 return snd_pcm_do_reset(substream, 0);
1312 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1314 struct snd_pcm_runtime *runtime = substream->runtime;
1315 runtime->control->appl_ptr = runtime->status->hw_ptr;
1316 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1319 static struct action_ops snd_pcm_action_prepare = {
1320 .pre_action = snd_pcm_pre_prepare,
1321 .do_action = snd_pcm_do_prepare,
1322 .post_action = snd_pcm_post_prepare
1326 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1327 * @substream: the PCM substream instance
1328 * @file: file to refer f_flags
1330 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1331 struct file *file)
1333 int res;
1334 struct snd_card *card = substream->pcm->card;
1335 int f_flags;
1337 if (file)
1338 f_flags = file->f_flags;
1339 else
1340 f_flags = substream->f_flags;
1342 snd_power_lock(card);
1343 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1344 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1345 substream, f_flags);
1346 snd_power_unlock(card);
1347 return res;
1351 * drain ioctl
1354 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1356 substream->runtime->trigger_master = substream;
1357 return 0;
1360 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1362 struct snd_pcm_runtime *runtime = substream->runtime;
1363 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1364 switch (runtime->status->state) {
1365 case SNDRV_PCM_STATE_PREPARED:
1366 /* start playback stream if possible */
1367 if (! snd_pcm_playback_empty(substream)) {
1368 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1369 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1371 break;
1372 case SNDRV_PCM_STATE_RUNNING:
1373 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1374 break;
1375 default:
1376 break;
1378 } else {
1379 /* stop running stream */
1380 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1381 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1382 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1383 snd_pcm_do_stop(substream, new_state);
1384 snd_pcm_post_stop(substream, new_state);
1387 return 0;
1390 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1394 static struct action_ops snd_pcm_action_drain_init = {
1395 .pre_action = snd_pcm_pre_drain_init,
1396 .do_action = snd_pcm_do_drain_init,
1397 .post_action = snd_pcm_post_drain_init
1400 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1403 * Drain the stream(s).
1404 * When the substream is linked, sync until the draining of all playback streams
1405 * is finished.
1406 * After this call, all streams are supposed to be either SETUP or DRAINING
1407 * (capture only) state.
1409 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1410 struct file *file)
1412 struct snd_card *card;
1413 struct snd_pcm_runtime *runtime;
1414 struct snd_pcm_substream *s;
1415 wait_queue_t wait;
1416 int result = 0;
1417 int nonblock = 0;
1419 card = substream->pcm->card;
1420 runtime = substream->runtime;
1422 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1423 return -EBADFD;
1425 snd_power_lock(card);
1426 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1427 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1428 if (result < 0) {
1429 snd_power_unlock(card);
1430 return result;
1434 if (file) {
1435 if (file->f_flags & O_NONBLOCK)
1436 nonblock = 1;
1437 } else if (substream->f_flags & O_NONBLOCK)
1438 nonblock = 1;
1440 down_read(&snd_pcm_link_rwsem);
1441 snd_pcm_stream_lock_irq(substream);
1442 /* resume pause */
1443 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1444 snd_pcm_pause(substream, 0);
1446 /* pre-start/stop - all running streams are changed to DRAINING state */
1447 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1448 if (result < 0)
1449 goto unlock;
1450 /* in non-blocking, we don't wait in ioctl but let caller poll */
1451 if (nonblock) {
1452 result = -EAGAIN;
1453 goto unlock;
1456 for (;;) {
1457 long tout;
1458 struct snd_pcm_runtime *to_check;
1459 if (signal_pending(current)) {
1460 result = -ERESTARTSYS;
1461 break;
1463 /* find a substream to drain */
1464 to_check = NULL;
1465 snd_pcm_group_for_each_entry(s, substream) {
1466 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1467 continue;
1468 runtime = s->runtime;
1469 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1470 to_check = runtime;
1471 break;
1474 if (!to_check)
1475 break; /* all drained */
1476 init_waitqueue_entry(&wait, current);
1477 add_wait_queue(&to_check->sleep, &wait);
1478 set_current_state(TASK_INTERRUPTIBLE);
1479 snd_pcm_stream_unlock_irq(substream);
1480 up_read(&snd_pcm_link_rwsem);
1481 snd_power_unlock(card);
1482 tout = schedule_timeout(10 * HZ);
1483 snd_power_lock(card);
1484 down_read(&snd_pcm_link_rwsem);
1485 snd_pcm_stream_lock_irq(substream);
1486 remove_wait_queue(&to_check->sleep, &wait);
1487 if (tout == 0) {
1488 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1489 result = -ESTRPIPE;
1490 else {
1491 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1492 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1493 result = -EIO;
1495 break;
1499 unlock:
1500 snd_pcm_stream_unlock_irq(substream);
1501 up_read(&snd_pcm_link_rwsem);
1502 snd_power_unlock(card);
1504 return result;
1508 * drop ioctl
1510 * Immediately put all linked substreams into SETUP state.
1512 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1514 struct snd_pcm_runtime *runtime;
1515 struct snd_card *card;
1516 int result = 0;
1518 if (PCM_RUNTIME_CHECK(substream))
1519 return -ENXIO;
1520 runtime = substream->runtime;
1521 card = substream->pcm->card;
1523 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1524 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1525 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1526 return -EBADFD;
1528 snd_pcm_stream_lock_irq(substream);
1529 /* resume pause */
1530 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1531 snd_pcm_pause(substream, 0);
1533 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1534 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1535 snd_pcm_stream_unlock_irq(substream);
1537 return result;
1541 /* WARNING: Don't forget to fput back the file */
1542 static struct file *snd_pcm_file_fd(int fd)
1544 struct file *file;
1545 struct inode *inode;
1546 unsigned int minor;
1548 file = fget(fd);
1549 if (!file)
1550 return NULL;
1551 inode = file->f_path.dentry->d_inode;
1552 if (!S_ISCHR(inode->i_mode) ||
1553 imajor(inode) != snd_major) {
1554 fput(file);
1555 return NULL;
1557 minor = iminor(inode);
1558 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1559 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1560 fput(file);
1561 return NULL;
1563 return file;
1567 * PCM link handling
1569 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1571 int res = 0;
1572 struct file *file;
1573 struct snd_pcm_file *pcm_file;
1574 struct snd_pcm_substream *substream1;
1576 file = snd_pcm_file_fd(fd);
1577 if (!file)
1578 return -EBADFD;
1579 pcm_file = file->private_data;
1580 substream1 = pcm_file->substream;
1581 down_write(&snd_pcm_link_rwsem);
1582 write_lock_irq(&snd_pcm_link_rwlock);
1583 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1584 substream->runtime->status->state != substream1->runtime->status->state) {
1585 res = -EBADFD;
1586 goto _end;
1588 if (snd_pcm_stream_linked(substream1)) {
1589 res = -EALREADY;
1590 goto _end;
1592 if (!snd_pcm_stream_linked(substream)) {
1593 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1594 if (substream->group == NULL) {
1595 res = -ENOMEM;
1596 goto _end;
1598 spin_lock_init(&substream->group->lock);
1599 INIT_LIST_HEAD(&substream->group->substreams);
1600 list_add_tail(&substream->link_list, &substream->group->substreams);
1601 substream->group->count = 1;
1603 list_add_tail(&substream1->link_list, &substream->group->substreams);
1604 substream->group->count++;
1605 substream1->group = substream->group;
1606 _end:
1607 write_unlock_irq(&snd_pcm_link_rwlock);
1608 up_write(&snd_pcm_link_rwsem);
1609 fput(file);
1610 return res;
1613 static void relink_to_local(struct snd_pcm_substream *substream)
1615 substream->group = &substream->self_group;
1616 INIT_LIST_HEAD(&substream->self_group.substreams);
1617 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1620 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1622 struct snd_pcm_substream *s;
1623 int res = 0;
1625 down_write(&snd_pcm_link_rwsem);
1626 write_lock_irq(&snd_pcm_link_rwlock);
1627 if (!snd_pcm_stream_linked(substream)) {
1628 res = -EALREADY;
1629 goto _end;
1631 list_del(&substream->link_list);
1632 substream->group->count--;
1633 if (substream->group->count == 1) { /* detach the last stream, too */
1634 snd_pcm_group_for_each_entry(s, substream) {
1635 relink_to_local(s);
1636 break;
1638 kfree(substream->group);
1640 relink_to_local(substream);
1641 _end:
1642 write_unlock_irq(&snd_pcm_link_rwlock);
1643 up_write(&snd_pcm_link_rwsem);
1644 return res;
1648 * hw configurator
1650 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1651 struct snd_pcm_hw_rule *rule)
1653 struct snd_interval t;
1654 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1655 hw_param_interval_c(params, rule->deps[1]), &t);
1656 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1659 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1660 struct snd_pcm_hw_rule *rule)
1662 struct snd_interval t;
1663 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1664 hw_param_interval_c(params, rule->deps[1]), &t);
1665 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1668 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1669 struct snd_pcm_hw_rule *rule)
1671 struct snd_interval t;
1672 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1673 hw_param_interval_c(params, rule->deps[1]),
1674 (unsigned long) rule->private, &t);
1675 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1678 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1679 struct snd_pcm_hw_rule *rule)
1681 struct snd_interval t;
1682 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1683 (unsigned long) rule->private,
1684 hw_param_interval_c(params, rule->deps[1]), &t);
1685 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1688 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1689 struct snd_pcm_hw_rule *rule)
1691 unsigned int k;
1692 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1693 struct snd_mask m;
1694 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1695 snd_mask_any(&m);
1696 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1697 int bits;
1698 if (! snd_mask_test(mask, k))
1699 continue;
1700 bits = snd_pcm_format_physical_width(k);
1701 if (bits <= 0)
1702 continue; /* ignore invalid formats */
1703 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1704 snd_mask_reset(&m, k);
1706 return snd_mask_refine(mask, &m);
1709 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1710 struct snd_pcm_hw_rule *rule)
1712 struct snd_interval t;
1713 unsigned int k;
1714 t.min = UINT_MAX;
1715 t.max = 0;
1716 t.openmin = 0;
1717 t.openmax = 0;
1718 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1719 int bits;
1720 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1721 continue;
1722 bits = snd_pcm_format_physical_width(k);
1723 if (bits <= 0)
1724 continue; /* ignore invalid formats */
1725 if (t.min > (unsigned)bits)
1726 t.min = bits;
1727 if (t.max < (unsigned)bits)
1728 t.max = bits;
1730 t.integer = 1;
1731 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1734 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1735 #error "Change this table"
1736 #endif
1738 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1739 48000, 64000, 88200, 96000, 176400, 192000 };
1741 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1742 .count = ARRAY_SIZE(rates),
1743 .list = rates,
1746 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1747 struct snd_pcm_hw_rule *rule)
1749 struct snd_pcm_hardware *hw = rule->private;
1750 return snd_interval_list(hw_param_interval(params, rule->var),
1751 snd_pcm_known_rates.count,
1752 snd_pcm_known_rates.list, hw->rates);
1755 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1756 struct snd_pcm_hw_rule *rule)
1758 struct snd_interval t;
1759 struct snd_pcm_substream *substream = rule->private;
1760 t.min = 0;
1761 t.max = substream->buffer_bytes_max;
1762 t.openmin = 0;
1763 t.openmax = 0;
1764 t.integer = 1;
1765 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1768 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1770 struct snd_pcm_runtime *runtime = substream->runtime;
1771 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1772 int k, err;
1774 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1775 snd_mask_any(constrs_mask(constrs, k));
1778 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1779 snd_interval_any(constrs_interval(constrs, k));
1782 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1783 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1785 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1786 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1788 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1789 snd_pcm_hw_rule_format, NULL,
1790 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1791 if (err < 0)
1792 return err;
1793 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1794 snd_pcm_hw_rule_sample_bits, NULL,
1795 SNDRV_PCM_HW_PARAM_FORMAT,
1796 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1797 if (err < 0)
1798 return err;
1799 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1800 snd_pcm_hw_rule_div, NULL,
1801 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1802 if (err < 0)
1803 return err;
1804 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1805 snd_pcm_hw_rule_mul, NULL,
1806 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1807 if (err < 0)
1808 return err;
1809 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1810 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1811 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1812 if (err < 0)
1813 return err;
1814 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1815 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1816 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1817 if (err < 0)
1818 return err;
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1820 snd_pcm_hw_rule_div, NULL,
1821 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1822 if (err < 0)
1823 return err;
1824 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1825 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1826 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1827 if (err < 0)
1828 return err;
1829 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1830 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1831 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1832 if (err < 0)
1833 return err;
1834 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1835 snd_pcm_hw_rule_div, NULL,
1836 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1837 if (err < 0)
1838 return err;
1839 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1840 snd_pcm_hw_rule_div, NULL,
1841 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1842 if (err < 0)
1843 return err;
1844 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1845 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1846 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1847 if (err < 0)
1848 return err;
1849 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1850 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1851 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1852 if (err < 0)
1853 return err;
1854 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1855 snd_pcm_hw_rule_mul, NULL,
1856 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1857 if (err < 0)
1858 return err;
1859 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1860 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1861 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1862 if (err < 0)
1863 return err;
1864 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1865 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1866 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1867 if (err < 0)
1868 return err;
1869 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1870 snd_pcm_hw_rule_muldivk, (void*) 8,
1871 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1872 if (err < 0)
1873 return err;
1874 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1875 snd_pcm_hw_rule_muldivk, (void*) 8,
1876 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1877 if (err < 0)
1878 return err;
1879 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1880 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1881 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1882 if (err < 0)
1883 return err;
1884 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1885 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1886 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1887 if (err < 0)
1888 return err;
1889 return 0;
1892 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1894 struct snd_pcm_runtime *runtime = substream->runtime;
1895 struct snd_pcm_hardware *hw = &runtime->hw;
1896 int err;
1897 unsigned int mask = 0;
1899 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1900 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1901 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1902 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1903 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1904 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1905 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1906 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1909 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1911 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1912 if (err < 0)
1913 return err;
1915 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1916 if (err < 0)
1917 return err;
1919 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1920 if (err < 0)
1921 return err;
1923 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1924 hw->channels_min, hw->channels_max);
1925 if (err < 0)
1926 return err;
1928 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1929 hw->rate_min, hw->rate_max);
1930 if (err < 0)
1931 return err;
1933 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1934 hw->period_bytes_min, hw->period_bytes_max);
1935 if (err < 0)
1936 return err;
1938 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1939 hw->periods_min, hw->periods_max);
1940 if (err < 0)
1941 return err;
1943 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1944 hw->period_bytes_min, hw->buffer_bytes_max);
1945 if (err < 0)
1946 return err;
1948 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1949 snd_pcm_hw_rule_buffer_bytes_max, substream,
1950 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1951 if (err < 0)
1952 return err;
1954 if (runtime->dma_bytes) {
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1956 if (err < 0)
1957 return -EINVAL;
1960 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1961 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1962 snd_pcm_hw_rule_rate, hw,
1963 SNDRV_PCM_HW_PARAM_RATE, -1);
1964 if (err < 0)
1965 return err;
1968 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1970 return 0;
1973 static void pcm_release_private(struct snd_pcm_substream *substream)
1975 snd_pcm_unlink(substream);
1978 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1980 substream->ref_count--;
1981 if (substream->ref_count > 0)
1982 return;
1984 snd_pcm_drop(substream);
1985 if (substream->hw_opened) {
1986 if (substream->ops->hw_free != NULL)
1987 substream->ops->hw_free(substream);
1988 substream->ops->close(substream);
1989 substream->hw_opened = 0;
1991 if (pm_qos_request_active(&substream->latency_pm_qos_req))
1992 pm_qos_remove_request(&substream->latency_pm_qos_req);
1993 if (substream->pcm_release) {
1994 substream->pcm_release(substream);
1995 substream->pcm_release = NULL;
1997 snd_pcm_detach_substream(substream);
2000 EXPORT_SYMBOL(snd_pcm_release_substream);
2002 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2003 struct file *file,
2004 struct snd_pcm_substream **rsubstream)
2006 struct snd_pcm_substream *substream;
2007 int err;
2009 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2010 if (err < 0)
2011 return err;
2012 if (substream->ref_count > 1) {
2013 *rsubstream = substream;
2014 return 0;
2017 err = snd_pcm_hw_constraints_init(substream);
2018 if (err < 0) {
2019 snd_printd("snd_pcm_hw_constraints_init failed\n");
2020 goto error;
2023 if ((err = substream->ops->open(substream)) < 0)
2024 goto error;
2026 substream->hw_opened = 1;
2028 err = snd_pcm_hw_constraints_complete(substream);
2029 if (err < 0) {
2030 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2031 goto error;
2034 *rsubstream = substream;
2035 return 0;
2037 error:
2038 snd_pcm_release_substream(substream);
2039 return err;
2042 EXPORT_SYMBOL(snd_pcm_open_substream);
2044 static int snd_pcm_open_file(struct file *file,
2045 struct snd_pcm *pcm,
2046 int stream,
2047 struct snd_pcm_file **rpcm_file)
2049 struct snd_pcm_file *pcm_file;
2050 struct snd_pcm_substream *substream;
2051 struct snd_pcm_str *str;
2052 int err;
2054 if (rpcm_file)
2055 *rpcm_file = NULL;
2057 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2058 if (err < 0)
2059 return err;
2061 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2062 if (pcm_file == NULL) {
2063 snd_pcm_release_substream(substream);
2064 return -ENOMEM;
2066 pcm_file->substream = substream;
2067 if (substream->ref_count == 1) {
2068 str = substream->pstr;
2069 substream->file = pcm_file;
2070 substream->pcm_release = pcm_release_private;
2072 file->private_data = pcm_file;
2073 if (rpcm_file)
2074 *rpcm_file = pcm_file;
2075 return 0;
2078 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2080 struct snd_pcm *pcm;
2081 int err = nonseekable_open(inode, file);
2082 if (err < 0)
2083 return err;
2084 pcm = snd_lookup_minor_data(iminor(inode),
2085 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2086 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2089 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2091 struct snd_pcm *pcm;
2092 int err = nonseekable_open(inode, file);
2093 if (err < 0)
2094 return err;
2095 pcm = snd_lookup_minor_data(iminor(inode),
2096 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2097 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2100 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2102 int err;
2103 struct snd_pcm_file *pcm_file;
2104 wait_queue_t wait;
2106 if (pcm == NULL) {
2107 err = -ENODEV;
2108 goto __error1;
2110 err = snd_card_file_add(pcm->card, file);
2111 if (err < 0)
2112 goto __error1;
2113 if (!try_module_get(pcm->card->module)) {
2114 err = -EFAULT;
2115 goto __error2;
2117 init_waitqueue_entry(&wait, current);
2118 add_wait_queue(&pcm->open_wait, &wait);
2119 mutex_lock(&pcm->open_mutex);
2120 while (1) {
2121 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2122 if (err >= 0)
2123 break;
2124 if (err == -EAGAIN) {
2125 if (file->f_flags & O_NONBLOCK) {
2126 err = -EBUSY;
2127 break;
2129 } else
2130 break;
2131 set_current_state(TASK_INTERRUPTIBLE);
2132 mutex_unlock(&pcm->open_mutex);
2133 schedule();
2134 mutex_lock(&pcm->open_mutex);
2135 if (signal_pending(current)) {
2136 err = -ERESTARTSYS;
2137 break;
2140 remove_wait_queue(&pcm->open_wait, &wait);
2141 mutex_unlock(&pcm->open_mutex);
2142 if (err < 0)
2143 goto __error;
2144 return err;
2146 __error:
2147 module_put(pcm->card->module);
2148 __error2:
2149 snd_card_file_remove(pcm->card, file);
2150 __error1:
2151 return err;
2154 static int snd_pcm_release(struct inode *inode, struct file *file)
2156 struct snd_pcm *pcm;
2157 struct snd_pcm_substream *substream;
2158 struct snd_pcm_file *pcm_file;
2160 pcm_file = file->private_data;
2161 substream = pcm_file->substream;
2162 if (snd_BUG_ON(!substream))
2163 return -ENXIO;
2164 pcm = substream->pcm;
2165 mutex_lock(&pcm->open_mutex);
2166 snd_pcm_release_substream(substream);
2167 kfree(pcm_file);
2168 mutex_unlock(&pcm->open_mutex);
2169 wake_up(&pcm->open_wait);
2170 module_put(pcm->card->module);
2171 snd_card_file_remove(pcm->card, file);
2172 return 0;
2175 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2176 snd_pcm_uframes_t frames)
2178 struct snd_pcm_runtime *runtime = substream->runtime;
2179 snd_pcm_sframes_t appl_ptr;
2180 snd_pcm_sframes_t ret;
2181 snd_pcm_sframes_t hw_avail;
2183 if (frames == 0)
2184 return 0;
2186 snd_pcm_stream_lock_irq(substream);
2187 switch (runtime->status->state) {
2188 case SNDRV_PCM_STATE_PREPARED:
2189 break;
2190 case SNDRV_PCM_STATE_DRAINING:
2191 case SNDRV_PCM_STATE_RUNNING:
2192 if (snd_pcm_update_hw_ptr(substream) >= 0)
2193 break;
2194 /* Fall through */
2195 case SNDRV_PCM_STATE_XRUN:
2196 ret = -EPIPE;
2197 goto __end;
2198 case SNDRV_PCM_STATE_SUSPENDED:
2199 ret = -ESTRPIPE;
2200 goto __end;
2201 default:
2202 ret = -EBADFD;
2203 goto __end;
2206 hw_avail = snd_pcm_playback_hw_avail(runtime);
2207 if (hw_avail <= 0) {
2208 ret = 0;
2209 goto __end;
2211 if (frames > (snd_pcm_uframes_t)hw_avail)
2212 frames = hw_avail;
2213 appl_ptr = runtime->control->appl_ptr - frames;
2214 if (appl_ptr < 0)
2215 appl_ptr += runtime->boundary;
2216 runtime->control->appl_ptr = appl_ptr;
2217 ret = frames;
2218 __end:
2219 snd_pcm_stream_unlock_irq(substream);
2220 return ret;
2223 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2224 snd_pcm_uframes_t frames)
2226 struct snd_pcm_runtime *runtime = substream->runtime;
2227 snd_pcm_sframes_t appl_ptr;
2228 snd_pcm_sframes_t ret;
2229 snd_pcm_sframes_t hw_avail;
2231 if (frames == 0)
2232 return 0;
2234 snd_pcm_stream_lock_irq(substream);
2235 switch (runtime->status->state) {
2236 case SNDRV_PCM_STATE_PREPARED:
2237 case SNDRV_PCM_STATE_DRAINING:
2238 break;
2239 case SNDRV_PCM_STATE_RUNNING:
2240 if (snd_pcm_update_hw_ptr(substream) >= 0)
2241 break;
2242 /* Fall through */
2243 case SNDRV_PCM_STATE_XRUN:
2244 ret = -EPIPE;
2245 goto __end;
2246 case SNDRV_PCM_STATE_SUSPENDED:
2247 ret = -ESTRPIPE;
2248 goto __end;
2249 default:
2250 ret = -EBADFD;
2251 goto __end;
2254 hw_avail = snd_pcm_capture_hw_avail(runtime);
2255 if (hw_avail <= 0) {
2256 ret = 0;
2257 goto __end;
2259 if (frames > (snd_pcm_uframes_t)hw_avail)
2260 frames = hw_avail;
2261 appl_ptr = runtime->control->appl_ptr - frames;
2262 if (appl_ptr < 0)
2263 appl_ptr += runtime->boundary;
2264 runtime->control->appl_ptr = appl_ptr;
2265 ret = frames;
2266 __end:
2267 snd_pcm_stream_unlock_irq(substream);
2268 return ret;
2271 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2272 snd_pcm_uframes_t frames)
2274 struct snd_pcm_runtime *runtime = substream->runtime;
2275 snd_pcm_sframes_t appl_ptr;
2276 snd_pcm_sframes_t ret;
2277 snd_pcm_sframes_t avail;
2279 if (frames == 0)
2280 return 0;
2282 snd_pcm_stream_lock_irq(substream);
2283 switch (runtime->status->state) {
2284 case SNDRV_PCM_STATE_PREPARED:
2285 case SNDRV_PCM_STATE_PAUSED:
2286 break;
2287 case SNDRV_PCM_STATE_DRAINING:
2288 case SNDRV_PCM_STATE_RUNNING:
2289 if (snd_pcm_update_hw_ptr(substream) >= 0)
2290 break;
2291 /* Fall through */
2292 case SNDRV_PCM_STATE_XRUN:
2293 ret = -EPIPE;
2294 goto __end;
2295 case SNDRV_PCM_STATE_SUSPENDED:
2296 ret = -ESTRPIPE;
2297 goto __end;
2298 default:
2299 ret = -EBADFD;
2300 goto __end;
2303 avail = snd_pcm_playback_avail(runtime);
2304 if (avail <= 0) {
2305 ret = 0;
2306 goto __end;
2308 if (frames > (snd_pcm_uframes_t)avail)
2309 frames = avail;
2310 appl_ptr = runtime->control->appl_ptr + frames;
2311 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2312 appl_ptr -= runtime->boundary;
2313 runtime->control->appl_ptr = appl_ptr;
2314 ret = frames;
2315 __end:
2316 snd_pcm_stream_unlock_irq(substream);
2317 return ret;
2320 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2321 snd_pcm_uframes_t frames)
2323 struct snd_pcm_runtime *runtime = substream->runtime;
2324 snd_pcm_sframes_t appl_ptr;
2325 snd_pcm_sframes_t ret;
2326 snd_pcm_sframes_t avail;
2328 if (frames == 0)
2329 return 0;
2331 snd_pcm_stream_lock_irq(substream);
2332 switch (runtime->status->state) {
2333 case SNDRV_PCM_STATE_PREPARED:
2334 case SNDRV_PCM_STATE_DRAINING:
2335 case SNDRV_PCM_STATE_PAUSED:
2336 break;
2337 case SNDRV_PCM_STATE_RUNNING:
2338 if (snd_pcm_update_hw_ptr(substream) >= 0)
2339 break;
2340 /* Fall through */
2341 case SNDRV_PCM_STATE_XRUN:
2342 ret = -EPIPE;
2343 goto __end;
2344 case SNDRV_PCM_STATE_SUSPENDED:
2345 ret = -ESTRPIPE;
2346 goto __end;
2347 default:
2348 ret = -EBADFD;
2349 goto __end;
2352 avail = snd_pcm_capture_avail(runtime);
2353 if (avail <= 0) {
2354 ret = 0;
2355 goto __end;
2357 if (frames > (snd_pcm_uframes_t)avail)
2358 frames = avail;
2359 appl_ptr = runtime->control->appl_ptr + frames;
2360 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2361 appl_ptr -= runtime->boundary;
2362 runtime->control->appl_ptr = appl_ptr;
2363 ret = frames;
2364 __end:
2365 snd_pcm_stream_unlock_irq(substream);
2366 return ret;
2369 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2371 struct snd_pcm_runtime *runtime = substream->runtime;
2372 int err;
2374 snd_pcm_stream_lock_irq(substream);
2375 switch (runtime->status->state) {
2376 case SNDRV_PCM_STATE_DRAINING:
2377 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2378 goto __badfd;
2379 case SNDRV_PCM_STATE_RUNNING:
2380 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2381 break;
2382 /* Fall through */
2383 case SNDRV_PCM_STATE_PREPARED:
2384 case SNDRV_PCM_STATE_SUSPENDED:
2385 err = 0;
2386 break;
2387 case SNDRV_PCM_STATE_XRUN:
2388 err = -EPIPE;
2389 break;
2390 default:
2391 __badfd:
2392 err = -EBADFD;
2393 break;
2395 snd_pcm_stream_unlock_irq(substream);
2396 return err;
2399 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2400 snd_pcm_sframes_t __user *res)
2402 struct snd_pcm_runtime *runtime = substream->runtime;
2403 int err;
2404 snd_pcm_sframes_t n = 0;
2406 snd_pcm_stream_lock_irq(substream);
2407 switch (runtime->status->state) {
2408 case SNDRV_PCM_STATE_DRAINING:
2409 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2410 goto __badfd;
2411 case SNDRV_PCM_STATE_RUNNING:
2412 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2413 break;
2414 /* Fall through */
2415 case SNDRV_PCM_STATE_PREPARED:
2416 case SNDRV_PCM_STATE_SUSPENDED:
2417 err = 0;
2418 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2419 n = snd_pcm_playback_hw_avail(runtime);
2420 else
2421 n = snd_pcm_capture_avail(runtime);
2422 n += runtime->delay;
2423 break;
2424 case SNDRV_PCM_STATE_XRUN:
2425 err = -EPIPE;
2426 break;
2427 default:
2428 __badfd:
2429 err = -EBADFD;
2430 break;
2432 snd_pcm_stream_unlock_irq(substream);
2433 if (!err)
2434 if (put_user(n, res))
2435 err = -EFAULT;
2436 return err;
2439 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2440 struct snd_pcm_sync_ptr __user *_sync_ptr)
2442 struct snd_pcm_runtime *runtime = substream->runtime;
2443 struct snd_pcm_sync_ptr sync_ptr;
2444 volatile struct snd_pcm_mmap_status *status;
2445 volatile struct snd_pcm_mmap_control *control;
2446 int err;
2448 memset(&sync_ptr, 0, sizeof(sync_ptr));
2449 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2450 return -EFAULT;
2451 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2452 return -EFAULT;
2453 status = runtime->status;
2454 control = runtime->control;
2455 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2456 err = snd_pcm_hwsync(substream);
2457 if (err < 0)
2458 return err;
2460 snd_pcm_stream_lock_irq(substream);
2461 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2462 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2463 else
2464 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2465 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2466 control->avail_min = sync_ptr.c.control.avail_min;
2467 else
2468 sync_ptr.c.control.avail_min = control->avail_min;
2469 sync_ptr.s.status.state = status->state;
2470 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2471 sync_ptr.s.status.tstamp = status->tstamp;
2472 sync_ptr.s.status.suspended_state = status->suspended_state;
2473 snd_pcm_stream_unlock_irq(substream);
2474 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2475 return -EFAULT;
2476 return 0;
2479 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2481 struct snd_pcm_runtime *runtime = substream->runtime;
2482 int arg;
2484 if (get_user(arg, _arg))
2485 return -EFAULT;
2486 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2487 return -EINVAL;
2488 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2489 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2490 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2491 return 0;
2494 static int snd_pcm_common_ioctl1(struct file *file,
2495 struct snd_pcm_substream *substream,
2496 unsigned int cmd, void __user *arg)
2498 switch (cmd) {
2499 case SNDRV_PCM_IOCTL_PVERSION:
2500 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2501 case SNDRV_PCM_IOCTL_INFO:
2502 return snd_pcm_info_user(substream, arg);
2503 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2504 return 0;
2505 case SNDRV_PCM_IOCTL_TTSTAMP:
2506 return snd_pcm_tstamp(substream, arg);
2507 case SNDRV_PCM_IOCTL_HW_REFINE:
2508 return snd_pcm_hw_refine_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_HW_PARAMS:
2510 return snd_pcm_hw_params_user(substream, arg);
2511 case SNDRV_PCM_IOCTL_HW_FREE:
2512 return snd_pcm_hw_free(substream);
2513 case SNDRV_PCM_IOCTL_SW_PARAMS:
2514 return snd_pcm_sw_params_user(substream, arg);
2515 case SNDRV_PCM_IOCTL_STATUS:
2516 return snd_pcm_status_user(substream, arg);
2517 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2518 return snd_pcm_channel_info_user(substream, arg);
2519 case SNDRV_PCM_IOCTL_PREPARE:
2520 return snd_pcm_prepare(substream, file);
2521 case SNDRV_PCM_IOCTL_RESET:
2522 return snd_pcm_reset(substream);
2523 case SNDRV_PCM_IOCTL_START:
2524 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2525 case SNDRV_PCM_IOCTL_LINK:
2526 return snd_pcm_link(substream, (int)(unsigned long) arg);
2527 case SNDRV_PCM_IOCTL_UNLINK:
2528 return snd_pcm_unlink(substream);
2529 case SNDRV_PCM_IOCTL_RESUME:
2530 return snd_pcm_resume(substream);
2531 case SNDRV_PCM_IOCTL_XRUN:
2532 return snd_pcm_xrun(substream);
2533 case SNDRV_PCM_IOCTL_HWSYNC:
2534 return snd_pcm_hwsync(substream);
2535 case SNDRV_PCM_IOCTL_DELAY:
2536 return snd_pcm_delay(substream, arg);
2537 case SNDRV_PCM_IOCTL_SYNC_PTR:
2538 return snd_pcm_sync_ptr(substream, arg);
2539 #ifdef CONFIG_SND_SUPPORT_OLD_API
2540 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2541 return snd_pcm_hw_refine_old_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2543 return snd_pcm_hw_params_old_user(substream, arg);
2544 #endif
2545 case SNDRV_PCM_IOCTL_DRAIN:
2546 return snd_pcm_drain(substream, file);
2547 case SNDRV_PCM_IOCTL_DROP:
2548 return snd_pcm_drop(substream);
2549 case SNDRV_PCM_IOCTL_PAUSE:
2551 int res;
2552 snd_pcm_stream_lock_irq(substream);
2553 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2554 snd_pcm_stream_unlock_irq(substream);
2555 return res;
2558 snd_printd("unknown ioctl = 0x%x\n", cmd);
2559 return -ENOTTY;
2562 static int snd_pcm_playback_ioctl1(struct file *file,
2563 struct snd_pcm_substream *substream,
2564 unsigned int cmd, void __user *arg)
2566 if (snd_BUG_ON(!substream))
2567 return -ENXIO;
2568 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2569 return -EINVAL;
2570 switch (cmd) {
2571 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2573 struct snd_xferi xferi;
2574 struct snd_xferi __user *_xferi = arg;
2575 struct snd_pcm_runtime *runtime = substream->runtime;
2576 snd_pcm_sframes_t result;
2577 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2578 return -EBADFD;
2579 if (put_user(0, &_xferi->result))
2580 return -EFAULT;
2581 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2582 return -EFAULT;
2583 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2584 __put_user(result, &_xferi->result);
2585 return result < 0 ? result : 0;
2587 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2589 struct snd_xfern xfern;
2590 struct snd_xfern __user *_xfern = arg;
2591 struct snd_pcm_runtime *runtime = substream->runtime;
2592 void __user **bufs;
2593 snd_pcm_sframes_t result;
2594 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2595 return -EBADFD;
2596 if (runtime->channels > 128)
2597 return -EINVAL;
2598 if (put_user(0, &_xfern->result))
2599 return -EFAULT;
2600 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2601 return -EFAULT;
2603 bufs = memdup_user(xfern.bufs,
2604 sizeof(void *) * runtime->channels);
2605 if (IS_ERR(bufs))
2606 return PTR_ERR(bufs);
2607 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2608 kfree(bufs);
2609 __put_user(result, &_xfern->result);
2610 return result < 0 ? result : 0;
2612 case SNDRV_PCM_IOCTL_REWIND:
2614 snd_pcm_uframes_t frames;
2615 snd_pcm_uframes_t __user *_frames = arg;
2616 snd_pcm_sframes_t result;
2617 if (get_user(frames, _frames))
2618 return -EFAULT;
2619 if (put_user(0, _frames))
2620 return -EFAULT;
2621 result = snd_pcm_playback_rewind(substream, frames);
2622 __put_user(result, _frames);
2623 return result < 0 ? result : 0;
2625 case SNDRV_PCM_IOCTL_FORWARD:
2627 snd_pcm_uframes_t frames;
2628 snd_pcm_uframes_t __user *_frames = arg;
2629 snd_pcm_sframes_t result;
2630 if (get_user(frames, _frames))
2631 return -EFAULT;
2632 if (put_user(0, _frames))
2633 return -EFAULT;
2634 result = snd_pcm_playback_forward(substream, frames);
2635 __put_user(result, _frames);
2636 return result < 0 ? result : 0;
2639 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2642 static int snd_pcm_capture_ioctl1(struct file *file,
2643 struct snd_pcm_substream *substream,
2644 unsigned int cmd, void __user *arg)
2646 if (snd_BUG_ON(!substream))
2647 return -ENXIO;
2648 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2649 return -EINVAL;
2650 switch (cmd) {
2651 case SNDRV_PCM_IOCTL_READI_FRAMES:
2653 struct snd_xferi xferi;
2654 struct snd_xferi __user *_xferi = arg;
2655 struct snd_pcm_runtime *runtime = substream->runtime;
2656 snd_pcm_sframes_t result;
2657 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2658 return -EBADFD;
2659 if (put_user(0, &_xferi->result))
2660 return -EFAULT;
2661 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2662 return -EFAULT;
2663 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2664 __put_user(result, &_xferi->result);
2665 return result < 0 ? result : 0;
2667 case SNDRV_PCM_IOCTL_READN_FRAMES:
2669 struct snd_xfern xfern;
2670 struct snd_xfern __user *_xfern = arg;
2671 struct snd_pcm_runtime *runtime = substream->runtime;
2672 void *bufs;
2673 snd_pcm_sframes_t result;
2674 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2675 return -EBADFD;
2676 if (runtime->channels > 128)
2677 return -EINVAL;
2678 if (put_user(0, &_xfern->result))
2679 return -EFAULT;
2680 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2681 return -EFAULT;
2683 bufs = memdup_user(xfern.bufs,
2684 sizeof(void *) * runtime->channels);
2685 if (IS_ERR(bufs))
2686 return PTR_ERR(bufs);
2687 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2688 kfree(bufs);
2689 __put_user(result, &_xfern->result);
2690 return result < 0 ? result : 0;
2692 case SNDRV_PCM_IOCTL_REWIND:
2694 snd_pcm_uframes_t frames;
2695 snd_pcm_uframes_t __user *_frames = arg;
2696 snd_pcm_sframes_t result;
2697 if (get_user(frames, _frames))
2698 return -EFAULT;
2699 if (put_user(0, _frames))
2700 return -EFAULT;
2701 result = snd_pcm_capture_rewind(substream, frames);
2702 __put_user(result, _frames);
2703 return result < 0 ? result : 0;
2705 case SNDRV_PCM_IOCTL_FORWARD:
2707 snd_pcm_uframes_t frames;
2708 snd_pcm_uframes_t __user *_frames = arg;
2709 snd_pcm_sframes_t result;
2710 if (get_user(frames, _frames))
2711 return -EFAULT;
2712 if (put_user(0, _frames))
2713 return -EFAULT;
2714 result = snd_pcm_capture_forward(substream, frames);
2715 __put_user(result, _frames);
2716 return result < 0 ? result : 0;
2719 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2722 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2723 unsigned long arg)
2725 struct snd_pcm_file *pcm_file;
2727 pcm_file = file->private_data;
2729 if (((cmd >> 8) & 0xff) != 'A')
2730 return -ENOTTY;
2732 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2733 (void __user *)arg);
2736 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2737 unsigned long arg)
2739 struct snd_pcm_file *pcm_file;
2741 pcm_file = file->private_data;
2743 if (((cmd >> 8) & 0xff) != 'A')
2744 return -ENOTTY;
2746 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2747 (void __user *)arg);
2750 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2751 unsigned int cmd, void *arg)
2753 mm_segment_t fs;
2754 int result;
2756 fs = snd_enter_user();
2757 switch (substream->stream) {
2758 case SNDRV_PCM_STREAM_PLAYBACK:
2759 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2760 (void __user *)arg);
2761 break;
2762 case SNDRV_PCM_STREAM_CAPTURE:
2763 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2764 (void __user *)arg);
2765 break;
2766 default:
2767 result = -EINVAL;
2768 break;
2770 snd_leave_user(fs);
2771 return result;
2774 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2776 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2777 loff_t * offset)
2779 struct snd_pcm_file *pcm_file;
2780 struct snd_pcm_substream *substream;
2781 struct snd_pcm_runtime *runtime;
2782 snd_pcm_sframes_t result;
2784 pcm_file = file->private_data;
2785 substream = pcm_file->substream;
2786 if (PCM_RUNTIME_CHECK(substream))
2787 return -ENXIO;
2788 runtime = substream->runtime;
2789 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2790 return -EBADFD;
2791 if (!frame_aligned(runtime, count))
2792 return -EINVAL;
2793 count = bytes_to_frames(runtime, count);
2794 result = snd_pcm_lib_read(substream, buf, count);
2795 if (result > 0)
2796 result = frames_to_bytes(runtime, result);
2797 return result;
2800 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2801 size_t count, loff_t * offset)
2803 struct snd_pcm_file *pcm_file;
2804 struct snd_pcm_substream *substream;
2805 struct snd_pcm_runtime *runtime;
2806 snd_pcm_sframes_t result;
2808 pcm_file = file->private_data;
2809 substream = pcm_file->substream;
2810 if (PCM_RUNTIME_CHECK(substream))
2811 return -ENXIO;
2812 runtime = substream->runtime;
2813 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2814 return -EBADFD;
2815 if (!frame_aligned(runtime, count))
2816 return -EINVAL;
2817 count = bytes_to_frames(runtime, count);
2818 result = snd_pcm_lib_write(substream, buf, count);
2819 if (result > 0)
2820 result = frames_to_bytes(runtime, result);
2821 return result;
2824 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2825 unsigned long nr_segs, loff_t pos)
2828 struct snd_pcm_file *pcm_file;
2829 struct snd_pcm_substream *substream;
2830 struct snd_pcm_runtime *runtime;
2831 snd_pcm_sframes_t result;
2832 unsigned long i;
2833 void __user **bufs;
2834 snd_pcm_uframes_t frames;
2836 pcm_file = iocb->ki_filp->private_data;
2837 substream = pcm_file->substream;
2838 if (PCM_RUNTIME_CHECK(substream))
2839 return -ENXIO;
2840 runtime = substream->runtime;
2841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2842 return -EBADFD;
2843 if (nr_segs > 1024 || nr_segs != runtime->channels)
2844 return -EINVAL;
2845 if (!frame_aligned(runtime, iov->iov_len))
2846 return -EINVAL;
2847 frames = bytes_to_samples(runtime, iov->iov_len);
2848 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2849 if (bufs == NULL)
2850 return -ENOMEM;
2851 for (i = 0; i < nr_segs; ++i)
2852 bufs[i] = iov[i].iov_base;
2853 result = snd_pcm_lib_readv(substream, bufs, frames);
2854 if (result > 0)
2855 result = frames_to_bytes(runtime, result);
2856 kfree(bufs);
2857 return result;
2860 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2861 unsigned long nr_segs, loff_t pos)
2863 struct snd_pcm_file *pcm_file;
2864 struct snd_pcm_substream *substream;
2865 struct snd_pcm_runtime *runtime;
2866 snd_pcm_sframes_t result;
2867 unsigned long i;
2868 void __user **bufs;
2869 snd_pcm_uframes_t frames;
2871 pcm_file = iocb->ki_filp->private_data;
2872 substream = pcm_file->substream;
2873 if (PCM_RUNTIME_CHECK(substream))
2874 return -ENXIO;
2875 runtime = substream->runtime;
2876 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2877 return -EBADFD;
2878 if (nr_segs > 128 || nr_segs != runtime->channels ||
2879 !frame_aligned(runtime, iov->iov_len))
2880 return -EINVAL;
2881 frames = bytes_to_samples(runtime, iov->iov_len);
2882 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2883 if (bufs == NULL)
2884 return -ENOMEM;
2885 for (i = 0; i < nr_segs; ++i)
2886 bufs[i] = iov[i].iov_base;
2887 result = snd_pcm_lib_writev(substream, bufs, frames);
2888 if (result > 0)
2889 result = frames_to_bytes(runtime, result);
2890 kfree(bufs);
2891 return result;
2894 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2896 struct snd_pcm_file *pcm_file;
2897 struct snd_pcm_substream *substream;
2898 struct snd_pcm_runtime *runtime;
2899 unsigned int mask;
2900 snd_pcm_uframes_t avail;
2902 pcm_file = file->private_data;
2904 substream = pcm_file->substream;
2905 if (PCM_RUNTIME_CHECK(substream))
2906 return -ENXIO;
2907 runtime = substream->runtime;
2909 poll_wait(file, &runtime->sleep, wait);
2911 snd_pcm_stream_lock_irq(substream);
2912 avail = snd_pcm_playback_avail(runtime);
2913 switch (runtime->status->state) {
2914 case SNDRV_PCM_STATE_RUNNING:
2915 case SNDRV_PCM_STATE_PREPARED:
2916 case SNDRV_PCM_STATE_PAUSED:
2917 if (avail >= runtime->control->avail_min) {
2918 mask = POLLOUT | POLLWRNORM;
2919 break;
2921 /* Fall through */
2922 case SNDRV_PCM_STATE_DRAINING:
2923 mask = 0;
2924 break;
2925 default:
2926 mask = POLLOUT | POLLWRNORM | POLLERR;
2927 break;
2929 snd_pcm_stream_unlock_irq(substream);
2930 return mask;
2933 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2935 struct snd_pcm_file *pcm_file;
2936 struct snd_pcm_substream *substream;
2937 struct snd_pcm_runtime *runtime;
2938 unsigned int mask;
2939 snd_pcm_uframes_t avail;
2941 pcm_file = file->private_data;
2943 substream = pcm_file->substream;
2944 if (PCM_RUNTIME_CHECK(substream))
2945 return -ENXIO;
2946 runtime = substream->runtime;
2948 poll_wait(file, &runtime->sleep, wait);
2950 snd_pcm_stream_lock_irq(substream);
2951 avail = snd_pcm_capture_avail(runtime);
2952 switch (runtime->status->state) {
2953 case SNDRV_PCM_STATE_RUNNING:
2954 case SNDRV_PCM_STATE_PREPARED:
2955 case SNDRV_PCM_STATE_PAUSED:
2956 if (avail >= runtime->control->avail_min) {
2957 mask = POLLIN | POLLRDNORM;
2958 break;
2960 mask = 0;
2961 break;
2962 case SNDRV_PCM_STATE_DRAINING:
2963 if (avail > 0) {
2964 mask = POLLIN | POLLRDNORM;
2965 break;
2967 /* Fall through */
2968 default:
2969 mask = POLLIN | POLLRDNORM | POLLERR;
2970 break;
2972 snd_pcm_stream_unlock_irq(substream);
2973 return mask;
2977 * mmap support
2981 * Only on coherent architectures, we can mmap the status and the control records
2982 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2984 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2986 * mmap status record
2988 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2989 struct vm_fault *vmf)
2991 struct snd_pcm_substream *substream = area->vm_private_data;
2992 struct snd_pcm_runtime *runtime;
2994 if (substream == NULL)
2995 return VM_FAULT_SIGBUS;
2996 runtime = substream->runtime;
2997 vmf->page = virt_to_page(runtime->status);
2998 get_page(vmf->page);
2999 return 0;
3002 static const struct vm_operations_struct snd_pcm_vm_ops_status =
3004 .fault = snd_pcm_mmap_status_fault,
3007 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3008 struct vm_area_struct *area)
3010 struct snd_pcm_runtime *runtime;
3011 long size;
3012 if (!(area->vm_flags & VM_READ))
3013 return -EINVAL;
3014 runtime = substream->runtime;
3015 size = area->vm_end - area->vm_start;
3016 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3017 return -EINVAL;
3018 area->vm_ops = &snd_pcm_vm_ops_status;
3019 area->vm_private_data = substream;
3020 area->vm_flags |= VM_RESERVED;
3021 return 0;
3025 * mmap control record
3027 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3028 struct vm_fault *vmf)
3030 struct snd_pcm_substream *substream = area->vm_private_data;
3031 struct snd_pcm_runtime *runtime;
3033 if (substream == NULL)
3034 return VM_FAULT_SIGBUS;
3035 runtime = substream->runtime;
3036 vmf->page = virt_to_page(runtime->control);
3037 get_page(vmf->page);
3038 return 0;
3041 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3043 .fault = snd_pcm_mmap_control_fault,
3046 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3047 struct vm_area_struct *area)
3049 struct snd_pcm_runtime *runtime;
3050 long size;
3051 if (!(area->vm_flags & VM_READ))
3052 return -EINVAL;
3053 runtime = substream->runtime;
3054 size = area->vm_end - area->vm_start;
3055 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3056 return -EINVAL;
3057 area->vm_ops = &snd_pcm_vm_ops_control;
3058 area->vm_private_data = substream;
3059 area->vm_flags |= VM_RESERVED;
3060 return 0;
3062 #else /* ! coherent mmap */
3064 * don't support mmap for status and control records.
3066 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3067 struct vm_area_struct *area)
3069 return -ENXIO;
3071 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3072 struct vm_area_struct *area)
3074 return -ENXIO;
3076 #endif /* coherent mmap */
3078 static inline struct page *
3079 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3081 void *vaddr = substream->runtime->dma_area + ofs;
3082 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3083 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3084 return virt_to_page(CAC_ADDR(vaddr));
3085 #endif
3086 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3087 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3088 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3089 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3090 /* assume dma_handle set via pfn_to_phys() in
3091 * mm/dma-noncoherent.c
3093 return pfn_to_page(addr >> PAGE_SHIFT);
3095 #endif
3096 return virt_to_page(vaddr);
3100 * fault callback for mmapping a RAM page
3102 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3103 struct vm_fault *vmf)
3105 struct snd_pcm_substream *substream = area->vm_private_data;
3106 struct snd_pcm_runtime *runtime;
3107 unsigned long offset;
3108 struct page * page;
3109 size_t dma_bytes;
3111 if (substream == NULL)
3112 return VM_FAULT_SIGBUS;
3113 runtime = substream->runtime;
3114 offset = vmf->pgoff << PAGE_SHIFT;
3115 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3116 if (offset > dma_bytes - PAGE_SIZE)
3117 return VM_FAULT_SIGBUS;
3118 if (substream->ops->page)
3119 page = substream->ops->page(substream, offset);
3120 else
3121 page = snd_pcm_default_page_ops(substream, offset);
3122 if (!page)
3123 return VM_FAULT_SIGBUS;
3124 get_page(page);
3125 vmf->page = page;
3126 return 0;
3129 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3130 .open = snd_pcm_mmap_data_open,
3131 .close = snd_pcm_mmap_data_close,
3134 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3135 .open = snd_pcm_mmap_data_open,
3136 .close = snd_pcm_mmap_data_close,
3137 .fault = snd_pcm_mmap_data_fault,
3140 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3141 /* This should be defined / handled globally! */
3142 #ifdef CONFIG_ARM
3143 #define ARCH_HAS_DMA_MMAP_COHERENT
3144 #endif
3145 #endif
3148 * mmap the DMA buffer on RAM
3150 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3151 struct vm_area_struct *area)
3153 area->vm_flags |= VM_RESERVED;
3154 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3155 if (!substream->ops->page &&
3156 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3157 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3158 area,
3159 substream->runtime->dma_area,
3160 substream->runtime->dma_addr,
3161 area->vm_end - area->vm_start);
3162 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3163 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3164 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3165 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3166 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3167 /* mmap with fault handler */
3168 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3169 return 0;
3173 * mmap the DMA buffer on I/O memory area
3175 #if SNDRV_PCM_INFO_MMAP_IOMEM
3176 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3177 struct vm_area_struct *area)
3179 long size;
3180 unsigned long offset;
3182 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3183 area->vm_flags |= VM_IO;
3184 size = area->vm_end - area->vm_start;
3185 offset = area->vm_pgoff << PAGE_SHIFT;
3186 if (io_remap_pfn_range(area, area->vm_start,
3187 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3188 size, area->vm_page_prot))
3189 return -EAGAIN;
3190 return 0;
3193 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3194 #endif /* SNDRV_PCM_INFO_MMAP */
3196 /* mmap callback with pgprot_noncached */
3197 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3198 struct vm_area_struct *area)
3200 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3201 return snd_pcm_default_mmap(substream, area);
3203 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3206 * mmap DMA buffer
3208 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3209 struct vm_area_struct *area)
3211 struct snd_pcm_runtime *runtime;
3212 long size;
3213 unsigned long offset;
3214 size_t dma_bytes;
3215 int err;
3217 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3218 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3219 return -EINVAL;
3220 } else {
3221 if (!(area->vm_flags & VM_READ))
3222 return -EINVAL;
3224 runtime = substream->runtime;
3225 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3226 return -EBADFD;
3227 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3228 return -ENXIO;
3229 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3230 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3231 return -EINVAL;
3232 size = area->vm_end - area->vm_start;
3233 offset = area->vm_pgoff << PAGE_SHIFT;
3234 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3235 if ((size_t)size > dma_bytes)
3236 return -EINVAL;
3237 if (offset > dma_bytes - size)
3238 return -EINVAL;
3240 area->vm_ops = &snd_pcm_vm_ops_data;
3241 area->vm_private_data = substream;
3242 if (substream->ops->mmap)
3243 err = substream->ops->mmap(substream, area);
3244 else
3245 err = snd_pcm_default_mmap(substream, area);
3246 if (!err)
3247 atomic_inc(&substream->mmap_count);
3248 return err;
3251 EXPORT_SYMBOL(snd_pcm_mmap_data);
3253 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3255 struct snd_pcm_file * pcm_file;
3256 struct snd_pcm_substream *substream;
3257 unsigned long offset;
3259 pcm_file = file->private_data;
3260 substream = pcm_file->substream;
3261 if (PCM_RUNTIME_CHECK(substream))
3262 return -ENXIO;
3264 offset = area->vm_pgoff << PAGE_SHIFT;
3265 switch (offset) {
3266 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3267 if (pcm_file->no_compat_mmap)
3268 return -ENXIO;
3269 return snd_pcm_mmap_status(substream, file, area);
3270 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3271 if (pcm_file->no_compat_mmap)
3272 return -ENXIO;
3273 return snd_pcm_mmap_control(substream, file, area);
3274 default:
3275 return snd_pcm_mmap_data(substream, file, area);
3277 return 0;
3280 static int snd_pcm_fasync(int fd, struct file * file, int on)
3282 struct snd_pcm_file * pcm_file;
3283 struct snd_pcm_substream *substream;
3284 struct snd_pcm_runtime *runtime;
3286 pcm_file = file->private_data;
3287 substream = pcm_file->substream;
3288 if (PCM_RUNTIME_CHECK(substream))
3289 return -ENXIO;
3290 runtime = substream->runtime;
3291 return fasync_helper(fd, file, on, &runtime->fasync);
3295 * ioctl32 compat
3297 #ifdef CONFIG_COMPAT
3298 #include "pcm_compat.c"
3299 #else
3300 #define snd_pcm_ioctl_compat NULL
3301 #endif
3304 * To be removed helpers to keep binary compatibility
3307 #ifdef CONFIG_SND_SUPPORT_OLD_API
3308 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3309 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3311 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3312 struct snd_pcm_hw_params_old *oparams)
3314 unsigned int i;
3316 memset(params, 0, sizeof(*params));
3317 params->flags = oparams->flags;
3318 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3319 params->masks[i].bits[0] = oparams->masks[i];
3320 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3321 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3322 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3323 params->info = oparams->info;
3324 params->msbits = oparams->msbits;
3325 params->rate_num = oparams->rate_num;
3326 params->rate_den = oparams->rate_den;
3327 params->fifo_size = oparams->fifo_size;
3330 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3331 struct snd_pcm_hw_params *params)
3333 unsigned int i;
3335 memset(oparams, 0, sizeof(*oparams));
3336 oparams->flags = params->flags;
3337 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3338 oparams->masks[i] = params->masks[i].bits[0];
3339 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3340 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3341 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3342 oparams->info = params->info;
3343 oparams->msbits = params->msbits;
3344 oparams->rate_num = params->rate_num;
3345 oparams->rate_den = params->rate_den;
3346 oparams->fifo_size = params->fifo_size;
3349 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3350 struct snd_pcm_hw_params_old __user * _oparams)
3352 struct snd_pcm_hw_params *params;
3353 struct snd_pcm_hw_params_old *oparams = NULL;
3354 int err;
3356 params = kmalloc(sizeof(*params), GFP_KERNEL);
3357 if (!params)
3358 return -ENOMEM;
3360 oparams = memdup_user(_oparams, sizeof(*oparams));
3361 if (IS_ERR(oparams)) {
3362 err = PTR_ERR(oparams);
3363 goto out;
3365 snd_pcm_hw_convert_from_old_params(params, oparams);
3366 err = snd_pcm_hw_refine(substream, params);
3367 snd_pcm_hw_convert_to_old_params(oparams, params);
3368 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3369 if (!err)
3370 err = -EFAULT;
3373 kfree(oparams);
3374 out:
3375 kfree(params);
3376 return err;
3379 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3380 struct snd_pcm_hw_params_old __user * _oparams)
3382 struct snd_pcm_hw_params *params;
3383 struct snd_pcm_hw_params_old *oparams = NULL;
3384 int err;
3386 params = kmalloc(sizeof(*params), GFP_KERNEL);
3387 if (!params)
3388 return -ENOMEM;
3390 oparams = memdup_user(_oparams, sizeof(*oparams));
3391 if (IS_ERR(oparams)) {
3392 err = PTR_ERR(oparams);
3393 goto out;
3395 snd_pcm_hw_convert_from_old_params(params, oparams);
3396 err = snd_pcm_hw_params(substream, params);
3397 snd_pcm_hw_convert_to_old_params(oparams, params);
3398 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3399 if (!err)
3400 err = -EFAULT;
3403 kfree(oparams);
3404 out:
3405 kfree(params);
3406 return err;
3408 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3410 #ifndef CONFIG_MMU
3411 static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3412 unsigned long addr,
3413 unsigned long len,
3414 unsigned long pgoff,
3415 unsigned long flags)
3417 struct snd_pcm_file *pcm_file = file->private_data;
3418 struct snd_pcm_substream *substream = pcm_file->substream;
3419 struct snd_pcm_runtime *runtime = substream->runtime;
3420 unsigned long offset = pgoff << PAGE_SHIFT;
3422 switch (offset) {
3423 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3424 return (unsigned long)runtime->status;
3425 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3426 return (unsigned long)runtime->control;
3427 default:
3428 return (unsigned long)runtime->dma_area + offset;
3431 #else
3432 # define snd_pcm_get_unmapped_area NULL
3433 #endif
3436 * Register section
3439 const struct file_operations snd_pcm_f_ops[2] = {
3441 .owner = THIS_MODULE,
3442 .write = snd_pcm_write,
3443 .aio_write = snd_pcm_aio_write,
3444 .open = snd_pcm_playback_open,
3445 .release = snd_pcm_release,
3446 .llseek = no_llseek,
3447 .poll = snd_pcm_playback_poll,
3448 .unlocked_ioctl = snd_pcm_playback_ioctl,
3449 .compat_ioctl = snd_pcm_ioctl_compat,
3450 .mmap = snd_pcm_mmap,
3451 .fasync = snd_pcm_fasync,
3452 .get_unmapped_area = snd_pcm_get_unmapped_area,
3455 .owner = THIS_MODULE,
3456 .read = snd_pcm_read,
3457 .aio_read = snd_pcm_aio_read,
3458 .open = snd_pcm_capture_open,
3459 .release = snd_pcm_release,
3460 .llseek = no_llseek,
3461 .poll = snd_pcm_capture_poll,
3462 .unlocked_ioctl = snd_pcm_capture_ioctl,
3463 .compat_ioctl = snd_pcm_ioctl_compat,
3464 .mmap = snd_pcm_mmap,
3465 .fasync = snd_pcm_fasync,
3466 .get_unmapped_area = snd_pcm_get_unmapped_area,