blob: 87288762403055a3f6f842c70b9e99794956a1cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -060025#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080027#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010029#include <linux/dma-mapping.h>
Jaroslav Kysela7910b4a2010-01-27 18:10:13 +010030#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <sound/core.h>
32#include <sound/control.h>
33#include <sound/info.h>
34#include <sound/pcm.h>
35#include <sound/pcm_params.h>
36#include <sound/timer.h>
37#include <sound/minors.h>
38#include <asm/io.h>
39
40/*
41 * Compatibility
42 */
43
Takashi Iwai877211f2005-11-17 13:59:38 +010044struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int flags;
46 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
47 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010048 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
50 unsigned int rmask;
51 unsigned int cmask;
52 unsigned int info;
53 unsigned int msbits;
54 unsigned int rate_num;
55 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010056 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char reserved[64];
58};
59
Takashi Iwai59d48582005-12-01 10:51:58 +010060#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010061#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
62#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Takashi Iwai877211f2005-11-17 13:59:38 +010064static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
66static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010068#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010069static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/*
72 *
73 */
74
75DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020076EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020078static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static inline mm_segment_t snd_enter_user(void)
81{
82 mm_segment_t fs = get_fs();
83 set_fs(get_ds());
84 return fs;
85}
86
87static inline void snd_leave_user(mm_segment_t fs)
88{
89 set_fs(fs);
90}
91
92
93
Takashi Iwai877211f2005-11-17 13:59:38 +010094int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Takashi Iwai877211f2005-11-17 13:59:38 +010096 struct snd_pcm_runtime *runtime;
97 struct snd_pcm *pcm = substream->pcm;
98 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 memset(info, 0, sizeof(*info));
101 info->card = pcm->card->number;
102 info->device = pcm->device;
103 info->stream = substream->stream;
104 info->subdevice = substream->number;
105 strlcpy(info->id, pcm->id, sizeof(info->id));
106 strlcpy(info->name, pcm->name, sizeof(info->name));
107 info->dev_class = pcm->dev_class;
108 info->dev_subclass = pcm->dev_subclass;
109 info->subdevices_count = pstr->substream_count;
110 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
111 strlcpy(info->subname, substream->name, sizeof(info->subname));
112 runtime = substream->runtime;
113 /* AB: FIXME!!! This is definitely nonsense */
114 if (runtime) {
115 info->sync = runtime->sync;
116 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
117 }
118 return 0;
119}
120
Takashi Iwai877211f2005-11-17 13:59:38 +0100121int snd_pcm_info_user(struct snd_pcm_substream *substream,
122 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Takashi Iwai877211f2005-11-17 13:59:38 +0100124 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 int err;
126
127 info = kmalloc(sizeof(*info), GFP_KERNEL);
128 if (! info)
129 return -ENOMEM;
130 err = snd_pcm_info(substream, info);
131 if (err >= 0) {
132 if (copy_to_user(_info, info, sizeof(*info)))
133 err = -EFAULT;
134 }
135 kfree(info);
136 return err;
137}
138
139#undef RULES_DEBUG
140
141#ifdef RULES_DEBUG
142#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
143char *snd_pcm_hw_param_names[] = {
144 HW_PARAM(ACCESS),
145 HW_PARAM(FORMAT),
146 HW_PARAM(SUBFORMAT),
147 HW_PARAM(SAMPLE_BITS),
148 HW_PARAM(FRAME_BITS),
149 HW_PARAM(CHANNELS),
150 HW_PARAM(RATE),
151 HW_PARAM(PERIOD_TIME),
152 HW_PARAM(PERIOD_SIZE),
153 HW_PARAM(PERIOD_BYTES),
154 HW_PARAM(PERIODS),
155 HW_PARAM(BUFFER_TIME),
156 HW_PARAM(BUFFER_SIZE),
157 HW_PARAM(BUFFER_BYTES),
158 HW_PARAM(TICK_TIME),
159};
160#endif
161
Takashi Iwai877211f2005-11-17 13:59:38 +0100162int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
163 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100166 struct snd_pcm_hardware *hw;
167 struct snd_interval *i = NULL;
168 struct snd_mask *m = NULL;
169 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 unsigned int rstamps[constrs->rules_num];
171 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
172 unsigned int stamp = 2;
173 int changed, again;
174
175 params->info = 0;
176 params->fifo_size = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
178 params->msbits = 0;
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
180 params->rate_num = 0;
181 params->rate_den = 0;
182 }
183
184 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
185 m = hw_param_mask(params, k);
186 if (snd_mask_empty(m))
187 return -EINVAL;
188 if (!(params->rmask & (1 << k)))
189 continue;
190#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100191 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
193#endif
194 changed = snd_mask_refine(m, constrs_mask(constrs, k));
195#ifdef RULES_DEBUG
196 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
197#endif
198 if (changed)
199 params->cmask |= 1 << k;
200 if (changed < 0)
201 return changed;
202 }
203
204 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
205 i = hw_param_interval(params, k);
206 if (snd_interval_empty(i))
207 return -EINVAL;
208 if (!(params->rmask & (1 << k)))
209 continue;
210#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100211 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (i->empty)
213 printk("empty");
214 else
215 printk("%c%u %u%c",
216 i->openmin ? '(' : '[', i->min,
217 i->max, i->openmax ? ')' : ']');
218 printk(" -> ");
219#endif
220 changed = snd_interval_refine(i, constrs_interval(constrs, k));
221#ifdef RULES_DEBUG
222 if (i->empty)
223 printk("empty\n");
224 else
225 printk("%c%u %u%c\n",
226 i->openmin ? '(' : '[', i->min,
227 i->max, i->openmax ? ')' : ']');
228#endif
229 if (changed)
230 params->cmask |= 1 << k;
231 if (changed < 0)
232 return changed;
233 }
234
235 for (k = 0; k < constrs->rules_num; k++)
236 rstamps[k] = 0;
237 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
238 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
239 do {
240 again = 0;
241 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100242 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned int d;
244 int doit = 0;
245 if (r->cond && !(r->cond & params->flags))
246 continue;
247 for (d = 0; r->deps[d] >= 0; d++) {
248 if (vstamps[r->deps[d]] > rstamps[k]) {
249 doit = 1;
250 break;
251 }
252 }
253 if (!doit)
254 continue;
255#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100256 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (r->var >= 0) {
258 printk("%s = ", snd_pcm_hw_param_names[r->var]);
259 if (hw_is_mask(r->var)) {
260 m = hw_param_mask(params, r->var);
261 printk("%x", *m->bits);
262 } else {
263 i = hw_param_interval(params, r->var);
264 if (i->empty)
265 printk("empty");
266 else
267 printk("%c%u %u%c",
268 i->openmin ? '(' : '[', i->min,
269 i->max, i->openmax ? ')' : ']');
270 }
271 }
272#endif
273 changed = r->func(params, r);
274#ifdef RULES_DEBUG
275 if (r->var >= 0) {
276 printk(" -> ");
277 if (hw_is_mask(r->var))
278 printk("%x", *m->bits);
279 else {
280 if (i->empty)
281 printk("empty");
282 else
283 printk("%c%u %u%c",
284 i->openmin ? '(' : '[', i->min,
285 i->max, i->openmax ? ')' : ']');
286 }
287 }
288 printk("\n");
289#endif
290 rstamps[k] = stamp;
291 if (changed && r->var >= 0) {
292 params->cmask |= (1 << r->var);
293 vstamps[r->var] = stamp;
294 again = 1;
295 }
296 if (changed < 0)
297 return changed;
298 stamp++;
299 }
300 } while (again);
301 if (!params->msbits) {
302 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
303 if (snd_interval_single(i))
304 params->msbits = snd_interval_value(i);
305 }
306
307 if (!params->rate_den) {
308 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
309 if (snd_interval_single(i)) {
310 params->rate_num = snd_interval_value(i);
311 params->rate_den = 1;
312 }
313 }
314
315 hw = &substream->runtime->hw;
316 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200317 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
318 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100319 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
320 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
321 if (snd_mask_min(m) == snd_mask_max(m) &&
322 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200323 changed = substream->ops->ioctl(substream,
324 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200325 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200326 return changed;
327 }
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 params->rmask = 0;
330 return 0;
331}
332
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200333EXPORT_SYMBOL(snd_pcm_hw_refine);
334
Takashi Iwai877211f2005-11-17 13:59:38 +0100335static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Takashi Iwai877211f2005-11-17 13:59:38 +0100338 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int err;
340
Li Zefanef44a1e2009-04-10 09:43:08 +0800341 params = memdup_user(_params, sizeof(*params));
342 if (IS_ERR(params))
343 return PTR_ERR(params);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 err = snd_pcm_hw_refine(substream, params);
346 if (copy_to_user(_params, params, sizeof(*params))) {
347 if (!err)
348 err = -EFAULT;
349 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 kfree(params);
352 return err;
353}
354
Takashi Iwai9442e692006-09-30 23:27:19 -0700355static int period_to_usecs(struct snd_pcm_runtime *runtime)
356{
357 int usecs;
358
359 if (! runtime->rate)
360 return -1; /* invalid */
361
362 /* take 75% of period time as the deadline */
363 usecs = (750000 / runtime->rate) * runtime->period_size;
364 usecs += ((750000 % runtime->rate) * runtime->period_size) /
365 runtime->rate;
366
367 return usecs;
368}
369
Jaroslav Kysela7910b4a2010-01-27 18:10:13 +0100370static int calc_boundary(struct snd_pcm_runtime *runtime)
371{
372 u_int64_t boundary;
373
374 boundary = (u_int64_t)runtime->buffer_size *
375 (u_int64_t)runtime->period_size;
376#if BITS_PER_LONG < 64
377 /* try to find lowest common multiple for buffer and period */
378 if (boundary > LONG_MAX - runtime->buffer_size) {
379 u_int32_t remainder = -1;
380 u_int32_t divident = runtime->buffer_size;
381 u_int32_t divisor = runtime->period_size;
382 while (remainder) {
383 remainder = divident % divisor;
384 if (remainder) {
385 divident = divisor;
386 divisor = remainder;
387 }
388 }
389 boundary = div_u64(boundary, divisor);
390 if (boundary > LONG_MAX - runtime->buffer_size)
391 return -ERANGE;
392 }
393#endif
394 if (boundary == 0)
395 return -ERANGE;
396 runtime->boundary = boundary;
397 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
398 runtime->boundary *= 2;
399 return 0;
400}
401
Takashi Iwai877211f2005-11-17 13:59:38 +0100402static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
403 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Takashi Iwai877211f2005-11-17 13:59:38 +0100405 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700406 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned int bits;
408 snd_pcm_uframes_t frames;
409
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200410 if (PCM_RUNTIME_CHECK(substream))
411 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 snd_pcm_stream_lock_irq(substream);
414 switch (runtime->status->state) {
415 case SNDRV_PCM_STATE_OPEN:
416 case SNDRV_PCM_STATE_SETUP:
417 case SNDRV_PCM_STATE_PREPARED:
418 break;
419 default:
420 snd_pcm_stream_unlock_irq(substream);
421 return -EBADFD;
422 }
423 snd_pcm_stream_unlock_irq(substream);
424#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
425 if (!substream->oss.oss)
426#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200427 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EBADFD;
429
430 params->rmask = ~0U;
431 err = snd_pcm_hw_refine(substream, params);
432 if (err < 0)
433 goto _error;
434
435 err = snd_pcm_hw_params_choose(substream, params);
436 if (err < 0)
437 goto _error;
438
439 if (substream->ops->hw_params != NULL) {
440 err = substream->ops->hw_params(substream, params);
441 if (err < 0)
442 goto _error;
443 }
444
445 runtime->access = params_access(params);
446 runtime->format = params_format(params);
447 runtime->subformat = params_subformat(params);
448 runtime->channels = params_channels(params);
449 runtime->rate = params_rate(params);
450 runtime->period_size = params_period_size(params);
451 runtime->periods = params_periods(params);
452 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 runtime->info = params->info;
454 runtime->rate_num = params->rate_num;
455 runtime->rate_den = params->rate_den;
456
457 bits = snd_pcm_format_physical_width(runtime->format);
458 runtime->sample_bits = bits;
459 bits *= runtime->channels;
460 runtime->frame_bits = bits;
461 frames = 1;
462 while (bits % 8 != 0) {
463 bits *= 2;
464 frames *= 2;
465 }
466 runtime->byte_align = bits / 8;
467 runtime->min_align = frames;
468
469 /* Default sw params */
470 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
471 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 runtime->start_threshold = 1;
474 runtime->stop_threshold = runtime->buffer_size;
475 runtime->silence_threshold = 0;
476 runtime->silence_size = 0;
Jaroslav Kysela7910b4a2010-01-27 18:10:13 +0100477 err = calc_boundary(runtime);
478 if (err < 0)
479 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 snd_pcm_timer_resolution_change(substream);
482 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700483
Mark Grossf011e2e2008-02-04 22:30:09 -0800484 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
485 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700486 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800487 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
488 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 0;
490 _error:
491 /* hardware might be unuseable from this time,
492 so we force application to retry to set
493 the correct hardware parameter settings */
494 runtime->status->state = SNDRV_PCM_STATE_OPEN;
495 if (substream->ops->hw_free != NULL)
496 substream->ops->hw_free(substream);
497 return err;
498}
499
Takashi Iwai877211f2005-11-17 13:59:38 +0100500static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
501 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Takashi Iwai877211f2005-11-17 13:59:38 +0100503 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int err;
505
Li Zefanef44a1e2009-04-10 09:43:08 +0800506 params = memdup_user(_params, sizeof(*params));
507 if (IS_ERR(params))
508 return PTR_ERR(params);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 err = snd_pcm_hw_params(substream, params);
511 if (copy_to_user(_params, params, sizeof(*params))) {
512 if (!err)
513 err = -EFAULT;
514 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 kfree(params);
517 return err;
518}
519
Takashi Iwai877211f2005-11-17 13:59:38 +0100520static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Takashi Iwai877211f2005-11-17 13:59:38 +0100522 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int result = 0;
524
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200525 if (PCM_RUNTIME_CHECK(substream))
526 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 snd_pcm_stream_lock_irq(substream);
529 switch (runtime->status->state) {
530 case SNDRV_PCM_STATE_SETUP:
531 case SNDRV_PCM_STATE_PREPARED:
532 break;
533 default:
534 snd_pcm_stream_unlock_irq(substream);
535 return -EBADFD;
536 }
537 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200538 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EBADFD;
540 if (substream->ops->hw_free)
541 result = substream->ops->hw_free(substream);
542 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800543 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
544 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return result;
546}
547
Takashi Iwai877211f2005-11-17 13:59:38 +0100548static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
549 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai877211f2005-11-17 13:59:38 +0100551 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100552 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200554 if (PCM_RUNTIME_CHECK(substream))
555 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 snd_pcm_stream_lock_irq(substream);
558 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
559 snd_pcm_stream_unlock_irq(substream);
560 return -EBADFD;
561 }
562 snd_pcm_stream_unlock_irq(substream);
563
564 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
565 return -EINVAL;
566 if (params->avail_min == 0)
567 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (params->silence_size >= runtime->boundary) {
569 if (params->silence_threshold != 0)
570 return -EINVAL;
571 } else {
572 if (params->silence_size > params->silence_threshold)
573 return -EINVAL;
574 if (params->silence_threshold > runtime->buffer_size)
575 return -EINVAL;
576 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100577 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 snd_pcm_stream_lock_irq(substream);
579 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 runtime->period_step = params->period_step;
581 runtime->control->avail_min = params->avail_min;
582 runtime->start_threshold = params->start_threshold;
583 runtime->stop_threshold = params->stop_threshold;
584 runtime->silence_threshold = params->silence_threshold;
585 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 params->boundary = runtime->boundary;
587 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
589 runtime->silence_size > 0)
590 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100591 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100594 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Takashi Iwai877211f2005-11-17 13:59:38 +0100597static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
598 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwai877211f2005-11-17 13:59:38 +0100600 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int err;
602 if (copy_from_user(&params, _params, sizeof(params)))
603 return -EFAULT;
604 err = snd_pcm_sw_params(substream, &params);
605 if (copy_to_user(_params, &params, sizeof(params)))
606 return -EFAULT;
607 return err;
608}
609
Takashi Iwai877211f2005-11-17 13:59:38 +0100610int snd_pcm_status(struct snd_pcm_substream *substream,
611 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Takashi Iwai877211f2005-11-17 13:59:38 +0100613 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 snd_pcm_stream_lock_irq(substream);
616 status->state = runtime->status->state;
617 status->suspended_state = runtime->status->suspended_state;
618 if (status->state == SNDRV_PCM_STATE_OPEN)
619 goto _end;
620 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100621 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100623 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
624 status->tstamp = runtime->status->tstamp;
625 goto _tstamp_end;
626 }
627 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100628 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100629 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 status->appl_ptr = runtime->control->appl_ptr;
631 status->hw_ptr = runtime->status->hw_ptr;
632 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
633 status->avail = snd_pcm_playback_avail(runtime);
634 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200635 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200637 status->delay += runtime->delay;
638 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 status->delay = 0;
640 } else {
641 status->avail = snd_pcm_capture_avail(runtime);
642 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200643 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 else
645 status->delay = 0;
646 }
647 status->avail_max = runtime->avail_max;
648 status->overrange = runtime->overrange;
649 runtime->avail_max = 0;
650 runtime->overrange = 0;
651 _end:
652 snd_pcm_stream_unlock_irq(substream);
653 return 0;
654}
655
Takashi Iwai877211f2005-11-17 13:59:38 +0100656static int snd_pcm_status_user(struct snd_pcm_substream *substream,
657 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Takashi Iwai877211f2005-11-17 13:59:38 +0100659 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int res;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 memset(&status, 0, sizeof(status));
663 res = snd_pcm_status(substream, &status);
664 if (res < 0)
665 return res;
666 if (copy_to_user(_status, &status, sizeof(status)))
667 return -EFAULT;
668 return 0;
669}
670
Takashi Iwai877211f2005-11-17 13:59:38 +0100671static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
672 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Takashi Iwai877211f2005-11-17 13:59:38 +0100674 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned int channel;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 channel = info->channel;
678 runtime = substream->runtime;
679 snd_pcm_stream_lock_irq(substream);
680 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
681 snd_pcm_stream_unlock_irq(substream);
682 return -EBADFD;
683 }
684 snd_pcm_stream_unlock_irq(substream);
685 if (channel >= runtime->channels)
686 return -EINVAL;
687 memset(info, 0, sizeof(*info));
688 info->channel = channel;
689 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
690}
691
Takashi Iwai877211f2005-11-17 13:59:38 +0100692static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
693 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int res;
697
698 if (copy_from_user(&info, _info, sizeof(info)))
699 return -EFAULT;
700 res = snd_pcm_channel_info(substream, &info);
701 if (res < 0)
702 return res;
703 if (copy_to_user(_info, &info, sizeof(info)))
704 return -EFAULT;
705 return 0;
706}
707
Takashi Iwai877211f2005-11-17 13:59:38 +0100708static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Takashi Iwai877211f2005-11-17 13:59:38 +0100710 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (runtime->trigger_master == NULL)
712 return;
713 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100714 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } else {
716 snd_pcm_trigger_tstamp(runtime->trigger_master);
717 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
718 }
719 runtime->trigger_master = NULL;
720}
721
722struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100723 int (*pre_action)(struct snd_pcm_substream *substream, int state);
724 int (*do_action)(struct snd_pcm_substream *substream, int state);
725 void (*undo_action)(struct snd_pcm_substream *substream, int state);
726 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727};
728
729/*
730 * this functions is core for handling of linked stream
731 * Note: the stream state might be changed also on failure
732 * Note2: call with calling stream lock + link lock
733 */
734static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100735 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int state, int do_lock)
737{
Takashi Iwai877211f2005-11-17 13:59:38 +0100738 struct snd_pcm_substream *s = NULL;
739 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int res = 0;
741
Takashi Iwaief991b92007-02-22 12:52:53 +0100742 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200744 spin_lock_nested(&s->self_group.lock,
745 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 res = ops->pre_action(s, state);
747 if (res < 0)
748 goto _unlock;
749 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100750 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 res = ops->do_action(s, state);
752 if (res < 0) {
753 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100754 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (s1 == s) /* failed stream */
756 break;
757 ops->undo_action(s1, state);
758 }
759 }
760 s = NULL; /* unlock all */
761 goto _unlock;
762 }
763 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100764 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 ops->post_action(s, state);
766 }
767 _unlock:
768 if (do_lock) {
769 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100770 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (s1 != substream)
772 spin_unlock(&s1->self_group.lock);
773 if (s1 == s) /* end */
774 break;
775 }
776 }
777 return res;
778}
779
780/*
781 * Note: call with stream lock
782 */
783static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100784 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 int state)
786{
787 int res;
788
789 res = ops->pre_action(substream, state);
790 if (res < 0)
791 return res;
792 res = ops->do_action(substream, state);
793 if (res == 0)
794 ops->post_action(substream, state);
795 else if (ops->undo_action)
796 ops->undo_action(substream, state);
797 return res;
798}
799
800/*
801 * Note: call with stream lock
802 */
803static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100804 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int state)
806{
807 int res;
808
809 if (snd_pcm_stream_linked(substream)) {
810 if (!spin_trylock(&substream->group->lock)) {
811 spin_unlock(&substream->self_group.lock);
812 spin_lock(&substream->group->lock);
813 spin_lock(&substream->self_group.lock);
814 }
815 res = snd_pcm_action_group(ops, substream, state, 1);
816 spin_unlock(&substream->group->lock);
817 } else {
818 res = snd_pcm_action_single(ops, substream, state);
819 }
820 return res;
821}
822
823/*
824 * Note: don't use any locks before
825 */
826static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100827 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int state)
829{
830 int res;
831
832 read_lock_irq(&snd_pcm_link_rwlock);
833 if (snd_pcm_stream_linked(substream)) {
834 spin_lock(&substream->group->lock);
835 spin_lock(&substream->self_group.lock);
836 res = snd_pcm_action_group(ops, substream, state, 1);
837 spin_unlock(&substream->self_group.lock);
838 spin_unlock(&substream->group->lock);
839 } else {
840 spin_lock(&substream->self_group.lock);
841 res = snd_pcm_action_single(ops, substream, state);
842 spin_unlock(&substream->self_group.lock);
843 }
844 read_unlock_irq(&snd_pcm_link_rwlock);
845 return res;
846}
847
848/*
849 */
850static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100851 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int state)
853{
854 int res;
855
856 down_read(&snd_pcm_link_rwsem);
857 if (snd_pcm_stream_linked(substream))
858 res = snd_pcm_action_group(ops, substream, state, 0);
859 else
860 res = snd_pcm_action_single(ops, substream, state);
861 up_read(&snd_pcm_link_rwsem);
862 return res;
863}
864
865/*
866 * start callbacks
867 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100868static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
872 return -EBADFD;
873 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
874 !snd_pcm_playback_data(substream))
875 return -EPIPE;
876 runtime->trigger_master = substream;
877 return 0;
878}
879
Takashi Iwai877211f2005-11-17 13:59:38 +0100880static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 if (substream->runtime->trigger_master != substream)
883 return 0;
884 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
885}
886
Takashi Iwai877211f2005-11-17 13:59:38 +0100887static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 if (substream->runtime->trigger_master == substream)
890 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
891}
892
Takashi Iwai877211f2005-11-17 13:59:38 +0100893static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200897 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 runtime->status->state = state;
899 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
900 runtime->silence_size > 0)
901 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100903 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
904 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907static struct action_ops snd_pcm_action_start = {
908 .pre_action = snd_pcm_pre_start,
909 .do_action = snd_pcm_do_start,
910 .undo_action = snd_pcm_undo_start,
911 .post_action = snd_pcm_post_start
912};
913
914/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700915 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200916 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100918int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Takashi Iwai877211f2005-11-17 13:59:38 +0100920 return snd_pcm_action(&snd_pcm_action_start, substream,
921 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/*
925 * stop callbacks
926 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100927static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
Takashi Iwai877211f2005-11-17 13:59:38 +0100929 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
931 return -EBADFD;
932 runtime->trigger_master = substream;
933 return 0;
934}
935
Takashi Iwai877211f2005-11-17 13:59:38 +0100936static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 if (substream->runtime->trigger_master == substream &&
939 snd_pcm_running(substream))
940 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
941 return 0; /* unconditonally stop all substreams */
942}
943
Takashi Iwai877211f2005-11-17 13:59:38 +0100944static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Takashi Iwai877211f2005-11-17 13:59:38 +0100946 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (runtime->status->state != state) {
948 snd_pcm_trigger_tstamp(substream);
949 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100950 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
951 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
954 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100955 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958static struct action_ops snd_pcm_action_stop = {
959 .pre_action = snd_pcm_pre_stop,
960 .do_action = snd_pcm_do_stop,
961 .post_action = snd_pcm_post_stop
962};
963
964/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700965 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200966 * @substream: the PCM substream instance
967 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700969 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100971int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
974}
975
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200976EXPORT_SYMBOL(snd_pcm_stop);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700979 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200980 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700982 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Unlike snd_pcm_stop(), this affects only the given stream.
984 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100985int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Takashi Iwai877211f2005-11-17 13:59:38 +0100987 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
988 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991/*
992 * pause callbacks
993 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100994static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
Takashi Iwai877211f2005-11-17 13:59:38 +0100996 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
998 return -ENOSYS;
999 if (push) {
1000 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1001 return -EBADFD;
1002 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1003 return -EBADFD;
1004 runtime->trigger_master = substream;
1005 return 0;
1006}
1007
Takashi Iwai877211f2005-11-17 13:59:38 +01001008static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 if (substream->runtime->trigger_master != substream)
1011 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001012 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1013 * a delta betwen the current jiffies, this gives a large enough
1014 * delta, effectively to skip the check once.
1015 */
1016 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return substream->ops->trigger(substream,
1018 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1019 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1020}
1021
Takashi Iwai877211f2005-11-17 13:59:38 +01001022static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 if (substream->runtime->trigger_master == substream)
1025 substream->ops->trigger(substream,
1026 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1027 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1028}
1029
Takashi Iwai877211f2005-11-17 13:59:38 +01001030static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Takashi Iwai877211f2005-11-17 13:59:38 +01001032 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 snd_pcm_trigger_tstamp(substream);
1034 if (push) {
1035 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1036 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001037 snd_timer_notify(substream->timer,
1038 SNDRV_TIMER_EVENT_MPAUSE,
1039 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001041 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 } else {
1043 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001045 snd_timer_notify(substream->timer,
1046 SNDRV_TIMER_EVENT_MCONTINUE,
1047 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 }
1049}
1050
1051static struct action_ops snd_pcm_action_pause = {
1052 .pre_action = snd_pcm_pre_pause,
1053 .do_action = snd_pcm_do_pause,
1054 .undo_action = snd_pcm_undo_pause,
1055 .post_action = snd_pcm_post_pause
1056};
1057
1058/*
1059 * Push/release the pause for all linked streams.
1060 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001061static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
1063 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1064}
1065
1066#ifdef CONFIG_PM
1067/* suspend */
1068
Takashi Iwai877211f2005-11-17 13:59:38 +01001069static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Takashi Iwai877211f2005-11-17 13:59:38 +01001071 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1073 return -EBUSY;
1074 runtime->trigger_master = substream;
1075 return 0;
1076}
1077
Takashi Iwai877211f2005-11-17 13:59:38 +01001078static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Takashi Iwai877211f2005-11-17 13:59:38 +01001080 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (runtime->trigger_master != substream)
1082 return 0;
1083 if (! snd_pcm_running(substream))
1084 return 0;
1085 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1086 return 0; /* suspend unconditionally */
1087}
1088
Takashi Iwai877211f2005-11-17 13:59:38 +01001089static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Takashi Iwai877211f2005-11-17 13:59:38 +01001091 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 snd_pcm_trigger_tstamp(substream);
1093 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001094 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1095 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 runtime->status->suspended_state = runtime->status->state;
1097 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001099 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102static struct action_ops snd_pcm_action_suspend = {
1103 .pre_action = snd_pcm_pre_suspend,
1104 .do_action = snd_pcm_do_suspend,
1105 .post_action = snd_pcm_post_suspend
1106};
1107
1108/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001109 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001110 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * After this call, all streams are changed to SUSPENDED state.
1113 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001114int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 int err;
1117 unsigned long flags;
1118
Takashi Iwai603bf522005-11-17 15:59:14 +01001119 if (! substream)
1120 return 0;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 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
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001128EXPORT_SYMBOL(snd_pcm_suspend);
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001131 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001132 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * After this call, all streams are changed to SUSPENDED state.
1135 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001136int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Takashi Iwai877211f2005-11-17 13:59:38 +01001138 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 int stream, err = 0;
1140
Takashi Iwai603bf522005-11-17 15:59:14 +01001141 if (! pcm)
1142 return 0;
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001145 for (substream = pcm->streams[stream].substream;
1146 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* FIXME: the open/close code should lock this as well */
1148 if (substream->runtime == NULL)
1149 continue;
1150 err = snd_pcm_suspend(substream);
1151 if (err < 0 && err != -EBUSY)
1152 return err;
1153 }
1154 }
1155 return 0;
1156}
1157
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001158EXPORT_SYMBOL(snd_pcm_suspend_all);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/* resume */
1161
Takashi Iwai877211f2005-11-17 13:59:38 +01001162static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Takashi Iwai877211f2005-11-17 13:59:38 +01001164 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1166 return -ENOSYS;
1167 runtime->trigger_master = substream;
1168 return 0;
1169}
1170
Takashi Iwai877211f2005-11-17 13:59:38 +01001171static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwai877211f2005-11-17 13:59:38 +01001173 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (runtime->trigger_master != substream)
1175 return 0;
1176 /* DMA not running previously? */
1177 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1178 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1179 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1180 return 0;
1181 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1182}
1183
Takashi Iwai877211f2005-11-17 13:59:38 +01001184static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 if (substream->runtime->trigger_master == substream &&
1187 snd_pcm_running(substream))
1188 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1189}
1190
Takashi Iwai877211f2005-11-17 13:59:38 +01001191static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Takashi Iwai877211f2005-11-17 13:59:38 +01001193 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 snd_pcm_trigger_tstamp(substream);
1195 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001196 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1197 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static struct action_ops snd_pcm_action_resume = {
1202 .pre_action = snd_pcm_pre_resume,
1203 .do_action = snd_pcm_do_resume,
1204 .undo_action = snd_pcm_undo_resume,
1205 .post_action = snd_pcm_post_resume
1206};
1207
Takashi Iwai877211f2005-11-17 13:59:38 +01001208static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Takashi Iwai877211f2005-11-17 13:59:38 +01001210 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int res;
1212
1213 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001214 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1216 snd_power_unlock(card);
1217 return res;
1218}
1219
1220#else
1221
Takashi Iwai877211f2005-11-17 13:59:38 +01001222static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 return -ENOSYS;
1225}
1226
1227#endif /* CONFIG_PM */
1228
1229/*
1230 * xrun ioctl
1231 *
1232 * Change the RUNNING stream(s) to XRUN state.
1233 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001234static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Takashi Iwai877211f2005-11-17 13:59:38 +01001236 struct snd_card *card = substream->pcm->card;
1237 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 int result;
1239
1240 snd_power_lock(card);
1241 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001242 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (result < 0)
1244 goto _unlock;
1245 }
1246
1247 snd_pcm_stream_lock_irq(substream);
1248 switch (runtime->status->state) {
1249 case SNDRV_PCM_STATE_XRUN:
1250 result = 0; /* already there */
1251 break;
1252 case SNDRV_PCM_STATE_RUNNING:
1253 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1254 break;
1255 default:
1256 result = -EBADFD;
1257 }
1258 snd_pcm_stream_unlock_irq(substream);
1259 _unlock:
1260 snd_power_unlock(card);
1261 return result;
1262}
1263
1264/*
1265 * reset ioctl
1266 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001267static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Takashi Iwai877211f2005-11-17 13:59:38 +01001269 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 switch (runtime->status->state) {
1271 case SNDRV_PCM_STATE_RUNNING:
1272 case SNDRV_PCM_STATE_PREPARED:
1273 case SNDRV_PCM_STATE_PAUSED:
1274 case SNDRV_PCM_STATE_SUSPENDED:
1275 return 0;
1276 default:
1277 return -EBADFD;
1278 }
1279}
1280
Takashi Iwai877211f2005-11-17 13:59:38 +01001281static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Takashi Iwai877211f2005-11-17 13:59:38 +01001283 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1285 if (err < 0)
1286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001288 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1289 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 runtime->silence_start = runtime->status->hw_ptr;
1291 runtime->silence_filled = 0;
1292 return 0;
1293}
1294
Takashi Iwai877211f2005-11-17 13:59:38 +01001295static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Takashi Iwai877211f2005-11-17 13:59:38 +01001297 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 runtime->control->appl_ptr = runtime->status->hw_ptr;
1299 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1300 runtime->silence_size > 0)
1301 snd_pcm_playback_silence(substream, ULONG_MAX);
1302}
1303
1304static struct action_ops snd_pcm_action_reset = {
1305 .pre_action = snd_pcm_pre_reset,
1306 .do_action = snd_pcm_do_reset,
1307 .post_action = snd_pcm_post_reset
1308};
1309
Takashi Iwai877211f2005-11-17 13:59:38 +01001310static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1313}
1314
1315/*
1316 * prepare ioctl
1317 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001318/* we use the second argument for updating f_flags */
1319static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1320 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Takashi Iwai877211f2005-11-17 13:59:38 +01001322 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001323 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1324 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EBADFD;
1326 if (snd_pcm_running(substream))
1327 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001328 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
1330}
1331
Takashi Iwai877211f2005-11-17 13:59:38 +01001332static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 int err;
1335 err = substream->ops->prepare(substream);
1336 if (err < 0)
1337 return err;
1338 return snd_pcm_do_reset(substream, 0);
1339}
1340
Takashi Iwai877211f2005-11-17 13:59:38 +01001341static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Takashi Iwai877211f2005-11-17 13:59:38 +01001343 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 runtime->control->appl_ptr = runtime->status->hw_ptr;
1345 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1346}
1347
1348static struct action_ops snd_pcm_action_prepare = {
1349 .pre_action = snd_pcm_pre_prepare,
1350 .do_action = snd_pcm_do_prepare,
1351 .post_action = snd_pcm_post_prepare
1352};
1353
1354/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001355 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001356 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001357 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001359static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1360 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
1362 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001363 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001364 int f_flags;
1365
1366 if (file)
1367 f_flags = file->f_flags;
1368 else
1369 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001372 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001373 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1374 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 snd_power_unlock(card);
1376 return res;
1377}
1378
1379/*
1380 * drain ioctl
1381 */
1382
Takashi Iwai877211f2005-11-17 13:59:38 +01001383static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 substream->runtime->trigger_master = substream;
1386 return 0;
1387}
1388
Takashi Iwai877211f2005-11-17 13:59:38 +01001389static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Takashi Iwai877211f2005-11-17 13:59:38 +01001391 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1393 switch (runtime->status->state) {
1394 case SNDRV_PCM_STATE_PREPARED:
1395 /* start playback stream if possible */
1396 if (! snd_pcm_playback_empty(substream)) {
1397 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1398 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1399 }
1400 break;
1401 case SNDRV_PCM_STATE_RUNNING:
1402 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1403 break;
1404 default:
1405 break;
1406 }
1407 } else {
1408 /* stop running stream */
1409 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001410 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001412 snd_pcm_do_stop(substream, new_state);
1413 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415 }
1416 return 0;
1417}
1418
Takashi Iwai877211f2005-11-17 13:59:38 +01001419static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
1421}
1422
1423static struct action_ops snd_pcm_action_drain_init = {
1424 .pre_action = snd_pcm_pre_drain_init,
1425 .do_action = snd_pcm_do_drain_init,
1426 .post_action = snd_pcm_post_drain_init
1427};
1428
Takashi Iwai877211f2005-11-17 13:59:38 +01001429static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431/*
1432 * Drain the stream(s).
1433 * When the substream is linked, sync until the draining of all playback streams
1434 * is finished.
1435 * After this call, all streams are supposed to be either SETUP or DRAINING
1436 * (capture only) state.
1437 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001438static int snd_pcm_drain(struct snd_pcm_substream *substream,
1439 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Takashi Iwai877211f2005-11-17 13:59:38 +01001441 struct snd_card *card;
1442 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001443 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001444 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001446 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 card = substream->pcm->card;
1449 runtime = substream->runtime;
1450
1451 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1452 return -EBADFD;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 snd_power_lock(card);
1455 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001456 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001457 if (result < 0) {
1458 snd_power_unlock(card);
1459 return result;
1460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
1462
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001463 if (file) {
1464 if (file->f_flags & O_NONBLOCK)
1465 nonblock = 1;
1466 } else if (substream->f_flags & O_NONBLOCK)
1467 nonblock = 1;
1468
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001469 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001470 snd_pcm_stream_lock_irq(substream);
1471 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001472 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001473 snd_pcm_pause(substream, 0);
1474
1475 /* pre-start/stop - all running streams are changed to DRAINING state */
1476 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001477 if (result < 0)
1478 goto unlock;
1479 /* in non-blocking, we don't wait in ioctl but let caller poll */
1480 if (nonblock) {
1481 result = -EAGAIN;
1482 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 for (;;) {
1486 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001487 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 if (signal_pending(current)) {
1489 result = -ERESTARTSYS;
1490 break;
1491 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001492 /* find a substream to drain */
1493 to_check = NULL;
1494 snd_pcm_group_for_each_entry(s, substream) {
1495 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1496 continue;
1497 runtime = s->runtime;
1498 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1499 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001500 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001501 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001502 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001503 if (!to_check)
1504 break; /* all drained */
1505 init_waitqueue_entry(&wait, current);
1506 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 set_current_state(TASK_INTERRUPTIBLE);
1508 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001509 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 snd_power_unlock(card);
1511 tout = schedule_timeout(10 * HZ);
1512 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001513 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001515 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (tout == 0) {
1517 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1518 result = -ESTRPIPE;
1519 else {
1520 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1521 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1522 result = -EIO;
1523 }
1524 break;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001527
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001528 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001529 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001530 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 return result;
1534}
1535
1536/*
1537 * drop ioctl
1538 *
1539 * Immediately put all linked substreams into SETUP state.
1540 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001541static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Takashi Iwai877211f2005-11-17 13:59:38 +01001543 struct snd_pcm_runtime *runtime;
1544 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int result = 0;
1546
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001547 if (PCM_RUNTIME_CHECK(substream))
1548 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 runtime = substream->runtime;
1550 card = substream->pcm->card;
1551
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001552 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001553 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1554 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EBADFD;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 snd_pcm_stream_lock_irq(substream);
1558 /* resume pause */
1559 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1560 snd_pcm_pause(substream, 0);
1561
1562 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1563 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1564 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return result;
1567}
1568
1569
1570/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static struct file *snd_pcm_file_fd(int fd)
1572{
1573 struct file *file;
1574 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001575 unsigned int minor;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 file = fget(fd);
1578 if (!file)
1579 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001580 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (!S_ISCHR(inode->i_mode) ||
1582 imajor(inode) != snd_major) {
1583 fput(file);
1584 return NULL;
1585 }
1586 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001587 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1588 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 fput(file);
1590 return NULL;
1591 }
1592 return file;
1593}
1594
1595/*
1596 * PCM link handling
1597 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001598static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 int res = 0;
1601 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001602 struct snd_pcm_file *pcm_file;
1603 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 file = snd_pcm_file_fd(fd);
1606 if (!file)
1607 return -EBADFD;
1608 pcm_file = file->private_data;
1609 substream1 = pcm_file->substream;
1610 down_write(&snd_pcm_link_rwsem);
1611 write_lock_irq(&snd_pcm_link_rwlock);
1612 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1613 substream->runtime->status->state != substream1->runtime->status->state) {
1614 res = -EBADFD;
1615 goto _end;
1616 }
1617 if (snd_pcm_stream_linked(substream1)) {
1618 res = -EALREADY;
1619 goto _end;
1620 }
1621 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001622 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (substream->group == NULL) {
1624 res = -ENOMEM;
1625 goto _end;
1626 }
1627 spin_lock_init(&substream->group->lock);
1628 INIT_LIST_HEAD(&substream->group->substreams);
1629 list_add_tail(&substream->link_list, &substream->group->substreams);
1630 substream->group->count = 1;
1631 }
1632 list_add_tail(&substream1->link_list, &substream->group->substreams);
1633 substream->group->count++;
1634 substream1->group = substream->group;
1635 _end:
1636 write_unlock_irq(&snd_pcm_link_rwlock);
1637 up_write(&snd_pcm_link_rwsem);
1638 fput(file);
1639 return res;
1640}
1641
Takashi Iwai877211f2005-11-17 13:59:38 +01001642static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 substream->group = &substream->self_group;
1645 INIT_LIST_HEAD(&substream->self_group.substreams);
1646 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1647}
1648
Takashi Iwai877211f2005-11-17 13:59:38 +01001649static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Takashi Iwaief991b92007-02-22 12:52:53 +01001651 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int res = 0;
1653
1654 down_write(&snd_pcm_link_rwsem);
1655 write_lock_irq(&snd_pcm_link_rwlock);
1656 if (!snd_pcm_stream_linked(substream)) {
1657 res = -EALREADY;
1658 goto _end;
1659 }
1660 list_del(&substream->link_list);
1661 substream->group->count--;
1662 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001663 snd_pcm_group_for_each_entry(s, substream) {
1664 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 }
1667 kfree(substream->group);
1668 }
1669 relink_to_local(substream);
1670 _end:
1671 write_unlock_irq(&snd_pcm_link_rwlock);
1672 up_write(&snd_pcm_link_rwsem);
1673 return res;
1674}
1675
1676/*
1677 * hw configurator
1678 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001679static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1680 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai877211f2005-11-17 13:59:38 +01001682 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1684 hw_param_interval_c(params, rule->deps[1]), &t);
1685 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1686}
1687
Takashi Iwai877211f2005-11-17 13:59:38 +01001688static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1689 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Takashi Iwai877211f2005-11-17 13:59:38 +01001691 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1693 hw_param_interval_c(params, rule->deps[1]), &t);
1694 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1695}
1696
Takashi Iwai877211f2005-11-17 13:59:38 +01001697static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Takashi Iwai877211f2005-11-17 13:59:38 +01001700 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1702 hw_param_interval_c(params, rule->deps[1]),
1703 (unsigned long) rule->private, &t);
1704 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1705}
1706
Takashi Iwai877211f2005-11-17 13:59:38 +01001707static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1708 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Takashi Iwai877211f2005-11-17 13:59:38 +01001710 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1712 (unsigned long) rule->private,
1713 hw_param_interval_c(params, rule->deps[1]), &t);
1714 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1715}
1716
Takashi Iwai877211f2005-11-17 13:59:38 +01001717static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1718 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
1720 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001721 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1722 struct snd_mask m;
1723 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 snd_mask_any(&m);
1725 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1726 int bits;
1727 if (! snd_mask_test(mask, k))
1728 continue;
1729 bits = snd_pcm_format_physical_width(k);
1730 if (bits <= 0)
1731 continue; /* ignore invalid formats */
1732 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1733 snd_mask_reset(&m, k);
1734 }
1735 return snd_mask_refine(mask, &m);
1736}
1737
Takashi Iwai877211f2005-11-17 13:59:38 +01001738static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1739 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Takashi Iwai877211f2005-11-17 13:59:38 +01001741 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int k;
1743 t.min = UINT_MAX;
1744 t.max = 0;
1745 t.openmin = 0;
1746 t.openmax = 0;
1747 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1748 int bits;
1749 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1750 continue;
1751 bits = snd_pcm_format_physical_width(k);
1752 if (bits <= 0)
1753 continue; /* ignore invalid formats */
1754 if (t.min > (unsigned)bits)
1755 t.min = bits;
1756 if (t.max < (unsigned)bits)
1757 t.max = bits;
1758 }
1759 t.integer = 1;
1760 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1761}
1762
1763#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1764#error "Change this table"
1765#endif
1766
1767static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1768 48000, 64000, 88200, 96000, 176400, 192000 };
1769
Clemens Ladisch7653d552007-08-13 17:38:54 +02001770const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1771 .count = ARRAY_SIZE(rates),
1772 .list = rates,
1773};
1774
Takashi Iwai877211f2005-11-17 13:59:38 +01001775static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1776 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwai877211f2005-11-17 13:59:38 +01001778 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001780 snd_pcm_known_rates.count,
1781 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
Takashi Iwai877211f2005-11-17 13:59:38 +01001784static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1785 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Takashi Iwai877211f2005-11-17 13:59:38 +01001787 struct snd_interval t;
1788 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 t.min = 0;
1790 t.max = substream->buffer_bytes_max;
1791 t.openmin = 0;
1792 t.openmax = 0;
1793 t.integer = 1;
1794 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1795}
1796
Takashi Iwai877211f2005-11-17 13:59:38 +01001797int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Takashi Iwai877211f2005-11-17 13:59:38 +01001799 struct snd_pcm_runtime *runtime = substream->runtime;
1800 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 int k, err;
1802
1803 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1804 snd_mask_any(constrs_mask(constrs, k));
1805 }
1806
1807 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1808 snd_interval_any(constrs_interval(constrs, k));
1809 }
1810
1811 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1812 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1813 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1816
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1818 snd_pcm_hw_rule_format, NULL,
1819 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1820 if (err < 0)
1821 return err;
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1823 snd_pcm_hw_rule_sample_bits, NULL,
1824 SNDRV_PCM_HW_PARAM_FORMAT,
1825 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1829 snd_pcm_hw_rule_div, NULL,
1830 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1834 snd_pcm_hw_rule_mul, NULL,
1835 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1839 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1844 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1845 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1849 snd_pcm_hw_rule_div, NULL,
1850 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1855 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1859 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1864 snd_pcm_hw_rule_div, NULL,
1865 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1869 snd_pcm_hw_rule_div, NULL,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1879 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1881 if (err < 0)
1882 return err;
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1884 snd_pcm_hw_rule_mul, NULL,
1885 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1886 if (err < 0)
1887 return err;
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1889 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1891 if (err < 0)
1892 return err;
1893 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1894 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1895 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1896 if (err < 0)
1897 return err;
1898 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1899 snd_pcm_hw_rule_muldivk, (void*) 8,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1901 if (err < 0)
1902 return err;
1903 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1904 snd_pcm_hw_rule_muldivk, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1906 if (err < 0)
1907 return err;
1908 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1909 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1910 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1911 if (err < 0)
1912 return err;
1913 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1914 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1915 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1916 if (err < 0)
1917 return err;
1918 return 0;
1919}
1920
Takashi Iwai877211f2005-11-17 13:59:38 +01001921int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Takashi Iwai877211f2005-11-17 13:59:38 +01001923 struct snd_pcm_runtime *runtime = substream->runtime;
1924 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 int err;
1926 unsigned int mask = 0;
1927
1928 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1929 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1930 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1931 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1932 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1933 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1934 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1935 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1936 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1937 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1938 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1939 }
1940 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001941 if (err < 0)
1942 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001945 if (err < 0)
1946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001949 if (err < 0)
1950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1953 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001954 if (err < 0)
1955 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1958 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001959 if (err < 0)
1960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1963 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001964 if (err < 0)
1965 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1968 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001969 if (err < 0)
1970 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1973 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001974 if (err < 0)
1975 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1978 snd_pcm_hw_rule_buffer_bytes_max, substream,
1979 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1980 if (err < 0)
1981 return err;
1982
1983 /* FIXME: remove */
1984 if (runtime->dma_bytes) {
1985 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001986 if (err < 0)
1987 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1991 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1992 snd_pcm_hw_rule_rate, hw,
1993 SNDRV_PCM_HW_PARAM_RATE, -1);
1994 if (err < 0)
1995 return err;
1996 }
1997
1998 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2000
2001 return 0;
2002}
2003
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002004static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002007}
2008
2009void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2010{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002011 substream->ref_count--;
2012 if (substream->ref_count > 0)
2013 return;
2014
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002015 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002016 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (substream->ops->hw_free != NULL)
2018 substream->ops->hw_free(substream);
2019 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002020 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Takashi Iwai15762742006-04-06 19:47:42 +02002022 if (substream->pcm_release) {
2023 substream->pcm_release(substream);
2024 substream->pcm_release = NULL;
2025 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002026 snd_pcm_detach_substream(substream);
2027}
2028
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002029EXPORT_SYMBOL(snd_pcm_release_substream);
2030
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002031int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2032 struct file *file,
2033 struct snd_pcm_substream **rsubstream)
2034{
2035 struct snd_pcm_substream *substream;
2036 int err;
2037
2038 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2039 if (err < 0)
2040 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002041 if (substream->ref_count > 1) {
2042 *rsubstream = substream;
2043 return 0;
2044 }
2045
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002046 err = snd_pcm_hw_constraints_init(substream);
2047 if (err < 0) {
2048 snd_printd("snd_pcm_hw_constraints_init failed\n");
2049 goto error;
2050 }
2051
2052 if ((err = substream->ops->open(substream)) < 0)
2053 goto error;
2054
2055 substream->hw_opened = 1;
2056
2057 err = snd_pcm_hw_constraints_complete(substream);
2058 if (err < 0) {
2059 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2060 goto error;
2061 }
2062
2063 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002065
2066 error:
2067 snd_pcm_release_substream(substream);
2068 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002071EXPORT_SYMBOL(snd_pcm_open_substream);
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002074 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002076 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Takashi Iwai877211f2005-11-17 13:59:38 +01002078 struct snd_pcm_file *pcm_file;
2079 struct snd_pcm_substream *substream;
2080 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002083 if (rpcm_file)
2084 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002086 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2087 if (err < 0)
2088 return err;
2089
Takashi Iwai548a6482006-07-31 16:51:51 +02002090 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2091 if (pcm_file == NULL) {
2092 snd_pcm_release_substream(substream);
2093 return -ENOMEM;
2094 }
2095 pcm_file->substream = substream;
2096 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002097 str = substream->pstr;
2098 substream->file = pcm_file;
2099 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002102 if (rpcm_file)
2103 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return 0;
2105}
2106
Clemens Ladischf87135f2005-11-20 14:06:59 +01002107static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Takashi Iwai877211f2005-11-17 13:59:38 +01002109 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002110
2111 pcm = snd_lookup_minor_data(iminor(inode),
2112 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2113 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2114}
2115
2116static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2117{
2118 struct snd_pcm *pcm;
2119
2120 pcm = snd_lookup_minor_data(iminor(inode),
2121 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2122 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2123}
2124
2125static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2126{
2127 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002128 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 wait_queue_t wait;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (pcm == NULL) {
2132 err = -ENODEV;
2133 goto __error1;
2134 }
2135 err = snd_card_file_add(pcm->card, file);
2136 if (err < 0)
2137 goto __error1;
2138 if (!try_module_get(pcm->card->module)) {
2139 err = -EFAULT;
2140 goto __error2;
2141 }
2142 init_waitqueue_entry(&wait, current);
2143 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002144 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002146 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (err >= 0)
2148 break;
2149 if (err == -EAGAIN) {
2150 if (file->f_flags & O_NONBLOCK) {
2151 err = -EBUSY;
2152 break;
2153 }
2154 } else
2155 break;
2156 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002157 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002159 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (signal_pending(current)) {
2161 err = -ERESTARTSYS;
2162 break;
2163 }
2164 }
2165 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002166 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (err < 0)
2168 goto __error;
2169 return err;
2170
2171 __error:
2172 module_put(pcm->card->module);
2173 __error2:
2174 snd_card_file_remove(pcm->card, file);
2175 __error1:
2176 return err;
2177}
2178
2179static int snd_pcm_release(struct inode *inode, struct file *file)
2180{
Takashi Iwai877211f2005-11-17 13:59:38 +01002181 struct snd_pcm *pcm;
2182 struct snd_pcm_substream *substream;
2183 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 pcm_file = file->private_data;
2186 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002187 if (snd_BUG_ON(!substream))
2188 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002190 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002191 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002192 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002193 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 wake_up(&pcm->open_wait);
2195 module_put(pcm->card->module);
2196 snd_card_file_remove(pcm->card, file);
2197 return 0;
2198}
2199
Takashi Iwai877211f2005-11-17 13:59:38 +01002200static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2201 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Takashi Iwai877211f2005-11-17 13:59:38 +01002203 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 snd_pcm_sframes_t appl_ptr;
2205 snd_pcm_sframes_t ret;
2206 snd_pcm_sframes_t hw_avail;
2207
2208 if (frames == 0)
2209 return 0;
2210
2211 snd_pcm_stream_lock_irq(substream);
2212 switch (runtime->status->state) {
2213 case SNDRV_PCM_STATE_PREPARED:
2214 break;
2215 case SNDRV_PCM_STATE_DRAINING:
2216 case SNDRV_PCM_STATE_RUNNING:
2217 if (snd_pcm_update_hw_ptr(substream) >= 0)
2218 break;
2219 /* Fall through */
2220 case SNDRV_PCM_STATE_XRUN:
2221 ret = -EPIPE;
2222 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002223 case SNDRV_PCM_STATE_SUSPENDED:
2224 ret = -ESTRPIPE;
2225 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 default:
2227 ret = -EBADFD;
2228 goto __end;
2229 }
2230
2231 hw_avail = snd_pcm_playback_hw_avail(runtime);
2232 if (hw_avail <= 0) {
2233 ret = 0;
2234 goto __end;
2235 }
2236 if (frames > (snd_pcm_uframes_t)hw_avail)
2237 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 appl_ptr = runtime->control->appl_ptr - frames;
2239 if (appl_ptr < 0)
2240 appl_ptr += runtime->boundary;
2241 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 ret = frames;
2243 __end:
2244 snd_pcm_stream_unlock_irq(substream);
2245 return ret;
2246}
2247
Takashi Iwai877211f2005-11-17 13:59:38 +01002248static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2249 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Takashi Iwai877211f2005-11-17 13:59:38 +01002251 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 snd_pcm_sframes_t appl_ptr;
2253 snd_pcm_sframes_t ret;
2254 snd_pcm_sframes_t hw_avail;
2255
2256 if (frames == 0)
2257 return 0;
2258
2259 snd_pcm_stream_lock_irq(substream);
2260 switch (runtime->status->state) {
2261 case SNDRV_PCM_STATE_PREPARED:
2262 case SNDRV_PCM_STATE_DRAINING:
2263 break;
2264 case SNDRV_PCM_STATE_RUNNING:
2265 if (snd_pcm_update_hw_ptr(substream) >= 0)
2266 break;
2267 /* Fall through */
2268 case SNDRV_PCM_STATE_XRUN:
2269 ret = -EPIPE;
2270 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002271 case SNDRV_PCM_STATE_SUSPENDED:
2272 ret = -ESTRPIPE;
2273 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 default:
2275 ret = -EBADFD;
2276 goto __end;
2277 }
2278
2279 hw_avail = snd_pcm_capture_hw_avail(runtime);
2280 if (hw_avail <= 0) {
2281 ret = 0;
2282 goto __end;
2283 }
2284 if (frames > (snd_pcm_uframes_t)hw_avail)
2285 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 appl_ptr = runtime->control->appl_ptr - frames;
2287 if (appl_ptr < 0)
2288 appl_ptr += runtime->boundary;
2289 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 ret = frames;
2291 __end:
2292 snd_pcm_stream_unlock_irq(substream);
2293 return ret;
2294}
2295
Takashi Iwai877211f2005-11-17 13:59:38 +01002296static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2297 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Takashi Iwai877211f2005-11-17 13:59:38 +01002299 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 snd_pcm_sframes_t appl_ptr;
2301 snd_pcm_sframes_t ret;
2302 snd_pcm_sframes_t avail;
2303
2304 if (frames == 0)
2305 return 0;
2306
2307 snd_pcm_stream_lock_irq(substream);
2308 switch (runtime->status->state) {
2309 case SNDRV_PCM_STATE_PREPARED:
2310 case SNDRV_PCM_STATE_PAUSED:
2311 break;
2312 case SNDRV_PCM_STATE_DRAINING:
2313 case SNDRV_PCM_STATE_RUNNING:
2314 if (snd_pcm_update_hw_ptr(substream) >= 0)
2315 break;
2316 /* Fall through */
2317 case SNDRV_PCM_STATE_XRUN:
2318 ret = -EPIPE;
2319 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002320 case SNDRV_PCM_STATE_SUSPENDED:
2321 ret = -ESTRPIPE;
2322 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 default:
2324 ret = -EBADFD;
2325 goto __end;
2326 }
2327
2328 avail = snd_pcm_playback_avail(runtime);
2329 if (avail <= 0) {
2330 ret = 0;
2331 goto __end;
2332 }
2333 if (frames > (snd_pcm_uframes_t)avail)
2334 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 appl_ptr = runtime->control->appl_ptr + frames;
2336 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2337 appl_ptr -= runtime->boundary;
2338 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 ret = frames;
2340 __end:
2341 snd_pcm_stream_unlock_irq(substream);
2342 return ret;
2343}
2344
Takashi Iwai877211f2005-11-17 13:59:38 +01002345static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2346 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Takashi Iwai877211f2005-11-17 13:59:38 +01002348 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 snd_pcm_sframes_t appl_ptr;
2350 snd_pcm_sframes_t ret;
2351 snd_pcm_sframes_t avail;
2352
2353 if (frames == 0)
2354 return 0;
2355
2356 snd_pcm_stream_lock_irq(substream);
2357 switch (runtime->status->state) {
2358 case SNDRV_PCM_STATE_PREPARED:
2359 case SNDRV_PCM_STATE_DRAINING:
2360 case SNDRV_PCM_STATE_PAUSED:
2361 break;
2362 case SNDRV_PCM_STATE_RUNNING:
2363 if (snd_pcm_update_hw_ptr(substream) >= 0)
2364 break;
2365 /* Fall through */
2366 case SNDRV_PCM_STATE_XRUN:
2367 ret = -EPIPE;
2368 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002369 case SNDRV_PCM_STATE_SUSPENDED:
2370 ret = -ESTRPIPE;
2371 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 default:
2373 ret = -EBADFD;
2374 goto __end;
2375 }
2376
2377 avail = snd_pcm_capture_avail(runtime);
2378 if (avail <= 0) {
2379 ret = 0;
2380 goto __end;
2381 }
2382 if (frames > (snd_pcm_uframes_t)avail)
2383 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 appl_ptr = runtime->control->appl_ptr + frames;
2385 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2386 appl_ptr -= runtime->boundary;
2387 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ret = frames;
2389 __end:
2390 snd_pcm_stream_unlock_irq(substream);
2391 return ret;
2392}
2393
Takashi Iwai877211f2005-11-17 13:59:38 +01002394static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Takashi Iwai877211f2005-11-17 13:59:38 +01002396 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int err;
2398
2399 snd_pcm_stream_lock_irq(substream);
2400 switch (runtime->status->state) {
2401 case SNDRV_PCM_STATE_DRAINING:
2402 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2403 goto __badfd;
2404 case SNDRV_PCM_STATE_RUNNING:
2405 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2406 break;
2407 /* Fall through */
2408 case SNDRV_PCM_STATE_PREPARED:
2409 case SNDRV_PCM_STATE_SUSPENDED:
2410 err = 0;
2411 break;
2412 case SNDRV_PCM_STATE_XRUN:
2413 err = -EPIPE;
2414 break;
2415 default:
2416 __badfd:
2417 err = -EBADFD;
2418 break;
2419 }
2420 snd_pcm_stream_unlock_irq(substream);
2421 return err;
2422}
2423
Takashi Iwai877211f2005-11-17 13:59:38 +01002424static int snd_pcm_delay(struct snd_pcm_substream *substream,
2425 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Takashi Iwai877211f2005-11-17 13:59:38 +01002427 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 int err;
2429 snd_pcm_sframes_t n = 0;
2430
2431 snd_pcm_stream_lock_irq(substream);
2432 switch (runtime->status->state) {
2433 case SNDRV_PCM_STATE_DRAINING:
2434 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2435 goto __badfd;
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 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2444 n = snd_pcm_playback_hw_avail(runtime);
2445 else
2446 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002447 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 break;
2449 case SNDRV_PCM_STATE_XRUN:
2450 err = -EPIPE;
2451 break;
2452 default:
2453 __badfd:
2454 err = -EBADFD;
2455 break;
2456 }
2457 snd_pcm_stream_unlock_irq(substream);
2458 if (!err)
2459 if (put_user(n, res))
2460 err = -EFAULT;
2461 return err;
2462}
2463
Takashi Iwai877211f2005-11-17 13:59:38 +01002464static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2465 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Takashi Iwai877211f2005-11-17 13:59:38 +01002467 struct snd_pcm_runtime *runtime = substream->runtime;
2468 struct snd_pcm_sync_ptr sync_ptr;
2469 volatile struct snd_pcm_mmap_status *status;
2470 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 int err;
2472
2473 memset(&sync_ptr, 0, sizeof(sync_ptr));
2474 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2475 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002476 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 return -EFAULT;
2478 status = runtime->status;
2479 control = runtime->control;
2480 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2481 err = snd_pcm_hwsync(substream);
2482 if (err < 0)
2483 return err;
2484 }
2485 snd_pcm_stream_lock_irq(substream);
2486 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2487 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2488 else
2489 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2490 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2491 control->avail_min = sync_ptr.c.control.avail_min;
2492 else
2493 sync_ptr.c.control.avail_min = control->avail_min;
2494 sync_ptr.s.status.state = status->state;
2495 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2496 sync_ptr.s.status.tstamp = status->tstamp;
2497 sync_ptr.s.status.suspended_state = status->suspended_state;
2498 snd_pcm_stream_unlock_irq(substream);
2499 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2500 return -EFAULT;
2501 return 0;
2502}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002503
2504static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2505{
2506 struct snd_pcm_runtime *runtime = substream->runtime;
2507 int arg;
2508
2509 if (get_user(arg, _arg))
2510 return -EFAULT;
2511 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2512 return -EINVAL;
2513 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2514 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2515 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2516 return 0;
2517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Takashi Iwai0df63e42006-04-28 15:13:41 +02002519static int snd_pcm_common_ioctl1(struct file *file,
2520 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 unsigned int cmd, void __user *arg)
2522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 switch (cmd) {
2524 case SNDRV_PCM_IOCTL_PVERSION:
2525 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2526 case SNDRV_PCM_IOCTL_INFO:
2527 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002528 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2529 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002530 case SNDRV_PCM_IOCTL_TTSTAMP:
2531 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 case SNDRV_PCM_IOCTL_HW_REFINE:
2533 return snd_pcm_hw_refine_user(substream, arg);
2534 case SNDRV_PCM_IOCTL_HW_PARAMS:
2535 return snd_pcm_hw_params_user(substream, arg);
2536 case SNDRV_PCM_IOCTL_HW_FREE:
2537 return snd_pcm_hw_free(substream);
2538 case SNDRV_PCM_IOCTL_SW_PARAMS:
2539 return snd_pcm_sw_params_user(substream, arg);
2540 case SNDRV_PCM_IOCTL_STATUS:
2541 return snd_pcm_status_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2543 return snd_pcm_channel_info_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002545 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 case SNDRV_PCM_IOCTL_RESET:
2547 return snd_pcm_reset(substream);
2548 case SNDRV_PCM_IOCTL_START:
2549 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2550 case SNDRV_PCM_IOCTL_LINK:
2551 return snd_pcm_link(substream, (int)(unsigned long) arg);
2552 case SNDRV_PCM_IOCTL_UNLINK:
2553 return snd_pcm_unlink(substream);
2554 case SNDRV_PCM_IOCTL_RESUME:
2555 return snd_pcm_resume(substream);
2556 case SNDRV_PCM_IOCTL_XRUN:
2557 return snd_pcm_xrun(substream);
2558 case SNDRV_PCM_IOCTL_HWSYNC:
2559 return snd_pcm_hwsync(substream);
2560 case SNDRV_PCM_IOCTL_DELAY:
2561 return snd_pcm_delay(substream, arg);
2562 case SNDRV_PCM_IOCTL_SYNC_PTR:
2563 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002564#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2566 return snd_pcm_hw_refine_old_user(substream, arg);
2567 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2568 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002569#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002571 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 case SNDRV_PCM_IOCTL_DROP:
2573 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002574 case SNDRV_PCM_IOCTL_PAUSE:
2575 {
2576 int res;
2577 snd_pcm_stream_lock_irq(substream);
2578 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2579 snd_pcm_stream_unlock_irq(substream);
2580 return res;
2581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583 snd_printd("unknown ioctl = 0x%x\n", cmd);
2584 return -ENOTTY;
2585}
2586
Takashi Iwai0df63e42006-04-28 15:13:41 +02002587static int snd_pcm_playback_ioctl1(struct file *file,
2588 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 unsigned int cmd, void __user *arg)
2590{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002591 if (snd_BUG_ON(!substream))
2592 return -ENXIO;
2593 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2594 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 switch (cmd) {
2596 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2597 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002598 struct snd_xferi xferi;
2599 struct snd_xferi __user *_xferi = arg;
2600 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 snd_pcm_sframes_t result;
2602 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2603 return -EBADFD;
2604 if (put_user(0, &_xferi->result))
2605 return -EFAULT;
2606 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2607 return -EFAULT;
2608 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2609 __put_user(result, &_xferi->result);
2610 return result < 0 ? result : 0;
2611 }
2612 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2613 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002614 struct snd_xfern xfern;
2615 struct snd_xfern __user *_xfern = arg;
2616 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 void __user **bufs;
2618 snd_pcm_sframes_t result;
2619 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2620 return -EBADFD;
2621 if (runtime->channels > 128)
2622 return -EINVAL;
2623 if (put_user(0, &_xfern->result))
2624 return -EFAULT;
2625 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2626 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002627
2628 bufs = memdup_user(xfern.bufs,
2629 sizeof(void *) * runtime->channels);
2630 if (IS_ERR(bufs))
2631 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2633 kfree(bufs);
2634 __put_user(result, &_xfern->result);
2635 return result < 0 ? result : 0;
2636 }
2637 case SNDRV_PCM_IOCTL_REWIND:
2638 {
2639 snd_pcm_uframes_t frames;
2640 snd_pcm_uframes_t __user *_frames = arg;
2641 snd_pcm_sframes_t result;
2642 if (get_user(frames, _frames))
2643 return -EFAULT;
2644 if (put_user(0, _frames))
2645 return -EFAULT;
2646 result = snd_pcm_playback_rewind(substream, frames);
2647 __put_user(result, _frames);
2648 return result < 0 ? result : 0;
2649 }
2650 case SNDRV_PCM_IOCTL_FORWARD:
2651 {
2652 snd_pcm_uframes_t frames;
2653 snd_pcm_uframes_t __user *_frames = arg;
2654 snd_pcm_sframes_t result;
2655 if (get_user(frames, _frames))
2656 return -EFAULT;
2657 if (put_user(0, _frames))
2658 return -EFAULT;
2659 result = snd_pcm_playback_forward(substream, frames);
2660 __put_user(result, _frames);
2661 return result < 0 ? result : 0;
2662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002664 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Takashi Iwai0df63e42006-04-28 15:13:41 +02002667static int snd_pcm_capture_ioctl1(struct file *file,
2668 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 unsigned int cmd, void __user *arg)
2670{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002671 if (snd_BUG_ON(!substream))
2672 return -ENXIO;
2673 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2674 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 switch (cmd) {
2676 case SNDRV_PCM_IOCTL_READI_FRAMES:
2677 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002678 struct snd_xferi xferi;
2679 struct snd_xferi __user *_xferi = arg;
2680 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 snd_pcm_sframes_t result;
2682 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2683 return -EBADFD;
2684 if (put_user(0, &_xferi->result))
2685 return -EFAULT;
2686 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2687 return -EFAULT;
2688 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2689 __put_user(result, &_xferi->result);
2690 return result < 0 ? result : 0;
2691 }
2692 case SNDRV_PCM_IOCTL_READN_FRAMES:
2693 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002694 struct snd_xfern xfern;
2695 struct snd_xfern __user *_xfern = arg;
2696 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 void *bufs;
2698 snd_pcm_sframes_t result;
2699 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2700 return -EBADFD;
2701 if (runtime->channels > 128)
2702 return -EINVAL;
2703 if (put_user(0, &_xfern->result))
2704 return -EFAULT;
2705 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2706 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002707
2708 bufs = memdup_user(xfern.bufs,
2709 sizeof(void *) * runtime->channels);
2710 if (IS_ERR(bufs))
2711 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2713 kfree(bufs);
2714 __put_user(result, &_xfern->result);
2715 return result < 0 ? result : 0;
2716 }
2717 case SNDRV_PCM_IOCTL_REWIND:
2718 {
2719 snd_pcm_uframes_t frames;
2720 snd_pcm_uframes_t __user *_frames = arg;
2721 snd_pcm_sframes_t result;
2722 if (get_user(frames, _frames))
2723 return -EFAULT;
2724 if (put_user(0, _frames))
2725 return -EFAULT;
2726 result = snd_pcm_capture_rewind(substream, frames);
2727 __put_user(result, _frames);
2728 return result < 0 ? result : 0;
2729 }
2730 case SNDRV_PCM_IOCTL_FORWARD:
2731 {
2732 snd_pcm_uframes_t frames;
2733 snd_pcm_uframes_t __user *_frames = arg;
2734 snd_pcm_sframes_t result;
2735 if (get_user(frames, _frames))
2736 return -EFAULT;
2737 if (put_user(0, _frames))
2738 return -EFAULT;
2739 result = snd_pcm_capture_forward(substream, frames);
2740 __put_user(result, _frames);
2741 return result < 0 ? result : 0;
2742 }
2743 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002744 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Takashi Iwai877211f2005-11-17 13:59:38 +01002747static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2748 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
Takashi Iwai877211f2005-11-17 13:59:38 +01002750 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
2752 pcm_file = file->private_data;
2753
2754 if (((cmd >> 8) & 0xff) != 'A')
2755 return -ENOTTY;
2756
Takashi Iwai0df63e42006-04-28 15:13:41 +02002757 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2758 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Takashi Iwai877211f2005-11-17 13:59:38 +01002761static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2762 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Takashi Iwai877211f2005-11-17 13:59:38 +01002764 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 pcm_file = file->private_data;
2767
2768 if (((cmd >> 8) & 0xff) != 'A')
2769 return -ENOTTY;
2770
Takashi Iwai0df63e42006-04-28 15:13:41 +02002771 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2772 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Takashi Iwai877211f2005-11-17 13:59:38 +01002775int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 unsigned int cmd, void *arg)
2777{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002778 mm_segment_t fs;
2779 int result;
2780
2781 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 switch (substream->stream) {
2783 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002784 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2785 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002788 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2789 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002792 result = -EINVAL;
2793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002795 snd_leave_user(fs);
2796 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002799EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2800
Takashi Iwai877211f2005-11-17 13:59:38 +01002801static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2802 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Takashi Iwai877211f2005-11-17 13:59:38 +01002804 struct snd_pcm_file *pcm_file;
2805 struct snd_pcm_substream *substream;
2806 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 snd_pcm_sframes_t result;
2808
2809 pcm_file = file->private_data;
2810 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002811 if (PCM_RUNTIME_CHECK(substream))
2812 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 runtime = substream->runtime;
2814 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2815 return -EBADFD;
2816 if (!frame_aligned(runtime, count))
2817 return -EINVAL;
2818 count = bytes_to_frames(runtime, count);
2819 result = snd_pcm_lib_read(substream, buf, count);
2820 if (result > 0)
2821 result = frames_to_bytes(runtime, result);
2822 return result;
2823}
2824
Takashi Iwai877211f2005-11-17 13:59:38 +01002825static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2826 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Takashi Iwai877211f2005-11-17 13:59:38 +01002828 struct snd_pcm_file *pcm_file;
2829 struct snd_pcm_substream *substream;
2830 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 snd_pcm_sframes_t result;
2832
2833 pcm_file = file->private_data;
2834 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002835 if (PCM_RUNTIME_CHECK(substream))
2836 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002838 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2839 return -EBADFD;
2840 if (!frame_aligned(runtime, count))
2841 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 count = bytes_to_frames(runtime, count);
2843 result = snd_pcm_lib_write(substream, buf, count);
2844 if (result > 0)
2845 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return result;
2847}
2848
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002849static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2850 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852{
Takashi Iwai877211f2005-11-17 13:59:38 +01002853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 snd_pcm_sframes_t result;
2857 unsigned long i;
2858 void __user **bufs;
2859 snd_pcm_uframes_t frames;
2860
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002861 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002863 if (PCM_RUNTIME_CHECK(substream))
2864 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 runtime = substream->runtime;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002870 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002872 frames = bytes_to_samples(runtime, iov->iov_len);
2873 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (bufs == NULL)
2875 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002876 for (i = 0; i < nr_segs; ++i)
2877 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 result = snd_pcm_lib_readv(substream, bufs, frames);
2879 if (result > 0)
2880 result = frames_to_bytes(runtime, result);
2881 kfree(bufs);
2882 return result;
2883}
2884
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002885static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2886 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Takashi Iwai877211f2005-11-17 13:59:38 +01002888 struct snd_pcm_file *pcm_file;
2889 struct snd_pcm_substream *substream;
2890 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 snd_pcm_sframes_t result;
2892 unsigned long i;
2893 void __user **bufs;
2894 snd_pcm_uframes_t frames;
2895
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002896 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002898 if (PCM_RUNTIME_CHECK(substream))
2899 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002901 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2902 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002903 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002904 !frame_aligned(runtime, iov->iov_len))
2905 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002906 frames = bytes_to_samples(runtime, iov->iov_len);
2907 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (bufs == NULL)
2909 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002910 for (i = 0; i < nr_segs; ++i)
2911 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 result = snd_pcm_lib_writev(substream, bufs, frames);
2913 if (result > 0)
2914 result = frames_to_bytes(runtime, result);
2915 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return result;
2917}
2918
2919static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2920{
Takashi Iwai877211f2005-11-17 13:59:38 +01002921 struct snd_pcm_file *pcm_file;
2922 struct snd_pcm_substream *substream;
2923 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 unsigned int mask;
2925 snd_pcm_uframes_t avail;
2926
2927 pcm_file = file->private_data;
2928
2929 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002930 if (PCM_RUNTIME_CHECK(substream))
2931 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 runtime = substream->runtime;
2933
2934 poll_wait(file, &runtime->sleep, wait);
2935
2936 snd_pcm_stream_lock_irq(substream);
2937 avail = snd_pcm_playback_avail(runtime);
2938 switch (runtime->status->state) {
2939 case SNDRV_PCM_STATE_RUNNING:
2940 case SNDRV_PCM_STATE_PREPARED:
2941 case SNDRV_PCM_STATE_PAUSED:
2942 if (avail >= runtime->control->avail_min) {
2943 mask = POLLOUT | POLLWRNORM;
2944 break;
2945 }
2946 /* Fall through */
2947 case SNDRV_PCM_STATE_DRAINING:
2948 mask = 0;
2949 break;
2950 default:
2951 mask = POLLOUT | POLLWRNORM | POLLERR;
2952 break;
2953 }
2954 snd_pcm_stream_unlock_irq(substream);
2955 return mask;
2956}
2957
2958static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2959{
Takashi Iwai877211f2005-11-17 13:59:38 +01002960 struct snd_pcm_file *pcm_file;
2961 struct snd_pcm_substream *substream;
2962 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 unsigned int mask;
2964 snd_pcm_uframes_t avail;
2965
2966 pcm_file = file->private_data;
2967
2968 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002969 if (PCM_RUNTIME_CHECK(substream))
2970 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 runtime = substream->runtime;
2972
2973 poll_wait(file, &runtime->sleep, wait);
2974
2975 snd_pcm_stream_lock_irq(substream);
2976 avail = snd_pcm_capture_avail(runtime);
2977 switch (runtime->status->state) {
2978 case SNDRV_PCM_STATE_RUNNING:
2979 case SNDRV_PCM_STATE_PREPARED:
2980 case SNDRV_PCM_STATE_PAUSED:
2981 if (avail >= runtime->control->avail_min) {
2982 mask = POLLIN | POLLRDNORM;
2983 break;
2984 }
2985 mask = 0;
2986 break;
2987 case SNDRV_PCM_STATE_DRAINING:
2988 if (avail > 0) {
2989 mask = POLLIN | POLLRDNORM;
2990 break;
2991 }
2992 /* Fall through */
2993 default:
2994 mask = POLLIN | POLLRDNORM | POLLERR;
2995 break;
2996 }
2997 snd_pcm_stream_unlock_irq(substream);
2998 return mask;
2999}
3000
3001/*
3002 * mmap support
3003 */
3004
3005/*
3006 * Only on coherent architectures, we can mmap the status and the control records
3007 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3008 */
3009#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3010/*
3011 * mmap status record
3012 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003013static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3014 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Takashi Iwai877211f2005-11-17 13:59:38 +01003016 struct snd_pcm_substream *substream = area->vm_private_data;
3017 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003020 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003022 vmf->page = virt_to_page(runtime->status);
3023 get_page(vmf->page);
3024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003027static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003029 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030};
3031
Takashi Iwai877211f2005-11-17 13:59:38 +01003032static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct vm_area_struct *area)
3034{
Takashi Iwai877211f2005-11-17 13:59:38 +01003035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 long size;
3037 if (!(area->vm_flags & VM_READ))
3038 return -EINVAL;
3039 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003041 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return -EINVAL;
3043 area->vm_ops = &snd_pcm_vm_ops_status;
3044 area->vm_private_data = substream;
3045 area->vm_flags |= VM_RESERVED;
3046 return 0;
3047}
3048
3049/*
3050 * mmap control record
3051 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003052static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3053 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
Takashi Iwai877211f2005-11-17 13:59:38 +01003055 struct snd_pcm_substream *substream = area->vm_private_data;
3056 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003059 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003061 vmf->page = virt_to_page(runtime->control);
3062 get_page(vmf->page);
3063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003066static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003068 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069};
3070
Takashi Iwai877211f2005-11-17 13:59:38 +01003071static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 struct vm_area_struct *area)
3073{
Takashi Iwai877211f2005-11-17 13:59:38 +01003074 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 long size;
3076 if (!(area->vm_flags & VM_READ))
3077 return -EINVAL;
3078 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003080 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return -EINVAL;
3082 area->vm_ops = &snd_pcm_vm_ops_control;
3083 area->vm_private_data = substream;
3084 area->vm_flags |= VM_RESERVED;
3085 return 0;
3086}
3087#else /* ! coherent mmap */
3088/*
3089 * don't support mmap for status and control records.
3090 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003091static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct vm_area_struct *area)
3093{
3094 return -ENXIO;
3095}
Takashi Iwai877211f2005-11-17 13:59:38 +01003096static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 struct vm_area_struct *area)
3098{
3099 return -ENXIO;
3100}
3101#endif /* coherent mmap */
3102
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003103static inline struct page *
3104snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3105{
3106 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003107#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3108 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3109 return virt_to_page(CAC_ADDR(vaddr));
3110#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003111#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3112 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3113 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3114 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3115 /* assume dma_handle set via pfn_to_phys() in
3116 * mm/dma-noncoherent.c
3117 */
3118 return pfn_to_page(addr >> PAGE_SHIFT);
3119 }
3120#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003121 return virt_to_page(vaddr);
3122}
3123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003125 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003127static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3128 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Takashi Iwai877211f2005-11-17 13:59:38 +01003130 struct snd_pcm_substream *substream = area->vm_private_data;
3131 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 unsigned long offset;
3133 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 size_t dma_bytes;
3135
3136 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003137 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003139 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3141 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003142 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003143 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003145 else
3146 page = snd_pcm_default_page_ops(substream, offset);
3147 if (!page)
3148 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003149 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003150 vmf->page = page;
3151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Takashi Iwai657b1982009-11-26 12:40:21 +01003154static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3155 .open = snd_pcm_mmap_data_open,
3156 .close = snd_pcm_mmap_data_close,
3157};
3158
3159static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 .open = snd_pcm_mmap_data_open,
3161 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003162 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163};
3164
Takashi Iwai657b1982009-11-26 12:40:21 +01003165#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3166/* This should be defined / handled globally! */
3167#ifdef CONFIG_ARM
3168#define ARCH_HAS_DMA_MMAP_COHERENT
3169#endif
3170#endif
3171
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172/*
3173 * mmap the DMA buffer on RAM
3174 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003175static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3176 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003179#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3180 if (!substream->ops->page &&
3181 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3182 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3183 area,
3184 substream->runtime->dma_area,
3185 substream->runtime->dma_addr,
3186 area->vm_end - area->vm_start);
3187#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3188 /* mmap with fault handler */
3189 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return 0;
3191}
3192
3193/*
3194 * mmap the DMA buffer on I/O memory area
3195 */
3196#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003197int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3198 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
3200 long size;
3201 unsigned long offset;
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 area->vm_flags |= VM_IO;
3205 size = area->vm_end - area->vm_start;
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3207 if (io_remap_pfn_range(area, area->vm_start,
3208 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3209 size, area->vm_page_prot))
3210 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return 0;
3212}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003213
3214EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215#endif /* SNDRV_PCM_INFO_MMAP */
3216
Takashi Iwaic32d9772010-01-18 14:58:57 +01003217/* mmap callback with pgprot_noncached */
3218int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3219 struct vm_area_struct *area)
3220{
3221 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3222 return snd_pcm_default_mmap(substream, area);
3223}
3224EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
3227 * mmap DMA buffer
3228 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003229int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct vm_area_struct *area)
3231{
Takashi Iwai877211f2005-11-17 13:59:38 +01003232 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 long size;
3234 unsigned long offset;
3235 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3239 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3240 return -EINVAL;
3241 } else {
3242 if (!(area->vm_flags & VM_READ))
3243 return -EINVAL;
3244 }
3245 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3247 return -EBADFD;
3248 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3249 return -ENXIO;
3250 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3251 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3252 return -EINVAL;
3253 size = area->vm_end - area->vm_start;
3254 offset = area->vm_pgoff << PAGE_SHIFT;
3255 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3256 if ((size_t)size > dma_bytes)
3257 return -EINVAL;
3258 if (offset > dma_bytes - size)
3259 return -EINVAL;
3260
Takashi Iwai657b1982009-11-26 12:40:21 +01003261 area->vm_ops = &snd_pcm_vm_ops_data;
3262 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003264 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003266 err = snd_pcm_default_mmap(substream, area);
3267 if (!err)
3268 atomic_inc(&substream->mmap_count);
3269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003272EXPORT_SYMBOL(snd_pcm_mmap_data);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3275{
Takashi Iwai877211f2005-11-17 13:59:38 +01003276 struct snd_pcm_file * pcm_file;
3277 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 unsigned long offset;
3279
3280 pcm_file = file->private_data;
3281 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003282 if (PCM_RUNTIME_CHECK(substream))
3283 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
3285 offset = area->vm_pgoff << PAGE_SHIFT;
3286 switch (offset) {
3287 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003288 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 return -ENXIO;
3290 return snd_pcm_mmap_status(substream, file, area);
3291 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003292 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return -ENXIO;
3294 return snd_pcm_mmap_control(substream, file, area);
3295 default:
3296 return snd_pcm_mmap_data(substream, file, area);
3297 }
3298 return 0;
3299}
3300
3301static int snd_pcm_fasync(int fd, struct file * file, int on)
3302{
Takashi Iwai877211f2005-11-17 13:59:38 +01003303 struct snd_pcm_file * pcm_file;
3304 struct snd_pcm_substream *substream;
3305 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003306 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003308 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 pcm_file = file->private_data;
3310 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003311 if (PCM_RUNTIME_CHECK(substream))
3312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003315out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003316 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003317 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318}
3319
3320/*
3321 * ioctl32 compat
3322 */
3323#ifdef CONFIG_COMPAT
3324#include "pcm_compat.c"
3325#else
3326#define snd_pcm_ioctl_compat NULL
3327#endif
3328
3329/*
3330 * To be removed helpers to keep binary compatibility
3331 */
3332
Takashi Iwai59d48582005-12-01 10:51:58 +01003333#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3335#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3336
Takashi Iwai877211f2005-11-17 13:59:38 +01003337static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3338 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 unsigned int i;
3341
3342 memset(params, 0, sizeof(*params));
3343 params->flags = oparams->flags;
3344 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3345 params->masks[i].bits[0] = oparams->masks[i];
3346 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3347 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3348 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3349 params->info = oparams->info;
3350 params->msbits = oparams->msbits;
3351 params->rate_num = oparams->rate_num;
3352 params->rate_den = oparams->rate_den;
3353 params->fifo_size = oparams->fifo_size;
3354}
3355
Takashi Iwai877211f2005-11-17 13:59:38 +01003356static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3357 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 unsigned int i;
3360
3361 memset(oparams, 0, sizeof(*oparams));
3362 oparams->flags = params->flags;
3363 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3364 oparams->masks[i] = params->masks[i].bits[0];
3365 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3366 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3367 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3368 oparams->info = params->info;
3369 oparams->msbits = params->msbits;
3370 oparams->rate_num = params->rate_num;
3371 oparams->rate_den = params->rate_den;
3372 oparams->fifo_size = params->fifo_size;
3373}
3374
Takashi Iwai877211f2005-11-17 13:59:38 +01003375static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3376 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Takashi Iwai877211f2005-11-17 13:59:38 +01003378 struct snd_pcm_hw_params *params;
3379 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 int err;
3381
3382 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003383 if (!params)
3384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Li Zefanef44a1e2009-04-10 09:43:08 +08003386 oparams = memdup_user(_oparams, sizeof(*oparams));
3387 if (IS_ERR(oparams)) {
3388 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 goto out;
3390 }
3391 snd_pcm_hw_convert_from_old_params(params, oparams);
3392 err = snd_pcm_hw_refine(substream, params);
3393 snd_pcm_hw_convert_to_old_params(oparams, params);
3394 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3395 if (!err)
3396 err = -EFAULT;
3397 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003398
3399 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400out:
3401 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 return err;
3403}
3404
Takashi Iwai877211f2005-11-17 13:59:38 +01003405static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3406 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407{
Takashi Iwai877211f2005-11-17 13:59:38 +01003408 struct snd_pcm_hw_params *params;
3409 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 int err;
3411
3412 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003413 if (!params)
3414 return -ENOMEM;
3415
3416 oparams = memdup_user(_oparams, sizeof(*oparams));
3417 if (IS_ERR(oparams)) {
3418 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 goto out;
3420 }
3421 snd_pcm_hw_convert_from_old_params(params, oparams);
3422 err = snd_pcm_hw_params(substream, params);
3423 snd_pcm_hw_convert_to_old_params(oparams, params);
3424 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3425 if (!err)
3426 err = -EFAULT;
3427 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003428
3429 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430out:
3431 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return err;
3433}
Takashi Iwai59d48582005-12-01 10:51:58 +01003434#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Cliff Cai70036092008-09-03 10:54:36 +02003436#ifndef CONFIG_MMU
3437unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3438 unsigned long len, unsigned long pgoff,
3439 unsigned long flags)
3440{
3441 return 0;
3442}
3443#else
3444# define dummy_get_unmapped_area NULL
3445#endif
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447/*
3448 * Register section
3449 */
3450
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003451const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003453 .owner = THIS_MODULE,
3454 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003455 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003456 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003457 .release = snd_pcm_release,
3458 .poll = snd_pcm_playback_poll,
3459 .unlocked_ioctl = snd_pcm_playback_ioctl,
3460 .compat_ioctl = snd_pcm_ioctl_compat,
3461 .mmap = snd_pcm_mmap,
3462 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003463 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 },
3465 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003466 .owner = THIS_MODULE,
3467 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003468 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003469 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003470 .release = snd_pcm_release,
3471 .poll = snd_pcm_capture_poll,
3472 .unlocked_ioctl = snd_pcm_capture_ioctl,
3473 .compat_ioctl = snd_pcm_ioctl_compat,
3474 .mmap = snd_pcm_mmap,
3475 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003476 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478};