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