source: GPL/branches/uniaud-2.0/alsa-kernel/core/pcm_native.c@ 307

Last change on this file since 307 was 307, checked in by Paul Smedley, 17 years ago

Fix for detection of sample rate from Yoda

File size: 97.1 KB
Line 
1/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 *
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22#ifdef TARGET_OS2
23#include <sound/core.h>
24#endif
25#include <linux/mm.h>
26#include <linux/file.h>
27#include <linux/slab.h>
28#include <linux/time.h>
29#include <linux/latency.h>
30#include <linux/uio.h>
31#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
38#include <asm/io.h>
39
40/*
41 * Compatibility
42 */
43
44struct snd_pcm_hw_params_old {
45 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
48 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
49 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
56 snd_pcm_uframes_t fifo_size;
57 unsigned char reserved[64];
58};
59
60#ifdef CONFIG_SND_SUPPORT_OLD_API
61#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
63
64static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68#endif
69static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
70
71/*
72 *
73 */
74
75#ifndef TARGET_OS2
76DEFINE_RWLOCK(snd_pcm_link_rwlock);
77#else
78rwlock_t snd_pcm_link_rwlock = RW_LOCK_UNLOCKED;
79#endif
80EXPORT_SYMBOL(snd_pcm_link_rwlock);
81
82static DECLARE_RWSEM(snd_pcm_link_rwsem);
83
84static inline mm_segment_t snd_enter_user(void)
85{
86 mm_segment_t fs = get_fs();
87 set_fs(get_ds());
88 return fs;
89}
90
91static inline void snd_leave_user(mm_segment_t fs)
92{
93 set_fs(fs);
94}
95
96
97
98int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
99{
100 struct snd_pcm_runtime *runtime;
101 struct snd_pcm *pcm = substream->pcm;
102 struct snd_pcm_str *pstr = substream->pstr;
103
104 snd_assert(substream != NULL, return -ENXIO);
105 memset(info, 0, sizeof(*info));
106 info->card = pcm->card->number;
107 info->device = pcm->device;
108 info->stream = substream->stream;
109 info->subdevice = substream->number;
110 strlcpy(info->id, pcm->id, sizeof(info->id));
111 strlcpy(info->name, pcm->name, sizeof(info->name));
112 info->dev_class = pcm->dev_class;
113 info->dev_subclass = pcm->dev_subclass;
114 info->subdevices_count = pstr->substream_count;
115 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
116 strlcpy(info->subname, substream->name, sizeof(info->subname));
117 runtime = substream->runtime;
118 /* AB: FIXME!!! This is definitely nonsense */
119 if (runtime) {
120 info->sync = runtime->sync;
121 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
122 }
123 return 0;
124}
125
126int snd_pcm_info_user(struct snd_pcm_substream *substream,
127 struct snd_pcm_info __user * _info)
128{
129 struct snd_pcm_info *info;
130 int err;
131
132 info = kmalloc(sizeof(*info), GFP_KERNEL);
133 if (! info)
134 return -ENOMEM;
135 err = snd_pcm_info(substream, info);
136 if (err >= 0) {
137 if (copy_to_user(_info, info, sizeof(*info)))
138 err = -EFAULT;
139 }
140 kfree(info);
141 return err;
142}
143
144#undef RULES_DEBUG
145
146#ifdef RULES_DEBUG
147#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
148char *snd_pcm_hw_param_names[] = {
149 HW_PARAM(ACCESS),
150 HW_PARAM(FORMAT),
151 HW_PARAM(SUBFORMAT),
152 HW_PARAM(SAMPLE_BITS),
153 HW_PARAM(FRAME_BITS),
154 HW_PARAM(CHANNELS),
155 HW_PARAM(RATE),
156 HW_PARAM(PERIOD_TIME),
157 HW_PARAM(PERIOD_SIZE),
158 HW_PARAM(PERIOD_BYTES),
159 HW_PARAM(PERIODS),
160 HW_PARAM(BUFFER_TIME),
161 HW_PARAM(BUFFER_SIZE),
162 HW_PARAM(BUFFER_BYTES),
163 HW_PARAM(TICK_TIME),
164};
165#endif
166
167int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
168 struct snd_pcm_hw_params *params)
169{
170 unsigned int k;
171 struct snd_pcm_hardware *hw;
172 struct snd_interval *i = NULL;
173 struct snd_mask *m = NULL;
174 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
175#ifndef TARGET_OS2
176 unsigned int rstamps[constrs->rules_num];
177#else
178 unsigned int rstamps[32];
179#endif
180 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
181 unsigned int stamp = 2;
182 int changed, again;
183
184 params->info = 0;
185 params->fifo_size = 0;
186 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
187 params->msbits = 0;
188 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
189 params->rate_num = 0;
190 params->rate_den = 0;
191 }
192
193 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
194 m = hw_param_mask(params, k);
195 if (snd_mask_empty(m))
196 return -EINVAL;
197 if (!(params->rmask & (1 << k)))
198 continue;
199#ifdef RULES_DEBUG
200 printk("%s = ", snd_pcm_hw_param_names[k]);
201 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
202#endif
203 changed = snd_mask_refine(m, constrs_mask(constrs, k));
204#ifdef RULES_DEBUG
205 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
206#endif
207 if (changed)
208 params->cmask |= 1 << k;
209 if (changed < 0)
210 return changed;
211 }
212
213 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
214 i = hw_param_interval(params, k);
215 if (snd_interval_empty(i))
216 return -EINVAL;
217 if (!(params->rmask & (1 << k)))
218 continue;
219#ifdef RULES_DEBUG
220 printk("%s = ", snd_pcm_hw_param_names[k]);
221 if (i->empty)
222 printk("empty");
223 else
224 printk("%c%u %u%c",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
227 printk(" -> ");
228#endif
229 changed = snd_interval_refine(i, constrs_interval(constrs, k));
230#ifdef RULES_DEBUG
231 if (i->empty)
232 printk("empty\n");
233 else
234 printk("%c%u %u%c\n",
235 i->openmin ? '(' : '[', i->min,
236 i->max, i->openmax ? ')' : ']');
237#endif
238 if (changed)
239 params->cmask |= 1 << k;
240 if (changed < 0)
241 return changed;
242 }
243
244 for (k = 0; k < constrs->rules_num; k++)
245 rstamps[k] = 0;
246 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
247 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
248 do {
249 again = 0;
250 for (k = 0; k < constrs->rules_num; k++) {
251 struct snd_pcm_hw_rule *r = &constrs->rules[k];
252 unsigned int d;
253 int doit = 0;
254 if (r->cond && !(r->cond & params->flags))
255 continue;
256 for (d = 0; r->deps[d] >= 0; d++) {
257 if (vstamps[r->deps[d]] > rstamps[k]) {
258 doit = 1;
259 break;
260 }
261 }
262 if (!doit)
263 continue;
264#ifdef RULES_DEBUG
265 printk("Rule %d [%p]: ", k, r->func);
266 if (r->var >= 0) {
267 printk("%s = ", snd_pcm_hw_param_names[r->var]);
268 if (hw_is_mask(r->var)) {
269 m = hw_param_mask(params, r->var);
270 printk("%x", *m->bits);
271 } else {
272 i = hw_param_interval(params, r->var);
273 if (i->empty)
274 printk("empty");
275 else
276 printk("%c%u %u%c",
277 i->openmin ? '(' : '[', i->min,
278 i->max, i->openmax ? ')' : ']');
279 }
280 }
281#endif
282 changed = r->func(params, r);
283#ifdef RULES_DEBUG
284 if (r->var >= 0) {
285 printk(" -> ");
286 if (hw_is_mask(r->var))
287 printk("%x", *m->bits);
288 else {
289 if (i->empty)
290 printk("empty");
291 else
292 printk("%c%u %u%c",
293 i->openmin ? '(' : '[', i->min,
294 i->max, i->openmax ? ')' : ']');
295 }
296 }
297 printk("\n");
298#endif
299 rstamps[k] = stamp;
300 if (changed && r->var >= 0) {
301 params->cmask |= (1 << r->var);
302 vstamps[r->var] = stamp;
303 again = 1;
304 }
305 if (changed < 0)
306 return changed;
307 stamp++;
308 }
309 } while (again);
310 if (!params->msbits) {
311 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
312 if (snd_interval_single(i))
313 params->msbits = snd_interval_value(i);
314 }
315
316 if (!params->rate_den) {
317 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
318 if (snd_interval_single(i)) {
319 params->rate_num = snd_interval_value(i);
320 params->rate_den = 1;
321 }
322 }
323
324 hw = &substream->runtime->hw;
325 if (!params->info)
326 params->info = hw->info;
327 if (!params->fifo_size)
328 params->fifo_size = hw->fifo_size;
329 params->rmask = 0;
330 return 0;
331}
332
333EXPORT_SYMBOL(snd_pcm_hw_refine);
334
335static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params __user * _params)
337{
338 struct snd_pcm_hw_params *params;
339 int err;
340
341 params = kmalloc(sizeof(*params), GFP_KERNEL);
342 if (!params) {
343 err = -ENOMEM;
344 goto out;
345 }
346 if (copy_from_user(params, _params, sizeof(*params))) {
347 err = -EFAULT;
348 goto out;
349 }
350 err = snd_pcm_hw_refine(substream, params);
351 if (copy_to_user(_params, params, sizeof(*params))) {
352 if (!err)
353 err = -EFAULT;
354 }
355out:
356 kfree(params);
357 return err;
358}
359
360static int period_to_usecs(struct snd_pcm_runtime *runtime)
361{
362 int usecs;
363
364 if (! runtime->rate)
365 return -1; /* invalid */
366
367 /* take 75% of period time as the deadline */
368 usecs = (750000 / runtime->rate) * runtime->period_size;
369 usecs += ((750000 % runtime->rate) * runtime->period_size) /
370 runtime->rate;
371
372 return usecs;
373}
374
375static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
376 struct snd_pcm_hw_params *params)
377{
378 struct snd_pcm_runtime *runtime;
379 int err, usecs;
380 unsigned int bits;
381 snd_pcm_uframes_t frames;
382
383 snd_assert(substream != NULL, return -ENXIO);
384 runtime = substream->runtime;
385 snd_assert(runtime != NULL, return -ENXIO);
386 snd_pcm_stream_lock_irq(substream);
387 switch (runtime->status->state) {
388 case SNDRV_PCM_STATE_OPEN:
389 case SNDRV_PCM_STATE_SETUP:
390 case SNDRV_PCM_STATE_PREPARED:
391 break;
392 default:
393 snd_pcm_stream_unlock_irq(substream);
394 return -EBADFD;
395 }
396 snd_pcm_stream_unlock_irq(substream);
397#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
398 if (!substream->oss.oss)
399#endif
400 if (atomic_read(&substream->mmap_count))
401 return -EBADFD;
402
403 params->rmask = ~0U;
404 err = snd_pcm_hw_refine(substream, params);
405 if (err < 0)
406 goto _error;
407
408 err = snd_pcm_hw_params_choose(substream, params);
409 if (err < 0)
410 goto _error;
411
412 if (substream->ops->hw_params != NULL) {
413 err = substream->ops->hw_params(substream, params);
414 if (err < 0)
415 goto _error;
416 }
417
418 runtime->access = params_access(params);
419 runtime->format = params_format(params);
420 runtime->subformat = params_subformat(params);
421 runtime->channels = params_channels(params);
422 runtime->rate = params_rate(params);
423 runtime->period_size = params_period_size(params);
424 runtime->periods = params_periods(params);
425 runtime->buffer_size = params_buffer_size(params);
426 runtime->info = params->info;
427 runtime->rate_num = params->rate_num;
428 runtime->rate_den = params->rate_den;
429
430 bits = snd_pcm_format_physical_width(runtime->format);
431 runtime->sample_bits = bits;
432 bits *= runtime->channels;
433 runtime->frame_bits = bits;
434 frames = 1;
435 while (bits % 8 != 0) {
436 bits *= 2;
437 frames *= 2;
438 }
439 runtime->byte_align = bits / 8;
440 runtime->min_align = frames;
441
442 /* Default sw params */
443 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
444 runtime->period_step = 1;
445 runtime->control->avail_min = runtime->period_size;
446 runtime->start_threshold = 1;
447 runtime->stop_threshold = runtime->buffer_size;
448 runtime->silence_threshold = 0;
449 runtime->silence_size = 0;
450 runtime->boundary = runtime->buffer_size;
451 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
452 runtime->boundary *= 2;
453
454 snd_pcm_timer_resolution_change(substream);
455 runtime->status->state = SNDRV_PCM_STATE_SETUP;
456
457 remove_acceptable_latency(substream->latency_id);
458 if ((usecs = period_to_usecs(runtime)) >= 0)
459 set_acceptable_latency(substream->latency_id, usecs);
460 return 0;
461 _error:
462 /* hardware might be unuseable from this time,
463 so we force application to retry to set
464 the correct hardware parameter settings */
465 runtime->status->state = SNDRV_PCM_STATE_OPEN;
466 if (substream->ops->hw_free != NULL)
467 substream->ops->hw_free(substream);
468 return err;
469}
470
471static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
472 struct snd_pcm_hw_params __user * _params)
473{
474 struct snd_pcm_hw_params *params;
475 int err;
476
477 params = kmalloc(sizeof(*params), GFP_KERNEL);
478 if (!params) {
479 err = -ENOMEM;
480 goto out;
481 }
482 if (copy_from_user(params, _params, sizeof(*params))) {
483 err = -EFAULT;
484 goto out;
485 }
486 err = snd_pcm_hw_params(substream, params);
487 if (copy_to_user(_params, params, sizeof(*params))) {
488 if (!err)
489 err = -EFAULT;
490 }
491out:
492 kfree(params);
493 return err;
494}
495
496static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
497{
498 struct snd_pcm_runtime *runtime;
499 int result = 0;
500
501 snd_assert(substream != NULL, return -ENXIO);
502 runtime = substream->runtime;
503 snd_assert(runtime != NULL, return -ENXIO);
504 snd_pcm_stream_lock_irq(substream);
505 switch (runtime->status->state) {
506 case SNDRV_PCM_STATE_SETUP:
507 case SNDRV_PCM_STATE_PREPARED:
508 break;
509 default:
510 snd_pcm_stream_unlock_irq(substream);
511 return -EBADFD;
512 }
513 snd_pcm_stream_unlock_irq(substream);
514 if (atomic_read(&substream->mmap_count))
515 return -EBADFD;
516 if (substream->ops->hw_free)
517 result = substream->ops->hw_free(substream);
518 runtime->status->state = SNDRV_PCM_STATE_OPEN;
519 remove_acceptable_latency(substream->latency_id);
520 return result;
521}
522
523static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
524 struct snd_pcm_sw_params *params)
525{
526 struct snd_pcm_runtime *runtime;
527
528 snd_assert(substream != NULL, return -ENXIO);
529 runtime = substream->runtime;
530 snd_assert(runtime != NULL, return -ENXIO);
531 snd_pcm_stream_lock_irq(substream);
532 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
533 snd_pcm_stream_unlock_irq(substream);
534 return -EBADFD;
535 }
536 snd_pcm_stream_unlock_irq(substream);
537
538 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
539 return -EINVAL;
540 if (params->avail_min == 0)
541 return -EINVAL;
542 if (params->silence_size >= runtime->boundary) {
543 if (params->silence_threshold != 0)
544 return -EINVAL;
545 } else {
546 if (params->silence_size > params->silence_threshold)
547 return -EINVAL;
548 if (params->silence_threshold > runtime->buffer_size)
549 return -EINVAL;
550 }
551 snd_pcm_stream_lock_irq(substream);
552 runtime->tstamp_mode = params->tstamp_mode;
553 runtime->period_step = params->period_step;
554 runtime->control->avail_min = params->avail_min;
555 runtime->start_threshold = params->start_threshold;
556 runtime->stop_threshold = params->stop_threshold;
557 runtime->silence_threshold = params->silence_threshold;
558 runtime->silence_size = params->silence_size;
559 params->boundary = runtime->boundary;
560 if (snd_pcm_running(substream)) {
561 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
562 runtime->silence_size > 0)
563 snd_pcm_playback_silence(substream, ULONG_MAX);
564 wake_up(&runtime->sleep);
565 }
566 snd_pcm_stream_unlock_irq(substream);
567 return 0;
568}
569
570static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
571 struct snd_pcm_sw_params __user * _params)
572{
573 struct snd_pcm_sw_params params;
574 int err;
575 if (copy_from_user(&params, _params, sizeof(params)))
576 return -EFAULT;
577 err = snd_pcm_sw_params(substream, &params);
578 if (copy_to_user(_params, &params, sizeof(params)))
579 return -EFAULT;
580 return err;
581}
582
583int snd_pcm_status(struct snd_pcm_substream *substream,
584 struct snd_pcm_status *status)
585{
586 struct snd_pcm_runtime *runtime = substream->runtime;
587
588 snd_pcm_stream_lock_irq(substream);
589 status->state = runtime->status->state;
590 status->suspended_state = runtime->status->suspended_state;
591 if (status->state == SNDRV_PCM_STATE_OPEN)
592 goto _end;
593 status->trigger_tstamp = runtime->trigger_tstamp;
594 if (snd_pcm_running(substream)) {
595 snd_pcm_update_hw_ptr(substream);
596 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
597 status->tstamp = runtime->status->tstamp;
598 goto _tstamp_end;
599 }
600 }
601 snd_pcm_gettime(runtime, &status->tstamp);
602 _tstamp_end:
603 status->appl_ptr = runtime->control->appl_ptr;
604 status->hw_ptr = runtime->status->hw_ptr;
605 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
606 status->avail = snd_pcm_playback_avail(runtime);
607 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
608 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
609 status->delay = runtime->buffer_size - status->avail;
610 else
611 status->delay = 0;
612 } else {
613 status->avail = snd_pcm_capture_avail(runtime);
614 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
615 status->delay = status->avail;
616 else
617 status->delay = 0;
618 }
619 status->avail_max = runtime->avail_max;
620 status->overrange = runtime->overrange;
621 runtime->avail_max = 0;
622 runtime->overrange = 0;
623 _end:
624 snd_pcm_stream_unlock_irq(substream);
625 return 0;
626}
627
628static int snd_pcm_status_user(struct snd_pcm_substream *substream,
629 struct snd_pcm_status __user * _status)
630{
631 struct snd_pcm_status status;
632 struct snd_pcm_runtime *runtime;
633 int res;
634
635 snd_assert(substream != NULL, return -ENXIO);
636 runtime = substream->runtime;
637 memset(&status, 0, sizeof(status));
638 res = snd_pcm_status(substream, &status);
639 if (res < 0)
640 return res;
641 if (copy_to_user(_status, &status, sizeof(status)))
642 return -EFAULT;
643 return 0;
644}
645
646static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
647 struct snd_pcm_channel_info * info)
648{
649 struct snd_pcm_runtime *runtime;
650 unsigned int channel;
651
652 snd_assert(substream != NULL, return -ENXIO);
653 channel = info->channel;
654 runtime = substream->runtime;
655 snd_pcm_stream_lock_irq(substream);
656 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
657 snd_pcm_stream_unlock_irq(substream);
658 return -EBADFD;
659 }
660 snd_pcm_stream_unlock_irq(substream);
661 if (channel >= runtime->channels)
662 return -EINVAL;
663 memset(info, 0, sizeof(*info));
664 info->channel = channel;
665 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
666}
667
668static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
669 struct snd_pcm_channel_info __user * _info)
670{
671 struct snd_pcm_channel_info info;
672 int res;
673
674 if (copy_from_user(&info, _info, sizeof(info)))
675 return -EFAULT;
676 res = snd_pcm_channel_info(substream, &info);
677 if (res < 0)
678 return res;
679 if (copy_to_user(_info, &info, sizeof(info)))
680 return -EFAULT;
681 return 0;
682}
683
684static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
685{
686 struct snd_pcm_runtime *runtime = substream->runtime;
687 if (runtime->trigger_master == NULL)
688 return;
689 if (runtime->trigger_master == substream) {
690 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
691 } else {
692 snd_pcm_trigger_tstamp(runtime->trigger_master);
693 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
694 }
695 runtime->trigger_master = NULL;
696}
697
698struct action_ops {
699 int (*pre_action)(struct snd_pcm_substream *substream, int state);
700 int (*do_action)(struct snd_pcm_substream *substream, int state);
701 void (*undo_action)(struct snd_pcm_substream *substream, int state);
702 void (*post_action)(struct snd_pcm_substream *substream, int state);
703};
704
705/*
706 * this functions is core for handling of linked stream
707 * Note: the stream state might be changed also on failure
708 * Note2: call with calling stream lock + link lock
709 */
710static int snd_pcm_action_group(struct action_ops *ops,
711 struct snd_pcm_substream *substream,
712 int state, int do_lock)
713{
714 struct snd_pcm_substream *s = NULL;
715 struct snd_pcm_substream *s1;
716 int res = 0;
717
718 snd_pcm_group_for_each_entry(s, substream) {
719 if (do_lock && s != substream)
720 spin_lock_nested(&s->self_group.lock,
721 SINGLE_DEPTH_NESTING);
722 res = ops->pre_action(s, state);
723 if (res < 0)
724 goto _unlock;
725 }
726 snd_pcm_group_for_each_entry(s, substream) {
727 res = ops->do_action(s, state);
728 if (res < 0) {
729 if (ops->undo_action) {
730 snd_pcm_group_for_each_entry(s1, substream) {
731 if (s1 == s) /* failed stream */
732 break;
733 ops->undo_action(s1, state);
734 }
735 }
736 s = NULL; /* unlock all */
737 goto _unlock;
738 }
739 }
740 snd_pcm_group_for_each_entry(s, substream) {
741 ops->post_action(s, state);
742 }
743 _unlock:
744 if (do_lock) {
745 /* unlock streams */
746 snd_pcm_group_for_each_entry(s1, substream) {
747 if (s1 != substream)
748 spin_unlock(&s1->self_group.lock);
749 if (s1 == s) /* end */
750 break;
751 }
752 }
753 return res;
754}
755
756/*
757 * Note: call with stream lock
758 */
759static int snd_pcm_action_single(struct action_ops *ops,
760 struct snd_pcm_substream *substream,
761 int state)
762{
763 int res;
764
765 res = ops->pre_action(substream, state);
766 if (res < 0)
767 return res;
768 res = ops->do_action(substream, state);
769 if (res == 0)
770 ops->post_action(substream, state);
771 else if (ops->undo_action)
772 ops->undo_action(substream, state);
773 return res;
774}
775
776/*
777 * Note: call with stream lock
778 */
779static int snd_pcm_action(struct action_ops *ops,
780 struct snd_pcm_substream *substream,
781 int state)
782{
783 int res;
784
785 if (snd_pcm_stream_linked(substream)) {
786 if (!spin_trylock(&substream->group->lock)) {
787 spin_unlock(&substream->self_group.lock);
788 spin_lock(&substream->group->lock);
789 spin_lock(&substream->self_group.lock);
790 }
791 res = snd_pcm_action_group(ops, substream, state, 1);
792 spin_unlock(&substream->group->lock);
793 } else {
794 res = snd_pcm_action_single(ops, substream, state);
795 }
796 return res;
797}
798
799/*
800 * Note: don't use any locks before
801 */
802static int snd_pcm_action_lock_irq(struct action_ops *ops,
803 struct snd_pcm_substream *substream,
804 int state)
805{
806 int res;
807
808 read_lock_irq(&snd_pcm_link_rwlock);
809 if (snd_pcm_stream_linked(substream)) {
810 spin_lock(&substream->group->lock);
811 spin_lock(&substream->self_group.lock);
812 res = snd_pcm_action_group(ops, substream, state, 1);
813 spin_unlock(&substream->self_group.lock);
814 spin_unlock(&substream->group->lock);
815 } else {
816 spin_lock(&substream->self_group.lock);
817 res = snd_pcm_action_single(ops, substream, state);
818 spin_unlock(&substream->self_group.lock);
819 }
820 read_unlock_irq(&snd_pcm_link_rwlock);
821 return res;
822}
823
824/*
825 */
826static int snd_pcm_action_nonatomic(struct action_ops *ops,
827 struct snd_pcm_substream *substream,
828 int state)
829{
830 int res;
831
832 down_read(&snd_pcm_link_rwsem);
833 if (snd_pcm_stream_linked(substream))
834 res = snd_pcm_action_group(ops, substream, state, 0);
835 else
836 res = snd_pcm_action_single(ops, substream, state);
837 up_read(&snd_pcm_link_rwsem);
838 return res;
839}
840
841/*
842 * start callbacks
843 */
844static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
845{
846 struct snd_pcm_runtime *runtime = substream->runtime;
847 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
848 return -EBADFD;
849 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
850 !snd_pcm_playback_data(substream))
851 return -EPIPE;
852 runtime->trigger_master = substream;
853 return 0;
854}
855
856static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
857{
858 if (substream->runtime->trigger_master != substream)
859 return 0;
860 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
861}
862
863static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
864{
865 if (substream->runtime->trigger_master == substream)
866 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
867}
868
869static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
870{
871 struct snd_pcm_runtime *runtime = substream->runtime;
872 snd_pcm_trigger_tstamp(substream);
873 runtime->status->state = state;
874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
875 runtime->silence_size > 0)
876 snd_pcm_playback_silence(substream, ULONG_MAX);
877 if (substream->timer)
878 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
879 &runtime->trigger_tstamp);
880}
881
882static struct action_ops snd_pcm_action_start = {
883 .pre_action = snd_pcm_pre_start,
884 .do_action = snd_pcm_do_start,
885 .undo_action = snd_pcm_undo_start,
886 .post_action = snd_pcm_post_start
887};
888
889/**
890 * snd_pcm_start
891 * @substream: the PCM substream instance
892 *
893 * Start all linked streams.
894 */
895int snd_pcm_start(struct snd_pcm_substream *substream)
896{
897 return snd_pcm_action(&snd_pcm_action_start, substream,
898 SNDRV_PCM_STATE_RUNNING);
899}
900
901/*
902 * stop callbacks
903 */
904static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
905{
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
908 return -EBADFD;
909 runtime->trigger_master = substream;
910 return 0;
911}
912
913static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
914{
915 if (substream->runtime->trigger_master == substream &&
916 snd_pcm_running(substream))
917 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
918 return 0; /* unconditonally stop all substreams */
919}
920
921static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
922{
923 struct snd_pcm_runtime *runtime = substream->runtime;
924 if (runtime->status->state != state) {
925 snd_pcm_trigger_tstamp(substream);
926 if (substream->timer)
927 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
928 &runtime->trigger_tstamp);
929 runtime->status->state = state;
930 }
931 wake_up(&runtime->sleep);
932}
933
934static struct action_ops snd_pcm_action_stop = {
935 .pre_action = snd_pcm_pre_stop,
936 .do_action = snd_pcm_do_stop,
937 .post_action = snd_pcm_post_stop
938};
939
940/**
941 * snd_pcm_stop
942 * @substream: the PCM substream instance
943 * @state: PCM state after stopping the stream
944 *
945 * Try to stop all running streams in the substream group.
946 * The state of each stream is changed to the given value after that unconditionally.
947 */
948int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
949{
950 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
951}
952
953EXPORT_SYMBOL(snd_pcm_stop);
954
955/**
956 * snd_pcm_drain_done
957 * @substream: the PCM substream
958 *
959 * Stop the DMA only when the given stream is playback.
960 * The state is changed to SETUP.
961 * Unlike snd_pcm_stop(), this affects only the given stream.
962 */
963int snd_pcm_drain_done(struct snd_pcm_substream *substream)
964{
965 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
966 SNDRV_PCM_STATE_SETUP);
967}
968
969/*
970 * pause callbacks
971 */
972static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
973{
974 struct snd_pcm_runtime *runtime = substream->runtime;
975 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
976 return -ENOSYS;
977 if (push) {
978 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
979 return -EBADFD;
980 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
981 return -EBADFD;
982 runtime->trigger_master = substream;
983 return 0;
984}
985
986static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
987{
988 if (substream->runtime->trigger_master != substream)
989 return 0;
990 return substream->ops->trigger(substream,
991 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
992 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
993}
994
995static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
996{
997 if (substream->runtime->trigger_master == substream)
998 substream->ops->trigger(substream,
999 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1000 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1001}
1002
1003static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1004{
1005 struct snd_pcm_runtime *runtime = substream->runtime;
1006 snd_pcm_trigger_tstamp(substream);
1007 if (push) {
1008 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1009 if (substream->timer)
1010 snd_timer_notify(substream->timer,
1011 SNDRV_TIMER_EVENT_MPAUSE,
1012 &runtime->trigger_tstamp);
1013 wake_up(&runtime->sleep);
1014 } else {
1015 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1016 if (substream->timer)
1017 snd_timer_notify(substream->timer,
1018 SNDRV_TIMER_EVENT_MCONTINUE,
1019 &runtime->trigger_tstamp);
1020 }
1021}
1022
1023static struct action_ops snd_pcm_action_pause = {
1024 .pre_action = snd_pcm_pre_pause,
1025 .do_action = snd_pcm_do_pause,
1026 .undo_action = snd_pcm_undo_pause,
1027 .post_action = snd_pcm_post_pause
1028};
1029
1030/*
1031 * Push/release the pause for all linked streams.
1032 */
1033static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1034{
1035 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1036}
1037
1038#ifdef CONFIG_PM
1039/* suspend */
1040
1041static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1042{
1043 struct snd_pcm_runtime *runtime = substream->runtime;
1044 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1045 return -EBUSY;
1046 runtime->trigger_master = substream;
1047 return 0;
1048}
1049
1050static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1051{
1052 struct snd_pcm_runtime *runtime = substream->runtime;
1053 if (runtime->trigger_master != substream)
1054 return 0;
1055 if (! snd_pcm_running(substream))
1056 return 0;
1057 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1058 return 0; /* suspend unconditionally */
1059}
1060
1061static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1062{
1063 struct snd_pcm_runtime *runtime = substream->runtime;
1064 snd_pcm_trigger_tstamp(substream);
1065 if (substream->timer)
1066 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1067 &runtime->trigger_tstamp);
1068 runtime->status->suspended_state = runtime->status->state;
1069 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1070 wake_up(&runtime->sleep);
1071}
1072
1073static struct action_ops snd_pcm_action_suspend = {
1074 .pre_action = snd_pcm_pre_suspend,
1075 .do_action = snd_pcm_do_suspend,
1076 .post_action = snd_pcm_post_suspend
1077};
1078
1079/**
1080 * snd_pcm_suspend
1081 * @substream: the PCM substream
1082 *
1083 * Trigger SUSPEND to all linked streams.
1084 * After this call, all streams are changed to SUSPENDED state.
1085 */
1086int snd_pcm_suspend(struct snd_pcm_substream *substream)
1087{
1088 int err;
1089 unsigned long flags;
1090
1091 if (! substream)
1092 return 0;
1093
1094 snd_pcm_stream_lock_irqsave(substream, flags);
1095 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1096 snd_pcm_stream_unlock_irqrestore(substream, flags);
1097 return err;
1098}
1099
1100EXPORT_SYMBOL(snd_pcm_suspend);
1101
1102/**
1103 * snd_pcm_suspend_all
1104 * @pcm: the PCM instance
1105 *
1106 * Trigger SUSPEND to all substreams in the given pcm.
1107 * After this call, all streams are changed to SUSPENDED state.
1108 */
1109int snd_pcm_suspend_all(struct snd_pcm *pcm)
1110{
1111 struct snd_pcm_substream *substream;
1112 int stream, err = 0;
1113
1114 if (! pcm)
1115 return 0;
1116
1117 for (stream = 0; stream < 2; stream++) {
1118 for (substream = pcm->streams[stream].substream;
1119 substream; substream = substream->next) {
1120 /* FIXME: the open/close code should lock this as well */
1121 if (substream->runtime == NULL)
1122 continue;
1123 err = snd_pcm_suspend(substream);
1124 if (err < 0 && err != -EBUSY)
1125 return err;
1126 }
1127 }
1128 return 0;
1129}
1130
1131EXPORT_SYMBOL(snd_pcm_suspend_all);
1132
1133/* resume */
1134
1135static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1136{
1137 struct snd_pcm_runtime *runtime = substream->runtime;
1138 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1139 return -ENOSYS;
1140 runtime->trigger_master = substream;
1141 return 0;
1142}
1143
1144static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1145{
1146 struct snd_pcm_runtime *runtime = substream->runtime;
1147 if (runtime->trigger_master != substream)
1148 return 0;
1149 /* DMA not running previously? */
1150 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1151 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1152 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1153 return 0;
1154 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1155}
1156
1157static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1158{
1159 if (substream->runtime->trigger_master == substream &&
1160 snd_pcm_running(substream))
1161 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1162}
1163
1164static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1165{
1166 struct snd_pcm_runtime *runtime = substream->runtime;
1167 snd_pcm_trigger_tstamp(substream);
1168 if (substream->timer)
1169 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1170 &runtime->trigger_tstamp);
1171 runtime->status->state = runtime->status->suspended_state;
1172}
1173
1174static struct action_ops snd_pcm_action_resume = {
1175 .pre_action = snd_pcm_pre_resume,
1176 .do_action = snd_pcm_do_resume,
1177 .undo_action = snd_pcm_undo_resume,
1178 .post_action = snd_pcm_post_resume
1179};
1180
1181static int snd_pcm_resume(struct snd_pcm_substream *substream)
1182{
1183 struct snd_card *card = substream->pcm->card;
1184 int res;
1185
1186 snd_power_lock(card);
1187 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1188 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1189 snd_power_unlock(card);
1190 return res;
1191}
1192
1193#else
1194
1195static int snd_pcm_resume(struct snd_pcm_substream *substream)
1196{
1197 return -ENOSYS;
1198}
1199
1200#endif /* CONFIG_PM */
1201
1202/*
1203 * xrun ioctl
1204 *
1205 * Change the RUNNING stream(s) to XRUN state.
1206 */
1207static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1208{
1209 struct snd_card *card = substream->pcm->card;
1210 struct snd_pcm_runtime *runtime = substream->runtime;
1211 int result;
1212
1213 snd_power_lock(card);
1214 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1215 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1216 if (result < 0)
1217 goto _unlock;
1218 }
1219
1220 snd_pcm_stream_lock_irq(substream);
1221 switch (runtime->status->state) {
1222 case SNDRV_PCM_STATE_XRUN:
1223 result = 0; /* already there */
1224 break;
1225 case SNDRV_PCM_STATE_RUNNING:
1226 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1227 break;
1228 default:
1229 result = -EBADFD;
1230 }
1231 snd_pcm_stream_unlock_irq(substream);
1232 _unlock:
1233 snd_power_unlock(card);
1234 return result;
1235}
1236
1237/*
1238 * reset ioctl
1239 */
1240static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1241{
1242 struct snd_pcm_runtime *runtime = substream->runtime;
1243 switch (runtime->status->state) {
1244 case SNDRV_PCM_STATE_RUNNING:
1245 case SNDRV_PCM_STATE_PREPARED:
1246 case SNDRV_PCM_STATE_PAUSED:
1247 case SNDRV_PCM_STATE_SUSPENDED:
1248 return 0;
1249 default:
1250 return -EBADFD;
1251 }
1252}
1253
1254static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1255{
1256 struct snd_pcm_runtime *runtime = substream->runtime;
1257 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1258 if (err < 0)
1259 return err;
1260 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1261 runtime->hw_ptr_base = 0;
1262 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1263 runtime->status->hw_ptr % runtime->period_size;
1264 runtime->silence_start = runtime->status->hw_ptr;
1265 runtime->silence_filled = 0;
1266 return 0;
1267}
1268
1269static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1270{
1271 struct snd_pcm_runtime *runtime = substream->runtime;
1272 runtime->control->appl_ptr = runtime->status->hw_ptr;
1273 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1274 runtime->silence_size > 0)
1275 snd_pcm_playback_silence(substream, ULONG_MAX);
1276}
1277
1278static struct action_ops snd_pcm_action_reset = {
1279 .pre_action = snd_pcm_pre_reset,
1280 .do_action = snd_pcm_do_reset,
1281 .post_action = snd_pcm_post_reset
1282};
1283
1284static int snd_pcm_reset(struct snd_pcm_substream *substream)
1285{
1286 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1287}
1288
1289/*
1290 * prepare ioctl
1291 */
1292/* we use the second argument for updating f_flags */
1293static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1294 int f_flags)
1295{
1296 struct snd_pcm_runtime *runtime = substream->runtime;
1297 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1298 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1299 return -EBADFD;
1300 if (snd_pcm_running(substream))
1301 return -EBUSY;
1302 substream->f_flags = f_flags;
1303 return 0;
1304}
1305
1306static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1307{
1308 int err;
1309 err = substream->ops->prepare(substream);
1310 if (err < 0)
1311 return err;
1312 return snd_pcm_do_reset(substream, 0);
1313}
1314
1315static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1316{
1317 struct snd_pcm_runtime *runtime = substream->runtime;
1318 runtime->control->appl_ptr = runtime->status->hw_ptr;
1319 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1320}
1321
1322static struct action_ops snd_pcm_action_prepare = {
1323 .pre_action = snd_pcm_pre_prepare,
1324 .do_action = snd_pcm_do_prepare,
1325 .post_action = snd_pcm_post_prepare
1326};
1327
1328/**
1329 * snd_pcm_prepare
1330 * @substream: the PCM substream instance
1331 * @file: file to refer f_flags
1332 *
1333 * Prepare the PCM substream to be triggerable.
1334 */
1335static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1336 struct file *file)
1337{
1338 int res;
1339 struct snd_card *card = substream->pcm->card;
1340 int f_flags;
1341
1342 if (file)
1343 f_flags = file->f_flags;
1344 else
1345 f_flags = substream->f_flags;
1346
1347 snd_power_lock(card);
1348 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
1349 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1350 substream, f_flags);
1351 snd_power_unlock(card);
1352 return res;
1353}
1354
1355/*
1356 * drain ioctl
1357 */
1358
1359static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1360{
1361 if (substream->f_flags & O_NONBLOCK)
1362 return -EAGAIN;
1363 substream->runtime->trigger_master = substream;
1364 return 0;
1365}
1366
1367static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1368{
1369 struct snd_pcm_runtime *runtime = substream->runtime;
1370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1371 switch (runtime->status->state) {
1372 case SNDRV_PCM_STATE_PREPARED:
1373 /* start playback stream if possible */
1374 if (! snd_pcm_playback_empty(substream)) {
1375 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1376 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1377 }
1378 break;
1379 case SNDRV_PCM_STATE_RUNNING:
1380 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1381 break;
1382 default:
1383 break;
1384 }
1385 } else {
1386 /* stop running stream */
1387 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1388 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
1389 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1390 snd_pcm_do_stop(substream, new_state);
1391 snd_pcm_post_stop(substream, new_state);
1392 }
1393 }
1394 return 0;
1395}
1396
1397static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1398{
1399}
1400
1401static struct action_ops snd_pcm_action_drain_init = {
1402 .pre_action = snd_pcm_pre_drain_init,
1403 .do_action = snd_pcm_do_drain_init,
1404 .post_action = snd_pcm_post_drain_init
1405};
1406
1407struct drain_rec {
1408 struct snd_pcm_substream *substream;
1409 wait_queue_t wait;
1410 snd_pcm_uframes_t stop_threshold;
1411};
1412
1413static int snd_pcm_drop(struct snd_pcm_substream *substream);
1414
1415/*
1416 * Drain the stream(s).
1417 * When the substream is linked, sync until the draining of all playback streams
1418 * is finished.
1419 * After this call, all streams are supposed to be either SETUP or DRAINING
1420 * (capture only) state.
1421 */
1422static int snd_pcm_drain(struct snd_pcm_substream *substream)
1423{
1424 struct snd_card *card;
1425 struct snd_pcm_runtime *runtime;
1426 struct snd_pcm_substream *s;
1427 int result = 0;
1428 int i, num_drecs;
1429 struct drain_rec *drec, drec_tmp, *d;
1430
1431 snd_assert(substream != NULL, return -ENXIO);
1432 card = substream->pcm->card;
1433 runtime = substream->runtime;
1434
1435 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1436 return -EBADFD;
1437
1438 snd_power_lock(card);
1439 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1440 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1441 if (result < 0) {
1442 snd_power_unlock(card);
1443 return result;
1444 }
1445 }
1446
1447 /* allocate temporary record for drain sync */
1448 down_read(&snd_pcm_link_rwsem);
1449 if (snd_pcm_stream_linked(substream)) {
1450 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1451 if (! drec) {
1452 up_read(&snd_pcm_link_rwsem);
1453 snd_power_unlock(card);
1454 return -ENOMEM;
1455 }
1456 } else
1457 drec = &drec_tmp;
1458
1459 /* count only playback streams */
1460 num_drecs = 0;
1461 snd_pcm_group_for_each_entry(s, substream) {
1462 runtime = s->runtime;
1463 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1464 d = &drec[num_drecs++];
1465 d->substream = s;
1466 init_waitqueue_entry(&d->wait, current);
1467 add_wait_queue(&runtime->sleep, &d->wait);
1468 /* stop_threshold fixup to avoid endless loop when
1469 * stop_threshold > buffer_size
1470 */
1471 d->stop_threshold = runtime->stop_threshold;
1472 if (runtime->stop_threshold > runtime->buffer_size)
1473 runtime->stop_threshold = runtime->buffer_size;
1474 }
1475 }
1476 up_read(&snd_pcm_link_rwsem);
1477
1478 snd_pcm_stream_lock_irq(substream);
1479 /* resume pause */
1480 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1481 snd_pcm_pause(substream, 0);
1482
1483 /* pre-start/stop - all running streams are changed to DRAINING state */
1484 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1485 if (result < 0) {
1486 snd_pcm_stream_unlock_irq(substream);
1487 goto _error;
1488 }
1489
1490 for (;;) {
1491 long tout;
1492 if (signal_pending(current)) {
1493 result = -ERESTARTSYS;
1494 break;
1495 }
1496 /* all finished? */
1497 for (i = 0; i < num_drecs; i++) {
1498 runtime = drec[i].substream->runtime;
1499 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1500 break;
1501 }
1502 if (i == num_drecs)
1503 break; /* yes, all drained */
1504
1505 set_current_state(TASK_INTERRUPTIBLE);
1506 snd_pcm_stream_unlock_irq(substream);
1507 snd_power_unlock(card);
1508 tout = schedule_timeout(10 * HZ);
1509 snd_power_lock(card);
1510 snd_pcm_stream_lock_irq(substream);
1511 if (tout == 0) {
1512 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1513 result = -ESTRPIPE;
1514 else {
1515 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1516 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1517 result = -EIO;
1518 }
1519 break;
1520 }
1521 }
1522
1523 snd_pcm_stream_unlock_irq(substream);
1524
1525 _error:
1526 for (i = 0; i < num_drecs; i++) {
1527 d = &drec[i];
1528 runtime = d->substream->runtime;
1529 remove_wait_queue(&runtime->sleep, &d->wait);
1530 runtime->stop_threshold = d->stop_threshold;
1531 }
1532
1533 if (drec != &drec_tmp)
1534 kfree(drec);
1535 snd_power_unlock(card);
1536
1537 return result;
1538}
1539
1540/*
1541 * drop ioctl
1542 *
1543 * Immediately put all linked substreams into SETUP state.
1544 */
1545static int snd_pcm_drop(struct snd_pcm_substream *substream)
1546{
1547 struct snd_pcm_runtime *runtime;
1548 struct snd_card *card;
1549 int result = 0;
1550
1551 snd_assert(substream != NULL, return -ENXIO);
1552 runtime = substream->runtime;
1553 card = substream->pcm->card;
1554
1555 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1556 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
1557 return -EBADFD;
1558
1559 snd_power_lock(card);
1560 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1561 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
1562 if (result < 0)
1563 goto _unlock;
1564 }
1565
1566 snd_pcm_stream_lock_irq(substream);
1567 /* resume pause */
1568 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1569 snd_pcm_pause(substream, 0);
1570
1571 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1572 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1573 snd_pcm_stream_unlock_irq(substream);
1574 _unlock:
1575 snd_power_unlock(card);
1576 return result;
1577}
1578
1579
1580#ifndef TARGET_OS2
1581/* WARNING: Don't forget to fput back the file */
1582static struct file *snd_pcm_file_fd(int fd)
1583{
1584 struct file *file;
1585 struct inode *inode;
1586 unsigned int minor;
1587
1588 file = fget(fd);
1589 if (!file)
1590 return NULL;
1591 inode = file->f_path.dentry->d_inode;
1592 if (!S_ISCHR(inode->i_mode) ||
1593 imajor(inode) != snd_major) {
1594 fput(file);
1595 return NULL;
1596 }
1597 minor = iminor(inode);
1598 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1599 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
1600 fput(file);
1601 return NULL;
1602 }
1603 return file;
1604}
1605#endif
1606
1607/*
1608 * PCM link handling
1609 */
1610static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1611{
1612 int res = 0;
1613 struct file *file;
1614 struct snd_pcm_file *pcm_file;
1615 struct snd_pcm_substream *substream1;
1616
1617#ifdef TARGET_OS2
1618 file = (struct file *)substream->file;
1619#else
1620 file = snd_pcm_file_fd(fd);
1621#endif
1622 if (!file)
1623 return -EBADFD;
1624 pcm_file = file->private_data;
1625 substream1 = pcm_file->substream;
1626 down_write(&snd_pcm_link_rwsem);
1627 write_lock_irq(&snd_pcm_link_rwlock);
1628 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1629 substream->runtime->status->state != substream1->runtime->status->state) {
1630 res = -EBADFD;
1631 goto _end;
1632 }
1633 if (snd_pcm_stream_linked(substream1)) {
1634 res = -EALREADY;
1635 goto _end;
1636 }
1637 if (!snd_pcm_stream_linked(substream)) {
1638 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1639 if (substream->group == NULL) {
1640 res = -ENOMEM;
1641 goto _end;
1642 }
1643 spin_lock_init(&substream->group->lock);
1644 INIT_LIST_HEAD(&substream->group->substreams);
1645 list_add_tail(&substream->link_list, &substream->group->substreams);
1646 substream->group->count = 1;
1647 }
1648 list_add_tail(&substream1->link_list, &substream->group->substreams);
1649 substream->group->count++;
1650 substream1->group = substream->group;
1651 _end:
1652 write_unlock_irq(&snd_pcm_link_rwlock);
1653 up_write(&snd_pcm_link_rwsem);
1654#ifndef TARGET_OS2
1655 fput(file);
1656#endif
1657 return res;
1658}
1659
1660static void relink_to_local(struct snd_pcm_substream *substream)
1661{
1662 substream->group = &substream->self_group;
1663 INIT_LIST_HEAD(&substream->self_group.substreams);
1664 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1665}
1666
1667static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1668{
1669 struct snd_pcm_substream *s;
1670 int res = 0;
1671
1672 down_write(&snd_pcm_link_rwsem);
1673 write_lock_irq(&snd_pcm_link_rwlock);
1674 if (!snd_pcm_stream_linked(substream)) {
1675 res = -EALREADY;
1676 goto _end;
1677 }
1678 list_del(&substream->link_list);
1679 substream->group->count--;
1680 if (substream->group->count == 1) { /* detach the last stream, too */
1681 snd_pcm_group_for_each_entry(s, substream) {
1682 relink_to_local(s);
1683 break;
1684 }
1685 kfree(substream->group);
1686 }
1687 relink_to_local(substream);
1688 _end:
1689 write_unlock_irq(&snd_pcm_link_rwlock);
1690 up_write(&snd_pcm_link_rwsem);
1691 return res;
1692}
1693
1694/*
1695 * hw configurator
1696 */
1697static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
1699{
1700 struct snd_interval t;
1701 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1702 hw_param_interval_c(params, rule->deps[1]), &t);
1703 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1704}
1705
1706static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1707 struct snd_pcm_hw_rule *rule)
1708{
1709 struct snd_interval t;
1710 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1711 hw_param_interval_c(params, rule->deps[1]), &t);
1712 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1713}
1714
1715static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1716 struct snd_pcm_hw_rule *rule)
1717{
1718 struct snd_interval t;
1719 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1720 hw_param_interval_c(params, rule->deps[1]),
1721 (unsigned long) rule->private, &t);
1722 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1723}
1724
1725static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1726 struct snd_pcm_hw_rule *rule)
1727{
1728 struct snd_interval t;
1729 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1730 (unsigned long) rule->private,
1731 hw_param_interval_c(params, rule->deps[1]), &t);
1732 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1733}
1734
1735static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1736 struct snd_pcm_hw_rule *rule)
1737{
1738 unsigned int k;
1739 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1740 struct snd_mask m;
1741 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1742 snd_mask_any(&m);
1743 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1744 int bits;
1745 if (! snd_mask_test(mask, k))
1746 continue;
1747 bits = snd_pcm_format_physical_width(k);
1748 if (bits <= 0)
1749 continue; /* ignore invalid formats */
1750 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1751 snd_mask_reset(&m, k);
1752 }
1753 return snd_mask_refine(mask, &m);
1754}
1755
1756static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1757 struct snd_pcm_hw_rule *rule)
1758{
1759 struct snd_interval t;
1760 unsigned int k;
1761 t.min = UINT_MAX;
1762 t.max = 0;
1763 t.openmin = 0;
1764 t.openmax = 0;
1765 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1766 int bits;
1767 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1768 continue;
1769 bits = snd_pcm_format_physical_width(k);
1770 if (bits <= 0)
1771 continue; /* ignore invalid formats */
1772 if (t.min > (unsigned)bits)
1773 t.min = bits;
1774 if (t.max < (unsigned)bits)
1775 t.max = bits;
1776 }
1777 t.integer = 1;
1778 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1779}
1780
1781#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1782#error "Change this table"
1783#endif
1784
1785static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1786 48000, 64000, 88200, 96000, 176400, 192000 };
1787
1788const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1789 .count = ARRAY_SIZE(rates),
1790 .list = rates,
1791};
1792
1793static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1794 struct snd_pcm_hw_rule *rule)
1795{
1796 struct snd_pcm_hardware *hw = rule->private;
1797 return snd_interval_list(hw_param_interval(params, rule->var),
1798 snd_pcm_known_rates.count,
1799 snd_pcm_known_rates.list, hw->rates);
1800}
1801
1802static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1803 struct snd_pcm_hw_rule *rule)
1804{
1805 struct snd_interval t;
1806 struct snd_pcm_substream *substream = rule->private;
1807 t.min = 0;
1808 t.max = substream->buffer_bytes_max;
1809 t.openmin = 0;
1810 t.openmax = 0;
1811 t.integer = 1;
1812 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1813}
1814
1815int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1816{
1817 struct snd_pcm_runtime *runtime = substream->runtime;
1818 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1819 int k, err;
1820
1821 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1822 snd_mask_any(constrs_mask(constrs, k));
1823 }
1824
1825 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1826 snd_interval_any(constrs_interval(constrs, k));
1827 }
1828
1829 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1831 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1832 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1833 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1834
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1836 snd_pcm_hw_rule_format, NULL,
1837 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1838 if (err < 0)
1839 return err;
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1841 snd_pcm_hw_rule_sample_bits, NULL,
1842 SNDRV_PCM_HW_PARAM_FORMAT,
1843 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1852 snd_pcm_hw_rule_mul, NULL,
1853 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1858 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1867 snd_pcm_hw_rule_div, NULL,
1868 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1872 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1878 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1882 snd_pcm_hw_rule_div, NULL,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1887 snd_pcm_hw_rule_div, NULL,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1897 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1902 snd_pcm_hw_rule_mul, NULL,
1903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1904 if (err < 0)
1905 return err;
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1907 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1908 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1909 if (err < 0)
1910 return err;
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1912 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1913 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1914 if (err < 0)
1915 return err;
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1917 snd_pcm_hw_rule_muldivk, (void*) 8,
1918 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1919 if (err < 0)
1920 return err;
1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1922 snd_pcm_hw_rule_muldivk, (void*) 8,
1923 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1924 if (err < 0)
1925 return err;
1926 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1927 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1928 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1929 if (err < 0)
1930 return err;
1931 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1932 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1933 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1934 if (err < 0)
1935 return err;
1936 return 0;
1937}
1938
1939int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1940{
1941 struct snd_pcm_runtime *runtime = substream->runtime;
1942 struct snd_pcm_hardware *hw = &runtime->hw;
1943 int err;
1944 unsigned int mask = 0;
1945
1946 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1947 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1948 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1949 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1950 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1951 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1952 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1953 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1954 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1955 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1956 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1957 }
1958 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1959 snd_assert(err >= 0, return -EINVAL);
1960
1961 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1962 snd_assert(err >= 0, return -EINVAL);
1963
1964 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1965 snd_assert(err >= 0, return -EINVAL);
1966
1967#ifdef TARGET_OS2
1968 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_RATE_MASK, hw->rates);
1969 snd_assert(err >= 0, return -EINVAL);
1970#endif
1971
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1973 hw->channels_min, hw->channels_max);
1974 snd_assert(err >= 0, return -EINVAL);
1975
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1977 hw->rate_min, hw->rate_max);
1978 snd_assert(err >= 0, return -EINVAL);
1979
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1981 hw->period_bytes_min, hw->period_bytes_max);
1982 snd_assert(err >= 0, return -EINVAL);
1983
1984 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1985 hw->periods_min, hw->periods_max);
1986 snd_assert(err >= 0, return -EINVAL);
1987
1988 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1989 hw->period_bytes_min, hw->buffer_bytes_max);
1990 snd_assert(err >= 0, return -EINVAL);
1991
1992 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1993 snd_pcm_hw_rule_buffer_bytes_max, substream,
1994 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1995 if (err < 0)
1996 return err;
1997
1998 /* FIXME: remove */
1999 if (runtime->dma_bytes) {
2000 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
2001 snd_assert(err >= 0, return -EINVAL);
2002 }
2003
2004 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2005 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2006 snd_pcm_hw_rule_rate, hw,
2007 SNDRV_PCM_HW_PARAM_RATE, -1);
2008 if (err < 0)
2009 return err;
2010 }
2011
2012 /* FIXME: this belong to lowlevel */
2013 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2014
2015 return 0;
2016}
2017
2018static void pcm_release_private(struct snd_pcm_substream *substream)
2019{
2020 snd_pcm_unlink(substream);
2021}
2022
2023void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2024{
2025 substream->ref_count--;
2026 if (substream->ref_count > 0)
2027 return;
2028
2029 snd_pcm_drop(substream);
2030 if (substream->hw_opened) {
2031 if (substream->ops->hw_free != NULL)
2032 substream->ops->hw_free(substream);
2033 substream->ops->close(substream);
2034 substream->hw_opened = 0;
2035 }
2036 if (substream->pcm_release) {
2037 substream->pcm_release(substream);
2038 substream->pcm_release = NULL;
2039 }
2040 snd_pcm_detach_substream(substream);
2041}
2042
2043EXPORT_SYMBOL(snd_pcm_release_substream);
2044
2045int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2046 struct file *file,
2047 struct snd_pcm_substream **rsubstream)
2048{
2049 struct snd_pcm_substream *substream;
2050 int err;
2051
2052 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2053 if (err < 0)
2054 return err;
2055 if (substream->ref_count > 1) {
2056 *rsubstream = substream;
2057 return 0;
2058 }
2059
2060 err = snd_pcm_hw_constraints_init(substream);
2061 if (err < 0) {
2062 snd_printd("snd_pcm_hw_constraints_init failed\n");
2063 goto error;
2064 }
2065
2066 if ((err = substream->ops->open(substream)) < 0)
2067 goto error;
2068
2069 substream->hw_opened = 1;
2070
2071 err = snd_pcm_hw_constraints_complete(substream);
2072 if (err < 0) {
2073 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2074 goto error;
2075 }
2076
2077 *rsubstream = substream;
2078 return 0;
2079
2080 error:
2081 snd_pcm_release_substream(substream);
2082 return err;
2083}
2084
2085EXPORT_SYMBOL(snd_pcm_open_substream);
2086
2087static int snd_pcm_open_file(struct file *file,
2088 struct snd_pcm *pcm,
2089 int stream,
2090 struct snd_pcm_file **rpcm_file)
2091{
2092 struct snd_pcm_file *pcm_file;
2093 struct snd_pcm_substream *substream;
2094 struct snd_pcm_str *str;
2095 int err;
2096
2097 snd_assert(rpcm_file != NULL, return -EINVAL);
2098 *rpcm_file = NULL;
2099
2100 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2101 if (err < 0)
2102 return err;
2103
2104 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2105 if (pcm_file == NULL) {
2106 snd_pcm_release_substream(substream);
2107 return -ENOMEM;
2108 }
2109 pcm_file->substream = substream;
2110 if (substream->ref_count == 1) {
2111 str = substream->pstr;
2112 substream->file = pcm_file;
2113 substream->pcm_release = pcm_release_private;
2114 }
2115 file->private_data = pcm_file;
2116 *rpcm_file = pcm_file;
2117 return 0;
2118}
2119
2120static int snd_pcm_playback_open(struct inode *inode, struct file *file)
2121{
2122 struct snd_pcm *pcm;
2123
2124#ifndef TARGET_OS2
2125 pcm = snd_lookup_minor_data(iminor(inode),
2126#else
2127 pcm = snd_lookup_minor_data(MINOR(inode->i_rdev),
2128#endif
2129 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2130 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2131}
2132
2133static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2134{
2135 struct snd_pcm *pcm;
2136
2137#ifndef TARGET_OS2
2138 pcm = snd_lookup_minor_data(iminor(inode),
2139#else
2140 pcm = snd_lookup_minor_data(MINOR(inode->i_rdev),
2141#endif
2142 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2143 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2144}
2145
2146static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2147{
2148 int err;
2149 struct snd_pcm_file *pcm_file;
2150 wait_queue_t wait;
2151
2152 if (pcm == NULL) {
2153 err = -ENODEV;
2154 goto __error1;
2155 }
2156 err = snd_card_file_add(pcm->card, file);
2157 if (err < 0)
2158 goto __error1;
2159 if (!try_module_get(pcm->card->module)) {
2160 err = -EFAULT;
2161 goto __error2;
2162 }
2163 init_waitqueue_entry(&wait, current);
2164 add_wait_queue(&pcm->open_wait, &wait);
2165 mutex_lock(&pcm->open_mutex);
2166 while (1) {
2167 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
2168 if (err >= 0)
2169 break;
2170 if (err == -EAGAIN) {
2171 if (file->f_flags & O_NONBLOCK) {
2172 err = -EBUSY;
2173 break;
2174 }
2175 } else
2176 break;
2177 set_current_state(TASK_INTERRUPTIBLE);
2178 mutex_unlock(&pcm->open_mutex);
2179 schedule();
2180 mutex_lock(&pcm->open_mutex);
2181 if (signal_pending(current)) {
2182 err = -ERESTARTSYS;
2183 break;
2184 }
2185 }
2186 remove_wait_queue(&pcm->open_wait, &wait);
2187 mutex_unlock(&pcm->open_mutex);
2188 if (err < 0)
2189 goto __error;
2190 return err;
2191
2192 __error:
2193 module_put(pcm->card->module);
2194 __error2:
2195 snd_card_file_remove(pcm->card, file);
2196 __error1:
2197 return err;
2198}
2199
2200static int snd_pcm_release(struct inode *inode, struct file *file)
2201{
2202 struct snd_pcm *pcm;
2203 struct snd_pcm_substream *substream;
2204 struct snd_pcm_file *pcm_file;
2205
2206 pcm_file = file->private_data;
2207 substream = pcm_file->substream;
2208 snd_assert(substream != NULL, return -ENXIO);
2209 pcm = substream->pcm;
2210 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2211 mutex_lock(&pcm->open_mutex);
2212 snd_pcm_release_substream(substream);
2213 kfree(pcm_file);
2214 mutex_unlock(&pcm->open_mutex);
2215 wake_up(&pcm->open_wait);
2216 module_put(pcm->card->module);
2217 snd_card_file_remove(pcm->card, file);
2218 return 0;
2219}
2220
2221static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2222 snd_pcm_uframes_t frames)
2223{
2224 struct snd_pcm_runtime *runtime = substream->runtime;
2225 snd_pcm_sframes_t appl_ptr;
2226 snd_pcm_sframes_t ret;
2227 snd_pcm_sframes_t hw_avail;
2228
2229 if (frames == 0)
2230 return 0;
2231
2232 snd_pcm_stream_lock_irq(substream);
2233 switch (runtime->status->state) {
2234 case SNDRV_PCM_STATE_PREPARED:
2235 break;
2236 case SNDRV_PCM_STATE_DRAINING:
2237 case SNDRV_PCM_STATE_RUNNING:
2238 if (snd_pcm_update_hw_ptr(substream) >= 0)
2239 break;
2240 /* Fall through */
2241 case SNDRV_PCM_STATE_XRUN:
2242 ret = -EPIPE;
2243 goto __end;
2244 default:
2245 ret = -EBADFD;
2246 goto __end;
2247 }
2248
2249 hw_avail = snd_pcm_playback_hw_avail(runtime);
2250 if (hw_avail <= 0) {
2251 ret = 0;
2252 goto __end;
2253 }
2254 if (frames > (snd_pcm_uframes_t)hw_avail)
2255 frames = hw_avail;
2256 appl_ptr = runtime->control->appl_ptr - frames;
2257 if (appl_ptr < 0)
2258 appl_ptr += runtime->boundary;
2259 runtime->control->appl_ptr = appl_ptr;
2260 ret = frames;
2261 __end:
2262 snd_pcm_stream_unlock_irq(substream);
2263 return ret;
2264}
2265
2266static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2267 snd_pcm_uframes_t frames)
2268{
2269 struct snd_pcm_runtime *runtime = substream->runtime;
2270 snd_pcm_sframes_t appl_ptr;
2271 snd_pcm_sframes_t ret;
2272 snd_pcm_sframes_t hw_avail;
2273
2274 if (frames == 0)
2275 return 0;
2276
2277 snd_pcm_stream_lock_irq(substream);
2278 switch (runtime->status->state) {
2279 case SNDRV_PCM_STATE_PREPARED:
2280 case SNDRV_PCM_STATE_DRAINING:
2281 break;
2282 case SNDRV_PCM_STATE_RUNNING:
2283 if (snd_pcm_update_hw_ptr(substream) >= 0)
2284 break;
2285 /* Fall through */
2286 case SNDRV_PCM_STATE_XRUN:
2287 ret = -EPIPE;
2288 goto __end;
2289 default:
2290 ret = -EBADFD;
2291 goto __end;
2292 }
2293
2294 hw_avail = snd_pcm_capture_hw_avail(runtime);
2295 if (hw_avail <= 0) {
2296 ret = 0;
2297 goto __end;
2298 }
2299 if (frames > (snd_pcm_uframes_t)hw_avail)
2300 frames = hw_avail;
2301 appl_ptr = runtime->control->appl_ptr - frames;
2302 if (appl_ptr < 0)
2303 appl_ptr += runtime->boundary;
2304 runtime->control->appl_ptr = appl_ptr;
2305 ret = frames;
2306 __end:
2307 snd_pcm_stream_unlock_irq(substream);
2308 return ret;
2309}
2310
2311static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2312 snd_pcm_uframes_t frames)
2313{
2314 struct snd_pcm_runtime *runtime = substream->runtime;
2315 snd_pcm_sframes_t appl_ptr;
2316 snd_pcm_sframes_t ret;
2317 snd_pcm_sframes_t avail;
2318
2319 if (frames == 0)
2320 return 0;
2321
2322 snd_pcm_stream_lock_irq(substream);
2323 switch (runtime->status->state) {
2324 case SNDRV_PCM_STATE_PREPARED:
2325 case SNDRV_PCM_STATE_PAUSED:
2326 break;
2327 case SNDRV_PCM_STATE_DRAINING:
2328 case SNDRV_PCM_STATE_RUNNING:
2329 if (snd_pcm_update_hw_ptr(substream) >= 0)
2330 break;
2331 /* Fall through */
2332 case SNDRV_PCM_STATE_XRUN:
2333 ret = -EPIPE;
2334 goto __end;
2335 default:
2336 ret = -EBADFD;
2337 goto __end;
2338 }
2339
2340 avail = snd_pcm_playback_avail(runtime);
2341 if (avail <= 0) {
2342 ret = 0;
2343 goto __end;
2344 }
2345 if (frames > (snd_pcm_uframes_t)avail)
2346 frames = avail;
2347 appl_ptr = runtime->control->appl_ptr + frames;
2348 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2349 appl_ptr -= runtime->boundary;
2350 runtime->control->appl_ptr = appl_ptr;
2351 ret = frames;
2352 __end:
2353 snd_pcm_stream_unlock_irq(substream);
2354 return ret;
2355}
2356
2357static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2358 snd_pcm_uframes_t frames)
2359{
2360 struct snd_pcm_runtime *runtime = substream->runtime;
2361 snd_pcm_sframes_t appl_ptr;
2362 snd_pcm_sframes_t ret;
2363 snd_pcm_sframes_t avail;
2364
2365 if (frames == 0)
2366 return 0;
2367
2368 snd_pcm_stream_lock_irq(substream);
2369 switch (runtime->status->state) {
2370 case SNDRV_PCM_STATE_PREPARED:
2371 case SNDRV_PCM_STATE_DRAINING:
2372 case SNDRV_PCM_STATE_PAUSED:
2373 break;
2374 case SNDRV_PCM_STATE_RUNNING:
2375 if (snd_pcm_update_hw_ptr(substream) >= 0)
2376 break;
2377 /* Fall through */
2378 case SNDRV_PCM_STATE_XRUN:
2379 ret = -EPIPE;
2380 goto __end;
2381 default:
2382 ret = -EBADFD;
2383 goto __end;
2384 }
2385
2386 avail = snd_pcm_capture_avail(runtime);
2387 if (avail <= 0) {
2388 ret = 0;
2389 goto __end;
2390 }
2391 if (frames > (snd_pcm_uframes_t)avail)
2392 frames = avail;
2393 appl_ptr = runtime->control->appl_ptr + frames;
2394 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2395 appl_ptr -= runtime->boundary;
2396 runtime->control->appl_ptr = appl_ptr;
2397 ret = frames;
2398 __end:
2399 snd_pcm_stream_unlock_irq(substream);
2400 return ret;
2401}
2402
2403static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
2404{
2405 struct snd_pcm_runtime *runtime = substream->runtime;
2406 int err;
2407
2408 snd_pcm_stream_lock_irq(substream);
2409 switch (runtime->status->state) {
2410 case SNDRV_PCM_STATE_DRAINING:
2411 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2412 goto __badfd;
2413 case SNDRV_PCM_STATE_RUNNING:
2414 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2415 break;
2416 /* Fall through */
2417 case SNDRV_PCM_STATE_PREPARED:
2418 case SNDRV_PCM_STATE_SUSPENDED:
2419 err = 0;
2420 break;
2421 case SNDRV_PCM_STATE_XRUN:
2422 err = -EPIPE;
2423 break;
2424 default:
2425 __badfd:
2426 err = -EBADFD;
2427 break;
2428 }
2429 snd_pcm_stream_unlock_irq(substream);
2430 return err;
2431}
2432
2433static int snd_pcm_delay(struct snd_pcm_substream *substream,
2434 snd_pcm_sframes_t __user *res)
2435{
2436 struct snd_pcm_runtime *runtime = substream->runtime;
2437 int err;
2438 snd_pcm_sframes_t n = 0;
2439
2440 snd_pcm_stream_lock_irq(substream);
2441 switch (runtime->status->state) {
2442 case SNDRV_PCM_STATE_DRAINING:
2443 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2444 goto __badfd;
2445 case SNDRV_PCM_STATE_RUNNING:
2446 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2447 break;
2448 /* Fall through */
2449 case SNDRV_PCM_STATE_PREPARED:
2450 case SNDRV_PCM_STATE_SUSPENDED:
2451 err = 0;
2452 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2453 n = snd_pcm_playback_hw_avail(runtime);
2454 else
2455 n = snd_pcm_capture_avail(runtime);
2456 break;
2457 case SNDRV_PCM_STATE_XRUN:
2458 err = -EPIPE;
2459 break;
2460 default:
2461 __badfd:
2462 err = -EBADFD;
2463 break;
2464 }
2465 snd_pcm_stream_unlock_irq(substream);
2466 if (!err)
2467 if (put_user(n, res))
2468 err = -EFAULT;
2469 return err;
2470}
2471
2472static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2473 struct snd_pcm_sync_ptr __user *_sync_ptr)
2474{
2475 struct snd_pcm_runtime *runtime = substream->runtime;
2476 struct snd_pcm_sync_ptr sync_ptr;
2477 volatile struct snd_pcm_mmap_status *status;
2478 volatile struct snd_pcm_mmap_control *control;
2479 int err;
2480
2481 memset(&sync_ptr, 0, sizeof(sync_ptr));
2482 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2483 return -EFAULT;
2484 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
2485 return -EFAULT;
2486 status = runtime->status;
2487 control = runtime->control;
2488 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2489 err = snd_pcm_hwsync(substream);
2490 if (err < 0)
2491 return err;
2492 }
2493 snd_pcm_stream_lock_irq(substream);
2494 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2495 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2496 else
2497 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2498 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2499 control->avail_min = sync_ptr.c.control.avail_min;
2500 else
2501 sync_ptr.c.control.avail_min = control->avail_min;
2502 sync_ptr.s.status.state = status->state;
2503 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2504 sync_ptr.s.status.tstamp = status->tstamp;
2505 sync_ptr.s.status.suspended_state = status->suspended_state;
2506 snd_pcm_stream_unlock_irq(substream);
2507 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2508 return -EFAULT;
2509 return 0;
2510}
2511
2512static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2513{
2514 struct snd_pcm_runtime *runtime = substream->runtime;
2515 int arg;
2516
2517 if (get_user(arg, _arg))
2518 return -EFAULT;
2519 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2520 return -EINVAL;
2521 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2522 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2523 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2524 return 0;
2525}
2526
2527static int snd_pcm_common_ioctl1(struct file *file,
2528 struct snd_pcm_substream *substream,
2529 unsigned int cmd, void __user *arg)
2530{
2531 snd_assert(substream != NULL, return -ENXIO);
2532
2533 switch (cmd) {
2534 case SNDRV_PCM_IOCTL_PVERSION:
2535 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2536 case SNDRV_PCM_IOCTL_INFO:
2537 return snd_pcm_info_user(substream, arg);
2538 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2539 return 0;
2540 case SNDRV_PCM_IOCTL_TTSTAMP:
2541 return snd_pcm_tstamp(substream, arg);
2542 case SNDRV_PCM_IOCTL_HW_REFINE:
2543 return snd_pcm_hw_refine_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_HW_PARAMS:
2545 return snd_pcm_hw_params_user(substream, arg);
2546 case SNDRV_PCM_IOCTL_HW_FREE:
2547 return snd_pcm_hw_free(substream);
2548 case SNDRV_PCM_IOCTL_SW_PARAMS:
2549 return snd_pcm_sw_params_user(substream, arg);
2550 case SNDRV_PCM_IOCTL_STATUS:
2551 return snd_pcm_status_user(substream, arg);
2552 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2553 return snd_pcm_channel_info_user(substream, arg);
2554 case SNDRV_PCM_IOCTL_PREPARE:
2555 return snd_pcm_prepare(substream, file);
2556 case SNDRV_PCM_IOCTL_RESET:
2557 return snd_pcm_reset(substream);
2558 case SNDRV_PCM_IOCTL_START:
2559 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2560 case SNDRV_PCM_IOCTL_LINK:
2561 return snd_pcm_link(substream, (int)(unsigned long) arg);
2562 case SNDRV_PCM_IOCTL_UNLINK:
2563 return snd_pcm_unlink(substream);
2564 case SNDRV_PCM_IOCTL_RESUME:
2565 return snd_pcm_resume(substream);
2566 case SNDRV_PCM_IOCTL_XRUN:
2567 return snd_pcm_xrun(substream);
2568 case SNDRV_PCM_IOCTL_HWSYNC:
2569 return snd_pcm_hwsync(substream);
2570 case SNDRV_PCM_IOCTL_DELAY:
2571 return snd_pcm_delay(substream, arg);
2572 case SNDRV_PCM_IOCTL_SYNC_PTR:
2573 return snd_pcm_sync_ptr(substream, arg);
2574#ifdef CONFIG_SND_SUPPORT_OLD_API
2575 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2576 return snd_pcm_hw_refine_old_user(substream, arg);
2577 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2578 return snd_pcm_hw_params_old_user(substream, arg);
2579#endif
2580 case SNDRV_PCM_IOCTL_DRAIN:
2581 return snd_pcm_drain(substream);
2582 case SNDRV_PCM_IOCTL_DROP:
2583 return snd_pcm_drop(substream);
2584 case SNDRV_PCM_IOCTL_PAUSE:
2585 {
2586 int res;
2587 snd_pcm_stream_lock_irq(substream);
2588 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2589 snd_pcm_stream_unlock_irq(substream);
2590 return res;
2591 }
2592 }
2593 snd_printd("unknown ioctl = 0x%x\n", cmd);
2594 return -ENOTTY;
2595}
2596
2597static int snd_pcm_playback_ioctl1(struct file *file,
2598 struct snd_pcm_substream *substream,
2599 unsigned int cmd, void __user *arg)
2600{
2601 snd_assert(substream != NULL, return -ENXIO);
2602 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2603 switch (cmd) {
2604 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2605 {
2606 struct snd_xferi xferi;
2607 struct snd_xferi __user *_xferi = arg;
2608 struct snd_pcm_runtime *runtime = substream->runtime;
2609 snd_pcm_sframes_t result;
2610 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2611 return -EBADFD;
2612 if (put_user(0, &_xferi->result))
2613 return -EFAULT;
2614 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2615 return -EFAULT;
2616 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2617 __put_user(result, &_xferi->result);
2618 return result < 0 ? result : 0;
2619 }
2620 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2621 {
2622 struct snd_xfern xfern;
2623 struct snd_xfern __user *_xfern = arg;
2624 struct snd_pcm_runtime *runtime = substream->runtime;
2625 void __user **bufs;
2626 snd_pcm_sframes_t result;
2627 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2628 return -EBADFD;
2629 if (runtime->channels > 128)
2630 return -EINVAL;
2631 if (put_user(0, &_xfern->result))
2632 return -EFAULT;
2633 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2634 return -EFAULT;
2635 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2636 if (bufs == NULL)
2637 return -ENOMEM;
2638 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2639 kfree(bufs);
2640 return -EFAULT;
2641 }
2642 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2643 kfree(bufs);
2644 __put_user(result, &_xfern->result);
2645 return result < 0 ? result : 0;
2646 }
2647 case SNDRV_PCM_IOCTL_REWIND:
2648 {
2649 snd_pcm_uframes_t frames;
2650 snd_pcm_uframes_t __user *_frames = arg;
2651 snd_pcm_sframes_t result;
2652 if (get_user(frames, _frames))
2653 return -EFAULT;
2654 if (put_user(0, _frames))
2655 return -EFAULT;
2656 result = snd_pcm_playback_rewind(substream, frames);
2657 __put_user(result, _frames);
2658 return result < 0 ? result : 0;
2659 }
2660 case SNDRV_PCM_IOCTL_FORWARD:
2661 {
2662 snd_pcm_uframes_t frames;
2663 snd_pcm_uframes_t __user *_frames = arg;
2664 snd_pcm_sframes_t result;
2665 if (get_user(frames, _frames))
2666 return -EFAULT;
2667 if (put_user(0, _frames))
2668 return -EFAULT;
2669 result = snd_pcm_playback_forward(substream, frames);
2670 __put_user(result, _frames);
2671 return result < 0 ? result : 0;
2672 }
2673 }
2674 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2675}
2676
2677static int snd_pcm_capture_ioctl1(struct file *file,
2678 struct snd_pcm_substream *substream,
2679 unsigned int cmd, void __user *arg)
2680{
2681 snd_assert(substream != NULL, return -ENXIO);
2682 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2683 switch (cmd) {
2684 case SNDRV_PCM_IOCTL_READI_FRAMES:
2685 {
2686 struct snd_xferi xferi;
2687 struct snd_xferi __user *_xferi = arg;
2688 struct snd_pcm_runtime *runtime = substream->runtime;
2689 snd_pcm_sframes_t result;
2690 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2691 return -EBADFD;
2692 if (put_user(0, &_xferi->result))
2693 return -EFAULT;
2694 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2695 return -EFAULT;
2696 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2697 __put_user(result, &_xferi->result);
2698 return result < 0 ? result : 0;
2699 }
2700 case SNDRV_PCM_IOCTL_READN_FRAMES:
2701 {
2702 struct snd_xfern xfern;
2703 struct snd_xfern __user *_xfern = arg;
2704 struct snd_pcm_runtime *runtime = substream->runtime;
2705 void *bufs;
2706 snd_pcm_sframes_t result;
2707 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2708 return -EBADFD;
2709 if (runtime->channels > 128)
2710 return -EINVAL;
2711 if (put_user(0, &_xfern->result))
2712 return -EFAULT;
2713 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2714 return -EFAULT;
2715 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2716 if (bufs == NULL)
2717 return -ENOMEM;
2718 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2719 kfree(bufs);
2720 return -EFAULT;
2721 }
2722 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2723 kfree(bufs);
2724 __put_user(result, &_xfern->result);
2725 return result < 0 ? result : 0;
2726 }
2727 case SNDRV_PCM_IOCTL_REWIND:
2728 {
2729 snd_pcm_uframes_t frames;
2730 snd_pcm_uframes_t __user *_frames = arg;
2731 snd_pcm_sframes_t result;
2732 if (get_user(frames, _frames))
2733 return -EFAULT;
2734 if (put_user(0, _frames))
2735 return -EFAULT;
2736 result = snd_pcm_capture_rewind(substream, frames);
2737 __put_user(result, _frames);
2738 return result < 0 ? result : 0;
2739 }
2740 case SNDRV_PCM_IOCTL_FORWARD:
2741 {
2742 snd_pcm_uframes_t frames;
2743 snd_pcm_uframes_t __user *_frames = arg;
2744 snd_pcm_sframes_t result;
2745 if (get_user(frames, _frames))
2746 return -EFAULT;
2747 if (put_user(0, _frames))
2748 return -EFAULT;
2749 result = snd_pcm_capture_forward(substream, frames);
2750 __put_user(result, _frames);
2751 return result < 0 ? result : 0;
2752 }
2753 }
2754 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
2755}
2756
2757static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2758 unsigned long arg)
2759{
2760 struct snd_pcm_file *pcm_file;
2761
2762 pcm_file = file->private_data;
2763
2764 if (((cmd >> 8) & 0xff) != 'A')
2765 return -ENOTTY;
2766
2767 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2768 (void __user *)arg);
2769}
2770
2771static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2772 unsigned long arg)
2773{
2774 struct snd_pcm_file *pcm_file;
2775
2776 pcm_file = file->private_data;
2777
2778 if (((cmd >> 8) & 0xff) != 'A')
2779 return -ENOTTY;
2780
2781 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2782 (void __user *)arg);
2783}
2784
2785int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
2786 unsigned int cmd, void *arg)
2787{
2788 mm_segment_t fs;
2789 int result;
2790
2791 fs = snd_enter_user();
2792 switch (substream->stream) {
2793 case SNDRV_PCM_STREAM_PLAYBACK:
2794 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2795 (void __user *)arg);
2796 break;
2797 case SNDRV_PCM_STREAM_CAPTURE:
2798 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2799 (void __user *)arg);
2800 break;
2801 default:
2802 result = -EINVAL;
2803 break;
2804 }
2805 snd_leave_user(fs);
2806 return result;
2807}
2808
2809EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2810
2811static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2812 loff_t * offset)
2813{
2814 struct snd_pcm_file *pcm_file;
2815 struct snd_pcm_substream *substream;
2816 struct snd_pcm_runtime *runtime;
2817 snd_pcm_sframes_t result;
2818
2819 pcm_file = file->private_data;
2820 substream = pcm_file->substream;
2821 snd_assert(substream != NULL, return -ENXIO);
2822 runtime = substream->runtime;
2823 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2824 return -EBADFD;
2825 if (!frame_aligned(runtime, count))
2826 return -EINVAL;
2827 count = bytes_to_frames(runtime, count);
2828 result = snd_pcm_lib_read(substream, buf, count);
2829 if (result > 0)
2830 result = frames_to_bytes(runtime, result);
2831 return result;
2832}
2833
2834static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2835 size_t count, loff_t * offset)
2836{
2837 struct snd_pcm_file *pcm_file;
2838 struct snd_pcm_substream *substream;
2839 struct snd_pcm_runtime *runtime;
2840 snd_pcm_sframes_t result;
2841
2842 pcm_file = file->private_data;
2843 substream = pcm_file->substream;
2844 snd_assert(substream != NULL, result = -ENXIO; goto end);
2845 runtime = substream->runtime;
2846 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2847 result = -EBADFD;
2848 goto end;
2849 }
2850 if (!frame_aligned(runtime, count)) {
2851 result = -EINVAL;
2852 goto end;
2853 }
2854 count = bytes_to_frames(runtime, count);
2855 result = snd_pcm_lib_write(substream, buf, count);
2856 if (result > 0)
2857 result = frames_to_bytes(runtime, result);
2858 end:
2859 return result;
2860}
2861
2862#ifdef SND_PCM_USE_AIO
2863static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2864 unsigned long nr_segs, loff_t pos)
2865#else
2866static ssize_t snd_pcm_readv(struct file *file, const struct iovec *iov,
2867 unsigned long nr_segs, loff_t * offset)
2868#endif
2869{
2870 struct snd_pcm_file *pcm_file;
2871 struct snd_pcm_substream *substream;
2872 struct snd_pcm_runtime *runtime;
2873 snd_pcm_sframes_t result;
2874 unsigned long i;
2875 void __user **bufs;
2876 snd_pcm_uframes_t frames;
2877
2878#ifdef SND_PCM_USE_AIO
2879 pcm_file = iocb->ki_filp->private_data;
2880#else
2881 pcm_file = file->private_data;
2882#endif
2883 substream = pcm_file->substream;
2884 snd_assert(substream != NULL, return -ENXIO);
2885 runtime = substream->runtime;
2886 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2887 return -EBADFD;
2888 if (nr_segs > 1024 || nr_segs != runtime->channels)
2889 return -EINVAL;
2890 if (!frame_aligned(runtime, iov->iov_len))
2891 return -EINVAL;
2892 frames = bytes_to_samples(runtime, iov->iov_len);
2893 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2894 if (bufs == NULL)
2895 return -ENOMEM;
2896 for (i = 0; i < nr_segs; ++i)
2897 bufs[i] = iov[i].iov_base;
2898 result = snd_pcm_lib_readv(substream, bufs, frames);
2899 if (result > 0)
2900 result = frames_to_bytes(runtime, result);
2901 kfree(bufs);
2902 return result;
2903}
2904
2905#ifdef SND_PCM_USE_AIO
2906static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2907 unsigned long nr_segs, loff_t pos)
2908#else
2909static ssize_t snd_pcm_writev(struct file *file, const struct iovec *iov,
2910 unsigned long nr_segs, loff_t * offset)
2911#endif
2912{
2913 struct snd_pcm_file *pcm_file;
2914 struct snd_pcm_substream *substream;
2915 struct snd_pcm_runtime *runtime;
2916 snd_pcm_sframes_t result;
2917 unsigned long i;
2918 void __user **bufs;
2919 snd_pcm_uframes_t frames;
2920
2921#ifdef SND_PCM_USE_AIO
2922 pcm_file = iocb->ki_filp->private_data;
2923#else
2924 pcm_file = file->private_data;
2925#endif
2926 substream = pcm_file->substream;
2927 snd_assert(substream != NULL, result = -ENXIO; goto end);
2928 runtime = substream->runtime;
2929 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2930 result = -EBADFD;
2931 goto end;
2932 }
2933 if (nr_segs > 128 || nr_segs != runtime->channels ||
2934 !frame_aligned(runtime, iov->iov_len)) {
2935 result = -EINVAL;
2936 goto end;
2937 }
2938 frames = bytes_to_samples(runtime, iov->iov_len);
2939 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
2940 if (bufs == NULL)
2941 return -ENOMEM;
2942 for (i = 0; i < nr_segs; ++i)
2943 bufs[i] = iov[i].iov_base;
2944 result = snd_pcm_lib_writev(substream, bufs, frames);
2945 if (result > 0)
2946 result = frames_to_bytes(runtime, result);
2947 kfree(bufs);
2948 end:
2949 return result;
2950}
2951
2952static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2953{
2954 struct snd_pcm_file *pcm_file;
2955 struct snd_pcm_substream *substream;
2956 struct snd_pcm_runtime *runtime;
2957 unsigned int mask;
2958 snd_pcm_uframes_t avail;
2959
2960 pcm_file = file->private_data;
2961
2962 substream = pcm_file->substream;
2963 snd_assert(substream != NULL, return -ENXIO);
2964 runtime = substream->runtime;
2965
2966 poll_wait(file, &runtime->sleep, wait);
2967
2968 snd_pcm_stream_lock_irq(substream);
2969 avail = snd_pcm_playback_avail(runtime);
2970 switch (runtime->status->state) {
2971 case SNDRV_PCM_STATE_RUNNING:
2972 case SNDRV_PCM_STATE_PREPARED:
2973 case SNDRV_PCM_STATE_PAUSED:
2974 if (avail >= runtime->control->avail_min) {
2975 mask = POLLOUT | POLLWRNORM;
2976 break;
2977 }
2978 /* Fall through */
2979 case SNDRV_PCM_STATE_DRAINING:
2980 mask = 0;
2981 break;
2982 default:
2983 mask = POLLOUT | POLLWRNORM | POLLERR;
2984 break;
2985 }
2986 snd_pcm_stream_unlock_irq(substream);
2987 return mask;
2988}
2989
2990static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2991{
2992 struct snd_pcm_file *pcm_file;
2993 struct snd_pcm_substream *substream;
2994 struct snd_pcm_runtime *runtime;
2995 unsigned int mask;
2996 snd_pcm_uframes_t avail;
2997
2998 pcm_file = file->private_data;
2999
3000 substream = pcm_file->substream;
3001 snd_assert(substream != NULL, return -ENXIO);
3002 runtime = substream->runtime;
3003
3004 poll_wait(file, &runtime->sleep, wait);
3005
3006 snd_pcm_stream_lock_irq(substream);
3007 avail = snd_pcm_capture_avail(runtime);
3008 switch (runtime->status->state) {
3009 case SNDRV_PCM_STATE_RUNNING:
3010 case SNDRV_PCM_STATE_PREPARED:
3011 case SNDRV_PCM_STATE_PAUSED:
3012 if (avail >= runtime->control->avail_min) {
3013 mask = POLLIN | POLLRDNORM;
3014 break;
3015 }
3016 mask = 0;
3017 break;
3018 case SNDRV_PCM_STATE_DRAINING:
3019 if (avail > 0) {
3020 mask = POLLIN | POLLRDNORM;
3021 break;
3022 }
3023 /* Fall through */
3024 default:
3025 mask = POLLIN | POLLRDNORM | POLLERR;
3026 break;
3027 }
3028 snd_pcm_stream_unlock_irq(substream);
3029 return mask;
3030}
3031
3032/*
3033 * mmap support
3034 */
3035
3036#ifndef TARGET_OS2
3037
3038/*
3039 * Only on coherent architectures, we can mmap the status and the control records
3040 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3041 */
3042#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3043/*
3044 * mmap status record
3045 */
3046static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3047 struct vm_fault *vmf)
3048{
3049 struct snd_pcm_substream *substream = area->vm_private_data;
3050 struct snd_pcm_runtime *runtime;
3051
3052 if (substream == NULL)
3053 return VM_FAULT_SIGBUS;
3054 runtime = substream->runtime;
3055 vmf->page = virt_to_page(runtime->status);
3056 get_page(vmf->page);
3057 return 0;
3058}
3059
3060static struct vm_operations_struct snd_pcm_vm_ops_status =
3061{
3062 .fault = snd_pcm_mmap_status_fault,
3063};
3064
3065static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3066 struct vm_area_struct *area)
3067{
3068 struct snd_pcm_runtime *runtime;
3069 long size;
3070 if (!(area->vm_flags & VM_READ))
3071 return -EINVAL;
3072 runtime = substream->runtime;
3073 snd_assert(runtime != NULL, return -EAGAIN);
3074 size = area->vm_end - area->vm_start;
3075 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
3076 return -EINVAL;
3077 area->vm_ops = &snd_pcm_vm_ops_status;
3078 area->vm_private_data = substream;
3079 area->vm_flags |= VM_RESERVED;
3080 return 0;
3081}
3082
3083/*
3084 * mmap control record
3085 */
3086static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3087 struct vm_fault *vmf)
3088{
3089 struct snd_pcm_substream *substream = area->vm_private_data;
3090 struct snd_pcm_runtime *runtime;
3091
3092 if (substream == NULL)
3093 return VM_FAULT_SIGBUS;
3094 runtime = substream->runtime;
3095 vmf->page = virt_to_page(runtime->control);
3096 get_page(vmf->page);
3097 return 0;
3098}
3099
3100static struct vm_operations_struct snd_pcm_vm_ops_control =
3101{
3102 .fault = snd_pcm_mmap_control_fault,
3103};
3104
3105static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3106 struct vm_area_struct *area)
3107{
3108 struct snd_pcm_runtime *runtime;
3109 long size;
3110 if (!(area->vm_flags & VM_READ))
3111 return -EINVAL;
3112 runtime = substream->runtime;
3113 snd_assert(runtime != NULL, return -EAGAIN);
3114 size = area->vm_end - area->vm_start;
3115 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
3116 return -EINVAL;
3117 area->vm_ops = &snd_pcm_vm_ops_control;
3118 area->vm_private_data = substream;
3119 area->vm_flags |= VM_RESERVED;
3120 return 0;
3121}
3122#else /* ! coherent mmap */
3123/*
3124 * don't support mmap for status and control records.
3125 */
3126static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
3127 struct vm_area_struct *area)
3128{
3129 return -ENXIO;
3130}
3131static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
3132 struct vm_area_struct *area)
3133{
3134 return -ENXIO;
3135}
3136#endif /* coherent mmap */
3137
3138/*
3139 * fault callback for mmapping a RAM page
3140 */
3141static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3142 struct vm_fault *vmf)
3143{
3144 struct snd_pcm_substream *substream = area->vm_private_data;
3145 struct snd_pcm_runtime *runtime;
3146 unsigned long offset;
3147 struct page * page;
3148 void *vaddr;
3149 size_t dma_bytes;
3150
3151 if (substream == NULL)
3152 return VM_FAULT_SIGBUS;
3153 runtime = substream->runtime;
3154 offset = vmf->pgoff << PAGE_SHIFT;
3155 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3156 if (offset > dma_bytes - PAGE_SIZE)
3157 return VM_FAULT_SIGBUS;
3158 if (substream->ops->page) {
3159 page = substream->ops->page(substream, offset);
3160 if (!page)
3161 return VM_FAULT_SIGBUS;
3162 } else {
3163 vaddr = runtime->dma_area + offset;
3164 page = virt_to_page(vaddr);
3165 }
3166 get_page(page);
3167 vmf->page = page;
3168 return 0;
3169}
3170
3171static struct vm_operations_struct snd_pcm_vm_ops_data =
3172{
3173 .open = snd_pcm_mmap_data_open,
3174 .close = snd_pcm_mmap_data_close,
3175 .fault = snd_pcm_mmap_data_fault,
3176};
3177
3178/*
3179 * mmap the DMA buffer on RAM
3180 */
3181static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3182 struct vm_area_struct *area)
3183{
3184 area->vm_ops = &snd_pcm_vm_ops_data;
3185 area->vm_private_data = substream;
3186 area->vm_flags |= VM_RESERVED;
3187 atomic_inc(&substream->mmap_count);
3188 return 0;
3189}
3190
3191/*
3192 * mmap the DMA buffer on I/O memory area
3193 */
3194#if SNDRV_PCM_INFO_MMAP_IOMEM
3195static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3196{
3197 .open = snd_pcm_mmap_data_open,
3198 .close = snd_pcm_mmap_data_close,
3199};
3200
3201int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3202 struct vm_area_struct *area)
3203{
3204 long size;
3205 unsigned long offset;
3206
3207#ifdef pgprot_noncached
3208 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3209#endif
3210 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3211 area->vm_private_data = substream;
3212 area->vm_flags |= VM_IO;
3213 size = area->vm_end - area->vm_start;
3214 offset = area->vm_pgoff << PAGE_SHIFT;
3215 if (io_remap_pfn_range(area, area->vm_start,
3216 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3217 size, area->vm_page_prot))
3218 return -EAGAIN;
3219 atomic_inc(&substream->mmap_count);
3220 return 0;
3221}
3222
3223EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
3224#endif /* SNDRV_PCM_INFO_MMAP */
3225
3226/*
3227 * mmap DMA buffer
3228 */
3229int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
3230 struct vm_area_struct *area)
3231{
3232 struct snd_pcm_runtime *runtime;
3233 long size;
3234 unsigned long offset;
3235 size_t dma_bytes;
3236
3237 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3238 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3239 return -EINVAL;
3240 } else {
3241 if (!(area->vm_flags & VM_READ))
3242 return -EINVAL;
3243 }
3244 runtime = substream->runtime;
3245 snd_assert(runtime != NULL, return -EAGAIN);
3246 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3247 return -EBADFD;
3248 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3249 return -ENXIO;
3250 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3251 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3252 return -EINVAL;
3253 size = area->vm_end - area->vm_start;
3254 offset = area->vm_pgoff << PAGE_SHIFT;
3255 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3256 if ((size_t)size > dma_bytes)
3257 return -EINVAL;
3258 if (offset > dma_bytes - size)
3259 return -EINVAL;
3260
3261 if (substream->ops->mmap)
3262 return substream->ops->mmap(substream, area);
3263 else
3264 return snd_pcm_default_mmap(substream, area);
3265}
3266
3267EXPORT_SYMBOL(snd_pcm_mmap_data);
3268
3269static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3270{
3271 struct snd_pcm_file * pcm_file;
3272 struct snd_pcm_substream *substream;
3273 unsigned long offset;
3274
3275 pcm_file = file->private_data;
3276 substream = pcm_file->substream;
3277 snd_assert(substream != NULL, return -ENXIO);
3278
3279 offset = area->vm_pgoff << PAGE_SHIFT;
3280 switch (offset) {
3281 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3282 if (pcm_file->no_compat_mmap)
3283 return -ENXIO;
3284 return snd_pcm_mmap_status(substream, file, area);
3285 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3286 if (pcm_file->no_compat_mmap)
3287 return -ENXIO;
3288 return snd_pcm_mmap_control(substream, file, area);
3289 default:
3290 return snd_pcm_mmap_data(substream, file, area);
3291 }
3292 return 0;
3293}
3294#endif //!TARGET_OS2
3295
3296static int snd_pcm_fasync(int fd, struct file * file, int on)
3297{
3298 struct snd_pcm_file * pcm_file;
3299 struct snd_pcm_substream *substream;
3300 struct snd_pcm_runtime *runtime;
3301 int err;
3302
3303 pcm_file = file->private_data;
3304 substream = pcm_file->substream;
3305 snd_assert(substream != NULL, return -ENXIO);
3306 runtime = substream->runtime;
3307
3308 err = fasync_helper(fd, file, on, &runtime->fasync);
3309 if (err < 0)
3310 return err;
3311 return 0;
3312}
3313
3314/*
3315 * ioctl32 compat
3316 */
3317#ifdef CONFIG_COMPAT
3318#include "pcm_compat.c"
3319#else
3320#define snd_pcm_ioctl_compat NULL
3321#endif
3322
3323#ifndef CONFIG_SND_HAVE_NEW_IOCTL
3324/* need to unlock BKL to allow preemption */
3325static int snd_pcm_playback_ioctl_old(struct inode *inode, struct file * file,
3326 unsigned int cmd, unsigned long arg)
3327{
3328 int err;
3329 err = snd_pcm_playback_ioctl(file, cmd, arg);
3330 return err;
3331}
3332static int snd_pcm_capture_ioctl_old(struct inode *inode, struct file * file,
3333 unsigned int cmd, unsigned long arg)
3334{
3335 int err;
3336 err = snd_pcm_capture_ioctl(file, cmd, arg);
3337 return err;
3338}
3339#endif
3340
3341/*
3342 * To be removed helpers to keep binary compatibility
3343 */
3344
3345#ifdef CONFIG_SND_SUPPORT_OLD_API
3346#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3347#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3348
3349static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3350 struct snd_pcm_hw_params_old *oparams)
3351{
3352 unsigned int i;
3353
3354 memset(params, 0, sizeof(*params));
3355 params->flags = oparams->flags;
3356 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3357 params->masks[i].bits[0] = oparams->masks[i];
3358 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3359 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3360 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3361 params->info = oparams->info;
3362 params->msbits = oparams->msbits;
3363 params->rate_num = oparams->rate_num;
3364 params->rate_den = oparams->rate_den;
3365 params->fifo_size = oparams->fifo_size;
3366}
3367
3368static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3369 struct snd_pcm_hw_params *params)
3370{
3371 unsigned int i;
3372
3373 memset(oparams, 0, sizeof(*oparams));
3374 oparams->flags = params->flags;
3375 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3376 oparams->masks[i] = params->masks[i].bits[0];
3377 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3378 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3379 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3380 oparams->info = params->info;
3381 oparams->msbits = params->msbits;
3382 oparams->rate_num = params->rate_num;
3383 oparams->rate_den = params->rate_den;
3384 oparams->fifo_size = params->fifo_size;
3385}
3386
3387static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3388 struct snd_pcm_hw_params_old __user * _oparams)
3389{
3390 struct snd_pcm_hw_params *params;
3391 struct snd_pcm_hw_params_old *oparams = NULL;
3392 int err;
3393
3394 params = kmalloc(sizeof(*params), GFP_KERNEL);
3395 if (!params) {
3396 err = -ENOMEM;
3397 goto out;
3398 }
3399 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3400 if (!oparams) {
3401 err = -ENOMEM;
3402 goto out;
3403 }
3404
3405 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3406 err = -EFAULT;
3407 goto out;
3408 }
3409 snd_pcm_hw_convert_from_old_params(params, oparams);
3410 err = snd_pcm_hw_refine(substream, params);
3411 snd_pcm_hw_convert_to_old_params(oparams, params);
3412 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3413 if (!err)
3414 err = -EFAULT;
3415 }
3416out:
3417 kfree(params);
3418 kfree(oparams);
3419 return err;
3420}
3421
3422static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3423 struct snd_pcm_hw_params_old __user * _oparams)
3424{
3425 struct snd_pcm_hw_params *params;
3426 struct snd_pcm_hw_params_old *oparams = NULL;
3427 int err;
3428
3429 params = kmalloc(sizeof(*params), GFP_KERNEL);
3430 if (!params) {
3431 err = -ENOMEM;
3432 goto out;
3433 }
3434 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3435 if (!oparams) {
3436 err = -ENOMEM;
3437 goto out;
3438 }
3439 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3440 err = -EFAULT;
3441 goto out;
3442 }
3443 snd_pcm_hw_convert_from_old_params(params, oparams);
3444 err = snd_pcm_hw_params(substream, params);
3445 snd_pcm_hw_convert_to_old_params(oparams, params);
3446 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3447 if (!err)
3448 err = -EFAULT;
3449 }
3450out:
3451 kfree(params);
3452 kfree(oparams);
3453 return err;
3454}
3455#endif /* CONFIG_SND_SUPPORT_OLD_API */
3456
3457/*
3458 * Register section
3459 */
3460
3461const struct file_operations snd_pcm_f_ops[2] = {
3462 {
3463#ifndef TARGET_OS2
3464 .owner = THIS_MODULE,
3465#endif
3466 .write = snd_pcm_write,
3467#ifdef SND_PCM_USE_AIO
3468 .aio_write = snd_pcm_aio_write,
3469#elif defined(SND_PCM_USE_READV)
3470 .writev = snd_pcm_writev,
3471#endif
3472 .open = snd_pcm_playback_open,
3473 .release = snd_pcm_release,
3474 .poll = snd_pcm_playback_poll,
3475#ifdef CONFIG_SND_HAVE_NEW_IOCTL
3476 .unlocked_ioctl = snd_pcm_playback_ioctl,
3477 .compat_ioctl = snd_pcm_ioctl_compat,
3478#else
3479 .ioctl = snd_pcm_playback_ioctl_old,
3480#endif
3481#ifndef TARGET_OS2
3482 .mmap = snd_pcm_mmap,
3483#endif
3484 .fasync = snd_pcm_fasync,
3485 },
3486 {
3487#ifndef TARGET_OS2
3488 .owner = THIS_MODULE,
3489#endif
3490 .read = snd_pcm_read,
3491#ifdef SND_PCM_USE_AIO
3492 .aio_read = snd_pcm_aio_read,
3493#elif defined(SND_PCM_USE_READV)
3494 .readv = snd_pcm_readv,
3495#endif
3496 .open = snd_pcm_capture_open,
3497 .release = snd_pcm_release,
3498 .poll = snd_pcm_capture_poll,
3499#ifdef CONFIG_SND_HAVE_NEW_IOCTL
3500 .unlocked_ioctl = snd_pcm_capture_ioctl,
3501 .compat_ioctl = snd_pcm_ioctl_compat,
3502#else
3503 .ioctl = snd_pcm_capture_ioctl_old,
3504#endif
3505#ifndef TARGET_OS2
3506 .mmap = snd_pcm_mmap,
3507#endif
3508 .fasync = snd_pcm_fasync,
3509 }
3510};
3511
Note: See TracBrowser for help on using the repository browser.