revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / sound / core / pcm_native.c
blob725f51b089363c1b6e5ec3460be1a1b55b3f5d85
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 <sound/driver.h>
23 #include <linux/mm.h>
24 #include <linux/file.h>
25 #include <linux/slab.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <sound/core.h>
30 #include <sound/control.h>
31 #include <sound/info.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/timer.h>
35 #include <sound/minors.h>
36 #include <asm/io.h>
39 * Compatibility
42 struct snd_pcm_hw_params_old {
43 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
46 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
47 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
48 unsigned int rmask;
49 unsigned int cmask;
50 unsigned int info;
51 unsigned int msbits;
52 unsigned int rate_num;
53 unsigned int rate_den;
54 snd_pcm_uframes_t fifo_size;
55 unsigned char reserved[64];
58 #ifdef CONFIG_SND_SUPPORT_OLD_API
59 #define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60 #define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
62 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66 #endif
67 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
73 DEFINE_RWLOCK(snd_pcm_link_rwlock);
74 EXPORT_SYMBOL(snd_pcm_link_rwlock);
76 static DECLARE_RWSEM(snd_pcm_link_rwsem);
78 static inline mm_segment_t snd_enter_user(void)
80 mm_segment_t fs = get_fs();
81 set_fs(get_ds());
82 return fs;
85 static inline void snd_leave_user(mm_segment_t fs)
87 set_fs(fs);
92 int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
94 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
98 snd_assert(substream != NULL, return -ENXIO);
99 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
113 if (runtime) {
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
117 return 0;
120 int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
123 struct snd_pcm_info *info;
124 int err;
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
127 if (! info)
128 return -ENOMEM;
129 err = snd_pcm_info(substream, info);
130 if (err >= 0) {
131 if (copy_to_user(_info, info, sizeof(*info)))
132 err = -EFAULT;
134 kfree(info);
135 return err;
138 #undef RULES_DEBUG
140 #ifdef RULES_DEBUG
141 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142 char *snd_pcm_hw_param_names[] = {
143 HW_PARAM(ACCESS),
144 HW_PARAM(FORMAT),
145 HW_PARAM(SUBFORMAT),
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
148 HW_PARAM(CHANNELS),
149 HW_PARAM(RATE),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(PERIODS),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
157 HW_PARAM(TICK_TIME),
159 #endif
161 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
164 unsigned int k;
165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
172 int changed, again;
174 params->info = 0;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 params->msbits = 0;
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
186 return -EINVAL;
187 if (!(params->rmask & (1 << k)))
188 continue;
189 #ifdef RULES_DEBUG
190 printk("%s = ", snd_pcm_hw_param_names[k]);
191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192 #endif
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194 #ifdef RULES_DEBUG
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
196 #endif
197 if (changed)
198 params->cmask |= 1 << k;
199 if (changed < 0)
200 return changed;
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
206 return -EINVAL;
207 if (!(params->rmask & (1 << k)))
208 continue;
209 #ifdef RULES_DEBUG
210 printk("%s = ", snd_pcm_hw_param_names[k]);
211 if (i->empty)
212 printk("empty");
213 else
214 printk("%c%u %u%c",
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
217 printk(" -> ");
218 #endif
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
220 #ifdef RULES_DEBUG
221 if (i->empty)
222 printk("empty\n");
223 else
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
227 #endif
228 if (changed)
229 params->cmask |= 1 << k;
230 if (changed < 0)
231 return changed;
234 for (k = 0; k < constrs->rules_num; k++)
235 rstamps[k] = 0;
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
238 do {
239 again = 0;
240 for (k = 0; k < constrs->rules_num; k++) {
241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
242 unsigned int d;
243 int doit = 0;
244 if (r->cond && !(r->cond & params->flags))
245 continue;
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
248 doit = 1;
249 break;
252 if (!doit)
253 continue;
254 #ifdef RULES_DEBUG
255 printk("Rule %d [%p]: ", k, r->func);
256 if (r->var >= 0) {
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
261 } else {
262 i = hw_param_interval(params, r->var);
263 if (i->empty)
264 printk("empty");
265 else
266 printk("%c%u %u%c",
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
271 #endif
272 changed = r->func(params, r);
273 #ifdef RULES_DEBUG
274 if (r->var >= 0) {
275 printk(" -> ");
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
278 else {
279 if (i->empty)
280 printk("empty");
281 else
282 printk("%c%u %u%c",
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
287 printk("\n");
288 #endif
289 rstamps[k] = stamp;
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
293 again = 1;
295 if (changed < 0)
296 return changed;
297 stamp++;
299 } while (again);
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
314 hw = &substream->runtime->hw;
315 if (!params->info)
316 params->info = hw->info;
317 if (!params->fifo_size)
318 params->fifo_size = hw->fifo_size;
319 params->rmask = 0;
320 return 0;
323 EXPORT_SYMBOL(snd_pcm_hw_refine);
325 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
326 struct snd_pcm_hw_params __user * _params)
328 struct snd_pcm_hw_params *params;
329 int err;
331 params = kmalloc(sizeof(*params), GFP_KERNEL);
332 if (!params) {
333 err = -ENOMEM;
334 goto out;
336 if (copy_from_user(params, _params, sizeof(*params))) {
337 err = -EFAULT;
338 goto out;
340 err = snd_pcm_hw_refine(substream, params);
341 if (copy_to_user(_params, params, sizeof(*params))) {
342 if (!err)
343 err = -EFAULT;
345 out:
346 kfree(params);
347 return err;
350 static int period_to_usecs(struct snd_pcm_runtime *runtime)
352 int usecs;
354 if (! runtime->rate)
355 return -1; /* invalid */
357 /* take 75% of period time as the deadline */
358 usecs = (750000 / runtime->rate) * runtime->period_size;
359 usecs += ((750000 % runtime->rate) * runtime->period_size) /
360 runtime->rate;
362 return usecs;
365 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
366 struct snd_pcm_hw_params *params)
368 struct snd_pcm_runtime *runtime;
369 int err, usecs;
370 unsigned int bits;
371 snd_pcm_uframes_t frames;
373 snd_assert(substream != NULL, return -ENXIO);
374 runtime = substream->runtime;
375 snd_assert(runtime != NULL, return -ENXIO);
376 snd_pcm_stream_lock_irq(substream);
377 switch (runtime->status->state) {
378 case SNDRV_PCM_STATE_OPEN:
379 case SNDRV_PCM_STATE_SETUP:
380 case SNDRV_PCM_STATE_PREPARED:
381 break;
382 default:
383 snd_pcm_stream_unlock_irq(substream);
384 return -EBADFD;
386 snd_pcm_stream_unlock_irq(substream);
387 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
388 if (!substream->oss.oss)
389 #endif
390 if (atomic_read(&substream->mmap_count))
391 return -EBADFD;
393 params->rmask = ~0U;
394 err = snd_pcm_hw_refine(substream, params);
395 if (err < 0)
396 goto _error;
398 err = snd_pcm_hw_params_choose(substream, params);
399 if (err < 0)
400 goto _error;
402 if (substream->ops->hw_params != NULL) {
403 err = substream->ops->hw_params(substream, params);
404 if (err < 0)
405 goto _error;
408 runtime->access = params_access(params);
409 runtime->format = params_format(params);
410 runtime->subformat = params_subformat(params);
411 runtime->channels = params_channels(params);
412 runtime->rate = params_rate(params);
413 runtime->period_size = params_period_size(params);
414 runtime->periods = params_periods(params);
415 runtime->buffer_size = params_buffer_size(params);
416 runtime->tick_time = params_tick_time(params);
417 runtime->info = params->info;
418 runtime->rate_num = params->rate_num;
419 runtime->rate_den = params->rate_den;
421 bits = snd_pcm_format_physical_width(runtime->format);
422 runtime->sample_bits = bits;
423 bits *= runtime->channels;
424 runtime->frame_bits = bits;
425 frames = 1;
426 while (bits % 8 != 0) {
427 bits *= 2;
428 frames *= 2;
430 runtime->byte_align = bits / 8;
431 runtime->min_align = frames;
433 /* Default sw params */
434 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
435 runtime->period_step = 1;
436 runtime->sleep_min = 0;
437 runtime->control->avail_min = runtime->period_size;
438 runtime->xfer_align = runtime->period_size;
439 runtime->start_threshold = 1;
440 runtime->stop_threshold = runtime->buffer_size;
441 runtime->silence_threshold = 0;
442 runtime->silence_size = 0;
443 runtime->boundary = runtime->buffer_size;
444 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
445 runtime->boundary *= 2;
447 snd_pcm_timer_resolution_change(substream);
448 runtime->status->state = SNDRV_PCM_STATE_SETUP;
450 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
451 substream->latency_id);
452 if ((usecs = period_to_usecs(runtime)) >= 0)
453 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
454 substream->latency_id, usecs);
455 return 0;
456 _error:
457 /* hardware might be unuseable from this time,
458 so we force application to retry to set
459 the correct hardware parameter settings */
460 runtime->status->state = SNDRV_PCM_STATE_OPEN;
461 if (substream->ops->hw_free != NULL)
462 substream->ops->hw_free(substream);
463 return err;
466 static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params __user * _params)
469 struct snd_pcm_hw_params *params;
470 int err;
472 params = kmalloc(sizeof(*params), GFP_KERNEL);
473 if (!params) {
474 err = -ENOMEM;
475 goto out;
477 if (copy_from_user(params, _params, sizeof(*params))) {
478 err = -EFAULT;
479 goto out;
481 err = snd_pcm_hw_params(substream, params);
482 if (copy_to_user(_params, params, sizeof(*params))) {
483 if (!err)
484 err = -EFAULT;
486 out:
487 kfree(params);
488 return err;
491 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
493 struct snd_pcm_runtime *runtime;
494 int result = 0;
496 snd_assert(substream != NULL, return -ENXIO);
497 runtime = substream->runtime;
498 snd_assert(runtime != NULL, return -ENXIO);
499 snd_pcm_stream_lock_irq(substream);
500 switch (runtime->status->state) {
501 case SNDRV_PCM_STATE_SETUP:
502 case SNDRV_PCM_STATE_PREPARED:
503 break;
504 default:
505 snd_pcm_stream_unlock_irq(substream);
506 return -EBADFD;
508 snd_pcm_stream_unlock_irq(substream);
509 if (atomic_read(&substream->mmap_count))
510 return -EBADFD;
511 if (substream->ops->hw_free)
512 result = substream->ops->hw_free(substream);
513 runtime->status->state = SNDRV_PCM_STATE_OPEN;
514 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
515 substream->latency_id);
516 return result;
519 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
520 struct snd_pcm_sw_params *params)
522 struct snd_pcm_runtime *runtime;
524 snd_assert(substream != NULL, return -ENXIO);
525 runtime = substream->runtime;
526 snd_assert(runtime != NULL, return -ENXIO);
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->xfer_align == 0 ||
539 params->xfer_align % runtime->min_align != 0)
540 return -EINVAL;
541 if (params->silence_size >= runtime->boundary) {
542 if (params->silence_threshold != 0)
543 return -EINVAL;
544 } else {
545 if (params->silence_size > params->silence_threshold)
546 return -EINVAL;
547 if (params->silence_threshold > runtime->buffer_size)
548 return -EINVAL;
550 snd_pcm_stream_lock_irq(substream);
551 runtime->tstamp_mode = params->tstamp_mode;
552 runtime->sleep_min = params->sleep_min;
553 runtime->period_step = params->period_step;
554 runtime->control->avail_min = params->avail_min;
555 runtime->start_threshold = params->start_threshold;
556 runtime->stop_threshold = params->stop_threshold;
557 runtime->silence_threshold = params->silence_threshold;
558 runtime->silence_size = params->silence_size;
559 runtime->xfer_align = params->xfer_align;
560 params->boundary = runtime->boundary;
561 if (snd_pcm_running(substream)) {
562 if (runtime->sleep_min)
563 snd_pcm_tick_prepare(substream);
564 else
565 snd_pcm_tick_set(substream, 0);
566 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
567 runtime->silence_size > 0)
568 snd_pcm_playback_silence(substream, ULONG_MAX);
569 wake_up(&runtime->sleep);
571 snd_pcm_stream_unlock_irq(substream);
572 return 0;
575 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
576 struct snd_pcm_sw_params __user * _params)
578 struct snd_pcm_sw_params params;
579 int err;
580 if (copy_from_user(&params, _params, sizeof(params)))
581 return -EFAULT;
582 err = snd_pcm_sw_params(substream, &params);
583 if (copy_to_user(_params, &params, sizeof(params)))
584 return -EFAULT;
585 return err;
588 int snd_pcm_status(struct snd_pcm_substream *substream,
589 struct snd_pcm_status *status)
591 struct snd_pcm_runtime *runtime = substream->runtime;
593 snd_pcm_stream_lock_irq(substream);
594 status->state = runtime->status->state;
595 status->suspended_state = runtime->status->suspended_state;
596 if (status->state == SNDRV_PCM_STATE_OPEN)
597 goto _end;
598 status->trigger_tstamp = runtime->trigger_tstamp;
599 if (snd_pcm_running(substream)) {
600 snd_pcm_update_hw_ptr(substream);
601 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
602 status->tstamp = runtime->status->tstamp;
603 else
604 getnstimeofday(&status->tstamp);
605 } else
606 getnstimeofday(&status->tstamp);
607 status->appl_ptr = runtime->control->appl_ptr;
608 status->hw_ptr = runtime->status->hw_ptr;
609 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
610 status->avail = snd_pcm_playback_avail(runtime);
611 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
612 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
613 status->delay = runtime->buffer_size - status->avail;
614 else
615 status->delay = 0;
616 } else {
617 status->avail = snd_pcm_capture_avail(runtime);
618 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
619 status->delay = status->avail;
620 else
621 status->delay = 0;
623 status->avail_max = runtime->avail_max;
624 status->overrange = runtime->overrange;
625 runtime->avail_max = 0;
626 runtime->overrange = 0;
627 _end:
628 snd_pcm_stream_unlock_irq(substream);
629 return 0;
632 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
633 struct snd_pcm_status __user * _status)
635 struct snd_pcm_status status;
636 struct snd_pcm_runtime *runtime;
637 int res;
639 snd_assert(substream != NULL, return -ENXIO);
640 runtime = substream->runtime;
641 memset(&status, 0, sizeof(status));
642 res = snd_pcm_status(substream, &status);
643 if (res < 0)
644 return res;
645 if (copy_to_user(_status, &status, sizeof(status)))
646 return -EFAULT;
647 return 0;
650 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
651 struct snd_pcm_channel_info * info)
653 struct snd_pcm_runtime *runtime;
654 unsigned int channel;
656 snd_assert(substream != NULL, return -ENXIO);
657 channel = info->channel;
658 runtime = substream->runtime;
659 snd_pcm_stream_lock_irq(substream);
660 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
661 snd_pcm_stream_unlock_irq(substream);
662 return -EBADFD;
664 snd_pcm_stream_unlock_irq(substream);
665 if (channel >= runtime->channels)
666 return -EINVAL;
667 memset(info, 0, sizeof(*info));
668 info->channel = channel;
669 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
672 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
673 struct snd_pcm_channel_info __user * _info)
675 struct snd_pcm_channel_info info;
676 int res;
678 if (copy_from_user(&info, _info, sizeof(info)))
679 return -EFAULT;
680 res = snd_pcm_channel_info(substream, &info);
681 if (res < 0)
682 return res;
683 if (copy_to_user(_info, &info, sizeof(info)))
684 return -EFAULT;
685 return 0;
688 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
690 struct snd_pcm_runtime *runtime = substream->runtime;
691 if (runtime->trigger_master == NULL)
692 return;
693 if (runtime->trigger_master == substream) {
694 getnstimeofday(&runtime->trigger_tstamp);
695 } else {
696 snd_pcm_trigger_tstamp(runtime->trigger_master);
697 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
699 runtime->trigger_master = NULL;
702 struct action_ops {
703 int (*pre_action)(struct snd_pcm_substream *substream, int state);
704 int (*do_action)(struct snd_pcm_substream *substream, int state);
705 void (*undo_action)(struct snd_pcm_substream *substream, int state);
706 void (*post_action)(struct snd_pcm_substream *substream, int state);
710 * this functions is core for handling of linked stream
711 * Note: the stream state might be changed also on failure
712 * Note2: call with calling stream lock + link lock
714 static int snd_pcm_action_group(struct action_ops *ops,
715 struct snd_pcm_substream *substream,
716 int state, int do_lock)
718 struct snd_pcm_substream *s = NULL;
719 struct snd_pcm_substream *s1;
720 int res = 0;
722 snd_pcm_group_for_each_entry(s, substream) {
723 if (do_lock && s != substream)
724 spin_lock_nested(&s->self_group.lock,
725 SINGLE_DEPTH_NESTING);
726 res = ops->pre_action(s, state);
727 if (res < 0)
728 goto _unlock;
730 snd_pcm_group_for_each_entry(s, substream) {
731 res = ops->do_action(s, state);
732 if (res < 0) {
733 if (ops->undo_action) {
734 snd_pcm_group_for_each_entry(s1, substream) {
735 if (s1 == s) /* failed stream */
736 break;
737 ops->undo_action(s1, state);
740 s = NULL; /* unlock all */
741 goto _unlock;
744 snd_pcm_group_for_each_entry(s, substream) {
745 ops->post_action(s, state);
747 _unlock:
748 if (do_lock) {
749 /* unlock streams */
750 snd_pcm_group_for_each_entry(s1, substream) {
751 if (s1 != substream)
752 spin_unlock(&s1->self_group.lock);
753 if (s1 == s) /* end */
754 break;
757 return res;
761 * Note: call with stream lock
763 static int snd_pcm_action_single(struct action_ops *ops,
764 struct snd_pcm_substream *substream,
765 int state)
767 int res;
769 res = ops->pre_action(substream, state);
770 if (res < 0)
771 return res;
772 res = ops->do_action(substream, state);
773 if (res == 0)
774 ops->post_action(substream, state);
775 else if (ops->undo_action)
776 ops->undo_action(substream, state);
777 return res;
781 * Note: call with stream lock
783 static int snd_pcm_action(struct action_ops *ops,
784 struct snd_pcm_substream *substream,
785 int state)
787 int res;
789 if (snd_pcm_stream_linked(substream)) {
790 if (!spin_trylock(&substream->group->lock)) {
791 spin_unlock(&substream->self_group.lock);
792 spin_lock(&substream->group->lock);
793 spin_lock(&substream->self_group.lock);
795 res = snd_pcm_action_group(ops, substream, state, 1);
796 spin_unlock(&substream->group->lock);
797 } else {
798 res = snd_pcm_action_single(ops, substream, state);
800 return res;
804 * Note: don't use any locks before
806 static int snd_pcm_action_lock_irq(struct action_ops *ops,
807 struct snd_pcm_substream *substream,
808 int state)
810 int res;
812 read_lock_irq(&snd_pcm_link_rwlock);
813 if (snd_pcm_stream_linked(substream)) {
814 spin_lock(&substream->group->lock);
815 spin_lock(&substream->self_group.lock);
816 res = snd_pcm_action_group(ops, substream, state, 1);
817 spin_unlock(&substream->self_group.lock);
818 spin_unlock(&substream->group->lock);
819 } else {
820 spin_lock(&substream->self_group.lock);
821 res = snd_pcm_action_single(ops, substream, state);
822 spin_unlock(&substream->self_group.lock);
824 read_unlock_irq(&snd_pcm_link_rwlock);
825 return res;
830 static int snd_pcm_action_nonatomic(struct action_ops *ops,
831 struct snd_pcm_substream *substream,
832 int state)
834 int res;
836 down_read(&snd_pcm_link_rwsem);
837 if (snd_pcm_stream_linked(substream))
838 res = snd_pcm_action_group(ops, substream, state, 0);
839 else
840 res = snd_pcm_action_single(ops, substream, state);
841 up_read(&snd_pcm_link_rwsem);
842 return res;
846 * start callbacks
848 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
850 struct snd_pcm_runtime *runtime = substream->runtime;
851 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
852 return -EBADFD;
853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
854 !snd_pcm_playback_data(substream))
855 return -EPIPE;
856 runtime->trigger_master = substream;
857 return 0;
860 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
862 if (substream->runtime->trigger_master != substream)
863 return 0;
864 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
867 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
869 if (substream->runtime->trigger_master == substream)
870 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
873 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
875 struct snd_pcm_runtime *runtime = substream->runtime;
876 snd_pcm_trigger_tstamp(substream);
877 runtime->status->state = state;
878 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
879 runtime->silence_size > 0)
880 snd_pcm_playback_silence(substream, ULONG_MAX);
881 if (runtime->sleep_min)
882 snd_pcm_tick_prepare(substream);
883 if (substream->timer)
884 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
885 &runtime->trigger_tstamp);
888 static struct action_ops snd_pcm_action_start = {
889 .pre_action = snd_pcm_pre_start,
890 .do_action = snd_pcm_do_start,
891 .undo_action = snd_pcm_undo_start,
892 .post_action = snd_pcm_post_start
896 * snd_pcm_start
897 * @substream: the PCM substream instance
899 * Start all linked streams.
901 int snd_pcm_start(struct snd_pcm_substream *substream)
903 return snd_pcm_action(&snd_pcm_action_start, substream,
904 SNDRV_PCM_STATE_RUNNING);
908 * stop callbacks
910 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
912 struct snd_pcm_runtime *runtime = substream->runtime;
913 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
914 return -EBADFD;
915 runtime->trigger_master = substream;
916 return 0;
919 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
921 if (substream->runtime->trigger_master == substream &&
922 snd_pcm_running(substream))
923 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
924 return 0; /* unconditonally stop all substreams */
927 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
929 struct snd_pcm_runtime *runtime = substream->runtime;
930 if (runtime->status->state != state) {
931 snd_pcm_trigger_tstamp(substream);
932 if (substream->timer)
933 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
934 &runtime->trigger_tstamp);
935 runtime->status->state = state;
936 snd_pcm_tick_set(substream, 0);
938 wake_up(&runtime->sleep);
941 static struct action_ops snd_pcm_action_stop = {
942 .pre_action = snd_pcm_pre_stop,
943 .do_action = snd_pcm_do_stop,
944 .post_action = snd_pcm_post_stop
948 * snd_pcm_stop
949 * @substream: the PCM substream instance
950 * @state: PCM state after stopping the stream
952 * Try to stop all running streams in the substream group.
953 * The state of each stream is changed to the given value after that unconditionally.
955 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
957 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
960 EXPORT_SYMBOL(snd_pcm_stop);
963 * snd_pcm_drain_done
964 * @substream: the PCM substream
966 * Stop the DMA only when the given stream is playback.
967 * The state is changed to SETUP.
968 * Unlike snd_pcm_stop(), this affects only the given stream.
970 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
972 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
973 SNDRV_PCM_STATE_SETUP);
977 * pause callbacks
979 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
981 struct snd_pcm_runtime *runtime = substream->runtime;
982 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
983 return -ENOSYS;
984 if (push) {
985 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
986 return -EBADFD;
987 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
988 return -EBADFD;
989 runtime->trigger_master = substream;
990 return 0;
993 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
995 if (substream->runtime->trigger_master != substream)
996 return 0;
997 return substream->ops->trigger(substream,
998 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
999 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1002 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1004 if (substream->runtime->trigger_master == substream)
1005 substream->ops->trigger(substream,
1006 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1007 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1010 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1012 struct snd_pcm_runtime *runtime = substream->runtime;
1013 snd_pcm_trigger_tstamp(substream);
1014 if (push) {
1015 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1016 if (substream->timer)
1017 snd_timer_notify(substream->timer,
1018 SNDRV_TIMER_EVENT_MPAUSE,
1019 &runtime->trigger_tstamp);
1020 snd_pcm_tick_set(substream, 0);
1021 wake_up(&runtime->sleep);
1022 } else {
1023 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1024 if (runtime->sleep_min)
1025 snd_pcm_tick_prepare(substream);
1026 if (substream->timer)
1027 snd_timer_notify(substream->timer,
1028 SNDRV_TIMER_EVENT_MCONTINUE,
1029 &runtime->trigger_tstamp);
1033 static struct action_ops snd_pcm_action_pause = {
1034 .pre_action = snd_pcm_pre_pause,
1035 .do_action = snd_pcm_do_pause,
1036 .undo_action = snd_pcm_undo_pause,
1037 .post_action = snd_pcm_post_pause
1041 * Push/release the pause for all linked streams.
1043 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1045 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1048 #ifdef CONFIG_PM
1049 /* suspend */
1051 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1053 struct snd_pcm_runtime *runtime = substream->runtime;
1054 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1055 return -EBUSY;
1056 runtime->trigger_master = substream;
1057 return 0;
1060 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1062 struct snd_pcm_runtime *runtime = substream->runtime;
1063 if (runtime->trigger_master != substream)
1064 return 0;
1065 if (! snd_pcm_running(substream))
1066 return 0;
1067 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1068 return 0; /* suspend unconditionally */
1071 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1073 struct snd_pcm_runtime *runtime = substream->runtime;
1074 snd_pcm_trigger_tstamp(substream);
1075 if (substream->timer)
1076 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1077 &runtime->trigger_tstamp);
1078 runtime->status->suspended_state = runtime->status->state;
1079 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1080 snd_pcm_tick_set(substream, 0);
1081 wake_up(&runtime->sleep);
1084 static struct action_ops snd_pcm_action_suspend = {
1085 .pre_action = snd_pcm_pre_suspend,
1086 .do_action = snd_pcm_do_suspend,
1087 .post_action = snd_pcm_post_suspend
1091 * snd_pcm_suspend
1092 * @substream: the PCM substream
1094 * Trigger SUSPEND to all linked streams.
1095 * After this call, all streams are changed to SUSPENDED state.
1097 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1099 int err;
1100 unsigned long flags;
1102 if (! substream)
1103 return 0;
1105 snd_pcm_stream_lock_irqsave(substream, flags);
1106 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1107 snd_pcm_stream_unlock_irqrestore(substream, flags);
1108 return err;
1111 EXPORT_SYMBOL(snd_pcm_suspend);
1114 * snd_pcm_suspend_all
1115 * @pcm: the PCM instance
1117 * Trigger SUSPEND to all substreams in the given pcm.
1118 * After this call, all streams are changed to SUSPENDED state.
1120 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1122 struct snd_pcm_substream *substream;
1123 int stream, err = 0;
1125 if (! pcm)
1126 return 0;
1128 for (stream = 0; stream < 2; stream++) {
1129 for (substream = pcm->streams[stream].substream;
1130 substream; substream = substream->next) {
1131 /* FIXME: the open/close code should lock this as well */
1132 if (substream->runtime == NULL)
1133 continue;
1134 err = snd_pcm_suspend(substream);
1135 if (err < 0 && err != -EBUSY)
1136 return err;
1139 return 0;
1142 EXPORT_SYMBOL(snd_pcm_suspend_all);
1144 /* resume */
1146 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1148 struct snd_pcm_runtime *runtime = substream->runtime;
1149 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1150 return -ENOSYS;
1151 runtime->trigger_master = substream;
1152 return 0;
1155 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1157 struct snd_pcm_runtime *runtime = substream->runtime;
1158 if (runtime->trigger_master != substream)
1159 return 0;
1160 /* DMA not running previously? */
1161 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1162 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1163 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1164 return 0;
1165 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1168 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1170 if (substream->runtime->trigger_master == substream &&
1171 snd_pcm_running(substream))
1172 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1175 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1177 struct snd_pcm_runtime *runtime = substream->runtime;
1178 snd_pcm_trigger_tstamp(substream);
1179 if (substream->timer)
1180 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1181 &runtime->trigger_tstamp);
1182 runtime->status->state = runtime->status->suspended_state;
1183 if (runtime->sleep_min)
1184 snd_pcm_tick_prepare(substream);
1187 static struct action_ops snd_pcm_action_resume = {
1188 .pre_action = snd_pcm_pre_resume,
1189 .do_action = snd_pcm_do_resume,
1190 .undo_action = snd_pcm_undo_resume,
1191 .post_action = snd_pcm_post_resume
1194 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1196 struct snd_card *card = substream->pcm->card;
1197 int res;
1199 snd_power_lock(card);
1200 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1201 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1202 snd_power_unlock(card);
1203 return res;
1206 #else
1208 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1210 return -ENOSYS;
1213 #endif /* CONFIG_PM */
1216 * xrun ioctl
1218 * Change the RUNNING stream(s) to XRUN state.
1220 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1222 struct snd_card *card = substream->pcm->card;
1223 struct snd_pcm_runtime *runtime = substream->runtime;
1224 int result;
1226 snd_power_lock(card);
1227 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1228 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1229 if (result < 0)
1230 goto _unlock;
1233 snd_pcm_stream_lock_irq(substream);
1234 switch (runtime->status->state) {
1235 case SNDRV_PCM_STATE_XRUN:
1236 result = 0; /* already there */
1237 break;
1238 case SNDRV_PCM_STATE_RUNNING:
1239 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1240 break;
1241 default:
1242 result = -EBADFD;
1244 snd_pcm_stream_unlock_irq(substream);
1245 _unlock:
1246 snd_power_unlock(card);
1247 return result;
1251 * reset ioctl
1253 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1255 struct snd_pcm_runtime *runtime = substream->runtime;
1256 switch (runtime->status->state) {
1257 case SNDRV_PCM_STATE_RUNNING:
1258 case SNDRV_PCM_STATE_PREPARED:
1259 case SNDRV_PCM_STATE_PAUSED:
1260 case SNDRV_PCM_STATE_SUSPENDED:
1261 return 0;
1262 default:
1263 return -EBADFD;
1267 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1269 struct snd_pcm_runtime *runtime = substream->runtime;
1270 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1271 if (err < 0)
1272 return err;
1273 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1274 runtime->hw_ptr_base = 0;
1275 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1276 runtime->status->hw_ptr % runtime->period_size;
1277 runtime->silence_start = runtime->status->hw_ptr;
1278 runtime->silence_filled = 0;
1279 return 0;
1282 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1284 struct snd_pcm_runtime *runtime = substream->runtime;
1285 runtime->control->appl_ptr = runtime->status->hw_ptr;
1286 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1287 runtime->silence_size > 0)
1288 snd_pcm_playback_silence(substream, ULONG_MAX);
1291 static struct action_ops snd_pcm_action_reset = {
1292 .pre_action = snd_pcm_pre_reset,
1293 .do_action = snd_pcm_do_reset,
1294 .post_action = snd_pcm_post_reset
1297 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1299 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1303 * prepare ioctl
1305 /* we use the second argument for updating f_flags */
1306 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1307 int f_flags)
1309 struct snd_pcm_runtime *runtime = substream->runtime;
1310 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1311 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1312 return -EBADFD;
1313 if (snd_pcm_running(substream))
1314 return -EBUSY;
1315 substream->f_flags = f_flags;
1316 return 0;
1319 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1321 int err;
1322 err = substream->ops->prepare(substream);
1323 if (err < 0)
1324 return err;
1325 return snd_pcm_do_reset(substream, 0);
1328 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1330 struct snd_pcm_runtime *runtime = substream->runtime;
1331 runtime->control->appl_ptr = runtime->status->hw_ptr;
1332 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1335 static struct action_ops snd_pcm_action_prepare = {
1336 .pre_action = snd_pcm_pre_prepare,
1337 .do_action = snd_pcm_do_prepare,
1338 .post_action = snd_pcm_post_prepare
1342 * snd_pcm_prepare
1343 * @substream: the PCM substream instance
1344 * @file: file to refer f_flags
1346 * Prepare the PCM substream to be triggerable.
1348 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1349 struct file *file)
1351 int res;
1352 struct snd_card *card = substream->pcm->card;
1353 int f_flags;
1355 if (file)
1356 f_flags = file->f_flags;
1357 else
1358 f_flags = substream->f_flags;
1360 snd_power_lock(card);
1361 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1362 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1363 substream, f_flags);
1364 snd_power_unlock(card);
1365 return res;
1369 * drain ioctl
1372 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1374 if (substream->f_flags & O_NONBLOCK)
1375 return -EAGAIN;
1376 substream->runtime->trigger_master = substream;
1377 return 0;
1380 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1382 struct snd_pcm_runtime *runtime = substream->runtime;
1383 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1384 switch (runtime->status->state) {
1385 case SNDRV_PCM_STATE_PREPARED:
1386 /* start playback stream if possible */
1387 if (! snd_pcm_playback_empty(substream)) {
1388 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1389 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1391 break;
1392 case SNDRV_PCM_STATE_RUNNING:
1393 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1394 break;
1395 default:
1396 break;
1398 } else {
1399 /* stop running stream */
1400 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1401 int state = snd_pcm_capture_avail(runtime) > 0 ?
1402 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1403 snd_pcm_do_stop(substream, state);
1404 snd_pcm_post_stop(substream, state);
1407 return 0;
1410 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1414 static struct action_ops snd_pcm_action_drain_init = {
1415 .pre_action = snd_pcm_pre_drain_init,
1416 .do_action = snd_pcm_do_drain_init,
1417 .post_action = snd_pcm_post_drain_init
1420 struct drain_rec {
1421 struct snd_pcm_substream *substream;
1422 wait_queue_t wait;
1423 snd_pcm_uframes_t stop_threshold;
1426 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1429 * Drain the stream(s).
1430 * When the substream is linked, sync until the draining of all playback streams
1431 * is finished.
1432 * After this call, all streams are supposed to be either SETUP or DRAINING
1433 * (capture only) state.
1435 static int snd_pcm_drain(struct snd_pcm_substream *substream)
1437 struct snd_card *card;
1438 struct snd_pcm_runtime *runtime;
1439 struct snd_pcm_substream *s;
1440 int result = 0;
1441 int i, num_drecs;
1442 struct drain_rec *drec, drec_tmp, *d;
1444 snd_assert(substream != NULL, return -ENXIO);
1445 card = substream->pcm->card;
1446 runtime = substream->runtime;
1448 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1449 return -EBADFD;
1451 snd_power_lock(card);
1452 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1453 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1454 if (result < 0) {
1455 snd_power_unlock(card);
1456 return result;
1460 /* allocate temporary record for drain sync */
1461 down_read(&snd_pcm_link_rwsem);
1462 if (snd_pcm_stream_linked(substream)) {
1463 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1464 if (! drec) {
1465 up_read(&snd_pcm_link_rwsem);
1466 snd_power_unlock(card);
1467 return -ENOMEM;
1469 } else
1470 drec = &drec_tmp;
1472 /* count only playback streams */
1473 num_drecs = 0;
1474 snd_pcm_group_for_each_entry(s, substream) {
1475 runtime = s->runtime;
1476 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1477 d = &drec[num_drecs++];
1478 d->substream = s;
1479 init_waitqueue_entry(&d->wait, current);
1480 add_wait_queue(&runtime->sleep, &d->wait);
1481 /* stop_threshold fixup to avoid endless loop when
1482 * stop_threshold > buffer_size
1484 d->stop_threshold = runtime->stop_threshold;
1485 if (runtime->stop_threshold > runtime->buffer_size)
1486 runtime->stop_threshold = runtime->buffer_size;
1489 up_read(&snd_pcm_link_rwsem);
1491 snd_pcm_stream_lock_irq(substream);
1492 /* resume pause */
1493 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1494 snd_pcm_pause(substream, 0);
1496 /* pre-start/stop - all running streams are changed to DRAINING state */
1497 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1498 if (result < 0) {
1499 snd_pcm_stream_unlock_irq(substream);
1500 goto _error;
1503 for (;;) {
1504 long tout;
1505 if (signal_pending(current)) {
1506 result = -ERESTARTSYS;
1507 break;
1509 /* all finished? */
1510 for (i = 0; i < num_drecs; i++) {
1511 runtime = drec[i].substream->runtime;
1512 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1513 break;
1515 if (i == num_drecs)
1516 break; /* yes, all drained */
1518 set_current_state(TASK_INTERRUPTIBLE);
1519 snd_pcm_stream_unlock_irq(substream);
1520 snd_power_unlock(card);
1521 tout = schedule_timeout(10 * HZ);
1522 snd_power_lock(card);
1523 snd_pcm_stream_lock_irq(substream);
1524 if (tout == 0) {
1525 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1526 result = -ESTRPIPE;
1527 else {
1528 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1529 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1530 result = -EIO;
1532 break;
1536 snd_pcm_stream_unlock_irq(substream);
1538 _error:
1539 for (i = 0; i < num_drecs; i++) {
1540 d = &drec[i];
1541 runtime = d->substream->runtime;
1542 remove_wait_queue(&runtime->sleep, &d->wait);
1543 runtime->stop_threshold = d->stop_threshold;
1546 if (drec != &drec_tmp)
1547 kfree(drec);
1548 snd_power_unlock(card);
1550 return result;
1554 * drop ioctl
1556 * Immediately put all linked substreams into SETUP state.
1558 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1560 struct snd_pcm_runtime *runtime;
1561 struct snd_card *card;
1562 int result = 0;
1564 snd_assert(substream != NULL, return -ENXIO);
1565 runtime = substream->runtime;
1566 card = substream->pcm->card;
1568 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1569 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1570 return -EBADFD;
1572 snd_power_lock(card);
1573 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1574 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1575 if (result < 0)
1576 goto _unlock;
1579 snd_pcm_stream_lock_irq(substream);
1580 /* resume pause */
1581 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1582 snd_pcm_pause(substream, 0);
1584 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1585 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1586 snd_pcm_stream_unlock_irq(substream);
1587 _unlock:
1588 snd_power_unlock(card);
1589 return result;
1593 /* WARNING: Don't forget to fput back the file */
1594 static struct file *snd_pcm_file_fd(int fd)
1596 struct file *file;
1597 struct inode *inode;
1598 unsigned int minor;
1600 file = fget(fd);
1601 if (!file)
1602 return NULL;
1603 inode = file->f_path.dentry->d_inode;
1604 if (!S_ISCHR(inode->i_mode) ||
1605 imajor(inode) != snd_major) {
1606 fput(file);
1607 return NULL;
1609 minor = iminor(inode);
1610 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1611 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1612 fput(file);
1613 return NULL;
1615 return file;
1619 * PCM link handling
1621 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1623 int res = 0;
1624 struct file *file;
1625 struct snd_pcm_file *pcm_file;
1626 struct snd_pcm_substream *substream1;
1628 file = snd_pcm_file_fd(fd);
1629 if (!file)
1630 return -EBADFD;
1631 pcm_file = file->private_data;
1632 substream1 = pcm_file->substream;
1633 down_write(&snd_pcm_link_rwsem);
1634 write_lock_irq(&snd_pcm_link_rwlock);
1635 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1636 substream->runtime->status->state != substream1->runtime->status->state) {
1637 res = -EBADFD;
1638 goto _end;
1640 if (snd_pcm_stream_linked(substream1)) {
1641 res = -EALREADY;
1642 goto _end;
1644 if (!snd_pcm_stream_linked(substream)) {
1645 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1646 if (substream->group == NULL) {
1647 res = -ENOMEM;
1648 goto _end;
1650 spin_lock_init(&substream->group->lock);
1651 INIT_LIST_HEAD(&substream->group->substreams);
1652 list_add_tail(&substream->link_list, &substream->group->substreams);
1653 substream->group->count = 1;
1655 list_add_tail(&substream1->link_list, &substream->group->substreams);
1656 substream->group->count++;
1657 substream1->group = substream->group;
1658 _end:
1659 write_unlock_irq(&snd_pcm_link_rwlock);
1660 up_write(&snd_pcm_link_rwsem);
1661 fput(file);
1662 return res;
1665 static void relink_to_local(struct snd_pcm_substream *substream)
1667 substream->group = &substream->self_group;
1668 INIT_LIST_HEAD(&substream->self_group.substreams);
1669 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1672 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1674 struct snd_pcm_substream *s;
1675 int res = 0;
1677 down_write(&snd_pcm_link_rwsem);
1678 write_lock_irq(&snd_pcm_link_rwlock);
1679 if (!snd_pcm_stream_linked(substream)) {
1680 res = -EALREADY;
1681 goto _end;
1683 list_del(&substream->link_list);
1684 substream->group->count--;
1685 if (substream->group->count == 1) { /* detach the last stream, too */
1686 snd_pcm_group_for_each_entry(s, substream) {
1687 relink_to_local(s);
1688 break;
1690 kfree(substream->group);
1692 relink_to_local(substream);
1693 _end:
1694 write_unlock_irq(&snd_pcm_link_rwlock);
1695 up_write(&snd_pcm_link_rwsem);
1696 return res;
1700 * hw configurator
1702 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1703 struct snd_pcm_hw_rule *rule)
1705 struct snd_interval t;
1706 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1707 hw_param_interval_c(params, rule->deps[1]), &t);
1708 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1711 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1712 struct snd_pcm_hw_rule *rule)
1714 struct snd_interval t;
1715 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1716 hw_param_interval_c(params, rule->deps[1]), &t);
1717 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1720 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1721 struct snd_pcm_hw_rule *rule)
1723 struct snd_interval t;
1724 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1725 hw_param_interval_c(params, rule->deps[1]),
1726 (unsigned long) rule->private, &t);
1727 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1730 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1731 struct snd_pcm_hw_rule *rule)
1733 struct snd_interval t;
1734 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1735 (unsigned long) rule->private,
1736 hw_param_interval_c(params, rule->deps[1]), &t);
1737 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1740 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
1743 unsigned int k;
1744 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1745 struct snd_mask m;
1746 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1747 snd_mask_any(&m);
1748 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1749 int bits;
1750 if (! snd_mask_test(mask, k))
1751 continue;
1752 bits = snd_pcm_format_physical_width(k);
1753 if (bits <= 0)
1754 continue; /* ignore invalid formats */
1755 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1756 snd_mask_reset(&m, k);
1758 return snd_mask_refine(mask, &m);
1761 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1762 struct snd_pcm_hw_rule *rule)
1764 struct snd_interval t;
1765 unsigned int k;
1766 t.min = UINT_MAX;
1767 t.max = 0;
1768 t.openmin = 0;
1769 t.openmax = 0;
1770 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1771 int bits;
1772 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1773 continue;
1774 bits = snd_pcm_format_physical_width(k);
1775 if (bits <= 0)
1776 continue; /* ignore invalid formats */
1777 if (t.min > (unsigned)bits)
1778 t.min = bits;
1779 if (t.max < (unsigned)bits)
1780 t.max = bits;
1782 t.integer = 1;
1783 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1786 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1787 #error "Change this table"
1788 #endif
1790 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1791 48000, 64000, 88200, 96000, 176400, 192000 };
1793 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1794 .count = ARRAY_SIZE(rates),
1795 .list = rates,
1798 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1799 struct snd_pcm_hw_rule *rule)
1801 struct snd_pcm_hardware *hw = rule->private;
1802 return snd_interval_list(hw_param_interval(params, rule->var),
1803 snd_pcm_known_rates.count,
1804 snd_pcm_known_rates.list, hw->rates);
1807 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1808 struct snd_pcm_hw_rule *rule)
1810 struct snd_interval t;
1811 struct snd_pcm_substream *substream = rule->private;
1812 t.min = 0;
1813 t.max = substream->buffer_bytes_max;
1814 t.openmin = 0;
1815 t.openmax = 0;
1816 t.integer = 1;
1817 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1820 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1822 struct snd_pcm_runtime *runtime = substream->runtime;
1823 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1824 int k, err;
1826 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1827 snd_mask_any(constrs_mask(constrs, k));
1830 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1831 snd_interval_any(constrs_interval(constrs, k));
1834 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1835 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1836 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1837 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1838 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1841 snd_pcm_hw_rule_format, NULL,
1842 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1843 if (err < 0)
1844 return err;
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1846 snd_pcm_hw_rule_sample_bits, NULL,
1847 SNDRV_PCM_HW_PARAM_FORMAT,
1848 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1852 snd_pcm_hw_rule_div, NULL,
1853 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1857 snd_pcm_hw_rule_mul, NULL,
1858 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1872 snd_pcm_hw_rule_div, NULL,
1873 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1882 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1887 snd_pcm_hw_rule_div, NULL,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1892 snd_pcm_hw_rule_div, NULL,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1897 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1898 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1902 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1903 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1904 if (err < 0)
1905 return err;
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1907 snd_pcm_hw_rule_mul, NULL,
1908 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1909 if (err < 0)
1910 return err;
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1912 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1913 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1914 if (err < 0)
1915 return err;
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1917 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1918 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1919 if (err < 0)
1920 return err;
1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1922 snd_pcm_hw_rule_muldivk, (void*) 8,
1923 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1924 if (err < 0)
1925 return err;
1926 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1927 snd_pcm_hw_rule_muldivk, (void*) 8,
1928 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1929 if (err < 0)
1930 return err;
1931 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1932 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1933 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1934 if (err < 0)
1935 return err;
1936 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1937 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1938 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1939 if (err < 0)
1940 return err;
1941 return 0;
1944 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1946 struct snd_pcm_runtime *runtime = substream->runtime;
1947 struct snd_pcm_hardware *hw = &runtime->hw;
1948 int err;
1949 unsigned int mask = 0;
1951 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1952 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1953 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1954 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1955 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1956 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1957 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1958 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1959 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1960 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1961 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1963 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1964 snd_assert(err >= 0, return -EINVAL);
1966 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1967 snd_assert(err >= 0, return -EINVAL);
1969 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1970 snd_assert(err >= 0, return -EINVAL);
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1973 hw->channels_min, hw->channels_max);
1974 snd_assert(err >= 0, return -EINVAL);
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1977 hw->rate_min, hw->rate_max);
1978 snd_assert(err >= 0, return -EINVAL);
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1981 hw->period_bytes_min, hw->period_bytes_max);
1982 snd_assert(err >= 0, return -EINVAL);
1984 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1985 hw->periods_min, hw->periods_max);
1986 snd_assert(err >= 0, return -EINVAL);
1988 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1989 hw->period_bytes_min, hw->buffer_bytes_max);
1990 snd_assert(err >= 0, return -EINVAL);
1992 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1993 snd_pcm_hw_rule_buffer_bytes_max, substream,
1994 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1995 if (err < 0)
1996 return err;
1998 /* FIXME: remove */
1999 if (runtime->dma_bytes) {
2000 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2001 snd_assert(err >= 0, return -EINVAL);
2004 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2005 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2006 snd_pcm_hw_rule_rate, hw,
2007 SNDRV_PCM_HW_PARAM_RATE, -1);
2008 if (err < 0)
2009 return err;
2012 /* FIXME: this belong to lowlevel */
2013 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2014 1000000 / HZ, 1000000 / HZ);
2015 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2017 return 0;
2020 static void pcm_release_private(struct snd_pcm_substream *substream)
2022 snd_pcm_unlink(substream);
2025 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2027 substream->ref_count--;
2028 if (substream->ref_count > 0)
2029 return;
2031 snd_pcm_drop(substream);
2032 if (substream->hw_opened) {
2033 if (substream->ops->hw_free != NULL)
2034 substream->ops->hw_free(substream);
2035 substream->ops->close(substream);
2036 substream->hw_opened = 0;
2038 if (substream->pcm_release) {
2039 substream->pcm_release(substream);
2040 substream->pcm_release = NULL;
2042 snd_pcm_detach_substream(substream);
2045 EXPORT_SYMBOL(snd_pcm_release_substream);
2047 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2048 struct file *file,
2049 struct snd_pcm_substream **rsubstream)
2051 struct snd_pcm_substream *substream;
2052 int err;
2054 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2055 if (err < 0)
2056 return err;
2057 if (substream->ref_count > 1) {
2058 *rsubstream = substream;
2059 return 0;
2062 err = snd_pcm_hw_constraints_init(substream);
2063 if (err < 0) {
2064 snd_printd("snd_pcm_hw_constraints_init failed\n");
2065 goto error;
2068 if ((err = substream->ops->open(substream)) < 0)
2069 goto error;
2071 substream->hw_opened = 1;
2073 err = snd_pcm_hw_constraints_complete(substream);
2074 if (err < 0) {
2075 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2076 goto error;
2079 *rsubstream = substream;
2080 return 0;
2082 error:
2083 snd_pcm_release_substream(substream);
2084 return err;
2087 EXPORT_SYMBOL(snd_pcm_open_substream);
2089 static int snd_pcm_open_file(struct file *file,
2090 struct snd_pcm *pcm,
2091 int stream,
2092 struct snd_pcm_file **rpcm_file)
2094 struct snd_pcm_file *pcm_file;
2095 struct snd_pcm_substream *substream;
2096 struct snd_pcm_str *str;
2097 int err;
2099 snd_assert(rpcm_file != NULL, return -EINVAL);
2100 *rpcm_file = NULL;
2102 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2103 if (err < 0)
2104 return err;
2106 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2107 if (pcm_file == NULL) {
2108 snd_pcm_release_substream(substream);
2109 return -ENOMEM;
2111 pcm_file->substream = substream;
2112 if (substream->ref_count == 1) {
2113 str = substream->pstr;
2114 substream->file = pcm_file;
2115 substream->pcm_release = pcm_release_private;
2117 file->private_data = pcm_file;
2118 *rpcm_file = pcm_file;
2119 return 0;
2122 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2124 struct snd_pcm *pcm;
2126 pcm = snd_lookup_minor_data(iminor(inode),
2127 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2128 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2131 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2133 struct snd_pcm *pcm;
2135 pcm = snd_lookup_minor_data(iminor(inode),
2136 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2137 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2140 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2142 int err;
2143 struct snd_pcm_file *pcm_file;
2144 wait_queue_t wait;
2146 if (pcm == NULL) {
2147 err = -ENODEV;
2148 goto __error1;
2150 err = snd_card_file_add(pcm->card, file);
2151 if (err < 0)
2152 goto __error1;
2153 if (!try_module_get(pcm->card->module)) {
2154 err = -EFAULT;
2155 goto __error2;
2157 init_waitqueue_entry(&wait, current);
2158 add_wait_queue(&pcm->open_wait, &wait);
2159 mutex_lock(&pcm->open_mutex);
2160 while (1) {
2161 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2162 if (err >= 0)
2163 break;
2164 if (err == -EAGAIN) {
2165 if (file->f_flags & O_NONBLOCK) {
2166 err = -EBUSY;
2167 break;
2169 } else
2170 break;
2171 set_current_state(TASK_INTERRUPTIBLE);
2172 mutex_unlock(&pcm->open_mutex);
2173 schedule();
2174 mutex_lock(&pcm->open_mutex);
2175 if (signal_pending(current)) {
2176 err = -ERESTARTSYS;
2177 break;
2180 remove_wait_queue(&pcm->open_wait, &wait);
2181 mutex_unlock(&pcm->open_mutex);
2182 if (err < 0)
2183 goto __error;
2184 return err;
2186 __error:
2187 module_put(pcm->card->module);
2188 __error2:
2189 snd_card_file_remove(pcm->card, file);
2190 __error1:
2191 return err;
2194 static int snd_pcm_release(struct inode *inode, struct file *file)
2196 struct snd_pcm *pcm;
2197 struct snd_pcm_substream *substream;
2198 struct snd_pcm_file *pcm_file;
2200 pcm_file = file->private_data;
2201 substream = pcm_file->substream;
2202 snd_assert(substream != NULL, return -ENXIO);
2203 pcm = substream->pcm;
2204 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2205 mutex_lock(&pcm->open_mutex);
2206 snd_pcm_release_substream(substream);
2207 kfree(pcm_file);
2208 mutex_unlock(&pcm->open_mutex);
2209 wake_up(&pcm->open_wait);
2210 module_put(pcm->card->module);
2211 snd_card_file_remove(pcm->card, file);
2212 return 0;
2215 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2216 snd_pcm_uframes_t frames)
2218 struct snd_pcm_runtime *runtime = substream->runtime;
2219 snd_pcm_sframes_t appl_ptr;
2220 snd_pcm_sframes_t ret;
2221 snd_pcm_sframes_t hw_avail;
2223 if (frames == 0)
2224 return 0;
2226 snd_pcm_stream_lock_irq(substream);
2227 switch (runtime->status->state) {
2228 case SNDRV_PCM_STATE_PREPARED:
2229 break;
2230 case SNDRV_PCM_STATE_DRAINING:
2231 case SNDRV_PCM_STATE_RUNNING:
2232 if (snd_pcm_update_hw_ptr(substream) >= 0)
2233 break;
2234 /* Fall through */
2235 case SNDRV_PCM_STATE_XRUN:
2236 ret = -EPIPE;
2237 goto __end;
2238 default:
2239 ret = -EBADFD;
2240 goto __end;
2243 hw_avail = snd_pcm_playback_hw_avail(runtime);
2244 if (hw_avail <= 0) {
2245 ret = 0;
2246 goto __end;
2248 if (frames > (snd_pcm_uframes_t)hw_avail)
2249 frames = hw_avail;
2250 else
2251 frames -= frames % runtime->xfer_align;
2252 appl_ptr = runtime->control->appl_ptr - frames;
2253 if (appl_ptr < 0)
2254 appl_ptr += runtime->boundary;
2255 runtime->control->appl_ptr = appl_ptr;
2256 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2257 runtime->sleep_min)
2258 snd_pcm_tick_prepare(substream);
2259 ret = frames;
2260 __end:
2261 snd_pcm_stream_unlock_irq(substream);
2262 return ret;
2265 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2266 snd_pcm_uframes_t frames)
2268 struct snd_pcm_runtime *runtime = substream->runtime;
2269 snd_pcm_sframes_t appl_ptr;
2270 snd_pcm_sframes_t ret;
2271 snd_pcm_sframes_t hw_avail;
2273 if (frames == 0)
2274 return 0;
2276 snd_pcm_stream_lock_irq(substream);
2277 switch (runtime->status->state) {
2278 case SNDRV_PCM_STATE_PREPARED:
2279 case SNDRV_PCM_STATE_DRAINING:
2280 break;
2281 case SNDRV_PCM_STATE_RUNNING:
2282 if (snd_pcm_update_hw_ptr(substream) >= 0)
2283 break;
2284 /* Fall through */
2285 case SNDRV_PCM_STATE_XRUN:
2286 ret = -EPIPE;
2287 goto __end;
2288 default:
2289 ret = -EBADFD;
2290 goto __end;
2293 hw_avail = snd_pcm_capture_hw_avail(runtime);
2294 if (hw_avail <= 0) {
2295 ret = 0;
2296 goto __end;
2298 if (frames > (snd_pcm_uframes_t)hw_avail)
2299 frames = hw_avail;
2300 else
2301 frames -= frames % runtime->xfer_align;
2302 appl_ptr = runtime->control->appl_ptr - frames;
2303 if (appl_ptr < 0)
2304 appl_ptr += runtime->boundary;
2305 runtime->control->appl_ptr = appl_ptr;
2306 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2307 runtime->sleep_min)
2308 snd_pcm_tick_prepare(substream);
2309 ret = frames;
2310 __end:
2311 snd_pcm_stream_unlock_irq(substream);
2312 return ret;
2315 static snd_pcm_sframes_t snd_pcm_playback_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_PAUSED:
2330 break;
2331 case SNDRV_PCM_STATE_DRAINING:
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 default:
2340 ret = -EBADFD;
2341 goto __end;
2344 avail = snd_pcm_playback_avail(runtime);
2345 if (avail <= 0) {
2346 ret = 0;
2347 goto __end;
2349 if (frames > (snd_pcm_uframes_t)avail)
2350 frames = avail;
2351 else
2352 frames -= frames % runtime->xfer_align;
2353 appl_ptr = runtime->control->appl_ptr + frames;
2354 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2355 appl_ptr -= runtime->boundary;
2356 runtime->control->appl_ptr = appl_ptr;
2357 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2358 runtime->sleep_min)
2359 snd_pcm_tick_prepare(substream);
2360 ret = frames;
2361 __end:
2362 snd_pcm_stream_unlock_irq(substream);
2363 return ret;
2366 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2367 snd_pcm_uframes_t frames)
2369 struct snd_pcm_runtime *runtime = substream->runtime;
2370 snd_pcm_sframes_t appl_ptr;
2371 snd_pcm_sframes_t ret;
2372 snd_pcm_sframes_t avail;
2374 if (frames == 0)
2375 return 0;
2377 snd_pcm_stream_lock_irq(substream);
2378 switch (runtime->status->state) {
2379 case SNDRV_PCM_STATE_PREPARED:
2380 case SNDRV_PCM_STATE_DRAINING:
2381 case SNDRV_PCM_STATE_PAUSED:
2382 break;
2383 case SNDRV_PCM_STATE_RUNNING:
2384 if (snd_pcm_update_hw_ptr(substream) >= 0)
2385 break;
2386 /* Fall through */
2387 case SNDRV_PCM_STATE_XRUN:
2388 ret = -EPIPE;
2389 goto __end;
2390 default:
2391 ret = -EBADFD;
2392 goto __end;
2395 avail = snd_pcm_capture_avail(runtime);
2396 if (avail <= 0) {
2397 ret = 0;
2398 goto __end;
2400 if (frames > (snd_pcm_uframes_t)avail)
2401 frames = avail;
2402 else
2403 frames -= frames % runtime->xfer_align;
2404 appl_ptr = runtime->control->appl_ptr + frames;
2405 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2406 appl_ptr -= runtime->boundary;
2407 runtime->control->appl_ptr = appl_ptr;
2408 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2409 runtime->sleep_min)
2410 snd_pcm_tick_prepare(substream);
2411 ret = frames;
2412 __end:
2413 snd_pcm_stream_unlock_irq(substream);
2414 return ret;
2417 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2419 struct snd_pcm_runtime *runtime = substream->runtime;
2420 int err;
2422 snd_pcm_stream_lock_irq(substream);
2423 switch (runtime->status->state) {
2424 case SNDRV_PCM_STATE_DRAINING:
2425 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2426 goto __badfd;
2427 case SNDRV_PCM_STATE_RUNNING:
2428 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2429 break;
2430 /* Fall through */
2431 case SNDRV_PCM_STATE_PREPARED:
2432 case SNDRV_PCM_STATE_SUSPENDED:
2433 err = 0;
2434 break;
2435 case SNDRV_PCM_STATE_XRUN:
2436 err = -EPIPE;
2437 break;
2438 default:
2439 __badfd:
2440 err = -EBADFD;
2441 break;
2443 snd_pcm_stream_unlock_irq(substream);
2444 return err;
2447 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2448 snd_pcm_sframes_t __user *res)
2450 struct snd_pcm_runtime *runtime = substream->runtime;
2451 int err;
2452 snd_pcm_sframes_t n = 0;
2454 snd_pcm_stream_lock_irq(substream);
2455 switch (runtime->status->state) {
2456 case SNDRV_PCM_STATE_DRAINING:
2457 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2458 goto __badfd;
2459 case SNDRV_PCM_STATE_RUNNING:
2460 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2461 break;
2462 /* Fall through */
2463 case SNDRV_PCM_STATE_PREPARED:
2464 case SNDRV_PCM_STATE_SUSPENDED:
2465 err = 0;
2466 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2467 n = snd_pcm_playback_hw_avail(runtime);
2468 else
2469 n = snd_pcm_capture_avail(runtime);
2470 break;
2471 case SNDRV_PCM_STATE_XRUN:
2472 err = -EPIPE;
2473 break;
2474 default:
2475 __badfd:
2476 err = -EBADFD;
2477 break;
2479 snd_pcm_stream_unlock_irq(substream);
2480 if (!err)
2481 if (put_user(n, res))
2482 err = -EFAULT;
2483 return err;
2486 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2487 struct snd_pcm_sync_ptr __user *_sync_ptr)
2489 struct snd_pcm_runtime *runtime = substream->runtime;
2490 struct snd_pcm_sync_ptr sync_ptr;
2491 volatile struct snd_pcm_mmap_status *status;
2492 volatile struct snd_pcm_mmap_control *control;
2493 int err;
2495 memset(&sync_ptr, 0, sizeof(sync_ptr));
2496 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2497 return -EFAULT;
2498 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2499 return -EFAULT;
2500 status = runtime->status;
2501 control = runtime->control;
2502 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2503 err = snd_pcm_hwsync(substream);
2504 if (err < 0)
2505 return err;
2507 snd_pcm_stream_lock_irq(substream);
2508 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2509 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2510 else
2511 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2512 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2513 control->avail_min = sync_ptr.c.control.avail_min;
2514 else
2515 sync_ptr.c.control.avail_min = control->avail_min;
2516 sync_ptr.s.status.state = status->state;
2517 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2518 sync_ptr.s.status.tstamp = status->tstamp;
2519 sync_ptr.s.status.suspended_state = status->suspended_state;
2520 snd_pcm_stream_unlock_irq(substream);
2521 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2522 return -EFAULT;
2523 return 0;
2526 static int snd_pcm_common_ioctl1(struct file *file,
2527 struct snd_pcm_substream *substream,
2528 unsigned int cmd, void __user *arg)
2530 snd_assert(substream != NULL, return -ENXIO);
2532 switch (cmd) {
2533 case SNDRV_PCM_IOCTL_PVERSION:
2534 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2535 case SNDRV_PCM_IOCTL_INFO:
2536 return snd_pcm_info_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2538 return 0;
2539 case SNDRV_PCM_IOCTL_HW_REFINE:
2540 return snd_pcm_hw_refine_user(substream, arg);
2541 case SNDRV_PCM_IOCTL_HW_PARAMS:
2542 return snd_pcm_hw_params_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_HW_FREE:
2544 return snd_pcm_hw_free(substream);
2545 case SNDRV_PCM_IOCTL_SW_PARAMS:
2546 return snd_pcm_sw_params_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_STATUS:
2548 return snd_pcm_status_user(substream, arg);
2549 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2550 return snd_pcm_channel_info_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_PREPARE:
2552 return snd_pcm_prepare(substream, file);
2553 case SNDRV_PCM_IOCTL_RESET:
2554 return snd_pcm_reset(substream);
2555 case SNDRV_PCM_IOCTL_START:
2556 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2557 case SNDRV_PCM_IOCTL_LINK:
2558 return snd_pcm_link(substream, (int)(unsigned long) arg);
2559 case SNDRV_PCM_IOCTL_UNLINK:
2560 return snd_pcm_unlink(substream);
2561 case SNDRV_PCM_IOCTL_RESUME:
2562 return snd_pcm_resume(substream);
2563 case SNDRV_PCM_IOCTL_XRUN:
2564 return snd_pcm_xrun(substream);
2565 case SNDRV_PCM_IOCTL_HWSYNC:
2566 return snd_pcm_hwsync(substream);
2567 case SNDRV_PCM_IOCTL_DELAY:
2568 return snd_pcm_delay(substream, arg);
2569 case SNDRV_PCM_IOCTL_SYNC_PTR:
2570 return snd_pcm_sync_ptr(substream, arg);
2571 #ifdef CONFIG_SND_SUPPORT_OLD_API
2572 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2573 return snd_pcm_hw_refine_old_user(substream, arg);
2574 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2575 return snd_pcm_hw_params_old_user(substream, arg);
2576 #endif
2577 case SNDRV_PCM_IOCTL_DRAIN:
2578 return snd_pcm_drain(substream);
2579 case SNDRV_PCM_IOCTL_DROP:
2580 return snd_pcm_drop(substream);
2581 case SNDRV_PCM_IOCTL_PAUSE:
2583 int res;
2584 snd_pcm_stream_lock_irq(substream);
2585 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2586 snd_pcm_stream_unlock_irq(substream);
2587 return res;
2590 snd_printd("unknown ioctl = 0x%x\n", cmd);
2591 return -ENOTTY;
2594 static int snd_pcm_playback_ioctl1(struct file *file,
2595 struct snd_pcm_substream *substream,
2596 unsigned int cmd, void __user *arg)
2598 snd_assert(substream != NULL, return -ENXIO);
2599 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2600 switch (cmd) {
2601 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2603 struct snd_xferi xferi;
2604 struct snd_xferi __user *_xferi = arg;
2605 struct snd_pcm_runtime *runtime = substream->runtime;
2606 snd_pcm_sframes_t result;
2607 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2608 return -EBADFD;
2609 if (put_user(0, &_xferi->result))
2610 return -EFAULT;
2611 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2612 return -EFAULT;
2613 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2614 __put_user(result, &_xferi->result);
2615 return result < 0 ? result : 0;
2617 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2619 struct snd_xfern xfern;
2620 struct snd_xfern __user *_xfern = arg;
2621 struct snd_pcm_runtime *runtime = substream->runtime;
2622 void __user **bufs;
2623 snd_pcm_sframes_t result;
2624 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2625 return -EBADFD;
2626 if (runtime->channels > 128)
2627 return -EINVAL;
2628 if (put_user(0, &_xfern->result))
2629 return -EFAULT;
2630 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2631 return -EFAULT;
2632 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2633 if (bufs == NULL)
2634 return -ENOMEM;
2635 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2636 kfree(bufs);
2637 return -EFAULT;
2639 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2640 kfree(bufs);
2641 __put_user(result, &_xfern->result);
2642 return result < 0 ? result : 0;
2644 case SNDRV_PCM_IOCTL_REWIND:
2646 snd_pcm_uframes_t frames;
2647 snd_pcm_uframes_t __user *_frames = arg;
2648 snd_pcm_sframes_t result;
2649 if (get_user(frames, _frames))
2650 return -EFAULT;
2651 if (put_user(0, _frames))
2652 return -EFAULT;
2653 result = snd_pcm_playback_rewind(substream, frames);
2654 __put_user(result, _frames);
2655 return result < 0 ? result : 0;
2657 case SNDRV_PCM_IOCTL_FORWARD:
2659 snd_pcm_uframes_t frames;
2660 snd_pcm_uframes_t __user *_frames = arg;
2661 snd_pcm_sframes_t result;
2662 if (get_user(frames, _frames))
2663 return -EFAULT;
2664 if (put_user(0, _frames))
2665 return -EFAULT;
2666 result = snd_pcm_playback_forward(substream, frames);
2667 __put_user(result, _frames);
2668 return result < 0 ? result : 0;
2671 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2674 static int snd_pcm_capture_ioctl1(struct file *file,
2675 struct snd_pcm_substream *substream,
2676 unsigned int cmd, void __user *arg)
2678 snd_assert(substream != NULL, return -ENXIO);
2679 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2680 switch (cmd) {
2681 case SNDRV_PCM_IOCTL_READI_FRAMES:
2683 struct snd_xferi xferi;
2684 struct snd_xferi __user *_xferi = arg;
2685 struct snd_pcm_runtime *runtime = substream->runtime;
2686 snd_pcm_sframes_t result;
2687 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2688 return -EBADFD;
2689 if (put_user(0, &_xferi->result))
2690 return -EFAULT;
2691 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2692 return -EFAULT;
2693 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2694 __put_user(result, &_xferi->result);
2695 return result < 0 ? result : 0;
2697 case SNDRV_PCM_IOCTL_READN_FRAMES:
2699 struct snd_xfern xfern;
2700 struct snd_xfern __user *_xfern = arg;
2701 struct snd_pcm_runtime *runtime = substream->runtime;
2702 void *bufs;
2703 snd_pcm_sframes_t result;
2704 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2705 return -EBADFD;
2706 if (runtime->channels > 128)
2707 return -EINVAL;
2708 if (put_user(0, &_xfern->result))
2709 return -EFAULT;
2710 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2711 return -EFAULT;
2712 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2713 if (bufs == NULL)
2714 return -ENOMEM;
2715 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2716 kfree(bufs);
2717 return -EFAULT;
2719 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2720 kfree(bufs);
2721 __put_user(result, &_xfern->result);
2722 return result < 0 ? result : 0;
2724 case SNDRV_PCM_IOCTL_REWIND:
2726 snd_pcm_uframes_t frames;
2727 snd_pcm_uframes_t __user *_frames = arg;
2728 snd_pcm_sframes_t result;
2729 if (get_user(frames, _frames))
2730 return -EFAULT;
2731 if (put_user(0, _frames))
2732 return -EFAULT;
2733 result = snd_pcm_capture_rewind(substream, frames);
2734 __put_user(result, _frames);
2735 return result < 0 ? result : 0;
2737 case SNDRV_PCM_IOCTL_FORWARD:
2739 snd_pcm_uframes_t frames;
2740 snd_pcm_uframes_t __user *_frames = arg;
2741 snd_pcm_sframes_t result;
2742 if (get_user(frames, _frames))
2743 return -EFAULT;
2744 if (put_user(0, _frames))
2745 return -EFAULT;
2746 result = snd_pcm_capture_forward(substream, frames);
2747 __put_user(result, _frames);
2748 return result < 0 ? result : 0;
2751 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2754 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2755 unsigned long arg)
2757 struct snd_pcm_file *pcm_file;
2759 pcm_file = file->private_data;
2761 if (((cmd >> 8) & 0xff) != 'A')
2762 return -ENOTTY;
2764 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2765 (void __user *)arg);
2768 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2769 unsigned long arg)
2771 struct snd_pcm_file *pcm_file;
2773 pcm_file = file->private_data;
2775 if (((cmd >> 8) & 0xff) != 'A')
2776 return -ENOTTY;
2778 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2779 (void __user *)arg);
2782 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2783 unsigned int cmd, void *arg)
2785 mm_segment_t fs;
2786 int result;
2788 fs = snd_enter_user();
2789 switch (substream->stream) {
2790 case SNDRV_PCM_STREAM_PLAYBACK:
2791 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2792 (void __user *)arg);
2793 break;
2794 case SNDRV_PCM_STREAM_CAPTURE:
2795 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2796 (void __user *)arg);
2797 break;
2798 default:
2799 result = -EINVAL;
2800 break;
2802 snd_leave_user(fs);
2803 return result;
2806 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2808 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2809 loff_t * offset)
2811 struct snd_pcm_file *pcm_file;
2812 struct snd_pcm_substream *substream;
2813 struct snd_pcm_runtime *runtime;
2814 snd_pcm_sframes_t result;
2816 pcm_file = file->private_data;
2817 substream = pcm_file->substream;
2818 snd_assert(substream != NULL, return -ENXIO);
2819 runtime = substream->runtime;
2820 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2821 return -EBADFD;
2822 if (!frame_aligned(runtime, count))
2823 return -EINVAL;
2824 count = bytes_to_frames(runtime, count);
2825 result = snd_pcm_lib_read(substream, buf, count);
2826 if (result > 0)
2827 result = frames_to_bytes(runtime, result);
2828 return result;
2831 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2832 size_t count, loff_t * offset)
2834 struct snd_pcm_file *pcm_file;
2835 struct snd_pcm_substream *substream;
2836 struct snd_pcm_runtime *runtime;
2837 snd_pcm_sframes_t result;
2839 pcm_file = file->private_data;
2840 substream = pcm_file->substream;
2841 snd_assert(substream != NULL, result = -ENXIO; goto end);
2842 runtime = substream->runtime;
2843 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2844 result = -EBADFD;
2845 goto end;
2847 if (!frame_aligned(runtime, count)) {
2848 result = -EINVAL;
2849 goto end;
2851 count = bytes_to_frames(runtime, count);
2852 result = snd_pcm_lib_write(substream, buf, count);
2853 if (result > 0)
2854 result = frames_to_bytes(runtime, result);
2855 end:
2856 return result;
2859 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2860 unsigned long nr_segs, loff_t pos)
2863 struct snd_pcm_file *pcm_file;
2864 struct snd_pcm_substream *substream;
2865 struct snd_pcm_runtime *runtime;
2866 snd_pcm_sframes_t result;
2867 unsigned long i;
2868 void __user **bufs;
2869 snd_pcm_uframes_t frames;
2871 pcm_file = iocb->ki_filp->private_data;
2872 substream = pcm_file->substream;
2873 snd_assert(substream != NULL, return -ENXIO);
2874 runtime = substream->runtime;
2875 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2876 return -EBADFD;
2877 if (nr_segs > 1024 || nr_segs != runtime->channels)
2878 return -EINVAL;
2879 if (!frame_aligned(runtime, iov->iov_len))
2880 return -EINVAL;
2881 frames = bytes_to_samples(runtime, iov->iov_len);
2882 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2883 if (bufs == NULL)
2884 return -ENOMEM;
2885 for (i = 0; i < nr_segs; ++i)
2886 bufs[i] = iov[i].iov_base;
2887 result = snd_pcm_lib_readv(substream, bufs, frames);
2888 if (result > 0)
2889 result = frames_to_bytes(runtime, result);
2890 kfree(bufs);
2891 return result;
2894 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2895 unsigned long nr_segs, loff_t pos)
2897 struct snd_pcm_file *pcm_file;
2898 struct snd_pcm_substream *substream;
2899 struct snd_pcm_runtime *runtime;
2900 snd_pcm_sframes_t result;
2901 unsigned long i;
2902 void __user **bufs;
2903 snd_pcm_uframes_t frames;
2905 pcm_file = iocb->ki_filp->private_data;
2906 substream = pcm_file->substream;
2907 snd_assert(substream != NULL, result = -ENXIO; goto end);
2908 runtime = substream->runtime;
2909 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2910 result = -EBADFD;
2911 goto end;
2913 if (nr_segs > 128 || nr_segs != runtime->channels ||
2914 !frame_aligned(runtime, iov->iov_len)) {
2915 result = -EINVAL;
2916 goto end;
2918 frames = bytes_to_samples(runtime, iov->iov_len);
2919 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2920 if (bufs == NULL)
2921 return -ENOMEM;
2922 for (i = 0; i < nr_segs; ++i)
2923 bufs[i] = iov[i].iov_base;
2924 result = snd_pcm_lib_writev(substream, bufs, frames);
2925 if (result > 0)
2926 result = frames_to_bytes(runtime, result);
2927 kfree(bufs);
2928 end:
2929 return result;
2932 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2934 struct snd_pcm_file *pcm_file;
2935 struct snd_pcm_substream *substream;
2936 struct snd_pcm_runtime *runtime;
2937 unsigned int mask;
2938 snd_pcm_uframes_t avail;
2940 pcm_file = file->private_data;
2942 substream = pcm_file->substream;
2943 snd_assert(substream != NULL, return -ENXIO);
2944 runtime = substream->runtime;
2946 poll_wait(file, &runtime->sleep, wait);
2948 snd_pcm_stream_lock_irq(substream);
2949 avail = snd_pcm_playback_avail(runtime);
2950 switch (runtime->status->state) {
2951 case SNDRV_PCM_STATE_RUNNING:
2952 case SNDRV_PCM_STATE_PREPARED:
2953 case SNDRV_PCM_STATE_PAUSED:
2954 if (avail >= runtime->control->avail_min) {
2955 mask = POLLOUT | POLLWRNORM;
2956 break;
2958 /* Fall through */
2959 case SNDRV_PCM_STATE_DRAINING:
2960 mask = 0;
2961 break;
2962 default:
2963 mask = POLLOUT | POLLWRNORM | POLLERR;
2964 break;
2966 snd_pcm_stream_unlock_irq(substream);
2967 return mask;
2970 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2972 struct snd_pcm_file *pcm_file;
2973 struct snd_pcm_substream *substream;
2974 struct snd_pcm_runtime *runtime;
2975 unsigned int mask;
2976 snd_pcm_uframes_t avail;
2978 pcm_file = file->private_data;
2980 substream = pcm_file->substream;
2981 snd_assert(substream != NULL, return -ENXIO);
2982 runtime = substream->runtime;
2984 poll_wait(file, &runtime->sleep, wait);
2986 snd_pcm_stream_lock_irq(substream);
2987 avail = snd_pcm_capture_avail(runtime);
2988 switch (runtime->status->state) {
2989 case SNDRV_PCM_STATE_RUNNING:
2990 case SNDRV_PCM_STATE_PREPARED:
2991 case SNDRV_PCM_STATE_PAUSED:
2992 if (avail >= runtime->control->avail_min) {
2993 mask = POLLIN | POLLRDNORM;
2994 break;
2996 mask = 0;
2997 break;
2998 case SNDRV_PCM_STATE_DRAINING:
2999 if (avail > 0) {
3000 mask = POLLIN | POLLRDNORM;
3001 break;
3003 /* Fall through */
3004 default:
3005 mask = POLLIN | POLLRDNORM | POLLERR;
3006 break;
3008 snd_pcm_stream_unlock_irq(substream);
3009 return mask;
3013 * mmap support
3017 * Only on coherent architectures, we can mmap the status and the control records
3018 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3020 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3022 * mmap status record
3024 static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
3025 unsigned long address, int *type)
3027 struct snd_pcm_substream *substream = area->vm_private_data;
3028 struct snd_pcm_runtime *runtime;
3029 struct page * page;
3031 if (substream == NULL)
3032 return NOPAGE_SIGBUS;
3033 runtime = substream->runtime;
3034 page = virt_to_page(runtime->status);
3035 get_page(page);
3036 if (type)
3037 *type = VM_FAULT_MINOR;
3038 return page;
3041 static struct vm_operations_struct snd_pcm_vm_ops_status =
3043 .nopage = snd_pcm_mmap_status_nopage,
3046 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3047 struct vm_area_struct *area)
3049 struct snd_pcm_runtime *runtime;
3050 long size;
3051 if (!(area->vm_flags & VM_READ))
3052 return -EINVAL;
3053 runtime = substream->runtime;
3054 snd_assert(runtime != NULL, return -EAGAIN);
3055 size = area->vm_end - area->vm_start;
3056 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3057 return -EINVAL;
3058 area->vm_ops = &snd_pcm_vm_ops_status;
3059 area->vm_private_data = substream;
3060 area->vm_flags |= VM_RESERVED;
3061 return 0;
3065 * mmap control record
3067 static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3068 unsigned long address, int *type)
3070 struct snd_pcm_substream *substream = area->vm_private_data;
3071 struct snd_pcm_runtime *runtime;
3072 struct page * page;
3074 if (substream == NULL)
3075 return NOPAGE_SIGBUS;
3076 runtime = substream->runtime;
3077 page = virt_to_page(runtime->control);
3078 get_page(page);
3079 if (type)
3080 *type = VM_FAULT_MINOR;
3081 return page;
3084 static struct vm_operations_struct snd_pcm_vm_ops_control =
3086 .nopage = snd_pcm_mmap_control_nopage,
3089 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3090 struct vm_area_struct *area)
3092 struct snd_pcm_runtime *runtime;
3093 long size;
3094 if (!(area->vm_flags & VM_READ))
3095 return -EINVAL;
3096 runtime = substream->runtime;
3097 snd_assert(runtime != NULL, return -EAGAIN);
3098 size = area->vm_end - area->vm_start;
3099 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3100 return -EINVAL;
3101 area->vm_ops = &snd_pcm_vm_ops_control;
3102 area->vm_private_data = substream;
3103 area->vm_flags |= VM_RESERVED;
3104 return 0;
3106 #else /* ! coherent mmap */
3108 * don't support mmap for status and control records.
3110 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3111 struct vm_area_struct *area)
3113 return -ENXIO;
3115 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3116 struct vm_area_struct *area)
3118 return -ENXIO;
3120 #endif /* coherent mmap */
3123 * nopage callback for mmapping a RAM page
3125 static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3126 unsigned long address, int *type)
3128 struct snd_pcm_substream *substream = area->vm_private_data;
3129 struct snd_pcm_runtime *runtime;
3130 unsigned long offset;
3131 struct page * page;
3132 void *vaddr;
3133 size_t dma_bytes;
3135 if (substream == NULL)
3136 return NOPAGE_SIGBUS;
3137 runtime = substream->runtime;
3138 offset = area->vm_pgoff << PAGE_SHIFT;
3139 offset += address - area->vm_start;
3140 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS);
3141 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3142 if (offset > dma_bytes - PAGE_SIZE)
3143 return NOPAGE_SIGBUS;
3144 if (substream->ops->page) {
3145 page = substream->ops->page(substream, offset);
3146 if (! page)
3147 return NOPAGE_OOM; /* XXX: is this really due to OOM? */
3148 } else {
3149 vaddr = runtime->dma_area + offset;
3150 page = virt_to_page(vaddr);
3152 get_page(page);
3153 if (type)
3154 *type = VM_FAULT_MINOR;
3155 return page;
3158 static struct vm_operations_struct snd_pcm_vm_ops_data =
3160 .open = snd_pcm_mmap_data_open,
3161 .close = snd_pcm_mmap_data_close,
3162 .nopage = snd_pcm_mmap_data_nopage,
3166 * mmap the DMA buffer on RAM
3168 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3169 struct vm_area_struct *area)
3171 area->vm_ops = &snd_pcm_vm_ops_data;
3172 area->vm_private_data = substream;
3173 area->vm_flags |= VM_RESERVED;
3174 atomic_inc(&substream->mmap_count);
3175 return 0;
3179 * mmap the DMA buffer on I/O memory area
3181 #if SNDRV_PCM_INFO_MMAP_IOMEM
3182 static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3184 .open = snd_pcm_mmap_data_open,
3185 .close = snd_pcm_mmap_data_close,
3188 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3189 struct vm_area_struct *area)
3191 long size;
3192 unsigned long offset;
3194 #ifdef pgprot_noncached
3195 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3196 #endif
3197 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3198 area->vm_private_data = substream;
3199 area->vm_flags |= VM_IO;
3200 size = area->vm_end - area->vm_start;
3201 offset = area->vm_pgoff << PAGE_SHIFT;
3202 if (io_remap_pfn_range(area, area->vm_start,
3203 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3204 size, area->vm_page_prot))
3205 return -EAGAIN;
3206 atomic_inc(&substream->mmap_count);
3207 return 0;
3210 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3211 #endif /* SNDRV_PCM_INFO_MMAP */
3214 * mmap DMA buffer
3216 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3217 struct vm_area_struct *area)
3219 struct snd_pcm_runtime *runtime;
3220 long size;
3221 unsigned long offset;
3222 size_t dma_bytes;
3224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3225 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3226 return -EINVAL;
3227 } else {
3228 if (!(area->vm_flags & VM_READ))
3229 return -EINVAL;
3231 runtime = substream->runtime;
3232 snd_assert(runtime != NULL, return -EAGAIN);
3233 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3234 return -EBADFD;
3235 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3236 return -ENXIO;
3237 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3238 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3239 return -EINVAL;
3240 size = area->vm_end - area->vm_start;
3241 offset = area->vm_pgoff << PAGE_SHIFT;
3242 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3243 if ((size_t)size > dma_bytes)
3244 return -EINVAL;
3245 if (offset > dma_bytes - size)
3246 return -EINVAL;
3248 if (substream->ops->mmap)
3249 return substream->ops->mmap(substream, area);
3250 else
3251 return snd_pcm_default_mmap(substream, area);
3254 EXPORT_SYMBOL(snd_pcm_mmap_data);
3256 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3258 struct snd_pcm_file * pcm_file;
3259 struct snd_pcm_substream *substream;
3260 unsigned long offset;
3262 pcm_file = file->private_data;
3263 substream = pcm_file->substream;
3264 snd_assert(substream != NULL, return -ENXIO);
3266 offset = area->vm_pgoff << PAGE_SHIFT;
3267 switch (offset) {
3268 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3269 if (pcm_file->no_compat_mmap)
3270 return -ENXIO;
3271 return snd_pcm_mmap_status(substream, file, area);
3272 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3273 if (pcm_file->no_compat_mmap)
3274 return -ENXIO;
3275 return snd_pcm_mmap_control(substream, file, area);
3276 default:
3277 return snd_pcm_mmap_data(substream, file, area);
3279 return 0;
3282 static int snd_pcm_fasync(int fd, struct file * file, int on)
3284 struct snd_pcm_file * pcm_file;
3285 struct snd_pcm_substream *substream;
3286 struct snd_pcm_runtime *runtime;
3287 int err;
3289 pcm_file = file->private_data;
3290 substream = pcm_file->substream;
3291 snd_assert(substream != NULL, return -ENXIO);
3292 runtime = substream->runtime;
3294 err = fasync_helper(fd, file, on, &runtime->fasync);
3295 if (err < 0)
3296 return err;
3297 return 0;
3301 * ioctl32 compat
3303 #ifdef CONFIG_COMPAT
3304 #include "pcm_compat.c"
3305 #else
3306 #define snd_pcm_ioctl_compat NULL
3307 #endif
3310 * To be removed helpers to keep binary compatibility
3313 #ifdef CONFIG_SND_SUPPORT_OLD_API
3314 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3315 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3317 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3318 struct snd_pcm_hw_params_old *oparams)
3320 unsigned int i;
3322 memset(params, 0, sizeof(*params));
3323 params->flags = oparams->flags;
3324 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3325 params->masks[i].bits[0] = oparams->masks[i];
3326 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3327 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3328 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3329 params->info = oparams->info;
3330 params->msbits = oparams->msbits;
3331 params->rate_num = oparams->rate_num;
3332 params->rate_den = oparams->rate_den;
3333 params->fifo_size = oparams->fifo_size;
3336 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3337 struct snd_pcm_hw_params *params)
3339 unsigned int i;
3341 memset(oparams, 0, sizeof(*oparams));
3342 oparams->flags = params->flags;
3343 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3344 oparams->masks[i] = params->masks[i].bits[0];
3345 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3346 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3347 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3348 oparams->info = params->info;
3349 oparams->msbits = params->msbits;
3350 oparams->rate_num = params->rate_num;
3351 oparams->rate_den = params->rate_den;
3352 oparams->fifo_size = params->fifo_size;
3355 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
3358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
3360 int err;
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
3363 if (!params) {
3364 err = -ENOMEM;
3365 goto out;
3367 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3368 if (!oparams) {
3369 err = -ENOMEM;
3370 goto out;
3373 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3374 err = -EFAULT;
3375 goto out;
3377 snd_pcm_hw_convert_from_old_params(params, oparams);
3378 err = snd_pcm_hw_refine(substream, params);
3379 snd_pcm_hw_convert_to_old_params(oparams, params);
3380 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3381 if (!err)
3382 err = -EFAULT;
3384 out:
3385 kfree(params);
3386 kfree(oparams);
3387 return err;
3390 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3391 struct snd_pcm_hw_params_old __user * _oparams)
3393 struct snd_pcm_hw_params *params;
3394 struct snd_pcm_hw_params_old *oparams = NULL;
3395 int err;
3397 params = kmalloc(sizeof(*params), GFP_KERNEL);
3398 if (!params) {
3399 err = -ENOMEM;
3400 goto out;
3402 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3403 if (!oparams) {
3404 err = -ENOMEM;
3405 goto out;
3407 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3408 err = -EFAULT;
3409 goto out;
3411 snd_pcm_hw_convert_from_old_params(params, oparams);
3412 err = snd_pcm_hw_params(substream, params);
3413 snd_pcm_hw_convert_to_old_params(oparams, params);
3414 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3415 if (!err)
3416 err = -EFAULT;
3418 out:
3419 kfree(params);
3420 kfree(oparams);
3421 return err;
3423 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3426 * Register section
3429 const struct file_operations snd_pcm_f_ops[2] = {
3431 .owner = THIS_MODULE,
3432 .write = snd_pcm_write,
3433 .aio_write = snd_pcm_aio_write,
3434 .open = snd_pcm_playback_open,
3435 .release = snd_pcm_release,
3436 .poll = snd_pcm_playback_poll,
3437 .unlocked_ioctl = snd_pcm_playback_ioctl,
3438 .compat_ioctl = snd_pcm_ioctl_compat,
3439 .mmap = snd_pcm_mmap,
3440 .fasync = snd_pcm_fasync,
3443 .owner = THIS_MODULE,
3444 .read = snd_pcm_read,
3445 .aio_read = snd_pcm_aio_read,
3446 .open = snd_pcm_capture_open,
3447 .release = snd_pcm_release,
3448 .poll = snd_pcm_capture_poll,
3449 .unlocked_ioctl = snd_pcm_capture_ioctl,
3450 .compat_ioctl = snd_pcm_ioctl_compat,
3451 .mmap = snd_pcm_mmap,
3452 .fasync = snd_pcm_fasync,