blob: 56ec35e8510b1fdf296c260245820ca1c89a1189 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
37#include <asm/io.h>
38
39/*
40 * Compatibility
41 */
42
Takashi Iwai877211f2005-11-17 13:59:38 +010043struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 unsigned int flags;
45 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
46 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010047 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
49 unsigned int rmask;
50 unsigned int cmask;
51 unsigned int info;
52 unsigned int msbits;
53 unsigned int rate_num;
54 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010055 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned char reserved[64];
57};
58
Takashi Iwai59d48582005-12-01 10:51:58 +010059#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010060#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
61#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Takashi Iwai877211f2005-11-17 13:59:38 +010063static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
65static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
66 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010067#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010068static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70/*
71 *
72 */
73
74DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020075EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020077static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static inline mm_segment_t snd_enter_user(void)
80{
81 mm_segment_t fs = get_fs();
82 set_fs(get_ds());
83 return fs;
84}
85
86static inline void snd_leave_user(mm_segment_t fs)
87{
88 set_fs(fs);
89}
90
91
92
Takashi Iwai877211f2005-11-17 13:59:38 +010093int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Takashi Iwai877211f2005-11-17 13:59:38 +010095 struct snd_pcm_runtime *runtime;
96 struct snd_pcm *pcm = substream->pcm;
97 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
113 if (runtime) {
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
116 }
117 return 0;
118}
119
Takashi Iwai877211f2005-11-17 13:59:38 +0100120int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai877211f2005-11-17 13:59:38 +0100123 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int err;
125
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
127 if (! info)
128 return -ENOMEM;
129 err = snd_pcm_info(substream, info);
130 if (err >= 0) {
131 if (copy_to_user(_info, info, sizeof(*info)))
132 err = -EFAULT;
133 }
134 kfree(info);
135 return err;
136}
137
138#undef RULES_DEBUG
139
140#ifdef RULES_DEBUG
141#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142char *snd_pcm_hw_param_names[] = {
143 HW_PARAM(ACCESS),
144 HW_PARAM(FORMAT),
145 HW_PARAM(SUBFORMAT),
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
148 HW_PARAM(CHANNELS),
149 HW_PARAM(RATE),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(PERIODS),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
157 HW_PARAM(TICK_TIME),
158};
159#endif
160
Takashi Iwai877211f2005-11-17 13:59:38 +0100161int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
172 int changed, again;
173
174 params->info = 0;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 params->msbits = 0;
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
181 }
182
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
186 return -EINVAL;
187 if (!(params->rmask & (1 << k)))
188 continue;
189#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100190 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192#endif
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194#ifdef RULES_DEBUG
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
196#endif
197 if (changed)
198 params->cmask |= 1 << k;
199 if (changed < 0)
200 return changed;
201 }
202
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
206 return -EINVAL;
207 if (!(params->rmask & (1 << k)))
208 continue;
209#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100210 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (i->empty)
212 printk("empty");
213 else
214 printk("%c%u %u%c",
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
217 printk(" -> ");
218#endif
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
220#ifdef RULES_DEBUG
221 if (i->empty)
222 printk("empty\n");
223 else
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
227#endif
228 if (changed)
229 params->cmask |= 1 << k;
230 if (changed < 0)
231 return changed;
232 }
233
234 for (k = 0; k < constrs->rules_num; k++)
235 rstamps[k] = 0;
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
238 do {
239 again = 0;
240 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int d;
243 int doit = 0;
244 if (r->cond && !(r->cond & params->flags))
245 continue;
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
248 doit = 1;
249 break;
250 }
251 }
252 if (!doit)
253 continue;
254#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100255 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (r->var >= 0) {
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
261 } else {
262 i = hw_param_interval(params, r->var);
263 if (i->empty)
264 printk("empty");
265 else
266 printk("%c%u %u%c",
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
269 }
270 }
271#endif
272 changed = r->func(params, r);
273#ifdef RULES_DEBUG
274 if (r->var >= 0) {
275 printk(" -> ");
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
278 else {
279 if (i->empty)
280 printk("empty");
281 else
282 printk("%c%u %u%c",
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
285 }
286 }
287 printk("\n");
288#endif
289 rstamps[k] = stamp;
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
293 again = 1;
294 }
295 if (changed < 0)
296 return changed;
297 stamp++;
298 }
299 } while (again);
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
304 }
305
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
311 }
312 }
313
314 hw = &substream->runtime->hw;
315 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200316 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
317 if (!params->fifo_size) {
318 if (snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) ==
319 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_FORMAT]) &&
320 snd_mask_min(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS]) ==
321 snd_mask_max(&params->masks[SNDRV_PCM_HW_PARAM_CHANNELS])) {
322 changed = substream->ops->ioctl(substream,
323 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200324 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200325 return changed;
326 }
327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 params->rmask = 0;
329 return 0;
330}
331
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200332EXPORT_SYMBOL(snd_pcm_hw_refine);
333
Takashi Iwai877211f2005-11-17 13:59:38 +0100334static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
335 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Takashi Iwai877211f2005-11-17 13:59:38 +0100337 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int err;
339
Li Zefanef44a1e2009-04-10 09:43:08 +0800340 params = memdup_user(_params, sizeof(*params));
341 if (IS_ERR(params))
342 return PTR_ERR(params);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 err = snd_pcm_hw_refine(substream, params);
345 if (copy_to_user(_params, params, sizeof(*params))) {
346 if (!err)
347 err = -EFAULT;
348 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(params);
351 return err;
352}
353
Takashi Iwai9442e692006-09-30 23:27:19 -0700354static int period_to_usecs(struct snd_pcm_runtime *runtime)
355{
356 int usecs;
357
358 if (! runtime->rate)
359 return -1; /* invalid */
360
361 /* take 75% of period time as the deadline */
362 usecs = (750000 / runtime->rate) * runtime->period_size;
363 usecs += ((750000 % runtime->rate) * runtime->period_size) /
364 runtime->rate;
365
366 return usecs;
367}
368
Takashi Iwai877211f2005-11-17 13:59:38 +0100369static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
370 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Takashi Iwai877211f2005-11-17 13:59:38 +0100372 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700373 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unsigned int bits;
375 snd_pcm_uframes_t frames;
376
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200377 if (PCM_RUNTIME_CHECK(substream))
378 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 snd_pcm_stream_lock_irq(substream);
381 switch (runtime->status->state) {
382 case SNDRV_PCM_STATE_OPEN:
383 case SNDRV_PCM_STATE_SETUP:
384 case SNDRV_PCM_STATE_PREPARED:
385 break;
386 default:
387 snd_pcm_stream_unlock_irq(substream);
388 return -EBADFD;
389 }
390 snd_pcm_stream_unlock_irq(substream);
391#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
392 if (!substream->oss.oss)
393#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200394 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EBADFD;
396
397 params->rmask = ~0U;
398 err = snd_pcm_hw_refine(substream, params);
399 if (err < 0)
400 goto _error;
401
402 err = snd_pcm_hw_params_choose(substream, params);
403 if (err < 0)
404 goto _error;
405
406 if (substream->ops->hw_params != NULL) {
407 err = substream->ops->hw_params(substream, params);
408 if (err < 0)
409 goto _error;
410 }
411
412 runtime->access = params_access(params);
413 runtime->format = params_format(params);
414 runtime->subformat = params_subformat(params);
415 runtime->channels = params_channels(params);
416 runtime->rate = params_rate(params);
417 runtime->period_size = params_period_size(params);
418 runtime->periods = params_periods(params);
419 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 runtime->info = params->info;
421 runtime->rate_num = params->rate_num;
422 runtime->rate_den = params->rate_den;
423
424 bits = snd_pcm_format_physical_width(runtime->format);
425 runtime->sample_bits = bits;
426 bits *= runtime->channels;
427 runtime->frame_bits = bits;
428 frames = 1;
429 while (bits % 8 != 0) {
430 bits *= 2;
431 frames *= 2;
432 }
433 runtime->byte_align = bits / 8;
434 runtime->min_align = frames;
435
436 /* Default sw params */
437 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
438 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 runtime->start_threshold = 1;
441 runtime->stop_threshold = runtime->buffer_size;
442 runtime->silence_threshold = 0;
443 runtime->silence_size = 0;
444 runtime->boundary = runtime->buffer_size;
445 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
446 runtime->boundary *= 2;
447
448 snd_pcm_timer_resolution_change(substream);
449 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700450
Mark Grossf011e2e2008-02-04 22:30:09 -0800451 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
452 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700453 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800454 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
455 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457 _error:
458 /* hardware might be unuseable from this time,
459 so we force application to retry to set
460 the correct hardware parameter settings */
461 runtime->status->state = SNDRV_PCM_STATE_OPEN;
462 if (substream->ops->hw_free != NULL)
463 substream->ops->hw_free(substream);
464 return err;
465}
466
Takashi Iwai877211f2005-11-17 13:59:38 +0100467static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
468 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai877211f2005-11-17 13:59:38 +0100470 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 int err;
472
Li Zefanef44a1e2009-04-10 09:43:08 +0800473 params = memdup_user(_params, sizeof(*params));
474 if (IS_ERR(params))
475 return PTR_ERR(params);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 err = snd_pcm_hw_params(substream, params);
478 if (copy_to_user(_params, params, sizeof(*params))) {
479 if (!err)
480 err = -EFAULT;
481 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 kfree(params);
484 return err;
485}
486
Takashi Iwai877211f2005-11-17 13:59:38 +0100487static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai877211f2005-11-17 13:59:38 +0100489 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int result = 0;
491
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200492 if (PCM_RUNTIME_CHECK(substream))
493 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 snd_pcm_stream_lock_irq(substream);
496 switch (runtime->status->state) {
497 case SNDRV_PCM_STATE_SETUP:
498 case SNDRV_PCM_STATE_PREPARED:
499 break;
500 default:
501 snd_pcm_stream_unlock_irq(substream);
502 return -EBADFD;
503 }
504 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200505 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return -EBADFD;
507 if (substream->ops->hw_free)
508 result = substream->ops->hw_free(substream);
509 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800510 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
511 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return result;
513}
514
Takashi Iwai877211f2005-11-17 13:59:38 +0100515static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
516 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Takashi Iwai877211f2005-11-17 13:59:38 +0100518 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100519 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200521 if (PCM_RUNTIME_CHECK(substream))
522 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 snd_pcm_stream_lock_irq(substream);
525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
526 snd_pcm_stream_unlock_irq(substream);
527 return -EBADFD;
528 }
529 snd_pcm_stream_unlock_irq(substream);
530
531 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
532 return -EINVAL;
533 if (params->avail_min == 0)
534 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (params->silence_size >= runtime->boundary) {
536 if (params->silence_threshold != 0)
537 return -EINVAL;
538 } else {
539 if (params->silence_size > params->silence_threshold)
540 return -EINVAL;
541 if (params->silence_threshold > runtime->buffer_size)
542 return -EINVAL;
543 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100544 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 snd_pcm_stream_lock_irq(substream);
546 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 runtime->period_step = params->period_step;
548 runtime->control->avail_min = params->avail_min;
549 runtime->start_threshold = params->start_threshold;
550 runtime->stop_threshold = params->stop_threshold;
551 runtime->silence_threshold = params->silence_threshold;
552 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 params->boundary = runtime->boundary;
554 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
556 runtime->silence_size > 0)
557 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100558 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100561 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Takashi Iwai877211f2005-11-17 13:59:38 +0100564static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
565 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwai877211f2005-11-17 13:59:38 +0100567 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int err;
569 if (copy_from_user(&params, _params, sizeof(params)))
570 return -EFAULT;
571 err = snd_pcm_sw_params(substream, &params);
572 if (copy_to_user(_params, &params, sizeof(params)))
573 return -EFAULT;
574 return err;
575}
576
Takashi Iwai877211f2005-11-17 13:59:38 +0100577int snd_pcm_status(struct snd_pcm_substream *substream,
578 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai877211f2005-11-17 13:59:38 +0100580 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 snd_pcm_stream_lock_irq(substream);
583 status->state = runtime->status->state;
584 status->suspended_state = runtime->status->suspended_state;
585 if (status->state == SNDRV_PCM_STATE_OPEN)
586 goto _end;
587 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100588 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100590 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
591 status->tstamp = runtime->status->tstamp;
592 goto _tstamp_end;
593 }
594 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100595 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100596 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 status->appl_ptr = runtime->control->appl_ptr;
598 status->hw_ptr = runtime->status->hw_ptr;
599 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
600 status->avail = snd_pcm_playback_avail(runtime);
601 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200602 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200604 status->delay += runtime->delay;
605 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 status->delay = 0;
607 } else {
608 status->avail = snd_pcm_capture_avail(runtime);
609 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200610 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 else
612 status->delay = 0;
613 }
614 status->avail_max = runtime->avail_max;
615 status->overrange = runtime->overrange;
616 runtime->avail_max = 0;
617 runtime->overrange = 0;
618 _end:
619 snd_pcm_stream_unlock_irq(substream);
620 return 0;
621}
622
Takashi Iwai877211f2005-11-17 13:59:38 +0100623static int snd_pcm_status_user(struct snd_pcm_substream *substream,
624 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Takashi Iwai877211f2005-11-17 13:59:38 +0100626 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int res;
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 memset(&status, 0, sizeof(status));
630 res = snd_pcm_status(substream, &status);
631 if (res < 0)
632 return res;
633 if (copy_to_user(_status, &status, sizeof(status)))
634 return -EFAULT;
635 return 0;
636}
637
Takashi Iwai877211f2005-11-17 13:59:38 +0100638static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
639 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Takashi Iwai877211f2005-11-17 13:59:38 +0100641 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned int channel;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 channel = info->channel;
645 runtime = substream->runtime;
646 snd_pcm_stream_lock_irq(substream);
647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
648 snd_pcm_stream_unlock_irq(substream);
649 return -EBADFD;
650 }
651 snd_pcm_stream_unlock_irq(substream);
652 if (channel >= runtime->channels)
653 return -EINVAL;
654 memset(info, 0, sizeof(*info));
655 info->channel = channel;
656 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
657}
658
Takashi Iwai877211f2005-11-17 13:59:38 +0100659static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
660 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Takashi Iwai877211f2005-11-17 13:59:38 +0100662 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int res;
664
665 if (copy_from_user(&info, _info, sizeof(info)))
666 return -EFAULT;
667 res = snd_pcm_channel_info(substream, &info);
668 if (res < 0)
669 return res;
670 if (copy_to_user(_info, &info, sizeof(info)))
671 return -EFAULT;
672 return 0;
673}
674
Takashi Iwai877211f2005-11-17 13:59:38 +0100675static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (runtime->trigger_master == NULL)
679 return;
680 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100681 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 } else {
683 snd_pcm_trigger_tstamp(runtime->trigger_master);
684 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
685 }
686 runtime->trigger_master = NULL;
687}
688
689struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100690 int (*pre_action)(struct snd_pcm_substream *substream, int state);
691 int (*do_action)(struct snd_pcm_substream *substream, int state);
692 void (*undo_action)(struct snd_pcm_substream *substream, int state);
693 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694};
695
696/*
697 * this functions is core for handling of linked stream
698 * Note: the stream state might be changed also on failure
699 * Note2: call with calling stream lock + link lock
700 */
701static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100702 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int state, int do_lock)
704{
Takashi Iwai877211f2005-11-17 13:59:38 +0100705 struct snd_pcm_substream *s = NULL;
706 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 int res = 0;
708
Takashi Iwaief991b92007-02-22 12:52:53 +0100709 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200711 spin_lock_nested(&s->self_group.lock,
712 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 res = ops->pre_action(s, state);
714 if (res < 0)
715 goto _unlock;
716 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100717 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 res = ops->do_action(s, state);
719 if (res < 0) {
720 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100721 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (s1 == s) /* failed stream */
723 break;
724 ops->undo_action(s1, state);
725 }
726 }
727 s = NULL; /* unlock all */
728 goto _unlock;
729 }
730 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100731 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ops->post_action(s, state);
733 }
734 _unlock:
735 if (do_lock) {
736 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100737 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (s1 != substream)
739 spin_unlock(&s1->self_group.lock);
740 if (s1 == s) /* end */
741 break;
742 }
743 }
744 return res;
745}
746
747/*
748 * Note: call with stream lock
749 */
750static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100751 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int state)
753{
754 int res;
755
756 res = ops->pre_action(substream, state);
757 if (res < 0)
758 return res;
759 res = ops->do_action(substream, state);
760 if (res == 0)
761 ops->post_action(substream, state);
762 else if (ops->undo_action)
763 ops->undo_action(substream, state);
764 return res;
765}
766
767/*
768 * Note: call with stream lock
769 */
770static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100771 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 int state)
773{
774 int res;
775
776 if (snd_pcm_stream_linked(substream)) {
777 if (!spin_trylock(&substream->group->lock)) {
778 spin_unlock(&substream->self_group.lock);
779 spin_lock(&substream->group->lock);
780 spin_lock(&substream->self_group.lock);
781 }
782 res = snd_pcm_action_group(ops, substream, state, 1);
783 spin_unlock(&substream->group->lock);
784 } else {
785 res = snd_pcm_action_single(ops, substream, state);
786 }
787 return res;
788}
789
790/*
791 * Note: don't use any locks before
792 */
793static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100794 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 int state)
796{
797 int res;
798
799 read_lock_irq(&snd_pcm_link_rwlock);
800 if (snd_pcm_stream_linked(substream)) {
801 spin_lock(&substream->group->lock);
802 spin_lock(&substream->self_group.lock);
803 res = snd_pcm_action_group(ops, substream, state, 1);
804 spin_unlock(&substream->self_group.lock);
805 spin_unlock(&substream->group->lock);
806 } else {
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_single(ops, substream, state);
809 spin_unlock(&substream->self_group.lock);
810 }
811 read_unlock_irq(&snd_pcm_link_rwlock);
812 return res;
813}
814
815/*
816 */
817static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100818 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 int state)
820{
821 int res;
822
823 down_read(&snd_pcm_link_rwsem);
824 if (snd_pcm_stream_linked(substream))
825 res = snd_pcm_action_group(ops, substream, state, 0);
826 else
827 res = snd_pcm_action_single(ops, substream, state);
828 up_read(&snd_pcm_link_rwsem);
829 return res;
830}
831
832/*
833 * start callbacks
834 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100835static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Takashi Iwai877211f2005-11-17 13:59:38 +0100837 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
839 return -EBADFD;
840 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
841 !snd_pcm_playback_data(substream))
842 return -EPIPE;
843 runtime->trigger_master = substream;
844 return 0;
845}
846
Takashi Iwai877211f2005-11-17 13:59:38 +0100847static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 if (substream->runtime->trigger_master != substream)
850 return 0;
851 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
852}
853
Takashi Iwai877211f2005-11-17 13:59:38 +0100854static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 if (substream->runtime->trigger_master == substream)
857 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
858}
859
Takashi Iwai877211f2005-11-17 13:59:38 +0100860static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Takashi Iwai877211f2005-11-17 13:59:38 +0100862 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200864 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 runtime->status->state = state;
866 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
867 runtime->silence_size > 0)
868 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100870 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
871 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static struct action_ops snd_pcm_action_start = {
875 .pre_action = snd_pcm_pre_start,
876 .do_action = snd_pcm_do_start,
877 .undo_action = snd_pcm_undo_start,
878 .post_action = snd_pcm_post_start
879};
880
881/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700882 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200883 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100885int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Takashi Iwai877211f2005-11-17 13:59:38 +0100887 return snd_pcm_action(&snd_pcm_action_start, substream,
888 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
892 * stop callbacks
893 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100894static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Takashi Iwai877211f2005-11-17 13:59:38 +0100896 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
898 return -EBADFD;
899 runtime->trigger_master = substream;
900 return 0;
901}
902
Takashi Iwai877211f2005-11-17 13:59:38 +0100903static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
905 if (substream->runtime->trigger_master == substream &&
906 snd_pcm_running(substream))
907 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
908 return 0; /* unconditonally stop all substreams */
909}
910
Takashi Iwai877211f2005-11-17 13:59:38 +0100911static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Takashi Iwai877211f2005-11-17 13:59:38 +0100913 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (runtime->status->state != state) {
915 snd_pcm_trigger_tstamp(substream);
916 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100917 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
918 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100922 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
925static struct action_ops snd_pcm_action_stop = {
926 .pre_action = snd_pcm_pre_stop,
927 .do_action = snd_pcm_do_stop,
928 .post_action = snd_pcm_post_stop
929};
930
931/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700932 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200933 * @substream: the PCM substream instance
934 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700936 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100938int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
941}
942
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200943EXPORT_SYMBOL(snd_pcm_stop);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700946 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200947 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700949 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * Unlike snd_pcm_stop(), this affects only the given stream.
951 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100952int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Takashi Iwai877211f2005-11-17 13:59:38 +0100954 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
955 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956}
957
958/*
959 * pause callbacks
960 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100961static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwai877211f2005-11-17 13:59:38 +0100963 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
965 return -ENOSYS;
966 if (push) {
967 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
968 return -EBADFD;
969 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
970 return -EBADFD;
971 runtime->trigger_master = substream;
972 return 0;
973}
974
Takashi Iwai877211f2005-11-17 13:59:38 +0100975static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 if (substream->runtime->trigger_master != substream)
978 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200979 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
980 * a delta betwen the current jiffies, this gives a large enough
981 * delta, effectively to skip the check once.
982 */
983 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return substream->ops->trigger(substream,
985 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
986 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
987}
988
Takashi Iwai877211f2005-11-17 13:59:38 +0100989static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 if (substream->runtime->trigger_master == substream)
992 substream->ops->trigger(substream,
993 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
994 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
995}
996
Takashi Iwai877211f2005-11-17 13:59:38 +0100997static void snd_pcm_post_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 snd_pcm_trigger_tstamp(substream);
1001 if (push) {
1002 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1003 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001004 snd_timer_notify(substream->timer,
1005 SNDRV_TIMER_EVENT_MPAUSE,
1006 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001008 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
1010 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001012 snd_timer_notify(substream->timer,
1013 SNDRV_TIMER_EVENT_MCONTINUE,
1014 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017
1018static struct action_ops snd_pcm_action_pause = {
1019 .pre_action = snd_pcm_pre_pause,
1020 .do_action = snd_pcm_do_pause,
1021 .undo_action = snd_pcm_undo_pause,
1022 .post_action = snd_pcm_post_pause
1023};
1024
1025/*
1026 * Push/release the pause for all linked streams.
1027 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001028static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
1030 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1031}
1032
1033#ifdef CONFIG_PM
1034/* suspend */
1035
Takashi Iwai877211f2005-11-17 13:59:38 +01001036static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Takashi Iwai877211f2005-11-17 13:59:38 +01001038 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1040 return -EBUSY;
1041 runtime->trigger_master = substream;
1042 return 0;
1043}
1044
Takashi Iwai877211f2005-11-17 13:59:38 +01001045static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Takashi Iwai877211f2005-11-17 13:59:38 +01001047 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (runtime->trigger_master != substream)
1049 return 0;
1050 if (! snd_pcm_running(substream))
1051 return 0;
1052 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1053 return 0; /* suspend unconditionally */
1054}
1055
Takashi Iwai877211f2005-11-17 13:59:38 +01001056static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai877211f2005-11-17 13:59:38 +01001058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 snd_pcm_trigger_tstamp(substream);
1060 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001061 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1062 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 runtime->status->suspended_state = runtime->status->state;
1064 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001066 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069static struct action_ops snd_pcm_action_suspend = {
1070 .pre_action = snd_pcm_pre_suspend,
1071 .do_action = snd_pcm_do_suspend,
1072 .post_action = snd_pcm_post_suspend
1073};
1074
1075/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001076 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001077 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * After this call, all streams are changed to SUSPENDED state.
1080 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001081int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 int err;
1084 unsigned long flags;
1085
Takashi Iwai603bf522005-11-17 15:59:14 +01001086 if (! substream)
1087 return 0;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 snd_pcm_stream_lock_irqsave(substream, flags);
1090 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1091 snd_pcm_stream_unlock_irqrestore(substream, flags);
1092 return err;
1093}
1094
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001095EXPORT_SYMBOL(snd_pcm_suspend);
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001098 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001099 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 * After this call, all streams are changed to SUSPENDED state.
1102 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001103int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Takashi Iwai877211f2005-11-17 13:59:38 +01001105 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int stream, err = 0;
1107
Takashi Iwai603bf522005-11-17 15:59:14 +01001108 if (! pcm)
1109 return 0;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001112 for (substream = pcm->streams[stream].substream;
1113 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /* FIXME: the open/close code should lock this as well */
1115 if (substream->runtime == NULL)
1116 continue;
1117 err = snd_pcm_suspend(substream);
1118 if (err < 0 && err != -EBUSY)
1119 return err;
1120 }
1121 }
1122 return 0;
1123}
1124
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001125EXPORT_SYMBOL(snd_pcm_suspend_all);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/* resume */
1128
Takashi Iwai877211f2005-11-17 13:59:38 +01001129static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Takashi Iwai877211f2005-11-17 13:59:38 +01001131 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1133 return -ENOSYS;
1134 runtime->trigger_master = substream;
1135 return 0;
1136}
1137
Takashi Iwai877211f2005-11-17 13:59:38 +01001138static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai877211f2005-11-17 13:59:38 +01001140 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (runtime->trigger_master != substream)
1142 return 0;
1143 /* DMA not running previously? */
1144 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1145 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1146 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1147 return 0;
1148 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1149}
1150
Takashi Iwai877211f2005-11-17 13:59:38 +01001151static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 if (substream->runtime->trigger_master == substream &&
1154 snd_pcm_running(substream))
1155 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1156}
1157
Takashi Iwai877211f2005-11-17 13:59:38 +01001158static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Takashi Iwai877211f2005-11-17 13:59:38 +01001160 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 snd_pcm_trigger_tstamp(substream);
1162 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001163 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1164 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
1168static struct action_ops snd_pcm_action_resume = {
1169 .pre_action = snd_pcm_pre_resume,
1170 .do_action = snd_pcm_do_resume,
1171 .undo_action = snd_pcm_undo_resume,
1172 .post_action = snd_pcm_post_resume
1173};
1174
Takashi Iwai877211f2005-11-17 13:59:38 +01001175static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Takashi Iwai877211f2005-11-17 13:59:38 +01001177 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 int res;
1179
1180 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001181 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1183 snd_power_unlock(card);
1184 return res;
1185}
1186
1187#else
1188
Takashi Iwai877211f2005-11-17 13:59:38 +01001189static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 return -ENOSYS;
1192}
1193
1194#endif /* CONFIG_PM */
1195
1196/*
1197 * xrun ioctl
1198 *
1199 * Change the RUNNING stream(s) to XRUN state.
1200 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001201static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Takashi Iwai877211f2005-11-17 13:59:38 +01001203 struct snd_card *card = substream->pcm->card;
1204 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int result;
1206
1207 snd_power_lock(card);
1208 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001209 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (result < 0)
1211 goto _unlock;
1212 }
1213
1214 snd_pcm_stream_lock_irq(substream);
1215 switch (runtime->status->state) {
1216 case SNDRV_PCM_STATE_XRUN:
1217 result = 0; /* already there */
1218 break;
1219 case SNDRV_PCM_STATE_RUNNING:
1220 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1221 break;
1222 default:
1223 result = -EBADFD;
1224 }
1225 snd_pcm_stream_unlock_irq(substream);
1226 _unlock:
1227 snd_power_unlock(card);
1228 return result;
1229}
1230
1231/*
1232 * reset ioctl
1233 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001234static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Takashi Iwai877211f2005-11-17 13:59:38 +01001236 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 switch (runtime->status->state) {
1238 case SNDRV_PCM_STATE_RUNNING:
1239 case SNDRV_PCM_STATE_PREPARED:
1240 case SNDRV_PCM_STATE_PAUSED:
1241 case SNDRV_PCM_STATE_SUSPENDED:
1242 return 0;
1243 default:
1244 return -EBADFD;
1245 }
1246}
1247
Takashi Iwai877211f2005-11-17 13:59:38 +01001248static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Takashi Iwai877211f2005-11-17 13:59:38 +01001250 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1252 if (err < 0)
1253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 runtime->hw_ptr_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 runtime->silence_start = runtime->status->hw_ptr;
1256 runtime->silence_filled = 0;
1257 return 0;
1258}
1259
Takashi Iwai877211f2005-11-17 13:59:38 +01001260static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwai877211f2005-11-17 13:59:38 +01001262 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 runtime->control->appl_ptr = runtime->status->hw_ptr;
1264 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1265 runtime->silence_size > 0)
1266 snd_pcm_playback_silence(substream, ULONG_MAX);
1267}
1268
1269static struct action_ops snd_pcm_action_reset = {
1270 .pre_action = snd_pcm_pre_reset,
1271 .do_action = snd_pcm_do_reset,
1272 .post_action = snd_pcm_post_reset
1273};
1274
Takashi Iwai877211f2005-11-17 13:59:38 +01001275static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1278}
1279
1280/*
1281 * prepare ioctl
1282 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001283/* we use the second argument for updating f_flags */
1284static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1285 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Takashi Iwai877211f2005-11-17 13:59:38 +01001287 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001288 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1289 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EBADFD;
1291 if (snd_pcm_running(substream))
1292 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001293 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
1296
Takashi Iwai877211f2005-11-17 13:59:38 +01001297static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 int err;
1300 err = substream->ops->prepare(substream);
1301 if (err < 0)
1302 return err;
1303 return snd_pcm_do_reset(substream, 0);
1304}
1305
Takashi Iwai877211f2005-11-17 13:59:38 +01001306static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Takashi Iwai877211f2005-11-17 13:59:38 +01001308 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 runtime->control->appl_ptr = runtime->status->hw_ptr;
1310 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1311}
1312
1313static struct action_ops snd_pcm_action_prepare = {
1314 .pre_action = snd_pcm_pre_prepare,
1315 .do_action = snd_pcm_do_prepare,
1316 .post_action = snd_pcm_post_prepare
1317};
1318
1319/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001320 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001321 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001322 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001324static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1325 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001328 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001329 int f_flags;
1330
1331 if (file)
1332 f_flags = file->f_flags;
1333 else
1334 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001337 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001338 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1339 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 snd_power_unlock(card);
1341 return res;
1342}
1343
1344/*
1345 * drain ioctl
1346 */
1347
Takashi Iwai877211f2005-11-17 13:59:38 +01001348static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 substream->runtime->trigger_master = substream;
1351 return 0;
1352}
1353
Takashi Iwai877211f2005-11-17 13:59:38 +01001354static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
Takashi Iwai877211f2005-11-17 13:59:38 +01001356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1358 switch (runtime->status->state) {
1359 case SNDRV_PCM_STATE_PREPARED:
1360 /* start playback stream if possible */
1361 if (! snd_pcm_playback_empty(substream)) {
1362 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1363 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1364 }
1365 break;
1366 case SNDRV_PCM_STATE_RUNNING:
1367 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1368 break;
1369 default:
1370 break;
1371 }
1372 } else {
1373 /* stop running stream */
1374 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001375 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001377 snd_pcm_do_stop(substream, new_state);
1378 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 }
1381 return 0;
1382}
1383
Takashi Iwai877211f2005-11-17 13:59:38 +01001384static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386}
1387
1388static struct action_ops snd_pcm_action_drain_init = {
1389 .pre_action = snd_pcm_pre_drain_init,
1390 .do_action = snd_pcm_do_drain_init,
1391 .post_action = snd_pcm_post_drain_init
1392};
1393
Takashi Iwai877211f2005-11-17 13:59:38 +01001394static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396/*
1397 * Drain the stream(s).
1398 * When the substream is linked, sync until the draining of all playback streams
1399 * is finished.
1400 * After this call, all streams are supposed to be either SETUP or DRAINING
1401 * (capture only) state.
1402 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001403static int snd_pcm_drain(struct snd_pcm_substream *substream,
1404 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Takashi Iwai877211f2005-11-17 13:59:38 +01001406 struct snd_card *card;
1407 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001408 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001409 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001411 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 card = substream->pcm->card;
1414 runtime = substream->runtime;
1415
1416 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1417 return -EBADFD;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 snd_power_lock(card);
1420 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001421 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001422 if (result < 0) {
1423 snd_power_unlock(card);
1424 return result;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001428 if (file) {
1429 if (file->f_flags & O_NONBLOCK)
1430 nonblock = 1;
1431 } else if (substream->f_flags & O_NONBLOCK)
1432 nonblock = 1;
1433
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001434 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001435 snd_pcm_stream_lock_irq(substream);
1436 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001437 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001438 snd_pcm_pause(substream, 0);
1439
1440 /* pre-start/stop - all running streams are changed to DRAINING state */
1441 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001442 if (result < 0)
1443 goto unlock;
1444 /* in non-blocking, we don't wait in ioctl but let caller poll */
1445 if (nonblock) {
1446 result = -EAGAIN;
1447 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 for (;;) {
1451 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001452 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (signal_pending(current)) {
1454 result = -ERESTARTSYS;
1455 break;
1456 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001457 /* find a substream to drain */
1458 to_check = NULL;
1459 snd_pcm_group_for_each_entry(s, substream) {
1460 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1461 continue;
1462 runtime = s->runtime;
1463 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1464 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001465 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001466 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001467 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001468 if (!to_check)
1469 break; /* all drained */
1470 init_waitqueue_entry(&wait, current);
1471 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 set_current_state(TASK_INTERRUPTIBLE);
1473 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001474 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 snd_power_unlock(card);
1476 tout = schedule_timeout(10 * HZ);
1477 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001478 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001480 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (tout == 0) {
1482 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1483 result = -ESTRPIPE;
1484 else {
1485 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1486 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1487 result = -EIO;
1488 }
1489 break;
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001492
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001493 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001494 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001495 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 return result;
1499}
1500
1501/*
1502 * drop ioctl
1503 *
1504 * Immediately put all linked substreams into SETUP state.
1505 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001506static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Takashi Iwai877211f2005-11-17 13:59:38 +01001508 struct snd_pcm_runtime *runtime;
1509 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 int result = 0;
1511
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001512 if (PCM_RUNTIME_CHECK(substream))
1513 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 runtime = substream->runtime;
1515 card = substream->pcm->card;
1516
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001517 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001518 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1519 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return -EBADFD;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 snd_pcm_stream_lock_irq(substream);
1523 /* resume pause */
1524 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1525 snd_pcm_pause(substream, 0);
1526
1527 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1528 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1529 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return result;
1532}
1533
1534
1535/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536static struct file *snd_pcm_file_fd(int fd)
1537{
1538 struct file *file;
1539 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001540 unsigned int minor;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 file = fget(fd);
1543 if (!file)
1544 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001545 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (!S_ISCHR(inode->i_mode) ||
1547 imajor(inode) != snd_major) {
1548 fput(file);
1549 return NULL;
1550 }
1551 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001552 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1553 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 fput(file);
1555 return NULL;
1556 }
1557 return file;
1558}
1559
1560/*
1561 * PCM link handling
1562 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001563static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 int res = 0;
1566 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001567 struct snd_pcm_file *pcm_file;
1568 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 file = snd_pcm_file_fd(fd);
1571 if (!file)
1572 return -EBADFD;
1573 pcm_file = file->private_data;
1574 substream1 = pcm_file->substream;
1575 down_write(&snd_pcm_link_rwsem);
1576 write_lock_irq(&snd_pcm_link_rwlock);
1577 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1578 substream->runtime->status->state != substream1->runtime->status->state) {
1579 res = -EBADFD;
1580 goto _end;
1581 }
1582 if (snd_pcm_stream_linked(substream1)) {
1583 res = -EALREADY;
1584 goto _end;
1585 }
1586 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001587 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (substream->group == NULL) {
1589 res = -ENOMEM;
1590 goto _end;
1591 }
1592 spin_lock_init(&substream->group->lock);
1593 INIT_LIST_HEAD(&substream->group->substreams);
1594 list_add_tail(&substream->link_list, &substream->group->substreams);
1595 substream->group->count = 1;
1596 }
1597 list_add_tail(&substream1->link_list, &substream->group->substreams);
1598 substream->group->count++;
1599 substream1->group = substream->group;
1600 _end:
1601 write_unlock_irq(&snd_pcm_link_rwlock);
1602 up_write(&snd_pcm_link_rwsem);
1603 fput(file);
1604 return res;
1605}
1606
Takashi Iwai877211f2005-11-17 13:59:38 +01001607static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 substream->group = &substream->self_group;
1610 INIT_LIST_HEAD(&substream->self_group.substreams);
1611 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1612}
1613
Takashi Iwai877211f2005-11-17 13:59:38 +01001614static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Takashi Iwaief991b92007-02-22 12:52:53 +01001616 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int res = 0;
1618
1619 down_write(&snd_pcm_link_rwsem);
1620 write_lock_irq(&snd_pcm_link_rwlock);
1621 if (!snd_pcm_stream_linked(substream)) {
1622 res = -EALREADY;
1623 goto _end;
1624 }
1625 list_del(&substream->link_list);
1626 substream->group->count--;
1627 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001628 snd_pcm_group_for_each_entry(s, substream) {
1629 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 break;
1631 }
1632 kfree(substream->group);
1633 }
1634 relink_to_local(substream);
1635 _end:
1636 write_unlock_irq(&snd_pcm_link_rwlock);
1637 up_write(&snd_pcm_link_rwsem);
1638 return res;
1639}
1640
1641/*
1642 * hw configurator
1643 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001644static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1645 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Takashi Iwai877211f2005-11-17 13:59:38 +01001647 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1649 hw_param_interval_c(params, rule->deps[1]), &t);
1650 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1651}
1652
Takashi Iwai877211f2005-11-17 13:59:38 +01001653static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1654 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Takashi Iwai877211f2005-11-17 13:59:38 +01001656 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1658 hw_param_interval_c(params, rule->deps[1]), &t);
1659 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1660}
1661
Takashi Iwai877211f2005-11-17 13:59:38 +01001662static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1663 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Takashi Iwai877211f2005-11-17 13:59:38 +01001665 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1667 hw_param_interval_c(params, rule->deps[1]),
1668 (unsigned long) rule->private, &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1670}
1671
Takashi Iwai877211f2005-11-17 13:59:38 +01001672static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai877211f2005-11-17 13:59:38 +01001675 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1677 (unsigned long) rule->private,
1678 hw_param_interval_c(params, rule->deps[1]), &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1680}
1681
Takashi Iwai877211f2005-11-17 13:59:38 +01001682static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001686 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1687 struct snd_mask m;
1688 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snd_mask_any(&m);
1690 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1691 int bits;
1692 if (! snd_mask_test(mask, k))
1693 continue;
1694 bits = snd_pcm_format_physical_width(k);
1695 if (bits <= 0)
1696 continue; /* ignore invalid formats */
1697 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1698 snd_mask_reset(&m, k);
1699 }
1700 return snd_mask_refine(mask, &m);
1701}
1702
Takashi Iwai877211f2005-11-17 13:59:38 +01001703static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1704 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwai877211f2005-11-17 13:59:38 +01001706 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 unsigned int k;
1708 t.min = UINT_MAX;
1709 t.max = 0;
1710 t.openmin = 0;
1711 t.openmax = 0;
1712 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1713 int bits;
1714 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1715 continue;
1716 bits = snd_pcm_format_physical_width(k);
1717 if (bits <= 0)
1718 continue; /* ignore invalid formats */
1719 if (t.min > (unsigned)bits)
1720 t.min = bits;
1721 if (t.max < (unsigned)bits)
1722 t.max = bits;
1723 }
1724 t.integer = 1;
1725 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1726}
1727
1728#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1729#error "Change this table"
1730#endif
1731
1732static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1733 48000, 64000, 88200, 96000, 176400, 192000 };
1734
Clemens Ladisch7653d552007-08-13 17:38:54 +02001735const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1736 .count = ARRAY_SIZE(rates),
1737 .list = rates,
1738};
1739
Takashi Iwai877211f2005-11-17 13:59:38 +01001740static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1741 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Takashi Iwai877211f2005-11-17 13:59:38 +01001743 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001745 snd_pcm_known_rates.count,
1746 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
Takashi Iwai877211f2005-11-17 13:59:38 +01001749static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1750 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Takashi Iwai877211f2005-11-17 13:59:38 +01001752 struct snd_interval t;
1753 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 t.min = 0;
1755 t.max = substream->buffer_bytes_max;
1756 t.openmin = 0;
1757 t.openmax = 0;
1758 t.integer = 1;
1759 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1760}
1761
Takashi Iwai877211f2005-11-17 13:59:38 +01001762int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Takashi Iwai877211f2005-11-17 13:59:38 +01001764 struct snd_pcm_runtime *runtime = substream->runtime;
1765 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 int k, err;
1767
1768 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1769 snd_mask_any(constrs_mask(constrs, k));
1770 }
1771
1772 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1773 snd_interval_any(constrs_interval(constrs, k));
1774 }
1775
1776 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1777 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1778 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1779 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1780 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1781
1782 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1783 snd_pcm_hw_rule_format, NULL,
1784 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1785 if (err < 0)
1786 return err;
1787 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1788 snd_pcm_hw_rule_sample_bits, NULL,
1789 SNDRV_PCM_HW_PARAM_FORMAT,
1790 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1791 if (err < 0)
1792 return err;
1793 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1794 snd_pcm_hw_rule_div, NULL,
1795 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1796 if (err < 0)
1797 return err;
1798 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1799 snd_pcm_hw_rule_mul, NULL,
1800 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1801 if (err < 0)
1802 return err;
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1804 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1805 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1806 if (err < 0)
1807 return err;
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1810 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1811 if (err < 0)
1812 return err;
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1814 snd_pcm_hw_rule_div, NULL,
1815 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1816 if (err < 0)
1817 return err;
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1819 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1820 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1824 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1825 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1829 snd_pcm_hw_rule_div, NULL,
1830 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1834 snd_pcm_hw_rule_div, NULL,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1839 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1840 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1845 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1849 snd_pcm_hw_rule_mul, NULL,
1850 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1855 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1860 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1864 snd_pcm_hw_rule_muldivk, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1869 snd_pcm_hw_rule_muldivk, (void*) 8,
1870 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1879 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1881 if (err < 0)
1882 return err;
1883 return 0;
1884}
1885
Takashi Iwai877211f2005-11-17 13:59:38 +01001886int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Takashi Iwai877211f2005-11-17 13:59:38 +01001888 struct snd_pcm_runtime *runtime = substream->runtime;
1889 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int err;
1891 unsigned int mask = 0;
1892
1893 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1894 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1895 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1896 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1897 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1898 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1899 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1900 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1901 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1902 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1903 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1904 }
1905 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001906 if (err < 0)
1907 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001910 if (err < 0)
1911 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001914 if (err < 0)
1915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1918 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001919 if (err < 0)
1920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1923 hw->rate_min, hw->rate_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001924 if (err < 0)
1925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1928 hw->period_bytes_min, hw->period_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001929 if (err < 0)
1930 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1933 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001934 if (err < 0)
1935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1938 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001939 if (err < 0)
1940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1943 snd_pcm_hw_rule_buffer_bytes_max, substream,
1944 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1945 if (err < 0)
1946 return err;
1947
1948 /* FIXME: remove */
1949 if (runtime->dma_bytes) {
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001951 if (err < 0)
1952 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954
1955 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1956 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1957 snd_pcm_hw_rule_rate, hw,
1958 SNDRV_PCM_HW_PARAM_RATE, -1);
1959 if (err < 0)
1960 return err;
1961 }
1962
1963 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1965
1966 return 0;
1967}
1968
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001969static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001972}
1973
1974void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1975{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001976 substream->ref_count--;
1977 if (substream->ref_count > 0)
1978 return;
1979
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001980 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001981 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (substream->ops->hw_free != NULL)
1983 substream->ops->hw_free(substream);
1984 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001985 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
Takashi Iwai15762742006-04-06 19:47:42 +02001987 if (substream->pcm_release) {
1988 substream->pcm_release(substream);
1989 substream->pcm_release = NULL;
1990 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001991 snd_pcm_detach_substream(substream);
1992}
1993
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001994EXPORT_SYMBOL(snd_pcm_release_substream);
1995
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001996int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1997 struct file *file,
1998 struct snd_pcm_substream **rsubstream)
1999{
2000 struct snd_pcm_substream *substream;
2001 int err;
2002
2003 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2004 if (err < 0)
2005 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002006 if (substream->ref_count > 1) {
2007 *rsubstream = substream;
2008 return 0;
2009 }
2010
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002011 err = snd_pcm_hw_constraints_init(substream);
2012 if (err < 0) {
2013 snd_printd("snd_pcm_hw_constraints_init failed\n");
2014 goto error;
2015 }
2016
2017 if ((err = substream->ops->open(substream)) < 0)
2018 goto error;
2019
2020 substream->hw_opened = 1;
2021
2022 err = snd_pcm_hw_constraints_complete(substream);
2023 if (err < 0) {
2024 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2025 goto error;
2026 }
2027
2028 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002030
2031 error:
2032 snd_pcm_release_substream(substream);
2033 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002036EXPORT_SYMBOL(snd_pcm_open_substream);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002039 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002041 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Takashi Iwai877211f2005-11-17 13:59:38 +01002043 struct snd_pcm_file *pcm_file;
2044 struct snd_pcm_substream *substream;
2045 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002046 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002048 if (rpcm_file)
2049 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002051 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2052 if (err < 0)
2053 return err;
2054
Takashi Iwai548a6482006-07-31 16:51:51 +02002055 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2056 if (pcm_file == NULL) {
2057 snd_pcm_release_substream(substream);
2058 return -ENOMEM;
2059 }
2060 pcm_file->substream = substream;
2061 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002062 str = substream->pstr;
2063 substream->file = pcm_file;
2064 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002067 if (rpcm_file)
2068 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 0;
2070}
2071
Clemens Ladischf87135f2005-11-20 14:06:59 +01002072static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
Takashi Iwai877211f2005-11-17 13:59:38 +01002074 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002075
2076 pcm = snd_lookup_minor_data(iminor(inode),
2077 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2078 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2079}
2080
2081static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2082{
2083 struct snd_pcm *pcm;
2084
2085 pcm = snd_lookup_minor_data(iminor(inode),
2086 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2087 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2088}
2089
2090static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2091{
2092 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002093 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 wait_queue_t wait;
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (pcm == NULL) {
2097 err = -ENODEV;
2098 goto __error1;
2099 }
2100 err = snd_card_file_add(pcm->card, file);
2101 if (err < 0)
2102 goto __error1;
2103 if (!try_module_get(pcm->card->module)) {
2104 err = -EFAULT;
2105 goto __error2;
2106 }
2107 init_waitqueue_entry(&wait, current);
2108 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002109 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002111 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (err >= 0)
2113 break;
2114 if (err == -EAGAIN) {
2115 if (file->f_flags & O_NONBLOCK) {
2116 err = -EBUSY;
2117 break;
2118 }
2119 } else
2120 break;
2121 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002122 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002124 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (signal_pending(current)) {
2126 err = -ERESTARTSYS;
2127 break;
2128 }
2129 }
2130 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002131 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (err < 0)
2133 goto __error;
2134 return err;
2135
2136 __error:
2137 module_put(pcm->card->module);
2138 __error2:
2139 snd_card_file_remove(pcm->card, file);
2140 __error1:
2141 return err;
2142}
2143
2144static int snd_pcm_release(struct inode *inode, struct file *file)
2145{
Takashi Iwai877211f2005-11-17 13:59:38 +01002146 struct snd_pcm *pcm;
2147 struct snd_pcm_substream *substream;
2148 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 pcm_file = file->private_data;
2151 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002152 if (snd_BUG_ON(!substream))
2153 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002155 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002156 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002157 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002158 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 wake_up(&pcm->open_wait);
2160 module_put(pcm->card->module);
2161 snd_card_file_remove(pcm->card, file);
2162 return 0;
2163}
2164
Takashi Iwai877211f2005-11-17 13:59:38 +01002165static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2166 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Takashi Iwai877211f2005-11-17 13:59:38 +01002168 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 snd_pcm_sframes_t appl_ptr;
2170 snd_pcm_sframes_t ret;
2171 snd_pcm_sframes_t hw_avail;
2172
2173 if (frames == 0)
2174 return 0;
2175
2176 snd_pcm_stream_lock_irq(substream);
2177 switch (runtime->status->state) {
2178 case SNDRV_PCM_STATE_PREPARED:
2179 break;
2180 case SNDRV_PCM_STATE_DRAINING:
2181 case SNDRV_PCM_STATE_RUNNING:
2182 if (snd_pcm_update_hw_ptr(substream) >= 0)
2183 break;
2184 /* Fall through */
2185 case SNDRV_PCM_STATE_XRUN:
2186 ret = -EPIPE;
2187 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002188 case SNDRV_PCM_STATE_SUSPENDED:
2189 ret = -ESTRPIPE;
2190 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 default:
2192 ret = -EBADFD;
2193 goto __end;
2194 }
2195
2196 hw_avail = snd_pcm_playback_hw_avail(runtime);
2197 if (hw_avail <= 0) {
2198 ret = 0;
2199 goto __end;
2200 }
2201 if (frames > (snd_pcm_uframes_t)hw_avail)
2202 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 appl_ptr = runtime->control->appl_ptr - frames;
2204 if (appl_ptr < 0)
2205 appl_ptr += runtime->boundary;
2206 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ret = frames;
2208 __end:
2209 snd_pcm_stream_unlock_irq(substream);
2210 return ret;
2211}
2212
Takashi Iwai877211f2005-11-17 13:59:38 +01002213static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2214 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Takashi Iwai877211f2005-11-17 13:59:38 +01002216 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 snd_pcm_sframes_t appl_ptr;
2218 snd_pcm_sframes_t ret;
2219 snd_pcm_sframes_t hw_avail;
2220
2221 if (frames == 0)
2222 return 0;
2223
2224 snd_pcm_stream_lock_irq(substream);
2225 switch (runtime->status->state) {
2226 case SNDRV_PCM_STATE_PREPARED:
2227 case SNDRV_PCM_STATE_DRAINING:
2228 break;
2229 case SNDRV_PCM_STATE_RUNNING:
2230 if (snd_pcm_update_hw_ptr(substream) >= 0)
2231 break;
2232 /* Fall through */
2233 case SNDRV_PCM_STATE_XRUN:
2234 ret = -EPIPE;
2235 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002236 case SNDRV_PCM_STATE_SUSPENDED:
2237 ret = -ESTRPIPE;
2238 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 default:
2240 ret = -EBADFD;
2241 goto __end;
2242 }
2243
2244 hw_avail = snd_pcm_capture_hw_avail(runtime);
2245 if (hw_avail <= 0) {
2246 ret = 0;
2247 goto __end;
2248 }
2249 if (frames > (snd_pcm_uframes_t)hw_avail)
2250 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 appl_ptr = runtime->control->appl_ptr - frames;
2252 if (appl_ptr < 0)
2253 appl_ptr += runtime->boundary;
2254 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 ret = frames;
2256 __end:
2257 snd_pcm_stream_unlock_irq(substream);
2258 return ret;
2259}
2260
Takashi Iwai877211f2005-11-17 13:59:38 +01002261static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2262 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Takashi Iwai877211f2005-11-17 13:59:38 +01002264 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 snd_pcm_sframes_t appl_ptr;
2266 snd_pcm_sframes_t ret;
2267 snd_pcm_sframes_t avail;
2268
2269 if (frames == 0)
2270 return 0;
2271
2272 snd_pcm_stream_lock_irq(substream);
2273 switch (runtime->status->state) {
2274 case SNDRV_PCM_STATE_PREPARED:
2275 case SNDRV_PCM_STATE_PAUSED:
2276 break;
2277 case SNDRV_PCM_STATE_DRAINING:
2278 case SNDRV_PCM_STATE_RUNNING:
2279 if (snd_pcm_update_hw_ptr(substream) >= 0)
2280 break;
2281 /* Fall through */
2282 case SNDRV_PCM_STATE_XRUN:
2283 ret = -EPIPE;
2284 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002285 case SNDRV_PCM_STATE_SUSPENDED:
2286 ret = -ESTRPIPE;
2287 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 default:
2289 ret = -EBADFD;
2290 goto __end;
2291 }
2292
2293 avail = snd_pcm_playback_avail(runtime);
2294 if (avail <= 0) {
2295 ret = 0;
2296 goto __end;
2297 }
2298 if (frames > (snd_pcm_uframes_t)avail)
2299 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 appl_ptr = runtime->control->appl_ptr + frames;
2301 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2302 appl_ptr -= runtime->boundary;
2303 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 ret = frames;
2305 __end:
2306 snd_pcm_stream_unlock_irq(substream);
2307 return ret;
2308}
2309
Takashi Iwai877211f2005-11-17 13:59:38 +01002310static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2311 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312{
Takashi Iwai877211f2005-11-17 13:59:38 +01002313 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 snd_pcm_sframes_t appl_ptr;
2315 snd_pcm_sframes_t ret;
2316 snd_pcm_sframes_t avail;
2317
2318 if (frames == 0)
2319 return 0;
2320
2321 snd_pcm_stream_lock_irq(substream);
2322 switch (runtime->status->state) {
2323 case SNDRV_PCM_STATE_PREPARED:
2324 case SNDRV_PCM_STATE_DRAINING:
2325 case SNDRV_PCM_STATE_PAUSED:
2326 break;
2327 case SNDRV_PCM_STATE_RUNNING:
2328 if (snd_pcm_update_hw_ptr(substream) >= 0)
2329 break;
2330 /* Fall through */
2331 case SNDRV_PCM_STATE_XRUN:
2332 ret = -EPIPE;
2333 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002334 case SNDRV_PCM_STATE_SUSPENDED:
2335 ret = -ESTRPIPE;
2336 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 default:
2338 ret = -EBADFD;
2339 goto __end;
2340 }
2341
2342 avail = snd_pcm_capture_avail(runtime);
2343 if (avail <= 0) {
2344 ret = 0;
2345 goto __end;
2346 }
2347 if (frames > (snd_pcm_uframes_t)avail)
2348 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 appl_ptr = runtime->control->appl_ptr + frames;
2350 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2351 appl_ptr -= runtime->boundary;
2352 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 ret = frames;
2354 __end:
2355 snd_pcm_stream_unlock_irq(substream);
2356 return ret;
2357}
2358
Takashi Iwai877211f2005-11-17 13:59:38 +01002359static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Takashi Iwai877211f2005-11-17 13:59:38 +01002361 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 int err;
2363
2364 snd_pcm_stream_lock_irq(substream);
2365 switch (runtime->status->state) {
2366 case SNDRV_PCM_STATE_DRAINING:
2367 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2368 goto __badfd;
2369 case SNDRV_PCM_STATE_RUNNING:
2370 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2371 break;
2372 /* Fall through */
2373 case SNDRV_PCM_STATE_PREPARED:
2374 case SNDRV_PCM_STATE_SUSPENDED:
2375 err = 0;
2376 break;
2377 case SNDRV_PCM_STATE_XRUN:
2378 err = -EPIPE;
2379 break;
2380 default:
2381 __badfd:
2382 err = -EBADFD;
2383 break;
2384 }
2385 snd_pcm_stream_unlock_irq(substream);
2386 return err;
2387}
2388
Takashi Iwai877211f2005-11-17 13:59:38 +01002389static int snd_pcm_delay(struct snd_pcm_substream *substream,
2390 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Takashi Iwai877211f2005-11-17 13:59:38 +01002392 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 int err;
2394 snd_pcm_sframes_t n = 0;
2395
2396 snd_pcm_stream_lock_irq(substream);
2397 switch (runtime->status->state) {
2398 case SNDRV_PCM_STATE_DRAINING:
2399 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2400 goto __badfd;
2401 case SNDRV_PCM_STATE_RUNNING:
2402 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2403 break;
2404 /* Fall through */
2405 case SNDRV_PCM_STATE_PREPARED:
2406 case SNDRV_PCM_STATE_SUSPENDED:
2407 err = 0;
2408 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2409 n = snd_pcm_playback_hw_avail(runtime);
2410 else
2411 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002412 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
2414 case SNDRV_PCM_STATE_XRUN:
2415 err = -EPIPE;
2416 break;
2417 default:
2418 __badfd:
2419 err = -EBADFD;
2420 break;
2421 }
2422 snd_pcm_stream_unlock_irq(substream);
2423 if (!err)
2424 if (put_user(n, res))
2425 err = -EFAULT;
2426 return err;
2427}
2428
Takashi Iwai877211f2005-11-17 13:59:38 +01002429static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2430 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Takashi Iwai877211f2005-11-17 13:59:38 +01002432 struct snd_pcm_runtime *runtime = substream->runtime;
2433 struct snd_pcm_sync_ptr sync_ptr;
2434 volatile struct snd_pcm_mmap_status *status;
2435 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int err;
2437
2438 memset(&sync_ptr, 0, sizeof(sync_ptr));
2439 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2440 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002441 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 -07002442 return -EFAULT;
2443 status = runtime->status;
2444 control = runtime->control;
2445 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2446 err = snd_pcm_hwsync(substream);
2447 if (err < 0)
2448 return err;
2449 }
2450 snd_pcm_stream_lock_irq(substream);
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2452 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2453 else
2454 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2455 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2456 control->avail_min = sync_ptr.c.control.avail_min;
2457 else
2458 sync_ptr.c.control.avail_min = control->avail_min;
2459 sync_ptr.s.status.state = status->state;
2460 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2461 sync_ptr.s.status.tstamp = status->tstamp;
2462 sync_ptr.s.status.suspended_state = status->suspended_state;
2463 snd_pcm_stream_unlock_irq(substream);
2464 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2465 return -EFAULT;
2466 return 0;
2467}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002468
2469static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2470{
2471 struct snd_pcm_runtime *runtime = substream->runtime;
2472 int arg;
2473
2474 if (get_user(arg, _arg))
2475 return -EFAULT;
2476 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2477 return -EINVAL;
2478 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2479 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2480 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2481 return 0;
2482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Takashi Iwai0df63e42006-04-28 15:13:41 +02002484static int snd_pcm_common_ioctl1(struct file *file,
2485 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 unsigned int cmd, void __user *arg)
2487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 switch (cmd) {
2489 case SNDRV_PCM_IOCTL_PVERSION:
2490 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2491 case SNDRV_PCM_IOCTL_INFO:
2492 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002493 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2494 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002495 case SNDRV_PCM_IOCTL_TTSTAMP:
2496 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 case SNDRV_PCM_IOCTL_HW_REFINE:
2498 return snd_pcm_hw_refine_user(substream, arg);
2499 case SNDRV_PCM_IOCTL_HW_PARAMS:
2500 return snd_pcm_hw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_HW_FREE:
2502 return snd_pcm_hw_free(substream);
2503 case SNDRV_PCM_IOCTL_SW_PARAMS:
2504 return snd_pcm_sw_params_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_STATUS:
2506 return snd_pcm_status_user(substream, arg);
2507 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2508 return snd_pcm_channel_info_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002510 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 case SNDRV_PCM_IOCTL_RESET:
2512 return snd_pcm_reset(substream);
2513 case SNDRV_PCM_IOCTL_START:
2514 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2515 case SNDRV_PCM_IOCTL_LINK:
2516 return snd_pcm_link(substream, (int)(unsigned long) arg);
2517 case SNDRV_PCM_IOCTL_UNLINK:
2518 return snd_pcm_unlink(substream);
2519 case SNDRV_PCM_IOCTL_RESUME:
2520 return snd_pcm_resume(substream);
2521 case SNDRV_PCM_IOCTL_XRUN:
2522 return snd_pcm_xrun(substream);
2523 case SNDRV_PCM_IOCTL_HWSYNC:
2524 return snd_pcm_hwsync(substream);
2525 case SNDRV_PCM_IOCTL_DELAY:
2526 return snd_pcm_delay(substream, arg);
2527 case SNDRV_PCM_IOCTL_SYNC_PTR:
2528 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002529#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2531 return snd_pcm_hw_refine_old_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2533 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002534#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002536 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 case SNDRV_PCM_IOCTL_DROP:
2538 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002539 case SNDRV_PCM_IOCTL_PAUSE:
2540 {
2541 int res;
2542 snd_pcm_stream_lock_irq(substream);
2543 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2544 snd_pcm_stream_unlock_irq(substream);
2545 return res;
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 snd_printd("unknown ioctl = 0x%x\n", cmd);
2549 return -ENOTTY;
2550}
2551
Takashi Iwai0df63e42006-04-28 15:13:41 +02002552static int snd_pcm_playback_ioctl1(struct file *file,
2553 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 unsigned int cmd, void __user *arg)
2555{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002556 if (snd_BUG_ON(!substream))
2557 return -ENXIO;
2558 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 switch (cmd) {
2561 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2562 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002563 struct snd_xferi xferi;
2564 struct snd_xferi __user *_xferi = arg;
2565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 snd_pcm_sframes_t result;
2567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2568 return -EBADFD;
2569 if (put_user(0, &_xferi->result))
2570 return -EFAULT;
2571 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2572 return -EFAULT;
2573 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2574 __put_user(result, &_xferi->result);
2575 return result < 0 ? result : 0;
2576 }
2577 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2578 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002579 struct snd_xfern xfern;
2580 struct snd_xfern __user *_xfern = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 void __user **bufs;
2583 snd_pcm_sframes_t result;
2584 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2585 return -EBADFD;
2586 if (runtime->channels > 128)
2587 return -EINVAL;
2588 if (put_user(0, &_xfern->result))
2589 return -EFAULT;
2590 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2591 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002592
2593 bufs = memdup_user(xfern.bufs,
2594 sizeof(void *) * runtime->channels);
2595 if (IS_ERR(bufs))
2596 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2598 kfree(bufs);
2599 __put_user(result, &_xfern->result);
2600 return result < 0 ? result : 0;
2601 }
2602 case SNDRV_PCM_IOCTL_REWIND:
2603 {
2604 snd_pcm_uframes_t frames;
2605 snd_pcm_uframes_t __user *_frames = arg;
2606 snd_pcm_sframes_t result;
2607 if (get_user(frames, _frames))
2608 return -EFAULT;
2609 if (put_user(0, _frames))
2610 return -EFAULT;
2611 result = snd_pcm_playback_rewind(substream, frames);
2612 __put_user(result, _frames);
2613 return result < 0 ? result : 0;
2614 }
2615 case SNDRV_PCM_IOCTL_FORWARD:
2616 {
2617 snd_pcm_uframes_t frames;
2618 snd_pcm_uframes_t __user *_frames = arg;
2619 snd_pcm_sframes_t result;
2620 if (get_user(frames, _frames))
2621 return -EFAULT;
2622 if (put_user(0, _frames))
2623 return -EFAULT;
2624 result = snd_pcm_playback_forward(substream, frames);
2625 __put_user(result, _frames);
2626 return result < 0 ? result : 0;
2627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002629 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Takashi Iwai0df63e42006-04-28 15:13:41 +02002632static int snd_pcm_capture_ioctl1(struct file *file,
2633 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 unsigned int cmd, void __user *arg)
2635{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002636 if (snd_BUG_ON(!substream))
2637 return -ENXIO;
2638 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 switch (cmd) {
2641 case SNDRV_PCM_IOCTL_READI_FRAMES:
2642 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002643 struct snd_xferi xferi;
2644 struct snd_xferi __user *_xferi = arg;
2645 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 snd_pcm_sframes_t result;
2647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2648 return -EBADFD;
2649 if (put_user(0, &_xferi->result))
2650 return -EFAULT;
2651 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2652 return -EFAULT;
2653 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2654 __put_user(result, &_xferi->result);
2655 return result < 0 ? result : 0;
2656 }
2657 case SNDRV_PCM_IOCTL_READN_FRAMES:
2658 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002659 struct snd_xfern xfern;
2660 struct snd_xfern __user *_xfern = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 void *bufs;
2663 snd_pcm_sframes_t result;
2664 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2665 return -EBADFD;
2666 if (runtime->channels > 128)
2667 return -EINVAL;
2668 if (put_user(0, &_xfern->result))
2669 return -EFAULT;
2670 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2671 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002672
2673 bufs = memdup_user(xfern.bufs,
2674 sizeof(void *) * runtime->channels);
2675 if (IS_ERR(bufs))
2676 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2678 kfree(bufs);
2679 __put_user(result, &_xfern->result);
2680 return result < 0 ? result : 0;
2681 }
2682 case SNDRV_PCM_IOCTL_REWIND:
2683 {
2684 snd_pcm_uframes_t frames;
2685 snd_pcm_uframes_t __user *_frames = arg;
2686 snd_pcm_sframes_t result;
2687 if (get_user(frames, _frames))
2688 return -EFAULT;
2689 if (put_user(0, _frames))
2690 return -EFAULT;
2691 result = snd_pcm_capture_rewind(substream, frames);
2692 __put_user(result, _frames);
2693 return result < 0 ? result : 0;
2694 }
2695 case SNDRV_PCM_IOCTL_FORWARD:
2696 {
2697 snd_pcm_uframes_t frames;
2698 snd_pcm_uframes_t __user *_frames = arg;
2699 snd_pcm_sframes_t result;
2700 if (get_user(frames, _frames))
2701 return -EFAULT;
2702 if (put_user(0, _frames))
2703 return -EFAULT;
2704 result = snd_pcm_capture_forward(substream, frames);
2705 __put_user(result, _frames);
2706 return result < 0 ? result : 0;
2707 }
2708 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002709 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Takashi Iwai877211f2005-11-17 13:59:38 +01002712static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2713 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Takashi Iwai877211f2005-11-17 13:59:38 +01002715 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 pcm_file = file->private_data;
2718
2719 if (((cmd >> 8) & 0xff) != 'A')
2720 return -ENOTTY;
2721
Takashi Iwai0df63e42006-04-28 15:13:41 +02002722 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2723 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Takashi Iwai877211f2005-11-17 13:59:38 +01002726static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2727 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Takashi Iwai877211f2005-11-17 13:59:38 +01002729 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 pcm_file = file->private_data;
2732
2733 if (((cmd >> 8) & 0xff) != 'A')
2734 return -ENOTTY;
2735
Takashi Iwai0df63e42006-04-28 15:13:41 +02002736 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2737 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Takashi Iwai877211f2005-11-17 13:59:38 +01002740int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 unsigned int cmd, void *arg)
2742{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002743 mm_segment_t fs;
2744 int result;
2745
2746 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 switch (substream->stream) {
2748 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002749 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2750 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002753 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2754 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002757 result = -EINVAL;
2758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002760 snd_leave_user(fs);
2761 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002764EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2765
Takashi Iwai877211f2005-11-17 13:59:38 +01002766static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2767 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Takashi Iwai877211f2005-11-17 13:59:38 +01002769 struct snd_pcm_file *pcm_file;
2770 struct snd_pcm_substream *substream;
2771 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 snd_pcm_sframes_t result;
2773
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002776 if (PCM_RUNTIME_CHECK(substream))
2777 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 runtime = substream->runtime;
2779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2780 return -EBADFD;
2781 if (!frame_aligned(runtime, count))
2782 return -EINVAL;
2783 count = bytes_to_frames(runtime, count);
2784 result = snd_pcm_lib_read(substream, buf, count);
2785 if (result > 0)
2786 result = frames_to_bytes(runtime, result);
2787 return result;
2788}
2789
Takashi Iwai877211f2005-11-17 13:59:38 +01002790static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2791 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Takashi Iwai877211f2005-11-17 13:59:38 +01002793 struct snd_pcm_file *pcm_file;
2794 struct snd_pcm_substream *substream;
2795 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 snd_pcm_sframes_t result;
2797
2798 pcm_file = file->private_data;
2799 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002800 if (PCM_RUNTIME_CHECK(substream))
2801 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002803 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2804 return -EBADFD;
2805 if (!frame_aligned(runtime, count))
2806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 count = bytes_to_frames(runtime, count);
2808 result = snd_pcm_lib_write(substream, buf, count);
2809 if (result > 0)
2810 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return result;
2812}
2813
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002814static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2815 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817{
Takashi Iwai877211f2005-11-17 13:59:38 +01002818 struct snd_pcm_file *pcm_file;
2819 struct snd_pcm_substream *substream;
2820 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 snd_pcm_sframes_t result;
2822 unsigned long i;
2823 void __user **bufs;
2824 snd_pcm_uframes_t frames;
2825
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002826 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002828 if (PCM_RUNTIME_CHECK(substream))
2829 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 runtime = substream->runtime;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2832 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002833 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002835 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002837 frames = bytes_to_samples(runtime, iov->iov_len);
2838 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (bufs == NULL)
2840 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002841 for (i = 0; i < nr_segs; ++i)
2842 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 result = snd_pcm_lib_readv(substream, bufs, frames);
2844 if (result > 0)
2845 result = frames_to_bytes(runtime, result);
2846 kfree(bufs);
2847 return result;
2848}
2849
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002850static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2851 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Takashi Iwai877211f2005-11-17 13:59:38 +01002853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 snd_pcm_sframes_t result;
2857 unsigned long i;
2858 void __user **bufs;
2859 snd_pcm_uframes_t frames;
2860
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002861 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002863 if (PCM_RUNTIME_CHECK(substream))
2864 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002869 !frame_aligned(runtime, iov->iov_len))
2870 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002871 frames = bytes_to_samples(runtime, iov->iov_len);
2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (bufs == NULL)
2874 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002875 for (i = 0; i < nr_segs; ++i)
2876 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 result = snd_pcm_lib_writev(substream, bufs, frames);
2878 if (result > 0)
2879 result = frames_to_bytes(runtime, result);
2880 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return result;
2882}
2883
2884static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2885{
Takashi Iwai877211f2005-11-17 13:59:38 +01002886 struct snd_pcm_file *pcm_file;
2887 struct snd_pcm_substream *substream;
2888 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 unsigned int mask;
2890 snd_pcm_uframes_t avail;
2891
2892 pcm_file = file->private_data;
2893
2894 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002895 if (PCM_RUNTIME_CHECK(substream))
2896 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 runtime = substream->runtime;
2898
2899 poll_wait(file, &runtime->sleep, wait);
2900
2901 snd_pcm_stream_lock_irq(substream);
2902 avail = snd_pcm_playback_avail(runtime);
2903 switch (runtime->status->state) {
2904 case SNDRV_PCM_STATE_RUNNING:
2905 case SNDRV_PCM_STATE_PREPARED:
2906 case SNDRV_PCM_STATE_PAUSED:
2907 if (avail >= runtime->control->avail_min) {
2908 mask = POLLOUT | POLLWRNORM;
2909 break;
2910 }
2911 /* Fall through */
2912 case SNDRV_PCM_STATE_DRAINING:
2913 mask = 0;
2914 break;
2915 default:
2916 mask = POLLOUT | POLLWRNORM | POLLERR;
2917 break;
2918 }
2919 snd_pcm_stream_unlock_irq(substream);
2920 return mask;
2921}
2922
2923static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2924{
Takashi Iwai877211f2005-11-17 13:59:38 +01002925 struct snd_pcm_file *pcm_file;
2926 struct snd_pcm_substream *substream;
2927 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned int mask;
2929 snd_pcm_uframes_t avail;
2930
2931 pcm_file = file->private_data;
2932
2933 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002934 if (PCM_RUNTIME_CHECK(substream))
2935 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 runtime = substream->runtime;
2937
2938 poll_wait(file, &runtime->sleep, wait);
2939
2940 snd_pcm_stream_lock_irq(substream);
2941 avail = snd_pcm_capture_avail(runtime);
2942 switch (runtime->status->state) {
2943 case SNDRV_PCM_STATE_RUNNING:
2944 case SNDRV_PCM_STATE_PREPARED:
2945 case SNDRV_PCM_STATE_PAUSED:
2946 if (avail >= runtime->control->avail_min) {
2947 mask = POLLIN | POLLRDNORM;
2948 break;
2949 }
2950 mask = 0;
2951 break;
2952 case SNDRV_PCM_STATE_DRAINING:
2953 if (avail > 0) {
2954 mask = POLLIN | POLLRDNORM;
2955 break;
2956 }
2957 /* Fall through */
2958 default:
2959 mask = POLLIN | POLLRDNORM | POLLERR;
2960 break;
2961 }
2962 snd_pcm_stream_unlock_irq(substream);
2963 return mask;
2964}
2965
2966/*
2967 * mmap support
2968 */
2969
2970/*
2971 * Only on coherent architectures, we can mmap the status and the control records
2972 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2973 */
2974#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2975/*
2976 * mmap status record
2977 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002978static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2979 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Takashi Iwai877211f2005-11-17 13:59:38 +01002981 struct snd_pcm_substream *substream = area->vm_private_data;
2982 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002985 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002987 vmf->page = virt_to_page(runtime->status);
2988 get_page(vmf->page);
2989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002992static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002994 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995};
2996
Takashi Iwai877211f2005-11-17 13:59:38 +01002997static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct vm_area_struct *area)
2999{
Takashi Iwai877211f2005-11-17 13:59:38 +01003000 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 long size;
3002 if (!(area->vm_flags & VM_READ))
3003 return -EINVAL;
3004 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003006 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return -EINVAL;
3008 area->vm_ops = &snd_pcm_vm_ops_status;
3009 area->vm_private_data = substream;
3010 area->vm_flags |= VM_RESERVED;
3011 return 0;
3012}
3013
3014/*
3015 * mmap control record
3016 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003017static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3018 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Takashi Iwai877211f2005-11-17 13:59:38 +01003020 struct snd_pcm_substream *substream = area->vm_private_data;
3021 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003024 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003026 vmf->page = virt_to_page(runtime->control);
3027 get_page(vmf->page);
3028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003031static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003033 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034};
3035
Takashi Iwai877211f2005-11-17 13:59:38 +01003036static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct vm_area_struct *area)
3038{
Takashi Iwai877211f2005-11-17 13:59:38 +01003039 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 long size;
3041 if (!(area->vm_flags & VM_READ))
3042 return -EINVAL;
3043 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003045 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return -EINVAL;
3047 area->vm_ops = &snd_pcm_vm_ops_control;
3048 area->vm_private_data = substream;
3049 area->vm_flags |= VM_RESERVED;
3050 return 0;
3051}
3052#else /* ! coherent mmap */
3053/*
3054 * don't support mmap for status and control records.
3055 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003056static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct vm_area_struct *area)
3058{
3059 return -ENXIO;
3060}
Takashi Iwai877211f2005-11-17 13:59:38 +01003061static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct vm_area_struct *area)
3063{
3064 return -ENXIO;
3065}
3066#endif /* coherent mmap */
3067
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003068static inline struct page *
3069snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3070{
3071 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003072#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3073 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3074 return virt_to_page(CAC_ADDR(vaddr));
3075#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003076#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3077 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3078 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3079 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3080 /* assume dma_handle set via pfn_to_phys() in
3081 * mm/dma-noncoherent.c
3082 */
3083 return pfn_to_page(addr >> PAGE_SHIFT);
3084 }
3085#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003086 return virt_to_page(vaddr);
3087}
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003090 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003092static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3093 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Takashi Iwai877211f2005-11-17 13:59:38 +01003095 struct snd_pcm_substream *substream = area->vm_private_data;
3096 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 unsigned long offset;
3098 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 size_t dma_bytes;
3100
3101 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003102 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3106 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003107 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003108 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003110 else
3111 page = snd_pcm_default_page_ops(substream, offset);
3112 if (!page)
3113 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003114 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003115 vmf->page = page;
3116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
3118
Takashi Iwai657b1982009-11-26 12:40:21 +01003119static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3120 .open = snd_pcm_mmap_data_open,
3121 .close = snd_pcm_mmap_data_close,
3122};
3123
3124static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 .open = snd_pcm_mmap_data_open,
3126 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003127 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128};
3129
Takashi Iwai657b1982009-11-26 12:40:21 +01003130#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3131/* This should be defined / handled globally! */
3132#ifdef CONFIG_ARM
3133#define ARCH_HAS_DMA_MMAP_COHERENT
3134#endif
3135#endif
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/*
3138 * mmap the DMA buffer on RAM
3139 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003140static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3141 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003144#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3145 if (!substream->ops->page &&
3146 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3147 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3148 area,
3149 substream->runtime->dma_area,
3150 substream->runtime->dma_addr,
3151 area->vm_end - area->vm_start);
3152#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3153 /* mmap with fault handler */
3154 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 return 0;
3156}
3157
3158/*
3159 * mmap the DMA buffer on I/O memory area
3160 */
3161#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003162int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3163 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
3165 long size;
3166 unsigned long offset;
3167
3168#ifdef pgprot_noncached
3169 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3170#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 area->vm_flags |= VM_IO;
3172 size = area->vm_end - area->vm_start;
3173 offset = area->vm_pgoff << PAGE_SHIFT;
3174 if (io_remap_pfn_range(area, area->vm_start,
3175 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3176 size, area->vm_page_prot))
3177 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return 0;
3179}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003180
3181EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182#endif /* SNDRV_PCM_INFO_MMAP */
3183
3184/*
3185 * mmap DMA buffer
3186 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003187int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 struct vm_area_struct *area)
3189{
Takashi Iwai877211f2005-11-17 13:59:38 +01003190 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 long size;
3192 unsigned long offset;
3193 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003194 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3197 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3198 return -EINVAL;
3199 } else {
3200 if (!(area->vm_flags & VM_READ))
3201 return -EINVAL;
3202 }
3203 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3205 return -EBADFD;
3206 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3207 return -ENXIO;
3208 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3209 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3210 return -EINVAL;
3211 size = area->vm_end - area->vm_start;
3212 offset = area->vm_pgoff << PAGE_SHIFT;
3213 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3214 if ((size_t)size > dma_bytes)
3215 return -EINVAL;
3216 if (offset > dma_bytes - size)
3217 return -EINVAL;
3218
Takashi Iwai657b1982009-11-26 12:40:21 +01003219 area->vm_ops = &snd_pcm_vm_ops_data;
3220 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003222 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003224 err = snd_pcm_default_mmap(substream, area);
3225 if (!err)
3226 atomic_inc(&substream->mmap_count);
3227 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003230EXPORT_SYMBOL(snd_pcm_mmap_data);
3231
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3233{
Takashi Iwai877211f2005-11-17 13:59:38 +01003234 struct snd_pcm_file * pcm_file;
3235 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 unsigned long offset;
3237
3238 pcm_file = file->private_data;
3239 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003240 if (PCM_RUNTIME_CHECK(substream))
3241 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
3243 offset = area->vm_pgoff << PAGE_SHIFT;
3244 switch (offset) {
3245 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003246 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return -ENXIO;
3248 return snd_pcm_mmap_status(substream, file, area);
3249 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003250 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return -ENXIO;
3252 return snd_pcm_mmap_control(substream, file, area);
3253 default:
3254 return snd_pcm_mmap_data(substream, file, area);
3255 }
3256 return 0;
3257}
3258
3259static int snd_pcm_fasync(int fd, struct file * file, int on)
3260{
Takashi Iwai877211f2005-11-17 13:59:38 +01003261 struct snd_pcm_file * pcm_file;
3262 struct snd_pcm_substream *substream;
3263 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003264 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003266 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 pcm_file = file->private_data;
3268 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003269 if (PCM_RUNTIME_CHECK(substream))
3270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003273out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003274 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278/*
3279 * ioctl32 compat
3280 */
3281#ifdef CONFIG_COMPAT
3282#include "pcm_compat.c"
3283#else
3284#define snd_pcm_ioctl_compat NULL
3285#endif
3286
3287/*
3288 * To be removed helpers to keep binary compatibility
3289 */
3290
Takashi Iwai59d48582005-12-01 10:51:58 +01003291#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3293#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3294
Takashi Iwai877211f2005-11-17 13:59:38 +01003295static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3296 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
3298 unsigned int i;
3299
3300 memset(params, 0, sizeof(*params));
3301 params->flags = oparams->flags;
3302 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3303 params->masks[i].bits[0] = oparams->masks[i];
3304 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3305 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3306 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3307 params->info = oparams->info;
3308 params->msbits = oparams->msbits;
3309 params->rate_num = oparams->rate_num;
3310 params->rate_den = oparams->rate_den;
3311 params->fifo_size = oparams->fifo_size;
3312}
3313
Takashi Iwai877211f2005-11-17 13:59:38 +01003314static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3315 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
3317 unsigned int i;
3318
3319 memset(oparams, 0, sizeof(*oparams));
3320 oparams->flags = params->flags;
3321 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3322 oparams->masks[i] = params->masks[i].bits[0];
3323 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3324 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3325 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3326 oparams->info = params->info;
3327 oparams->msbits = params->msbits;
3328 oparams->rate_num = params->rate_num;
3329 oparams->rate_den = params->rate_den;
3330 oparams->fifo_size = params->fifo_size;
3331}
3332
Takashi Iwai877211f2005-11-17 13:59:38 +01003333static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3334 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Takashi Iwai877211f2005-11-17 13:59:38 +01003336 struct snd_pcm_hw_params *params;
3337 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 int err;
3339
3340 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003341 if (!params)
3342 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Li Zefanef44a1e2009-04-10 09:43:08 +08003344 oparams = memdup_user(_oparams, sizeof(*oparams));
3345 if (IS_ERR(oparams)) {
3346 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 goto out;
3348 }
3349 snd_pcm_hw_convert_from_old_params(params, oparams);
3350 err = snd_pcm_hw_refine(substream, params);
3351 snd_pcm_hw_convert_to_old_params(oparams, params);
3352 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3353 if (!err)
3354 err = -EFAULT;
3355 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003356
3357 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358out:
3359 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return err;
3361}
3362
Takashi Iwai877211f2005-11-17 13:59:38 +01003363static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3364 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
Takashi Iwai877211f2005-11-17 13:59:38 +01003366 struct snd_pcm_hw_params *params;
3367 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 int err;
3369
3370 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003371 if (!params)
3372 return -ENOMEM;
3373
3374 oparams = memdup_user(_oparams, sizeof(*oparams));
3375 if (IS_ERR(oparams)) {
3376 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 goto out;
3378 }
3379 snd_pcm_hw_convert_from_old_params(params, oparams);
3380 err = snd_pcm_hw_params(substream, params);
3381 snd_pcm_hw_convert_to_old_params(oparams, params);
3382 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3383 if (!err)
3384 err = -EFAULT;
3385 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003386
3387 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388out:
3389 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 return err;
3391}
Takashi Iwai59d48582005-12-01 10:51:58 +01003392#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Cliff Cai70036092008-09-03 10:54:36 +02003394#ifndef CONFIG_MMU
3395unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3396 unsigned long len, unsigned long pgoff,
3397 unsigned long flags)
3398{
3399 return 0;
3400}
3401#else
3402# define dummy_get_unmapped_area NULL
3403#endif
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405/*
3406 * Register section
3407 */
3408
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003409const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003411 .owner = THIS_MODULE,
3412 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003413 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003414 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003415 .release = snd_pcm_release,
3416 .poll = snd_pcm_playback_poll,
3417 .unlocked_ioctl = snd_pcm_playback_ioctl,
3418 .compat_ioctl = snd_pcm_ioctl_compat,
3419 .mmap = snd_pcm_mmap,
3420 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003421 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 },
3423 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003424 .owner = THIS_MODULE,
3425 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003426 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003427 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003428 .release = snd_pcm_release,
3429 .poll = snd_pcm_capture_poll,
3430 .unlocked_ioctl = snd_pcm_capture_ioctl,
3431 .compat_ioctl = snd_pcm_ioctl_compat,
3432 .mmap = snd_pcm_mmap,
3433 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003434 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
3436};