x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / sound / core / pcm_native.c
blobab73edf2c89a1fe4e996c5f2d7ea6c751fba2d30
1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/mm.h>
23 #include <linux/file.h>
24 #include <linux/slab.h>
25 #include <linux/smp_lock.h>
26 #include <linux/time.h>
27 #include <linux/pm_qos_params.h>
28 #include <linux/uio.h>
29 #include <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 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
112 if (runtime) {
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
116 return 0;
119 int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
122 struct snd_pcm_info *info;
123 int err;
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
126 if (! info)
127 return -ENOMEM;
128 err = snd_pcm_info(substream, info);
129 if (err >= 0) {
130 if (copy_to_user(_info, info, sizeof(*info)))
131 err = -EFAULT;
133 kfree(info);
134 return err;
137 #undef RULES_DEBUG
139 #ifdef RULES_DEBUG
140 #define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141 char *snd_pcm_hw_param_names[] = {
142 HW_PARAM(ACCESS),
143 HW_PARAM(FORMAT),
144 HW_PARAM(SUBFORMAT),
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
147 HW_PARAM(CHANNELS),
148 HW_PARAM(RATE),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
152 HW_PARAM(PERIODS),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
156 HW_PARAM(TICK_TIME),
158 #endif
160 int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
163 unsigned int k;
164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
171 int changed, again;
173 params->info = 0;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
176 params->msbits = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
185 return -EINVAL;
186 if (!(params->rmask & (1 << k)))
187 continue;
188 #ifdef RULES_DEBUG
189 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
191 #endif
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
193 #ifdef RULES_DEBUG
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195 #endif
196 if (changed)
197 params->cmask |= 1 << k;
198 if (changed < 0)
199 return changed;
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
205 return -EINVAL;
206 if (!(params->rmask & (1 << k)))
207 continue;
208 #ifdef RULES_DEBUG
209 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
210 if (i->empty)
211 printk("empty");
212 else
213 printk("%c%u %u%c",
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
216 printk(" -> ");
217 #endif
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
219 #ifdef RULES_DEBUG
220 if (i->empty)
221 printk("empty\n");
222 else
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
226 #endif
227 if (changed)
228 params->cmask |= 1 << k;
229 if (changed < 0)
230 return changed;
233 for (k = 0; k < constrs->rules_num; k++)
234 rstamps[k] = 0;
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
237 do {
238 again = 0;
239 for (k = 0; k < constrs->rules_num; k++) {
240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
241 unsigned int d;
242 int doit = 0;
243 if (r->cond && !(r->cond & params->flags))
244 continue;
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
247 doit = 1;
248 break;
251 if (!doit)
252 continue;
253 #ifdef RULES_DEBUG
254 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
255 if (r->var >= 0) {
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
260 } else {
261 i = hw_param_interval(params, r->var);
262 if (i->empty)
263 printk("empty");
264 else
265 printk("%c%u %u%c",
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
270 #endif
271 changed = r->func(params, r);
272 #ifdef RULES_DEBUG
273 if (r->var >= 0) {
274 printk(" -> ");
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
277 else {
278 if (i->empty)
279 printk("empty");
280 else
281 printk("%c%u %u%c",
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
286 printk("\n");
287 #endif
288 rstamps[k] = stamp;
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
292 again = 1;
294 if (changed < 0)
295 return changed;
296 stamp++;
298 } while (again);
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
313 hw = &substream->runtime->hw;
314 if (!params->info)
315 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
316 if (!params->fifo_size) {
317 if (snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) ==
318 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) &&
319 snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS]) ==
320 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS])) {
321 changed = substream->ops->ioctl(substream,
322 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
323 if (changed < 0)
324 return changed;
327 params->rmask = 0;
328 return 0;
331 EXPORT_SYMBOL(snd_pcm_hw_refine);
333 static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
334 struct snd_pcm_hw_params __user * _params)
336 struct snd_pcm_hw_params *params;
337 int err;
339 params = memdup_user(_params, sizeof(*params));
340 if (IS_ERR(params))
341 return PTR_ERR(params);
343 err = snd_pcm_hw_refine(substream, params);
344 if (copy_to_user(_params, params, sizeof(*params))) {
345 if (!err)
346 err = -EFAULT;
349 kfree(params);
350 return err;
353 static int period_to_usecs(struct snd_pcm_runtime *runtime)
355 int usecs;
357 if (! runtime->rate)
358 return -1; /* invalid */
360 /* take 75% of period time as the deadline */
361 usecs = (750000 / runtime->rate) * runtime->period_size;
362 usecs += ((750000 % runtime->rate) * runtime->period_size) /
363 runtime->rate;
365 return usecs;
368 static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
369 struct snd_pcm_hw_params *params)
371 struct snd_pcm_runtime *runtime;
372 int err, usecs;
373 unsigned int bits;
374 snd_pcm_uframes_t frames;
376 if (PCM_RUNTIME_CHECK(substream))
377 return -ENXIO;
378 runtime = substream->runtime;
379 snd_pcm_stream_lock_irq(substream);
380 switch (runtime->status->state) {
381 case SNDRV_PCM_STATE_OPEN:
382 case SNDRV_PCM_STATE_SETUP:
383 case SNDRV_PCM_STATE_PREPARED:
384 break;
385 default:
386 snd_pcm_stream_unlock_irq(substream);
387 return -EBADFD;
389 snd_pcm_stream_unlock_irq(substream);
390 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
391 if (!substream->oss.oss)
392 #endif
393 if (atomic_read(&substream->mmap_count))
394 return -EBADFD;
396 params->rmask = ~0U;
397 err = snd_pcm_hw_refine(substream, params);
398 if (err < 0)
399 goto _error;
401 err = snd_pcm_hw_params_choose(substream, params);
402 if (err < 0)
403 goto _error;
405 if (substream->ops->hw_params != NULL) {
406 err = substream->ops->hw_params(substream, params);
407 if (err < 0)
408 goto _error;
411 runtime->access = params_access(params);
412 runtime->format = params_format(params);
413 runtime->subformat = params_subformat(params);
414 runtime->channels = params_channels(params);
415 runtime->rate = params_rate(params);
416 runtime->period_size = params_period_size(params);
417 runtime->periods = params_periods(params);
418 runtime->buffer_size = params_buffer_size(params);
419 runtime->info = params->info;
420 runtime->rate_num = params->rate_num;
421 runtime->rate_den = params->rate_den;
423 bits = snd_pcm_format_physical_width(runtime->format);
424 runtime->sample_bits = bits;
425 bits *= runtime->channels;
426 runtime->frame_bits = bits;
427 frames = 1;
428 while (bits % 8 != 0) {
429 bits *= 2;
430 frames *= 2;
432 runtime->byte_align = bits / 8;
433 runtime->min_align = frames;
435 /* Default sw params */
436 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
437 runtime->period_step = 1;
438 runtime->control->avail_min = 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 = memdup_user(_params, sizeof(*params));
473 if (IS_ERR(params))
474 return PTR_ERR(params);
476 err = snd_pcm_hw_params(substream, params);
477 if (copy_to_user(_params, params, sizeof(*params))) {
478 if (!err)
479 err = -EFAULT;
482 kfree(params);
483 return err;
486 static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
488 struct snd_pcm_runtime *runtime;
489 int result = 0;
491 if (PCM_RUNTIME_CHECK(substream))
492 return -ENXIO;
493 runtime = substream->runtime;
494 snd_pcm_stream_lock_irq(substream);
495 switch (runtime->status->state) {
496 case SNDRV_PCM_STATE_SETUP:
497 case SNDRV_PCM_STATE_PREPARED:
498 break;
499 default:
500 snd_pcm_stream_unlock_irq(substream);
501 return -EBADFD;
503 snd_pcm_stream_unlock_irq(substream);
504 if (atomic_read(&substream->mmap_count))
505 return -EBADFD;
506 if (substream->ops->hw_free)
507 result = substream->ops->hw_free(substream);
508 runtime->status->state = SNDRV_PCM_STATE_OPEN;
509 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
510 substream->latency_id);
511 return result;
514 static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
515 struct snd_pcm_sw_params *params)
517 struct snd_pcm_runtime *runtime;
519 if (PCM_RUNTIME_CHECK(substream))
520 return -ENXIO;
521 runtime = substream->runtime;
522 snd_pcm_stream_lock_irq(substream);
523 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
524 snd_pcm_stream_unlock_irq(substream);
525 return -EBADFD;
527 snd_pcm_stream_unlock_irq(substream);
529 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
530 return -EINVAL;
531 if (params->avail_min == 0)
532 return -EINVAL;
533 if (params->silence_size >= runtime->boundary) {
534 if (params->silence_threshold != 0)
535 return -EINVAL;
536 } else {
537 if (params->silence_size > params->silence_threshold)
538 return -EINVAL;
539 if (params->silence_threshold > runtime->buffer_size)
540 return -EINVAL;
542 snd_pcm_stream_lock_irq(substream);
543 runtime->tstamp_mode = params->tstamp_mode;
544 runtime->period_step = params->period_step;
545 runtime->control->avail_min = params->avail_min;
546 runtime->start_threshold = params->start_threshold;
547 runtime->stop_threshold = params->stop_threshold;
548 runtime->silence_threshold = params->silence_threshold;
549 runtime->silence_size = params->silence_size;
550 params->boundary = runtime->boundary;
551 if (snd_pcm_running(substream)) {
552 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
553 runtime->silence_size > 0)
554 snd_pcm_playback_silence(substream, ULONG_MAX);
555 wake_up(&runtime->sleep);
557 snd_pcm_stream_unlock_irq(substream);
558 return 0;
561 static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
562 struct snd_pcm_sw_params __user * _params)
564 struct snd_pcm_sw_params params;
565 int err;
566 if (copy_from_user(&params, _params, sizeof(params)))
567 return -EFAULT;
568 err = snd_pcm_sw_params(substream, &params);
569 if (copy_to_user(_params, &params, sizeof(params)))
570 return -EFAULT;
571 return err;
574 int snd_pcm_status(struct snd_pcm_substream *substream,
575 struct snd_pcm_status *status)
577 struct snd_pcm_runtime *runtime = substream->runtime;
579 snd_pcm_stream_lock_irq(substream);
580 status->state = runtime->status->state;
581 status->suspended_state = runtime->status->suspended_state;
582 if (status->state == SNDRV_PCM_STATE_OPEN)
583 goto _end;
584 status->trigger_tstamp = runtime->trigger_tstamp;
585 if (snd_pcm_running(substream)) {
586 snd_pcm_update_hw_ptr(substream);
587 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
588 status->tstamp = runtime->status->tstamp;
589 goto _tstamp_end;
592 snd_pcm_gettime(runtime, &status->tstamp);
593 _tstamp_end:
594 status->appl_ptr = runtime->control->appl_ptr;
595 status->hw_ptr = runtime->status->hw_ptr;
596 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
597 status->avail = snd_pcm_playback_avail(runtime);
598 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
599 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
600 status->delay = runtime->buffer_size - status->avail;
601 status->delay += runtime->delay;
602 } else
603 status->delay = 0;
604 } else {
605 status->avail = snd_pcm_capture_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
607 status->delay = status->avail + runtime->delay;
608 else
609 status->delay = 0;
611 status->avail_max = runtime->avail_max;
612 status->overrange = runtime->overrange;
613 runtime->avail_max = 0;
614 runtime->overrange = 0;
615 _end:
616 snd_pcm_stream_unlock_irq(substream);
617 return 0;
620 static int snd_pcm_status_user(struct snd_pcm_substream *substream,
621 struct snd_pcm_status __user * _status)
623 struct snd_pcm_status status;
624 int res;
626 memset(&status, 0, sizeof(status));
627 res = snd_pcm_status(substream, &status);
628 if (res < 0)
629 return res;
630 if (copy_to_user(_status, &status, sizeof(status)))
631 return -EFAULT;
632 return 0;
635 static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
636 struct snd_pcm_channel_info * info)
638 struct snd_pcm_runtime *runtime;
639 unsigned int channel;
641 channel = info->channel;
642 runtime = substream->runtime;
643 snd_pcm_stream_lock_irq(substream);
644 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
645 snd_pcm_stream_unlock_irq(substream);
646 return -EBADFD;
648 snd_pcm_stream_unlock_irq(substream);
649 if (channel >= runtime->channels)
650 return -EINVAL;
651 memset(info, 0, sizeof(*info));
652 info->channel = channel;
653 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
656 static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
657 struct snd_pcm_channel_info __user * _info)
659 struct snd_pcm_channel_info info;
660 int res;
662 if (copy_from_user(&info, _info, sizeof(info)))
663 return -EFAULT;
664 res = snd_pcm_channel_info(substream, &info);
665 if (res < 0)
666 return res;
667 if (copy_to_user(_info, &info, sizeof(info)))
668 return -EFAULT;
669 return 0;
672 static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
674 struct snd_pcm_runtime *runtime = substream->runtime;
675 if (runtime->trigger_master == NULL)
676 return;
677 if (runtime->trigger_master == substream) {
678 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
679 } else {
680 snd_pcm_trigger_tstamp(runtime->trigger_master);
681 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
683 runtime->trigger_master = NULL;
686 struct action_ops {
687 int (*pre_action)(struct snd_pcm_substream *substream, int state);
688 int (*do_action)(struct snd_pcm_substream *substream, int state);
689 void (*undo_action)(struct snd_pcm_substream *substream, int state);
690 void (*post_action)(struct snd_pcm_substream *substream, int state);
694 * this functions is core for handling of linked stream
695 * Note: the stream state might be changed also on failure
696 * Note2: call with calling stream lock + link lock
698 static int snd_pcm_action_group(struct action_ops *ops,
699 struct snd_pcm_substream *substream,
700 int state, int do_lock)
702 struct snd_pcm_substream *s = NULL;
703 struct snd_pcm_substream *s1;
704 int res = 0;
706 snd_pcm_group_for_each_entry(s, substream) {
707 if (do_lock && s != substream)
708 spin_lock_nested(&s->self_group.lock,
709 SINGLE_DEPTH_NESTING);
710 res = ops->pre_action(s, state);
711 if (res < 0)
712 goto _unlock;
714 snd_pcm_group_for_each_entry(s, substream) {
715 res = ops->do_action(s, state);
716 if (res < 0) {
717 if (ops->undo_action) {
718 snd_pcm_group_for_each_entry(s1, substream) {
719 if (s1 == s) /* failed stream */
720 break;
721 ops->undo_action(s1, state);
724 s = NULL; /* unlock all */
725 goto _unlock;
728 snd_pcm_group_for_each_entry(s, substream) {
729 ops->post_action(s, state);
731 _unlock:
732 if (do_lock) {
733 /* unlock streams */
734 snd_pcm_group_for_each_entry(s1, substream) {
735 if (s1 != substream)
736 spin_unlock(&s1->self_group.lock);
737 if (s1 == s) /* end */
738 break;
741 return res;
745 * Note: call with stream lock
747 static int snd_pcm_action_single(struct action_ops *ops,
748 struct snd_pcm_substream *substream,
749 int state)
751 int res;
753 res = ops->pre_action(substream, state);
754 if (res < 0)
755 return res;
756 res = ops->do_action(substream, state);
757 if (res == 0)
758 ops->post_action(substream, state);
759 else if (ops->undo_action)
760 ops->undo_action(substream, state);
761 return res;
765 * Note: call with stream lock
767 static int snd_pcm_action(struct action_ops *ops,
768 struct snd_pcm_substream *substream,
769 int state)
771 int res;
773 if (snd_pcm_stream_linked(substream)) {
774 if (!spin_trylock(&substream->group->lock)) {
775 spin_unlock(&substream->self_group.lock);
776 spin_lock(&substream->group->lock);
777 spin_lock(&substream->self_group.lock);
779 res = snd_pcm_action_group(ops, substream, state, 1);
780 spin_unlock(&substream->group->lock);
781 } else {
782 res = snd_pcm_action_single(ops, substream, state);
784 return res;
788 * Note: don't use any locks before
790 static int snd_pcm_action_lock_irq(struct action_ops *ops,
791 struct snd_pcm_substream *substream,
792 int state)
794 int res;
796 read_lock_irq(&snd_pcm_link_rwlock);
797 if (snd_pcm_stream_linked(substream)) {
798 spin_lock(&substream->group->lock);
799 spin_lock(&substream->self_group.lock);
800 res = snd_pcm_action_group(ops, substream, state, 1);
801 spin_unlock(&substream->self_group.lock);
802 spin_unlock(&substream->group->lock);
803 } else {
804 spin_lock(&substream->self_group.lock);
805 res = snd_pcm_action_single(ops, substream, state);
806 spin_unlock(&substream->self_group.lock);
808 read_unlock_irq(&snd_pcm_link_rwlock);
809 return res;
814 static int snd_pcm_action_nonatomic(struct action_ops *ops,
815 struct snd_pcm_substream *substream,
816 int state)
818 int res;
820 down_read(&snd_pcm_link_rwsem);
821 if (snd_pcm_stream_linked(substream))
822 res = snd_pcm_action_group(ops, substream, state, 0);
823 else
824 res = snd_pcm_action_single(ops, substream, state);
825 up_read(&snd_pcm_link_rwsem);
826 return res;
830 * start callbacks
832 static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
834 struct snd_pcm_runtime *runtime = substream->runtime;
835 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
836 return -EBADFD;
837 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
838 !snd_pcm_playback_data(substream))
839 return -EPIPE;
840 runtime->trigger_master = substream;
841 return 0;
844 static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
846 if (substream->runtime->trigger_master != substream)
847 return 0;
848 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
851 static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
853 if (substream->runtime->trigger_master == substream)
854 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
857 static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
859 struct snd_pcm_runtime *runtime = substream->runtime;
860 snd_pcm_trigger_tstamp(substream);
861 runtime->hw_ptr_jiffies = jiffies;
862 runtime->status->state = state;
863 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
864 runtime->silence_size > 0)
865 snd_pcm_playback_silence(substream, ULONG_MAX);
866 if (substream->timer)
867 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
868 &runtime->trigger_tstamp);
871 static struct action_ops snd_pcm_action_start = {
872 .pre_action = snd_pcm_pre_start,
873 .do_action = snd_pcm_do_start,
874 .undo_action = snd_pcm_undo_start,
875 .post_action = snd_pcm_post_start
879 * snd_pcm_start - start all linked streams
880 * @substream: the PCM substream instance
882 int snd_pcm_start(struct snd_pcm_substream *substream)
884 return snd_pcm_action(&snd_pcm_action_start, substream,
885 SNDRV_PCM_STATE_RUNNING);
889 * stop callbacks
891 static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
893 struct snd_pcm_runtime *runtime = substream->runtime;
894 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
895 return -EBADFD;
896 runtime->trigger_master = substream;
897 return 0;
900 static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
902 if (substream->runtime->trigger_master == substream &&
903 snd_pcm_running(substream))
904 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
905 return 0; /* unconditonally stop all substreams */
908 static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
910 struct snd_pcm_runtime *runtime = substream->runtime;
911 if (runtime->status->state != state) {
912 snd_pcm_trigger_tstamp(substream);
913 if (substream->timer)
914 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
915 &runtime->trigger_tstamp);
916 runtime->status->state = state;
918 wake_up(&runtime->sleep);
921 static struct action_ops snd_pcm_action_stop = {
922 .pre_action = snd_pcm_pre_stop,
923 .do_action = snd_pcm_do_stop,
924 .post_action = snd_pcm_post_stop
928 * snd_pcm_stop - try to stop all running streams in the substream group
929 * @substream: the PCM substream instance
930 * @state: PCM state after stopping the stream
932 * The state of each stream is then changed to the given state unconditionally.
934 int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
936 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
939 EXPORT_SYMBOL(snd_pcm_stop);
942 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
943 * @substream: the PCM substream
945 * After stopping, the state is changed to SETUP.
946 * Unlike snd_pcm_stop(), this affects only the given stream.
948 int snd_pcm_drain_done(struct snd_pcm_substream *substream)
950 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
951 SNDRV_PCM_STATE_SETUP);
955 * pause callbacks
957 static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
959 struct snd_pcm_runtime *runtime = substream->runtime;
960 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
961 return -ENOSYS;
962 if (push) {
963 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
964 return -EBADFD;
965 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
966 return -EBADFD;
967 runtime->trigger_master = substream;
968 return 0;
971 static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
973 if (substream->runtime->trigger_master != substream)
974 return 0;
975 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
976 * a delta betwen the current jiffies, this gives a large enough
977 * delta, effectively to skip the check once.
979 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
980 return substream->ops->trigger(substream,
981 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
982 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
985 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
987 if (substream->runtime->trigger_master == substream)
988 substream->ops->trigger(substream,
989 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
990 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
993 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
995 struct snd_pcm_runtime *runtime = substream->runtime;
996 snd_pcm_trigger_tstamp(substream);
997 if (push) {
998 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
999 if (substream->timer)
1000 snd_timer_notify(substream->timer,
1001 SNDRV_TIMER_EVENT_MPAUSE,
1002 &runtime->trigger_tstamp);
1003 wake_up(&runtime->sleep);
1004 } else {
1005 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1006 if (substream->timer)
1007 snd_timer_notify(substream->timer,
1008 SNDRV_TIMER_EVENT_MCONTINUE,
1009 &runtime->trigger_tstamp);
1013 static struct action_ops snd_pcm_action_pause = {
1014 .pre_action = snd_pcm_pre_pause,
1015 .do_action = snd_pcm_do_pause,
1016 .undo_action = snd_pcm_undo_pause,
1017 .post_action = snd_pcm_post_pause
1021 * Push/release the pause for all linked streams.
1023 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1025 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1028 #ifdef CONFIG_PM
1029 /* suspend */
1031 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1033 struct snd_pcm_runtime *runtime = substream->runtime;
1034 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1035 return -EBUSY;
1036 runtime->trigger_master = substream;
1037 return 0;
1040 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1042 struct snd_pcm_runtime *runtime = substream->runtime;
1043 if (runtime->trigger_master != substream)
1044 return 0;
1045 if (! snd_pcm_running(substream))
1046 return 0;
1047 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1048 return 0; /* suspend unconditionally */
1051 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1053 struct snd_pcm_runtime *runtime = substream->runtime;
1054 snd_pcm_trigger_tstamp(substream);
1055 if (substream->timer)
1056 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1057 &runtime->trigger_tstamp);
1058 runtime->status->suspended_state = runtime->status->state;
1059 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1060 wake_up(&runtime->sleep);
1063 static struct action_ops snd_pcm_action_suspend = {
1064 .pre_action = snd_pcm_pre_suspend,
1065 .do_action = snd_pcm_do_suspend,
1066 .post_action = snd_pcm_post_suspend
1070 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1071 * @substream: the PCM substream
1073 * After this call, all streams are changed to SUSPENDED state.
1075 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1077 int err;
1078 unsigned long flags;
1080 if (! substream)
1081 return 0;
1083 snd_pcm_stream_lock_irqsave(substream, flags);
1084 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1085 snd_pcm_stream_unlock_irqrestore(substream, flags);
1086 return err;
1089 EXPORT_SYMBOL(snd_pcm_suspend);
1092 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1093 * @pcm: the PCM instance
1095 * After this call, all streams are changed to SUSPENDED state.
1097 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1099 struct snd_pcm_substream *substream;
1100 int stream, err = 0;
1102 if (! pcm)
1103 return 0;
1105 for (stream = 0; stream < 2; stream++) {
1106 for (substream = pcm->streams[stream].substream;
1107 substream; substream = substream->next) {
1108 /* FIXME: the open/close code should lock this as well */
1109 if (substream->runtime == NULL)
1110 continue;
1111 err = snd_pcm_suspend(substream);
1112 if (err < 0 && err != -EBUSY)
1113 return err;
1116 return 0;
1119 EXPORT_SYMBOL(snd_pcm_suspend_all);
1121 /* resume */
1123 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1125 struct snd_pcm_runtime *runtime = substream->runtime;
1126 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1127 return -ENOSYS;
1128 runtime->trigger_master = substream;
1129 return 0;
1132 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1134 struct snd_pcm_runtime *runtime = substream->runtime;
1135 if (runtime->trigger_master != substream)
1136 return 0;
1137 /* DMA not running previously? */
1138 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1139 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1140 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1141 return 0;
1142 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1145 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1147 if (substream->runtime->trigger_master == substream &&
1148 snd_pcm_running(substream))
1149 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1152 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1154 struct snd_pcm_runtime *runtime = substream->runtime;
1155 snd_pcm_trigger_tstamp(substream);
1156 if (substream->timer)
1157 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1158 &runtime->trigger_tstamp);
1159 runtime->status->state = runtime->status->suspended_state;
1162 static struct action_ops snd_pcm_action_resume = {
1163 .pre_action = snd_pcm_pre_resume,
1164 .do_action = snd_pcm_do_resume,
1165 .undo_action = snd_pcm_undo_resume,
1166 .post_action = snd_pcm_post_resume
1169 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1171 struct snd_card *card = substream->pcm->card;
1172 int res;
1174 snd_power_lock(card);
1175 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1176 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1177 snd_power_unlock(card);
1178 return res;
1181 #else
1183 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1185 return -ENOSYS;
1188 #endif /* CONFIG_PM */
1191 * xrun ioctl
1193 * Change the RUNNING stream(s) to XRUN state.
1195 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1197 struct snd_card *card = substream->pcm->card;
1198 struct snd_pcm_runtime *runtime = substream->runtime;
1199 int result;
1201 snd_power_lock(card);
1202 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1203 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1204 if (result < 0)
1205 goto _unlock;
1208 snd_pcm_stream_lock_irq(substream);
1209 switch (runtime->status->state) {
1210 case SNDRV_PCM_STATE_XRUN:
1211 result = 0; /* already there */
1212 break;
1213 case SNDRV_PCM_STATE_RUNNING:
1214 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1215 break;
1216 default:
1217 result = -EBADFD;
1219 snd_pcm_stream_unlock_irq(substream);
1220 _unlock:
1221 snd_power_unlock(card);
1222 return result;
1226 * reset ioctl
1228 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1230 struct snd_pcm_runtime *runtime = substream->runtime;
1231 switch (runtime->status->state) {
1232 case SNDRV_PCM_STATE_RUNNING:
1233 case SNDRV_PCM_STATE_PREPARED:
1234 case SNDRV_PCM_STATE_PAUSED:
1235 case SNDRV_PCM_STATE_SUSPENDED:
1236 return 0;
1237 default:
1238 return -EBADFD;
1242 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1244 struct snd_pcm_runtime *runtime = substream->runtime;
1245 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1246 if (err < 0)
1247 return err;
1248 runtime->hw_ptr_base = 0;
1249 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1250 runtime->status->hw_ptr % runtime->period_size;
1251 runtime->silence_start = runtime->status->hw_ptr;
1252 runtime->silence_filled = 0;
1253 return 0;
1256 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1258 struct snd_pcm_runtime *runtime = substream->runtime;
1259 runtime->control->appl_ptr = runtime->status->hw_ptr;
1260 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1261 runtime->silence_size > 0)
1262 snd_pcm_playback_silence(substream, ULONG_MAX);
1265 static struct action_ops snd_pcm_action_reset = {
1266 .pre_action = snd_pcm_pre_reset,
1267 .do_action = snd_pcm_do_reset,
1268 .post_action = snd_pcm_post_reset
1271 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1273 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1277 * prepare ioctl
1279 /* we use the second argument for updating f_flags */
1280 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1281 int f_flags)
1283 struct snd_pcm_runtime *runtime = substream->runtime;
1284 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1285 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1286 return -EBADFD;
1287 if (snd_pcm_running(substream))
1288 return -EBUSY;
1289 substream->f_flags = f_flags;
1290 return 0;
1293 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1295 int err;
1296 err = substream->ops->prepare(substream);
1297 if (err < 0)
1298 return err;
1299 return snd_pcm_do_reset(substream, 0);
1302 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1304 struct snd_pcm_runtime *runtime = substream->runtime;
1305 runtime->control->appl_ptr = runtime->status->hw_ptr;
1306 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1309 static struct action_ops snd_pcm_action_prepare = {
1310 .pre_action = snd_pcm_pre_prepare,
1311 .do_action = snd_pcm_do_prepare,
1312 .post_action = snd_pcm_post_prepare
1316 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1317 * @substream: the PCM substream instance
1318 * @file: file to refer f_flags
1320 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1321 struct file *file)
1323 int res;
1324 struct snd_card *card = substream->pcm->card;
1325 int f_flags;
1327 if (file)
1328 f_flags = file->f_flags;
1329 else
1330 f_flags = substream->f_flags;
1332 snd_power_lock(card);
1333 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1334 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1335 substream, f_flags);
1336 snd_power_unlock(card);
1337 return res;
1341 * drain ioctl
1344 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1346 substream->runtime->trigger_master = substream;
1347 return 0;
1350 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1352 struct snd_pcm_runtime *runtime = substream->runtime;
1353 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1354 switch (runtime->status->state) {
1355 case SNDRV_PCM_STATE_PREPARED:
1356 /* start playback stream if possible */
1357 if (! snd_pcm_playback_empty(substream)) {
1358 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1359 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1361 break;
1362 case SNDRV_PCM_STATE_RUNNING:
1363 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1364 break;
1365 default:
1366 break;
1368 } else {
1369 /* stop running stream */
1370 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1371 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1372 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1373 snd_pcm_do_stop(substream, new_state);
1374 snd_pcm_post_stop(substream, new_state);
1377 return 0;
1380 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1384 static struct action_ops snd_pcm_action_drain_init = {
1385 .pre_action = snd_pcm_pre_drain_init,
1386 .do_action = snd_pcm_do_drain_init,
1387 .post_action = snd_pcm_post_drain_init
1390 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1393 * Drain the stream(s).
1394 * When the substream is linked, sync until the draining of all playback streams
1395 * is finished.
1396 * After this call, all streams are supposed to be either SETUP or DRAINING
1397 * (capture only) state.
1399 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1400 struct file *file)
1402 struct snd_card *card;
1403 struct snd_pcm_runtime *runtime;
1404 struct snd_pcm_substream *s;
1405 wait_queue_t wait;
1406 int result = 0;
1407 int nonblock = 0;
1409 card = substream->pcm->card;
1410 runtime = substream->runtime;
1412 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1413 return -EBADFD;
1415 snd_power_lock(card);
1416 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1417 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1418 if (result < 0) {
1419 snd_power_unlock(card);
1420 return result;
1424 if (file) {
1425 if (file->f_flags & O_NONBLOCK)
1426 nonblock = 1;
1427 } else if (substream->f_flags & O_NONBLOCK)
1428 nonblock = 1;
1430 down_read(&snd_pcm_link_rwsem);
1431 snd_pcm_stream_lock_irq(substream);
1432 /* resume pause */
1433 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1434 snd_pcm_pause(substream, 0);
1436 /* pre-start/stop - all running streams are changed to DRAINING state */
1437 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1438 if (result < 0)
1439 goto unlock;
1440 /* in non-blocking, we don't wait in ioctl but let caller poll */
1441 if (nonblock) {
1442 result = -EAGAIN;
1443 goto unlock;
1446 for (;;) {
1447 long tout;
1448 struct snd_pcm_runtime *to_check;
1449 if (signal_pending(current)) {
1450 result = -ERESTARTSYS;
1451 break;
1453 /* find a substream to drain */
1454 to_check = NULL;
1455 snd_pcm_group_for_each_entry(s, substream) {
1456 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1457 continue;
1458 runtime = s->runtime;
1459 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1460 to_check = runtime;
1461 break;
1464 if (!to_check)
1465 break; /* all drained */
1466 init_waitqueue_entry(&wait, current);
1467 add_wait_queue(&to_check->sleep, &wait);
1468 set_current_state(TASK_INTERRUPTIBLE);
1469 snd_pcm_stream_unlock_irq(substream);
1470 up_read(&snd_pcm_link_rwsem);
1471 snd_power_unlock(card);
1472 tout = schedule_timeout(10 * HZ);
1473 snd_power_lock(card);
1474 down_read(&snd_pcm_link_rwsem);
1475 snd_pcm_stream_lock_irq(substream);
1476 remove_wait_queue(&to_check->sleep, &wait);
1477 if (tout == 0) {
1478 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1479 result = -ESTRPIPE;
1480 else {
1481 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1482 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1483 result = -EIO;
1485 break;
1489 unlock:
1490 snd_pcm_stream_unlock_irq(substream);
1491 up_read(&snd_pcm_link_rwsem);
1492 snd_power_unlock(card);
1494 return result;
1498 * drop ioctl
1500 * Immediately put all linked substreams into SETUP state.
1502 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1504 struct snd_pcm_runtime *runtime;
1505 struct snd_card *card;
1506 int result = 0;
1508 if (PCM_RUNTIME_CHECK(substream))
1509 return -ENXIO;
1510 runtime = substream->runtime;
1511 card = substream->pcm->card;
1513 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1514 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1515 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1516 return -EBADFD;
1518 snd_pcm_stream_lock_irq(substream);
1519 /* resume pause */
1520 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1521 snd_pcm_pause(substream, 0);
1523 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1524 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1525 snd_pcm_stream_unlock_irq(substream);
1527 return result;
1531 /* WARNING: Don't forget to fput back the file */
1532 static struct file *snd_pcm_file_fd(int fd)
1534 struct file *file;
1535 struct inode *inode;
1536 unsigned int minor;
1538 file = fget(fd);
1539 if (!file)
1540 return NULL;
1541 inode = file->f_path.dentry->d_inode;
1542 if (!S_ISCHR(inode->i_mode) ||
1543 imajor(inode) != snd_major) {
1544 fput(file);
1545 return NULL;
1547 minor = iminor(inode);
1548 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1549 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1550 fput(file);
1551 return NULL;
1553 return file;
1557 * PCM link handling
1559 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1561 int res = 0;
1562 struct file *file;
1563 struct snd_pcm_file *pcm_file;
1564 struct snd_pcm_substream *substream1;
1566 file = snd_pcm_file_fd(fd);
1567 if (!file)
1568 return -EBADFD;
1569 pcm_file = file->private_data;
1570 substream1 = pcm_file->substream;
1571 down_write(&snd_pcm_link_rwsem);
1572 write_lock_irq(&snd_pcm_link_rwlock);
1573 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1574 substream->runtime->status->state != substream1->runtime->status->state) {
1575 res = -EBADFD;
1576 goto _end;
1578 if (snd_pcm_stream_linked(substream1)) {
1579 res = -EALREADY;
1580 goto _end;
1582 if (!snd_pcm_stream_linked(substream)) {
1583 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1584 if (substream->group == NULL) {
1585 res = -ENOMEM;
1586 goto _end;
1588 spin_lock_init(&substream->group->lock);
1589 INIT_LIST_HEAD(&substream->group->substreams);
1590 list_add_tail(&substream->link_list, &substream->group->substreams);
1591 substream->group->count = 1;
1593 list_add_tail(&substream1->link_list, &substream->group->substreams);
1594 substream->group->count++;
1595 substream1->group = substream->group;
1596 _end:
1597 write_unlock_irq(&snd_pcm_link_rwlock);
1598 up_write(&snd_pcm_link_rwsem);
1599 fput(file);
1600 return res;
1603 static void relink_to_local(struct snd_pcm_substream *substream)
1605 substream->group = &substream->self_group;
1606 INIT_LIST_HEAD(&substream->self_group.substreams);
1607 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1610 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1612 struct snd_pcm_substream *s;
1613 int res = 0;
1615 down_write(&snd_pcm_link_rwsem);
1616 write_lock_irq(&snd_pcm_link_rwlock);
1617 if (!snd_pcm_stream_linked(substream)) {
1618 res = -EALREADY;
1619 goto _end;
1621 list_del(&substream->link_list);
1622 substream->group->count--;
1623 if (substream->group->count == 1) { /* detach the last stream, too */
1624 snd_pcm_group_for_each_entry(s, substream) {
1625 relink_to_local(s);
1626 break;
1628 kfree(substream->group);
1630 relink_to_local(substream);
1631 _end:
1632 write_unlock_irq(&snd_pcm_link_rwlock);
1633 up_write(&snd_pcm_link_rwsem);
1634 return res;
1638 * hw configurator
1640 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1641 struct snd_pcm_hw_rule *rule)
1643 struct snd_interval t;
1644 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1645 hw_param_interval_c(params, rule->deps[1]), &t);
1646 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1649 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1650 struct snd_pcm_hw_rule *rule)
1652 struct snd_interval t;
1653 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1654 hw_param_interval_c(params, rule->deps[1]), &t);
1655 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1658 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1659 struct snd_pcm_hw_rule *rule)
1661 struct snd_interval t;
1662 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1663 hw_param_interval_c(params, rule->deps[1]),
1664 (unsigned long) rule->private, &t);
1665 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1668 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1669 struct snd_pcm_hw_rule *rule)
1671 struct snd_interval t;
1672 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1673 (unsigned long) rule->private,
1674 hw_param_interval_c(params, rule->deps[1]), &t);
1675 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1678 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1679 struct snd_pcm_hw_rule *rule)
1681 unsigned int k;
1682 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1683 struct snd_mask m;
1684 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1685 snd_mask_any(&m);
1686 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1687 int bits;
1688 if (! snd_mask_test(mask, k))
1689 continue;
1690 bits = snd_pcm_format_physical_width(k);
1691 if (bits <= 0)
1692 continue; /* ignore invalid formats */
1693 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1694 snd_mask_reset(&m, k);
1696 return snd_mask_refine(mask, &m);
1699 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
1702 struct snd_interval t;
1703 unsigned int k;
1704 t.min = UINT_MAX;
1705 t.max = 0;
1706 t.openmin = 0;
1707 t.openmax = 0;
1708 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1709 int bits;
1710 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1711 continue;
1712 bits = snd_pcm_format_physical_width(k);
1713 if (bits <= 0)
1714 continue; /* ignore invalid formats */
1715 if (t.min > (unsigned)bits)
1716 t.min = bits;
1717 if (t.max < (unsigned)bits)
1718 t.max = bits;
1720 t.integer = 1;
1721 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1724 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1725 #error "Change this table"
1726 #endif
1728 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1729 48000, 64000, 88200, 96000, 176400, 192000 };
1731 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1732 .count = ARRAY_SIZE(rates),
1733 .list = rates,
1736 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1737 struct snd_pcm_hw_rule *rule)
1739 struct snd_pcm_hardware *hw = rule->private;
1740 return snd_interval_list(hw_param_interval(params, rule->var),
1741 snd_pcm_known_rates.count,
1742 snd_pcm_known_rates.list, hw->rates);
1745 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1746 struct snd_pcm_hw_rule *rule)
1748 struct snd_interval t;
1749 struct snd_pcm_substream *substream = rule->private;
1750 t.min = 0;
1751 t.max = substream->buffer_bytes_max;
1752 t.openmin = 0;
1753 t.openmax = 0;
1754 t.integer = 1;
1755 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1758 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1760 struct snd_pcm_runtime *runtime = substream->runtime;
1761 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1762 int k, err;
1764 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1765 snd_mask_any(constrs_mask(constrs, k));
1768 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1769 snd_interval_any(constrs_interval(constrs, k));
1772 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1773 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1774 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1775 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1776 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1778 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1779 snd_pcm_hw_rule_format, NULL,
1780 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1781 if (err < 0)
1782 return err;
1783 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1784 snd_pcm_hw_rule_sample_bits, NULL,
1785 SNDRV_PCM_HW_PARAM_FORMAT,
1786 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1787 if (err < 0)
1788 return err;
1789 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1790 snd_pcm_hw_rule_div, NULL,
1791 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1792 if (err < 0)
1793 return err;
1794 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1795 snd_pcm_hw_rule_mul, NULL,
1796 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1797 if (err < 0)
1798 return err;
1799 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1800 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1801 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1802 if (err < 0)
1803 return err;
1804 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1805 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1806 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1807 if (err < 0)
1808 return err;
1809 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1810 snd_pcm_hw_rule_div, NULL,
1811 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1812 if (err < 0)
1813 return err;
1814 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1815 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1816 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1817 if (err < 0)
1818 return err;
1819 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1820 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1821 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1822 if (err < 0)
1823 return err;
1824 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1825 snd_pcm_hw_rule_div, NULL,
1826 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1827 if (err < 0)
1828 return err;
1829 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1830 snd_pcm_hw_rule_div, NULL,
1831 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1832 if (err < 0)
1833 return err;
1834 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1835 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1836 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1837 if (err < 0)
1838 return err;
1839 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1840 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1841 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1842 if (err < 0)
1843 return err;
1844 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1845 snd_pcm_hw_rule_mul, NULL,
1846 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1847 if (err < 0)
1848 return err;
1849 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1850 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1851 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1852 if (err < 0)
1853 return err;
1854 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1855 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1856 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1857 if (err < 0)
1858 return err;
1859 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1860 snd_pcm_hw_rule_muldivk, (void*) 8,
1861 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1862 if (err < 0)
1863 return err;
1864 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1865 snd_pcm_hw_rule_muldivk, (void*) 8,
1866 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1867 if (err < 0)
1868 return err;
1869 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1870 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1871 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1872 if (err < 0)
1873 return err;
1874 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1875 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1876 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1877 if (err < 0)
1878 return err;
1879 return 0;
1882 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1884 struct snd_pcm_runtime *runtime = substream->runtime;
1885 struct snd_pcm_hardware *hw = &runtime->hw;
1886 int err;
1887 unsigned int mask = 0;
1889 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1890 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1891 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1892 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1893 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1894 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1895 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1896 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1897 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1898 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1899 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1901 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1902 if (err < 0)
1903 return err;
1905 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1906 if (err < 0)
1907 return err;
1909 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1910 if (err < 0)
1911 return err;
1913 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1914 hw->channels_min, hw->channels_max);
1915 if (err < 0)
1916 return err;
1918 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1919 hw->rate_min, hw->rate_max);
1920 if (err < 0)
1921 return err;
1923 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1924 hw->period_bytes_min, hw->period_bytes_max);
1925 if (err < 0)
1926 return err;
1928 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1929 hw->periods_min, hw->periods_max);
1930 if (err < 0)
1931 return err;
1933 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1934 hw->period_bytes_min, hw->buffer_bytes_max);
1935 if (err < 0)
1936 return err;
1938 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1939 snd_pcm_hw_rule_buffer_bytes_max, substream,
1940 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1941 if (err < 0)
1942 return err;
1944 /* FIXME: remove */
1945 if (runtime->dma_bytes) {
1946 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1947 if (err < 0)
1948 return -EINVAL;
1951 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1952 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1953 snd_pcm_hw_rule_rate, hw,
1954 SNDRV_PCM_HW_PARAM_RATE, -1);
1955 if (err < 0)
1956 return err;
1959 /* FIXME: this belong to lowlevel */
1960 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1962 return 0;
1965 static void pcm_release_private(struct snd_pcm_substream *substream)
1967 snd_pcm_unlink(substream);
1970 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1972 substream->ref_count--;
1973 if (substream->ref_count > 0)
1974 return;
1976 snd_pcm_drop(substream);
1977 if (substream->hw_opened) {
1978 if (substream->ops->hw_free != NULL)
1979 substream->ops->hw_free(substream);
1980 substream->ops->close(substream);
1981 substream->hw_opened = 0;
1983 if (substream->pcm_release) {
1984 substream->pcm_release(substream);
1985 substream->pcm_release = NULL;
1987 snd_pcm_detach_substream(substream);
1990 EXPORT_SYMBOL(snd_pcm_release_substream);
1992 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1993 struct file *file,
1994 struct snd_pcm_substream **rsubstream)
1996 struct snd_pcm_substream *substream;
1997 int err;
1999 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2000 if (err < 0)
2001 return err;
2002 if (substream->ref_count > 1) {
2003 *rsubstream = substream;
2004 return 0;
2007 err = snd_pcm_hw_constraints_init(substream);
2008 if (err < 0) {
2009 snd_printd("snd_pcm_hw_constraints_init failed\n");
2010 goto error;
2013 if ((err = substream->ops->open(substream)) < 0)
2014 goto error;
2016 substream->hw_opened = 1;
2018 err = snd_pcm_hw_constraints_complete(substream);
2019 if (err < 0) {
2020 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2021 goto error;
2024 *rsubstream = substream;
2025 return 0;
2027 error:
2028 snd_pcm_release_substream(substream);
2029 return err;
2032 EXPORT_SYMBOL(snd_pcm_open_substream);
2034 static int snd_pcm_open_file(struct file *file,
2035 struct snd_pcm *pcm,
2036 int stream,
2037 struct snd_pcm_file **rpcm_file)
2039 struct snd_pcm_file *pcm_file;
2040 struct snd_pcm_substream *substream;
2041 struct snd_pcm_str *str;
2042 int err;
2044 if (rpcm_file)
2045 *rpcm_file = NULL;
2047 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2048 if (err < 0)
2049 return err;
2051 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2052 if (pcm_file == NULL) {
2053 snd_pcm_release_substream(substream);
2054 return -ENOMEM;
2056 pcm_file->substream = substream;
2057 if (substream->ref_count == 1) {
2058 str = substream->pstr;
2059 substream->file = pcm_file;
2060 substream->pcm_release = pcm_release_private;
2062 file->private_data = pcm_file;
2063 if (rpcm_file)
2064 *rpcm_file = pcm_file;
2065 return 0;
2068 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2070 struct snd_pcm *pcm;
2072 pcm = snd_lookup_minor_data(iminor(inode),
2073 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2074 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2077 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2079 struct snd_pcm *pcm;
2081 pcm = snd_lookup_minor_data(iminor(inode),
2082 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2083 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2086 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2088 int err;
2089 struct snd_pcm_file *pcm_file;
2090 wait_queue_t wait;
2092 if (pcm == NULL) {
2093 err = -ENODEV;
2094 goto __error1;
2096 err = snd_card_file_add(pcm->card, file);
2097 if (err < 0)
2098 goto __error1;
2099 if (!try_module_get(pcm->card->module)) {
2100 err = -EFAULT;
2101 goto __error2;
2103 init_waitqueue_entry(&wait, current);
2104 add_wait_queue(&pcm->open_wait, &wait);
2105 mutex_lock(&pcm->open_mutex);
2106 while (1) {
2107 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2108 if (err >= 0)
2109 break;
2110 if (err == -EAGAIN) {
2111 if (file->f_flags & O_NONBLOCK) {
2112 err = -EBUSY;
2113 break;
2115 } else
2116 break;
2117 set_current_state(TASK_INTERRUPTIBLE);
2118 mutex_unlock(&pcm->open_mutex);
2119 schedule();
2120 mutex_lock(&pcm->open_mutex);
2121 if (signal_pending(current)) {
2122 err = -ERESTARTSYS;
2123 break;
2126 remove_wait_queue(&pcm->open_wait, &wait);
2127 mutex_unlock(&pcm->open_mutex);
2128 if (err < 0)
2129 goto __error;
2130 return err;
2132 __error:
2133 module_put(pcm->card->module);
2134 __error2:
2135 snd_card_file_remove(pcm->card, file);
2136 __error1:
2137 return err;
2140 static int snd_pcm_release(struct inode *inode, struct file *file)
2142 struct snd_pcm *pcm;
2143 struct snd_pcm_substream *substream;
2144 struct snd_pcm_file *pcm_file;
2146 pcm_file = file->private_data;
2147 substream = pcm_file->substream;
2148 if (snd_BUG_ON(!substream))
2149 return -ENXIO;
2150 pcm = substream->pcm;
2151 mutex_lock(&pcm->open_mutex);
2152 snd_pcm_release_substream(substream);
2153 kfree(pcm_file);
2154 mutex_unlock(&pcm->open_mutex);
2155 wake_up(&pcm->open_wait);
2156 module_put(pcm->card->module);
2157 snd_card_file_remove(pcm->card, file);
2158 return 0;
2161 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2162 snd_pcm_uframes_t frames)
2164 struct snd_pcm_runtime *runtime = substream->runtime;
2165 snd_pcm_sframes_t appl_ptr;
2166 snd_pcm_sframes_t ret;
2167 snd_pcm_sframes_t hw_avail;
2169 if (frames == 0)
2170 return 0;
2172 snd_pcm_stream_lock_irq(substream);
2173 switch (runtime->status->state) {
2174 case SNDRV_PCM_STATE_PREPARED:
2175 break;
2176 case SNDRV_PCM_STATE_DRAINING:
2177 case SNDRV_PCM_STATE_RUNNING:
2178 if (snd_pcm_update_hw_ptr(substream) >= 0)
2179 break;
2180 /* Fall through */
2181 case SNDRV_PCM_STATE_XRUN:
2182 ret = -EPIPE;
2183 goto __end;
2184 case SNDRV_PCM_STATE_SUSPENDED:
2185 ret = -ESTRPIPE;
2186 goto __end;
2187 default:
2188 ret = -EBADFD;
2189 goto __end;
2192 hw_avail = snd_pcm_playback_hw_avail(runtime);
2193 if (hw_avail <= 0) {
2194 ret = 0;
2195 goto __end;
2197 if (frames > (snd_pcm_uframes_t)hw_avail)
2198 frames = hw_avail;
2199 appl_ptr = runtime->control->appl_ptr - frames;
2200 if (appl_ptr < 0)
2201 appl_ptr += runtime->boundary;
2202 runtime->control->appl_ptr = appl_ptr;
2203 ret = frames;
2204 __end:
2205 snd_pcm_stream_unlock_irq(substream);
2206 return ret;
2209 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2210 snd_pcm_uframes_t frames)
2212 struct snd_pcm_runtime *runtime = substream->runtime;
2213 snd_pcm_sframes_t appl_ptr;
2214 snd_pcm_sframes_t ret;
2215 snd_pcm_sframes_t hw_avail;
2217 if (frames == 0)
2218 return 0;
2220 snd_pcm_stream_lock_irq(substream);
2221 switch (runtime->status->state) {
2222 case SNDRV_PCM_STATE_PREPARED:
2223 case SNDRV_PCM_STATE_DRAINING:
2224 break;
2225 case SNDRV_PCM_STATE_RUNNING:
2226 if (snd_pcm_update_hw_ptr(substream) >= 0)
2227 break;
2228 /* Fall through */
2229 case SNDRV_PCM_STATE_XRUN:
2230 ret = -EPIPE;
2231 goto __end;
2232 case SNDRV_PCM_STATE_SUSPENDED:
2233 ret = -ESTRPIPE;
2234 goto __end;
2235 default:
2236 ret = -EBADFD;
2237 goto __end;
2240 hw_avail = snd_pcm_capture_hw_avail(runtime);
2241 if (hw_avail <= 0) {
2242 ret = 0;
2243 goto __end;
2245 if (frames > (snd_pcm_uframes_t)hw_avail)
2246 frames = hw_avail;
2247 appl_ptr = runtime->control->appl_ptr - frames;
2248 if (appl_ptr < 0)
2249 appl_ptr += runtime->boundary;
2250 runtime->control->appl_ptr = appl_ptr;
2251 ret = frames;
2252 __end:
2253 snd_pcm_stream_unlock_irq(substream);
2254 return ret;
2257 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2258 snd_pcm_uframes_t frames)
2260 struct snd_pcm_runtime *runtime = substream->runtime;
2261 snd_pcm_sframes_t appl_ptr;
2262 snd_pcm_sframes_t ret;
2263 snd_pcm_sframes_t avail;
2265 if (frames == 0)
2266 return 0;
2268 snd_pcm_stream_lock_irq(substream);
2269 switch (runtime->status->state) {
2270 case SNDRV_PCM_STATE_PREPARED:
2271 case SNDRV_PCM_STATE_PAUSED:
2272 break;
2273 case SNDRV_PCM_STATE_DRAINING:
2274 case SNDRV_PCM_STATE_RUNNING:
2275 if (snd_pcm_update_hw_ptr(substream) >= 0)
2276 break;
2277 /* Fall through */
2278 case SNDRV_PCM_STATE_XRUN:
2279 ret = -EPIPE;
2280 goto __end;
2281 case SNDRV_PCM_STATE_SUSPENDED:
2282 ret = -ESTRPIPE;
2283 goto __end;
2284 default:
2285 ret = -EBADFD;
2286 goto __end;
2289 avail = snd_pcm_playback_avail(runtime);
2290 if (avail <= 0) {
2291 ret = 0;
2292 goto __end;
2294 if (frames > (snd_pcm_uframes_t)avail)
2295 frames = avail;
2296 appl_ptr = runtime->control->appl_ptr + frames;
2297 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2298 appl_ptr -= runtime->boundary;
2299 runtime->control->appl_ptr = appl_ptr;
2300 ret = frames;
2301 __end:
2302 snd_pcm_stream_unlock_irq(substream);
2303 return ret;
2306 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2307 snd_pcm_uframes_t frames)
2309 struct snd_pcm_runtime *runtime = substream->runtime;
2310 snd_pcm_sframes_t appl_ptr;
2311 snd_pcm_sframes_t ret;
2312 snd_pcm_sframes_t avail;
2314 if (frames == 0)
2315 return 0;
2317 snd_pcm_stream_lock_irq(substream);
2318 switch (runtime->status->state) {
2319 case SNDRV_PCM_STATE_PREPARED:
2320 case SNDRV_PCM_STATE_DRAINING:
2321 case SNDRV_PCM_STATE_PAUSED:
2322 break;
2323 case SNDRV_PCM_STATE_RUNNING:
2324 if (snd_pcm_update_hw_ptr(substream) >= 0)
2325 break;
2326 /* Fall through */
2327 case SNDRV_PCM_STATE_XRUN:
2328 ret = -EPIPE;
2329 goto __end;
2330 case SNDRV_PCM_STATE_SUSPENDED:
2331 ret = -ESTRPIPE;
2332 goto __end;
2333 default:
2334 ret = -EBADFD;
2335 goto __end;
2338 avail = snd_pcm_capture_avail(runtime);
2339 if (avail <= 0) {
2340 ret = 0;
2341 goto __end;
2343 if (frames > (snd_pcm_uframes_t)avail)
2344 frames = avail;
2345 appl_ptr = runtime->control->appl_ptr + frames;
2346 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2347 appl_ptr -= runtime->boundary;
2348 runtime->control->appl_ptr = appl_ptr;
2349 ret = frames;
2350 __end:
2351 snd_pcm_stream_unlock_irq(substream);
2352 return ret;
2355 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2357 struct snd_pcm_runtime *runtime = substream->runtime;
2358 int err;
2360 snd_pcm_stream_lock_irq(substream);
2361 switch (runtime->status->state) {
2362 case SNDRV_PCM_STATE_DRAINING:
2363 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2364 goto __badfd;
2365 case SNDRV_PCM_STATE_RUNNING:
2366 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2367 break;
2368 /* Fall through */
2369 case SNDRV_PCM_STATE_PREPARED:
2370 case SNDRV_PCM_STATE_SUSPENDED:
2371 err = 0;
2372 break;
2373 case SNDRV_PCM_STATE_XRUN:
2374 err = -EPIPE;
2375 break;
2376 default:
2377 __badfd:
2378 err = -EBADFD;
2379 break;
2381 snd_pcm_stream_unlock_irq(substream);
2382 return err;
2385 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2386 snd_pcm_sframes_t __user *res)
2388 struct snd_pcm_runtime *runtime = substream->runtime;
2389 int err;
2390 snd_pcm_sframes_t n = 0;
2392 snd_pcm_stream_lock_irq(substream);
2393 switch (runtime->status->state) {
2394 case SNDRV_PCM_STATE_DRAINING:
2395 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2396 goto __badfd;
2397 case SNDRV_PCM_STATE_RUNNING:
2398 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2399 break;
2400 /* Fall through */
2401 case SNDRV_PCM_STATE_PREPARED:
2402 case SNDRV_PCM_STATE_SUSPENDED:
2403 err = 0;
2404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2405 n = snd_pcm_playback_hw_avail(runtime);
2406 else
2407 n = snd_pcm_capture_avail(runtime);
2408 n += runtime->delay;
2409 break;
2410 case SNDRV_PCM_STATE_XRUN:
2411 err = -EPIPE;
2412 break;
2413 default:
2414 __badfd:
2415 err = -EBADFD;
2416 break;
2418 snd_pcm_stream_unlock_irq(substream);
2419 if (!err)
2420 if (put_user(n, res))
2421 err = -EFAULT;
2422 return err;
2425 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2426 struct snd_pcm_sync_ptr __user *_sync_ptr)
2428 struct snd_pcm_runtime *runtime = substream->runtime;
2429 struct snd_pcm_sync_ptr sync_ptr;
2430 volatile struct snd_pcm_mmap_status *status;
2431 volatile struct snd_pcm_mmap_control *control;
2432 int err;
2434 memset(&sync_ptr, 0, sizeof(sync_ptr));
2435 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2436 return -EFAULT;
2437 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2438 return -EFAULT;
2439 status = runtime->status;
2440 control = runtime->control;
2441 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2442 err = snd_pcm_hwsync(substream);
2443 if (err < 0)
2444 return err;
2446 snd_pcm_stream_lock_irq(substream);
2447 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2448 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2449 else
2450 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2452 control->avail_min = sync_ptr.c.control.avail_min;
2453 else
2454 sync_ptr.c.control.avail_min = control->avail_min;
2455 sync_ptr.s.status.state = status->state;
2456 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2457 sync_ptr.s.status.tstamp = status->tstamp;
2458 sync_ptr.s.status.suspended_state = status->suspended_state;
2459 snd_pcm_stream_unlock_irq(substream);
2460 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2461 return -EFAULT;
2462 return 0;
2465 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2467 struct snd_pcm_runtime *runtime = substream->runtime;
2468 int arg;
2470 if (get_user(arg, _arg))
2471 return -EFAULT;
2472 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2473 return -EINVAL;
2474 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2475 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2476 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2477 return 0;
2480 static int snd_pcm_common_ioctl1(struct file *file,
2481 struct snd_pcm_substream *substream,
2482 unsigned int cmd, void __user *arg)
2484 switch (cmd) {
2485 case SNDRV_PCM_IOCTL_PVERSION:
2486 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2487 case SNDRV_PCM_IOCTL_INFO:
2488 return snd_pcm_info_user(substream, arg);
2489 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2490 return 0;
2491 case SNDRV_PCM_IOCTL_TTSTAMP:
2492 return snd_pcm_tstamp(substream, arg);
2493 case SNDRV_PCM_IOCTL_HW_REFINE:
2494 return snd_pcm_hw_refine_user(substream, arg);
2495 case SNDRV_PCM_IOCTL_HW_PARAMS:
2496 return snd_pcm_hw_params_user(substream, arg);
2497 case SNDRV_PCM_IOCTL_HW_FREE:
2498 return snd_pcm_hw_free(substream);
2499 case SNDRV_PCM_IOCTL_SW_PARAMS:
2500 return snd_pcm_sw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_STATUS:
2502 return snd_pcm_status_user(substream, arg);
2503 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2504 return snd_pcm_channel_info_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_PREPARE:
2506 return snd_pcm_prepare(substream, file);
2507 case SNDRV_PCM_IOCTL_RESET:
2508 return snd_pcm_reset(substream);
2509 case SNDRV_PCM_IOCTL_START:
2510 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2511 case SNDRV_PCM_IOCTL_LINK:
2512 return snd_pcm_link(substream, (int)(unsigned long) arg);
2513 case SNDRV_PCM_IOCTL_UNLINK:
2514 return snd_pcm_unlink(substream);
2515 case SNDRV_PCM_IOCTL_RESUME:
2516 return snd_pcm_resume(substream);
2517 case SNDRV_PCM_IOCTL_XRUN:
2518 return snd_pcm_xrun(substream);
2519 case SNDRV_PCM_IOCTL_HWSYNC:
2520 return snd_pcm_hwsync(substream);
2521 case SNDRV_PCM_IOCTL_DELAY:
2522 return snd_pcm_delay(substream, arg);
2523 case SNDRV_PCM_IOCTL_SYNC_PTR:
2524 return snd_pcm_sync_ptr(substream, arg);
2525 #ifdef CONFIG_SND_SUPPORT_OLD_API
2526 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2527 return snd_pcm_hw_refine_old_user(substream, arg);
2528 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2529 return snd_pcm_hw_params_old_user(substream, arg);
2530 #endif
2531 case SNDRV_PCM_IOCTL_DRAIN:
2532 return snd_pcm_drain(substream, file);
2533 case SNDRV_PCM_IOCTL_DROP:
2534 return snd_pcm_drop(substream);
2535 case SNDRV_PCM_IOCTL_PAUSE:
2537 int res;
2538 snd_pcm_stream_lock_irq(substream);
2539 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2540 snd_pcm_stream_unlock_irq(substream);
2541 return res;
2544 snd_printd("unknown ioctl = 0x%x\n", cmd);
2545 return -ENOTTY;
2548 static int snd_pcm_playback_ioctl1(struct file *file,
2549 struct snd_pcm_substream *substream,
2550 unsigned int cmd, void __user *arg)
2552 if (snd_BUG_ON(!substream))
2553 return -ENXIO;
2554 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2555 return -EINVAL;
2556 switch (cmd) {
2557 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2559 struct snd_xferi xferi;
2560 struct snd_xferi __user *_xferi = arg;
2561 struct snd_pcm_runtime *runtime = substream->runtime;
2562 snd_pcm_sframes_t result;
2563 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2564 return -EBADFD;
2565 if (put_user(0, &_xferi->result))
2566 return -EFAULT;
2567 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2568 return -EFAULT;
2569 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2570 __put_user(result, &_xferi->result);
2571 return result < 0 ? result : 0;
2573 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2575 struct snd_xfern xfern;
2576 struct snd_xfern __user *_xfern = arg;
2577 struct snd_pcm_runtime *runtime = substream->runtime;
2578 void __user **bufs;
2579 snd_pcm_sframes_t result;
2580 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2581 return -EBADFD;
2582 if (runtime->channels > 128)
2583 return -EINVAL;
2584 if (put_user(0, &_xfern->result))
2585 return -EFAULT;
2586 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2587 return -EFAULT;
2589 bufs = memdup_user(xfern.bufs,
2590 sizeof(void *) * runtime->channels);
2591 if (IS_ERR(bufs))
2592 return PTR_ERR(bufs);
2593 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2594 kfree(bufs);
2595 __put_user(result, &_xfern->result);
2596 return result < 0 ? result : 0;
2598 case SNDRV_PCM_IOCTL_REWIND:
2600 snd_pcm_uframes_t frames;
2601 snd_pcm_uframes_t __user *_frames = arg;
2602 snd_pcm_sframes_t result;
2603 if (get_user(frames, _frames))
2604 return -EFAULT;
2605 if (put_user(0, _frames))
2606 return -EFAULT;
2607 result = snd_pcm_playback_rewind(substream, frames);
2608 __put_user(result, _frames);
2609 return result < 0 ? result : 0;
2611 case SNDRV_PCM_IOCTL_FORWARD:
2613 snd_pcm_uframes_t frames;
2614 snd_pcm_uframes_t __user *_frames = arg;
2615 snd_pcm_sframes_t result;
2616 if (get_user(frames, _frames))
2617 return -EFAULT;
2618 if (put_user(0, _frames))
2619 return -EFAULT;
2620 result = snd_pcm_playback_forward(substream, frames);
2621 __put_user(result, _frames);
2622 return result < 0 ? result : 0;
2625 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2628 static int snd_pcm_capture_ioctl1(struct file *file,
2629 struct snd_pcm_substream *substream,
2630 unsigned int cmd, void __user *arg)
2632 if (snd_BUG_ON(!substream))
2633 return -ENXIO;
2634 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2635 return -EINVAL;
2636 switch (cmd) {
2637 case SNDRV_PCM_IOCTL_READI_FRAMES:
2639 struct snd_xferi xferi;
2640 struct snd_xferi __user *_xferi = arg;
2641 struct snd_pcm_runtime *runtime = substream->runtime;
2642 snd_pcm_sframes_t result;
2643 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2644 return -EBADFD;
2645 if (put_user(0, &_xferi->result))
2646 return -EFAULT;
2647 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2648 return -EFAULT;
2649 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2650 __put_user(result, &_xferi->result);
2651 return result < 0 ? result : 0;
2653 case SNDRV_PCM_IOCTL_READN_FRAMES:
2655 struct snd_xfern xfern;
2656 struct snd_xfern __user *_xfern = arg;
2657 struct snd_pcm_runtime *runtime = substream->runtime;
2658 void *bufs;
2659 snd_pcm_sframes_t result;
2660 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2661 return -EBADFD;
2662 if (runtime->channels > 128)
2663 return -EINVAL;
2664 if (put_user(0, &_xfern->result))
2665 return -EFAULT;
2666 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2667 return -EFAULT;
2669 bufs = memdup_user(xfern.bufs,
2670 sizeof(void *) * runtime->channels);
2671 if (IS_ERR(bufs))
2672 return PTR_ERR(bufs);
2673 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2674 kfree(bufs);
2675 __put_user(result, &_xfern->result);
2676 return result < 0 ? result : 0;
2678 case SNDRV_PCM_IOCTL_REWIND:
2680 snd_pcm_uframes_t frames;
2681 snd_pcm_uframes_t __user *_frames = arg;
2682 snd_pcm_sframes_t result;
2683 if (get_user(frames, _frames))
2684 return -EFAULT;
2685 if (put_user(0, _frames))
2686 return -EFAULT;
2687 result = snd_pcm_capture_rewind(substream, frames);
2688 __put_user(result, _frames);
2689 return result < 0 ? result : 0;
2691 case SNDRV_PCM_IOCTL_FORWARD:
2693 snd_pcm_uframes_t frames;
2694 snd_pcm_uframes_t __user *_frames = arg;
2695 snd_pcm_sframes_t result;
2696 if (get_user(frames, _frames))
2697 return -EFAULT;
2698 if (put_user(0, _frames))
2699 return -EFAULT;
2700 result = snd_pcm_capture_forward(substream, frames);
2701 __put_user(result, _frames);
2702 return result < 0 ? result : 0;
2705 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2708 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2709 unsigned long arg)
2711 struct snd_pcm_file *pcm_file;
2713 pcm_file = file->private_data;
2715 if (((cmd >> 8) & 0xff) != 'A')
2716 return -ENOTTY;
2718 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2719 (void __user *)arg);
2722 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2723 unsigned long arg)
2725 struct snd_pcm_file *pcm_file;
2727 pcm_file = file->private_data;
2729 if (((cmd >> 8) & 0xff) != 'A')
2730 return -ENOTTY;
2732 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2733 (void __user *)arg);
2736 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2737 unsigned int cmd, void *arg)
2739 mm_segment_t fs;
2740 int result;
2742 fs = snd_enter_user();
2743 switch (substream->stream) {
2744 case SNDRV_PCM_STREAM_PLAYBACK:
2745 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2746 (void __user *)arg);
2747 break;
2748 case SNDRV_PCM_STREAM_CAPTURE:
2749 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2750 (void __user *)arg);
2751 break;
2752 default:
2753 result = -EINVAL;
2754 break;
2756 snd_leave_user(fs);
2757 return result;
2760 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2762 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2763 loff_t * offset)
2765 struct snd_pcm_file *pcm_file;
2766 struct snd_pcm_substream *substream;
2767 struct snd_pcm_runtime *runtime;
2768 snd_pcm_sframes_t result;
2770 pcm_file = file->private_data;
2771 substream = pcm_file->substream;
2772 if (PCM_RUNTIME_CHECK(substream))
2773 return -ENXIO;
2774 runtime = substream->runtime;
2775 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2776 return -EBADFD;
2777 if (!frame_aligned(runtime, count))
2778 return -EINVAL;
2779 count = bytes_to_frames(runtime, count);
2780 result = snd_pcm_lib_read(substream, buf, count);
2781 if (result > 0)
2782 result = frames_to_bytes(runtime, result);
2783 return result;
2786 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2787 size_t count, loff_t * offset)
2789 struct snd_pcm_file *pcm_file;
2790 struct snd_pcm_substream *substream;
2791 struct snd_pcm_runtime *runtime;
2792 snd_pcm_sframes_t result;
2794 pcm_file = file->private_data;
2795 substream = pcm_file->substream;
2796 if (PCM_RUNTIME_CHECK(substream))
2797 return -ENXIO;
2798 runtime = substream->runtime;
2799 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2800 return -EBADFD;
2801 if (!frame_aligned(runtime, count))
2802 return -EINVAL;
2803 count = bytes_to_frames(runtime, count);
2804 result = snd_pcm_lib_write(substream, buf, count);
2805 if (result > 0)
2806 result = frames_to_bytes(runtime, result);
2807 return result;
2810 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2811 unsigned long nr_segs, loff_t pos)
2814 struct snd_pcm_file *pcm_file;
2815 struct snd_pcm_substream *substream;
2816 struct snd_pcm_runtime *runtime;
2817 snd_pcm_sframes_t result;
2818 unsigned long i;
2819 void __user **bufs;
2820 snd_pcm_uframes_t frames;
2822 pcm_file = iocb->ki_filp->private_data;
2823 substream = pcm_file->substream;
2824 if (PCM_RUNTIME_CHECK(substream))
2825 return -ENXIO;
2826 runtime = substream->runtime;
2827 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2828 return -EBADFD;
2829 if (nr_segs > 1024 || nr_segs != runtime->channels)
2830 return -EINVAL;
2831 if (!frame_aligned(runtime, iov->iov_len))
2832 return -EINVAL;
2833 frames = bytes_to_samples(runtime, iov->iov_len);
2834 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2835 if (bufs == NULL)
2836 return -ENOMEM;
2837 for (i = 0; i < nr_segs; ++i)
2838 bufs[i] = iov[i].iov_base;
2839 result = snd_pcm_lib_readv(substream, bufs, frames);
2840 if (result > 0)
2841 result = frames_to_bytes(runtime, result);
2842 kfree(bufs);
2843 return result;
2846 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2847 unsigned long nr_segs, loff_t pos)
2849 struct snd_pcm_file *pcm_file;
2850 struct snd_pcm_substream *substream;
2851 struct snd_pcm_runtime *runtime;
2852 snd_pcm_sframes_t result;
2853 unsigned long i;
2854 void __user **bufs;
2855 snd_pcm_uframes_t frames;
2857 pcm_file = iocb->ki_filp->private_data;
2858 substream = pcm_file->substream;
2859 if (PCM_RUNTIME_CHECK(substream))
2860 return -ENXIO;
2861 runtime = substream->runtime;
2862 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2863 return -EBADFD;
2864 if (nr_segs > 128 || nr_segs != runtime->channels ||
2865 !frame_aligned(runtime, iov->iov_len))
2866 return -EINVAL;
2867 frames = bytes_to_samples(runtime, iov->iov_len);
2868 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2869 if (bufs == NULL)
2870 return -ENOMEM;
2871 for (i = 0; i < nr_segs; ++i)
2872 bufs[i] = iov[i].iov_base;
2873 result = snd_pcm_lib_writev(substream, bufs, frames);
2874 if (result > 0)
2875 result = frames_to_bytes(runtime, result);
2876 kfree(bufs);
2877 return result;
2880 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2882 struct snd_pcm_file *pcm_file;
2883 struct snd_pcm_substream *substream;
2884 struct snd_pcm_runtime *runtime;
2885 unsigned int mask;
2886 snd_pcm_uframes_t avail;
2888 pcm_file = file->private_data;
2890 substream = pcm_file->substream;
2891 if (PCM_RUNTIME_CHECK(substream))
2892 return -ENXIO;
2893 runtime = substream->runtime;
2895 poll_wait(file, &runtime->sleep, wait);
2897 snd_pcm_stream_lock_irq(substream);
2898 avail = snd_pcm_playback_avail(runtime);
2899 switch (runtime->status->state) {
2900 case SNDRV_PCM_STATE_RUNNING:
2901 case SNDRV_PCM_STATE_PREPARED:
2902 case SNDRV_PCM_STATE_PAUSED:
2903 if (avail >= runtime->control->avail_min) {
2904 mask = POLLOUT | POLLWRNORM;
2905 break;
2907 /* Fall through */
2908 case SNDRV_PCM_STATE_DRAINING:
2909 mask = 0;
2910 break;
2911 default:
2912 mask = POLLOUT | POLLWRNORM | POLLERR;
2913 break;
2915 snd_pcm_stream_unlock_irq(substream);
2916 return mask;
2919 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2921 struct snd_pcm_file *pcm_file;
2922 struct snd_pcm_substream *substream;
2923 struct snd_pcm_runtime *runtime;
2924 unsigned int mask;
2925 snd_pcm_uframes_t avail;
2927 pcm_file = file->private_data;
2929 substream = pcm_file->substream;
2930 if (PCM_RUNTIME_CHECK(substream))
2931 return -ENXIO;
2932 runtime = substream->runtime;
2934 poll_wait(file, &runtime->sleep, wait);
2936 snd_pcm_stream_lock_irq(substream);
2937 avail = snd_pcm_capture_avail(runtime);
2938 switch (runtime->status->state) {
2939 case SNDRV_PCM_STATE_RUNNING:
2940 case SNDRV_PCM_STATE_PREPARED:
2941 case SNDRV_PCM_STATE_PAUSED:
2942 if (avail >= runtime->control->avail_min) {
2943 mask = POLLIN | POLLRDNORM;
2944 break;
2946 mask = 0;
2947 break;
2948 case SNDRV_PCM_STATE_DRAINING:
2949 if (avail > 0) {
2950 mask = POLLIN | POLLRDNORM;
2951 break;
2953 /* Fall through */
2954 default:
2955 mask = POLLIN | POLLRDNORM | POLLERR;
2956 break;
2958 snd_pcm_stream_unlock_irq(substream);
2959 return mask;
2963 * mmap support
2967 * Only on coherent architectures, we can mmap the status and the control records
2968 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2970 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2972 * mmap status record
2974 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2975 struct vm_fault *vmf)
2977 struct snd_pcm_substream *substream = area->vm_private_data;
2978 struct snd_pcm_runtime *runtime;
2980 if (substream == NULL)
2981 return VM_FAULT_SIGBUS;
2982 runtime = substream->runtime;
2983 vmf->page = virt_to_page(runtime->status);
2984 get_page(vmf->page);
2985 return 0;
2988 static const struct vm_operations_struct snd_pcm_vm_ops_status =
2990 .fault = snd_pcm_mmap_status_fault,
2993 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
2994 struct vm_area_struct *area)
2996 struct snd_pcm_runtime *runtime;
2997 long size;
2998 if (!(area->vm_flags & VM_READ))
2999 return -EINVAL;
3000 runtime = substream->runtime;
3001 size = area->vm_end - area->vm_start;
3002 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3003 return -EINVAL;
3004 area->vm_ops = &snd_pcm_vm_ops_status;
3005 area->vm_private_data = substream;
3006 area->vm_flags |= VM_RESERVED;
3007 return 0;
3011 * mmap control record
3013 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3014 struct vm_fault *vmf)
3016 struct snd_pcm_substream *substream = area->vm_private_data;
3017 struct snd_pcm_runtime *runtime;
3019 if (substream == NULL)
3020 return VM_FAULT_SIGBUS;
3021 runtime = substream->runtime;
3022 vmf->page = virt_to_page(runtime->control);
3023 get_page(vmf->page);
3024 return 0;
3027 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3029 .fault = snd_pcm_mmap_control_fault,
3032 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3033 struct vm_area_struct *area)
3035 struct snd_pcm_runtime *runtime;
3036 long size;
3037 if (!(area->vm_flags & VM_READ))
3038 return -EINVAL;
3039 runtime = substream->runtime;
3040 size = area->vm_end - area->vm_start;
3041 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3042 return -EINVAL;
3043 area->vm_ops = &snd_pcm_vm_ops_control;
3044 area->vm_private_data = substream;
3045 area->vm_flags |= VM_RESERVED;
3046 return 0;
3048 #else /* ! coherent mmap */
3050 * don't support mmap for status and control records.
3052 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3053 struct vm_area_struct *area)
3055 return -ENXIO;
3057 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3058 struct vm_area_struct *area)
3060 return -ENXIO;
3062 #endif /* coherent mmap */
3065 * fault callback for mmapping a RAM page
3067 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3068 struct vm_fault *vmf)
3070 struct snd_pcm_substream *substream = area->vm_private_data;
3071 struct snd_pcm_runtime *runtime;
3072 unsigned long offset;
3073 struct page * page;
3074 void *vaddr;
3075 size_t dma_bytes;
3077 if (substream == NULL)
3078 return VM_FAULT_SIGBUS;
3079 runtime = substream->runtime;
3080 offset = vmf->pgoff << PAGE_SHIFT;
3081 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3082 if (offset > dma_bytes - PAGE_SIZE)
3083 return VM_FAULT_SIGBUS;
3084 if (substream->ops->page) {
3085 page = substream->ops->page(substream, offset);
3086 if (!page)
3087 return VM_FAULT_SIGBUS;
3088 } else {
3089 vaddr = runtime->dma_area + offset;
3090 page = virt_to_page(vaddr);
3092 get_page(page);
3093 vmf->page = page;
3094 return 0;
3097 static const struct vm_operations_struct snd_pcm_vm_ops_data =
3099 .open = snd_pcm_mmap_data_open,
3100 .close = snd_pcm_mmap_data_close,
3101 .fault = snd_pcm_mmap_data_fault,
3105 * mmap the DMA buffer on RAM
3107 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3108 struct vm_area_struct *area)
3110 area->vm_ops = &snd_pcm_vm_ops_data;
3111 area->vm_private_data = substream;
3112 area->vm_flags |= VM_RESERVED;
3113 atomic_inc(&substream->mmap_count);
3114 return 0;
3118 * mmap the DMA buffer on I/O memory area
3120 #if SNDRV_PCM_INFO_MMAP_IOMEM
3121 static const struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3123 .open = snd_pcm_mmap_data_open,
3124 .close = snd_pcm_mmap_data_close,
3127 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3128 struct vm_area_struct *area)
3130 long size;
3131 unsigned long offset;
3133 #ifdef pgprot_noncached
3134 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3135 #endif
3136 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3137 area->vm_private_data = substream;
3138 area->vm_flags |= VM_IO;
3139 size = area->vm_end - area->vm_start;
3140 offset = area->vm_pgoff << PAGE_SHIFT;
3141 if (io_remap_pfn_range(area, area->vm_start,
3142 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3143 size, area->vm_page_prot))
3144 return -EAGAIN;
3145 atomic_inc(&substream->mmap_count);
3146 return 0;
3149 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3150 #endif /* SNDRV_PCM_INFO_MMAP */
3153 * mmap DMA buffer
3155 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3156 struct vm_area_struct *area)
3158 struct snd_pcm_runtime *runtime;
3159 long size;
3160 unsigned long offset;
3161 size_t dma_bytes;
3163 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3164 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3165 return -EINVAL;
3166 } else {
3167 if (!(area->vm_flags & VM_READ))
3168 return -EINVAL;
3170 runtime = substream->runtime;
3171 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3172 return -EBADFD;
3173 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3174 return -ENXIO;
3175 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3176 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3177 return -EINVAL;
3178 size = area->vm_end - area->vm_start;
3179 offset = area->vm_pgoff << PAGE_SHIFT;
3180 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3181 if ((size_t)size > dma_bytes)
3182 return -EINVAL;
3183 if (offset > dma_bytes - size)
3184 return -EINVAL;
3186 if (substream->ops->mmap)
3187 return substream->ops->mmap(substream, area);
3188 else
3189 return snd_pcm_default_mmap(substream, area);
3192 EXPORT_SYMBOL(snd_pcm_mmap_data);
3194 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3196 struct snd_pcm_file * pcm_file;
3197 struct snd_pcm_substream *substream;
3198 unsigned long offset;
3200 pcm_file = file->private_data;
3201 substream = pcm_file->substream;
3202 if (PCM_RUNTIME_CHECK(substream))
3203 return -ENXIO;
3205 offset = area->vm_pgoff << PAGE_SHIFT;
3206 switch (offset) {
3207 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3208 if (pcm_file->no_compat_mmap)
3209 return -ENXIO;
3210 return snd_pcm_mmap_status(substream, file, area);
3211 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3212 if (pcm_file->no_compat_mmap)
3213 return -ENXIO;
3214 return snd_pcm_mmap_control(substream, file, area);
3215 default:
3216 return snd_pcm_mmap_data(substream, file, area);
3218 return 0;
3221 static int snd_pcm_fasync(int fd, struct file * file, int on)
3223 struct snd_pcm_file * pcm_file;
3224 struct snd_pcm_substream *substream;
3225 struct snd_pcm_runtime *runtime;
3226 int err = -ENXIO;
3228 lock_kernel();
3229 pcm_file = file->private_data;
3230 substream = pcm_file->substream;
3231 if (PCM_RUNTIME_CHECK(substream))
3232 goto out;
3233 runtime = substream->runtime;
3234 err = fasync_helper(fd, file, on, &runtime->fasync);
3235 out:
3236 unlock_kernel();
3237 return err;
3241 * ioctl32 compat
3243 #ifdef CONFIG_COMPAT
3244 #include "pcm_compat.c"
3245 #else
3246 #define snd_pcm_ioctl_compat NULL
3247 #endif
3250 * To be removed helpers to keep binary compatibility
3253 #ifdef CONFIG_SND_SUPPORT_OLD_API
3254 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3255 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3257 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3258 struct snd_pcm_hw_params_old *oparams)
3260 unsigned int i;
3262 memset(params, 0, sizeof(*params));
3263 params->flags = oparams->flags;
3264 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3265 params->masks[i].bits[0] = oparams->masks[i];
3266 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3267 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3268 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3269 params->info = oparams->info;
3270 params->msbits = oparams->msbits;
3271 params->rate_num = oparams->rate_num;
3272 params->rate_den = oparams->rate_den;
3273 params->fifo_size = oparams->fifo_size;
3276 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3277 struct snd_pcm_hw_params *params)
3279 unsigned int i;
3281 memset(oparams, 0, sizeof(*oparams));
3282 oparams->flags = params->flags;
3283 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3284 oparams->masks[i] = params->masks[i].bits[0];
3285 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3286 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3287 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3288 oparams->info = params->info;
3289 oparams->msbits = params->msbits;
3290 oparams->rate_num = params->rate_num;
3291 oparams->rate_den = params->rate_den;
3292 oparams->fifo_size = params->fifo_size;
3295 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3296 struct snd_pcm_hw_params_old __user * _oparams)
3298 struct snd_pcm_hw_params *params;
3299 struct snd_pcm_hw_params_old *oparams = NULL;
3300 int err;
3302 params = kmalloc(sizeof(*params), GFP_KERNEL);
3303 if (!params)
3304 return -ENOMEM;
3306 oparams = memdup_user(_oparams, sizeof(*oparams));
3307 if (IS_ERR(oparams)) {
3308 err = PTR_ERR(oparams);
3309 goto out;
3311 snd_pcm_hw_convert_from_old_params(params, oparams);
3312 err = snd_pcm_hw_refine(substream, params);
3313 snd_pcm_hw_convert_to_old_params(oparams, params);
3314 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3315 if (!err)
3316 err = -EFAULT;
3319 kfree(oparams);
3320 out:
3321 kfree(params);
3322 return err;
3325 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3326 struct snd_pcm_hw_params_old __user * _oparams)
3328 struct snd_pcm_hw_params *params;
3329 struct snd_pcm_hw_params_old *oparams = NULL;
3330 int err;
3332 params = kmalloc(sizeof(*params), GFP_KERNEL);
3333 if (!params)
3334 return -ENOMEM;
3336 oparams = memdup_user(_oparams, sizeof(*oparams));
3337 if (IS_ERR(oparams)) {
3338 err = PTR_ERR(oparams);
3339 goto out;
3341 snd_pcm_hw_convert_from_old_params(params, oparams);
3342 err = snd_pcm_hw_params(substream, params);
3343 snd_pcm_hw_convert_to_old_params(oparams, params);
3344 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3345 if (!err)
3346 err = -EFAULT;
3349 kfree(oparams);
3350 out:
3351 kfree(params);
3352 return err;
3354 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3356 #ifndef CONFIG_MMU
3357 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3358 unsigned long len, unsigned long pgoff,
3359 unsigned long flags)
3361 return 0;
3363 #else
3364 # define dummy_get_unmapped_area NULL
3365 #endif
3368 * Register section
3371 const struct file_operations snd_pcm_f_ops[2] = {
3373 .owner = THIS_MODULE,
3374 .write = snd_pcm_write,
3375 .aio_write = snd_pcm_aio_write,
3376 .open = snd_pcm_playback_open,
3377 .release = snd_pcm_release,
3378 .poll = snd_pcm_playback_poll,
3379 .unlocked_ioctl = snd_pcm_playback_ioctl,
3380 .compat_ioctl = snd_pcm_ioctl_compat,
3381 .mmap = snd_pcm_mmap,
3382 .fasync = snd_pcm_fasync,
3383 .get_unmapped_area = dummy_get_unmapped_area,
3386 .owner = THIS_MODULE,
3387 .read = snd_pcm_read,
3388 .aio_read = snd_pcm_aio_read,
3389 .open = snd_pcm_capture_open,
3390 .release = snd_pcm_release,
3391 .poll = snd_pcm_capture_poll,
3392 .unlocked_ioctl = snd_pcm_capture_ioctl,
3393 .compat_ioctl = snd_pcm_ioctl_compat,
3394 .mmap = snd_pcm_mmap,
3395 .fasync = snd_pcm_fasync,
3396 .get_unmapped_area = dummy_get_unmapped_area,