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