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