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