blob: 20b5982c996b9a2aeef8ad200f62fe79cca41767 [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>
Takashi Iwai9fe17b52010-05-12 10:32:42 +020039#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
40#include <dma-coherence.h>
41#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
44 * Compatibility
45 */
46
Takashi Iwai877211f2005-11-17 13:59:38 +010047struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 unsigned int flags;
49 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
50 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010051 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
53 unsigned int rmask;
54 unsigned int cmask;
55 unsigned int info;
56 unsigned int msbits;
57 unsigned int rate_num;
58 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010059 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned char reserved[64];
61};
62
Takashi Iwai59d48582005-12-01 10:51:58 +010063#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010064#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
65#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Takashi Iwai877211f2005-11-17 13:59:38 +010067static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
68 struct snd_pcm_hw_params_old __user * _oparams);
69static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
70 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010071#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010072static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74/*
75 *
76 */
77
78DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020079EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020081static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static inline mm_segment_t snd_enter_user(void)
84{
85 mm_segment_t fs = get_fs();
86 set_fs(get_ds());
87 return fs;
88}
89
90static inline void snd_leave_user(mm_segment_t fs)
91{
92 set_fs(fs);
93}
94
95
96
Takashi Iwai877211f2005-11-17 13:59:38 +010097int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Takashi Iwai877211f2005-11-17 13:59:38 +010099 struct snd_pcm_runtime *runtime;
100 struct snd_pcm *pcm = substream->pcm;
101 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 memset(info, 0, sizeof(*info));
104 info->card = pcm->card->number;
105 info->device = pcm->device;
106 info->stream = substream->stream;
107 info->subdevice = substream->number;
108 strlcpy(info->id, pcm->id, sizeof(info->id));
109 strlcpy(info->name, pcm->name, sizeof(info->name));
110 info->dev_class = pcm->dev_class;
111 info->dev_subclass = pcm->dev_subclass;
112 info->subdevices_count = pstr->substream_count;
113 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
114 strlcpy(info->subname, substream->name, sizeof(info->subname));
115 runtime = substream->runtime;
116 /* AB: FIXME!!! This is definitely nonsense */
117 if (runtime) {
118 info->sync = runtime->sync;
119 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
120 }
121 return 0;
122}
123
Takashi Iwai877211f2005-11-17 13:59:38 +0100124int snd_pcm_info_user(struct snd_pcm_substream *substream,
125 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Takashi Iwai877211f2005-11-17 13:59:38 +0100127 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 int err;
129
130 info = kmalloc(sizeof(*info), GFP_KERNEL);
131 if (! info)
132 return -ENOMEM;
133 err = snd_pcm_info(substream, info);
134 if (err >= 0) {
135 if (copy_to_user(_info, info, sizeof(*info)))
136 err = -EFAULT;
137 }
138 kfree(info);
139 return err;
140}
141
142#undef RULES_DEBUG
143
144#ifdef RULES_DEBUG
145#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
146char *snd_pcm_hw_param_names[] = {
147 HW_PARAM(ACCESS),
148 HW_PARAM(FORMAT),
149 HW_PARAM(SUBFORMAT),
150 HW_PARAM(SAMPLE_BITS),
151 HW_PARAM(FRAME_BITS),
152 HW_PARAM(CHANNELS),
153 HW_PARAM(RATE),
154 HW_PARAM(PERIOD_TIME),
155 HW_PARAM(PERIOD_SIZE),
156 HW_PARAM(PERIOD_BYTES),
157 HW_PARAM(PERIODS),
158 HW_PARAM(BUFFER_TIME),
159 HW_PARAM(BUFFER_SIZE),
160 HW_PARAM(BUFFER_BYTES),
161 HW_PARAM(TICK_TIME),
162};
163#endif
164
Takashi Iwai877211f2005-11-17 13:59:38 +0100165int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
166 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100169 struct snd_pcm_hardware *hw;
170 struct snd_interval *i = NULL;
171 struct snd_mask *m = NULL;
172 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 unsigned int rstamps[constrs->rules_num];
174 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
175 unsigned int stamp = 2;
176 int changed, again;
177
178 params->info = 0;
179 params->fifo_size = 0;
180 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
181 params->msbits = 0;
182 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
183 params->rate_num = 0;
184 params->rate_den = 0;
185 }
186
187 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
188 m = hw_param_mask(params, k);
189 if (snd_mask_empty(m))
190 return -EINVAL;
191 if (!(params->rmask & (1 << k)))
192 continue;
193#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100194 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
196#endif
197 changed = snd_mask_refine(m, constrs_mask(constrs, k));
198#ifdef RULES_DEBUG
199 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
200#endif
201 if (changed)
202 params->cmask |= 1 << k;
203 if (changed < 0)
204 return changed;
205 }
206
207 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
208 i = hw_param_interval(params, k);
209 if (snd_interval_empty(i))
210 return -EINVAL;
211 if (!(params->rmask & (1 << k)))
212 continue;
213#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100214 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (i->empty)
216 printk("empty");
217 else
218 printk("%c%u %u%c",
219 i->openmin ? '(' : '[', i->min,
220 i->max, i->openmax ? ')' : ']');
221 printk(" -> ");
222#endif
223 changed = snd_interval_refine(i, constrs_interval(constrs, k));
224#ifdef RULES_DEBUG
225 if (i->empty)
226 printk("empty\n");
227 else
228 printk("%c%u %u%c\n",
229 i->openmin ? '(' : '[', i->min,
230 i->max, i->openmax ? ')' : ']');
231#endif
232 if (changed)
233 params->cmask |= 1 << k;
234 if (changed < 0)
235 return changed;
236 }
237
238 for (k = 0; k < constrs->rules_num; k++)
239 rstamps[k] = 0;
240 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
241 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
242 do {
243 again = 0;
244 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100245 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned int d;
247 int doit = 0;
248 if (r->cond && !(r->cond & params->flags))
249 continue;
250 for (d = 0; r->deps[d] >= 0; d++) {
251 if (vstamps[r->deps[d]] > rstamps[k]) {
252 doit = 1;
253 break;
254 }
255 }
256 if (!doit)
257 continue;
258#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100259 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (r->var >= 0) {
261 printk("%s = ", snd_pcm_hw_param_names[r->var]);
262 if (hw_is_mask(r->var)) {
263 m = hw_param_mask(params, r->var);
264 printk("%x", *m->bits);
265 } else {
266 i = hw_param_interval(params, r->var);
267 if (i->empty)
268 printk("empty");
269 else
270 printk("%c%u %u%c",
271 i->openmin ? '(' : '[', i->min,
272 i->max, i->openmax ? ')' : ']');
273 }
274 }
275#endif
276 changed = r->func(params, r);
277#ifdef RULES_DEBUG
278 if (r->var >= 0) {
279 printk(" -> ");
280 if (hw_is_mask(r->var))
281 printk("%x", *m->bits);
282 else {
283 if (i->empty)
284 printk("empty");
285 else
286 printk("%c%u %u%c",
287 i->openmin ? '(' : '[', i->min,
288 i->max, i->openmax ? ')' : ']');
289 }
290 }
291 printk("\n");
292#endif
293 rstamps[k] = stamp;
294 if (changed && r->var >= 0) {
295 params->cmask |= (1 << r->var);
296 vstamps[r->var] = stamp;
297 again = 1;
298 }
299 if (changed < 0)
300 return changed;
301 stamp++;
302 }
303 } while (again);
304 if (!params->msbits) {
305 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
306 if (snd_interval_single(i))
307 params->msbits = snd_interval_value(i);
308 }
309
310 if (!params->rate_den) {
311 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
312 if (snd_interval_single(i)) {
313 params->rate_num = snd_interval_value(i);
314 params->rate_den = 1;
315 }
316 }
317
318 hw = &substream->runtime->hw;
319 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200320 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
321 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100322 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
323 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
324 if (snd_mask_min(m) == snd_mask_max(m) &&
325 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200326 changed = substream->ops->ioctl(substream,
327 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200328 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200329 return changed;
330 }
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 params->rmask = 0;
333 return 0;
334}
335
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200336EXPORT_SYMBOL(snd_pcm_hw_refine);
337
Takashi Iwai877211f2005-11-17 13:59:38 +0100338static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
339 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Takashi Iwai877211f2005-11-17 13:59:38 +0100341 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 int err;
343
Li Zefanef44a1e2009-04-10 09:43:08 +0800344 params = memdup_user(_params, sizeof(*params));
345 if (IS_ERR(params))
346 return PTR_ERR(params);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 err = snd_pcm_hw_refine(substream, params);
349 if (copy_to_user(_params, params, sizeof(*params))) {
350 if (!err)
351 err = -EFAULT;
352 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(params);
355 return err;
356}
357
Takashi Iwai9442e692006-09-30 23:27:19 -0700358static int period_to_usecs(struct snd_pcm_runtime *runtime)
359{
360 int usecs;
361
362 if (! runtime->rate)
363 return -1; /* invalid */
364
365 /* take 75% of period time as the deadline */
366 usecs = (750000 / runtime->rate) * runtime->period_size;
367 usecs += ((750000 % runtime->rate) * runtime->period_size) /
368 runtime->rate;
369
370 return usecs;
371}
372
Jaroslav Kysela7910b4a2010-01-27 18:10:13 +0100373static int calc_boundary(struct snd_pcm_runtime *runtime)
374{
375 u_int64_t boundary;
376
377 boundary = (u_int64_t)runtime->buffer_size *
378 (u_int64_t)runtime->period_size;
379#if BITS_PER_LONG < 64
380 /* try to find lowest common multiple for buffer and period */
381 if (boundary > LONG_MAX - runtime->buffer_size) {
382 u_int32_t remainder = -1;
383 u_int32_t divident = runtime->buffer_size;
384 u_int32_t divisor = runtime->period_size;
385 while (remainder) {
386 remainder = divident % divisor;
387 if (remainder) {
388 divident = divisor;
389 divisor = remainder;
390 }
391 }
392 boundary = div_u64(boundary, divisor);
393 if (boundary > LONG_MAX - runtime->buffer_size)
394 return -ERANGE;
395 }
396#endif
397 if (boundary == 0)
398 return -ERANGE;
399 runtime->boundary = boundary;
400 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
401 runtime->boundary *= 2;
402 return 0;
403}
404
Takashi Iwai877211f2005-11-17 13:59:38 +0100405static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
406 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Takashi Iwai877211f2005-11-17 13:59:38 +0100408 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700409 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned int bits;
411 snd_pcm_uframes_t frames;
412
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200413 if (PCM_RUNTIME_CHECK(substream))
414 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 snd_pcm_stream_lock_irq(substream);
417 switch (runtime->status->state) {
418 case SNDRV_PCM_STATE_OPEN:
419 case SNDRV_PCM_STATE_SETUP:
420 case SNDRV_PCM_STATE_PREPARED:
421 break;
422 default:
423 snd_pcm_stream_unlock_irq(substream);
424 return -EBADFD;
425 }
426 snd_pcm_stream_unlock_irq(substream);
427#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
428 if (!substream->oss.oss)
429#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200430 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return -EBADFD;
432
433 params->rmask = ~0U;
434 err = snd_pcm_hw_refine(substream, params);
435 if (err < 0)
436 goto _error;
437
438 err = snd_pcm_hw_params_choose(substream, params);
439 if (err < 0)
440 goto _error;
441
442 if (substream->ops->hw_params != NULL) {
443 err = substream->ops->hw_params(substream, params);
444 if (err < 0)
445 goto _error;
446 }
447
448 runtime->access = params_access(params);
449 runtime->format = params_format(params);
450 runtime->subformat = params_subformat(params);
451 runtime->channels = params_channels(params);
452 runtime->rate = params_rate(params);
453 runtime->period_size = params_period_size(params);
454 runtime->periods = params_periods(params);
455 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 runtime->info = params->info;
457 runtime->rate_num = params->rate_num;
458 runtime->rate_den = params->rate_den;
459
460 bits = snd_pcm_format_physical_width(runtime->format);
461 runtime->sample_bits = bits;
462 bits *= runtime->channels;
463 runtime->frame_bits = bits;
464 frames = 1;
465 while (bits % 8 != 0) {
466 bits *= 2;
467 frames *= 2;
468 }
469 runtime->byte_align = bits / 8;
470 runtime->min_align = frames;
471
472 /* Default sw params */
473 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
474 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 runtime->start_threshold = 1;
477 runtime->stop_threshold = runtime->buffer_size;
478 runtime->silence_threshold = 0;
479 runtime->silence_size = 0;
Jaroslav Kysela7910b4a2010-01-27 18:10:13 +0100480 err = calc_boundary(runtime);
481 if (err < 0)
482 goto _error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 snd_pcm_timer_resolution_change(substream);
485 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700486
Mark Grossf011e2e2008-02-04 22:30:09 -0800487 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
488 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700489 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800490 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
491 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493 _error:
494 /* hardware might be unuseable from this time,
495 so we force application to retry to set
496 the correct hardware parameter settings */
497 runtime->status->state = SNDRV_PCM_STATE_OPEN;
498 if (substream->ops->hw_free != NULL)
499 substream->ops->hw_free(substream);
500 return err;
501}
502
Takashi Iwai877211f2005-11-17 13:59:38 +0100503static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
504 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Takashi Iwai877211f2005-11-17 13:59:38 +0100506 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int err;
508
Li Zefanef44a1e2009-04-10 09:43:08 +0800509 params = memdup_user(_params, sizeof(*params));
510 if (IS_ERR(params))
511 return PTR_ERR(params);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 err = snd_pcm_hw_params(substream, params);
514 if (copy_to_user(_params, params, sizeof(*params))) {
515 if (!err)
516 err = -EFAULT;
517 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 kfree(params);
520 return err;
521}
522
Takashi Iwai877211f2005-11-17 13:59:38 +0100523static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Takashi Iwai877211f2005-11-17 13:59:38 +0100525 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int result = 0;
527
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200528 if (PCM_RUNTIME_CHECK(substream))
529 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 snd_pcm_stream_lock_irq(substream);
532 switch (runtime->status->state) {
533 case SNDRV_PCM_STATE_SETUP:
534 case SNDRV_PCM_STATE_PREPARED:
535 break;
536 default:
537 snd_pcm_stream_unlock_irq(substream);
538 return -EBADFD;
539 }
540 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200541 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -EBADFD;
543 if (substream->ops->hw_free)
544 result = substream->ops->hw_free(substream);
545 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800546 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
547 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return result;
549}
550
Takashi Iwai877211f2005-11-17 13:59:38 +0100551static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
552 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Takashi Iwai877211f2005-11-17 13:59:38 +0100554 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100555 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200557 if (PCM_RUNTIME_CHECK(substream))
558 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 snd_pcm_stream_lock_irq(substream);
561 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
562 snd_pcm_stream_unlock_irq(substream);
563 return -EBADFD;
564 }
565 snd_pcm_stream_unlock_irq(substream);
566
567 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
568 return -EINVAL;
569 if (params->avail_min == 0)
570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (params->silence_size >= runtime->boundary) {
572 if (params->silence_threshold != 0)
573 return -EINVAL;
574 } else {
575 if (params->silence_size > params->silence_threshold)
576 return -EINVAL;
577 if (params->silence_threshold > runtime->buffer_size)
578 return -EINVAL;
579 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100580 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 snd_pcm_stream_lock_irq(substream);
582 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 runtime->period_step = params->period_step;
584 runtime->control->avail_min = params->avail_min;
585 runtime->start_threshold = params->start_threshold;
586 runtime->stop_threshold = params->stop_threshold;
587 runtime->silence_threshold = params->silence_threshold;
588 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 params->boundary = runtime->boundary;
590 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
592 runtime->silence_size > 0)
593 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100594 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100597 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Takashi Iwai877211f2005-11-17 13:59:38 +0100600static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
601 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwai877211f2005-11-17 13:59:38 +0100603 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int err;
605 if (copy_from_user(&params, _params, sizeof(params)))
606 return -EFAULT;
607 err = snd_pcm_sw_params(substream, &params);
608 if (copy_to_user(_params, &params, sizeof(params)))
609 return -EFAULT;
610 return err;
611}
612
Takashi Iwai877211f2005-11-17 13:59:38 +0100613int snd_pcm_status(struct snd_pcm_substream *substream,
614 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Takashi Iwai877211f2005-11-17 13:59:38 +0100616 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 snd_pcm_stream_lock_irq(substream);
619 status->state = runtime->status->state;
620 status->suspended_state = runtime->status->suspended_state;
621 if (status->state == SNDRV_PCM_STATE_OPEN)
622 goto _end;
623 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100624 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100626 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
627 status->tstamp = runtime->status->tstamp;
628 goto _tstamp_end;
629 }
630 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100631 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100632 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 status->appl_ptr = runtime->control->appl_ptr;
634 status->hw_ptr = runtime->status->hw_ptr;
635 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
636 status->avail = snd_pcm_playback_avail(runtime);
637 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200638 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200640 status->delay += runtime->delay;
641 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 status->delay = 0;
643 } else {
644 status->avail = snd_pcm_capture_avail(runtime);
645 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200646 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 else
648 status->delay = 0;
649 }
650 status->avail_max = runtime->avail_max;
651 status->overrange = runtime->overrange;
652 runtime->avail_max = 0;
653 runtime->overrange = 0;
654 _end:
655 snd_pcm_stream_unlock_irq(substream);
656 return 0;
657}
658
Takashi Iwai877211f2005-11-17 13:59:38 +0100659static int snd_pcm_status_user(struct snd_pcm_substream *substream,
660 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai877211f2005-11-17 13:59:38 +0100662 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int res;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 memset(&status, 0, sizeof(status));
666 res = snd_pcm_status(substream, &status);
667 if (res < 0)
668 return res;
669 if (copy_to_user(_status, &status, sizeof(status)))
670 return -EFAULT;
671 return 0;
672}
673
Takashi Iwai877211f2005-11-17 13:59:38 +0100674static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
675 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned int channel;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 channel = info->channel;
681 runtime = substream->runtime;
682 snd_pcm_stream_lock_irq(substream);
683 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
684 snd_pcm_stream_unlock_irq(substream);
685 return -EBADFD;
686 }
687 snd_pcm_stream_unlock_irq(substream);
688 if (channel >= runtime->channels)
689 return -EINVAL;
690 memset(info, 0, sizeof(*info));
691 info->channel = channel;
692 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
693}
694
Takashi Iwai877211f2005-11-17 13:59:38 +0100695static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
696 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Takashi Iwai877211f2005-11-17 13:59:38 +0100698 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 int res;
700
701 if (copy_from_user(&info, _info, sizeof(info)))
702 return -EFAULT;
703 res = snd_pcm_channel_info(substream, &info);
704 if (res < 0)
705 return res;
706 if (copy_to_user(_info, &info, sizeof(info)))
707 return -EFAULT;
708 return 0;
709}
710
Takashi Iwai877211f2005-11-17 13:59:38 +0100711static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Takashi Iwai877211f2005-11-17 13:59:38 +0100713 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (runtime->trigger_master == NULL)
715 return;
716 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100717 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 snd_pcm_trigger_tstamp(runtime->trigger_master);
720 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
721 }
722 runtime->trigger_master = NULL;
723}
724
725struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100726 int (*pre_action)(struct snd_pcm_substream *substream, int state);
727 int (*do_action)(struct snd_pcm_substream *substream, int state);
728 void (*undo_action)(struct snd_pcm_substream *substream, int state);
729 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730};
731
732/*
733 * this functions is core for handling of linked stream
734 * Note: the stream state might be changed also on failure
735 * Note2: call with calling stream lock + link lock
736 */
737static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100738 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int state, int do_lock)
740{
Takashi Iwai877211f2005-11-17 13:59:38 +0100741 struct snd_pcm_substream *s = NULL;
742 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int res = 0;
744
Takashi Iwaief991b92007-02-22 12:52:53 +0100745 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200747 spin_lock_nested(&s->self_group.lock,
748 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 res = ops->pre_action(s, state);
750 if (res < 0)
751 goto _unlock;
752 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100753 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 res = ops->do_action(s, state);
755 if (res < 0) {
756 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100757 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (s1 == s) /* failed stream */
759 break;
760 ops->undo_action(s1, state);
761 }
762 }
763 s = NULL; /* unlock all */
764 goto _unlock;
765 }
766 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100767 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ops->post_action(s, state);
769 }
770 _unlock:
771 if (do_lock) {
772 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100773 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (s1 != substream)
775 spin_unlock(&s1->self_group.lock);
776 if (s1 == s) /* end */
777 break;
778 }
779 }
780 return res;
781}
782
783/*
784 * Note: call with stream lock
785 */
786static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100787 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 int state)
789{
790 int res;
791
792 res = ops->pre_action(substream, state);
793 if (res < 0)
794 return res;
795 res = ops->do_action(substream, state);
796 if (res == 0)
797 ops->post_action(substream, state);
798 else if (ops->undo_action)
799 ops->undo_action(substream, state);
800 return res;
801}
802
803/*
804 * Note: call with stream lock
805 */
806static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100807 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int state)
809{
810 int res;
811
812 if (snd_pcm_stream_linked(substream)) {
813 if (!spin_trylock(&substream->group->lock)) {
814 spin_unlock(&substream->self_group.lock);
815 spin_lock(&substream->group->lock);
816 spin_lock(&substream->self_group.lock);
817 }
818 res = snd_pcm_action_group(ops, substream, state, 1);
819 spin_unlock(&substream->group->lock);
820 } else {
821 res = snd_pcm_action_single(ops, substream, state);
822 }
823 return res;
824}
825
826/*
827 * Note: don't use any locks before
828 */
829static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100830 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int state)
832{
833 int res;
834
835 read_lock_irq(&snd_pcm_link_rwlock);
836 if (snd_pcm_stream_linked(substream)) {
837 spin_lock(&substream->group->lock);
838 spin_lock(&substream->self_group.lock);
839 res = snd_pcm_action_group(ops, substream, state, 1);
840 spin_unlock(&substream->self_group.lock);
841 spin_unlock(&substream->group->lock);
842 } else {
843 spin_lock(&substream->self_group.lock);
844 res = snd_pcm_action_single(ops, substream, state);
845 spin_unlock(&substream->self_group.lock);
846 }
847 read_unlock_irq(&snd_pcm_link_rwlock);
848 return res;
849}
850
851/*
852 */
853static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100854 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 int state)
856{
857 int res;
858
859 down_read(&snd_pcm_link_rwsem);
860 if (snd_pcm_stream_linked(substream))
861 res = snd_pcm_action_group(ops, substream, state, 0);
862 else
863 res = snd_pcm_action_single(ops, substream, state);
864 up_read(&snd_pcm_link_rwsem);
865 return res;
866}
867
868/*
869 * start callbacks
870 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100871static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Takashi Iwai877211f2005-11-17 13:59:38 +0100873 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
875 return -EBADFD;
876 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
877 !snd_pcm_playback_data(substream))
878 return -EPIPE;
879 runtime->trigger_master = substream;
880 return 0;
881}
882
Takashi Iwai877211f2005-11-17 13:59:38 +0100883static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 if (substream->runtime->trigger_master != substream)
886 return 0;
887 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
888}
889
Takashi Iwai877211f2005-11-17 13:59:38 +0100890static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 if (substream->runtime->trigger_master == substream)
893 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
894}
895
Takashi Iwai877211f2005-11-17 13:59:38 +0100896static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai877211f2005-11-17 13:59:38 +0100898 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200900 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 runtime->status->state = state;
902 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
903 runtime->silence_size > 0)
904 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100906 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
907 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910static struct action_ops snd_pcm_action_start = {
911 .pre_action = snd_pcm_pre_start,
912 .do_action = snd_pcm_do_start,
913 .undo_action = snd_pcm_undo_start,
914 .post_action = snd_pcm_post_start
915};
916
917/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700918 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200919 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100921int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Takashi Iwai877211f2005-11-17 13:59:38 +0100923 return snd_pcm_action(&snd_pcm_action_start, substream,
924 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927/*
928 * stop callbacks
929 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100930static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Takashi Iwai877211f2005-11-17 13:59:38 +0100932 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
934 return -EBADFD;
935 runtime->trigger_master = substream;
936 return 0;
937}
938
Takashi Iwai877211f2005-11-17 13:59:38 +0100939static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 if (substream->runtime->trigger_master == substream &&
942 snd_pcm_running(substream))
943 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
944 return 0; /* unconditonally stop all substreams */
945}
946
Takashi Iwai877211f2005-11-17 13:59:38 +0100947static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (runtime->status->state != state) {
951 snd_pcm_trigger_tstamp(substream);
952 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100953 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
954 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100958 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961static struct action_ops snd_pcm_action_stop = {
962 .pre_action = snd_pcm_pre_stop,
963 .do_action = snd_pcm_do_stop,
964 .post_action = snd_pcm_post_stop
965};
966
967/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700968 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200969 * @substream: the PCM substream instance
970 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700972 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100974int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
977}
978
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200979EXPORT_SYMBOL(snd_pcm_stop);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700982 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200983 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700985 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Unlike snd_pcm_stop(), this affects only the given stream.
987 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100988int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Takashi Iwai877211f2005-11-17 13:59:38 +0100990 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
991 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994/*
995 * pause callbacks
996 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100997static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Takashi Iwai877211f2005-11-17 13:59:38 +0100999 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
1001 return -ENOSYS;
1002 if (push) {
1003 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
1004 return -EBADFD;
1005 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
1006 return -EBADFD;
1007 runtime->trigger_master = substream;
1008 return 0;
1009}
1010
Takashi Iwai877211f2005-11-17 13:59:38 +01001011static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 if (substream->runtime->trigger_master != substream)
1014 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +02001015 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
1016 * a delta betwen the current jiffies, this gives a large enough
1017 * delta, effectively to skip the check once.
1018 */
1019 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return substream->ops->trigger(substream,
1021 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
1022 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
1023}
1024
Takashi Iwai877211f2005-11-17 13:59:38 +01001025static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 if (substream->runtime->trigger_master == substream)
1028 substream->ops->trigger(substream,
1029 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1030 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1031}
1032
Takashi Iwai877211f2005-11-17 13:59:38 +01001033static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai877211f2005-11-17 13:59:38 +01001035 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 snd_pcm_trigger_tstamp(substream);
1037 if (push) {
1038 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1039 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001040 snd_timer_notify(substream->timer,
1041 SNDRV_TIMER_EVENT_MPAUSE,
1042 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001044 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 } else {
1046 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001048 snd_timer_notify(substream->timer,
1049 SNDRV_TIMER_EVENT_MCONTINUE,
1050 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052}
1053
1054static struct action_ops snd_pcm_action_pause = {
1055 .pre_action = snd_pcm_pre_pause,
1056 .do_action = snd_pcm_do_pause,
1057 .undo_action = snd_pcm_undo_pause,
1058 .post_action = snd_pcm_post_pause
1059};
1060
1061/*
1062 * Push/release the pause for all linked streams.
1063 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001064static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1067}
1068
1069#ifdef CONFIG_PM
1070/* suspend */
1071
Takashi Iwai877211f2005-11-17 13:59:38 +01001072static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Takashi Iwai877211f2005-11-17 13:59:38 +01001074 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1076 return -EBUSY;
1077 runtime->trigger_master = substream;
1078 return 0;
1079}
1080
Takashi Iwai877211f2005-11-17 13:59:38 +01001081static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Takashi Iwai877211f2005-11-17 13:59:38 +01001083 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (runtime->trigger_master != substream)
1085 return 0;
1086 if (! snd_pcm_running(substream))
1087 return 0;
1088 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1089 return 0; /* suspend unconditionally */
1090}
1091
Takashi Iwai877211f2005-11-17 13:59:38 +01001092static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Takashi Iwai877211f2005-11-17 13:59:38 +01001094 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 snd_pcm_trigger_tstamp(substream);
1096 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001097 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1098 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 runtime->status->suspended_state = runtime->status->state;
1100 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001102 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105static struct action_ops snd_pcm_action_suspend = {
1106 .pre_action = snd_pcm_pre_suspend,
1107 .do_action = snd_pcm_do_suspend,
1108 .post_action = snd_pcm_post_suspend
1109};
1110
1111/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001112 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001113 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * After this call, all streams are changed to SUSPENDED state.
1116 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001117int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 int err;
1120 unsigned long flags;
1121
Takashi Iwai603bf522005-11-17 15:59:14 +01001122 if (! substream)
1123 return 0;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 snd_pcm_stream_lock_irqsave(substream, flags);
1126 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1127 snd_pcm_stream_unlock_irqrestore(substream, flags);
1128 return err;
1129}
1130
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001131EXPORT_SYMBOL(snd_pcm_suspend);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001134 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001135 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 * After this call, all streams are changed to SUSPENDED state.
1138 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001139int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Takashi Iwai877211f2005-11-17 13:59:38 +01001141 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 int stream, err = 0;
1143
Takashi Iwai603bf522005-11-17 15:59:14 +01001144 if (! pcm)
1145 return 0;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 for (substream = pcm->streams[stream].substream;
1149 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* FIXME: the open/close code should lock this as well */
1151 if (substream->runtime == NULL)
1152 continue;
1153 err = snd_pcm_suspend(substream);
1154 if (err < 0 && err != -EBUSY)
1155 return err;
1156 }
1157 }
1158 return 0;
1159}
1160
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001161EXPORT_SYMBOL(snd_pcm_suspend_all);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/* resume */
1164
Takashi Iwai877211f2005-11-17 13:59:38 +01001165static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Takashi Iwai877211f2005-11-17 13:59:38 +01001167 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1169 return -ENOSYS;
1170 runtime->trigger_master = substream;
1171 return 0;
1172}
1173
Takashi Iwai877211f2005-11-17 13:59:38 +01001174static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Takashi Iwai877211f2005-11-17 13:59:38 +01001176 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (runtime->trigger_master != substream)
1178 return 0;
1179 /* DMA not running previously? */
1180 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1181 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1182 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1183 return 0;
1184 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1185}
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 if (substream->runtime->trigger_master == substream &&
1190 snd_pcm_running(substream))
1191 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1192}
1193
Takashi Iwai877211f2005-11-17 13:59:38 +01001194static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Takashi Iwai877211f2005-11-17 13:59:38 +01001196 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 snd_pcm_trigger_tstamp(substream);
1198 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001199 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1200 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
1204static struct action_ops snd_pcm_action_resume = {
1205 .pre_action = snd_pcm_pre_resume,
1206 .do_action = snd_pcm_do_resume,
1207 .undo_action = snd_pcm_undo_resume,
1208 .post_action = snd_pcm_post_resume
1209};
1210
Takashi Iwai877211f2005-11-17 13:59:38 +01001211static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Takashi Iwai877211f2005-11-17 13:59:38 +01001213 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int res;
1215
1216 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001217 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1219 snd_power_unlock(card);
1220 return res;
1221}
1222
1223#else
1224
Takashi Iwai877211f2005-11-17 13:59:38 +01001225static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 return -ENOSYS;
1228}
1229
1230#endif /* CONFIG_PM */
1231
1232/*
1233 * xrun ioctl
1234 *
1235 * Change the RUNNING stream(s) to XRUN state.
1236 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001237static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Takashi Iwai877211f2005-11-17 13:59:38 +01001239 struct snd_card *card = substream->pcm->card;
1240 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 int result;
1242
1243 snd_power_lock(card);
1244 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001245 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (result < 0)
1247 goto _unlock;
1248 }
1249
1250 snd_pcm_stream_lock_irq(substream);
1251 switch (runtime->status->state) {
1252 case SNDRV_PCM_STATE_XRUN:
1253 result = 0; /* already there */
1254 break;
1255 case SNDRV_PCM_STATE_RUNNING:
1256 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1257 break;
1258 default:
1259 result = -EBADFD;
1260 }
1261 snd_pcm_stream_unlock_irq(substream);
1262 _unlock:
1263 snd_power_unlock(card);
1264 return result;
1265}
1266
1267/*
1268 * reset ioctl
1269 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001270static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Takashi Iwai877211f2005-11-17 13:59:38 +01001272 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 switch (runtime->status->state) {
1274 case SNDRV_PCM_STATE_RUNNING:
1275 case SNDRV_PCM_STATE_PREPARED:
1276 case SNDRV_PCM_STATE_PAUSED:
1277 case SNDRV_PCM_STATE_SUSPENDED:
1278 return 0;
1279 default:
1280 return -EBADFD;
1281 }
1282}
1283
Takashi Iwai877211f2005-11-17 13:59:38 +01001284static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Takashi Iwai877211f2005-11-17 13:59:38 +01001286 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1288 if (err < 0)
1289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001291 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1292 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 runtime->silence_start = runtime->status->hw_ptr;
1294 runtime->silence_filled = 0;
1295 return 0;
1296}
1297
Takashi Iwai877211f2005-11-17 13:59:38 +01001298static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Takashi Iwai877211f2005-11-17 13:59:38 +01001300 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 runtime->control->appl_ptr = runtime->status->hw_ptr;
1302 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1303 runtime->silence_size > 0)
1304 snd_pcm_playback_silence(substream, ULONG_MAX);
1305}
1306
1307static struct action_ops snd_pcm_action_reset = {
1308 .pre_action = snd_pcm_pre_reset,
1309 .do_action = snd_pcm_do_reset,
1310 .post_action = snd_pcm_post_reset
1311};
1312
Takashi Iwai877211f2005-11-17 13:59:38 +01001313static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
1315 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1316}
1317
1318/*
1319 * prepare ioctl
1320 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001321/* we use the second argument for updating f_flags */
1322static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1323 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Takashi Iwai877211f2005-11-17 13:59:38 +01001325 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001326 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1327 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EBADFD;
1329 if (snd_pcm_running(substream))
1330 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001331 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return 0;
1333}
1334
Takashi Iwai877211f2005-11-17 13:59:38 +01001335static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int err;
1338 err = substream->ops->prepare(substream);
1339 if (err < 0)
1340 return err;
1341 return snd_pcm_do_reset(substream, 0);
1342}
1343
Takashi Iwai877211f2005-11-17 13:59:38 +01001344static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Takashi Iwai877211f2005-11-17 13:59:38 +01001346 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 runtime->control->appl_ptr = runtime->status->hw_ptr;
1348 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1349}
1350
1351static struct action_ops snd_pcm_action_prepare = {
1352 .pre_action = snd_pcm_pre_prepare,
1353 .do_action = snd_pcm_do_prepare,
1354 .post_action = snd_pcm_post_prepare
1355};
1356
1357/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001358 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001359 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001360 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001362static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1363 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
1365 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001366 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001367 int f_flags;
1368
1369 if (file)
1370 f_flags = file->f_flags;
1371 else
1372 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001375 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001376 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1377 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 snd_power_unlock(card);
1379 return res;
1380}
1381
1382/*
1383 * drain ioctl
1384 */
1385
Takashi Iwai877211f2005-11-17 13:59:38 +01001386static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 substream->runtime->trigger_master = substream;
1389 return 0;
1390}
1391
Takashi Iwai877211f2005-11-17 13:59:38 +01001392static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai877211f2005-11-17 13:59:38 +01001394 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1396 switch (runtime->status->state) {
1397 case SNDRV_PCM_STATE_PREPARED:
1398 /* start playback stream if possible */
1399 if (! snd_pcm_playback_empty(substream)) {
1400 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1401 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1402 }
1403 break;
1404 case SNDRV_PCM_STATE_RUNNING:
1405 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1406 break;
1407 default:
1408 break;
1409 }
1410 } else {
1411 /* stop running stream */
1412 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001413 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001415 snd_pcm_do_stop(substream, new_state);
1416 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 }
1419 return 0;
1420}
1421
Takashi Iwai877211f2005-11-17 13:59:38 +01001422static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424}
1425
1426static struct action_ops snd_pcm_action_drain_init = {
1427 .pre_action = snd_pcm_pre_drain_init,
1428 .do_action = snd_pcm_do_drain_init,
1429 .post_action = snd_pcm_post_drain_init
1430};
1431
Takashi Iwai877211f2005-11-17 13:59:38 +01001432static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434/*
1435 * Drain the stream(s).
1436 * When the substream is linked, sync until the draining of all playback streams
1437 * is finished.
1438 * After this call, all streams are supposed to be either SETUP or DRAINING
1439 * (capture only) state.
1440 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001441static int snd_pcm_drain(struct snd_pcm_substream *substream,
1442 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Takashi Iwai877211f2005-11-17 13:59:38 +01001444 struct snd_card *card;
1445 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001446 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001447 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001449 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 card = substream->pcm->card;
1452 runtime = substream->runtime;
1453
1454 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1455 return -EBADFD;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 snd_power_lock(card);
1458 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001459 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001460 if (result < 0) {
1461 snd_power_unlock(card);
1462 return result;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001466 if (file) {
1467 if (file->f_flags & O_NONBLOCK)
1468 nonblock = 1;
1469 } else if (substream->f_flags & O_NONBLOCK)
1470 nonblock = 1;
1471
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001472 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001473 snd_pcm_stream_lock_irq(substream);
1474 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001475 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001476 snd_pcm_pause(substream, 0);
1477
1478 /* pre-start/stop - all running streams are changed to DRAINING state */
1479 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001480 if (result < 0)
1481 goto unlock;
1482 /* in non-blocking, we don't wait in ioctl but let caller poll */
1483 if (nonblock) {
1484 result = -EAGAIN;
1485 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 for (;;) {
1489 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001490 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (signal_pending(current)) {
1492 result = -ERESTARTSYS;
1493 break;
1494 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001495 /* find a substream to drain */
1496 to_check = NULL;
1497 snd_pcm_group_for_each_entry(s, substream) {
1498 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1499 continue;
1500 runtime = s->runtime;
1501 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1502 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001503 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001504 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001505 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001506 if (!to_check)
1507 break; /* all drained */
1508 init_waitqueue_entry(&wait, current);
1509 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001512 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 snd_power_unlock(card);
1514 tout = schedule_timeout(10 * HZ);
1515 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001516 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001518 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (tout == 0) {
1520 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1521 result = -ESTRPIPE;
1522 else {
1523 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1524 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1525 result = -EIO;
1526 }
1527 break;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001530
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001531 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001532 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001533 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 return result;
1537}
1538
1539/*
1540 * drop ioctl
1541 *
1542 * Immediately put all linked substreams into SETUP state.
1543 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001544static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Takashi Iwai877211f2005-11-17 13:59:38 +01001546 struct snd_pcm_runtime *runtime;
1547 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int result = 0;
1549
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001550 if (PCM_RUNTIME_CHECK(substream))
1551 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 runtime = substream->runtime;
1553 card = substream->pcm->card;
1554
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001555 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001556 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1557 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -EBADFD;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 snd_pcm_stream_lock_irq(substream);
1561 /* resume pause */
1562 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1563 snd_pcm_pause(substream, 0);
1564
1565 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1566 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1567 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return result;
1570}
1571
1572
1573/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574static struct file *snd_pcm_file_fd(int fd)
1575{
1576 struct file *file;
1577 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001578 unsigned int minor;
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 file = fget(fd);
1581 if (!file)
1582 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001583 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (!S_ISCHR(inode->i_mode) ||
1585 imajor(inode) != snd_major) {
1586 fput(file);
1587 return NULL;
1588 }
1589 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001590 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1591 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 fput(file);
1593 return NULL;
1594 }
1595 return file;
1596}
1597
1598/*
1599 * PCM link handling
1600 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001601static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 int res = 0;
1604 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001605 struct snd_pcm_file *pcm_file;
1606 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 file = snd_pcm_file_fd(fd);
1609 if (!file)
1610 return -EBADFD;
1611 pcm_file = file->private_data;
1612 substream1 = pcm_file->substream;
1613 down_write(&snd_pcm_link_rwsem);
1614 write_lock_irq(&snd_pcm_link_rwlock);
1615 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1616 substream->runtime->status->state != substream1->runtime->status->state) {
1617 res = -EBADFD;
1618 goto _end;
1619 }
1620 if (snd_pcm_stream_linked(substream1)) {
1621 res = -EALREADY;
1622 goto _end;
1623 }
1624 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001625 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (substream->group == NULL) {
1627 res = -ENOMEM;
1628 goto _end;
1629 }
1630 spin_lock_init(&substream->group->lock);
1631 INIT_LIST_HEAD(&substream->group->substreams);
1632 list_add_tail(&substream->link_list, &substream->group->substreams);
1633 substream->group->count = 1;
1634 }
1635 list_add_tail(&substream1->link_list, &substream->group->substreams);
1636 substream->group->count++;
1637 substream1->group = substream->group;
1638 _end:
1639 write_unlock_irq(&snd_pcm_link_rwlock);
1640 up_write(&snd_pcm_link_rwsem);
1641 fput(file);
1642 return res;
1643}
1644
Takashi Iwai877211f2005-11-17 13:59:38 +01001645static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 substream->group = &substream->self_group;
1648 INIT_LIST_HEAD(&substream->self_group.substreams);
1649 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1650}
1651
Takashi Iwai877211f2005-11-17 13:59:38 +01001652static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Takashi Iwaief991b92007-02-22 12:52:53 +01001654 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 int res = 0;
1656
1657 down_write(&snd_pcm_link_rwsem);
1658 write_lock_irq(&snd_pcm_link_rwlock);
1659 if (!snd_pcm_stream_linked(substream)) {
1660 res = -EALREADY;
1661 goto _end;
1662 }
1663 list_del(&substream->link_list);
1664 substream->group->count--;
1665 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001666 snd_pcm_group_for_each_entry(s, substream) {
1667 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 break;
1669 }
1670 kfree(substream->group);
1671 }
1672 relink_to_local(substream);
1673 _end:
1674 write_unlock_irq(&snd_pcm_link_rwlock);
1675 up_write(&snd_pcm_link_rwsem);
1676 return res;
1677}
1678
1679/*
1680 * hw configurator
1681 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001682static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai877211f2005-11-17 13:59:38 +01001685 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1687 hw_param_interval_c(params, rule->deps[1]), &t);
1688 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1689}
1690
Takashi Iwai877211f2005-11-17 13:59:38 +01001691static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1692 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Takashi Iwai877211f2005-11-17 13:59:38 +01001694 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1696 hw_param_interval_c(params, rule->deps[1]), &t);
1697 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1698}
1699
Takashi Iwai877211f2005-11-17 13:59:38 +01001700static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1701 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Takashi Iwai877211f2005-11-17 13:59:38 +01001703 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1705 hw_param_interval_c(params, rule->deps[1]),
1706 (unsigned long) rule->private, &t);
1707 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1708}
1709
Takashi Iwai877211f2005-11-17 13:59:38 +01001710static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1711 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Takashi Iwai877211f2005-11-17 13:59:38 +01001713 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1715 (unsigned long) rule->private,
1716 hw_param_interval_c(params, rule->deps[1]), &t);
1717 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1718}
1719
Takashi Iwai877211f2005-11-17 13:59:38 +01001720static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1721 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
1723 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001724 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1725 struct snd_mask m;
1726 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 snd_mask_any(&m);
1728 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1729 int bits;
1730 if (! snd_mask_test(mask, k))
1731 continue;
1732 bits = snd_pcm_format_physical_width(k);
1733 if (bits <= 0)
1734 continue; /* ignore invalid formats */
1735 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1736 snd_mask_reset(&m, k);
1737 }
1738 return snd_mask_refine(mask, &m);
1739}
1740
Takashi Iwai877211f2005-11-17 13:59:38 +01001741static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1742 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwai877211f2005-11-17 13:59:38 +01001744 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 unsigned int k;
1746 t.min = UINT_MAX;
1747 t.max = 0;
1748 t.openmin = 0;
1749 t.openmax = 0;
1750 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1751 int bits;
1752 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1753 continue;
1754 bits = snd_pcm_format_physical_width(k);
1755 if (bits <= 0)
1756 continue; /* ignore invalid formats */
1757 if (t.min > (unsigned)bits)
1758 t.min = bits;
1759 if (t.max < (unsigned)bits)
1760 t.max = bits;
1761 }
1762 t.integer = 1;
1763 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1764}
1765
1766#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1767#error "Change this table"
1768#endif
1769
1770static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1771 48000, 64000, 88200, 96000, 176400, 192000 };
1772
Clemens Ladisch7653d552007-08-13 17:38:54 +02001773const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1774 .count = ARRAY_SIZE(rates),
1775 .list = rates,
1776};
1777
Takashi Iwai877211f2005-11-17 13:59:38 +01001778static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1779 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780{
Takashi Iwai877211f2005-11-17 13:59:38 +01001781 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001783 snd_pcm_known_rates.count,
1784 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Takashi Iwai877211f2005-11-17 13:59:38 +01001787static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1788 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Takashi Iwai877211f2005-11-17 13:59:38 +01001790 struct snd_interval t;
1791 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 t.min = 0;
1793 t.max = substream->buffer_bytes_max;
1794 t.openmin = 0;
1795 t.openmax = 0;
1796 t.integer = 1;
1797 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1798}
1799
Takashi Iwai877211f2005-11-17 13:59:38 +01001800int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Takashi Iwai877211f2005-11-17 13:59:38 +01001802 struct snd_pcm_runtime *runtime = substream->runtime;
1803 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 int k, err;
1805
1806 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1807 snd_mask_any(constrs_mask(constrs, k));
1808 }
1809
1810 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1811 snd_interval_any(constrs_interval(constrs, k));
1812 }
1813
1814 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1818 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1819
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1821 snd_pcm_hw_rule_format, NULL,
1822 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1823 if (err < 0)
1824 return err;
1825 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1826 snd_pcm_hw_rule_sample_bits, NULL,
1827 SNDRV_PCM_HW_PARAM_FORMAT,
1828 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1832 snd_pcm_hw_rule_div, NULL,
1833 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1837 snd_pcm_hw_rule_mul, NULL,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1842 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1843 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1848 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1852 snd_pcm_hw_rule_div, NULL,
1853 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1863 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1867 snd_pcm_hw_rule_div, NULL,
1868 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1872 snd_pcm_hw_rule_div, NULL,
1873 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1882 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1887 snd_pcm_hw_rule_mul, NULL,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1897 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1902 snd_pcm_hw_rule_muldivk, (void*) 8,
1903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1904 if (err < 0)
1905 return err;
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1907 snd_pcm_hw_rule_muldivk, (void*) 8,
1908 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1909 if (err < 0)
1910 return err;
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1912 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1913 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1914 if (err < 0)
1915 return err;
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1917 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1918 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1919 if (err < 0)
1920 return err;
1921 return 0;
1922}
1923
Takashi Iwai877211f2005-11-17 13:59:38 +01001924int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Takashi Iwai877211f2005-11-17 13:59:38 +01001926 struct snd_pcm_runtime *runtime = substream->runtime;
1927 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int err;
1929 unsigned int mask = 0;
1930
1931 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1932 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1933 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1934 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1935 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1936 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1937 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1938 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1939 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1940 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1941 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1942 }
1943 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001944 if (err < 0)
1945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001948 if (err < 0)
1949 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 if (err < 0)
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1956 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001957 if (err < 0)
1958 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1961 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001962 if (err < 0)
1963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1966 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001967 if (err < 0)
1968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1971 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001972 if (err < 0)
1973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1976 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001977 if (err < 0)
1978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1981 snd_pcm_hw_rule_buffer_bytes_max, substream,
1982 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1983 if (err < 0)
1984 return err;
1985
1986 /* FIXME: remove */
1987 if (runtime->dma_bytes) {
1988 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001989 if (err < 0)
1990 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 }
1992
1993 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1994 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1995 snd_pcm_hw_rule_rate, hw,
1996 SNDRV_PCM_HW_PARAM_RATE, -1);
1997 if (err < 0)
1998 return err;
1999 }
2000
2001 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2003
2004 return 0;
2005}
2006
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002007static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002010}
2011
2012void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2013{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002014 substream->ref_count--;
2015 if (substream->ref_count > 0)
2016 return;
2017
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002018 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (substream->ops->hw_free != NULL)
2021 substream->ops->hw_free(substream);
2022 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002023 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Takashi Iwai15762742006-04-06 19:47:42 +02002025 if (substream->pcm_release) {
2026 substream->pcm_release(substream);
2027 substream->pcm_release = NULL;
2028 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002029 snd_pcm_detach_substream(substream);
2030}
2031
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002032EXPORT_SYMBOL(snd_pcm_release_substream);
2033
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2035 struct file *file,
2036 struct snd_pcm_substream **rsubstream)
2037{
2038 struct snd_pcm_substream *substream;
2039 int err;
2040
2041 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2042 if (err < 0)
2043 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002044 if (substream->ref_count > 1) {
2045 *rsubstream = substream;
2046 return 0;
2047 }
2048
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002049 err = snd_pcm_hw_constraints_init(substream);
2050 if (err < 0) {
2051 snd_printd("snd_pcm_hw_constraints_init failed\n");
2052 goto error;
2053 }
2054
2055 if ((err = substream->ops->open(substream)) < 0)
2056 goto error;
2057
2058 substream->hw_opened = 1;
2059
2060 err = snd_pcm_hw_constraints_complete(substream);
2061 if (err < 0) {
2062 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2063 goto error;
2064 }
2065
2066 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002068
2069 error:
2070 snd_pcm_release_substream(substream);
2071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002074EXPORT_SYMBOL(snd_pcm_open_substream);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002077 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002079 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Takashi Iwai877211f2005-11-17 13:59:38 +01002081 struct snd_pcm_file *pcm_file;
2082 struct snd_pcm_substream *substream;
2083 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002084 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002086 if (rpcm_file)
2087 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002089 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2090 if (err < 0)
2091 return err;
2092
Takashi Iwai548a6482006-07-31 16:51:51 +02002093 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2094 if (pcm_file == NULL) {
2095 snd_pcm_release_substream(substream);
2096 return -ENOMEM;
2097 }
2098 pcm_file->substream = substream;
2099 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002100 str = substream->pstr;
2101 substream->file = pcm_file;
2102 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002105 if (rpcm_file)
2106 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 0;
2108}
2109
Clemens Ladischf87135f2005-11-20 14:06:59 +01002110static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Takashi Iwai877211f2005-11-17 13:59:38 +01002112 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002113
2114 pcm = snd_lookup_minor_data(iminor(inode),
2115 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2116 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2117}
2118
2119static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2120{
2121 struct snd_pcm *pcm;
2122
2123 pcm = snd_lookup_minor_data(iminor(inode),
2124 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2125 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2126}
2127
2128static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2129{
2130 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002131 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 wait_queue_t wait;
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (pcm == NULL) {
2135 err = -ENODEV;
2136 goto __error1;
2137 }
2138 err = snd_card_file_add(pcm->card, file);
2139 if (err < 0)
2140 goto __error1;
2141 if (!try_module_get(pcm->card->module)) {
2142 err = -EFAULT;
2143 goto __error2;
2144 }
2145 init_waitqueue_entry(&wait, current);
2146 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002147 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002149 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (err >= 0)
2151 break;
2152 if (err == -EAGAIN) {
2153 if (file->f_flags & O_NONBLOCK) {
2154 err = -EBUSY;
2155 break;
2156 }
2157 } else
2158 break;
2159 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002160 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002162 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (signal_pending(current)) {
2164 err = -ERESTARTSYS;
2165 break;
2166 }
2167 }
2168 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002169 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (err < 0)
2171 goto __error;
2172 return err;
2173
2174 __error:
2175 module_put(pcm->card->module);
2176 __error2:
2177 snd_card_file_remove(pcm->card, file);
2178 __error1:
2179 return err;
2180}
2181
2182static int snd_pcm_release(struct inode *inode, struct file *file)
2183{
Takashi Iwai877211f2005-11-17 13:59:38 +01002184 struct snd_pcm *pcm;
2185 struct snd_pcm_substream *substream;
2186 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188 pcm_file = file->private_data;
2189 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002190 if (snd_BUG_ON(!substream))
2191 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002193 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002194 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002195 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002196 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 wake_up(&pcm->open_wait);
2198 module_put(pcm->card->module);
2199 snd_card_file_remove(pcm->card, file);
2200 return 0;
2201}
2202
Takashi Iwai877211f2005-11-17 13:59:38 +01002203static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2204 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Takashi Iwai877211f2005-11-17 13:59:38 +01002206 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 snd_pcm_sframes_t appl_ptr;
2208 snd_pcm_sframes_t ret;
2209 snd_pcm_sframes_t hw_avail;
2210
2211 if (frames == 0)
2212 return 0;
2213
2214 snd_pcm_stream_lock_irq(substream);
2215 switch (runtime->status->state) {
2216 case SNDRV_PCM_STATE_PREPARED:
2217 break;
2218 case SNDRV_PCM_STATE_DRAINING:
2219 case SNDRV_PCM_STATE_RUNNING:
2220 if (snd_pcm_update_hw_ptr(substream) >= 0)
2221 break;
2222 /* Fall through */
2223 case SNDRV_PCM_STATE_XRUN:
2224 ret = -EPIPE;
2225 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002226 case SNDRV_PCM_STATE_SUSPENDED:
2227 ret = -ESTRPIPE;
2228 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 default:
2230 ret = -EBADFD;
2231 goto __end;
2232 }
2233
2234 hw_avail = snd_pcm_playback_hw_avail(runtime);
2235 if (hw_avail <= 0) {
2236 ret = 0;
2237 goto __end;
2238 }
2239 if (frames > (snd_pcm_uframes_t)hw_avail)
2240 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 appl_ptr = runtime->control->appl_ptr - frames;
2242 if (appl_ptr < 0)
2243 appl_ptr += runtime->boundary;
2244 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 ret = frames;
2246 __end:
2247 snd_pcm_stream_unlock_irq(substream);
2248 return ret;
2249}
2250
Takashi Iwai877211f2005-11-17 13:59:38 +01002251static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2252 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Takashi Iwai877211f2005-11-17 13:59:38 +01002254 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 snd_pcm_sframes_t appl_ptr;
2256 snd_pcm_sframes_t ret;
2257 snd_pcm_sframes_t hw_avail;
2258
2259 if (frames == 0)
2260 return 0;
2261
2262 snd_pcm_stream_lock_irq(substream);
2263 switch (runtime->status->state) {
2264 case SNDRV_PCM_STATE_PREPARED:
2265 case SNDRV_PCM_STATE_DRAINING:
2266 break;
2267 case SNDRV_PCM_STATE_RUNNING:
2268 if (snd_pcm_update_hw_ptr(substream) >= 0)
2269 break;
2270 /* Fall through */
2271 case SNDRV_PCM_STATE_XRUN:
2272 ret = -EPIPE;
2273 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002274 case SNDRV_PCM_STATE_SUSPENDED:
2275 ret = -ESTRPIPE;
2276 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 default:
2278 ret = -EBADFD;
2279 goto __end;
2280 }
2281
2282 hw_avail = snd_pcm_capture_hw_avail(runtime);
2283 if (hw_avail <= 0) {
2284 ret = 0;
2285 goto __end;
2286 }
2287 if (frames > (snd_pcm_uframes_t)hw_avail)
2288 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 appl_ptr = runtime->control->appl_ptr - frames;
2290 if (appl_ptr < 0)
2291 appl_ptr += runtime->boundary;
2292 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 ret = frames;
2294 __end:
2295 snd_pcm_stream_unlock_irq(substream);
2296 return ret;
2297}
2298
Takashi Iwai877211f2005-11-17 13:59:38 +01002299static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2300 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301{
Takashi Iwai877211f2005-11-17 13:59:38 +01002302 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 snd_pcm_sframes_t appl_ptr;
2304 snd_pcm_sframes_t ret;
2305 snd_pcm_sframes_t avail;
2306
2307 if (frames == 0)
2308 return 0;
2309
2310 snd_pcm_stream_lock_irq(substream);
2311 switch (runtime->status->state) {
2312 case SNDRV_PCM_STATE_PREPARED:
2313 case SNDRV_PCM_STATE_PAUSED:
2314 break;
2315 case SNDRV_PCM_STATE_DRAINING:
2316 case SNDRV_PCM_STATE_RUNNING:
2317 if (snd_pcm_update_hw_ptr(substream) >= 0)
2318 break;
2319 /* Fall through */
2320 case SNDRV_PCM_STATE_XRUN:
2321 ret = -EPIPE;
2322 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002323 case SNDRV_PCM_STATE_SUSPENDED:
2324 ret = -ESTRPIPE;
2325 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 default:
2327 ret = -EBADFD;
2328 goto __end;
2329 }
2330
2331 avail = snd_pcm_playback_avail(runtime);
2332 if (avail <= 0) {
2333 ret = 0;
2334 goto __end;
2335 }
2336 if (frames > (snd_pcm_uframes_t)avail)
2337 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 appl_ptr = runtime->control->appl_ptr + frames;
2339 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2340 appl_ptr -= runtime->boundary;
2341 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 ret = frames;
2343 __end:
2344 snd_pcm_stream_unlock_irq(substream);
2345 return ret;
2346}
2347
Takashi Iwai877211f2005-11-17 13:59:38 +01002348static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2349 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
Takashi Iwai877211f2005-11-17 13:59:38 +01002351 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 snd_pcm_sframes_t appl_ptr;
2353 snd_pcm_sframes_t ret;
2354 snd_pcm_sframes_t avail;
2355
2356 if (frames == 0)
2357 return 0;
2358
2359 snd_pcm_stream_lock_irq(substream);
2360 switch (runtime->status->state) {
2361 case SNDRV_PCM_STATE_PREPARED:
2362 case SNDRV_PCM_STATE_DRAINING:
2363 case SNDRV_PCM_STATE_PAUSED:
2364 break;
2365 case SNDRV_PCM_STATE_RUNNING:
2366 if (snd_pcm_update_hw_ptr(substream) >= 0)
2367 break;
2368 /* Fall through */
2369 case SNDRV_PCM_STATE_XRUN:
2370 ret = -EPIPE;
2371 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002372 case SNDRV_PCM_STATE_SUSPENDED:
2373 ret = -ESTRPIPE;
2374 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 default:
2376 ret = -EBADFD;
2377 goto __end;
2378 }
2379
2380 avail = snd_pcm_capture_avail(runtime);
2381 if (avail <= 0) {
2382 ret = 0;
2383 goto __end;
2384 }
2385 if (frames > (snd_pcm_uframes_t)avail)
2386 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 appl_ptr = runtime->control->appl_ptr + frames;
2388 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2389 appl_ptr -= runtime->boundary;
2390 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 ret = frames;
2392 __end:
2393 snd_pcm_stream_unlock_irq(substream);
2394 return ret;
2395}
2396
Takashi Iwai877211f2005-11-17 13:59:38 +01002397static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Takashi Iwai877211f2005-11-17 13:59:38 +01002399 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 int err;
2401
2402 snd_pcm_stream_lock_irq(substream);
2403 switch (runtime->status->state) {
2404 case SNDRV_PCM_STATE_DRAINING:
2405 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2406 goto __badfd;
2407 case SNDRV_PCM_STATE_RUNNING:
2408 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2409 break;
2410 /* Fall through */
2411 case SNDRV_PCM_STATE_PREPARED:
2412 case SNDRV_PCM_STATE_SUSPENDED:
2413 err = 0;
2414 break;
2415 case SNDRV_PCM_STATE_XRUN:
2416 err = -EPIPE;
2417 break;
2418 default:
2419 __badfd:
2420 err = -EBADFD;
2421 break;
2422 }
2423 snd_pcm_stream_unlock_irq(substream);
2424 return err;
2425}
2426
Takashi Iwai877211f2005-11-17 13:59:38 +01002427static int snd_pcm_delay(struct snd_pcm_substream *substream,
2428 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Takashi Iwai877211f2005-11-17 13:59:38 +01002430 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 int err;
2432 snd_pcm_sframes_t n = 0;
2433
2434 snd_pcm_stream_lock_irq(substream);
2435 switch (runtime->status->state) {
2436 case SNDRV_PCM_STATE_DRAINING:
2437 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2438 goto __badfd;
2439 case SNDRV_PCM_STATE_RUNNING:
2440 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2441 break;
2442 /* Fall through */
2443 case SNDRV_PCM_STATE_PREPARED:
2444 case SNDRV_PCM_STATE_SUSPENDED:
2445 err = 0;
2446 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2447 n = snd_pcm_playback_hw_avail(runtime);
2448 else
2449 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002450 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 break;
2452 case SNDRV_PCM_STATE_XRUN:
2453 err = -EPIPE;
2454 break;
2455 default:
2456 __badfd:
2457 err = -EBADFD;
2458 break;
2459 }
2460 snd_pcm_stream_unlock_irq(substream);
2461 if (!err)
2462 if (put_user(n, res))
2463 err = -EFAULT;
2464 return err;
2465}
2466
Takashi Iwai877211f2005-11-17 13:59:38 +01002467static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2468 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Takashi Iwai877211f2005-11-17 13:59:38 +01002470 struct snd_pcm_runtime *runtime = substream->runtime;
2471 struct snd_pcm_sync_ptr sync_ptr;
2472 volatile struct snd_pcm_mmap_status *status;
2473 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int err;
2475
2476 memset(&sync_ptr, 0, sizeof(sync_ptr));
2477 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2478 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002479 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 -07002480 return -EFAULT;
2481 status = runtime->status;
2482 control = runtime->control;
2483 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2484 err = snd_pcm_hwsync(substream);
2485 if (err < 0)
2486 return err;
2487 }
2488 snd_pcm_stream_lock_irq(substream);
2489 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2490 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2491 else
2492 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2493 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2494 control->avail_min = sync_ptr.c.control.avail_min;
2495 else
2496 sync_ptr.c.control.avail_min = control->avail_min;
2497 sync_ptr.s.status.state = status->state;
2498 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2499 sync_ptr.s.status.tstamp = status->tstamp;
2500 sync_ptr.s.status.suspended_state = status->suspended_state;
2501 snd_pcm_stream_unlock_irq(substream);
2502 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2503 return -EFAULT;
2504 return 0;
2505}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002506
2507static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2508{
2509 struct snd_pcm_runtime *runtime = substream->runtime;
2510 int arg;
2511
2512 if (get_user(arg, _arg))
2513 return -EFAULT;
2514 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2515 return -EINVAL;
2516 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2517 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2518 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2519 return 0;
2520}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Takashi Iwai0df63e42006-04-28 15:13:41 +02002522static int snd_pcm_common_ioctl1(struct file *file,
2523 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 unsigned int cmd, void __user *arg)
2525{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 switch (cmd) {
2527 case SNDRV_PCM_IOCTL_PVERSION:
2528 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2529 case SNDRV_PCM_IOCTL_INFO:
2530 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002531 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2532 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002533 case SNDRV_PCM_IOCTL_TTSTAMP:
2534 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 case SNDRV_PCM_IOCTL_HW_REFINE:
2536 return snd_pcm_hw_refine_user(substream, arg);
2537 case SNDRV_PCM_IOCTL_HW_PARAMS:
2538 return snd_pcm_hw_params_user(substream, arg);
2539 case SNDRV_PCM_IOCTL_HW_FREE:
2540 return snd_pcm_hw_free(substream);
2541 case SNDRV_PCM_IOCTL_SW_PARAMS:
2542 return snd_pcm_sw_params_user(substream, arg);
2543 case SNDRV_PCM_IOCTL_STATUS:
2544 return snd_pcm_status_user(substream, arg);
2545 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2546 return snd_pcm_channel_info_user(substream, arg);
2547 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002548 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 case SNDRV_PCM_IOCTL_RESET:
2550 return snd_pcm_reset(substream);
2551 case SNDRV_PCM_IOCTL_START:
2552 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2553 case SNDRV_PCM_IOCTL_LINK:
2554 return snd_pcm_link(substream, (int)(unsigned long) arg);
2555 case SNDRV_PCM_IOCTL_UNLINK:
2556 return snd_pcm_unlink(substream);
2557 case SNDRV_PCM_IOCTL_RESUME:
2558 return snd_pcm_resume(substream);
2559 case SNDRV_PCM_IOCTL_XRUN:
2560 return snd_pcm_xrun(substream);
2561 case SNDRV_PCM_IOCTL_HWSYNC:
2562 return snd_pcm_hwsync(substream);
2563 case SNDRV_PCM_IOCTL_DELAY:
2564 return snd_pcm_delay(substream, arg);
2565 case SNDRV_PCM_IOCTL_SYNC_PTR:
2566 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002567#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2569 return snd_pcm_hw_refine_old_user(substream, arg);
2570 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2571 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002572#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002574 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 case SNDRV_PCM_IOCTL_DROP:
2576 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002577 case SNDRV_PCM_IOCTL_PAUSE:
2578 {
2579 int res;
2580 snd_pcm_stream_lock_irq(substream);
2581 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2582 snd_pcm_stream_unlock_irq(substream);
2583 return res;
2584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586 snd_printd("unknown ioctl = 0x%x\n", cmd);
2587 return -ENOTTY;
2588}
2589
Takashi Iwai0df63e42006-04-28 15:13:41 +02002590static int snd_pcm_playback_ioctl1(struct file *file,
2591 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 unsigned int cmd, void __user *arg)
2593{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002594 if (snd_BUG_ON(!substream))
2595 return -ENXIO;
2596 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2597 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 switch (cmd) {
2599 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2600 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002601 struct snd_xferi xferi;
2602 struct snd_xferi __user *_xferi = arg;
2603 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 snd_pcm_sframes_t result;
2605 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2606 return -EBADFD;
2607 if (put_user(0, &_xferi->result))
2608 return -EFAULT;
2609 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2610 return -EFAULT;
2611 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2612 __put_user(result, &_xferi->result);
2613 return result < 0 ? result : 0;
2614 }
2615 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2616 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002617 struct snd_xfern xfern;
2618 struct snd_xfern __user *_xfern = arg;
2619 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 void __user **bufs;
2621 snd_pcm_sframes_t result;
2622 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2623 return -EBADFD;
2624 if (runtime->channels > 128)
2625 return -EINVAL;
2626 if (put_user(0, &_xfern->result))
2627 return -EFAULT;
2628 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2629 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002630
2631 bufs = memdup_user(xfern.bufs,
2632 sizeof(void *) * runtime->channels);
2633 if (IS_ERR(bufs))
2634 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2636 kfree(bufs);
2637 __put_user(result, &_xfern->result);
2638 return result < 0 ? result : 0;
2639 }
2640 case SNDRV_PCM_IOCTL_REWIND:
2641 {
2642 snd_pcm_uframes_t frames;
2643 snd_pcm_uframes_t __user *_frames = arg;
2644 snd_pcm_sframes_t result;
2645 if (get_user(frames, _frames))
2646 return -EFAULT;
2647 if (put_user(0, _frames))
2648 return -EFAULT;
2649 result = snd_pcm_playback_rewind(substream, frames);
2650 __put_user(result, _frames);
2651 return result < 0 ? result : 0;
2652 }
2653 case SNDRV_PCM_IOCTL_FORWARD:
2654 {
2655 snd_pcm_uframes_t frames;
2656 snd_pcm_uframes_t __user *_frames = arg;
2657 snd_pcm_sframes_t result;
2658 if (get_user(frames, _frames))
2659 return -EFAULT;
2660 if (put_user(0, _frames))
2661 return -EFAULT;
2662 result = snd_pcm_playback_forward(substream, frames);
2663 __put_user(result, _frames);
2664 return result < 0 ? result : 0;
2665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002667 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
Takashi Iwai0df63e42006-04-28 15:13:41 +02002670static int snd_pcm_capture_ioctl1(struct file *file,
2671 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 unsigned int cmd, void __user *arg)
2673{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002674 if (snd_BUG_ON(!substream))
2675 return -ENXIO;
2676 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2677 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 switch (cmd) {
2679 case SNDRV_PCM_IOCTL_READI_FRAMES:
2680 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002681 struct snd_xferi xferi;
2682 struct snd_xferi __user *_xferi = arg;
2683 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 snd_pcm_sframes_t result;
2685 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2686 return -EBADFD;
2687 if (put_user(0, &_xferi->result))
2688 return -EFAULT;
2689 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2690 return -EFAULT;
2691 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2692 __put_user(result, &_xferi->result);
2693 return result < 0 ? result : 0;
2694 }
2695 case SNDRV_PCM_IOCTL_READN_FRAMES:
2696 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002697 struct snd_xfern xfern;
2698 struct snd_xfern __user *_xfern = arg;
2699 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 void *bufs;
2701 snd_pcm_sframes_t result;
2702 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2703 return -EBADFD;
2704 if (runtime->channels > 128)
2705 return -EINVAL;
2706 if (put_user(0, &_xfern->result))
2707 return -EFAULT;
2708 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2709 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002710
2711 bufs = memdup_user(xfern.bufs,
2712 sizeof(void *) * runtime->channels);
2713 if (IS_ERR(bufs))
2714 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2716 kfree(bufs);
2717 __put_user(result, &_xfern->result);
2718 return result < 0 ? result : 0;
2719 }
2720 case SNDRV_PCM_IOCTL_REWIND:
2721 {
2722 snd_pcm_uframes_t frames;
2723 snd_pcm_uframes_t __user *_frames = arg;
2724 snd_pcm_sframes_t result;
2725 if (get_user(frames, _frames))
2726 return -EFAULT;
2727 if (put_user(0, _frames))
2728 return -EFAULT;
2729 result = snd_pcm_capture_rewind(substream, frames);
2730 __put_user(result, _frames);
2731 return result < 0 ? result : 0;
2732 }
2733 case SNDRV_PCM_IOCTL_FORWARD:
2734 {
2735 snd_pcm_uframes_t frames;
2736 snd_pcm_uframes_t __user *_frames = arg;
2737 snd_pcm_sframes_t result;
2738 if (get_user(frames, _frames))
2739 return -EFAULT;
2740 if (put_user(0, _frames))
2741 return -EFAULT;
2742 result = snd_pcm_capture_forward(substream, frames);
2743 __put_user(result, _frames);
2744 return result < 0 ? result : 0;
2745 }
2746 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002747 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Takashi Iwai877211f2005-11-17 13:59:38 +01002750static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2751 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Takashi Iwai877211f2005-11-17 13:59:38 +01002753 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 pcm_file = file->private_data;
2756
2757 if (((cmd >> 8) & 0xff) != 'A')
2758 return -ENOTTY;
2759
Takashi Iwai0df63e42006-04-28 15:13:41 +02002760 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2761 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Takashi Iwai877211f2005-11-17 13:59:38 +01002764static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2765 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Takashi Iwai877211f2005-11-17 13:59:38 +01002767 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 pcm_file = file->private_data;
2770
2771 if (((cmd >> 8) & 0xff) != 'A')
2772 return -ENOTTY;
2773
Takashi Iwai0df63e42006-04-28 15:13:41 +02002774 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2775 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
Takashi Iwai877211f2005-11-17 13:59:38 +01002778int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 unsigned int cmd, void *arg)
2780{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002781 mm_segment_t fs;
2782 int result;
2783
2784 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 switch (substream->stream) {
2786 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002787 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2788 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002791 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2792 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002795 result = -EINVAL;
2796 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002798 snd_leave_user(fs);
2799 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002802EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2803
Takashi Iwai877211f2005-11-17 13:59:38 +01002804static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2805 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Takashi Iwai877211f2005-11-17 13:59:38 +01002807 struct snd_pcm_file *pcm_file;
2808 struct snd_pcm_substream *substream;
2809 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 snd_pcm_sframes_t result;
2811
2812 pcm_file = file->private_data;
2813 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002814 if (PCM_RUNTIME_CHECK(substream))
2815 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 runtime = substream->runtime;
2817 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2818 return -EBADFD;
2819 if (!frame_aligned(runtime, count))
2820 return -EINVAL;
2821 count = bytes_to_frames(runtime, count);
2822 result = snd_pcm_lib_read(substream, buf, count);
2823 if (result > 0)
2824 result = frames_to_bytes(runtime, result);
2825 return result;
2826}
2827
Takashi Iwai877211f2005-11-17 13:59:38 +01002828static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2829 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830{
Takashi Iwai877211f2005-11-17 13:59:38 +01002831 struct snd_pcm_file *pcm_file;
2832 struct snd_pcm_substream *substream;
2833 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 snd_pcm_sframes_t result;
2835
2836 pcm_file = file->private_data;
2837 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002838 if (PCM_RUNTIME_CHECK(substream))
2839 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002841 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2842 return -EBADFD;
2843 if (!frame_aligned(runtime, count))
2844 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 count = bytes_to_frames(runtime, count);
2846 result = snd_pcm_lib_write(substream, buf, count);
2847 if (result > 0)
2848 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return result;
2850}
2851
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002852static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2853 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855{
Takashi Iwai877211f2005-11-17 13:59:38 +01002856 struct snd_pcm_file *pcm_file;
2857 struct snd_pcm_substream *substream;
2858 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 snd_pcm_sframes_t result;
2860 unsigned long i;
2861 void __user **bufs;
2862 snd_pcm_uframes_t frames;
2863
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002864 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002866 if (PCM_RUNTIME_CHECK(substream))
2867 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 runtime = substream->runtime;
2869 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2870 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002871 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002873 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002875 frames = bytes_to_samples(runtime, iov->iov_len);
2876 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (bufs == NULL)
2878 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002879 for (i = 0; i < nr_segs; ++i)
2880 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 result = snd_pcm_lib_readv(substream, bufs, frames);
2882 if (result > 0)
2883 result = frames_to_bytes(runtime, result);
2884 kfree(bufs);
2885 return result;
2886}
2887
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002888static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2889 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Takashi Iwai877211f2005-11-17 13:59:38 +01002891 struct snd_pcm_file *pcm_file;
2892 struct snd_pcm_substream *substream;
2893 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 snd_pcm_sframes_t result;
2895 unsigned long i;
2896 void __user **bufs;
2897 snd_pcm_uframes_t frames;
2898
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002899 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002901 if (PCM_RUNTIME_CHECK(substream))
2902 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2905 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002906 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002907 !frame_aligned(runtime, iov->iov_len))
2908 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002909 frames = bytes_to_samples(runtime, iov->iov_len);
2910 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (bufs == NULL)
2912 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002913 for (i = 0; i < nr_segs; ++i)
2914 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 result = snd_pcm_lib_writev(substream, bufs, frames);
2916 if (result > 0)
2917 result = frames_to_bytes(runtime, result);
2918 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return result;
2920}
2921
2922static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2923{
Takashi Iwai877211f2005-11-17 13:59:38 +01002924 struct snd_pcm_file *pcm_file;
2925 struct snd_pcm_substream *substream;
2926 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 unsigned int mask;
2928 snd_pcm_uframes_t avail;
2929
2930 pcm_file = file->private_data;
2931
2932 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002933 if (PCM_RUNTIME_CHECK(substream))
2934 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 runtime = substream->runtime;
2936
2937 poll_wait(file, &runtime->sleep, wait);
2938
2939 snd_pcm_stream_lock_irq(substream);
2940 avail = snd_pcm_playback_avail(runtime);
2941 switch (runtime->status->state) {
2942 case SNDRV_PCM_STATE_RUNNING:
2943 case SNDRV_PCM_STATE_PREPARED:
2944 case SNDRV_PCM_STATE_PAUSED:
2945 if (avail >= runtime->control->avail_min) {
2946 mask = POLLOUT | POLLWRNORM;
2947 break;
2948 }
2949 /* Fall through */
2950 case SNDRV_PCM_STATE_DRAINING:
2951 mask = 0;
2952 break;
2953 default:
2954 mask = POLLOUT | POLLWRNORM | POLLERR;
2955 break;
2956 }
2957 snd_pcm_stream_unlock_irq(substream);
2958 return mask;
2959}
2960
2961static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2962{
Takashi Iwai877211f2005-11-17 13:59:38 +01002963 struct snd_pcm_file *pcm_file;
2964 struct snd_pcm_substream *substream;
2965 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 unsigned int mask;
2967 snd_pcm_uframes_t avail;
2968
2969 pcm_file = file->private_data;
2970
2971 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002972 if (PCM_RUNTIME_CHECK(substream))
2973 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 runtime = substream->runtime;
2975
2976 poll_wait(file, &runtime->sleep, wait);
2977
2978 snd_pcm_stream_lock_irq(substream);
2979 avail = snd_pcm_capture_avail(runtime);
2980 switch (runtime->status->state) {
2981 case SNDRV_PCM_STATE_RUNNING:
2982 case SNDRV_PCM_STATE_PREPARED:
2983 case SNDRV_PCM_STATE_PAUSED:
2984 if (avail >= runtime->control->avail_min) {
2985 mask = POLLIN | POLLRDNORM;
2986 break;
2987 }
2988 mask = 0;
2989 break;
2990 case SNDRV_PCM_STATE_DRAINING:
2991 if (avail > 0) {
2992 mask = POLLIN | POLLRDNORM;
2993 break;
2994 }
2995 /* Fall through */
2996 default:
2997 mask = POLLIN | POLLRDNORM | POLLERR;
2998 break;
2999 }
3000 snd_pcm_stream_unlock_irq(substream);
3001 return mask;
3002}
3003
3004/*
3005 * mmap support
3006 */
3007
3008/*
3009 * Only on coherent architectures, we can mmap the status and the control records
3010 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3011 */
3012#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3013/*
3014 * mmap status record
3015 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003016static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3017 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Takashi Iwai877211f2005-11-17 13:59:38 +01003019 struct snd_pcm_substream *substream = area->vm_private_data;
3020 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003023 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003025 vmf->page = virt_to_page(runtime->status);
3026 get_page(vmf->page);
3027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003030static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003032 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033};
3034
Takashi Iwai877211f2005-11-17 13:59:38 +01003035static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 struct vm_area_struct *area)
3037{
Takashi Iwai877211f2005-11-17 13:59:38 +01003038 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 long size;
3040 if (!(area->vm_flags & VM_READ))
3041 return -EINVAL;
3042 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003044 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return -EINVAL;
3046 area->vm_ops = &snd_pcm_vm_ops_status;
3047 area->vm_private_data = substream;
3048 area->vm_flags |= VM_RESERVED;
3049 return 0;
3050}
3051
3052/*
3053 * mmap control record
3054 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003055static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3056 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Takashi Iwai877211f2005-11-17 13:59:38 +01003058 struct snd_pcm_substream *substream = area->vm_private_data;
3059 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003062 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003064 vmf->page = virt_to_page(runtime->control);
3065 get_page(vmf->page);
3066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003069static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003071 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072};
3073
Takashi Iwai877211f2005-11-17 13:59:38 +01003074static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct vm_area_struct *area)
3076{
Takashi Iwai877211f2005-11-17 13:59:38 +01003077 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 long size;
3079 if (!(area->vm_flags & VM_READ))
3080 return -EINVAL;
3081 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003083 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return -EINVAL;
3085 area->vm_ops = &snd_pcm_vm_ops_control;
3086 area->vm_private_data = substream;
3087 area->vm_flags |= VM_RESERVED;
3088 return 0;
3089}
3090#else /* ! coherent mmap */
3091/*
3092 * don't support mmap for status and control records.
3093 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003094static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 struct vm_area_struct *area)
3096{
3097 return -ENXIO;
3098}
Takashi Iwai877211f2005-11-17 13:59:38 +01003099static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct vm_area_struct *area)
3101{
3102 return -ENXIO;
3103}
3104#endif /* coherent mmap */
3105
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003106static inline struct page *
3107snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3108{
3109 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003110#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3111 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3112 return virt_to_page(CAC_ADDR(vaddr));
3113#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003114#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3115 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3116 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3117 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3118 /* assume dma_handle set via pfn_to_phys() in
3119 * mm/dma-noncoherent.c
3120 */
3121 return pfn_to_page(addr >> PAGE_SHIFT);
3122 }
3123#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003124 return virt_to_page(vaddr);
3125}
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003128 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003130static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3131 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Takashi Iwai877211f2005-11-17 13:59:38 +01003133 struct snd_pcm_substream *substream = area->vm_private_data;
3134 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 unsigned long offset;
3136 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 size_t dma_bytes;
3138
3139 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003140 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003142 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3144 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003145 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003146 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003148 else
3149 page = snd_pcm_default_page_ops(substream, offset);
3150 if (!page)
3151 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003152 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003153 vmf->page = page;
3154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Takashi Iwai657b1982009-11-26 12:40:21 +01003157static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3158 .open = snd_pcm_mmap_data_open,
3159 .close = snd_pcm_mmap_data_close,
3160};
3161
3162static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 .open = snd_pcm_mmap_data_open,
3164 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003165 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166};
3167
Takashi Iwai657b1982009-11-26 12:40:21 +01003168#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3169/* This should be defined / handled globally! */
3170#ifdef CONFIG_ARM
3171#define ARCH_HAS_DMA_MMAP_COHERENT
3172#endif
3173#endif
3174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175/*
3176 * mmap the DMA buffer on RAM
3177 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003178static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3179 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003182#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3183 if (!substream->ops->page &&
3184 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3185 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3186 area,
3187 substream->runtime->dma_area,
3188 substream->runtime->dma_addr,
3189 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003190#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3191 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3192 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3193 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003194#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3195 /* mmap with fault handler */
3196 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 return 0;
3198}
3199
3200/*
3201 * mmap the DMA buffer on I/O memory area
3202 */
3203#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003204int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3205 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
3207 long size;
3208 unsigned long offset;
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 area->vm_flags |= VM_IO;
3212 size = area->vm_end - area->vm_start;
3213 offset = area->vm_pgoff << PAGE_SHIFT;
3214 if (io_remap_pfn_range(area, area->vm_start,
3215 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3216 size, area->vm_page_prot))
3217 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return 0;
3219}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003220
3221EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222#endif /* SNDRV_PCM_INFO_MMAP */
3223
Takashi Iwaic32d9772010-01-18 14:58:57 +01003224/* mmap callback with pgprot_noncached */
3225int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3226 struct vm_area_struct *area)
3227{
3228 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3229 return snd_pcm_default_mmap(substream, area);
3230}
3231EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
3234 * mmap DMA buffer
3235 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003236int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct vm_area_struct *area)
3238{
Takashi Iwai877211f2005-11-17 13:59:38 +01003239 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 long size;
3241 unsigned long offset;
3242 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003243 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3246 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3247 return -EINVAL;
3248 } else {
3249 if (!(area->vm_flags & VM_READ))
3250 return -EINVAL;
3251 }
3252 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3254 return -EBADFD;
3255 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3256 return -ENXIO;
3257 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3258 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3259 return -EINVAL;
3260 size = area->vm_end - area->vm_start;
3261 offset = area->vm_pgoff << PAGE_SHIFT;
3262 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3263 if ((size_t)size > dma_bytes)
3264 return -EINVAL;
3265 if (offset > dma_bytes - size)
3266 return -EINVAL;
3267
Takashi Iwai657b1982009-11-26 12:40:21 +01003268 area->vm_ops = &snd_pcm_vm_ops_data;
3269 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003271 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003273 err = snd_pcm_default_mmap(substream, area);
3274 if (!err)
3275 atomic_inc(&substream->mmap_count);
3276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003279EXPORT_SYMBOL(snd_pcm_mmap_data);
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3282{
Takashi Iwai877211f2005-11-17 13:59:38 +01003283 struct snd_pcm_file * pcm_file;
3284 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 unsigned long offset;
3286
3287 pcm_file = file->private_data;
3288 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003289 if (PCM_RUNTIME_CHECK(substream))
3290 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 offset = area->vm_pgoff << PAGE_SHIFT;
3293 switch (offset) {
3294 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003295 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return -ENXIO;
3297 return snd_pcm_mmap_status(substream, file, area);
3298 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003299 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return -ENXIO;
3301 return snd_pcm_mmap_control(substream, file, area);
3302 default:
3303 return snd_pcm_mmap_data(substream, file, area);
3304 }
3305 return 0;
3306}
3307
3308static int snd_pcm_fasync(int fd, struct file * file, int on)
3309{
Takashi Iwai877211f2005-11-17 13:59:38 +01003310 struct snd_pcm_file * pcm_file;
3311 struct snd_pcm_substream *substream;
3312 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003313 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003315 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 pcm_file = file->private_data;
3317 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003318 if (PCM_RUNTIME_CHECK(substream))
3319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003322out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003323 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
3327/*
3328 * ioctl32 compat
3329 */
3330#ifdef CONFIG_COMPAT
3331#include "pcm_compat.c"
3332#else
3333#define snd_pcm_ioctl_compat NULL
3334#endif
3335
3336/*
3337 * To be removed helpers to keep binary compatibility
3338 */
3339
Takashi Iwai59d48582005-12-01 10:51:58 +01003340#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3342#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3343
Takashi Iwai877211f2005-11-17 13:59:38 +01003344static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3345 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 unsigned int i;
3348
3349 memset(params, 0, sizeof(*params));
3350 params->flags = oparams->flags;
3351 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3352 params->masks[i].bits[0] = oparams->masks[i];
3353 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3354 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3355 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3356 params->info = oparams->info;
3357 params->msbits = oparams->msbits;
3358 params->rate_num = oparams->rate_num;
3359 params->rate_den = oparams->rate_den;
3360 params->fifo_size = oparams->fifo_size;
3361}
3362
Takashi Iwai877211f2005-11-17 13:59:38 +01003363static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3364 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 unsigned int i;
3367
3368 memset(oparams, 0, sizeof(*oparams));
3369 oparams->flags = params->flags;
3370 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3371 oparams->masks[i] = params->masks[i].bits[0];
3372 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3373 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3374 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3375 oparams->info = params->info;
3376 oparams->msbits = params->msbits;
3377 oparams->rate_num = params->rate_num;
3378 oparams->rate_den = params->rate_den;
3379 oparams->fifo_size = params->fifo_size;
3380}
3381
Takashi Iwai877211f2005-11-17 13:59:38 +01003382static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3383 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Takashi Iwai877211f2005-11-17 13:59:38 +01003385 struct snd_pcm_hw_params *params;
3386 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 int err;
3388
3389 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003390 if (!params)
3391 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Li Zefanef44a1e2009-04-10 09:43:08 +08003393 oparams = memdup_user(_oparams, sizeof(*oparams));
3394 if (IS_ERR(oparams)) {
3395 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 goto out;
3397 }
3398 snd_pcm_hw_convert_from_old_params(params, oparams);
3399 err = snd_pcm_hw_refine(substream, params);
3400 snd_pcm_hw_convert_to_old_params(oparams, params);
3401 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3402 if (!err)
3403 err = -EFAULT;
3404 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003405
3406 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407out:
3408 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 return err;
3410}
3411
Takashi Iwai877211f2005-11-17 13:59:38 +01003412static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3413 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
Takashi Iwai877211f2005-11-17 13:59:38 +01003415 struct snd_pcm_hw_params *params;
3416 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 int err;
3418
3419 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003420 if (!params)
3421 return -ENOMEM;
3422
3423 oparams = memdup_user(_oparams, sizeof(*oparams));
3424 if (IS_ERR(oparams)) {
3425 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 goto out;
3427 }
3428 snd_pcm_hw_convert_from_old_params(params, oparams);
3429 err = snd_pcm_hw_params(substream, params);
3430 snd_pcm_hw_convert_to_old_params(oparams, params);
3431 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3432 if (!err)
3433 err = -EFAULT;
3434 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003435
3436 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437out:
3438 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return err;
3440}
Takashi Iwai59d48582005-12-01 10:51:58 +01003441#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Cliff Cai70036092008-09-03 10:54:36 +02003443#ifndef CONFIG_MMU
3444unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3445 unsigned long len, unsigned long pgoff,
3446 unsigned long flags)
3447{
3448 return 0;
3449}
3450#else
3451# define dummy_get_unmapped_area NULL
3452#endif
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454/*
3455 * Register section
3456 */
3457
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003458const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003460 .owner = THIS_MODULE,
3461 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003462 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003463 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003464 .release = snd_pcm_release,
3465 .poll = snd_pcm_playback_poll,
3466 .unlocked_ioctl = snd_pcm_playback_ioctl,
3467 .compat_ioctl = snd_pcm_ioctl_compat,
3468 .mmap = snd_pcm_mmap,
3469 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003470 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 },
3472 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003473 .owner = THIS_MODULE,
3474 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003475 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003476 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003477 .release = snd_pcm_release,
3478 .poll = snd_pcm_capture_poll,
3479 .unlocked_ioctl = snd_pcm_capture_ioctl,
3480 .compat_ioctl = snd_pcm_ioctl_compat,
3481 .mmap = snd_pcm_mmap,
3482 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003483 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485};