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