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