initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / sound / core / pcm_native.c
blobe6d2d974d23143e42b24fbd1fff56f6cc2e23eb6
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 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
318 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
319 if (snd_mask_min(m) == snd_mask_max(m) &&
320 snd_interval_min(i) == snd_interval_max(i)) {
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 /* some drivers might use hw_ptr to recover from the pause -
976 update the hw_ptr now */
977 if (push)
978 snd_pcm_update_hw_ptr(substream);
979 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
980 * a delta betwen the current jiffies, this gives a large enough
981 * delta, effectively to skip the check once.
983 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
984 return substream->ops->trigger(substream,
985 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
986 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
989 static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
991 if (substream->runtime->trigger_master == substream)
992 substream->ops->trigger(substream,
993 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
994 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
997 static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
999 struct snd_pcm_runtime *runtime = substream->runtime;
1000 snd_pcm_trigger_tstamp(substream);
1001 if (push) {
1002 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1003 if (substream->timer)
1004 snd_timer_notify(substream->timer,
1005 SNDRV_TIMER_EVENT_MPAUSE,
1006 &runtime->trigger_tstamp);
1007 wake_up(&runtime->sleep);
1008 } else {
1009 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1010 if (substream->timer)
1011 snd_timer_notify(substream->timer,
1012 SNDRV_TIMER_EVENT_MCONTINUE,
1013 &runtime->trigger_tstamp);
1017 static struct action_ops snd_pcm_action_pause = {
1018 .pre_action = snd_pcm_pre_pause,
1019 .do_action = snd_pcm_do_pause,
1020 .undo_action = snd_pcm_undo_pause,
1021 .post_action = snd_pcm_post_pause
1025 * Push/release the pause for all linked streams.
1027 static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1029 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1032 #ifdef CONFIG_PM
1033 /* suspend */
1035 static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1039 return -EBUSY;
1040 runtime->trigger_master = substream;
1041 return 0;
1044 static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1046 struct snd_pcm_runtime *runtime = substream->runtime;
1047 if (runtime->trigger_master != substream)
1048 return 0;
1049 if (! snd_pcm_running(substream))
1050 return 0;
1051 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1052 return 0; /* suspend unconditionally */
1055 static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1057 struct snd_pcm_runtime *runtime = substream->runtime;
1058 snd_pcm_trigger_tstamp(substream);
1059 if (substream->timer)
1060 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1061 &runtime->trigger_tstamp);
1062 runtime->status->suspended_state = runtime->status->state;
1063 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1064 wake_up(&runtime->sleep);
1067 static struct action_ops snd_pcm_action_suspend = {
1068 .pre_action = snd_pcm_pre_suspend,
1069 .do_action = snd_pcm_do_suspend,
1070 .post_action = snd_pcm_post_suspend
1074 * snd_pcm_suspend - trigger SUSPEND to all linked streams
1075 * @substream: the PCM substream
1077 * After this call, all streams are changed to SUSPENDED state.
1079 int snd_pcm_suspend(struct snd_pcm_substream *substream)
1081 int err;
1082 unsigned long flags;
1084 if (! substream)
1085 return 0;
1087 snd_pcm_stream_lock_irqsave(substream, flags);
1088 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1089 snd_pcm_stream_unlock_irqrestore(substream, flags);
1090 return err;
1093 EXPORT_SYMBOL(snd_pcm_suspend);
1096 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
1097 * @pcm: the PCM instance
1099 * After this call, all streams are changed to SUSPENDED state.
1101 int snd_pcm_suspend_all(struct snd_pcm *pcm)
1103 struct snd_pcm_substream *substream;
1104 int stream, err = 0;
1106 if (! pcm)
1107 return 0;
1109 for (stream = 0; stream < 2; stream++) {
1110 for (substream = pcm->streams[stream].substream;
1111 substream; substream = substream->next) {
1112 /* FIXME: the open/close code should lock this as well */
1113 if (substream->runtime == NULL)
1114 continue;
1115 err = snd_pcm_suspend(substream);
1116 if (err < 0 && err != -EBUSY)
1117 return err;
1120 return 0;
1123 EXPORT_SYMBOL(snd_pcm_suspend_all);
1125 /* resume */
1127 static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1129 struct snd_pcm_runtime *runtime = substream->runtime;
1130 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1131 return -ENOSYS;
1132 runtime->trigger_master = substream;
1133 return 0;
1136 static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1138 struct snd_pcm_runtime *runtime = substream->runtime;
1139 if (runtime->trigger_master != substream)
1140 return 0;
1141 /* DMA not running previously? */
1142 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1143 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1144 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1145 return 0;
1146 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1149 static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1151 if (substream->runtime->trigger_master == substream &&
1152 snd_pcm_running(substream))
1153 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1156 static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1158 struct snd_pcm_runtime *runtime = substream->runtime;
1159 snd_pcm_trigger_tstamp(substream);
1160 if (substream->timer)
1161 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1162 &runtime->trigger_tstamp);
1163 runtime->status->state = runtime->status->suspended_state;
1166 static struct action_ops snd_pcm_action_resume = {
1167 .pre_action = snd_pcm_pre_resume,
1168 .do_action = snd_pcm_do_resume,
1169 .undo_action = snd_pcm_undo_resume,
1170 .post_action = snd_pcm_post_resume
1173 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1175 struct snd_card *card = substream->pcm->card;
1176 int res;
1178 snd_power_lock(card);
1179 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1180 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1181 snd_power_unlock(card);
1182 return res;
1185 #else
1187 static int snd_pcm_resume(struct snd_pcm_substream *substream)
1189 return -ENOSYS;
1192 #endif /* CONFIG_PM */
1195 * xrun ioctl
1197 * Change the RUNNING stream(s) to XRUN state.
1199 static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1201 struct snd_card *card = substream->pcm->card;
1202 struct snd_pcm_runtime *runtime = substream->runtime;
1203 int result;
1205 snd_power_lock(card);
1206 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1207 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1208 if (result < 0)
1209 goto _unlock;
1212 snd_pcm_stream_lock_irq(substream);
1213 switch (runtime->status->state) {
1214 case SNDRV_PCM_STATE_XRUN:
1215 result = 0; /* already there */
1216 break;
1217 case SNDRV_PCM_STATE_RUNNING:
1218 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1219 break;
1220 default:
1221 result = -EBADFD;
1223 snd_pcm_stream_unlock_irq(substream);
1224 _unlock:
1225 snd_power_unlock(card);
1226 return result;
1230 * reset ioctl
1232 static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1234 struct snd_pcm_runtime *runtime = substream->runtime;
1235 switch (runtime->status->state) {
1236 case SNDRV_PCM_STATE_RUNNING:
1237 case SNDRV_PCM_STATE_PREPARED:
1238 case SNDRV_PCM_STATE_PAUSED:
1239 case SNDRV_PCM_STATE_SUSPENDED:
1240 return 0;
1241 default:
1242 return -EBADFD;
1246 static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1250 if (err < 0)
1251 return err;
1252 runtime->hw_ptr_base = 0;
1253 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1254 runtime->status->hw_ptr % runtime->period_size;
1255 runtime->silence_start = runtime->status->hw_ptr;
1256 runtime->silence_filled = 0;
1257 return 0;
1260 static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1262 struct snd_pcm_runtime *runtime = substream->runtime;
1263 runtime->control->appl_ptr = runtime->status->hw_ptr;
1264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1265 runtime->silence_size > 0)
1266 snd_pcm_playback_silence(substream, ULONG_MAX);
1269 static struct action_ops snd_pcm_action_reset = {
1270 .pre_action = snd_pcm_pre_reset,
1271 .do_action = snd_pcm_do_reset,
1272 .post_action = snd_pcm_post_reset
1275 static int snd_pcm_reset(struct snd_pcm_substream *substream)
1277 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1281 * prepare ioctl
1283 /* we use the second argument for updating f_flags */
1284 static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1285 int f_flags)
1287 struct snd_pcm_runtime *runtime = substream->runtime;
1288 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1289 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1290 return -EBADFD;
1291 if (snd_pcm_running(substream))
1292 return -EBUSY;
1293 substream->f_flags = f_flags;
1294 return 0;
1297 static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1299 int err;
1300 err = substream->ops->prepare(substream);
1301 if (err < 0)
1302 return err;
1303 return snd_pcm_do_reset(substream, 0);
1306 static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1308 struct snd_pcm_runtime *runtime = substream->runtime;
1309 runtime->control->appl_ptr = runtime->status->hw_ptr;
1310 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1313 static struct action_ops snd_pcm_action_prepare = {
1314 .pre_action = snd_pcm_pre_prepare,
1315 .do_action = snd_pcm_do_prepare,
1316 .post_action = snd_pcm_post_prepare
1320 * snd_pcm_prepare - prepare the PCM substream to be triggerable
1321 * @substream: the PCM substream instance
1322 * @file: file to refer f_flags
1324 static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1325 struct file *file)
1327 int res;
1328 struct snd_card *card = substream->pcm->card;
1329 int f_flags;
1331 if (file)
1332 f_flags = file->f_flags;
1333 else
1334 f_flags = substream->f_flags;
1336 snd_power_lock(card);
1337 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1338 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1339 substream, f_flags);
1340 snd_power_unlock(card);
1341 return res;
1345 * drain ioctl
1348 static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1350 substream->runtime->trigger_master = substream;
1351 return 0;
1354 static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1356 struct snd_pcm_runtime *runtime = substream->runtime;
1357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1358 switch (runtime->status->state) {
1359 case SNDRV_PCM_STATE_PREPARED:
1360 /* start playback stream if possible */
1361 if (! snd_pcm_playback_empty(substream)) {
1362 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1363 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1365 break;
1366 case SNDRV_PCM_STATE_RUNNING:
1367 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1368 break;
1369 default:
1370 break;
1372 } else {
1373 /* stop running stream */
1374 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1375 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1376 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1377 snd_pcm_do_stop(substream, new_state);
1378 snd_pcm_post_stop(substream, new_state);
1381 return 0;
1384 static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1388 static struct action_ops snd_pcm_action_drain_init = {
1389 .pre_action = snd_pcm_pre_drain_init,
1390 .do_action = snd_pcm_do_drain_init,
1391 .post_action = snd_pcm_post_drain_init
1394 static int snd_pcm_drop(struct snd_pcm_substream *substream);
1397 * Drain the stream(s).
1398 * When the substream is linked, sync until the draining of all playback streams
1399 * is finished.
1400 * After this call, all streams are supposed to be either SETUP or DRAINING
1401 * (capture only) state.
1403 static int snd_pcm_drain(struct snd_pcm_substream *substream,
1404 struct file *file)
1406 struct snd_card *card;
1407 struct snd_pcm_runtime *runtime;
1408 struct snd_pcm_substream *s;
1409 wait_queue_t wait;
1410 int result = 0;
1411 int nonblock = 0;
1413 card = substream->pcm->card;
1414 runtime = substream->runtime;
1416 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1417 return -EBADFD;
1419 snd_power_lock(card);
1420 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1421 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1422 if (result < 0) {
1423 snd_power_unlock(card);
1424 return result;
1428 if (file) {
1429 if (file->f_flags & O_NONBLOCK)
1430 nonblock = 1;
1431 } else if (substream->f_flags & O_NONBLOCK)
1432 nonblock = 1;
1434 down_read(&snd_pcm_link_rwsem);
1435 snd_pcm_stream_lock_irq(substream);
1436 /* resume pause */
1437 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1438 snd_pcm_pause(substream, 0);
1440 /* pre-start/stop - all running streams are changed to DRAINING state */
1441 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1442 if (result < 0)
1443 goto unlock;
1444 /* in non-blocking, we don't wait in ioctl but let caller poll */
1445 if (nonblock) {
1446 result = -EAGAIN;
1447 goto unlock;
1450 for (;;) {
1451 long tout;
1452 struct snd_pcm_runtime *to_check;
1453 if (signal_pending(current)) {
1454 result = -ERESTARTSYS;
1455 break;
1457 /* find a substream to drain */
1458 to_check = NULL;
1459 snd_pcm_group_for_each_entry(s, substream) {
1460 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1461 continue;
1462 runtime = s->runtime;
1463 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1464 to_check = runtime;
1465 break;
1468 if (!to_check)
1469 break; /* all drained */
1470 init_waitqueue_entry(&wait, current);
1471 add_wait_queue(&to_check->sleep, &wait);
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 snd_pcm_stream_unlock_irq(substream);
1474 up_read(&snd_pcm_link_rwsem);
1475 snd_power_unlock(card);
1476 tout = schedule_timeout(10 * HZ);
1477 snd_power_lock(card);
1478 down_read(&snd_pcm_link_rwsem);
1479 snd_pcm_stream_lock_irq(substream);
1480 remove_wait_queue(&to_check->sleep, &wait);
1481 if (tout == 0) {
1482 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1483 result = -ESTRPIPE;
1484 else {
1485 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1486 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1487 result = -EIO;
1489 break;
1493 unlock:
1494 snd_pcm_stream_unlock_irq(substream);
1495 up_read(&snd_pcm_link_rwsem);
1496 snd_power_unlock(card);
1498 return result;
1502 * drop ioctl
1504 * Immediately put all linked substreams into SETUP state.
1506 static int snd_pcm_drop(struct snd_pcm_substream *substream)
1508 struct snd_pcm_runtime *runtime;
1509 struct snd_card *card;
1510 int result = 0;
1512 if (PCM_RUNTIME_CHECK(substream))
1513 return -ENXIO;
1514 runtime = substream->runtime;
1515 card = substream->pcm->card;
1517 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1518 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1519 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1520 return -EBADFD;
1522 snd_pcm_stream_lock_irq(substream);
1523 /* resume pause */
1524 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1525 snd_pcm_pause(substream, 0);
1527 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1528 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1529 snd_pcm_stream_unlock_irq(substream);
1531 return result;
1535 /* WARNING: Don't forget to fput back the file */
1536 static struct file *snd_pcm_file_fd(int fd)
1538 struct file *file;
1539 struct inode *inode;
1540 unsigned int minor;
1542 file = fget(fd);
1543 if (!file)
1544 return NULL;
1545 inode = file->f_path.dentry->d_inode;
1546 if (!S_ISCHR(inode->i_mode) ||
1547 imajor(inode) != snd_major) {
1548 fput(file);
1549 return NULL;
1551 minor = iminor(inode);
1552 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1553 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1554 fput(file);
1555 return NULL;
1557 return file;
1561 * PCM link handling
1563 static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1565 int res = 0;
1566 struct file *file;
1567 struct snd_pcm_file *pcm_file;
1568 struct snd_pcm_substream *substream1;
1570 file = snd_pcm_file_fd(fd);
1571 if (!file)
1572 return -EBADFD;
1573 pcm_file = file->private_data;
1574 substream1 = pcm_file->substream;
1575 down_write(&snd_pcm_link_rwsem);
1576 write_lock_irq(&snd_pcm_link_rwlock);
1577 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1578 substream->runtime->status->state != substream1->runtime->status->state) {
1579 res = -EBADFD;
1580 goto _end;
1582 if (snd_pcm_stream_linked(substream1)) {
1583 res = -EALREADY;
1584 goto _end;
1586 if (!snd_pcm_stream_linked(substream)) {
1587 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1588 if (substream->group == NULL) {
1589 res = -ENOMEM;
1590 goto _end;
1592 spin_lock_init(&substream->group->lock);
1593 INIT_LIST_HEAD(&substream->group->substreams);
1594 list_add_tail(&substream->link_list, &substream->group->substreams);
1595 substream->group->count = 1;
1597 list_add_tail(&substream1->link_list, &substream->group->substreams);
1598 substream->group->count++;
1599 substream1->group = substream->group;
1600 _end:
1601 write_unlock_irq(&snd_pcm_link_rwlock);
1602 up_write(&snd_pcm_link_rwsem);
1603 fput(file);
1604 return res;
1607 static void relink_to_local(struct snd_pcm_substream *substream)
1609 substream->group = &substream->self_group;
1610 INIT_LIST_HEAD(&substream->self_group.substreams);
1611 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1614 static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1616 struct snd_pcm_substream *s;
1617 int res = 0;
1619 down_write(&snd_pcm_link_rwsem);
1620 write_lock_irq(&snd_pcm_link_rwlock);
1621 if (!snd_pcm_stream_linked(substream)) {
1622 res = -EALREADY;
1623 goto _end;
1625 list_del(&substream->link_list);
1626 substream->group->count--;
1627 if (substream->group->count == 1) { /* detach the last stream, too */
1628 snd_pcm_group_for_each_entry(s, substream) {
1629 relink_to_local(s);
1630 break;
1632 kfree(substream->group);
1634 relink_to_local(substream);
1635 _end:
1636 write_unlock_irq(&snd_pcm_link_rwlock);
1637 up_write(&snd_pcm_link_rwsem);
1638 return res;
1642 * hw configurator
1644 static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1645 struct snd_pcm_hw_rule *rule)
1647 struct snd_interval t;
1648 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1649 hw_param_interval_c(params, rule->deps[1]), &t);
1650 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1653 static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1654 struct snd_pcm_hw_rule *rule)
1656 struct snd_interval t;
1657 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1658 hw_param_interval_c(params, rule->deps[1]), &t);
1659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1662 static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1663 struct snd_pcm_hw_rule *rule)
1665 struct snd_interval t;
1666 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1667 hw_param_interval_c(params, rule->deps[1]),
1668 (unsigned long) rule->private, &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1672 static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
1675 struct snd_interval t;
1676 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1677 (unsigned long) rule->private,
1678 hw_param_interval_c(params, rule->deps[1]), &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1682 static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1685 unsigned int k;
1686 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1687 struct snd_mask m;
1688 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1689 snd_mask_any(&m);
1690 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1691 int bits;
1692 if (! snd_mask_test(mask, k))
1693 continue;
1694 bits = snd_pcm_format_physical_width(k);
1695 if (bits <= 0)
1696 continue; /* ignore invalid formats */
1697 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1698 snd_mask_reset(&m, k);
1700 return snd_mask_refine(mask, &m);
1703 static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1704 struct snd_pcm_hw_rule *rule)
1706 struct snd_interval t;
1707 unsigned int k;
1708 t.min = UINT_MAX;
1709 t.max = 0;
1710 t.openmin = 0;
1711 t.openmax = 0;
1712 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1713 int bits;
1714 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1715 continue;
1716 bits = snd_pcm_format_physical_width(k);
1717 if (bits <= 0)
1718 continue; /* ignore invalid formats */
1719 if (t.min > (unsigned)bits)
1720 t.min = bits;
1721 if (t.max < (unsigned)bits)
1722 t.max = bits;
1724 t.integer = 1;
1725 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1728 #if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1729 #error "Change this table"
1730 #endif
1732 static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1733 48000, 64000, 88200, 96000, 176400, 192000 };
1735 const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1736 .count = ARRAY_SIZE(rates),
1737 .list = rates,
1740 static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
1743 struct snd_pcm_hardware *hw = rule->private;
1744 return snd_interval_list(hw_param_interval(params, rule->var),
1745 snd_pcm_known_rates.count,
1746 snd_pcm_known_rates.list, hw->rates);
1749 static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1750 struct snd_pcm_hw_rule *rule)
1752 struct snd_interval t;
1753 struct snd_pcm_substream *substream = rule->private;
1754 t.min = 0;
1755 t.max = substream->buffer_bytes_max;
1756 t.openmin = 0;
1757 t.openmax = 0;
1758 t.integer = 1;
1759 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1762 int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1764 struct snd_pcm_runtime *runtime = substream->runtime;
1765 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1766 int k, err;
1768 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1769 snd_mask_any(constrs_mask(constrs, k));
1772 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1773 snd_interval_any(constrs_interval(constrs, k));
1776 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1777 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1778 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1779 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1780 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1782 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1783 snd_pcm_hw_rule_format, NULL,
1784 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1785 if (err < 0)
1786 return err;
1787 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1788 snd_pcm_hw_rule_sample_bits, NULL,
1789 SNDRV_PCM_HW_PARAM_FORMAT,
1790 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1791 if (err < 0)
1792 return err;
1793 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1794 snd_pcm_hw_rule_div, NULL,
1795 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1796 if (err < 0)
1797 return err;
1798 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1799 snd_pcm_hw_rule_mul, NULL,
1800 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1801 if (err < 0)
1802 return err;
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1804 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1805 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1806 if (err < 0)
1807 return err;
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1811 if (err < 0)
1812 return err;
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1814 snd_pcm_hw_rule_div, NULL,
1815 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1816 if (err < 0)
1817 return err;
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1819 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1820 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1824 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1825 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1829 snd_pcm_hw_rule_div, NULL,
1830 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1834 snd_pcm_hw_rule_div, NULL,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1839 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1845 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1849 snd_pcm_hw_rule_mul, NULL,
1850 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1855 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1864 snd_pcm_hw_rule_muldivk, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1869 snd_pcm_hw_rule_muldivk, (void*) 8,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1881 if (err < 0)
1882 return err;
1883 return 0;
1886 int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1888 struct snd_pcm_runtime *runtime = substream->runtime;
1889 struct snd_pcm_hardware *hw = &runtime->hw;
1890 int err;
1891 unsigned int mask = 0;
1893 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1894 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1895 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1896 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1897 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1898 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1899 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1900 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1901 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1902 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1903 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1905 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1906 if (err < 0)
1907 return err;
1909 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1910 if (err < 0)
1911 return err;
1913 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1914 if (err < 0)
1915 return err;
1917 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1918 hw->channels_min, hw->channels_max);
1919 if (err < 0)
1920 return err;
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1923 hw->rate_min, hw->rate_max);
1924 if (err < 0)
1925 return err;
1927 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1928 hw->period_bytes_min, hw->period_bytes_max);
1929 if (err < 0)
1930 return err;
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1933 hw->periods_min, hw->periods_max);
1934 if (err < 0)
1935 return err;
1937 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1938 hw->period_bytes_min, hw->buffer_bytes_max);
1939 if (err < 0)
1940 return err;
1942 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1943 snd_pcm_hw_rule_buffer_bytes_max, substream,
1944 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1945 if (err < 0)
1946 return err;
1948 /* FIXME: remove */
1949 if (runtime->dma_bytes) {
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1951 if (err < 0)
1952 return -EINVAL;
1955 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1956 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1957 snd_pcm_hw_rule_rate, hw,
1958 SNDRV_PCM_HW_PARAM_RATE, -1);
1959 if (err < 0)
1960 return err;
1963 /* FIXME: this belong to lowlevel */
1964 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1966 return 0;
1969 static void pcm_release_private(struct snd_pcm_substream *substream)
1971 snd_pcm_unlink(substream);
1974 void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1976 substream->ref_count--;
1977 if (substream->ref_count > 0)
1978 return;
1980 snd_pcm_drop(substream);
1981 if (substream->hw_opened) {
1982 if (substream->ops->hw_free != NULL)
1983 substream->ops->hw_free(substream);
1984 substream->ops->close(substream);
1985 substream->hw_opened = 0;
1987 if (substream->pcm_release) {
1988 substream->pcm_release(substream);
1989 substream->pcm_release = NULL;
1991 snd_pcm_detach_substream(substream);
1994 EXPORT_SYMBOL(snd_pcm_release_substream);
1996 int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1997 struct file *file,
1998 struct snd_pcm_substream **rsubstream)
2000 struct snd_pcm_substream *substream;
2001 int err;
2003 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2004 if (err < 0)
2005 return err;
2006 if (substream->ref_count > 1) {
2007 *rsubstream = substream;
2008 return 0;
2011 err = snd_pcm_hw_constraints_init(substream);
2012 if (err < 0) {
2013 snd_printd("snd_pcm_hw_constraints_init failed\n");
2014 goto error;
2017 if ((err = substream->ops->open(substream)) < 0)
2018 goto error;
2020 substream->hw_opened = 1;
2022 err = snd_pcm_hw_constraints_complete(substream);
2023 if (err < 0) {
2024 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2025 goto error;
2028 *rsubstream = substream;
2029 return 0;
2031 error:
2032 snd_pcm_release_substream(substream);
2033 return err;
2036 EXPORT_SYMBOL(snd_pcm_open_substream);
2038 static int snd_pcm_open_file(struct file *file,
2039 struct snd_pcm *pcm,
2040 int stream,
2041 struct snd_pcm_file **rpcm_file)
2043 struct snd_pcm_file *pcm_file;
2044 struct snd_pcm_substream *substream;
2045 struct snd_pcm_str *str;
2046 int err;
2048 if (rpcm_file)
2049 *rpcm_file = NULL;
2051 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2052 if (err < 0)
2053 return err;
2055 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2056 if (pcm_file == NULL) {
2057 snd_pcm_release_substream(substream);
2058 return -ENOMEM;
2060 pcm_file->substream = substream;
2061 if (substream->ref_count == 1) {
2062 str = substream->pstr;
2063 substream->file = pcm_file;
2064 substream->pcm_release = pcm_release_private;
2066 file->private_data = pcm_file;
2067 if (rpcm_file)
2068 *rpcm_file = pcm_file;
2069 return 0;
2072 static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2074 struct snd_pcm *pcm;
2076 pcm = snd_lookup_minor_data(iminor(inode),
2077 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2078 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2081 static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2083 struct snd_pcm *pcm;
2085 pcm = snd_lookup_minor_data(iminor(inode),
2086 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2087 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2090 static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2092 int err;
2093 struct snd_pcm_file *pcm_file;
2094 wait_queue_t wait;
2096 if (pcm == NULL) {
2097 err = -ENODEV;
2098 goto __error1;
2100 err = snd_card_file_add(pcm->card, file);
2101 if (err < 0)
2102 goto __error1;
2103 if (!try_module_get(pcm->card->module)) {
2104 err = -EFAULT;
2105 goto __error2;
2107 init_waitqueue_entry(&wait, current);
2108 add_wait_queue(&pcm->open_wait, &wait);
2109 mutex_lock(&pcm->open_mutex);
2110 while (1) {
2111 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2112 if (err >= 0)
2113 break;
2114 if (err == -EAGAIN) {
2115 if (file->f_flags & O_NONBLOCK) {
2116 err = -EBUSY;
2117 break;
2119 } else
2120 break;
2121 set_current_state(TASK_INTERRUPTIBLE);
2122 mutex_unlock(&pcm->open_mutex);
2123 schedule();
2124 mutex_lock(&pcm->open_mutex);
2125 if (signal_pending(current)) {
2126 err = -ERESTARTSYS;
2127 break;
2130 remove_wait_queue(&pcm->open_wait, &wait);
2131 mutex_unlock(&pcm->open_mutex);
2132 if (err < 0)
2133 goto __error;
2134 return err;
2136 __error:
2137 module_put(pcm->card->module);
2138 __error2:
2139 snd_card_file_remove(pcm->card, file);
2140 __error1:
2141 return err;
2144 static int snd_pcm_release(struct inode *inode, struct file *file)
2146 struct snd_pcm *pcm;
2147 struct snd_pcm_substream *substream;
2148 struct snd_pcm_file *pcm_file;
2150 pcm_file = file->private_data;
2151 substream = pcm_file->substream;
2152 if (snd_BUG_ON(!substream))
2153 return -ENXIO;
2154 pcm = substream->pcm;
2155 mutex_lock(&pcm->open_mutex);
2156 snd_pcm_release_substream(substream);
2157 kfree(pcm_file);
2158 mutex_unlock(&pcm->open_mutex);
2159 wake_up(&pcm->open_wait);
2160 module_put(pcm->card->module);
2161 snd_card_file_remove(pcm->card, file);
2162 return 0;
2165 static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2166 snd_pcm_uframes_t frames)
2168 struct snd_pcm_runtime *runtime = substream->runtime;
2169 snd_pcm_sframes_t appl_ptr;
2170 snd_pcm_sframes_t ret;
2171 snd_pcm_sframes_t hw_avail;
2173 if (frames == 0)
2174 return 0;
2176 snd_pcm_stream_lock_irq(substream);
2177 switch (runtime->status->state) {
2178 case SNDRV_PCM_STATE_PREPARED:
2179 break;
2180 case SNDRV_PCM_STATE_DRAINING:
2181 case SNDRV_PCM_STATE_RUNNING:
2182 if (snd_pcm_update_hw_ptr(substream) >= 0)
2183 break;
2184 /* Fall through */
2185 case SNDRV_PCM_STATE_XRUN:
2186 ret = -EPIPE;
2187 goto __end;
2188 case SNDRV_PCM_STATE_SUSPENDED:
2189 ret = -ESTRPIPE;
2190 goto __end;
2191 default:
2192 ret = -EBADFD;
2193 goto __end;
2196 hw_avail = snd_pcm_playback_hw_avail(runtime);
2197 if (hw_avail <= 0) {
2198 ret = 0;
2199 goto __end;
2201 if (frames > (snd_pcm_uframes_t)hw_avail)
2202 frames = hw_avail;
2203 appl_ptr = runtime->control->appl_ptr - frames;
2204 if (appl_ptr < 0)
2205 appl_ptr += runtime->boundary;
2206 runtime->control->appl_ptr = appl_ptr;
2207 ret = frames;
2208 __end:
2209 snd_pcm_stream_unlock_irq(substream);
2210 return ret;
2213 static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2214 snd_pcm_uframes_t frames)
2216 struct snd_pcm_runtime *runtime = substream->runtime;
2217 snd_pcm_sframes_t appl_ptr;
2218 snd_pcm_sframes_t ret;
2219 snd_pcm_sframes_t hw_avail;
2221 if (frames == 0)
2222 return 0;
2224 snd_pcm_stream_lock_irq(substream);
2225 switch (runtime->status->state) {
2226 case SNDRV_PCM_STATE_PREPARED:
2227 case SNDRV_PCM_STATE_DRAINING:
2228 break;
2229 case SNDRV_PCM_STATE_RUNNING:
2230 if (snd_pcm_update_hw_ptr(substream) >= 0)
2231 break;
2232 /* Fall through */
2233 case SNDRV_PCM_STATE_XRUN:
2234 ret = -EPIPE;
2235 goto __end;
2236 case SNDRV_PCM_STATE_SUSPENDED:
2237 ret = -ESTRPIPE;
2238 goto __end;
2239 default:
2240 ret = -EBADFD;
2241 goto __end;
2244 hw_avail = snd_pcm_capture_hw_avail(runtime);
2245 if (hw_avail <= 0) {
2246 ret = 0;
2247 goto __end;
2249 if (frames > (snd_pcm_uframes_t)hw_avail)
2250 frames = hw_avail;
2251 appl_ptr = runtime->control->appl_ptr - frames;
2252 if (appl_ptr < 0)
2253 appl_ptr += runtime->boundary;
2254 runtime->control->appl_ptr = appl_ptr;
2255 ret = frames;
2256 __end:
2257 snd_pcm_stream_unlock_irq(substream);
2258 return ret;
2261 static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2262 snd_pcm_uframes_t frames)
2264 struct snd_pcm_runtime *runtime = substream->runtime;
2265 snd_pcm_sframes_t appl_ptr;
2266 snd_pcm_sframes_t ret;
2267 snd_pcm_sframes_t avail;
2269 if (frames == 0)
2270 return 0;
2272 snd_pcm_stream_lock_irq(substream);
2273 switch (runtime->status->state) {
2274 case SNDRV_PCM_STATE_PREPARED:
2275 case SNDRV_PCM_STATE_PAUSED:
2276 break;
2277 case SNDRV_PCM_STATE_DRAINING:
2278 case SNDRV_PCM_STATE_RUNNING:
2279 if (snd_pcm_update_hw_ptr(substream) >= 0)
2280 break;
2281 /* Fall through */
2282 case SNDRV_PCM_STATE_XRUN:
2283 ret = -EPIPE;
2284 goto __end;
2285 case SNDRV_PCM_STATE_SUSPENDED:
2286 ret = -ESTRPIPE;
2287 goto __end;
2288 default:
2289 ret = -EBADFD;
2290 goto __end;
2293 avail = snd_pcm_playback_avail(runtime);
2294 if (avail <= 0) {
2295 ret = 0;
2296 goto __end;
2298 if (frames > (snd_pcm_uframes_t)avail)
2299 frames = avail;
2300 appl_ptr = runtime->control->appl_ptr + frames;
2301 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2302 appl_ptr -= runtime->boundary;
2303 runtime->control->appl_ptr = appl_ptr;
2304 ret = frames;
2305 __end:
2306 snd_pcm_stream_unlock_irq(substream);
2307 return ret;
2310 static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2311 snd_pcm_uframes_t frames)
2313 struct snd_pcm_runtime *runtime = substream->runtime;
2314 snd_pcm_sframes_t appl_ptr;
2315 snd_pcm_sframes_t ret;
2316 snd_pcm_sframes_t avail;
2318 if (frames == 0)
2319 return 0;
2321 snd_pcm_stream_lock_irq(substream);
2322 switch (runtime->status->state) {
2323 case SNDRV_PCM_STATE_PREPARED:
2324 case SNDRV_PCM_STATE_DRAINING:
2325 case SNDRV_PCM_STATE_PAUSED:
2326 break;
2327 case SNDRV_PCM_STATE_RUNNING:
2328 if (snd_pcm_update_hw_ptr(substream) >= 0)
2329 break;
2330 /* Fall through */
2331 case SNDRV_PCM_STATE_XRUN:
2332 ret = -EPIPE;
2333 goto __end;
2334 case SNDRV_PCM_STATE_SUSPENDED:
2335 ret = -ESTRPIPE;
2336 goto __end;
2337 default:
2338 ret = -EBADFD;
2339 goto __end;
2342 avail = snd_pcm_capture_avail(runtime);
2343 if (avail <= 0) {
2344 ret = 0;
2345 goto __end;
2347 if (frames > (snd_pcm_uframes_t)avail)
2348 frames = avail;
2349 appl_ptr = runtime->control->appl_ptr + frames;
2350 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2351 appl_ptr -= runtime->boundary;
2352 runtime->control->appl_ptr = appl_ptr;
2353 ret = frames;
2354 __end:
2355 snd_pcm_stream_unlock_irq(substream);
2356 return ret;
2359 static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2361 struct snd_pcm_runtime *runtime = substream->runtime;
2362 int err;
2364 snd_pcm_stream_lock_irq(substream);
2365 switch (runtime->status->state) {
2366 case SNDRV_PCM_STATE_DRAINING:
2367 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2368 goto __badfd;
2369 case SNDRV_PCM_STATE_RUNNING:
2370 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2371 break;
2372 /* Fall through */
2373 case SNDRV_PCM_STATE_PREPARED:
2374 case SNDRV_PCM_STATE_SUSPENDED:
2375 err = 0;
2376 break;
2377 case SNDRV_PCM_STATE_XRUN:
2378 err = -EPIPE;
2379 break;
2380 default:
2381 __badfd:
2382 err = -EBADFD;
2383 break;
2385 snd_pcm_stream_unlock_irq(substream);
2386 return err;
2389 static int snd_pcm_delay(struct snd_pcm_substream *substream,
2390 snd_pcm_sframes_t __user *res)
2392 struct snd_pcm_runtime *runtime = substream->runtime;
2393 int err;
2394 snd_pcm_sframes_t n = 0;
2396 snd_pcm_stream_lock_irq(substream);
2397 switch (runtime->status->state) {
2398 case SNDRV_PCM_STATE_DRAINING:
2399 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2400 goto __badfd;
2401 case SNDRV_PCM_STATE_RUNNING:
2402 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2403 break;
2404 /* Fall through */
2405 case SNDRV_PCM_STATE_PREPARED:
2406 case SNDRV_PCM_STATE_SUSPENDED:
2407 err = 0;
2408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2409 n = snd_pcm_playback_hw_avail(runtime);
2410 else
2411 n = snd_pcm_capture_avail(runtime);
2412 n += runtime->delay;
2413 break;
2414 case SNDRV_PCM_STATE_XRUN:
2415 err = -EPIPE;
2416 break;
2417 default:
2418 __badfd:
2419 err = -EBADFD;
2420 break;
2422 snd_pcm_stream_unlock_irq(substream);
2423 if (!err)
2424 if (put_user(n, res))
2425 err = -EFAULT;
2426 return err;
2429 static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2430 struct snd_pcm_sync_ptr __user *_sync_ptr)
2432 struct snd_pcm_runtime *runtime = substream->runtime;
2433 struct snd_pcm_sync_ptr sync_ptr;
2434 volatile struct snd_pcm_mmap_status *status;
2435 volatile struct snd_pcm_mmap_control *control;
2436 int err;
2438 memset(&sync_ptr, 0, sizeof(sync_ptr));
2439 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2440 return -EFAULT;
2441 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2442 return -EFAULT;
2443 status = runtime->status;
2444 control = runtime->control;
2445 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2446 err = snd_pcm_hwsync(substream);
2447 if (err < 0)
2448 return err;
2450 snd_pcm_stream_lock_irq(substream);
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2452 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2453 else
2454 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2455 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2456 control->avail_min = sync_ptr.c.control.avail_min;
2457 else
2458 sync_ptr.c.control.avail_min = control->avail_min;
2459 sync_ptr.s.status.state = status->state;
2460 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2461 sync_ptr.s.status.tstamp = status->tstamp;
2462 sync_ptr.s.status.suspended_state = status->suspended_state;
2463 snd_pcm_stream_unlock_irq(substream);
2464 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2465 return -EFAULT;
2466 return 0;
2469 static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2471 struct snd_pcm_runtime *runtime = substream->runtime;
2472 int arg;
2474 if (get_user(arg, _arg))
2475 return -EFAULT;
2476 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2477 return -EINVAL;
2478 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2479 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2480 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2481 return 0;
2484 static int snd_pcm_common_ioctl1(struct file *file,
2485 struct snd_pcm_substream *substream,
2486 unsigned int cmd, void __user *arg)
2488 switch (cmd) {
2489 case SNDRV_PCM_IOCTL_PVERSION:
2490 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2491 case SNDRV_PCM_IOCTL_INFO:
2492 return snd_pcm_info_user(substream, arg);
2493 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2494 return 0;
2495 case SNDRV_PCM_IOCTL_TTSTAMP:
2496 return snd_pcm_tstamp(substream, arg);
2497 case SNDRV_PCM_IOCTL_HW_REFINE:
2498 return snd_pcm_hw_refine_user(substream, arg);
2499 case SNDRV_PCM_IOCTL_HW_PARAMS:
2500 return snd_pcm_hw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_HW_FREE:
2502 return snd_pcm_hw_free(substream);
2503 case SNDRV_PCM_IOCTL_SW_PARAMS:
2504 return snd_pcm_sw_params_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_STATUS:
2506 return snd_pcm_status_user(substream, arg);
2507 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2508 return snd_pcm_channel_info_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_PREPARE:
2510 return snd_pcm_prepare(substream, file);
2511 case SNDRV_PCM_IOCTL_RESET:
2512 return snd_pcm_reset(substream);
2513 case SNDRV_PCM_IOCTL_START:
2514 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2515 case SNDRV_PCM_IOCTL_LINK:
2516 return snd_pcm_link(substream, (int)(unsigned long) arg);
2517 case SNDRV_PCM_IOCTL_UNLINK:
2518 return snd_pcm_unlink(substream);
2519 case SNDRV_PCM_IOCTL_RESUME:
2520 return snd_pcm_resume(substream);
2521 case SNDRV_PCM_IOCTL_XRUN:
2522 return snd_pcm_xrun(substream);
2523 case SNDRV_PCM_IOCTL_HWSYNC:
2524 return snd_pcm_hwsync(substream);
2525 case SNDRV_PCM_IOCTL_DELAY:
2526 return snd_pcm_delay(substream, arg);
2527 case SNDRV_PCM_IOCTL_SYNC_PTR:
2528 return snd_pcm_sync_ptr(substream, arg);
2529 #ifdef CONFIG_SND_SUPPORT_OLD_API
2530 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2531 return snd_pcm_hw_refine_old_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2533 return snd_pcm_hw_params_old_user(substream, arg);
2534 #endif
2535 case SNDRV_PCM_IOCTL_DRAIN:
2536 return snd_pcm_drain(substream, file);
2537 case SNDRV_PCM_IOCTL_DROP:
2538 return snd_pcm_drop(substream);
2539 case SNDRV_PCM_IOCTL_PAUSE:
2541 int res;
2542 snd_pcm_stream_lock_irq(substream);
2543 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2544 snd_pcm_stream_unlock_irq(substream);
2545 return res;
2548 snd_printd("unknown ioctl = 0x%x\n", cmd);
2549 return -ENOTTY;
2552 static int snd_pcm_playback_ioctl1(struct file *file,
2553 struct snd_pcm_substream *substream,
2554 unsigned int cmd, void __user *arg)
2556 if (snd_BUG_ON(!substream))
2557 return -ENXIO;
2558 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2559 return -EINVAL;
2560 switch (cmd) {
2561 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2563 struct snd_xferi xferi;
2564 struct snd_xferi __user *_xferi = arg;
2565 struct snd_pcm_runtime *runtime = substream->runtime;
2566 snd_pcm_sframes_t result;
2567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2568 return -EBADFD;
2569 if (put_user(0, &_xferi->result))
2570 return -EFAULT;
2571 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2572 return -EFAULT;
2573 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2574 __put_user(result, &_xferi->result);
2575 return result < 0 ? result : 0;
2577 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2579 struct snd_xfern xfern;
2580 struct snd_xfern __user *_xfern = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
2582 void __user **bufs;
2583 snd_pcm_sframes_t result;
2584 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2585 return -EBADFD;
2586 if (runtime->channels > 128)
2587 return -EINVAL;
2588 if (put_user(0, &_xfern->result))
2589 return -EFAULT;
2590 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2591 return -EFAULT;
2593 bufs = memdup_user(xfern.bufs,
2594 sizeof(void *) * runtime->channels);
2595 if (IS_ERR(bufs))
2596 return PTR_ERR(bufs);
2597 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2598 kfree(bufs);
2599 __put_user(result, &_xfern->result);
2600 return result < 0 ? result : 0;
2602 case SNDRV_PCM_IOCTL_REWIND:
2604 snd_pcm_uframes_t frames;
2605 snd_pcm_uframes_t __user *_frames = arg;
2606 snd_pcm_sframes_t result;
2607 if (get_user(frames, _frames))
2608 return -EFAULT;
2609 if (put_user(0, _frames))
2610 return -EFAULT;
2611 result = snd_pcm_playback_rewind(substream, frames);
2612 __put_user(result, _frames);
2613 return result < 0 ? result : 0;
2615 case SNDRV_PCM_IOCTL_FORWARD:
2617 snd_pcm_uframes_t frames;
2618 snd_pcm_uframes_t __user *_frames = arg;
2619 snd_pcm_sframes_t result;
2620 if (get_user(frames, _frames))
2621 return -EFAULT;
2622 if (put_user(0, _frames))
2623 return -EFAULT;
2624 result = snd_pcm_playback_forward(substream, frames);
2625 __put_user(result, _frames);
2626 return result < 0 ? result : 0;
2629 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2632 static int snd_pcm_capture_ioctl1(struct file *file,
2633 struct snd_pcm_substream *substream,
2634 unsigned int cmd, void __user *arg)
2636 if (snd_BUG_ON(!substream))
2637 return -ENXIO;
2638 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2639 return -EINVAL;
2640 switch (cmd) {
2641 case SNDRV_PCM_IOCTL_READI_FRAMES:
2643 struct snd_xferi xferi;
2644 struct snd_xferi __user *_xferi = arg;
2645 struct snd_pcm_runtime *runtime = substream->runtime;
2646 snd_pcm_sframes_t result;
2647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2648 return -EBADFD;
2649 if (put_user(0, &_xferi->result))
2650 return -EFAULT;
2651 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2652 return -EFAULT;
2653 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2654 __put_user(result, &_xferi->result);
2655 return result < 0 ? result : 0;
2657 case SNDRV_PCM_IOCTL_READN_FRAMES:
2659 struct snd_xfern xfern;
2660 struct snd_xfern __user *_xfern = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
2662 void *bufs;
2663 snd_pcm_sframes_t result;
2664 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2665 return -EBADFD;
2666 if (runtime->channels > 128)
2667 return -EINVAL;
2668 if (put_user(0, &_xfern->result))
2669 return -EFAULT;
2670 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2671 return -EFAULT;
2673 bufs = memdup_user(xfern.bufs,
2674 sizeof(void *) * runtime->channels);
2675 if (IS_ERR(bufs))
2676 return PTR_ERR(bufs);
2677 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2678 kfree(bufs);
2679 __put_user(result, &_xfern->result);
2680 return result < 0 ? result : 0;
2682 case SNDRV_PCM_IOCTL_REWIND:
2684 snd_pcm_uframes_t frames;
2685 snd_pcm_uframes_t __user *_frames = arg;
2686 snd_pcm_sframes_t result;
2687 if (get_user(frames, _frames))
2688 return -EFAULT;
2689 if (put_user(0, _frames))
2690 return -EFAULT;
2691 result = snd_pcm_capture_rewind(substream, frames);
2692 __put_user(result, _frames);
2693 return result < 0 ? result : 0;
2695 case SNDRV_PCM_IOCTL_FORWARD:
2697 snd_pcm_uframes_t frames;
2698 snd_pcm_uframes_t __user *_frames = arg;
2699 snd_pcm_sframes_t result;
2700 if (get_user(frames, _frames))
2701 return -EFAULT;
2702 if (put_user(0, _frames))
2703 return -EFAULT;
2704 result = snd_pcm_capture_forward(substream, frames);
2705 __put_user(result, _frames);
2706 return result < 0 ? result : 0;
2709 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2712 static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2713 unsigned long arg)
2715 struct snd_pcm_file *pcm_file;
2717 pcm_file = file->private_data;
2719 if (((cmd >> 8) & 0xff) != 'A')
2720 return -ENOTTY;
2722 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2723 (void __user *)arg);
2726 static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2727 unsigned long arg)
2729 struct snd_pcm_file *pcm_file;
2731 pcm_file = file->private_data;
2733 if (((cmd >> 8) & 0xff) != 'A')
2734 return -ENOTTY;
2736 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2737 (void __user *)arg);
2740 int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2741 unsigned int cmd, void *arg)
2743 mm_segment_t fs;
2744 int result;
2746 fs = snd_enter_user();
2747 switch (substream->stream) {
2748 case SNDRV_PCM_STREAM_PLAYBACK:
2749 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2750 (void __user *)arg);
2751 break;
2752 case SNDRV_PCM_STREAM_CAPTURE:
2753 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2754 (void __user *)arg);
2755 break;
2756 default:
2757 result = -EINVAL;
2758 break;
2760 snd_leave_user(fs);
2761 return result;
2764 EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2766 static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2767 loff_t * offset)
2769 struct snd_pcm_file *pcm_file;
2770 struct snd_pcm_substream *substream;
2771 struct snd_pcm_runtime *runtime;
2772 snd_pcm_sframes_t result;
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
2776 if (PCM_RUNTIME_CHECK(substream))
2777 return -ENXIO;
2778 runtime = substream->runtime;
2779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2780 return -EBADFD;
2781 if (!frame_aligned(runtime, count))
2782 return -EINVAL;
2783 count = bytes_to_frames(runtime, count);
2784 result = snd_pcm_lib_read(substream, buf, count);
2785 if (result > 0)
2786 result = frames_to_bytes(runtime, result);
2787 return result;
2790 static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2791 size_t count, loff_t * offset)
2793 struct snd_pcm_file *pcm_file;
2794 struct snd_pcm_substream *substream;
2795 struct snd_pcm_runtime *runtime;
2796 snd_pcm_sframes_t result;
2798 pcm_file = file->private_data;
2799 substream = pcm_file->substream;
2800 if (PCM_RUNTIME_CHECK(substream))
2801 return -ENXIO;
2802 runtime = substream->runtime;
2803 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2804 return -EBADFD;
2805 if (!frame_aligned(runtime, count))
2806 return -EINVAL;
2807 count = bytes_to_frames(runtime, count);
2808 result = snd_pcm_lib_write(substream, buf, count);
2809 if (result > 0)
2810 result = frames_to_bytes(runtime, result);
2811 return result;
2814 static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2815 unsigned long nr_segs, loff_t pos)
2818 struct snd_pcm_file *pcm_file;
2819 struct snd_pcm_substream *substream;
2820 struct snd_pcm_runtime *runtime;
2821 snd_pcm_sframes_t result;
2822 unsigned long i;
2823 void __user **bufs;
2824 snd_pcm_uframes_t frames;
2826 pcm_file = iocb->ki_filp->private_data;
2827 substream = pcm_file->substream;
2828 if (PCM_RUNTIME_CHECK(substream))
2829 return -ENXIO;
2830 runtime = substream->runtime;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2832 return -EBADFD;
2833 if (nr_segs > 1024 || nr_segs != runtime->channels)
2834 return -EINVAL;
2835 if (!frame_aligned(runtime, iov->iov_len))
2836 return -EINVAL;
2837 frames = bytes_to_samples(runtime, iov->iov_len);
2838 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2839 if (bufs == NULL)
2840 return -ENOMEM;
2841 for (i = 0; i < nr_segs; ++i)
2842 bufs[i] = iov[i].iov_base;
2843 result = snd_pcm_lib_readv(substream, bufs, frames);
2844 if (result > 0)
2845 result = frames_to_bytes(runtime, result);
2846 kfree(bufs);
2847 return result;
2850 static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2851 unsigned long nr_segs, loff_t pos)
2853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
2856 snd_pcm_sframes_t result;
2857 unsigned long i;
2858 void __user **bufs;
2859 snd_pcm_uframes_t frames;
2861 pcm_file = iocb->ki_filp->private_data;
2862 substream = pcm_file->substream;
2863 if (PCM_RUNTIME_CHECK(substream))
2864 return -ENXIO;
2865 runtime = substream->runtime;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
2868 if (nr_segs > 128 || nr_segs != runtime->channels ||
2869 !frame_aligned(runtime, iov->iov_len))
2870 return -EINVAL;
2871 frames = bytes_to_samples(runtime, iov->iov_len);
2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2873 if (bufs == NULL)
2874 return -ENOMEM;
2875 for (i = 0; i < nr_segs; ++i)
2876 bufs[i] = iov[i].iov_base;
2877 result = snd_pcm_lib_writev(substream, bufs, frames);
2878 if (result > 0)
2879 result = frames_to_bytes(runtime, result);
2880 kfree(bufs);
2881 return result;
2884 static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2886 struct snd_pcm_file *pcm_file;
2887 struct snd_pcm_substream *substream;
2888 struct snd_pcm_runtime *runtime;
2889 unsigned int mask;
2890 snd_pcm_uframes_t avail;
2892 pcm_file = file->private_data;
2894 substream = pcm_file->substream;
2895 if (PCM_RUNTIME_CHECK(substream))
2896 return -ENXIO;
2897 runtime = substream->runtime;
2899 poll_wait(file, &runtime->sleep, wait);
2901 snd_pcm_stream_lock_irq(substream);
2902 avail = snd_pcm_playback_avail(runtime);
2903 switch (runtime->status->state) {
2904 case SNDRV_PCM_STATE_RUNNING:
2905 case SNDRV_PCM_STATE_PREPARED:
2906 case SNDRV_PCM_STATE_PAUSED:
2907 if (avail >= runtime->control->avail_min) {
2908 mask = POLLOUT | POLLWRNORM;
2909 break;
2911 /* Fall through */
2912 case SNDRV_PCM_STATE_DRAINING:
2913 mask = 0;
2914 break;
2915 default:
2916 mask = POLLOUT | POLLWRNORM | POLLERR;
2917 break;
2919 snd_pcm_stream_unlock_irq(substream);
2920 return mask;
2923 static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2925 struct snd_pcm_file *pcm_file;
2926 struct snd_pcm_substream *substream;
2927 struct snd_pcm_runtime *runtime;
2928 unsigned int mask;
2929 snd_pcm_uframes_t avail;
2931 pcm_file = file->private_data;
2933 substream = pcm_file->substream;
2934 if (PCM_RUNTIME_CHECK(substream))
2935 return -ENXIO;
2936 runtime = substream->runtime;
2938 poll_wait(file, &runtime->sleep, wait);
2940 snd_pcm_stream_lock_irq(substream);
2941 avail = snd_pcm_capture_avail(runtime);
2942 switch (runtime->status->state) {
2943 case SNDRV_PCM_STATE_RUNNING:
2944 case SNDRV_PCM_STATE_PREPARED:
2945 case SNDRV_PCM_STATE_PAUSED:
2946 if (avail >= runtime->control->avail_min) {
2947 mask = POLLIN | POLLRDNORM;
2948 break;
2950 mask = 0;
2951 break;
2952 case SNDRV_PCM_STATE_DRAINING:
2953 if (avail > 0) {
2954 mask = POLLIN | POLLRDNORM;
2955 break;
2957 /* Fall through */
2958 default:
2959 mask = POLLIN | POLLRDNORM | POLLERR;
2960 break;
2962 snd_pcm_stream_unlock_irq(substream);
2963 return mask;
2967 * mmap support
2971 * Only on coherent architectures, we can mmap the status and the control records
2972 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2974 #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2976 * mmap status record
2978 static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2979 struct vm_fault *vmf)
2981 struct snd_pcm_substream *substream = area->vm_private_data;
2982 struct snd_pcm_runtime *runtime;
2984 if (substream == NULL)
2985 return VM_FAULT_SIGBUS;
2986 runtime = substream->runtime;
2987 vmf->page = virt_to_page(runtime->status);
2988 get_page(vmf->page);
2989 return 0;
2992 static const struct vm_operations_struct snd_pcm_vm_ops_status =
2994 .fault = snd_pcm_mmap_status_fault,
2997 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
2998 struct vm_area_struct *area)
3000 struct snd_pcm_runtime *runtime;
3001 long size;
3002 if (!(area->vm_flags & VM_READ))
3003 return -EINVAL;
3004 runtime = substream->runtime;
3005 size = area->vm_end - area->vm_start;
3006 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3007 return -EINVAL;
3008 area->vm_ops = &snd_pcm_vm_ops_status;
3009 area->vm_private_data = substream;
3010 area->vm_flags |= VM_RESERVED;
3011 return 0;
3015 * mmap control record
3017 static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3018 struct vm_fault *vmf)
3020 struct snd_pcm_substream *substream = area->vm_private_data;
3021 struct snd_pcm_runtime *runtime;
3023 if (substream == NULL)
3024 return VM_FAULT_SIGBUS;
3025 runtime = substream->runtime;
3026 vmf->page = virt_to_page(runtime->control);
3027 get_page(vmf->page);
3028 return 0;
3031 static const struct vm_operations_struct snd_pcm_vm_ops_control =
3033 .fault = snd_pcm_mmap_control_fault,
3036 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3037 struct vm_area_struct *area)
3039 struct snd_pcm_runtime *runtime;
3040 long size;
3041 if (!(area->vm_flags & VM_READ))
3042 return -EINVAL;
3043 runtime = substream->runtime;
3044 size = area->vm_end - area->vm_start;
3045 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3046 return -EINVAL;
3047 area->vm_ops = &snd_pcm_vm_ops_control;
3048 area->vm_private_data = substream;
3049 area->vm_flags |= VM_RESERVED;
3050 return 0;
3052 #else /* ! coherent mmap */
3054 * don't support mmap for status and control records.
3056 static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3057 struct vm_area_struct *area)
3059 return -ENXIO;
3061 static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3062 struct vm_area_struct *area)
3064 return -ENXIO;
3066 #endif /* coherent mmap */
3069 * fault callback for mmapping a RAM page
3071 static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3072 struct vm_fault *vmf)
3074 struct snd_pcm_substream *substream = area->vm_private_data;
3075 struct snd_pcm_runtime *runtime;
3076 unsigned long offset;
3077 struct page * page;
3078 void *vaddr;
3079 size_t dma_bytes;
3081 if (substream == NULL)
3082 return VM_FAULT_SIGBUS;
3083 runtime = substream->runtime;
3084 offset = vmf->pgoff << PAGE_SHIFT;
3085 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3086 if (offset > dma_bytes - PAGE_SIZE)
3087 return VM_FAULT_SIGBUS;
3088 if (substream->ops->page) {
3089 page = substream->ops->page(substream, offset);
3090 if (!page)
3091 return VM_FAULT_SIGBUS;
3092 } else {
3093 vaddr = runtime->dma_area + offset;
3094 page = virt_to_page(vaddr);
3096 get_page(page);
3097 vmf->page = page;
3098 return 0;
3101 static const struct vm_operations_struct snd_pcm_vm_ops_data =
3103 .open = snd_pcm_mmap_data_open,
3104 .close = snd_pcm_mmap_data_close,
3105 .fault = snd_pcm_mmap_data_fault,
3109 * mmap the DMA buffer on RAM
3111 static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3112 struct vm_area_struct *area)
3114 area->vm_ops = &snd_pcm_vm_ops_data;
3115 area->vm_private_data = substream;
3116 area->vm_flags |= VM_RESERVED;
3117 atomic_inc(&substream->mmap_count);
3118 return 0;
3122 * mmap the DMA buffer on I/O memory area
3124 #if SNDRV_PCM_INFO_MMAP_IOMEM
3125 static const struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3127 .open = snd_pcm_mmap_data_open,
3128 .close = snd_pcm_mmap_data_close,
3131 int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3132 struct vm_area_struct *area)
3134 long size;
3135 unsigned long offset;
3137 #ifdef pgprot_noncached
3138 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3139 #endif
3140 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3141 area->vm_private_data = substream;
3142 area->vm_flags |= VM_IO;
3143 size = area->vm_end - area->vm_start;
3144 offset = area->vm_pgoff << PAGE_SHIFT;
3145 if (io_remap_pfn_range(area, area->vm_start,
3146 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3147 size, area->vm_page_prot))
3148 return -EAGAIN;
3149 atomic_inc(&substream->mmap_count);
3150 return 0;
3153 EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3154 #endif /* SNDRV_PCM_INFO_MMAP */
3157 * mmap DMA buffer
3159 int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3160 struct vm_area_struct *area)
3162 struct snd_pcm_runtime *runtime;
3163 long size;
3164 unsigned long offset;
3165 size_t dma_bytes;
3167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3168 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3169 return -EINVAL;
3170 } else {
3171 if (!(area->vm_flags & VM_READ))
3172 return -EINVAL;
3174 runtime = substream->runtime;
3175 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3176 return -EBADFD;
3177 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3178 return -ENXIO;
3179 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3180 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3181 return -EINVAL;
3182 size = area->vm_end - area->vm_start;
3183 offset = area->vm_pgoff << PAGE_SHIFT;
3184 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3185 if ((size_t)size > dma_bytes)
3186 return -EINVAL;
3187 if (offset > dma_bytes - size)
3188 return -EINVAL;
3190 if (substream->ops->mmap)
3191 return substream->ops->mmap(substream, area);
3192 else
3193 return snd_pcm_default_mmap(substream, area);
3196 EXPORT_SYMBOL(snd_pcm_mmap_data);
3198 static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3200 struct snd_pcm_file * pcm_file;
3201 struct snd_pcm_substream *substream;
3202 unsigned long offset;
3204 pcm_file = file->private_data;
3205 substream = pcm_file->substream;
3206 if (PCM_RUNTIME_CHECK(substream))
3207 return -ENXIO;
3209 offset = area->vm_pgoff << PAGE_SHIFT;
3210 switch (offset) {
3211 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3212 if (pcm_file->no_compat_mmap)
3213 return -ENXIO;
3214 return snd_pcm_mmap_status(substream, file, area);
3215 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3216 if (pcm_file->no_compat_mmap)
3217 return -ENXIO;
3218 return snd_pcm_mmap_control(substream, file, area);
3219 default:
3220 return snd_pcm_mmap_data(substream, file, area);
3222 return 0;
3225 static int snd_pcm_fasync(int fd, struct file * file, int on)
3227 struct snd_pcm_file * pcm_file;
3228 struct snd_pcm_substream *substream;
3229 struct snd_pcm_runtime *runtime;
3230 int err = -ENXIO;
3232 lock_kernel();
3233 pcm_file = file->private_data;
3234 substream = pcm_file->substream;
3235 if (PCM_RUNTIME_CHECK(substream))
3236 goto out;
3237 runtime = substream->runtime;
3238 err = fasync_helper(fd, file, on, &runtime->fasync);
3239 out:
3240 unlock_kernel();
3241 return err;
3245 * ioctl32 compat
3247 #ifdef CONFIG_COMPAT
3248 #include "pcm_compat.c"
3249 #else
3250 #define snd_pcm_ioctl_compat NULL
3251 #endif
3254 * To be removed helpers to keep binary compatibility
3257 #ifdef CONFIG_SND_SUPPORT_OLD_API
3258 #define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3259 #define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3261 static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3262 struct snd_pcm_hw_params_old *oparams)
3264 unsigned int i;
3266 memset(params, 0, sizeof(*params));
3267 params->flags = oparams->flags;
3268 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3269 params->masks[i].bits[0] = oparams->masks[i];
3270 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3271 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3272 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3273 params->info = oparams->info;
3274 params->msbits = oparams->msbits;
3275 params->rate_num = oparams->rate_num;
3276 params->rate_den = oparams->rate_den;
3277 params->fifo_size = oparams->fifo_size;
3280 static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3281 struct snd_pcm_hw_params *params)
3283 unsigned int i;
3285 memset(oparams, 0, sizeof(*oparams));
3286 oparams->flags = params->flags;
3287 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3288 oparams->masks[i] = params->masks[i].bits[0];
3289 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3290 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3291 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3292 oparams->info = params->info;
3293 oparams->msbits = params->msbits;
3294 oparams->rate_num = params->rate_num;
3295 oparams->rate_den = params->rate_den;
3296 oparams->fifo_size = params->fifo_size;
3299 static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3300 struct snd_pcm_hw_params_old __user * _oparams)
3302 struct snd_pcm_hw_params *params;
3303 struct snd_pcm_hw_params_old *oparams = NULL;
3304 int err;
3306 params = kmalloc(sizeof(*params), GFP_KERNEL);
3307 if (!params)
3308 return -ENOMEM;
3310 oparams = memdup_user(_oparams, sizeof(*oparams));
3311 if (IS_ERR(oparams)) {
3312 err = PTR_ERR(oparams);
3313 goto out;
3315 snd_pcm_hw_convert_from_old_params(params, oparams);
3316 err = snd_pcm_hw_refine(substream, params);
3317 snd_pcm_hw_convert_to_old_params(oparams, params);
3318 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3319 if (!err)
3320 err = -EFAULT;
3323 kfree(oparams);
3324 out:
3325 kfree(params);
3326 return err;
3329 static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3330 struct snd_pcm_hw_params_old __user * _oparams)
3332 struct snd_pcm_hw_params *params;
3333 struct snd_pcm_hw_params_old *oparams = NULL;
3334 int err;
3336 params = kmalloc(sizeof(*params), GFP_KERNEL);
3337 if (!params)
3338 return -ENOMEM;
3340 oparams = memdup_user(_oparams, sizeof(*oparams));
3341 if (IS_ERR(oparams)) {
3342 err = PTR_ERR(oparams);
3343 goto out;
3345 snd_pcm_hw_convert_from_old_params(params, oparams);
3346 err = snd_pcm_hw_params(substream, params);
3347 snd_pcm_hw_convert_to_old_params(oparams, params);
3348 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3349 if (!err)
3350 err = -EFAULT;
3353 kfree(oparams);
3354 out:
3355 kfree(params);
3356 return err;
3358 #endif /* CONFIG_SND_SUPPORT_OLD_API */
3360 #ifndef CONFIG_MMU
3361 unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3362 unsigned long len, unsigned long pgoff,
3363 unsigned long flags)
3365 return 0;
3367 #else
3368 # define dummy_get_unmapped_area NULL
3369 #endif
3372 * Register section
3375 const struct file_operations snd_pcm_f_ops[2] = {
3377 .owner = THIS_MODULE,
3378 .write = snd_pcm_write,
3379 .aio_write = snd_pcm_aio_write,
3380 .open = snd_pcm_playback_open,
3381 .release = snd_pcm_release,
3382 .poll = snd_pcm_playback_poll,
3383 .unlocked_ioctl = snd_pcm_playback_ioctl,
3384 .compat_ioctl = snd_pcm_ioctl_compat,
3385 .mmap = snd_pcm_mmap,
3386 .fasync = snd_pcm_fasync,
3387 .get_unmapped_area = dummy_get_unmapped_area,
3390 .owner = THIS_MODULE,
3391 .read = snd_pcm_read,
3392 .aio_read = snd_pcm_aio_read,
3393 .open = snd_pcm_capture_open,
3394 .release = snd_pcm_release,
3395 .poll = snd_pcm_capture_poll,
3396 .unlocked_ioctl = snd_pcm_capture_ioctl,
3397 .compat_ioctl = snd_pcm_ioctl_compat,
3398 .mmap = snd_pcm_mmap,
3399 .fasync = snd_pcm_fasync,
3400 .get_unmapped_area = dummy_get_unmapped_area,