fb: fix colliding defines for fb flags.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / core / pcm_native.c
blobd124e95a7d2eb05d5d271ffd9e58138d215ca938
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 /* AB: FIXME!!! This is definitely nonsense */
116 if (runtime) {
117 info->sync = runtime->sync;
118 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
120 return 0;
123 int snd_pcm_info_user(struct snd_pcm_substream *substream,
124 struct snd_pcm_info __user * _info)
126 struct snd_pcm_info *info;
127 int err;
129 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 if (! info)
131 return -ENOMEM;
132 err = snd_pcm_info(substream, info);
133 if (err >= 0) {
134 if (copy_to_user(_info, info, sizeof(*info)))
135 err = -EFAULT;
137 kfree(info);
138 return err;
141 #undef RULES_DEBUG
143 #ifdef RULES_DEBUG
144 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
145 char *snd_pcm_hw_param_names[] = {
146 HW_PARAM(ACCESS),
147 HW_PARAM(FORMAT),
148 HW_PARAM(SUBFORMAT),
149 HW_PARAM(SAMPLE_BITS),
150 HW_PARAM(FRAME_BITS),
151 HW_PARAM(CHANNELS),
152 HW_PARAM(RATE),
153 HW_PARAM(PERIOD_TIME),
154 HW_PARAM(PERIOD_SIZE),
155 HW_PARAM(PERIOD_BYTES),
156 HW_PARAM(PERIODS),
157 HW_PARAM(BUFFER_TIME),
158 HW_PARAM(BUFFER_SIZE),
159 HW_PARAM(BUFFER_BYTES),
160 HW_PARAM(TICK_TIME),
162 #endif
164 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
165 struct snd_pcm_hw_params *params)
167 unsigned int k;
168 struct snd_pcm_hardware *hw;
169 struct snd_interval *i = NULL;
170 struct snd_mask *m = NULL;
171 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
172 unsigned int rstamps[constrs->rules_num];
173 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
174 unsigned int stamp = 2;
175 int changed, again;
177 params->info = 0;
178 params->fifo_size = 0;
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
180 params->msbits = 0;
181 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
182 params->rate_num = 0;
183 params->rate_den = 0;
186 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
187 m = hw_param_mask(params, k);
188 if (snd_mask_empty(m))
189 return -EINVAL;
190 if (!(params->rmask & (1 << k)))
191 continue;
192 #ifdef RULES_DEBUG
193 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
194 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195 #endif
196 changed = snd_mask_refine(m, constrs_mask(constrs, k));
197 #ifdef RULES_DEBUG
198 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199 #endif
200 if (changed)
201 params->cmask |= 1 << k;
202 if (changed < 0)
203 return changed;
206 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
207 i = hw_param_interval(params, k);
208 if (snd_interval_empty(i))
209 return -EINVAL;
210 if (!(params->rmask & (1 << k)))
211 continue;
212 #ifdef RULES_DEBUG
213 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
214 if (i->empty)
215 printk("empty");
216 else
217 printk("%c%u %u%c",
218 i->openmin ? '(' : '[', i->min,
219 i->max, i->openmax ? ')' : ']');
220 printk(" -> ");
221 #endif
222 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223 #ifdef RULES_DEBUG
224 if (i->empty)
225 printk("empty\n");
226 else
227 printk("%c%u %u%c\n",
228 i->openmin ? '(' : '[', i->min,
229 i->max, i->openmax ? ')' : ']');
230 #endif
231 if (changed)
232 params->cmask |= 1 << k;
233 if (changed < 0)
234 return changed;
237 for (k = 0; k < constrs->rules_num; k++)
238 rstamps[k] = 0;
239 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
240 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 do {
242 again = 0;
243 for (k = 0; k < constrs->rules_num; k++) {
244 struct snd_pcm_hw_rule *r = &constrs->rules[k];
245 unsigned int d;
246 int doit = 0;
247 if (r->cond && !(r->cond & params->flags))
248 continue;
249 for (d = 0; r->deps[d] >= 0; d++) {
250 if (vstamps[r->deps[d]] > rstamps[k]) {
251 doit = 1;
252 break;
255 if (!doit)
256 continue;
257 #ifdef RULES_DEBUG
258 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
259 if (r->var >= 0) {
260 printk("%s = ", snd_pcm_hw_param_names[r->var]);
261 if (hw_is_mask(r->var)) {
262 m = hw_param_mask(params, r->var);
263 printk("%x", *m->bits);
264 } else {
265 i = hw_param_interval(params, r->var);
266 if (i->empty)
267 printk("empty");
268 else
269 printk("%c%u %u%c",
270 i->openmin ? '(' : '[', i->min,
271 i->max, i->openmax ? ')' : ']');
274 #endif
275 changed = r->func(params, r);
276 #ifdef RULES_DEBUG
277 if (r->var >= 0) {
278 printk(" -> ");
279 if (hw_is_mask(r->var))
280 printk("%x", *m->bits);
281 else {
282 if (i->empty)
283 printk("empty");
284 else
285 printk("%c%u %u%c",
286 i->openmin ? '(' : '[', i->min,
287 i->max, i->openmax ? ')' : ']');
290 printk("\n");
291 #endif
292 rstamps[k] = stamp;
293 if (changed && r->var >= 0) {
294 params->cmask |= (1 << r->var);
295 vstamps[r->var] = stamp;
296 again = 1;
298 if (changed < 0)
299 return changed;
300 stamp++;
302 } while (again);
303 if (!params->msbits) {
304 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
305 if (snd_interval_single(i))
306 params->msbits = snd_interval_value(i);
309 if (!params->rate_den) {
310 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
311 if (snd_interval_single(i)) {
312 params->rate_num = snd_interval_value(i);
313 params->rate_den = 1;
317 hw = &substream->runtime->hw;
318 if (!params->info)
319 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
320 if (!params->fifo_size) {
321 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
322 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
323 if (snd_mask_min(m) == snd_mask_max(m) &&
324 snd_interval_min(i) == snd_interval_max(i)) {
325 changed = substream->ops->ioctl(substream,
326 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
327 if (changed < 0)
328 return changed;
331 params->rmask = 0;
332 return 0;
335 EXPORT_SYMBOL(snd_pcm_hw_refine);
337 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
338 struct snd_pcm_hw_params __user * _params)
340 struct snd_pcm_hw_params *params;
341 int err;
343 params = memdup_user(_params, sizeof(*params));
344 if (IS_ERR(params))
345 return PTR_ERR(params);
347 err = snd_pcm_hw_refine(substream, params);
348 if (copy_to_user(_params, params, sizeof(*params))) {
349 if (!err)
350 err = -EFAULT;
353 kfree(params);
354 return err;
357 static int period_to_usecs(struct snd_pcm_runtime *runtime)
359 int usecs;
361 if (! runtime->rate)
362 return -1; /* invalid */
364 /* take 75% of period time as the deadline */
365 usecs = (750000 / runtime->rate) * runtime->period_size;
366 usecs += ((750000 % runtime->rate) * runtime->period_size) /
367 runtime->rate;
369 return usecs;
372 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params)
375 struct snd_pcm_runtime *runtime;
376 int err, usecs;
377 unsigned int bits;
378 snd_pcm_uframes_t frames;
380 if (PCM_RUNTIME_CHECK(substream))
381 return -ENXIO;
382 runtime = substream->runtime;
383 snd_pcm_stream_lock_irq(substream);
384 switch (runtime->status->state) {
385 case SNDRV_PCM_STATE_OPEN:
386 case SNDRV_PCM_STATE_SETUP:
387 case SNDRV_PCM_STATE_PREPARED:
388 break;
389 default:
390 snd_pcm_stream_unlock_irq(substream);
391 return -EBADFD;
393 snd_pcm_stream_unlock_irq(substream);
394 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
395 if (!substream->oss.oss)
396 #endif
397 if (atomic_read(&substream->mmap_count))
398 return -EBADFD;
400 params->rmask = ~0U;
401 err = snd_pcm_hw_refine(substream, params);
402 if (err < 0)
403 goto _error;
405 err = snd_pcm_hw_params_choose(substream, params);
406 if (err < 0)
407 goto _error;
409 if (substream->ops->hw_params != NULL) {
410 err = substream->ops->hw_params(substream, params);
411 if (err < 0)
412 goto _error;
415 runtime->access = params_access(params);
416 runtime->format = params_format(params);
417 runtime->subformat = params_subformat(params);
418 runtime->channels = params_channels(params);
419 runtime->rate = params_rate(params);
420 runtime->period_size = params_period_size(params);
421 runtime->periods = params_periods(params);
422 runtime->buffer_size = params_buffer_size(params);
423 runtime->info = params->info;
424 runtime->rate_num = params->rate_num;
425 runtime->rate_den = params->rate_den;
427 bits = snd_pcm_format_physical_width(runtime->format);
428 runtime->sample_bits = bits;
429 bits *= runtime->channels;
430 runtime->frame_bits = bits;
431 frames = 1;
432 while (bits % 8 != 0) {
433 bits *= 2;
434 frames *= 2;
436 runtime->byte_align = bits / 8;
437 runtime->min_align = frames;
439 /* Default sw params */
440 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
441 runtime->period_step = 1;
442 runtime->control->avail_min = runtime->period_size;
443 runtime->start_threshold = 1;
444 runtime->stop_threshold = runtime->buffer_size;
445 runtime->silence_threshold = 0;
446 runtime->silence_size = 0;
447 runtime->boundary = runtime->buffer_size;
448 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
449 runtime->boundary *= 2;
451 snd_pcm_timer_resolution_change(substream);
452 runtime->status->state = SNDRV_PCM_STATE_SETUP;
454 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
455 substream->latency_id);
456 if ((usecs = period_to_usecs(runtime)) >= 0)
457 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
458 substream->latency_id, usecs);
459 return 0;
460 _error:
461 /* hardware might be unuseable from this time,
462 so we force application to retry to set
463 the correct hardware parameter settings */
464 runtime->status->state = SNDRV_PCM_STATE_OPEN;
465 if (substream->ops->hw_free != NULL)
466 substream->ops->hw_free(substream);
467 return err;
470 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params __user * _params)
473 struct snd_pcm_hw_params *params;
474 int err;
476 params = memdup_user(_params, sizeof(*params));
477 if (IS_ERR(params))
478 return PTR_ERR(params);
480 err = snd_pcm_hw_params(substream, params);
481 if (copy_to_user(_params, params, sizeof(*params))) {
482 if (!err)
483 err = -EFAULT;
486 kfree(params);
487 return err;
490 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
492 struct snd_pcm_runtime *runtime;
493 int result = 0;
495 if (PCM_RUNTIME_CHECK(substream))
496 return -ENXIO;
497 runtime = substream->runtime;
498 snd_pcm_stream_lock_irq(substream);
499 switch (runtime->status->state) {
500 case SNDRV_PCM_STATE_SETUP:
501 case SNDRV_PCM_STATE_PREPARED:
502 break;
503 default:
504 snd_pcm_stream_unlock_irq(substream);
505 return -EBADFD;
507 snd_pcm_stream_unlock_irq(substream);
508 if (atomic_read(&substream->mmap_count))
509 return -EBADFD;
510 if (substream->ops->hw_free)
511 result = substream->ops->hw_free(substream);
512 runtime->status->state = SNDRV_PCM_STATE_OPEN;
513 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
514 substream->latency_id);
515 return result;
518 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
519 struct snd_pcm_sw_params *params)
521 struct snd_pcm_runtime *runtime;
522 int err;
524 if (PCM_RUNTIME_CHECK(substream))
525 return -ENXIO;
526 runtime = substream->runtime;
527 snd_pcm_stream_lock_irq(substream);
528 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
529 snd_pcm_stream_unlock_irq(substream);
530 return -EBADFD;
532 snd_pcm_stream_unlock_irq(substream);
534 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
535 return -EINVAL;
536 if (params->avail_min == 0)
537 return -EINVAL;
538 if (params->silence_size >= runtime->boundary) {
539 if (params->silence_threshold != 0)
540 return -EINVAL;
541 } else {
542 if (params->silence_size > params->silence_threshold)
543 return -EINVAL;
544 if (params->silence_threshold > runtime->buffer_size)
545 return -EINVAL;
547 err = 0;
548 snd_pcm_stream_lock_irq(substream);
549 runtime->tstamp_mode = params->tstamp_mode;
550 runtime->period_step = params->period_step;
551 runtime->control->avail_min = params->avail_min;
552 runtime->start_threshold = params->start_threshold;
553 runtime->stop_threshold = params->stop_threshold;
554 runtime->silence_threshold = params->silence_threshold;
555 runtime->silence_size = params->silence_size;
556 params->boundary = runtime->boundary;
557 if (snd_pcm_running(substream)) {
558 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
559 runtime->silence_size > 0)
560 snd_pcm_playback_silence(substream, ULONG_MAX);
561 err = snd_pcm_update_state(substream, runtime);
563 snd_pcm_stream_unlock_irq(substream);
564 return err;
567 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
568 struct snd_pcm_sw_params __user * _params)
570 struct snd_pcm_sw_params params;
571 int err;
572 if (copy_from_user(&params, _params, sizeof(params)))
573 return -EFAULT;
574 err = snd_pcm_sw_params(substream, &params);
575 if (copy_to_user(_params, &params, sizeof(params)))
576 return -EFAULT;
577 return err;
580 int snd_pcm_status(struct snd_pcm_substream *substream,
581 struct snd_pcm_status *status)
583 struct snd_pcm_runtime *runtime = substream->runtime;
585 snd_pcm_stream_lock_irq(substream);
586 status->state = runtime->status->state;
587 status->suspended_state = runtime->status->suspended_state;
588 if (status->state == SNDRV_PCM_STATE_OPEN)
589 goto _end;
590 status->trigger_tstamp = runtime->trigger_tstamp;
591 if (snd_pcm_running(substream)) {
592 snd_pcm_update_hw_ptr(substream);
593 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
594 status->tstamp = runtime->status->tstamp;
595 goto _tstamp_end;
598 snd_pcm_gettime(runtime, &status->tstamp);
599 _tstamp_end:
600 status->appl_ptr = runtime->control->appl_ptr;
601 status->hw_ptr = runtime->status->hw_ptr;
602 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
603 status->avail = snd_pcm_playback_avail(runtime);
604 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
605 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
606 status->delay = runtime->buffer_size - status->avail;
607 status->delay += runtime->delay;
608 } else
609 status->delay = 0;
610 } else {
611 status->avail = snd_pcm_capture_avail(runtime);
612 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
613 status->delay = status->avail + runtime->delay;
614 else
615 status->delay = 0;
617 status->avail_max = runtime->avail_max;
618 status->overrange = runtime->overrange;
619 runtime->avail_max = 0;
620 runtime->overrange = 0;
621 _end:
622 snd_pcm_stream_unlock_irq(substream);
623 return 0;
626 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
627 struct snd_pcm_status __user * _status)
629 struct snd_pcm_status status;
630 int res;
632 memset(&status, 0, sizeof(status));
633 res = snd_pcm_status(substream, &status);
634 if (res < 0)
635 return res;
636 if (copy_to_user(_status, &status, sizeof(status)))
637 return -EFAULT;
638 return 0;
641 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
642 struct snd_pcm_channel_info * info)
644 struct snd_pcm_runtime *runtime;
645 unsigned int channel;
647 channel = info->channel;
648 runtime = substream->runtime;
649 snd_pcm_stream_lock_irq(substream);
650 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
651 snd_pcm_stream_unlock_irq(substream);
652 return -EBADFD;
654 snd_pcm_stream_unlock_irq(substream);
655 if (channel >= runtime->channels)
656 return -EINVAL;
657 memset(info, 0, sizeof(*info));
658 info->channel = channel;
659 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
662 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
663 struct snd_pcm_channel_info __user * _info)
665 struct snd_pcm_channel_info info;
666 int res;
668 if (copy_from_user(&info, _info, sizeof(info)))
669 return -EFAULT;
670 res = snd_pcm_channel_info(substream, &info);
671 if (res < 0)
672 return res;
673 if (copy_to_user(_info, &info, sizeof(info)))
674 return -EFAULT;
675 return 0;
678 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 if (runtime->trigger_master == NULL)
682 return;
683 if (runtime->trigger_master == substream) {
684 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
685 } else {
686 snd_pcm_trigger_tstamp(runtime->trigger_master);
687 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
689 runtime->trigger_master = NULL;
692 struct action_ops {
693 int (*pre_action)(struct snd_pcm_substream *substream, int state);
694 int (*do_action)(struct snd_pcm_substream *substream, int state);
695 void (*undo_action)(struct snd_pcm_substream *substream, int state);
696 void (*post_action)(struct snd_pcm_substream *substream, int state);
700 * this functions is core for handling of linked stream
701 * Note: the stream state might be changed also on failure
702 * Note2: call with calling stream lock + link lock
704 static int snd_pcm_action_group(struct action_ops *ops,
705 struct snd_pcm_substream *substream,
706 int state, int do_lock)
708 struct snd_pcm_substream *s = NULL;
709 struct snd_pcm_substream *s1;
710 int res = 0;
712 snd_pcm_group_for_each_entry(s, substream) {
713 if (do_lock && s != substream)
714 spin_lock_nested(&s->self_group.lock,
715 SINGLE_DEPTH_NESTING);
716 res = ops->pre_action(s, state);
717 if (res < 0)
718 goto _unlock;
720 snd_pcm_group_for_each_entry(s, substream) {
721 res = ops->do_action(s, state);
722 if (res < 0) {
723 if (ops->undo_action) {
724 snd_pcm_group_for_each_entry(s1, substream) {
725 if (s1 == s) /* failed stream */
726 break;
727 ops->undo_action(s1, state);
730 s = NULL; /* unlock all */
731 goto _unlock;
734 snd_pcm_group_for_each_entry(s, substream) {
735 ops->post_action(s, state);
737 _unlock:
738 if (do_lock) {
739 /* unlock streams */
740 snd_pcm_group_for_each_entry(s1, substream) {
741 if (s1 != substream)
742 spin_unlock(&s1->self_group.lock);
743 if (s1 == s) /* end */
744 break;
747 return res;
751 * Note: call with stream lock
753 static int snd_pcm_action_single(struct action_ops *ops,
754 struct snd_pcm_substream *substream,
755 int state)
757 int res;
759 res = ops->pre_action(substream, state);
760 if (res < 0)
761 return res;
762 res = ops->do_action(substream, state);
763 if (res == 0)
764 ops->post_action(substream, state);
765 else if (ops->undo_action)
766 ops->undo_action(substream, state);
767 return res;
771 * Note: call with stream lock
773 static int snd_pcm_action(struct action_ops *ops,
774 struct snd_pcm_substream *substream,
775 int state)
777 int res;
779 if (snd_pcm_stream_linked(substream)) {
780 if (!spin_trylock(&substream->group->lock)) {
781 spin_unlock(&substream->self_group.lock);
782 spin_lock(&substream->group->lock);
783 spin_lock(&substream->self_group.lock);
785 res = snd_pcm_action_group(ops, substream, state, 1);
786 spin_unlock(&substream->group->lock);
787 } else {
788 res = snd_pcm_action_single(ops, substream, state);
790 return res;
794 * Note: don't use any locks before
796 static int snd_pcm_action_lock_irq(struct action_ops *ops,
797 struct snd_pcm_substream *substream,
798 int state)
800 int res;
802 read_lock_irq(&snd_pcm_link_rwlock);
803 if (snd_pcm_stream_linked(substream)) {
804 spin_lock(&substream->group->lock);
805 spin_lock(&substream->self_group.lock);
806 res = snd_pcm_action_group(ops, substream, state, 1);
807 spin_unlock(&substream->self_group.lock);
808 spin_unlock(&substream->group->lock);
809 } else {
810 spin_lock(&substream->self_group.lock);
811 res = snd_pcm_action_single(ops, substream, state);
812 spin_unlock(&substream->self_group.lock);
814 read_unlock_irq(&snd_pcm_link_rwlock);
815 return res;
820 static int snd_pcm_action_nonatomic(struct action_ops *ops,
821 struct snd_pcm_substream *substream,
822 int state)
824 int res;
826 down_read(&snd_pcm_link_rwsem);
827 if (snd_pcm_stream_linked(substream))
828 res = snd_pcm_action_group(ops, substream, state, 0);
829 else
830 res = snd_pcm_action_single(ops, substream, state);
831 up_read(&snd_pcm_link_rwsem);
832 return res;
836 * start callbacks
838 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
840 struct snd_pcm_runtime *runtime = substream->runtime;
841 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
842 return -EBADFD;
843 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
844 !snd_pcm_playback_data(substream))
845 return -EPIPE;
846 runtime->trigger_master = substream;
847 return 0;
850 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
852 if (substream->runtime->trigger_master != substream)
853 return 0;
854 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
857 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
859 if (substream->runtime->trigger_master == substream)
860 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
863 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
865 struct snd_pcm_runtime *runtime = substream->runtime;
866 snd_pcm_trigger_tstamp(substream);
867 runtime->hw_ptr_jiffies = jiffies;
868 runtime->status->state = state;
869 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
870 runtime->silence_size > 0)
871 snd_pcm_playback_silence(substream, ULONG_MAX);
872 if (substream->timer)
873 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
874 &runtime->trigger_tstamp);
877 static struct action_ops snd_pcm_action_start = {
878 .pre_action = snd_pcm_pre_start,
879 .do_action = snd_pcm_do_start,
880 .undo_action = snd_pcm_undo_start,
881 .post_action = snd_pcm_post_start
885 * snd_pcm_start - start all linked streams
886 * @substream: the PCM substream instance
888 int snd_pcm_start(struct snd_pcm_substream *substream)
890 return snd_pcm_action(&snd_pcm_action_start, substream,
891 SNDRV_PCM_STATE_RUNNING);
895 * stop callbacks
897 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
899 struct snd_pcm_runtime *runtime = substream->runtime;
900 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
901 return -EBADFD;
902 runtime->trigger_master = substream;
903 return 0;
906 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
908 if (substream->runtime->trigger_master == substream &&
909 snd_pcm_running(substream))
910 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
911 return 0; /* unconditonally stop all substreams */
914 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
916 struct snd_pcm_runtime *runtime = substream->runtime;
917 if (runtime->status->state != state) {
918 snd_pcm_trigger_tstamp(substream);
919 if (substream->timer)
920 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
921 &runtime->trigger_tstamp);
922 runtime->status->state = state;
924 wake_up(&runtime->sleep);
925 wake_up(&runtime->tsleep);
928 static struct action_ops snd_pcm_action_stop = {
929 .pre_action = snd_pcm_pre_stop,
930 .do_action = snd_pcm_do_stop,
931 .post_action = snd_pcm_post_stop
935 * snd_pcm_stop - try to stop all running streams in the substream group
936 * @substream: the PCM substream instance
937 * @state: PCM state after stopping the stream
939 * The state of each stream is then changed to the given state unconditionally.
941 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
943 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
946 EXPORT_SYMBOL(snd_pcm_stop);
949 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
950 * @substream: the PCM substream
952 * After stopping, the state is changed to SETUP.
953 * Unlike snd_pcm_stop(), this affects only the given stream.
955 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
957 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
958 SNDRV_PCM_STATE_SETUP);
962 * pause callbacks
964 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
966 struct snd_pcm_runtime *runtime = substream->runtime;
967 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
968 return -ENOSYS;
969 if (push) {
970 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
971 return -EBADFD;
972 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
973 return -EBADFD;
974 runtime->trigger_master = substream;
975 return 0;
978 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
980 if (substream->runtime->trigger_master != substream)
981 return 0;
982 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
983 * a delta betwen the current jiffies, this gives a large enough
984 * delta, effectively to skip the check once.
986 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
987 return substream->ops->trigger(substream,
988 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
989 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
992 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
994 if (substream->runtime->trigger_master == substream)
995 substream->ops->trigger(substream,
996 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
997 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1000 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1002 struct snd_pcm_runtime *runtime = substream->runtime;
1003 snd_pcm_trigger_tstamp(substream);
1004 if (push) {
1005 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1006 if (substream->timer)
1007 snd_timer_notify(substream->timer,
1008 SNDRV_TIMER_EVENT_MPAUSE,
1009 &runtime->trigger_tstamp);
1010 wake_up(&runtime->sleep);
1011 wake_up(&runtime->tsleep);
1012 } else {
1013 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1014 if (substream->timer)
1015 snd_timer_notify(substream->timer,
1016 SNDRV_TIMER_EVENT_MCONTINUE,
1017 &runtime->trigger_tstamp);
1021 static struct action_ops snd_pcm_action_pause = {
1022 .pre_action = snd_pcm_pre_pause,
1023 .do_action = snd_pcm_do_pause,
1024 .undo_action = snd_pcm_undo_pause,
1025 .post_action = snd_pcm_post_pause
1029 * Push/release the pause for all linked streams.
1031 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1033 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1036 #ifdef CONFIG_PM
1037 /* suspend */
1039 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1041 struct snd_pcm_runtime *runtime = substream->runtime;
1042 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1043 return -EBUSY;
1044 runtime->trigger_master = substream;
1045 return 0;
1048 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1050 struct snd_pcm_runtime *runtime = substream->runtime;
1051 if (runtime->trigger_master != substream)
1052 return 0;
1053 if (! snd_pcm_running(substream))
1054 return 0;
1055 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1056 return 0; /* suspend unconditionally */
1059 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1061 struct snd_pcm_runtime *runtime = substream->runtime;
1062 snd_pcm_trigger_tstamp(substream);
1063 if (substream->timer)
1064 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1065 &runtime->trigger_tstamp);
1066 runtime->status->suspended_state = runtime->status->state;
1067 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1068 wake_up(&runtime->sleep);
1069 wake_up(&runtime->tsleep);
1072 static struct action_ops snd_pcm_action_suspend = {
1073 .pre_action = snd_pcm_pre_suspend,
1074 .do_action = snd_pcm_do_suspend,
1075 .post_action = snd_pcm_post_suspend
1079 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1080 * @substream: the PCM substream
1082 * After this call, all streams are changed to SUSPENDED state.
1084 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1086 int err;
1087 unsigned long flags;
1089 if (! substream)
1090 return 0;
1092 snd_pcm_stream_lock_irqsave(substream, flags);
1093 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1094 snd_pcm_stream_unlock_irqrestore(substream, flags);
1095 return err;
1098 EXPORT_SYMBOL(snd_pcm_suspend);
1101 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1102 * @pcm: the PCM instance
1104 * After this call, all streams are changed to SUSPENDED state.
1106 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1108 struct snd_pcm_substream *substream;
1109 int stream, err = 0;
1111 if (! pcm)
1112 return 0;
1114 for (stream = 0; stream < 2; stream++) {
1115 for (substream = pcm->streams[stream].substream;
1116 substream; substream = substream->next) {
1117 /* FIXME: the open/close code should lock this as well */
1118 if (substream->runtime == NULL)
1119 continue;
1120 err = snd_pcm_suspend(substream);
1121 if (err < 0 && err != -EBUSY)
1122 return err;
1125 return 0;
1128 EXPORT_SYMBOL(snd_pcm_suspend_all);
1130 /* resume */
1132 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1134 struct snd_pcm_runtime *runtime = substream->runtime;
1135 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1136 return -ENOSYS;
1137 runtime->trigger_master = substream;
1138 return 0;
1141 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1143 struct snd_pcm_runtime *runtime = substream->runtime;
1144 if (runtime->trigger_master != substream)
1145 return 0;
1146 /* DMA not running previously? */
1147 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1148 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1149 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1150 return 0;
1151 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1154 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1156 if (substream->runtime->trigger_master == substream &&
1157 snd_pcm_running(substream))
1158 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1161 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1163 struct snd_pcm_runtime *runtime = substream->runtime;
1164 snd_pcm_trigger_tstamp(substream);
1165 if (substream->timer)
1166 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1167 &runtime->trigger_tstamp);
1168 runtime->status->state = runtime->status->suspended_state;
1171 static struct action_ops snd_pcm_action_resume = {
1172 .pre_action = snd_pcm_pre_resume,
1173 .do_action = snd_pcm_do_resume,
1174 .undo_action = snd_pcm_undo_resume,
1175 .post_action = snd_pcm_post_resume
1178 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1180 struct snd_card *card = substream->pcm->card;
1181 int res;
1183 snd_power_lock(card);
1184 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1185 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1186 snd_power_unlock(card);
1187 return res;
1190 #else
1192 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1194 return -ENOSYS;
1197 #endif /* CONFIG_PM */
1200 * xrun ioctl
1202 * Change the RUNNING stream(s) to XRUN state.
1204 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1206 struct snd_card *card = substream->pcm->card;
1207 struct snd_pcm_runtime *runtime = substream->runtime;
1208 int result;
1210 snd_power_lock(card);
1211 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1212 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1213 if (result < 0)
1214 goto _unlock;
1217 snd_pcm_stream_lock_irq(substream);
1218 switch (runtime->status->state) {
1219 case SNDRV_PCM_STATE_XRUN:
1220 result = 0; /* already there */
1221 break;
1222 case SNDRV_PCM_STATE_RUNNING:
1223 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1224 break;
1225 default:
1226 result = -EBADFD;
1228 snd_pcm_stream_unlock_irq(substream);
1229 _unlock:
1230 snd_power_unlock(card);
1231 return result;
1235 * reset ioctl
1237 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1239 struct snd_pcm_runtime *runtime = substream->runtime;
1240 switch (runtime->status->state) {
1241 case SNDRV_PCM_STATE_RUNNING:
1242 case SNDRV_PCM_STATE_PREPARED:
1243 case SNDRV_PCM_STATE_PAUSED:
1244 case SNDRV_PCM_STATE_SUSPENDED:
1245 return 0;
1246 default:
1247 return -EBADFD;
1251 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1253 struct snd_pcm_runtime *runtime = substream->runtime;
1254 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1255 if (err < 0)
1256 return err;
1257 runtime->hw_ptr_base = 0;
1258 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1259 runtime->status->hw_ptr % runtime->period_size;
1260 runtime->silence_start = runtime->status->hw_ptr;
1261 runtime->silence_filled = 0;
1262 return 0;
1265 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1267 struct snd_pcm_runtime *runtime = substream->runtime;
1268 runtime->control->appl_ptr = runtime->status->hw_ptr;
1269 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1270 runtime->silence_size > 0)
1271 snd_pcm_playback_silence(substream, ULONG_MAX);
1274 static struct action_ops snd_pcm_action_reset = {
1275 .pre_action = snd_pcm_pre_reset,
1276 .do_action = snd_pcm_do_reset,
1277 .post_action = snd_pcm_post_reset
1280 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1282 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1286 * prepare ioctl
1288 /* we use the second argument for updating f_flags */
1289 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1290 int f_flags)
1292 struct snd_pcm_runtime *runtime = substream->runtime;
1293 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1294 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1295 return -EBADFD;
1296 if (snd_pcm_running(substream))
1297 return -EBUSY;
1298 substream->f_flags = f_flags;
1299 return 0;
1302 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1304 int err;
1305 err = substream->ops->prepare(substream);
1306 if (err < 0)
1307 return err;
1308 return snd_pcm_do_reset(substream, 0);
1311 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1313 struct snd_pcm_runtime *runtime = substream->runtime;
1314 runtime->control->appl_ptr = runtime->status->hw_ptr;
1315 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1318 static struct action_ops snd_pcm_action_prepare = {
1319 .pre_action = snd_pcm_pre_prepare,
1320 .do_action = snd_pcm_do_prepare,
1321 .post_action = snd_pcm_post_prepare
1325 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1326 * @substream: the PCM substream instance
1327 * @file: file to refer f_flags
1329 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1330 struct file *file)
1332 int res;
1333 struct snd_card *card = substream->pcm->card;
1334 int f_flags;
1336 if (file)
1337 f_flags = file->f_flags;
1338 else
1339 f_flags = substream->f_flags;
1341 snd_power_lock(card);
1342 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1343 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1344 substream, f_flags);
1345 snd_power_unlock(card);
1346 return res;
1350 * drain ioctl
1353 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1355 substream->runtime->trigger_master = substream;
1356 return 0;
1359 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1361 struct snd_pcm_runtime *runtime = substream->runtime;
1362 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1363 switch (runtime->status->state) {
1364 case SNDRV_PCM_STATE_PREPARED:
1365 /* start playback stream if possible */
1366 if (! snd_pcm_playback_empty(substream)) {
1367 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1368 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1370 break;
1371 case SNDRV_PCM_STATE_RUNNING:
1372 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1373 break;
1374 default:
1375 break;
1377 } else {
1378 /* stop running stream */
1379 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1380 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1381 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1382 snd_pcm_do_stop(substream, new_state);
1383 snd_pcm_post_stop(substream, new_state);
1386 return 0;
1389 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1393 static struct action_ops snd_pcm_action_drain_init = {
1394 .pre_action = snd_pcm_pre_drain_init,
1395 .do_action = snd_pcm_do_drain_init,
1396 .post_action = snd_pcm_post_drain_init
1399 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1402 * Drain the stream(s).
1403 * When the substream is linked, sync until the draining of all playback streams
1404 * is finished.
1405 * After this call, all streams are supposed to be either SETUP or DRAINING
1406 * (capture only) state.
1408 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1409 struct file *file)
1411 struct snd_card *card;
1412 struct snd_pcm_runtime *runtime;
1413 struct snd_pcm_substream *s;
1414 wait_queue_t wait;
1415 int result = 0;
1416 int nonblock = 0;
1418 card = substream->pcm->card;
1419 runtime = substream->runtime;
1421 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1422 return -EBADFD;
1424 snd_power_lock(card);
1425 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1426 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1427 if (result < 0) {
1428 snd_power_unlock(card);
1429 return result;
1433 if (file) {
1434 if (file->f_flags & O_NONBLOCK)
1435 nonblock = 1;
1436 } else if (substream->f_flags & O_NONBLOCK)
1437 nonblock = 1;
1439 down_read(&snd_pcm_link_rwsem);
1440 snd_pcm_stream_lock_irq(substream);
1441 /* resume pause */
1442 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1443 snd_pcm_pause(substream, 0);
1445 /* pre-start/stop - all running streams are changed to DRAINING state */
1446 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1447 if (result < 0)
1448 goto unlock;
1449 /* in non-blocking, we don't wait in ioctl but let caller poll */
1450 if (nonblock) {
1451 result = -EAGAIN;
1452 goto unlock;
1455 for (;;) {
1456 long tout;
1457 struct snd_pcm_runtime *to_check;
1458 if (signal_pending(current)) {
1459 result = -ERESTARTSYS;
1460 break;
1462 /* find a substream to drain */
1463 to_check = NULL;
1464 snd_pcm_group_for_each_entry(s, substream) {
1465 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1466 continue;
1467 runtime = s->runtime;
1468 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1469 to_check = runtime;
1470 break;
1473 if (!to_check)
1474 break; /* all drained */
1475 init_waitqueue_entry(&wait, current);
1476 add_wait_queue(&to_check->sleep, &wait);
1477 set_current_state(TASK_INTERRUPTIBLE);
1478 snd_pcm_stream_unlock_irq(substream);
1479 up_read(&snd_pcm_link_rwsem);
1480 snd_power_unlock(card);
1481 tout = schedule_timeout(10 * HZ);
1482 snd_power_lock(card);
1483 down_read(&snd_pcm_link_rwsem);
1484 snd_pcm_stream_lock_irq(substream);
1485 remove_wait_queue(&to_check->sleep, &wait);
1486 if (tout == 0) {
1487 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1488 result = -ESTRPIPE;
1489 else {
1490 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1491 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1492 result = -EIO;
1494 break;
1498 unlock:
1499 snd_pcm_stream_unlock_irq(substream);
1500 up_read(&snd_pcm_link_rwsem);
1501 snd_power_unlock(card);
1503 return result;
1507 * drop ioctl
1509 * Immediately put all linked substreams into SETUP state.
1511 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1513 struct snd_pcm_runtime *runtime;
1514 struct snd_card *card;
1515 int result = 0;
1517 if (PCM_RUNTIME_CHECK(substream))
1518 return -ENXIO;
1519 runtime = substream->runtime;
1520 card = substream->pcm->card;
1522 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1523 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1524 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1525 return -EBADFD;
1527 snd_pcm_stream_lock_irq(substream);
1528 /* resume pause */
1529 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1530 snd_pcm_pause(substream, 0);
1532 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1533 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1534 snd_pcm_stream_unlock_irq(substream);
1536 return result;
1540 /* WARNING: Don't forget to fput back the file */
1541 static struct file *snd_pcm_file_fd(int fd)
1543 struct file *file;
1544 struct inode *inode;
1545 unsigned int minor;
1547 file = fget(fd);
1548 if (!file)
1549 return NULL;
1550 inode = file->f_path.dentry->d_inode;
1551 if (!S_ISCHR(inode->i_mode) ||
1552 imajor(inode) != snd_major) {
1553 fput(file);
1554 return NULL;
1556 minor = iminor(inode);
1557 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1558 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1559 fput(file);
1560 return NULL;
1562 return file;
1566 * PCM link handling
1568 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1570 int res = 0;
1571 struct file *file;
1572 struct snd_pcm_file *pcm_file;
1573 struct snd_pcm_substream *substream1;
1575 file = snd_pcm_file_fd(fd);
1576 if (!file)
1577 return -EBADFD;
1578 pcm_file = file->private_data;
1579 substream1 = pcm_file->substream;
1580 down_write(&snd_pcm_link_rwsem);
1581 write_lock_irq(&snd_pcm_link_rwlock);
1582 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1583 substream->runtime->status->state != substream1->runtime->status->state) {
1584 res = -EBADFD;
1585 goto _end;
1587 if (snd_pcm_stream_linked(substream1)) {
1588 res = -EALREADY;
1589 goto _end;
1591 if (!snd_pcm_stream_linked(substream)) {
1592 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1593 if (substream->group == NULL) {
1594 res = -ENOMEM;
1595 goto _end;
1597 spin_lock_init(&substream->group->lock);
1598 INIT_LIST_HEAD(&substream->group->substreams);
1599 list_add_tail(&substream->link_list, &substream->group->substreams);
1600 substream->group->count = 1;
1602 list_add_tail(&substream1->link_list, &substream->group->substreams);
1603 substream->group->count++;
1604 substream1->group = substream->group;
1605 _end:
1606 write_unlock_irq(&snd_pcm_link_rwlock);
1607 up_write(&snd_pcm_link_rwsem);
1608 fput(file);
1609 return res;
1612 static void relink_to_local(struct snd_pcm_substream *substream)
1614 substream->group = &substream->self_group;
1615 INIT_LIST_HEAD(&substream->self_group.substreams);
1616 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1619 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1621 struct snd_pcm_substream *s;
1622 int res = 0;
1624 down_write(&snd_pcm_link_rwsem);
1625 write_lock_irq(&snd_pcm_link_rwlock);
1626 if (!snd_pcm_stream_linked(substream)) {
1627 res = -EALREADY;
1628 goto _end;
1630 list_del(&substream->link_list);
1631 substream->group->count--;
1632 if (substream->group->count == 1) { /* detach the last stream, too */
1633 snd_pcm_group_for_each_entry(s, substream) {
1634 relink_to_local(s);
1635 break;
1637 kfree(substream->group);
1639 relink_to_local(substream);
1640 _end:
1641 write_unlock_irq(&snd_pcm_link_rwlock);
1642 up_write(&snd_pcm_link_rwsem);
1643 return res;
1647 * hw configurator
1649 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1650 struct snd_pcm_hw_rule *rule)
1652 struct snd_interval t;
1653 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1654 hw_param_interval_c(params, rule->deps[1]), &t);
1655 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1658 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1659 struct snd_pcm_hw_rule *rule)
1661 struct snd_interval t;
1662 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1663 hw_param_interval_c(params, rule->deps[1]), &t);
1664 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1667 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1668 struct snd_pcm_hw_rule *rule)
1670 struct snd_interval t;
1671 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1672 hw_param_interval_c(params, rule->deps[1]),
1673 (unsigned long) rule->private, &t);
1674 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1677 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1678 struct snd_pcm_hw_rule *rule)
1680 struct snd_interval t;
1681 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1682 (unsigned long) rule->private,
1683 hw_param_interval_c(params, rule->deps[1]), &t);
1684 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1687 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1688 struct snd_pcm_hw_rule *rule)
1690 unsigned int k;
1691 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1692 struct snd_mask m;
1693 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1694 snd_mask_any(&m);
1695 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1696 int bits;
1697 if (! snd_mask_test(mask, k))
1698 continue;
1699 bits = snd_pcm_format_physical_width(k);
1700 if (bits <= 0)
1701 continue; /* ignore invalid formats */
1702 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1703 snd_mask_reset(&m, k);
1705 return snd_mask_refine(mask, &m);
1708 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1709 struct snd_pcm_hw_rule *rule)
1711 struct snd_interval t;
1712 unsigned int k;
1713 t.min = UINT_MAX;
1714 t.max = 0;
1715 t.openmin = 0;
1716 t.openmax = 0;
1717 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1718 int bits;
1719 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1720 continue;
1721 bits = snd_pcm_format_physical_width(k);
1722 if (bits <= 0)
1723 continue; /* ignore invalid formats */
1724 if (t.min > (unsigned)bits)
1725 t.min = bits;
1726 if (t.max < (unsigned)bits)
1727 t.max = bits;
1729 t.integer = 1;
1730 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1733 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1734 #error "Change this table"
1735 #endif
1737 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1738 48000, 64000, 88200, 96000, 176400, 192000 };
1740 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1741 .count = ARRAY_SIZE(rates),
1742 .list = rates,
1745 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1746 struct snd_pcm_hw_rule *rule)
1748 struct snd_pcm_hardware *hw = rule->private;
1749 return snd_interval_list(hw_param_interval(params, rule->var),
1750 snd_pcm_known_rates.count,
1751 snd_pcm_known_rates.list, hw->rates);
1754 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1755 struct snd_pcm_hw_rule *rule)
1757 struct snd_interval t;
1758 struct snd_pcm_substream *substream = rule->private;
1759 t.min = 0;
1760 t.max = substream->buffer_bytes_max;
1761 t.openmin = 0;
1762 t.openmax = 0;
1763 t.integer = 1;
1764 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1767 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1769 struct snd_pcm_runtime *runtime = substream->runtime;
1770 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1771 int k, err;
1773 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1774 snd_mask_any(constrs_mask(constrs, k));
1777 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1778 snd_interval_any(constrs_interval(constrs, k));
1781 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1782 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1783 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1785 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1787 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1788 snd_pcm_hw_rule_format, NULL,
1789 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1790 if (err < 0)
1791 return err;
1792 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1793 snd_pcm_hw_rule_sample_bits, NULL,
1794 SNDRV_PCM_HW_PARAM_FORMAT,
1795 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1796 if (err < 0)
1797 return err;
1798 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1799 snd_pcm_hw_rule_div, NULL,
1800 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1801 if (err < 0)
1802 return err;
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1804 snd_pcm_hw_rule_mul, NULL,
1805 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1806 if (err < 0)
1807 return err;
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1811 if (err < 0)
1812 return err;
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1814 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1815 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1816 if (err < 0)
1817 return err;
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1819 snd_pcm_hw_rule_div, NULL,
1820 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1824 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1825 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1829 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1830 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1834 snd_pcm_hw_rule_div, NULL,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1839 snd_pcm_hw_rule_div, NULL,
1840 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1849 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1850 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1854 snd_pcm_hw_rule_mul, NULL,
1855 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1860 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1864 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1865 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1869 snd_pcm_hw_rule_muldivk, (void*) 8,
1870 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1874 snd_pcm_hw_rule_muldivk, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1881 if (err < 0)
1882 return err;
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1884 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 if (err < 0)
1887 return err;
1888 return 0;
1891 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1893 struct snd_pcm_runtime *runtime = substream->runtime;
1894 struct snd_pcm_hardware *hw = &runtime->hw;
1895 int err;
1896 unsigned int mask = 0;
1898 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1899 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1900 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1901 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1902 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1903 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1904 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1905 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1906 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1907 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1908 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1910 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1911 if (err < 0)
1912 return err;
1914 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1915 if (err < 0)
1916 return err;
1918 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1919 if (err < 0)
1920 return err;
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1923 hw->channels_min, hw->channels_max);
1924 if (err < 0)
1925 return err;
1927 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1928 hw->rate_min, hw->rate_max);
1929 if (err < 0)
1930 return err;
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1933 hw->period_bytes_min, hw->period_bytes_max);
1934 if (err < 0)
1935 return err;
1937 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1938 hw->periods_min, hw->periods_max);
1939 if (err < 0)
1940 return err;
1942 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1943 hw->period_bytes_min, hw->buffer_bytes_max);
1944 if (err < 0)
1945 return err;
1947 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1948 snd_pcm_hw_rule_buffer_bytes_max, substream,
1949 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1950 if (err < 0)
1951 return err;
1953 /* FIXME: remove */
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 /* FIXME: this belong to lowlevel */
1969 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1971 return 0;
1974 static void pcm_release_private(struct snd_pcm_substream *substream)
1976 snd_pcm_unlink(substream);
1979 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1981 substream->ref_count--;
1982 if (substream->ref_count > 0)
1983 return;
1985 snd_pcm_drop(substream);
1986 if (substream->hw_opened) {
1987 if (substream->ops->hw_free != NULL)
1988 substream->ops->hw_free(substream);
1989 substream->ops->close(substream);
1990 substream->hw_opened = 0;
1992 if (substream->pcm_release) {
1993 substream->pcm_release(substream);
1994 substream->pcm_release = NULL;
1996 snd_pcm_detach_substream(substream);
1999 EXPORT_SYMBOL(snd_pcm_release_substream);
2001 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2002 struct file *file,
2003 struct snd_pcm_substream **rsubstream)
2005 struct snd_pcm_substream *substream;
2006 int err;
2008 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2009 if (err < 0)
2010 return err;
2011 if (substream->ref_count > 1) {
2012 *rsubstream = substream;
2013 return 0;
2016 err = snd_pcm_hw_constraints_init(substream);
2017 if (err < 0) {
2018 snd_printd("snd_pcm_hw_constraints_init failed\n");
2019 goto error;
2022 if ((err = substream->ops->open(substream)) < 0)
2023 goto error;
2025 substream->hw_opened = 1;
2027 err = snd_pcm_hw_constraints_complete(substream);
2028 if (err < 0) {
2029 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2030 goto error;
2033 *rsubstream = substream;
2034 return 0;
2036 error:
2037 snd_pcm_release_substream(substream);
2038 return err;
2041 EXPORT_SYMBOL(snd_pcm_open_substream);
2043 static int snd_pcm_open_file(struct file *file,
2044 struct snd_pcm *pcm,
2045 int stream,
2046 struct snd_pcm_file **rpcm_file)
2048 struct snd_pcm_file *pcm_file;
2049 struct snd_pcm_substream *substream;
2050 struct snd_pcm_str *str;
2051 int err;
2053 if (rpcm_file)
2054 *rpcm_file = NULL;
2056 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2057 if (err < 0)
2058 return err;
2060 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2061 if (pcm_file == NULL) {
2062 snd_pcm_release_substream(substream);
2063 return -ENOMEM;
2065 pcm_file->substream = substream;
2066 if (substream->ref_count == 1) {
2067 str = substream->pstr;
2068 substream->file = pcm_file;
2069 substream->pcm_release = pcm_release_private;
2071 file->private_data = pcm_file;
2072 if (rpcm_file)
2073 *rpcm_file = pcm_file;
2074 return 0;
2077 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2079 struct snd_pcm *pcm;
2081 pcm = snd_lookup_minor_data(iminor(inode),
2082 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2083 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2086 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2088 struct snd_pcm *pcm;
2090 pcm = snd_lookup_minor_data(iminor(inode),
2091 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2092 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2095 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2097 int err;
2098 struct snd_pcm_file *pcm_file;
2099 wait_queue_t wait;
2101 if (pcm == NULL) {
2102 err = -ENODEV;
2103 goto __error1;
2105 err = snd_card_file_add(pcm->card, file);
2106 if (err < 0)
2107 goto __error1;
2108 if (!try_module_get(pcm->card->module)) {
2109 err = -EFAULT;
2110 goto __error2;
2112 init_waitqueue_entry(&wait, current);
2113 add_wait_queue(&pcm->open_wait, &wait);
2114 mutex_lock(&pcm->open_mutex);
2115 while (1) {
2116 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2117 if (err >= 0)
2118 break;
2119 if (err == -EAGAIN) {
2120 if (file->f_flags & O_NONBLOCK) {
2121 err = -EBUSY;
2122 break;
2124 } else
2125 break;
2126 set_current_state(TASK_INTERRUPTIBLE);
2127 mutex_unlock(&pcm->open_mutex);
2128 schedule();
2129 mutex_lock(&pcm->open_mutex);
2130 if (signal_pending(current)) {
2131 err = -ERESTARTSYS;
2132 break;
2135 remove_wait_queue(&pcm->open_wait, &wait);
2136 mutex_unlock(&pcm->open_mutex);
2137 if (err < 0)
2138 goto __error;
2139 return err;
2141 __error:
2142 module_put(pcm->card->module);
2143 __error2:
2144 snd_card_file_remove(pcm->card, file);
2145 __error1:
2146 return err;
2149 static int snd_pcm_release(struct inode *inode, struct file *file)
2151 struct snd_pcm *pcm;
2152 struct snd_pcm_substream *substream;
2153 struct snd_pcm_file *pcm_file;
2155 pcm_file = file->private_data;
2156 substream = pcm_file->substream;
2157 if (snd_BUG_ON(!substream))
2158 return -ENXIO;
2159 pcm = substream->pcm;
2160 mutex_lock(&pcm->open_mutex);
2161 snd_pcm_release_substream(substream);
2162 kfree(pcm_file);
2163 mutex_unlock(&pcm->open_mutex);
2164 wake_up(&pcm->open_wait);
2165 module_put(pcm->card->module);
2166 snd_card_file_remove(pcm->card, file);
2167 return 0;
2170 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2171 snd_pcm_uframes_t frames)
2173 struct snd_pcm_runtime *runtime = substream->runtime;
2174 snd_pcm_sframes_t appl_ptr;
2175 snd_pcm_sframes_t ret;
2176 snd_pcm_sframes_t hw_avail;
2178 if (frames == 0)
2179 return 0;
2181 snd_pcm_stream_lock_irq(substream);
2182 switch (runtime->status->state) {
2183 case SNDRV_PCM_STATE_PREPARED:
2184 break;
2185 case SNDRV_PCM_STATE_DRAINING:
2186 case SNDRV_PCM_STATE_RUNNING:
2187 if (snd_pcm_update_hw_ptr(substream) >= 0)
2188 break;
2189 /* Fall through */
2190 case SNDRV_PCM_STATE_XRUN:
2191 ret = -EPIPE;
2192 goto __end;
2193 case SNDRV_PCM_STATE_SUSPENDED:
2194 ret = -ESTRPIPE;
2195 goto __end;
2196 default:
2197 ret = -EBADFD;
2198 goto __end;
2201 hw_avail = snd_pcm_playback_hw_avail(runtime);
2202 if (hw_avail <= 0) {
2203 ret = 0;
2204 goto __end;
2206 if (frames > (snd_pcm_uframes_t)hw_avail)
2207 frames = hw_avail;
2208 appl_ptr = runtime->control->appl_ptr - frames;
2209 if (appl_ptr < 0)
2210 appl_ptr += runtime->boundary;
2211 runtime->control->appl_ptr = appl_ptr;
2212 ret = frames;
2213 __end:
2214 snd_pcm_stream_unlock_irq(substream);
2215 return ret;
2218 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2219 snd_pcm_uframes_t frames)
2221 struct snd_pcm_runtime *runtime = substream->runtime;
2222 snd_pcm_sframes_t appl_ptr;
2223 snd_pcm_sframes_t ret;
2224 snd_pcm_sframes_t hw_avail;
2226 if (frames == 0)
2227 return 0;
2229 snd_pcm_stream_lock_irq(substream);
2230 switch (runtime->status->state) {
2231 case SNDRV_PCM_STATE_PREPARED:
2232 case SNDRV_PCM_STATE_DRAINING:
2233 break;
2234 case SNDRV_PCM_STATE_RUNNING:
2235 if (snd_pcm_update_hw_ptr(substream) >= 0)
2236 break;
2237 /* Fall through */
2238 case SNDRV_PCM_STATE_XRUN:
2239 ret = -EPIPE;
2240 goto __end;
2241 case SNDRV_PCM_STATE_SUSPENDED:
2242 ret = -ESTRPIPE;
2243 goto __end;
2244 default:
2245 ret = -EBADFD;
2246 goto __end;
2249 hw_avail = snd_pcm_capture_hw_avail(runtime);
2250 if (hw_avail <= 0) {
2251 ret = 0;
2252 goto __end;
2254 if (frames > (snd_pcm_uframes_t)hw_avail)
2255 frames = hw_avail;
2256 appl_ptr = runtime->control->appl_ptr - frames;
2257 if (appl_ptr < 0)
2258 appl_ptr += runtime->boundary;
2259 runtime->control->appl_ptr = appl_ptr;
2260 ret = frames;
2261 __end:
2262 snd_pcm_stream_unlock_irq(substream);
2263 return ret;
2266 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2267 snd_pcm_uframes_t frames)
2269 struct snd_pcm_runtime *runtime = substream->runtime;
2270 snd_pcm_sframes_t appl_ptr;
2271 snd_pcm_sframes_t ret;
2272 snd_pcm_sframes_t avail;
2274 if (frames == 0)
2275 return 0;
2277 snd_pcm_stream_lock_irq(substream);
2278 switch (runtime->status->state) {
2279 case SNDRV_PCM_STATE_PREPARED:
2280 case SNDRV_PCM_STATE_PAUSED:
2281 break;
2282 case SNDRV_PCM_STATE_DRAINING:
2283 case SNDRV_PCM_STATE_RUNNING:
2284 if (snd_pcm_update_hw_ptr(substream) >= 0)
2285 break;
2286 /* Fall through */
2287 case SNDRV_PCM_STATE_XRUN:
2288 ret = -EPIPE;
2289 goto __end;
2290 case SNDRV_PCM_STATE_SUSPENDED:
2291 ret = -ESTRPIPE;
2292 goto __end;
2293 default:
2294 ret = -EBADFD;
2295 goto __end;
2298 avail = snd_pcm_playback_avail(runtime);
2299 if (avail <= 0) {
2300 ret = 0;
2301 goto __end;
2303 if (frames > (snd_pcm_uframes_t)avail)
2304 frames = avail;
2305 appl_ptr = runtime->control->appl_ptr + frames;
2306 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2307 appl_ptr -= runtime->boundary;
2308 runtime->control->appl_ptr = appl_ptr;
2309 ret = frames;
2310 __end:
2311 snd_pcm_stream_unlock_irq(substream);
2312 return ret;
2315 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2316 snd_pcm_uframes_t frames)
2318 struct snd_pcm_runtime *runtime = substream->runtime;
2319 snd_pcm_sframes_t appl_ptr;
2320 snd_pcm_sframes_t ret;
2321 snd_pcm_sframes_t avail;
2323 if (frames == 0)
2324 return 0;
2326 snd_pcm_stream_lock_irq(substream);
2327 switch (runtime->status->state) {
2328 case SNDRV_PCM_STATE_PREPARED:
2329 case SNDRV_PCM_STATE_DRAINING:
2330 case SNDRV_PCM_STATE_PAUSED:
2331 break;
2332 case SNDRV_PCM_STATE_RUNNING:
2333 if (snd_pcm_update_hw_ptr(substream) >= 0)
2334 break;
2335 /* Fall through */
2336 case SNDRV_PCM_STATE_XRUN:
2337 ret = -EPIPE;
2338 goto __end;
2339 case SNDRV_PCM_STATE_SUSPENDED:
2340 ret = -ESTRPIPE;
2341 goto __end;
2342 default:
2343 ret = -EBADFD;
2344 goto __end;
2347 avail = snd_pcm_capture_avail(runtime);
2348 if (avail <= 0) {
2349 ret = 0;
2350 goto __end;
2352 if (frames > (snd_pcm_uframes_t)avail)
2353 frames = avail;
2354 appl_ptr = runtime->control->appl_ptr + frames;
2355 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2356 appl_ptr -= runtime->boundary;
2357 runtime->control->appl_ptr = appl_ptr;
2358 ret = frames;
2359 __end:
2360 snd_pcm_stream_unlock_irq(substream);
2361 return ret;
2364 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2366 struct snd_pcm_runtime *runtime = substream->runtime;
2367 int err;
2369 snd_pcm_stream_lock_irq(substream);
2370 switch (runtime->status->state) {
2371 case SNDRV_PCM_STATE_DRAINING:
2372 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2373 goto __badfd;
2374 case SNDRV_PCM_STATE_RUNNING:
2375 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2376 break;
2377 /* Fall through */
2378 case SNDRV_PCM_STATE_PREPARED:
2379 case SNDRV_PCM_STATE_SUSPENDED:
2380 err = 0;
2381 break;
2382 case SNDRV_PCM_STATE_XRUN:
2383 err = -EPIPE;
2384 break;
2385 default:
2386 __badfd:
2387 err = -EBADFD;
2388 break;
2390 snd_pcm_stream_unlock_irq(substream);
2391 return err;
2394 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2395 snd_pcm_sframes_t __user *res)
2397 struct snd_pcm_runtime *runtime = substream->runtime;
2398 int err;
2399 snd_pcm_sframes_t n = 0;
2401 snd_pcm_stream_lock_irq(substream);
2402 switch (runtime->status->state) {
2403 case SNDRV_PCM_STATE_DRAINING:
2404 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2405 goto __badfd;
2406 case SNDRV_PCM_STATE_RUNNING:
2407 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2408 break;
2409 /* Fall through */
2410 case SNDRV_PCM_STATE_PREPARED:
2411 case SNDRV_PCM_STATE_SUSPENDED:
2412 err = 0;
2413 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2414 n = snd_pcm_playback_hw_avail(runtime);
2415 else
2416 n = snd_pcm_capture_avail(runtime);
2417 n += runtime->delay;
2418 break;
2419 case SNDRV_PCM_STATE_XRUN:
2420 err = -EPIPE;
2421 break;
2422 default:
2423 __badfd:
2424 err = -EBADFD;
2425 break;
2427 snd_pcm_stream_unlock_irq(substream);
2428 if (!err)
2429 if (put_user(n, res))
2430 err = -EFAULT;
2431 return err;
2434 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2435 struct snd_pcm_sync_ptr __user *_sync_ptr)
2437 struct snd_pcm_runtime *runtime = substream->runtime;
2438 struct snd_pcm_sync_ptr sync_ptr;
2439 volatile struct snd_pcm_mmap_status *status;
2440 volatile struct snd_pcm_mmap_control *control;
2441 int err;
2443 memset(&sync_ptr, 0, sizeof(sync_ptr));
2444 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2445 return -EFAULT;
2446 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2447 return -EFAULT;
2448 status = runtime->status;
2449 control = runtime->control;
2450 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2451 err = snd_pcm_hwsync(substream);
2452 if (err < 0)
2453 return err;
2455 snd_pcm_stream_lock_irq(substream);
2456 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2457 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2458 else
2459 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2460 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2461 control->avail_min = sync_ptr.c.control.avail_min;
2462 else
2463 sync_ptr.c.control.avail_min = control->avail_min;
2464 sync_ptr.s.status.state = status->state;
2465 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2466 sync_ptr.s.status.tstamp = status->tstamp;
2467 sync_ptr.s.status.suspended_state = status->suspended_state;
2468 snd_pcm_stream_unlock_irq(substream);
2469 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2470 return -EFAULT;
2471 return 0;
2474 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2476 struct snd_pcm_runtime *runtime = substream->runtime;
2477 int arg;
2479 if (get_user(arg, _arg))
2480 return -EFAULT;
2481 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2482 return -EINVAL;
2483 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2484 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2485 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2486 return 0;
2489 static int snd_pcm_common_ioctl1(struct file *file,
2490 struct snd_pcm_substream *substream,
2491 unsigned int cmd, void __user *arg)
2493 switch (cmd) {
2494 case SNDRV_PCM_IOCTL_PVERSION:
2495 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2496 case SNDRV_PCM_IOCTL_INFO:
2497 return snd_pcm_info_user(substream, arg);
2498 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2499 return 0;
2500 case SNDRV_PCM_IOCTL_TTSTAMP:
2501 return snd_pcm_tstamp(substream, arg);
2502 case SNDRV_PCM_IOCTL_HW_REFINE:
2503 return snd_pcm_hw_refine_user(substream, arg);
2504 case SNDRV_PCM_IOCTL_HW_PARAMS:
2505 return snd_pcm_hw_params_user(substream, arg);
2506 case SNDRV_PCM_IOCTL_HW_FREE:
2507 return snd_pcm_hw_free(substream);
2508 case SNDRV_PCM_IOCTL_SW_PARAMS:
2509 return snd_pcm_sw_params_user(substream, arg);
2510 case SNDRV_PCM_IOCTL_STATUS:
2511 return snd_pcm_status_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2513 return snd_pcm_channel_info_user(substream, arg);
2514 case SNDRV_PCM_IOCTL_PREPARE:
2515 return snd_pcm_prepare(substream, file);
2516 case SNDRV_PCM_IOCTL_RESET:
2517 return snd_pcm_reset(substream);
2518 case SNDRV_PCM_IOCTL_START:
2519 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2520 case SNDRV_PCM_IOCTL_LINK:
2521 return snd_pcm_link(substream, (int)(unsigned long) arg);
2522 case SNDRV_PCM_IOCTL_UNLINK:
2523 return snd_pcm_unlink(substream);
2524 case SNDRV_PCM_IOCTL_RESUME:
2525 return snd_pcm_resume(substream);
2526 case SNDRV_PCM_IOCTL_XRUN:
2527 return snd_pcm_xrun(substream);
2528 case SNDRV_PCM_IOCTL_HWSYNC:
2529 return snd_pcm_hwsync(substream);
2530 case SNDRV_PCM_IOCTL_DELAY:
2531 return snd_pcm_delay(substream, arg);
2532 case SNDRV_PCM_IOCTL_SYNC_PTR:
2533 return snd_pcm_sync_ptr(substream, arg);
2534 #ifdef CONFIG_SND_SUPPORT_OLD_API
2535 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2536 return snd_pcm_hw_refine_old_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2538 return snd_pcm_hw_params_old_user(substream, arg);
2539 #endif
2540 case SNDRV_PCM_IOCTL_DRAIN:
2541 return snd_pcm_drain(substream, file);
2542 case SNDRV_PCM_IOCTL_DROP:
2543 return snd_pcm_drop(substream);
2544 case SNDRV_PCM_IOCTL_PAUSE:
2546 int res;
2547 snd_pcm_stream_lock_irq(substream);
2548 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2549 snd_pcm_stream_unlock_irq(substream);
2550 return res;
2553 snd_printd("unknown ioctl = 0x%x\n", cmd);
2554 return -ENOTTY;
2557 static int snd_pcm_playback_ioctl1(struct file *file,
2558 struct snd_pcm_substream *substream,
2559 unsigned int cmd, void __user *arg)
2561 if (snd_BUG_ON(!substream))
2562 return -ENXIO;
2563 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2564 return -EINVAL;
2565 switch (cmd) {
2566 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2568 struct snd_xferi xferi;
2569 struct snd_xferi __user *_xferi = arg;
2570 struct snd_pcm_runtime *runtime = substream->runtime;
2571 snd_pcm_sframes_t result;
2572 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2573 return -EBADFD;
2574 if (put_user(0, &_xferi->result))
2575 return -EFAULT;
2576 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2577 return -EFAULT;
2578 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2579 __put_user(result, &_xferi->result);
2580 return result < 0 ? result : 0;
2582 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2584 struct snd_xfern xfern;
2585 struct snd_xfern __user *_xfern = arg;
2586 struct snd_pcm_runtime *runtime = substream->runtime;
2587 void __user **bufs;
2588 snd_pcm_sframes_t result;
2589 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2590 return -EBADFD;
2591 if (runtime->channels > 128)
2592 return -EINVAL;
2593 if (put_user(0, &_xfern->result))
2594 return -EFAULT;
2595 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2596 return -EFAULT;
2598 bufs = memdup_user(xfern.bufs,
2599 sizeof(void *) * runtime->channels);
2600 if (IS_ERR(bufs))
2601 return PTR_ERR(bufs);
2602 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2603 kfree(bufs);
2604 __put_user(result, &_xfern->result);
2605 return result < 0 ? result : 0;
2607 case SNDRV_PCM_IOCTL_REWIND:
2609 snd_pcm_uframes_t frames;
2610 snd_pcm_uframes_t __user *_frames = arg;
2611 snd_pcm_sframes_t result;
2612 if (get_user(frames, _frames))
2613 return -EFAULT;
2614 if (put_user(0, _frames))
2615 return -EFAULT;
2616 result = snd_pcm_playback_rewind(substream, frames);
2617 __put_user(result, _frames);
2618 return result < 0 ? result : 0;
2620 case SNDRV_PCM_IOCTL_FORWARD:
2622 snd_pcm_uframes_t frames;
2623 snd_pcm_uframes_t __user *_frames = arg;
2624 snd_pcm_sframes_t result;
2625 if (get_user(frames, _frames))
2626 return -EFAULT;
2627 if (put_user(0, _frames))
2628 return -EFAULT;
2629 result = snd_pcm_playback_forward(substream, frames);
2630 __put_user(result, _frames);
2631 return result < 0 ? result : 0;
2634 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2637 static int snd_pcm_capture_ioctl1(struct file *file,
2638 struct snd_pcm_substream *substream,
2639 unsigned int cmd, void __user *arg)
2641 if (snd_BUG_ON(!substream))
2642 return -ENXIO;
2643 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2644 return -EINVAL;
2645 switch (cmd) {
2646 case SNDRV_PCM_IOCTL_READI_FRAMES:
2648 struct snd_xferi xferi;
2649 struct snd_xferi __user *_xferi = arg;
2650 struct snd_pcm_runtime *runtime = substream->runtime;
2651 snd_pcm_sframes_t result;
2652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2653 return -EBADFD;
2654 if (put_user(0, &_xferi->result))
2655 return -EFAULT;
2656 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2657 return -EFAULT;
2658 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2659 __put_user(result, &_xferi->result);
2660 return result < 0 ? result : 0;
2662 case SNDRV_PCM_IOCTL_READN_FRAMES:
2664 struct snd_xfern xfern;
2665 struct snd_xfern __user *_xfern = arg;
2666 struct snd_pcm_runtime *runtime = substream->runtime;
2667 void *bufs;
2668 snd_pcm_sframes_t result;
2669 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2670 return -EBADFD;
2671 if (runtime->channels > 128)
2672 return -EINVAL;
2673 if (put_user(0, &_xfern->result))
2674 return -EFAULT;
2675 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2676 return -EFAULT;
2678 bufs = memdup_user(xfern.bufs,
2679 sizeof(void *) * runtime->channels);
2680 if (IS_ERR(bufs))
2681 return PTR_ERR(bufs);
2682 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2683 kfree(bufs);
2684 __put_user(result, &_xfern->result);
2685 return result < 0 ? result : 0;
2687 case SNDRV_PCM_IOCTL_REWIND:
2689 snd_pcm_uframes_t frames;
2690 snd_pcm_uframes_t __user *_frames = arg;
2691 snd_pcm_sframes_t result;
2692 if (get_user(frames, _frames))
2693 return -EFAULT;
2694 if (put_user(0, _frames))
2695 return -EFAULT;
2696 result = snd_pcm_capture_rewind(substream, frames);
2697 __put_user(result, _frames);
2698 return result < 0 ? result : 0;
2700 case SNDRV_PCM_IOCTL_FORWARD:
2702 snd_pcm_uframes_t frames;
2703 snd_pcm_uframes_t __user *_frames = arg;
2704 snd_pcm_sframes_t result;
2705 if (get_user(frames, _frames))
2706 return -EFAULT;
2707 if (put_user(0, _frames))
2708 return -EFAULT;
2709 result = snd_pcm_capture_forward(substream, frames);
2710 __put_user(result, _frames);
2711 return result < 0 ? result : 0;
2714 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2717 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2718 unsigned long arg)
2720 struct snd_pcm_file *pcm_file;
2722 pcm_file = file->private_data;
2724 if (((cmd >> 8) & 0xff) != 'A')
2725 return -ENOTTY;
2727 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2728 (void __user *)arg);
2731 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2732 unsigned long arg)
2734 struct snd_pcm_file *pcm_file;
2736 pcm_file = file->private_data;
2738 if (((cmd >> 8) & 0xff) != 'A')
2739 return -ENOTTY;
2741 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2742 (void __user *)arg);
2745 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2746 unsigned int cmd, void *arg)
2748 mm_segment_t fs;
2749 int result;
2751 fs = snd_enter_user();
2752 switch (substream->stream) {
2753 case SNDRV_PCM_STREAM_PLAYBACK:
2754 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2755 (void __user *)arg);
2756 break;
2757 case SNDRV_PCM_STREAM_CAPTURE:
2758 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2759 (void __user *)arg);
2760 break;
2761 default:
2762 result = -EINVAL;
2763 break;
2765 snd_leave_user(fs);
2766 return result;
2769 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2771 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2772 loff_t * offset)
2774 struct snd_pcm_file *pcm_file;
2775 struct snd_pcm_substream *substream;
2776 struct snd_pcm_runtime *runtime;
2777 snd_pcm_sframes_t result;
2779 pcm_file = file->private_data;
2780 substream = pcm_file->substream;
2781 if (PCM_RUNTIME_CHECK(substream))
2782 return -ENXIO;
2783 runtime = substream->runtime;
2784 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2785 return -EBADFD;
2786 if (!frame_aligned(runtime, count))
2787 return -EINVAL;
2788 count = bytes_to_frames(runtime, count);
2789 result = snd_pcm_lib_read(substream, buf, count);
2790 if (result > 0)
2791 result = frames_to_bytes(runtime, result);
2792 return result;
2795 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2796 size_t count, loff_t * offset)
2798 struct snd_pcm_file *pcm_file;
2799 struct snd_pcm_substream *substream;
2800 struct snd_pcm_runtime *runtime;
2801 snd_pcm_sframes_t result;
2803 pcm_file = file->private_data;
2804 substream = pcm_file->substream;
2805 if (PCM_RUNTIME_CHECK(substream))
2806 return -ENXIO;
2807 runtime = substream->runtime;
2808 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2809 return -EBADFD;
2810 if (!frame_aligned(runtime, count))
2811 return -EINVAL;
2812 count = bytes_to_frames(runtime, count);
2813 result = snd_pcm_lib_write(substream, buf, count);
2814 if (result > 0)
2815 result = frames_to_bytes(runtime, result);
2816 return result;
2819 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2820 unsigned long nr_segs, loff_t pos)
2823 struct snd_pcm_file *pcm_file;
2824 struct snd_pcm_substream *substream;
2825 struct snd_pcm_runtime *runtime;
2826 snd_pcm_sframes_t result;
2827 unsigned long i;
2828 void __user **bufs;
2829 snd_pcm_uframes_t frames;
2831 pcm_file = iocb->ki_filp->private_data;
2832 substream = pcm_file->substream;
2833 if (PCM_RUNTIME_CHECK(substream))
2834 return -ENXIO;
2835 runtime = substream->runtime;
2836 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2837 return -EBADFD;
2838 if (nr_segs > 1024 || nr_segs != runtime->channels)
2839 return -EINVAL;
2840 if (!frame_aligned(runtime, iov->iov_len))
2841 return -EINVAL;
2842 frames = bytes_to_samples(runtime, iov->iov_len);
2843 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2844 if (bufs == NULL)
2845 return -ENOMEM;
2846 for (i = 0; i < nr_segs; ++i)
2847 bufs[i] = iov[i].iov_base;
2848 result = snd_pcm_lib_readv(substream, bufs, frames);
2849 if (result > 0)
2850 result = frames_to_bytes(runtime, result);
2851 kfree(bufs);
2852 return result;
2855 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2856 unsigned long nr_segs, loff_t pos)
2858 struct snd_pcm_file *pcm_file;
2859 struct snd_pcm_substream *substream;
2860 struct snd_pcm_runtime *runtime;
2861 snd_pcm_sframes_t result;
2862 unsigned long i;
2863 void __user **bufs;
2864 snd_pcm_uframes_t frames;
2866 pcm_file = iocb->ki_filp->private_data;
2867 substream = pcm_file->substream;
2868 if (PCM_RUNTIME_CHECK(substream))
2869 return -ENXIO;
2870 runtime = substream->runtime;
2871 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2872 return -EBADFD;
2873 if (nr_segs > 128 || nr_segs != runtime->channels ||
2874 !frame_aligned(runtime, iov->iov_len))
2875 return -EINVAL;
2876 frames = bytes_to_samples(runtime, iov->iov_len);
2877 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2878 if (bufs == NULL)
2879 return -ENOMEM;
2880 for (i = 0; i < nr_segs; ++i)
2881 bufs[i] = iov[i].iov_base;
2882 result = snd_pcm_lib_writev(substream, bufs, frames);
2883 if (result > 0)
2884 result = frames_to_bytes(runtime, result);
2885 kfree(bufs);
2886 return result;
2889 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2891 struct snd_pcm_file *pcm_file;
2892 struct snd_pcm_substream *substream;
2893 struct snd_pcm_runtime *runtime;
2894 unsigned int mask;
2895 snd_pcm_uframes_t avail;
2897 pcm_file = file->private_data;
2899 substream = pcm_file->substream;
2900 if (PCM_RUNTIME_CHECK(substream))
2901 return -ENXIO;
2902 runtime = substream->runtime;
2904 poll_wait(file, &runtime->sleep, wait);
2906 snd_pcm_stream_lock_irq(substream);
2907 avail = snd_pcm_playback_avail(runtime);
2908 switch (runtime->status->state) {
2909 case SNDRV_PCM_STATE_RUNNING:
2910 case SNDRV_PCM_STATE_PREPARED:
2911 case SNDRV_PCM_STATE_PAUSED:
2912 if (avail >= runtime->control->avail_min) {
2913 mask = POLLOUT | POLLWRNORM;
2914 break;
2916 /* Fall through */
2917 case SNDRV_PCM_STATE_DRAINING:
2918 mask = 0;
2919 break;
2920 default:
2921 mask = POLLOUT | POLLWRNORM | POLLERR;
2922 break;
2924 snd_pcm_stream_unlock_irq(substream);
2925 return mask;
2928 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2930 struct snd_pcm_file *pcm_file;
2931 struct snd_pcm_substream *substream;
2932 struct snd_pcm_runtime *runtime;
2933 unsigned int mask;
2934 snd_pcm_uframes_t avail;
2936 pcm_file = file->private_data;
2938 substream = pcm_file->substream;
2939 if (PCM_RUNTIME_CHECK(substream))
2940 return -ENXIO;
2941 runtime = substream->runtime;
2943 poll_wait(file, &runtime->sleep, wait);
2945 snd_pcm_stream_lock_irq(substream);
2946 avail = snd_pcm_capture_avail(runtime);
2947 switch (runtime->status->state) {
2948 case SNDRV_PCM_STATE_RUNNING:
2949 case SNDRV_PCM_STATE_PREPARED:
2950 case SNDRV_PCM_STATE_PAUSED:
2951 if (avail >= runtime->control->avail_min) {
2952 mask = POLLIN | POLLRDNORM;
2953 break;
2955 mask = 0;
2956 break;
2957 case SNDRV_PCM_STATE_DRAINING:
2958 if (avail > 0) {
2959 mask = POLLIN | POLLRDNORM;
2960 break;
2962 /* Fall through */
2963 default:
2964 mask = POLLIN | POLLRDNORM | POLLERR;
2965 break;
2967 snd_pcm_stream_unlock_irq(substream);
2968 return mask;
2972 * mmap support
2976 * Only on coherent architectures, we can mmap the status and the control records
2977 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2979 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2981 * mmap status record
2983 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2984 struct vm_fault *vmf)
2986 struct snd_pcm_substream *substream = area->vm_private_data;
2987 struct snd_pcm_runtime *runtime;
2989 if (substream == NULL)
2990 return VM_FAULT_SIGBUS;
2991 runtime = substream->runtime;
2992 vmf->page = virt_to_page(runtime->status);
2993 get_page(vmf->page);
2994 return 0;
2997 static const struct vm_operations_struct snd_pcm_vm_ops_status =
2999 .fault = snd_pcm_mmap_status_fault,
3002 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3003 struct vm_area_struct *area)
3005 struct snd_pcm_runtime *runtime;
3006 long size;
3007 if (!(area->vm_flags & VM_READ))
3008 return -EINVAL;
3009 runtime = substream->runtime;
3010 size = area->vm_end - area->vm_start;
3011 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3012 return -EINVAL;
3013 area->vm_ops = &snd_pcm_vm_ops_status;
3014 area->vm_private_data = substream;
3015 area->vm_flags |= VM_RESERVED;
3016 return 0;
3020 * mmap control record
3022 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3023 struct vm_fault *vmf)
3025 struct snd_pcm_substream *substream = area->vm_private_data;
3026 struct snd_pcm_runtime *runtime;
3028 if (substream == NULL)
3029 return VM_FAULT_SIGBUS;
3030 runtime = substream->runtime;
3031 vmf->page = virt_to_page(runtime->control);
3032 get_page(vmf->page);
3033 return 0;
3036 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3038 .fault = snd_pcm_mmap_control_fault,
3041 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3042 struct vm_area_struct *area)
3044 struct snd_pcm_runtime *runtime;
3045 long size;
3046 if (!(area->vm_flags & VM_READ))
3047 return -EINVAL;
3048 runtime = substream->runtime;
3049 size = area->vm_end - area->vm_start;
3050 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3051 return -EINVAL;
3052 area->vm_ops = &snd_pcm_vm_ops_control;
3053 area->vm_private_data = substream;
3054 area->vm_flags |= VM_RESERVED;
3055 return 0;
3057 #else /* ! coherent mmap */
3059 * don't support mmap for status and control records.
3061 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3062 struct vm_area_struct *area)
3064 return -ENXIO;
3066 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3067 struct vm_area_struct *area)
3069 return -ENXIO;
3071 #endif /* coherent mmap */
3073 static inline struct page *
3074 snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3076 void *vaddr = substream->runtime->dma_area + ofs;
3077 #if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3078 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3079 return virt_to_page(CAC_ADDR(vaddr));
3080 #endif
3081 #if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3082 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3083 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3084 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3085 /* assume dma_handle set via pfn_to_phys() in
3086 * mm/dma-noncoherent.c
3088 return pfn_to_page(addr >> PAGE_SHIFT);
3090 #endif
3091 return virt_to_page(vaddr);
3095 * fault callback for mmapping a RAM page
3097 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3098 struct vm_fault *vmf)
3100 struct snd_pcm_substream *substream = area->vm_private_data;
3101 struct snd_pcm_runtime *runtime;
3102 unsigned long offset;
3103 struct page * page;
3104 size_t dma_bytes;
3106 if (substream == NULL)
3107 return VM_FAULT_SIGBUS;
3108 runtime = substream->runtime;
3109 offset = vmf->pgoff << PAGE_SHIFT;
3110 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3111 if (offset > dma_bytes - PAGE_SIZE)
3112 return VM_FAULT_SIGBUS;
3113 if (substream->ops->page)
3114 page = substream->ops->page(substream, offset);
3115 else
3116 page = snd_pcm_default_page_ops(substream, offset);
3117 if (!page)
3118 return VM_FAULT_SIGBUS;
3119 get_page(page);
3120 vmf->page = page;
3121 return 0;
3124 static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3125 .open = snd_pcm_mmap_data_open,
3126 .close = snd_pcm_mmap_data_close,
3129 static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
3130 .open = snd_pcm_mmap_data_open,
3131 .close = snd_pcm_mmap_data_close,
3132 .fault = snd_pcm_mmap_data_fault,
3135 #ifndef ARCH_HAS_DMA_MMAP_COHERENT
3136 /* This should be defined / handled globally! */
3137 #ifdef CONFIG_ARM
3138 #define ARCH_HAS_DMA_MMAP_COHERENT
3139 #endif
3140 #endif
3143 * mmap the DMA buffer on RAM
3145 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3146 struct vm_area_struct *area)
3148 area->vm_flags |= VM_RESERVED;
3149 #ifdef ARCH_HAS_DMA_MMAP_COHERENT
3150 if (!substream->ops->page &&
3151 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3152 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3153 area,
3154 substream->runtime->dma_area,
3155 substream->runtime->dma_addr,
3156 area->vm_end - area->vm_start);
3157 #elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3158 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3159 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3160 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3161 #endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3162 /* mmap with fault handler */
3163 area->vm_ops = &snd_pcm_vm_ops_data_fault;
3164 return 0;
3168 * mmap the DMA buffer on I/O memory area
3170 #if SNDRV_PCM_INFO_MMAP_IOMEM
3171 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3172 struct vm_area_struct *area)
3174 long size;
3175 unsigned long offset;
3177 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3178 area->vm_flags |= VM_IO;
3179 size = area->vm_end - area->vm_start;
3180 offset = area->vm_pgoff << PAGE_SHIFT;
3181 if (io_remap_pfn_range(area, area->vm_start,
3182 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3183 size, area->vm_page_prot))
3184 return -EAGAIN;
3185 return 0;
3188 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3189 #endif /* SNDRV_PCM_INFO_MMAP */
3191 /* mmap callback with pgprot_noncached */
3192 int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3193 struct vm_area_struct *area)
3195 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3196 return snd_pcm_default_mmap(substream, area);
3198 EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3201 * mmap DMA buffer
3203 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3204 struct vm_area_struct *area)
3206 struct snd_pcm_runtime *runtime;
3207 long size;
3208 unsigned long offset;
3209 size_t dma_bytes;
3210 int err;
3212 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3213 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3214 return -EINVAL;
3215 } else {
3216 if (!(area->vm_flags & VM_READ))
3217 return -EINVAL;
3219 runtime = substream->runtime;
3220 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3221 return -EBADFD;
3222 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3223 return -ENXIO;
3224 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3225 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3226 return -EINVAL;
3227 size = area->vm_end - area->vm_start;
3228 offset = area->vm_pgoff << PAGE_SHIFT;
3229 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3230 if ((size_t)size > dma_bytes)
3231 return -EINVAL;
3232 if (offset > dma_bytes - size)
3233 return -EINVAL;
3235 area->vm_ops = &snd_pcm_vm_ops_data;
3236 area->vm_private_data = substream;
3237 if (substream->ops->mmap)
3238 err = substream->ops->mmap(substream, area);
3239 else
3240 err = snd_pcm_default_mmap(substream, area);
3241 if (!err)
3242 atomic_inc(&substream->mmap_count);
3243 return err;
3246 EXPORT_SYMBOL(snd_pcm_mmap_data);
3248 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3250 struct snd_pcm_file * pcm_file;
3251 struct snd_pcm_substream *substream;
3252 unsigned long offset;
3254 pcm_file = file->private_data;
3255 substream = pcm_file->substream;
3256 if (PCM_RUNTIME_CHECK(substream))
3257 return -ENXIO;
3259 offset = area->vm_pgoff << PAGE_SHIFT;
3260 switch (offset) {
3261 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3262 if (pcm_file->no_compat_mmap)
3263 return -ENXIO;
3264 return snd_pcm_mmap_status(substream, file, area);
3265 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3266 if (pcm_file->no_compat_mmap)
3267 return -ENXIO;
3268 return snd_pcm_mmap_control(substream, file, area);
3269 default:
3270 return snd_pcm_mmap_data(substream, file, area);
3272 return 0;
3275 static int snd_pcm_fasync(int fd, struct file * file, int on)
3277 struct snd_pcm_file * pcm_file;
3278 struct snd_pcm_substream *substream;
3279 struct snd_pcm_runtime *runtime;
3280 int err = -ENXIO;
3282 lock_kernel();
3283 pcm_file = file->private_data;
3284 substream = pcm_file->substream;
3285 if (PCM_RUNTIME_CHECK(substream))
3286 goto out;
3287 runtime = substream->runtime;
3288 err = fasync_helper(fd, file, on, &runtime->fasync);
3289 out:
3290 unlock_kernel();
3291 return err;
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 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3412 unsigned long len, unsigned long pgoff,
3413 unsigned long flags)
3415 return 0;
3417 #else
3418 # define dummy_get_unmapped_area NULL
3419 #endif
3422 * Register section
3425 const struct file_operations snd_pcm_f_ops[2] = {
3427 .owner = THIS_MODULE,
3428 .write = snd_pcm_write,
3429 .aio_write = snd_pcm_aio_write,
3430 .open = snd_pcm_playback_open,
3431 .release = snd_pcm_release,
3432 .poll = snd_pcm_playback_poll,
3433 .unlocked_ioctl = snd_pcm_playback_ioctl,
3434 .compat_ioctl = snd_pcm_ioctl_compat,
3435 .mmap = snd_pcm_mmap,
3436 .fasync = snd_pcm_fasync,
3437 .get_unmapped_area = dummy_get_unmapped_area,
3440 .owner = THIS_MODULE,
3441 .read = snd_pcm_read,
3442 .aio_read = snd_pcm_aio_read,
3443 .open = snd_pcm_capture_open,
3444 .release = snd_pcm_release,
3445 .poll = snd_pcm_capture_poll,
3446 .unlocked_ioctl = snd_pcm_capture_ioctl,
3447 .compat_ioctl = snd_pcm_ioctl_compat,
3448 .mmap = snd_pcm_mmap,
3449 .fasync = snd_pcm_fasync,
3450 .get_unmapped_area = dummy_get_unmapped_area,