blob: 88fff44702a433fcea9a24966359f8ea577d5e13 [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);
922}
923
924static struct action_ops snd_pcm_action_stop = {
925 .pre_action = snd_pcm_pre_stop,
926 .do_action = snd_pcm_do_stop,
927 .post_action = snd_pcm_post_stop
928};
929
930/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700931 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200932 * @substream: the PCM substream instance
933 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700935 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100937int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
940}
941
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200942EXPORT_SYMBOL(snd_pcm_stop);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700945 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200946 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700948 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 * Unlike snd_pcm_stop(), this affects only the given stream.
950 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100951int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Takashi Iwai877211f2005-11-17 13:59:38 +0100953 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
954 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * pause callbacks
959 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100960static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai877211f2005-11-17 13:59:38 +0100962 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
964 return -ENOSYS;
965 if (push) {
966 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
967 return -EBADFD;
968 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
969 return -EBADFD;
970 runtime->trigger_master = substream;
971 return 0;
972}
973
Takashi Iwai877211f2005-11-17 13:59:38 +0100974static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 if (substream->runtime->trigger_master != substream)
977 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200978 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
979 * a delta betwen the current jiffies, this gives a large enough
980 * delta, effectively to skip the check once.
981 */
982 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return substream->ops->trigger(substream,
984 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
985 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
986}
987
Takashi Iwai877211f2005-11-17 13:59:38 +0100988static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 if (substream->runtime->trigger_master == substream)
991 substream->ops->trigger(substream,
992 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
993 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
994}
995
Takashi Iwai877211f2005-11-17 13:59:38 +0100996static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Takashi Iwai877211f2005-11-17 13:59:38 +0100998 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 snd_pcm_trigger_tstamp(substream);
1000 if (push) {
1001 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1002 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001003 snd_timer_notify(substream->timer,
1004 SNDRV_TIMER_EVENT_MPAUSE,
1005 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 wake_up(&runtime->sleep);
1007 } else {
1008 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001010 snd_timer_notify(substream->timer,
1011 SNDRV_TIMER_EVENT_MCONTINUE,
1012 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014}
1015
1016static struct action_ops snd_pcm_action_pause = {
1017 .pre_action = snd_pcm_pre_pause,
1018 .do_action = snd_pcm_do_pause,
1019 .undo_action = snd_pcm_undo_pause,
1020 .post_action = snd_pcm_post_pause
1021};
1022
1023/*
1024 * Push/release the pause for all linked streams.
1025 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001026static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
1028 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1029}
1030
1031#ifdef CONFIG_PM
1032/* suspend */
1033
Takashi Iwai877211f2005-11-17 13:59:38 +01001034static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
Takashi Iwai877211f2005-11-17 13:59:38 +01001036 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1038 return -EBUSY;
1039 runtime->trigger_master = substream;
1040 return 0;
1041}
1042
Takashi Iwai877211f2005-11-17 13:59:38 +01001043static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai877211f2005-11-17 13:59:38 +01001045 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (runtime->trigger_master != substream)
1047 return 0;
1048 if (! snd_pcm_running(substream))
1049 return 0;
1050 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1051 return 0; /* suspend unconditionally */
1052}
1053
Takashi Iwai877211f2005-11-17 13:59:38 +01001054static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Takashi Iwai877211f2005-11-17 13:59:38 +01001056 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 snd_pcm_trigger_tstamp(substream);
1058 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1060 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 runtime->status->suspended_state = runtime->status->state;
1062 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 wake_up(&runtime->sleep);
1064}
1065
1066static struct action_ops snd_pcm_action_suspend = {
1067 .pre_action = snd_pcm_pre_suspend,
1068 .do_action = snd_pcm_do_suspend,
1069 .post_action = snd_pcm_post_suspend
1070};
1071
1072/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001073 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001074 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * After this call, all streams are changed to SUSPENDED state.
1077 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001078int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 int err;
1081 unsigned long flags;
1082
Takashi Iwai603bf5242005-11-17 15:59:14 +01001083 if (! substream)
1084 return 0;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 snd_pcm_stream_lock_irqsave(substream, flags);
1087 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1088 snd_pcm_stream_unlock_irqrestore(substream, flags);
1089 return err;
1090}
1091
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001092EXPORT_SYMBOL(snd_pcm_suspend);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001095 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001096 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 * After this call, all streams are changed to SUSPENDED state.
1099 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001100int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwai877211f2005-11-17 13:59:38 +01001102 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int stream, err = 0;
1104
Takashi Iwai603bf5242005-11-17 15:59:14 +01001105 if (! pcm)
1106 return 0;
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001109 for (substream = pcm->streams[stream].substream;
1110 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 /* FIXME: the open/close code should lock this as well */
1112 if (substream->runtime == NULL)
1113 continue;
1114 err = snd_pcm_suspend(substream);
1115 if (err < 0 && err != -EBUSY)
1116 return err;
1117 }
1118 }
1119 return 0;
1120}
1121
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001122EXPORT_SYMBOL(snd_pcm_suspend_all);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/* resume */
1125
Takashi Iwai877211f2005-11-17 13:59:38 +01001126static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Takashi Iwai877211f2005-11-17 13:59:38 +01001128 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1130 return -ENOSYS;
1131 runtime->trigger_master = substream;
1132 return 0;
1133}
1134
Takashi Iwai877211f2005-11-17 13:59:38 +01001135static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwai877211f2005-11-17 13:59:38 +01001137 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (runtime->trigger_master != substream)
1139 return 0;
1140 /* DMA not running previously? */
1141 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1142 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1143 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1144 return 0;
1145 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1146}
1147
Takashi Iwai877211f2005-11-17 13:59:38 +01001148static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 if (substream->runtime->trigger_master == substream &&
1151 snd_pcm_running(substream))
1152 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1153}
1154
Takashi Iwai877211f2005-11-17 13:59:38 +01001155static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Takashi Iwai877211f2005-11-17 13:59:38 +01001157 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 snd_pcm_trigger_tstamp(substream);
1159 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001160 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1161 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static struct action_ops snd_pcm_action_resume = {
1166 .pre_action = snd_pcm_pre_resume,
1167 .do_action = snd_pcm_do_resume,
1168 .undo_action = snd_pcm_undo_resume,
1169 .post_action = snd_pcm_post_resume
1170};
1171
Takashi Iwai877211f2005-11-17 13:59:38 +01001172static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int res;
1176
1177 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001178 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1180 snd_power_unlock(card);
1181 return res;
1182}
1183
1184#else
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 return -ENOSYS;
1189}
1190
1191#endif /* CONFIG_PM */
1192
1193/*
1194 * xrun ioctl
1195 *
1196 * Change the RUNNING stream(s) to XRUN state.
1197 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001198static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Takashi Iwai877211f2005-11-17 13:59:38 +01001200 struct snd_card *card = substream->pcm->card;
1201 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int result;
1203
1204 snd_power_lock(card);
1205 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001206 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (result < 0)
1208 goto _unlock;
1209 }
1210
1211 snd_pcm_stream_lock_irq(substream);
1212 switch (runtime->status->state) {
1213 case SNDRV_PCM_STATE_XRUN:
1214 result = 0; /* already there */
1215 break;
1216 case SNDRV_PCM_STATE_RUNNING:
1217 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1218 break;
1219 default:
1220 result = -EBADFD;
1221 }
1222 snd_pcm_stream_unlock_irq(substream);
1223 _unlock:
1224 snd_power_unlock(card);
1225 return result;
1226}
1227
1228/*
1229 * reset ioctl
1230 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001231static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Takashi Iwai877211f2005-11-17 13:59:38 +01001233 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 switch (runtime->status->state) {
1235 case SNDRV_PCM_STATE_RUNNING:
1236 case SNDRV_PCM_STATE_PREPARED:
1237 case SNDRV_PCM_STATE_PAUSED:
1238 case SNDRV_PCM_STATE_SUSPENDED:
1239 return 0;
1240 default:
1241 return -EBADFD;
1242 }
1243}
1244
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai877211f2005-11-17 13:59:38 +01001247 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1249 if (err < 0)
1250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 runtime->hw_ptr_base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 runtime->silence_start = runtime->status->hw_ptr;
1253 runtime->silence_filled = 0;
1254 return 0;
1255}
1256
Takashi Iwai877211f2005-11-17 13:59:38 +01001257static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Takashi Iwai877211f2005-11-17 13:59:38 +01001259 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 runtime->control->appl_ptr = runtime->status->hw_ptr;
1261 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1262 runtime->silence_size > 0)
1263 snd_pcm_playback_silence(substream, ULONG_MAX);
1264}
1265
1266static struct action_ops snd_pcm_action_reset = {
1267 .pre_action = snd_pcm_pre_reset,
1268 .do_action = snd_pcm_do_reset,
1269 .post_action = snd_pcm_post_reset
1270};
1271
Takashi Iwai877211f2005-11-17 13:59:38 +01001272static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1275}
1276
1277/*
1278 * prepare ioctl
1279 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001280/* we use the second argument for updating f_flags */
1281static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1282 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Takashi Iwai877211f2005-11-17 13:59:38 +01001284 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001285 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1286 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EBADFD;
1288 if (snd_pcm_running(substream))
1289 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001290 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292}
1293
Takashi Iwai877211f2005-11-17 13:59:38 +01001294static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 int err;
1297 err = substream->ops->prepare(substream);
1298 if (err < 0)
1299 return err;
1300 return snd_pcm_do_reset(substream, 0);
1301}
1302
Takashi Iwai877211f2005-11-17 13:59:38 +01001303static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Takashi Iwai877211f2005-11-17 13:59:38 +01001305 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 runtime->control->appl_ptr = runtime->status->hw_ptr;
1307 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1308}
1309
1310static struct action_ops snd_pcm_action_prepare = {
1311 .pre_action = snd_pcm_pre_prepare,
1312 .do_action = snd_pcm_do_prepare,
1313 .post_action = snd_pcm_post_prepare
1314};
1315
1316/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001317 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001318 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001319 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001321static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1322 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001325 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001326 int f_flags;
1327
1328 if (file)
1329 f_flags = file->f_flags;
1330 else
1331 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001334 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001335 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1336 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 snd_power_unlock(card);
1338 return res;
1339}
1340
1341/*
1342 * drain ioctl
1343 */
1344
Takashi Iwai877211f2005-11-17 13:59:38 +01001345static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 substream->runtime->trigger_master = substream;
1348 return 0;
1349}
1350
Takashi Iwai877211f2005-11-17 13:59:38 +01001351static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Takashi Iwai877211f2005-11-17 13:59:38 +01001353 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1355 switch (runtime->status->state) {
1356 case SNDRV_PCM_STATE_PREPARED:
1357 /* start playback stream if possible */
1358 if (! snd_pcm_playback_empty(substream)) {
1359 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1360 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1361 }
1362 break;
1363 case SNDRV_PCM_STATE_RUNNING:
1364 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1365 break;
1366 default:
1367 break;
1368 }
1369 } else {
1370 /* stop running stream */
1371 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001372 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001374 snd_pcm_do_stop(substream, new_state);
1375 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 }
1378 return 0;
1379}
1380
Takashi Iwai877211f2005-11-17 13:59:38 +01001381static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
1383}
1384
1385static struct action_ops snd_pcm_action_drain_init = {
1386 .pre_action = snd_pcm_pre_drain_init,
1387 .do_action = snd_pcm_do_drain_init,
1388 .post_action = snd_pcm_post_drain_init
1389};
1390
Takashi Iwai877211f2005-11-17 13:59:38 +01001391static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393/*
1394 * Drain the stream(s).
1395 * When the substream is linked, sync until the draining of all playback streams
1396 * is finished.
1397 * After this call, all streams are supposed to be either SETUP or DRAINING
1398 * (capture only) state.
1399 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001400static int snd_pcm_drain(struct snd_pcm_substream *substream,
1401 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Takashi Iwai877211f2005-11-17 13:59:38 +01001403 struct snd_card *card;
1404 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001405 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001406 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001408 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 card = substream->pcm->card;
1411 runtime = substream->runtime;
1412
1413 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1414 return -EBADFD;
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 snd_power_lock(card);
1417 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001418 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001419 if (result < 0) {
1420 snd_power_unlock(card);
1421 return result;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001425 if (file) {
1426 if (file->f_flags & O_NONBLOCK)
1427 nonblock = 1;
1428 } else if (substream->f_flags & O_NONBLOCK)
1429 nonblock = 1;
1430
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001431 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001432 snd_pcm_stream_lock_irq(substream);
1433 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001434 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001435 snd_pcm_pause(substream, 0);
1436
1437 /* pre-start/stop - all running streams are changed to DRAINING state */
1438 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001439 if (result < 0)
1440 goto unlock;
1441 /* in non-blocking, we don't wait in ioctl but let caller poll */
1442 if (nonblock) {
1443 result = -EAGAIN;
1444 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 for (;;) {
1448 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001449 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (signal_pending(current)) {
1451 result = -ERESTARTSYS;
1452 break;
1453 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001454 /* find a substream to drain */
1455 to_check = NULL;
1456 snd_pcm_group_for_each_entry(s, substream) {
1457 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1458 continue;
1459 runtime = s->runtime;
1460 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1461 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001462 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001463 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001464 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001465 if (!to_check)
1466 break; /* all drained */
1467 init_waitqueue_entry(&wait, current);
1468 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 set_current_state(TASK_INTERRUPTIBLE);
1470 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001471 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 snd_power_unlock(card);
1473 tout = schedule_timeout(10 * HZ);
1474 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001475 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001477 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (tout == 0) {
1479 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1480 result = -ESTRPIPE;
1481 else {
1482 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1483 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1484 result = -EIO;
1485 }
1486 break;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001489
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001490 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001491 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001492 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 return result;
1496}
1497
1498/*
1499 * drop ioctl
1500 *
1501 * Immediately put all linked substreams into SETUP state.
1502 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001503static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
Takashi Iwai877211f2005-11-17 13:59:38 +01001505 struct snd_pcm_runtime *runtime;
1506 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int result = 0;
1508
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001509 if (PCM_RUNTIME_CHECK(substream))
1510 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 runtime = substream->runtime;
1512 card = substream->pcm->card;
1513
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001514 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001515 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1516 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return -EBADFD;
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 snd_pcm_stream_lock_irq(substream);
1520 /* resume pause */
1521 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1522 snd_pcm_pause(substream, 0);
1523
1524 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1525 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1526 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return result;
1529}
1530
1531
1532/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533static struct file *snd_pcm_file_fd(int fd)
1534{
1535 struct file *file;
1536 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001537 unsigned int minor;
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 file = fget(fd);
1540 if (!file)
1541 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001542 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (!S_ISCHR(inode->i_mode) ||
1544 imajor(inode) != snd_major) {
1545 fput(file);
1546 return NULL;
1547 }
1548 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001549 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1550 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 fput(file);
1552 return NULL;
1553 }
1554 return file;
1555}
1556
1557/*
1558 * PCM link handling
1559 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001560static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 int res = 0;
1563 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001564 struct snd_pcm_file *pcm_file;
1565 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 file = snd_pcm_file_fd(fd);
1568 if (!file)
1569 return -EBADFD;
1570 pcm_file = file->private_data;
1571 substream1 = pcm_file->substream;
1572 down_write(&snd_pcm_link_rwsem);
1573 write_lock_irq(&snd_pcm_link_rwlock);
1574 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1575 substream->runtime->status->state != substream1->runtime->status->state) {
1576 res = -EBADFD;
1577 goto _end;
1578 }
1579 if (snd_pcm_stream_linked(substream1)) {
1580 res = -EALREADY;
1581 goto _end;
1582 }
1583 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001584 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (substream->group == NULL) {
1586 res = -ENOMEM;
1587 goto _end;
1588 }
1589 spin_lock_init(&substream->group->lock);
1590 INIT_LIST_HEAD(&substream->group->substreams);
1591 list_add_tail(&substream->link_list, &substream->group->substreams);
1592 substream->group->count = 1;
1593 }
1594 list_add_tail(&substream1->link_list, &substream->group->substreams);
1595 substream->group->count++;
1596 substream1->group = substream->group;
1597 _end:
1598 write_unlock_irq(&snd_pcm_link_rwlock);
1599 up_write(&snd_pcm_link_rwsem);
1600 fput(file);
1601 return res;
1602}
1603
Takashi Iwai877211f2005-11-17 13:59:38 +01001604static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 substream->group = &substream->self_group;
1607 INIT_LIST_HEAD(&substream->self_group.substreams);
1608 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1609}
1610
Takashi Iwai877211f2005-11-17 13:59:38 +01001611static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Takashi Iwaief991b92007-02-22 12:52:53 +01001613 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 int res = 0;
1615
1616 down_write(&snd_pcm_link_rwsem);
1617 write_lock_irq(&snd_pcm_link_rwlock);
1618 if (!snd_pcm_stream_linked(substream)) {
1619 res = -EALREADY;
1620 goto _end;
1621 }
1622 list_del(&substream->link_list);
1623 substream->group->count--;
1624 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001625 snd_pcm_group_for_each_entry(s, substream) {
1626 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 }
1629 kfree(substream->group);
1630 }
1631 relink_to_local(substream);
1632 _end:
1633 write_unlock_irq(&snd_pcm_link_rwlock);
1634 up_write(&snd_pcm_link_rwsem);
1635 return res;
1636}
1637
1638/*
1639 * hw configurator
1640 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001641static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1642 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Takashi Iwai877211f2005-11-17 13:59:38 +01001644 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1646 hw_param_interval_c(params, rule->deps[1]), &t);
1647 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1648}
1649
Takashi Iwai877211f2005-11-17 13:59:38 +01001650static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1651 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
Takashi Iwai877211f2005-11-17 13:59:38 +01001653 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1655 hw_param_interval_c(params, rule->deps[1]), &t);
1656 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1657}
1658
Takashi Iwai877211f2005-11-17 13:59:38 +01001659static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1660 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Takashi Iwai877211f2005-11-17 13:59:38 +01001662 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1664 hw_param_interval_c(params, rule->deps[1]),
1665 (unsigned long) rule->private, &t);
1666 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1667}
1668
Takashi Iwai877211f2005-11-17 13:59:38 +01001669static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1670 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Takashi Iwai877211f2005-11-17 13:59:38 +01001672 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1674 (unsigned long) rule->private,
1675 hw_param_interval_c(params, rule->deps[1]), &t);
1676 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1677}
1678
Takashi Iwai877211f2005-11-17 13:59:38 +01001679static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1680 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001683 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1684 struct snd_mask m;
1685 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 snd_mask_any(&m);
1687 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1688 int bits;
1689 if (! snd_mask_test(mask, k))
1690 continue;
1691 bits = snd_pcm_format_physical_width(k);
1692 if (bits <= 0)
1693 continue; /* ignore invalid formats */
1694 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1695 snd_mask_reset(&m, k);
1696 }
1697 return snd_mask_refine(mask, &m);
1698}
1699
Takashi Iwai877211f2005-11-17 13:59:38 +01001700static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1701 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Takashi Iwai877211f2005-11-17 13:59:38 +01001703 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 unsigned int k;
1705 t.min = UINT_MAX;
1706 t.max = 0;
1707 t.openmin = 0;
1708 t.openmax = 0;
1709 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1710 int bits;
1711 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1712 continue;
1713 bits = snd_pcm_format_physical_width(k);
1714 if (bits <= 0)
1715 continue; /* ignore invalid formats */
1716 if (t.min > (unsigned)bits)
1717 t.min = bits;
1718 if (t.max < (unsigned)bits)
1719 t.max = bits;
1720 }
1721 t.integer = 1;
1722 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1723}
1724
1725#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1726#error "Change this table"
1727#endif
1728
1729static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1730 48000, 64000, 88200, 96000, 176400, 192000 };
1731
Clemens Ladisch7653d552007-08-13 17:38:54 +02001732const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1733 .count = ARRAY_SIZE(rates),
1734 .list = rates,
1735};
1736
Takashi Iwai877211f2005-11-17 13:59:38 +01001737static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1738 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Takashi Iwai877211f2005-11-17 13:59:38 +01001740 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001742 snd_pcm_known_rates.count,
1743 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
Takashi Iwai877211f2005-11-17 13:59:38 +01001746static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1747 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Takashi Iwai877211f2005-11-17 13:59:38 +01001749 struct snd_interval t;
1750 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 t.min = 0;
1752 t.max = substream->buffer_bytes_max;
1753 t.openmin = 0;
1754 t.openmax = 0;
1755 t.integer = 1;
1756 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1757}
1758
Takashi Iwai877211f2005-11-17 13:59:38 +01001759int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Takashi Iwai877211f2005-11-17 13:59:38 +01001761 struct snd_pcm_runtime *runtime = substream->runtime;
1762 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int k, err;
1764
1765 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1766 snd_mask_any(constrs_mask(constrs, k));
1767 }
1768
1769 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1770 snd_interval_any(constrs_interval(constrs, k));
1771 }
1772
1773 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1774 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1775 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1776 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1777 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1778
1779 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1780 snd_pcm_hw_rule_format, NULL,
1781 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1782 if (err < 0)
1783 return err;
1784 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1785 snd_pcm_hw_rule_sample_bits, NULL,
1786 SNDRV_PCM_HW_PARAM_FORMAT,
1787 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1788 if (err < 0)
1789 return err;
1790 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1791 snd_pcm_hw_rule_div, NULL,
1792 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1793 if (err < 0)
1794 return err;
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1796 snd_pcm_hw_rule_mul, NULL,
1797 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1798 if (err < 0)
1799 return err;
1800 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1801 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1802 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1803 if (err < 0)
1804 return err;
1805 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1806 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1807 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1808 if (err < 0)
1809 return err;
1810 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1811 snd_pcm_hw_rule_div, NULL,
1812 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1813 if (err < 0)
1814 return err;
1815 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1816 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1817 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1818 if (err < 0)
1819 return err;
1820 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1821 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1822 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1823 if (err < 0)
1824 return err;
1825 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1826 snd_pcm_hw_rule_div, NULL,
1827 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1828 if (err < 0)
1829 return err;
1830 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1831 snd_pcm_hw_rule_div, NULL,
1832 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1833 if (err < 0)
1834 return err;
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1836 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1837 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1838 if (err < 0)
1839 return err;
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1841 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1842 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1843 if (err < 0)
1844 return err;
1845 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1846 snd_pcm_hw_rule_mul, NULL,
1847 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1848 if (err < 0)
1849 return err;
1850 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1851 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1852 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1853 if (err < 0)
1854 return err;
1855 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1856 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1857 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1858 if (err < 0)
1859 return err;
1860 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1861 snd_pcm_hw_rule_muldivk, (void*) 8,
1862 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1863 if (err < 0)
1864 return err;
1865 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1866 snd_pcm_hw_rule_muldivk, (void*) 8,
1867 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1868 if (err < 0)
1869 return err;
1870 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1871 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1872 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1873 if (err < 0)
1874 return err;
1875 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1876 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1877 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1878 if (err < 0)
1879 return err;
1880 return 0;
1881}
1882
Takashi Iwai877211f2005-11-17 13:59:38 +01001883int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Takashi Iwai877211f2005-11-17 13:59:38 +01001885 struct snd_pcm_runtime *runtime = substream->runtime;
1886 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 int err;
1888 unsigned int mask = 0;
1889
1890 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1891 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1892 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1893 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1894 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1895 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1896 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1897 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1898 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1899 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1900 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1901 }
1902 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001903 if (err < 0)
1904 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001907 if (err < 0)
1908 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001911 if (err < 0)
1912 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1915 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001916 if (err < 0)
1917 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1920 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001921 if (err < 0)
1922 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1925 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001926 if (err < 0)
1927 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1930 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001931 if (err < 0)
1932 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1935 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001936 if (err < 0)
1937 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1940 snd_pcm_hw_rule_buffer_bytes_max, substream,
1941 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1942 if (err < 0)
1943 return err;
1944
1945 /* FIXME: remove */
1946 if (runtime->dma_bytes) {
1947 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001948 if (err < 0)
1949 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
1952 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1953 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1954 snd_pcm_hw_rule_rate, hw,
1955 SNDRV_PCM_HW_PARAM_RATE, -1);
1956 if (err < 0)
1957 return err;
1958 }
1959
1960 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1962
1963 return 0;
1964}
1965
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001966static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001969}
1970
1971void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1972{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001973 substream->ref_count--;
1974 if (substream->ref_count > 0)
1975 return;
1976
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001977 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001978 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 if (substream->ops->hw_free != NULL)
1980 substream->ops->hw_free(substream);
1981 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001982 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Takashi Iwai15762742006-04-06 19:47:42 +02001984 if (substream->pcm_release) {
1985 substream->pcm_release(substream);
1986 substream->pcm_release = NULL;
1987 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001988 snd_pcm_detach_substream(substream);
1989}
1990
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001991EXPORT_SYMBOL(snd_pcm_release_substream);
1992
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001993int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
1994 struct file *file,
1995 struct snd_pcm_substream **rsubstream)
1996{
1997 struct snd_pcm_substream *substream;
1998 int err;
1999
2000 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2001 if (err < 0)
2002 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002003 if (substream->ref_count > 1) {
2004 *rsubstream = substream;
2005 return 0;
2006 }
2007
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008 err = snd_pcm_hw_constraints_init(substream);
2009 if (err < 0) {
2010 snd_printd("snd_pcm_hw_constraints_init failed\n");
2011 goto error;
2012 }
2013
2014 if ((err = substream->ops->open(substream)) < 0)
2015 goto error;
2016
2017 substream->hw_opened = 1;
2018
2019 err = snd_pcm_hw_constraints_complete(substream);
2020 if (err < 0) {
2021 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2022 goto error;
2023 }
2024
2025 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002027
2028 error:
2029 snd_pcm_release_substream(substream);
2030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002033EXPORT_SYMBOL(snd_pcm_open_substream);
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002036 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002038 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Takashi Iwai877211f2005-11-17 13:59:38 +01002040 struct snd_pcm_file *pcm_file;
2041 struct snd_pcm_substream *substream;
2042 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002043 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002045 if (rpcm_file)
2046 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002048 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2049 if (err < 0)
2050 return err;
2051
Takashi Iwai548a6482006-07-31 16:51:51 +02002052 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2053 if (pcm_file == NULL) {
2054 snd_pcm_release_substream(substream);
2055 return -ENOMEM;
2056 }
2057 pcm_file->substream = substream;
2058 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002059 str = substream->pstr;
2060 substream->file = pcm_file;
2061 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002064 if (rpcm_file)
2065 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return 0;
2067}
2068
Clemens Ladischf87135f2005-11-20 14:06:59 +01002069static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Takashi Iwai877211f2005-11-17 13:59:38 +01002071 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002072
2073 pcm = snd_lookup_minor_data(iminor(inode),
2074 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2075 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2076}
2077
2078static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2079{
2080 struct snd_pcm *pcm;
2081
2082 pcm = snd_lookup_minor_data(iminor(inode),
2083 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2084 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2085}
2086
2087static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2088{
2089 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002090 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 wait_queue_t wait;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (pcm == NULL) {
2094 err = -ENODEV;
2095 goto __error1;
2096 }
2097 err = snd_card_file_add(pcm->card, file);
2098 if (err < 0)
2099 goto __error1;
2100 if (!try_module_get(pcm->card->module)) {
2101 err = -EFAULT;
2102 goto __error2;
2103 }
2104 init_waitqueue_entry(&wait, current);
2105 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002106 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002108 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (err >= 0)
2110 break;
2111 if (err == -EAGAIN) {
2112 if (file->f_flags & O_NONBLOCK) {
2113 err = -EBUSY;
2114 break;
2115 }
2116 } else
2117 break;
2118 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002119 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002121 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (signal_pending(current)) {
2123 err = -ERESTARTSYS;
2124 break;
2125 }
2126 }
2127 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002128 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (err < 0)
2130 goto __error;
2131 return err;
2132
2133 __error:
2134 module_put(pcm->card->module);
2135 __error2:
2136 snd_card_file_remove(pcm->card, file);
2137 __error1:
2138 return err;
2139}
2140
2141static int snd_pcm_release(struct inode *inode, struct file *file)
2142{
Takashi Iwai877211f2005-11-17 13:59:38 +01002143 struct snd_pcm *pcm;
2144 struct snd_pcm_substream *substream;
2145 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 pcm_file = file->private_data;
2148 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002149 if (snd_BUG_ON(!substream))
2150 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002152 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002153 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002154 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002155 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 wake_up(&pcm->open_wait);
2157 module_put(pcm->card->module);
2158 snd_card_file_remove(pcm->card, file);
2159 return 0;
2160}
2161
Takashi Iwai877211f2005-11-17 13:59:38 +01002162static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2163 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Takashi Iwai877211f2005-11-17 13:59:38 +01002165 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 snd_pcm_sframes_t appl_ptr;
2167 snd_pcm_sframes_t ret;
2168 snd_pcm_sframes_t hw_avail;
2169
2170 if (frames == 0)
2171 return 0;
2172
2173 snd_pcm_stream_lock_irq(substream);
2174 switch (runtime->status->state) {
2175 case SNDRV_PCM_STATE_PREPARED:
2176 break;
2177 case SNDRV_PCM_STATE_DRAINING:
2178 case SNDRV_PCM_STATE_RUNNING:
2179 if (snd_pcm_update_hw_ptr(substream) >= 0)
2180 break;
2181 /* Fall through */
2182 case SNDRV_PCM_STATE_XRUN:
2183 ret = -EPIPE;
2184 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002185 case SNDRV_PCM_STATE_SUSPENDED:
2186 ret = -ESTRPIPE;
2187 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 default:
2189 ret = -EBADFD;
2190 goto __end;
2191 }
2192
2193 hw_avail = snd_pcm_playback_hw_avail(runtime);
2194 if (hw_avail <= 0) {
2195 ret = 0;
2196 goto __end;
2197 }
2198 if (frames > (snd_pcm_uframes_t)hw_avail)
2199 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 appl_ptr = runtime->control->appl_ptr - frames;
2201 if (appl_ptr < 0)
2202 appl_ptr += runtime->boundary;
2203 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ret = frames;
2205 __end:
2206 snd_pcm_stream_unlock_irq(substream);
2207 return ret;
2208}
2209
Takashi Iwai877211f2005-11-17 13:59:38 +01002210static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2211 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Takashi Iwai877211f2005-11-17 13:59:38 +01002213 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 snd_pcm_sframes_t appl_ptr;
2215 snd_pcm_sframes_t ret;
2216 snd_pcm_sframes_t hw_avail;
2217
2218 if (frames == 0)
2219 return 0;
2220
2221 snd_pcm_stream_lock_irq(substream);
2222 switch (runtime->status->state) {
2223 case SNDRV_PCM_STATE_PREPARED:
2224 case SNDRV_PCM_STATE_DRAINING:
2225 break;
2226 case SNDRV_PCM_STATE_RUNNING:
2227 if (snd_pcm_update_hw_ptr(substream) >= 0)
2228 break;
2229 /* Fall through */
2230 case SNDRV_PCM_STATE_XRUN:
2231 ret = -EPIPE;
2232 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002233 case SNDRV_PCM_STATE_SUSPENDED:
2234 ret = -ESTRPIPE;
2235 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 default:
2237 ret = -EBADFD;
2238 goto __end;
2239 }
2240
2241 hw_avail = snd_pcm_capture_hw_avail(runtime);
2242 if (hw_avail <= 0) {
2243 ret = 0;
2244 goto __end;
2245 }
2246 if (frames > (snd_pcm_uframes_t)hw_avail)
2247 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 appl_ptr = runtime->control->appl_ptr - frames;
2249 if (appl_ptr < 0)
2250 appl_ptr += runtime->boundary;
2251 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ret = frames;
2253 __end:
2254 snd_pcm_stream_unlock_irq(substream);
2255 return ret;
2256}
2257
Takashi Iwai877211f2005-11-17 13:59:38 +01002258static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2259 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Takashi Iwai877211f2005-11-17 13:59:38 +01002261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 snd_pcm_sframes_t appl_ptr;
2263 snd_pcm_sframes_t ret;
2264 snd_pcm_sframes_t avail;
2265
2266 if (frames == 0)
2267 return 0;
2268
2269 snd_pcm_stream_lock_irq(substream);
2270 switch (runtime->status->state) {
2271 case SNDRV_PCM_STATE_PREPARED:
2272 case SNDRV_PCM_STATE_PAUSED:
2273 break;
2274 case SNDRV_PCM_STATE_DRAINING:
2275 case SNDRV_PCM_STATE_RUNNING:
2276 if (snd_pcm_update_hw_ptr(substream) >= 0)
2277 break;
2278 /* Fall through */
2279 case SNDRV_PCM_STATE_XRUN:
2280 ret = -EPIPE;
2281 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002282 case SNDRV_PCM_STATE_SUSPENDED:
2283 ret = -ESTRPIPE;
2284 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 default:
2286 ret = -EBADFD;
2287 goto __end;
2288 }
2289
2290 avail = snd_pcm_playback_avail(runtime);
2291 if (avail <= 0) {
2292 ret = 0;
2293 goto __end;
2294 }
2295 if (frames > (snd_pcm_uframes_t)avail)
2296 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 appl_ptr = runtime->control->appl_ptr + frames;
2298 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2299 appl_ptr -= runtime->boundary;
2300 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 ret = frames;
2302 __end:
2303 snd_pcm_stream_unlock_irq(substream);
2304 return ret;
2305}
2306
Takashi Iwai877211f2005-11-17 13:59:38 +01002307static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2308 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Takashi Iwai877211f2005-11-17 13:59:38 +01002310 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 snd_pcm_sframes_t appl_ptr;
2312 snd_pcm_sframes_t ret;
2313 snd_pcm_sframes_t avail;
2314
2315 if (frames == 0)
2316 return 0;
2317
2318 snd_pcm_stream_lock_irq(substream);
2319 switch (runtime->status->state) {
2320 case SNDRV_PCM_STATE_PREPARED:
2321 case SNDRV_PCM_STATE_DRAINING:
2322 case SNDRV_PCM_STATE_PAUSED:
2323 break;
2324 case SNDRV_PCM_STATE_RUNNING:
2325 if (snd_pcm_update_hw_ptr(substream) >= 0)
2326 break;
2327 /* Fall through */
2328 case SNDRV_PCM_STATE_XRUN:
2329 ret = -EPIPE;
2330 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002331 case SNDRV_PCM_STATE_SUSPENDED:
2332 ret = -ESTRPIPE;
2333 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 default:
2335 ret = -EBADFD;
2336 goto __end;
2337 }
2338
2339 avail = snd_pcm_capture_avail(runtime);
2340 if (avail <= 0) {
2341 ret = 0;
2342 goto __end;
2343 }
2344 if (frames > (snd_pcm_uframes_t)avail)
2345 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 appl_ptr = runtime->control->appl_ptr + frames;
2347 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2348 appl_ptr -= runtime->boundary;
2349 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 ret = frames;
2351 __end:
2352 snd_pcm_stream_unlock_irq(substream);
2353 return ret;
2354}
2355
Takashi Iwai877211f2005-11-17 13:59:38 +01002356static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Takashi Iwai877211f2005-11-17 13:59:38 +01002358 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 int err;
2360
2361 snd_pcm_stream_lock_irq(substream);
2362 switch (runtime->status->state) {
2363 case SNDRV_PCM_STATE_DRAINING:
2364 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2365 goto __badfd;
2366 case SNDRV_PCM_STATE_RUNNING:
2367 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2368 break;
2369 /* Fall through */
2370 case SNDRV_PCM_STATE_PREPARED:
2371 case SNDRV_PCM_STATE_SUSPENDED:
2372 err = 0;
2373 break;
2374 case SNDRV_PCM_STATE_XRUN:
2375 err = -EPIPE;
2376 break;
2377 default:
2378 __badfd:
2379 err = -EBADFD;
2380 break;
2381 }
2382 snd_pcm_stream_unlock_irq(substream);
2383 return err;
2384}
2385
Takashi Iwai877211f2005-11-17 13:59:38 +01002386static int snd_pcm_delay(struct snd_pcm_substream *substream,
2387 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Takashi Iwai877211f2005-11-17 13:59:38 +01002389 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 int err;
2391 snd_pcm_sframes_t n = 0;
2392
2393 snd_pcm_stream_lock_irq(substream);
2394 switch (runtime->status->state) {
2395 case SNDRV_PCM_STATE_DRAINING:
2396 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2397 goto __badfd;
2398 case SNDRV_PCM_STATE_RUNNING:
2399 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2400 break;
2401 /* Fall through */
2402 case SNDRV_PCM_STATE_PREPARED:
2403 case SNDRV_PCM_STATE_SUSPENDED:
2404 err = 0;
2405 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2406 n = snd_pcm_playback_hw_avail(runtime);
2407 else
2408 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002409 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 break;
2411 case SNDRV_PCM_STATE_XRUN:
2412 err = -EPIPE;
2413 break;
2414 default:
2415 __badfd:
2416 err = -EBADFD;
2417 break;
2418 }
2419 snd_pcm_stream_unlock_irq(substream);
2420 if (!err)
2421 if (put_user(n, res))
2422 err = -EFAULT;
2423 return err;
2424}
2425
Takashi Iwai877211f2005-11-17 13:59:38 +01002426static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2427 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Takashi Iwai877211f2005-11-17 13:59:38 +01002429 struct snd_pcm_runtime *runtime = substream->runtime;
2430 struct snd_pcm_sync_ptr sync_ptr;
2431 volatile struct snd_pcm_mmap_status *status;
2432 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 int err;
2434
2435 memset(&sync_ptr, 0, sizeof(sync_ptr));
2436 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2437 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002438 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 -07002439 return -EFAULT;
2440 status = runtime->status;
2441 control = runtime->control;
2442 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2443 err = snd_pcm_hwsync(substream);
2444 if (err < 0)
2445 return err;
2446 }
2447 snd_pcm_stream_lock_irq(substream);
2448 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2449 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2450 else
2451 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2452 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2453 control->avail_min = sync_ptr.c.control.avail_min;
2454 else
2455 sync_ptr.c.control.avail_min = control->avail_min;
2456 sync_ptr.s.status.state = status->state;
2457 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2458 sync_ptr.s.status.tstamp = status->tstamp;
2459 sync_ptr.s.status.suspended_state = status->suspended_state;
2460 snd_pcm_stream_unlock_irq(substream);
2461 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2462 return -EFAULT;
2463 return 0;
2464}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002465
2466static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2467{
2468 struct snd_pcm_runtime *runtime = substream->runtime;
2469 int arg;
2470
2471 if (get_user(arg, _arg))
2472 return -EFAULT;
2473 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2474 return -EINVAL;
2475 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2476 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2477 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2478 return 0;
2479}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Takashi Iwai0df63e42006-04-28 15:13:41 +02002481static int snd_pcm_common_ioctl1(struct file *file,
2482 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 unsigned int cmd, void __user *arg)
2484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 switch (cmd) {
2486 case SNDRV_PCM_IOCTL_PVERSION:
2487 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2488 case SNDRV_PCM_IOCTL_INFO:
2489 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002490 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2491 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002492 case SNDRV_PCM_IOCTL_TTSTAMP:
2493 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 case SNDRV_PCM_IOCTL_HW_REFINE:
2495 return snd_pcm_hw_refine_user(substream, arg);
2496 case SNDRV_PCM_IOCTL_HW_PARAMS:
2497 return snd_pcm_hw_params_user(substream, arg);
2498 case SNDRV_PCM_IOCTL_HW_FREE:
2499 return snd_pcm_hw_free(substream);
2500 case SNDRV_PCM_IOCTL_SW_PARAMS:
2501 return snd_pcm_sw_params_user(substream, arg);
2502 case SNDRV_PCM_IOCTL_STATUS:
2503 return snd_pcm_status_user(substream, arg);
2504 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2505 return snd_pcm_channel_info_user(substream, arg);
2506 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002507 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 case SNDRV_PCM_IOCTL_RESET:
2509 return snd_pcm_reset(substream);
2510 case SNDRV_PCM_IOCTL_START:
2511 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2512 case SNDRV_PCM_IOCTL_LINK:
2513 return snd_pcm_link(substream, (int)(unsigned long) arg);
2514 case SNDRV_PCM_IOCTL_UNLINK:
2515 return snd_pcm_unlink(substream);
2516 case SNDRV_PCM_IOCTL_RESUME:
2517 return snd_pcm_resume(substream);
2518 case SNDRV_PCM_IOCTL_XRUN:
2519 return snd_pcm_xrun(substream);
2520 case SNDRV_PCM_IOCTL_HWSYNC:
2521 return snd_pcm_hwsync(substream);
2522 case SNDRV_PCM_IOCTL_DELAY:
2523 return snd_pcm_delay(substream, arg);
2524 case SNDRV_PCM_IOCTL_SYNC_PTR:
2525 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002526#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2528 return snd_pcm_hw_refine_old_user(substream, arg);
2529 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2530 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002531#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002533 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 case SNDRV_PCM_IOCTL_DROP:
2535 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002536 case SNDRV_PCM_IOCTL_PAUSE:
2537 {
2538 int res;
2539 snd_pcm_stream_lock_irq(substream);
2540 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2541 snd_pcm_stream_unlock_irq(substream);
2542 return res;
2543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545 snd_printd("unknown ioctl = 0x%x\n", cmd);
2546 return -ENOTTY;
2547}
2548
Takashi Iwai0df63e42006-04-28 15:13:41 +02002549static int snd_pcm_playback_ioctl1(struct file *file,
2550 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 unsigned int cmd, void __user *arg)
2552{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002553 if (snd_BUG_ON(!substream))
2554 return -ENXIO;
2555 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2556 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 switch (cmd) {
2558 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2559 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002560 struct snd_xferi xferi;
2561 struct snd_xferi __user *_xferi = arg;
2562 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 snd_pcm_sframes_t result;
2564 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2565 return -EBADFD;
2566 if (put_user(0, &_xferi->result))
2567 return -EFAULT;
2568 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2569 return -EFAULT;
2570 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2571 __put_user(result, &_xferi->result);
2572 return result < 0 ? result : 0;
2573 }
2574 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2575 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002576 struct snd_xfern xfern;
2577 struct snd_xfern __user *_xfern = arg;
2578 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 void __user **bufs;
2580 snd_pcm_sframes_t result;
2581 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2582 return -EBADFD;
2583 if (runtime->channels > 128)
2584 return -EINVAL;
2585 if (put_user(0, &_xfern->result))
2586 return -EFAULT;
2587 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2588 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002589
2590 bufs = memdup_user(xfern.bufs,
2591 sizeof(void *) * runtime->channels);
2592 if (IS_ERR(bufs))
2593 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2595 kfree(bufs);
2596 __put_user(result, &_xfern->result);
2597 return result < 0 ? result : 0;
2598 }
2599 case SNDRV_PCM_IOCTL_REWIND:
2600 {
2601 snd_pcm_uframes_t frames;
2602 snd_pcm_uframes_t __user *_frames = arg;
2603 snd_pcm_sframes_t result;
2604 if (get_user(frames, _frames))
2605 return -EFAULT;
2606 if (put_user(0, _frames))
2607 return -EFAULT;
2608 result = snd_pcm_playback_rewind(substream, frames);
2609 __put_user(result, _frames);
2610 return result < 0 ? result : 0;
2611 }
2612 case SNDRV_PCM_IOCTL_FORWARD:
2613 {
2614 snd_pcm_uframes_t frames;
2615 snd_pcm_uframes_t __user *_frames = arg;
2616 snd_pcm_sframes_t result;
2617 if (get_user(frames, _frames))
2618 return -EFAULT;
2619 if (put_user(0, _frames))
2620 return -EFAULT;
2621 result = snd_pcm_playback_forward(substream, frames);
2622 __put_user(result, _frames);
2623 return result < 0 ? result : 0;
2624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002626 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Takashi Iwai0df63e42006-04-28 15:13:41 +02002629static int snd_pcm_capture_ioctl1(struct file *file,
2630 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 unsigned int cmd, void __user *arg)
2632{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002633 if (snd_BUG_ON(!substream))
2634 return -ENXIO;
2635 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2636 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 switch (cmd) {
2638 case SNDRV_PCM_IOCTL_READI_FRAMES:
2639 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002640 struct snd_xferi xferi;
2641 struct snd_xferi __user *_xferi = arg;
2642 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 snd_pcm_sframes_t result;
2644 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2645 return -EBADFD;
2646 if (put_user(0, &_xferi->result))
2647 return -EFAULT;
2648 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2649 return -EFAULT;
2650 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2651 __put_user(result, &_xferi->result);
2652 return result < 0 ? result : 0;
2653 }
2654 case SNDRV_PCM_IOCTL_READN_FRAMES:
2655 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002656 struct snd_xfern xfern;
2657 struct snd_xfern __user *_xfern = arg;
2658 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 void *bufs;
2660 snd_pcm_sframes_t result;
2661 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2662 return -EBADFD;
2663 if (runtime->channels > 128)
2664 return -EINVAL;
2665 if (put_user(0, &_xfern->result))
2666 return -EFAULT;
2667 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2668 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002669
2670 bufs = memdup_user(xfern.bufs,
2671 sizeof(void *) * runtime->channels);
2672 if (IS_ERR(bufs))
2673 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2675 kfree(bufs);
2676 __put_user(result, &_xfern->result);
2677 return result < 0 ? result : 0;
2678 }
2679 case SNDRV_PCM_IOCTL_REWIND:
2680 {
2681 snd_pcm_uframes_t frames;
2682 snd_pcm_uframes_t __user *_frames = arg;
2683 snd_pcm_sframes_t result;
2684 if (get_user(frames, _frames))
2685 return -EFAULT;
2686 if (put_user(0, _frames))
2687 return -EFAULT;
2688 result = snd_pcm_capture_rewind(substream, frames);
2689 __put_user(result, _frames);
2690 return result < 0 ? result : 0;
2691 }
2692 case SNDRV_PCM_IOCTL_FORWARD:
2693 {
2694 snd_pcm_uframes_t frames;
2695 snd_pcm_uframes_t __user *_frames = arg;
2696 snd_pcm_sframes_t result;
2697 if (get_user(frames, _frames))
2698 return -EFAULT;
2699 if (put_user(0, _frames))
2700 return -EFAULT;
2701 result = snd_pcm_capture_forward(substream, frames);
2702 __put_user(result, _frames);
2703 return result < 0 ? result : 0;
2704 }
2705 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002706 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707}
2708
Takashi Iwai877211f2005-11-17 13:59:38 +01002709static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2710 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Takashi Iwai877211f2005-11-17 13:59:38 +01002712 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 pcm_file = file->private_data;
2715
2716 if (((cmd >> 8) & 0xff) != 'A')
2717 return -ENOTTY;
2718
Takashi Iwai0df63e42006-04-28 15:13:41 +02002719 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2720 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Takashi Iwai877211f2005-11-17 13:59:38 +01002723static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2724 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Takashi Iwai877211f2005-11-17 13:59:38 +01002726 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 pcm_file = file->private_data;
2729
2730 if (((cmd >> 8) & 0xff) != 'A')
2731 return -ENOTTY;
2732
Takashi Iwai0df63e42006-04-28 15:13:41 +02002733 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2734 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Takashi Iwai877211f2005-11-17 13:59:38 +01002737int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 unsigned int cmd, void *arg)
2739{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002740 mm_segment_t fs;
2741 int result;
2742
2743 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 switch (substream->stream) {
2745 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002746 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2747 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002750 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2751 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002752 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002754 result = -EINVAL;
2755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002757 snd_leave_user(fs);
2758 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002761EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2762
Takashi Iwai877211f2005-11-17 13:59:38 +01002763static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2764 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Takashi Iwai877211f2005-11-17 13:59:38 +01002766 struct snd_pcm_file *pcm_file;
2767 struct snd_pcm_substream *substream;
2768 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 snd_pcm_sframes_t result;
2770
2771 pcm_file = file->private_data;
2772 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002773 if (PCM_RUNTIME_CHECK(substream))
2774 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 runtime = substream->runtime;
2776 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2777 return -EBADFD;
2778 if (!frame_aligned(runtime, count))
2779 return -EINVAL;
2780 count = bytes_to_frames(runtime, count);
2781 result = snd_pcm_lib_read(substream, buf, count);
2782 if (result > 0)
2783 result = frames_to_bytes(runtime, result);
2784 return result;
2785}
2786
Takashi Iwai877211f2005-11-17 13:59:38 +01002787static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2788 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Takashi Iwai877211f2005-11-17 13:59:38 +01002790 struct snd_pcm_file *pcm_file;
2791 struct snd_pcm_substream *substream;
2792 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 snd_pcm_sframes_t result;
2794
2795 pcm_file = file->private_data;
2796 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002797 if (PCM_RUNTIME_CHECK(substream))
2798 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002800 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2801 return -EBADFD;
2802 if (!frame_aligned(runtime, count))
2803 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 count = bytes_to_frames(runtime, count);
2805 result = snd_pcm_lib_write(substream, buf, count);
2806 if (result > 0)
2807 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return result;
2809}
2810
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002811static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2812 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814{
Takashi Iwai877211f2005-11-17 13:59:38 +01002815 struct snd_pcm_file *pcm_file;
2816 struct snd_pcm_substream *substream;
2817 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 snd_pcm_sframes_t result;
2819 unsigned long i;
2820 void __user **bufs;
2821 snd_pcm_uframes_t frames;
2822
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002823 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002825 if (PCM_RUNTIME_CHECK(substream))
2826 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 runtime = substream->runtime;
2828 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2829 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002830 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002832 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002834 frames = bytes_to_samples(runtime, iov->iov_len);
2835 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (bufs == NULL)
2837 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002838 for (i = 0; i < nr_segs; ++i)
2839 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 result = snd_pcm_lib_readv(substream, bufs, frames);
2841 if (result > 0)
2842 result = frames_to_bytes(runtime, result);
2843 kfree(bufs);
2844 return result;
2845}
2846
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002847static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2848 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Takashi Iwai877211f2005-11-17 13:59:38 +01002850 struct snd_pcm_file *pcm_file;
2851 struct snd_pcm_substream *substream;
2852 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 snd_pcm_sframes_t result;
2854 unsigned long i;
2855 void __user **bufs;
2856 snd_pcm_uframes_t frames;
2857
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002858 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002860 if (PCM_RUNTIME_CHECK(substream))
2861 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002863 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2864 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002865 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002866 !frame_aligned(runtime, iov->iov_len))
2867 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868 frames = bytes_to_samples(runtime, iov->iov_len);
2869 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (bufs == NULL)
2871 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002872 for (i = 0; i < nr_segs; ++i)
2873 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 result = snd_pcm_lib_writev(substream, bufs, frames);
2875 if (result > 0)
2876 result = frames_to_bytes(runtime, result);
2877 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return result;
2879}
2880
2881static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2882{
Takashi Iwai877211f2005-11-17 13:59:38 +01002883 struct snd_pcm_file *pcm_file;
2884 struct snd_pcm_substream *substream;
2885 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 unsigned int mask;
2887 snd_pcm_uframes_t avail;
2888
2889 pcm_file = file->private_data;
2890
2891 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002892 if (PCM_RUNTIME_CHECK(substream))
2893 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 runtime = substream->runtime;
2895
2896 poll_wait(file, &runtime->sleep, wait);
2897
2898 snd_pcm_stream_lock_irq(substream);
2899 avail = snd_pcm_playback_avail(runtime);
2900 switch (runtime->status->state) {
2901 case SNDRV_PCM_STATE_RUNNING:
2902 case SNDRV_PCM_STATE_PREPARED:
2903 case SNDRV_PCM_STATE_PAUSED:
2904 if (avail >= runtime->control->avail_min) {
2905 mask = POLLOUT | POLLWRNORM;
2906 break;
2907 }
2908 /* Fall through */
2909 case SNDRV_PCM_STATE_DRAINING:
2910 mask = 0;
2911 break;
2912 default:
2913 mask = POLLOUT | POLLWRNORM | POLLERR;
2914 break;
2915 }
2916 snd_pcm_stream_unlock_irq(substream);
2917 return mask;
2918}
2919
2920static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2921{
Takashi Iwai877211f2005-11-17 13:59:38 +01002922 struct snd_pcm_file *pcm_file;
2923 struct snd_pcm_substream *substream;
2924 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 unsigned int mask;
2926 snd_pcm_uframes_t avail;
2927
2928 pcm_file = file->private_data;
2929
2930 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002931 if (PCM_RUNTIME_CHECK(substream))
2932 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 runtime = substream->runtime;
2934
2935 poll_wait(file, &runtime->sleep, wait);
2936
2937 snd_pcm_stream_lock_irq(substream);
2938 avail = snd_pcm_capture_avail(runtime);
2939 switch (runtime->status->state) {
2940 case SNDRV_PCM_STATE_RUNNING:
2941 case SNDRV_PCM_STATE_PREPARED:
2942 case SNDRV_PCM_STATE_PAUSED:
2943 if (avail >= runtime->control->avail_min) {
2944 mask = POLLIN | POLLRDNORM;
2945 break;
2946 }
2947 mask = 0;
2948 break;
2949 case SNDRV_PCM_STATE_DRAINING:
2950 if (avail > 0) {
2951 mask = POLLIN | POLLRDNORM;
2952 break;
2953 }
2954 /* Fall through */
2955 default:
2956 mask = POLLIN | POLLRDNORM | POLLERR;
2957 break;
2958 }
2959 snd_pcm_stream_unlock_irq(substream);
2960 return mask;
2961}
2962
2963/*
2964 * mmap support
2965 */
2966
2967/*
2968 * Only on coherent architectures, we can mmap the status and the control records
2969 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2970 */
2971#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2972/*
2973 * mmap status record
2974 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002975static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2976 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Takashi Iwai877211f2005-11-17 13:59:38 +01002978 struct snd_pcm_substream *substream = area->vm_private_data;
2979 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002982 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002984 vmf->page = virt_to_page(runtime->status);
2985 get_page(vmf->page);
2986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987}
2988
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002989static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002991 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992};
2993
Takashi Iwai877211f2005-11-17 13:59:38 +01002994static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 struct vm_area_struct *area)
2996{
Takashi Iwai877211f2005-11-17 13:59:38 +01002997 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 long size;
2999 if (!(area->vm_flags & VM_READ))
3000 return -EINVAL;
3001 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003003 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return -EINVAL;
3005 area->vm_ops = &snd_pcm_vm_ops_status;
3006 area->vm_private_data = substream;
3007 area->vm_flags |= VM_RESERVED;
3008 return 0;
3009}
3010
3011/*
3012 * mmap control record
3013 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003014static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3015 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Takashi Iwai877211f2005-11-17 13:59:38 +01003017 struct snd_pcm_substream *substream = area->vm_private_data;
3018 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003021 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003023 vmf->page = virt_to_page(runtime->control);
3024 get_page(vmf->page);
3025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003028static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003030 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031};
3032
Takashi Iwai877211f2005-11-17 13:59:38 +01003033static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct vm_area_struct *area)
3035{
Takashi Iwai877211f2005-11-17 13:59:38 +01003036 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 long size;
3038 if (!(area->vm_flags & VM_READ))
3039 return -EINVAL;
3040 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003042 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return -EINVAL;
3044 area->vm_ops = &snd_pcm_vm_ops_control;
3045 area->vm_private_data = substream;
3046 area->vm_flags |= VM_RESERVED;
3047 return 0;
3048}
3049#else /* ! coherent mmap */
3050/*
3051 * don't support mmap for status and control records.
3052 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003053static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct vm_area_struct *area)
3055{
3056 return -ENXIO;
3057}
Takashi Iwai877211f2005-11-17 13:59:38 +01003058static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct vm_area_struct *area)
3060{
3061 return -ENXIO;
3062}
3063#endif /* coherent mmap */
3064
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003065static inline struct page *
3066snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3067{
3068 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003069#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3070 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3071 return virt_to_page(CAC_ADDR(vaddr));
3072#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003073#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3074 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3075 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3076 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3077 /* assume dma_handle set via pfn_to_phys() in
3078 * mm/dma-noncoherent.c
3079 */
3080 return pfn_to_page(addr >> PAGE_SHIFT);
3081 }
3082#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003083 return virt_to_page(vaddr);
3084}
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003087 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003089static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3090 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091{
Takashi Iwai877211f2005-11-17 13:59:38 +01003092 struct snd_pcm_substream *substream = area->vm_private_data;
3093 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 unsigned long offset;
3095 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 size_t dma_bytes;
3097
3098 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003099 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003101 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3103 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003105 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003107 else
3108 page = snd_pcm_default_page_ops(substream, offset);
3109 if (!page)
3110 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003111 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003112 vmf->page = page;
3113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Takashi Iwai657b1982009-11-26 12:40:21 +01003116static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3117 .open = snd_pcm_mmap_data_open,
3118 .close = snd_pcm_mmap_data_close,
3119};
3120
3121static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 .open = snd_pcm_mmap_data_open,
3123 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003124 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125};
3126
Takashi Iwai657b1982009-11-26 12:40:21 +01003127#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3128/* This should be defined / handled globally! */
3129#ifdef CONFIG_ARM
3130#define ARCH_HAS_DMA_MMAP_COHERENT
3131#endif
3132#endif
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134/*
3135 * mmap the DMA buffer on RAM
3136 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003137static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3138 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003141#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3142 if (!substream->ops->page &&
3143 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3144 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3145 area,
3146 substream->runtime->dma_area,
3147 substream->runtime->dma_addr,
3148 area->vm_end - area->vm_start);
3149#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3150 /* mmap with fault handler */
3151 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return 0;
3153}
3154
3155/*
3156 * mmap the DMA buffer on I/O memory area
3157 */
3158#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003159int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3160 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 long size;
3163 unsigned long offset;
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 area->vm_flags |= VM_IO;
3167 size = area->vm_end - area->vm_start;
3168 offset = area->vm_pgoff << PAGE_SHIFT;
3169 if (io_remap_pfn_range(area, area->vm_start,
3170 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3171 size, area->vm_page_prot))
3172 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return 0;
3174}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003175
3176EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177#endif /* SNDRV_PCM_INFO_MMAP */
3178
Takashi Iwaic32d9772010-01-18 14:58:57 +01003179/* mmap callback with pgprot_noncached */
3180int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3181 struct vm_area_struct *area)
3182{
3183 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3184 return snd_pcm_default_mmap(substream, area);
3185}
3186EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188/*
3189 * mmap DMA buffer
3190 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003191int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct vm_area_struct *area)
3193{
Takashi Iwai877211f2005-11-17 13:59:38 +01003194 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 long size;
3196 unsigned long offset;
3197 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3201 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3202 return -EINVAL;
3203 } else {
3204 if (!(area->vm_flags & VM_READ))
3205 return -EINVAL;
3206 }
3207 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3209 return -EBADFD;
3210 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3211 return -ENXIO;
3212 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3213 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3214 return -EINVAL;
3215 size = area->vm_end - area->vm_start;
3216 offset = area->vm_pgoff << PAGE_SHIFT;
3217 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3218 if ((size_t)size > dma_bytes)
3219 return -EINVAL;
3220 if (offset > dma_bytes - size)
3221 return -EINVAL;
3222
Takashi Iwai657b1982009-11-26 12:40:21 +01003223 area->vm_ops = &snd_pcm_vm_ops_data;
3224 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003226 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003228 err = snd_pcm_default_mmap(substream, area);
3229 if (!err)
3230 atomic_inc(&substream->mmap_count);
3231 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003234EXPORT_SYMBOL(snd_pcm_mmap_data);
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3237{
Takashi Iwai877211f2005-11-17 13:59:38 +01003238 struct snd_pcm_file * pcm_file;
3239 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 unsigned long offset;
3241
3242 pcm_file = file->private_data;
3243 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003244 if (PCM_RUNTIME_CHECK(substream))
3245 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 offset = area->vm_pgoff << PAGE_SHIFT;
3248 switch (offset) {
3249 case SNDRV_PCM_MMAP_OFFSET_STATUS:
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_status(substream, file, area);
3253 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003254 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 return -ENXIO;
3256 return snd_pcm_mmap_control(substream, file, area);
3257 default:
3258 return snd_pcm_mmap_data(substream, file, area);
3259 }
3260 return 0;
3261}
3262
3263static int snd_pcm_fasync(int fd, struct file * file, int on)
3264{
Takashi Iwai877211f2005-11-17 13:59:38 +01003265 struct snd_pcm_file * pcm_file;
3266 struct snd_pcm_substream *substream;
3267 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003268 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003270 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 pcm_file = file->private_data;
3272 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003273 if (PCM_RUNTIME_CHECK(substream))
3274 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003277out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003278 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280}
3281
3282/*
3283 * ioctl32 compat
3284 */
3285#ifdef CONFIG_COMPAT
3286#include "pcm_compat.c"
3287#else
3288#define snd_pcm_ioctl_compat NULL
3289#endif
3290
3291/*
3292 * To be removed helpers to keep binary compatibility
3293 */
3294
Takashi Iwai59d48582005-12-01 10:51:58 +01003295#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3297#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3298
Takashi Iwai877211f2005-11-17 13:59:38 +01003299static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3300 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
3302 unsigned int i;
3303
3304 memset(params, 0, sizeof(*params));
3305 params->flags = oparams->flags;
3306 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3307 params->masks[i].bits[0] = oparams->masks[i];
3308 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3309 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3310 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3311 params->info = oparams->info;
3312 params->msbits = oparams->msbits;
3313 params->rate_num = oparams->rate_num;
3314 params->rate_den = oparams->rate_den;
3315 params->fifo_size = oparams->fifo_size;
3316}
3317
Takashi Iwai877211f2005-11-17 13:59:38 +01003318static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3319 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 unsigned int i;
3322
3323 memset(oparams, 0, sizeof(*oparams));
3324 oparams->flags = params->flags;
3325 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3326 oparams->masks[i] = params->masks[i].bits[0];
3327 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3328 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3329 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3330 oparams->info = params->info;
3331 oparams->msbits = params->msbits;
3332 oparams->rate_num = params->rate_num;
3333 oparams->rate_den = params->rate_den;
3334 oparams->fifo_size = params->fifo_size;
3335}
3336
Takashi Iwai877211f2005-11-17 13:59:38 +01003337static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3338 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
Takashi Iwai877211f2005-11-17 13:59:38 +01003340 struct snd_pcm_hw_params *params;
3341 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 int err;
3343
3344 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003345 if (!params)
3346 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Li Zefanef44a1e2009-04-10 09:43:08 +08003348 oparams = memdup_user(_oparams, sizeof(*oparams));
3349 if (IS_ERR(oparams)) {
3350 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 goto out;
3352 }
3353 snd_pcm_hw_convert_from_old_params(params, oparams);
3354 err = snd_pcm_hw_refine(substream, params);
3355 snd_pcm_hw_convert_to_old_params(oparams, params);
3356 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3357 if (!err)
3358 err = -EFAULT;
3359 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003360
3361 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362out:
3363 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 return err;
3365}
3366
Takashi Iwai877211f2005-11-17 13:59:38 +01003367static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3368 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
Takashi Iwai877211f2005-11-17 13:59:38 +01003370 struct snd_pcm_hw_params *params;
3371 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 int err;
3373
3374 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003375 if (!params)
3376 return -ENOMEM;
3377
3378 oparams = memdup_user(_oparams, sizeof(*oparams));
3379 if (IS_ERR(oparams)) {
3380 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 goto out;
3382 }
3383 snd_pcm_hw_convert_from_old_params(params, oparams);
3384 err = snd_pcm_hw_params(substream, params);
3385 snd_pcm_hw_convert_to_old_params(oparams, params);
3386 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3387 if (!err)
3388 err = -EFAULT;
3389 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003390
3391 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392out:
3393 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return err;
3395}
Takashi Iwai59d48582005-12-01 10:51:58 +01003396#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Cliff Cai70036092008-09-03 10:54:36 +02003398#ifndef CONFIG_MMU
3399unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3400 unsigned long len, unsigned long pgoff,
3401 unsigned long flags)
3402{
3403 return 0;
3404}
3405#else
3406# define dummy_get_unmapped_area NULL
3407#endif
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409/*
3410 * Register section
3411 */
3412
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003413const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003415 .owner = THIS_MODULE,
3416 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003417 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003418 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003419 .release = snd_pcm_release,
3420 .poll = snd_pcm_playback_poll,
3421 .unlocked_ioctl = snd_pcm_playback_ioctl,
3422 .compat_ioctl = snd_pcm_ioctl_compat,
3423 .mmap = snd_pcm_mmap,
3424 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003425 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 },
3427 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003428 .owner = THIS_MODULE,
3429 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003430 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003431 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003432 .release = snd_pcm_release,
3433 .poll = snd_pcm_capture_poll,
3434 .unlocked_ioctl = snd_pcm_capture_ioctl,
3435 .compat_ioctl = snd_pcm_ioctl_compat,
3436 .mmap = snd_pcm_mmap,
3437 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003438 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 }
3440};