blob: b5da656d1ececa2e41b13dc55ea57b662bcc1b2e [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>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35#include <sound/minors.h>
36#include <asm/io.h>
37
38/*
39 * Compatibility
40 */
41
Takashi Iwai877211f2005-11-17 13:59:38 +010042struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010046 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
48 unsigned int rmask;
49 unsigned int cmask;
50 unsigned int info;
51 unsigned int msbits;
52 unsigned int rate_num;
53 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010054 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned char reserved[64];
56};
57
Takashi Iwai59d48582005-12-01 10:51:58 +010058#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010059#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Takashi Iwai877211f2005-11-17 13:59:38 +010062static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010066#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010067static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 *
71 */
72
73DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020074EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020076static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static inline mm_segment_t snd_enter_user(void)
79{
80 mm_segment_t fs = get_fs();
81 set_fs(get_ds());
82 return fs;
83}
84
85static inline void snd_leave_user(mm_segment_t fs)
86{
87 set_fs(fs);
88}
89
90
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai877211f2005-11-17 13:59:38 +010094 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
112 if (runtime) {
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
115 }
116 return 0;
117}
118
Takashi Iwai877211f2005-11-17 13:59:38 +0100119int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Takashi Iwai877211f2005-11-17 13:59:38 +0100122 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int err;
124
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
126 if (! info)
127 return -ENOMEM;
128 err = snd_pcm_info(substream, info);
129 if (err >= 0) {
130 if (copy_to_user(_info, info, sizeof(*info)))
131 err = -EFAULT;
132 }
133 kfree(info);
134 return err;
135}
136
137#undef RULES_DEBUG
138
139#ifdef RULES_DEBUG
140#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141char *snd_pcm_hw_param_names[] = {
142 HW_PARAM(ACCESS),
143 HW_PARAM(FORMAT),
144 HW_PARAM(SUBFORMAT),
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
147 HW_PARAM(CHANNELS),
148 HW_PARAM(RATE),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
152 HW_PARAM(PERIODS),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
156 HW_PARAM(TICK_TIME),
157};
158#endif
159
Takashi Iwai877211f2005-11-17 13:59:38 +0100160int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
171 int changed, again;
172
173 params->info = 0;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
176 params->msbits = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
180 }
181
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
185 return -EINVAL;
186 if (!(params->rmask & (1 << k)))
187 continue;
188#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100189 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
191#endif
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
193#ifdef RULES_DEBUG
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 if (changed)
197 params->cmask |= 1 << k;
198 if (changed < 0)
199 return changed;
200 }
201
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
205 return -EINVAL;
206 if (!(params->rmask & (1 << k)))
207 continue;
208#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100209 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (i->empty)
211 printk("empty");
212 else
213 printk("%c%u %u%c",
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
216 printk(" -> ");
217#endif
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
219#ifdef RULES_DEBUG
220 if (i->empty)
221 printk("empty\n");
222 else
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
226#endif
227 if (changed)
228 params->cmask |= 1 << k;
229 if (changed < 0)
230 return changed;
231 }
232
233 for (k = 0; k < constrs->rules_num; k++)
234 rstamps[k] = 0;
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
237 do {
238 again = 0;
239 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int d;
242 int doit = 0;
243 if (r->cond && !(r->cond & params->flags))
244 continue;
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
247 doit = 1;
248 break;
249 }
250 }
251 if (!doit)
252 continue;
253#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100254 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (r->var >= 0) {
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
260 } else {
261 i = hw_param_interval(params, r->var);
262 if (i->empty)
263 printk("empty");
264 else
265 printk("%c%u %u%c",
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
268 }
269 }
270#endif
271 changed = r->func(params, r);
272#ifdef RULES_DEBUG
273 if (r->var >= 0) {
274 printk(" -> ");
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
277 else {
278 if (i->empty)
279 printk("empty");
280 else
281 printk("%c%u %u%c",
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
284 }
285 }
286 printk("\n");
287#endif
288 rstamps[k] = stamp;
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
292 again = 1;
293 }
294 if (changed < 0)
295 return changed;
296 stamp++;
297 }
298 } while (again);
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
303 }
304
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
310 }
311 }
312
313 hw = &substream->runtime->hw;
314 if (!params->info)
315 params->info = hw->info;
316 if (!params->fifo_size)
317 params->fifo_size = hw->fifo_size;
318 params->rmask = 0;
319 return 0;
320}
321
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200322EXPORT_SYMBOL(snd_pcm_hw_refine);
323
Takashi Iwai877211f2005-11-17 13:59:38 +0100324static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
325 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Takashi Iwai877211f2005-11-17 13:59:38 +0100327 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 int err;
329
Li Zefanef44a1e2009-04-10 09:43:08 +0800330 params = memdup_user(_params, sizeof(*params));
331 if (IS_ERR(params))
332 return PTR_ERR(params);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 err = snd_pcm_hw_refine(substream, params);
335 if (copy_to_user(_params, params, sizeof(*params))) {
336 if (!err)
337 err = -EFAULT;
338 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 kfree(params);
341 return err;
342}
343
Takashi Iwai9442e692006-09-30 23:27:19 -0700344static int period_to_usecs(struct snd_pcm_runtime *runtime)
345{
346 int usecs;
347
348 if (! runtime->rate)
349 return -1; /* invalid */
350
351 /* take 75% of period time as the deadline */
352 usecs = (750000 / runtime->rate) * runtime->period_size;
353 usecs += ((750000 % runtime->rate) * runtime->period_size) /
354 runtime->rate;
355
356 return usecs;
357}
358
Takashi Iwai877211f2005-11-17 13:59:38 +0100359static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
360 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Takashi Iwai877211f2005-11-17 13:59:38 +0100362 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700363 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 unsigned int bits;
365 snd_pcm_uframes_t frames;
366
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200367 if (PCM_RUNTIME_CHECK(substream))
368 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 snd_pcm_stream_lock_irq(substream);
371 switch (runtime->status->state) {
372 case SNDRV_PCM_STATE_OPEN:
373 case SNDRV_PCM_STATE_SETUP:
374 case SNDRV_PCM_STATE_PREPARED:
375 break;
376 default:
377 snd_pcm_stream_unlock_irq(substream);
378 return -EBADFD;
379 }
380 snd_pcm_stream_unlock_irq(substream);
381#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
382 if (!substream->oss.oss)
383#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200384 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -EBADFD;
386
387 params->rmask = ~0U;
388 err = snd_pcm_hw_refine(substream, params);
389 if (err < 0)
390 goto _error;
391
392 err = snd_pcm_hw_params_choose(substream, params);
393 if (err < 0)
394 goto _error;
395
396 if (substream->ops->hw_params != NULL) {
397 err = substream->ops->hw_params(substream, params);
398 if (err < 0)
399 goto _error;
400 }
401
402 runtime->access = params_access(params);
403 runtime->format = params_format(params);
404 runtime->subformat = params_subformat(params);
405 runtime->channels = params_channels(params);
406 runtime->rate = params_rate(params);
407 runtime->period_size = params_period_size(params);
408 runtime->periods = params_periods(params);
409 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 runtime->info = params->info;
411 runtime->rate_num = params->rate_num;
412 runtime->rate_den = params->rate_den;
413
414 bits = snd_pcm_format_physical_width(runtime->format);
415 runtime->sample_bits = bits;
416 bits *= runtime->channels;
417 runtime->frame_bits = bits;
418 frames = 1;
419 while (bits % 8 != 0) {
420 bits *= 2;
421 frames *= 2;
422 }
423 runtime->byte_align = bits / 8;
424 runtime->min_align = frames;
425
426 /* Default sw params */
427 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
428 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 runtime->start_threshold = 1;
431 runtime->stop_threshold = runtime->buffer_size;
432 runtime->silence_threshold = 0;
433 runtime->silence_size = 0;
434 runtime->boundary = runtime->buffer_size;
435 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
436 runtime->boundary *= 2;
437
438 snd_pcm_timer_resolution_change(substream);
439 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700440
Mark Grossf011e2e2008-02-04 22:30:09 -0800441 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
442 substream->latency_id);
Takashi Iwai9442e692006-09-30 23:27:19 -0700443 if ((usecs = period_to_usecs(runtime)) >= 0)
Mark Grossf011e2e2008-02-04 22:30:09 -0800444 pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY,
445 substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447 _error:
448 /* hardware might be unuseable from this time,
449 so we force application to retry to set
450 the correct hardware parameter settings */
451 runtime->status->state = SNDRV_PCM_STATE_OPEN;
452 if (substream->ops->hw_free != NULL)
453 substream->ops->hw_free(substream);
454 return err;
455}
456
Takashi Iwai877211f2005-11-17 13:59:38 +0100457static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
458 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Takashi Iwai877211f2005-11-17 13:59:38 +0100460 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 int err;
462
Li Zefanef44a1e2009-04-10 09:43:08 +0800463 params = memdup_user(_params, sizeof(*params));
464 if (IS_ERR(params))
465 return PTR_ERR(params);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 err = snd_pcm_hw_params(substream, params);
468 if (copy_to_user(_params, params, sizeof(*params))) {
469 if (!err)
470 err = -EFAULT;
471 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 kfree(params);
474 return err;
475}
476
Takashi Iwai877211f2005-11-17 13:59:38 +0100477static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Takashi Iwai877211f2005-11-17 13:59:38 +0100479 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int result = 0;
481
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200482 if (PCM_RUNTIME_CHECK(substream))
483 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 snd_pcm_stream_lock_irq(substream);
486 switch (runtime->status->state) {
487 case SNDRV_PCM_STATE_SETUP:
488 case SNDRV_PCM_STATE_PREPARED:
489 break;
490 default:
491 snd_pcm_stream_unlock_irq(substream);
492 return -EBADFD;
493 }
494 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200495 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return -EBADFD;
497 if (substream->ops->hw_free)
498 result = substream->ops->hw_free(substream);
499 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Mark Grossf011e2e2008-02-04 22:30:09 -0800500 pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY,
501 substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return result;
503}
504
Takashi Iwai877211f2005-11-17 13:59:38 +0100505static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
506 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Takashi Iwai877211f2005-11-17 13:59:38 +0100508 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200510 if (PCM_RUNTIME_CHECK(substream))
511 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 snd_pcm_stream_lock_irq(substream);
514 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
515 snd_pcm_stream_unlock_irq(substream);
516 return -EBADFD;
517 }
518 snd_pcm_stream_unlock_irq(substream);
519
520 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
521 return -EINVAL;
522 if (params->avail_min == 0)
523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (params->silence_size >= runtime->boundary) {
525 if (params->silence_threshold != 0)
526 return -EINVAL;
527 } else {
528 if (params->silence_size > params->silence_threshold)
529 return -EINVAL;
530 if (params->silence_threshold > runtime->buffer_size)
531 return -EINVAL;
532 }
533 snd_pcm_stream_lock_irq(substream);
534 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 runtime->period_step = params->period_step;
536 runtime->control->avail_min = params->avail_min;
537 runtime->start_threshold = params->start_threshold;
538 runtime->stop_threshold = params->stop_threshold;
539 runtime->silence_threshold = params->silence_threshold;
540 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 params->boundary = runtime->boundary;
542 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
544 runtime->silence_size > 0)
545 snd_pcm_playback_silence(substream, ULONG_MAX);
546 wake_up(&runtime->sleep);
547 }
548 snd_pcm_stream_unlock_irq(substream);
549 return 0;
550}
551
Takashi Iwai877211f2005-11-17 13:59:38 +0100552static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
553 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Takashi Iwai877211f2005-11-17 13:59:38 +0100555 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int err;
557 if (copy_from_user(&params, _params, sizeof(params)))
558 return -EFAULT;
559 err = snd_pcm_sw_params(substream, &params);
560 if (copy_to_user(_params, &params, sizeof(params)))
561 return -EFAULT;
562 return err;
563}
564
Takashi Iwai877211f2005-11-17 13:59:38 +0100565int snd_pcm_status(struct snd_pcm_substream *substream,
566 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Takashi Iwai877211f2005-11-17 13:59:38 +0100568 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 snd_pcm_stream_lock_irq(substream);
571 status->state = runtime->status->state;
572 status->suspended_state = runtime->status->suspended_state;
573 if (status->state == SNDRV_PCM_STATE_OPEN)
574 goto _end;
575 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100576 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100578 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
579 status->tstamp = runtime->status->tstamp;
580 goto _tstamp_end;
581 }
582 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100583 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100584 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 status->appl_ptr = runtime->control->appl_ptr;
586 status->hw_ptr = runtime->status->hw_ptr;
587 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
588 status->avail = snd_pcm_playback_avail(runtime);
589 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
590 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
591 status->delay = runtime->buffer_size - status->avail;
592 else
593 status->delay = 0;
594 } else {
595 status->avail = snd_pcm_capture_avail(runtime);
596 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
597 status->delay = status->avail;
598 else
599 status->delay = 0;
600 }
601 status->avail_max = runtime->avail_max;
602 status->overrange = runtime->overrange;
603 runtime->avail_max = 0;
604 runtime->overrange = 0;
605 _end:
606 snd_pcm_stream_unlock_irq(substream);
607 return 0;
608}
609
Takashi Iwai877211f2005-11-17 13:59:38 +0100610static int snd_pcm_status_user(struct snd_pcm_substream *substream,
611 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Takashi Iwai877211f2005-11-17 13:59:38 +0100613 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 int res;
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 memset(&status, 0, sizeof(status));
617 res = snd_pcm_status(substream, &status);
618 if (res < 0)
619 return res;
620 if (copy_to_user(_status, &status, sizeof(status)))
621 return -EFAULT;
622 return 0;
623}
624
Takashi Iwai877211f2005-11-17 13:59:38 +0100625static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
626 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwai877211f2005-11-17 13:59:38 +0100628 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 unsigned int channel;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 channel = info->channel;
632 runtime = substream->runtime;
633 snd_pcm_stream_lock_irq(substream);
634 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
635 snd_pcm_stream_unlock_irq(substream);
636 return -EBADFD;
637 }
638 snd_pcm_stream_unlock_irq(substream);
639 if (channel >= runtime->channels)
640 return -EINVAL;
641 memset(info, 0, sizeof(*info));
642 info->channel = channel;
643 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
644}
645
Takashi Iwai877211f2005-11-17 13:59:38 +0100646static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
647 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai877211f2005-11-17 13:59:38 +0100649 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int res;
651
652 if (copy_from_user(&info, _info, sizeof(info)))
653 return -EFAULT;
654 res = snd_pcm_channel_info(substream, &info);
655 if (res < 0)
656 return res;
657 if (copy_to_user(_info, &info, sizeof(info)))
658 return -EFAULT;
659 return 0;
660}
661
Takashi Iwai877211f2005-11-17 13:59:38 +0100662static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai877211f2005-11-17 13:59:38 +0100664 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (runtime->trigger_master == NULL)
666 return;
667 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100668 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 } else {
670 snd_pcm_trigger_tstamp(runtime->trigger_master);
671 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
672 }
673 runtime->trigger_master = NULL;
674}
675
676struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 int (*pre_action)(struct snd_pcm_substream *substream, int state);
678 int (*do_action)(struct snd_pcm_substream *substream, int state);
679 void (*undo_action)(struct snd_pcm_substream *substream, int state);
680 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
683/*
684 * this functions is core for handling of linked stream
685 * Note: the stream state might be changed also on failure
686 * Note2: call with calling stream lock + link lock
687 */
688static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100689 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int state, int do_lock)
691{
Takashi Iwai877211f2005-11-17 13:59:38 +0100692 struct snd_pcm_substream *s = NULL;
693 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int res = 0;
695
Takashi Iwaief991b92007-02-22 12:52:53 +0100696 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200698 spin_lock_nested(&s->self_group.lock,
699 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 res = ops->pre_action(s, state);
701 if (res < 0)
702 goto _unlock;
703 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100704 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 res = ops->do_action(s, state);
706 if (res < 0) {
707 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100708 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (s1 == s) /* failed stream */
710 break;
711 ops->undo_action(s1, state);
712 }
713 }
714 s = NULL; /* unlock all */
715 goto _unlock;
716 }
717 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100718 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ops->post_action(s, state);
720 }
721 _unlock:
722 if (do_lock) {
723 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100724 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (s1 != substream)
726 spin_unlock(&s1->self_group.lock);
727 if (s1 == s) /* end */
728 break;
729 }
730 }
731 return res;
732}
733
734/*
735 * Note: call with stream lock
736 */
737static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100738 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int state)
740{
741 int res;
742
743 res = ops->pre_action(substream, state);
744 if (res < 0)
745 return res;
746 res = ops->do_action(substream, state);
747 if (res == 0)
748 ops->post_action(substream, state);
749 else if (ops->undo_action)
750 ops->undo_action(substream, state);
751 return res;
752}
753
754/*
755 * Note: call with stream lock
756 */
757static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100758 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 int state)
760{
761 int res;
762
763 if (snd_pcm_stream_linked(substream)) {
764 if (!spin_trylock(&substream->group->lock)) {
765 spin_unlock(&substream->self_group.lock);
766 spin_lock(&substream->group->lock);
767 spin_lock(&substream->self_group.lock);
768 }
769 res = snd_pcm_action_group(ops, substream, state, 1);
770 spin_unlock(&substream->group->lock);
771 } else {
772 res = snd_pcm_action_single(ops, substream, state);
773 }
774 return res;
775}
776
777/*
778 * Note: don't use any locks before
779 */
780static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100781 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int state)
783{
784 int res;
785
786 read_lock_irq(&snd_pcm_link_rwlock);
787 if (snd_pcm_stream_linked(substream)) {
788 spin_lock(&substream->group->lock);
789 spin_lock(&substream->self_group.lock);
790 res = snd_pcm_action_group(ops, substream, state, 1);
791 spin_unlock(&substream->self_group.lock);
792 spin_unlock(&substream->group->lock);
793 } else {
794 spin_lock(&substream->self_group.lock);
795 res = snd_pcm_action_single(ops, substream, state);
796 spin_unlock(&substream->self_group.lock);
797 }
798 read_unlock_irq(&snd_pcm_link_rwlock);
799 return res;
800}
801
802/*
803 */
804static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100805 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int state)
807{
808 int res;
809
810 down_read(&snd_pcm_link_rwsem);
811 if (snd_pcm_stream_linked(substream))
812 res = snd_pcm_action_group(ops, substream, state, 0);
813 else
814 res = snd_pcm_action_single(ops, substream, state);
815 up_read(&snd_pcm_link_rwsem);
816 return res;
817}
818
819/*
820 * start callbacks
821 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100822static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai877211f2005-11-17 13:59:38 +0100824 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
826 return -EBADFD;
827 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
828 !snd_pcm_playback_data(substream))
829 return -EPIPE;
830 runtime->trigger_master = substream;
831 return 0;
832}
833
Takashi Iwai877211f2005-11-17 13:59:38 +0100834static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 if (substream->runtime->trigger_master != substream)
837 return 0;
838 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
839}
840
Takashi Iwai877211f2005-11-17 13:59:38 +0100841static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 if (substream->runtime->trigger_master == substream)
844 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
845}
846
Takashi Iwai877211f2005-11-17 13:59:38 +0100847static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Takashi Iwai877211f2005-11-17 13:59:38 +0100849 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200851 runtime->hw_ptr_jiffies = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 runtime->status->state = state;
853 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
854 runtime->silence_size > 0)
855 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100857 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
858 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861static struct action_ops snd_pcm_action_start = {
862 .pre_action = snd_pcm_pre_start,
863 .do_action = snd_pcm_do_start,
864 .undo_action = snd_pcm_undo_start,
865 .post_action = snd_pcm_post_start
866};
867
868/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700869 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200870 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100872int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwai877211f2005-11-17 13:59:38 +0100874 return snd_pcm_action(&snd_pcm_action_start, substream,
875 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/*
879 * stop callbacks
880 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100881static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Takashi Iwai877211f2005-11-17 13:59:38 +0100883 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
885 return -EBADFD;
886 runtime->trigger_master = substream;
887 return 0;
888}
889
Takashi Iwai877211f2005-11-17 13:59:38 +0100890static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 if (substream->runtime->trigger_master == substream &&
893 snd_pcm_running(substream))
894 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
895 return 0; /* unconditonally stop all substreams */
896}
897
Takashi Iwai877211f2005-11-17 13:59:38 +0100898static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Takashi Iwai877211f2005-11-17 13:59:38 +0100900 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (runtime->status->state != state) {
902 snd_pcm_trigger_tstamp(substream);
903 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100904 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
905 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908 wake_up(&runtime->sleep);
909}
910
911static struct action_ops snd_pcm_action_stop = {
912 .pre_action = snd_pcm_pre_stop,
913 .do_action = snd_pcm_do_stop,
914 .post_action = snd_pcm_post_stop
915};
916
917/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700918 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200919 * @substream: the PCM substream instance
920 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700922 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100924int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
927}
928
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200929EXPORT_SYMBOL(snd_pcm_stop);
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700932 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200933 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700935 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Unlike snd_pcm_stop(), this affects only the given stream.
937 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100938int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Takashi Iwai877211f2005-11-17 13:59:38 +0100940 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
941 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944/*
945 * pause callbacks
946 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100947static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
951 return -ENOSYS;
952 if (push) {
953 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
954 return -EBADFD;
955 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
956 return -EBADFD;
957 runtime->trigger_master = substream;
958 return 0;
959}
960
Takashi Iwai877211f2005-11-17 13:59:38 +0100961static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 if (substream->runtime->trigger_master != substream)
964 return 0;
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200965 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
966 * a delta betwen the current jiffies, this gives a large enough
967 * delta, effectively to skip the check once.
968 */
969 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return substream->ops->trigger(substream,
971 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
972 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
973}
974
Takashi Iwai877211f2005-11-17 13:59:38 +0100975static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 if (substream->runtime->trigger_master == substream)
978 substream->ops->trigger(substream,
979 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
980 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
981}
982
Takashi Iwai877211f2005-11-17 13:59:38 +0100983static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Takashi Iwai877211f2005-11-17 13:59:38 +0100985 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 snd_pcm_trigger_tstamp(substream);
987 if (push) {
988 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
989 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100990 snd_timer_notify(substream->timer,
991 SNDRV_TIMER_EVENT_MPAUSE,
992 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 wake_up(&runtime->sleep);
994 } else {
995 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100997 snd_timer_notify(substream->timer,
998 SNDRV_TIMER_EVENT_MCONTINUE,
999 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001}
1002
1003static struct action_ops snd_pcm_action_pause = {
1004 .pre_action = snd_pcm_pre_pause,
1005 .do_action = snd_pcm_do_pause,
1006 .undo_action = snd_pcm_undo_pause,
1007 .post_action = snd_pcm_post_pause
1008};
1009
1010/*
1011 * Push/release the pause for all linked streams.
1012 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001013static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1016}
1017
1018#ifdef CONFIG_PM
1019/* suspend */
1020
Takashi Iwai877211f2005-11-17 13:59:38 +01001021static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Takashi Iwai877211f2005-11-17 13:59:38 +01001023 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1025 return -EBUSY;
1026 runtime->trigger_master = substream;
1027 return 0;
1028}
1029
Takashi Iwai877211f2005-11-17 13:59:38 +01001030static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Takashi Iwai877211f2005-11-17 13:59:38 +01001032 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (runtime->trigger_master != substream)
1034 return 0;
1035 if (! snd_pcm_running(substream))
1036 return 0;
1037 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1038 return 0; /* suspend unconditionally */
1039}
1040
Takashi Iwai877211f2005-11-17 13:59:38 +01001041static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Takashi Iwai877211f2005-11-17 13:59:38 +01001043 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 snd_pcm_trigger_tstamp(substream);
1045 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001046 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1047 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 runtime->status->suspended_state = runtime->status->state;
1049 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 wake_up(&runtime->sleep);
1051}
1052
1053static struct action_ops snd_pcm_action_suspend = {
1054 .pre_action = snd_pcm_pre_suspend,
1055 .do_action = snd_pcm_do_suspend,
1056 .post_action = snd_pcm_post_suspend
1057};
1058
1059/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001060 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001061 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * After this call, all streams are changed to SUSPENDED state.
1064 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001065int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 int err;
1068 unsigned long flags;
1069
Takashi Iwai603bf522005-11-17 15:59:14 +01001070 if (! substream)
1071 return 0;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 snd_pcm_stream_lock_irqsave(substream, flags);
1074 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1075 snd_pcm_stream_unlock_irqrestore(substream, flags);
1076 return err;
1077}
1078
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001079EXPORT_SYMBOL(snd_pcm_suspend);
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001082 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001083 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * After this call, all streams are changed to SUSPENDED state.
1086 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001087int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Takashi Iwai877211f2005-11-17 13:59:38 +01001089 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int stream, err = 0;
1091
Takashi Iwai603bf522005-11-17 15:59:14 +01001092 if (! pcm)
1093 return 0;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001096 for (substream = pcm->streams[stream].substream;
1097 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* FIXME: the open/close code should lock this as well */
1099 if (substream->runtime == NULL)
1100 continue;
1101 err = snd_pcm_suspend(substream);
1102 if (err < 0 && err != -EBUSY)
1103 return err;
1104 }
1105 }
1106 return 0;
1107}
1108
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001109EXPORT_SYMBOL(snd_pcm_suspend_all);
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/* resume */
1112
Takashi Iwai877211f2005-11-17 13:59:38 +01001113static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Takashi Iwai877211f2005-11-17 13:59:38 +01001115 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1117 return -ENOSYS;
1118 runtime->trigger_master = substream;
1119 return 0;
1120}
1121
Takashi Iwai877211f2005-11-17 13:59:38 +01001122static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (runtime->trigger_master != substream)
1126 return 0;
1127 /* DMA not running previously? */
1128 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1129 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1130 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1131 return 0;
1132 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1133}
1134
Takashi Iwai877211f2005-11-17 13:59:38 +01001135static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 if (substream->runtime->trigger_master == substream &&
1138 snd_pcm_running(substream))
1139 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1140}
1141
Takashi Iwai877211f2005-11-17 13:59:38 +01001142static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Takashi Iwai877211f2005-11-17 13:59:38 +01001144 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 snd_pcm_trigger_tstamp(substream);
1146 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001147 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1148 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150}
1151
1152static struct action_ops snd_pcm_action_resume = {
1153 .pre_action = snd_pcm_pre_resume,
1154 .do_action = snd_pcm_do_resume,
1155 .undo_action = snd_pcm_undo_resume,
1156 .post_action = snd_pcm_post_resume
1157};
1158
Takashi Iwai877211f2005-11-17 13:59:38 +01001159static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Takashi Iwai877211f2005-11-17 13:59:38 +01001161 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int res;
1163
1164 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001165 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1167 snd_power_unlock(card);
1168 return res;
1169}
1170
1171#else
1172
Takashi Iwai877211f2005-11-17 13:59:38 +01001173static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 return -ENOSYS;
1176}
1177
1178#endif /* CONFIG_PM */
1179
1180/*
1181 * xrun ioctl
1182 *
1183 * Change the RUNNING stream(s) to XRUN state.
1184 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001185static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Takashi Iwai877211f2005-11-17 13:59:38 +01001187 struct snd_card *card = substream->pcm->card;
1188 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int result;
1190
1191 snd_power_lock(card);
1192 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001193 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 if (result < 0)
1195 goto _unlock;
1196 }
1197
1198 snd_pcm_stream_lock_irq(substream);
1199 switch (runtime->status->state) {
1200 case SNDRV_PCM_STATE_XRUN:
1201 result = 0; /* already there */
1202 break;
1203 case SNDRV_PCM_STATE_RUNNING:
1204 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1205 break;
1206 default:
1207 result = -EBADFD;
1208 }
1209 snd_pcm_stream_unlock_irq(substream);
1210 _unlock:
1211 snd_power_unlock(card);
1212 return result;
1213}
1214
1215/*
1216 * reset ioctl
1217 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001218static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Takashi Iwai877211f2005-11-17 13:59:38 +01001220 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 switch (runtime->status->state) {
1222 case SNDRV_PCM_STATE_RUNNING:
1223 case SNDRV_PCM_STATE_PREPARED:
1224 case SNDRV_PCM_STATE_PAUSED:
1225 case SNDRV_PCM_STATE_SUSPENDED:
1226 return 0;
1227 default:
1228 return -EBADFD;
1229 }
1230}
1231
Takashi Iwai877211f2005-11-17 13:59:38 +01001232static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Takashi Iwai877211f2005-11-17 13:59:38 +01001234 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1236 if (err < 0)
1237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001239 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1240 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 runtime->silence_start = runtime->status->hw_ptr;
1242 runtime->silence_filled = 0;
1243 return 0;
1244}
1245
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 runtime->control->appl_ptr = runtime->status->hw_ptr;
1250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1251 runtime->silence_size > 0)
1252 snd_pcm_playback_silence(substream, ULONG_MAX);
1253}
1254
1255static struct action_ops snd_pcm_action_reset = {
1256 .pre_action = snd_pcm_pre_reset,
1257 .do_action = snd_pcm_do_reset,
1258 .post_action = snd_pcm_post_reset
1259};
1260
Takashi Iwai877211f2005-11-17 13:59:38 +01001261static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1264}
1265
1266/*
1267 * prepare ioctl
1268 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001269/* we use the second argument for updating f_flags */
1270static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1271 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Takashi Iwai877211f2005-11-17 13:59:38 +01001273 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001274 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1275 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return -EBADFD;
1277 if (snd_pcm_running(substream))
1278 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001279 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281}
1282
Takashi Iwai877211f2005-11-17 13:59:38 +01001283static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 int err;
1286 err = substream->ops->prepare(substream);
1287 if (err < 0)
1288 return err;
1289 return snd_pcm_do_reset(substream, 0);
1290}
1291
Takashi Iwai877211f2005-11-17 13:59:38 +01001292static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Takashi Iwai877211f2005-11-17 13:59:38 +01001294 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 runtime->control->appl_ptr = runtime->status->hw_ptr;
1296 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1297}
1298
1299static struct action_ops snd_pcm_action_prepare = {
1300 .pre_action = snd_pcm_pre_prepare,
1301 .do_action = snd_pcm_do_prepare,
1302 .post_action = snd_pcm_post_prepare
1303};
1304
1305/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001306 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001307 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001308 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001310static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1311 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001314 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001315 int f_flags;
1316
1317 if (file)
1318 f_flags = file->f_flags;
1319 else
1320 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
1322 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001323 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001324 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1325 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 snd_power_unlock(card);
1327 return res;
1328}
1329
1330/*
1331 * drain ioctl
1332 */
1333
Takashi Iwai877211f2005-11-17 13:59:38 +01001334static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001336 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return -EAGAIN;
1338 substream->runtime->trigger_master = substream;
1339 return 0;
1340}
1341
Takashi Iwai877211f2005-11-17 13:59:38 +01001342static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1346 switch (runtime->status->state) {
1347 case SNDRV_PCM_STATE_PREPARED:
1348 /* start playback stream if possible */
1349 if (! snd_pcm_playback_empty(substream)) {
1350 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1351 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1352 }
1353 break;
1354 case SNDRV_PCM_STATE_RUNNING:
1355 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1356 break;
1357 default:
1358 break;
1359 }
1360 } else {
1361 /* stop running stream */
1362 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001363 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001365 snd_pcm_do_stop(substream, new_state);
1366 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368 }
1369 return 0;
1370}
1371
Takashi Iwai877211f2005-11-17 13:59:38 +01001372static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374}
1375
1376static struct action_ops snd_pcm_action_drain_init = {
1377 .pre_action = snd_pcm_pre_drain_init,
1378 .do_action = snd_pcm_do_drain_init,
1379 .post_action = snd_pcm_post_drain_init
1380};
1381
1382struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001383 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 wait_queue_t wait;
1385 snd_pcm_uframes_t stop_threshold;
1386};
1387
Takashi Iwai877211f2005-11-17 13:59:38 +01001388static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390/*
1391 * Drain the stream(s).
1392 * When the substream is linked, sync until the draining of all playback streams
1393 * is finished.
1394 * After this call, all streams are supposed to be either SETUP or DRAINING
1395 * (capture only) state.
1396 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001397static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Takashi Iwai877211f2005-11-17 13:59:38 +01001399 struct snd_card *card;
1400 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001401 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 int result = 0;
1403 int i, num_drecs;
1404 struct drain_rec *drec, drec_tmp, *d;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 card = substream->pcm->card;
1407 runtime = substream->runtime;
1408
1409 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1410 return -EBADFD;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 snd_power_lock(card);
1413 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001414 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001415 if (result < 0) {
1416 snd_power_unlock(card);
1417 return result;
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
1421 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001422 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (snd_pcm_stream_linked(substream)) {
1424 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1425 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001426 up_read(&snd_pcm_link_rwsem);
1427 snd_power_unlock(card);
1428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
1430 } else
1431 drec = &drec_tmp;
1432
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001433 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001435 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1438 d = &drec[num_drecs++];
1439 d->substream = s;
1440 init_waitqueue_entry(&d->wait, current);
1441 add_wait_queue(&runtime->sleep, &d->wait);
1442 /* stop_threshold fixup to avoid endless loop when
1443 * stop_threshold > buffer_size
1444 */
1445 d->stop_threshold = runtime->stop_threshold;
1446 if (runtime->stop_threshold > runtime->buffer_size)
1447 runtime->stop_threshold = runtime->buffer_size;
1448 }
1449 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001450 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001451
1452 snd_pcm_stream_lock_irq(substream);
1453 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001454 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001455 snd_pcm_pause(substream, 0);
1456
1457 /* pre-start/stop - all running streams are changed to DRAINING state */
1458 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1459 if (result < 0) {
1460 snd_pcm_stream_unlock_irq(substream);
1461 goto _error;
1462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 for (;;) {
1465 long tout;
1466 if (signal_pending(current)) {
1467 result = -ERESTARTSYS;
1468 break;
1469 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001470 /* all finished? */
1471 for (i = 0; i < num_drecs; i++) {
1472 runtime = drec[i].substream->runtime;
1473 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1474 break;
1475 }
1476 if (i == num_drecs)
1477 break; /* yes, all drained */
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 set_current_state(TASK_INTERRUPTIBLE);
1480 snd_pcm_stream_unlock_irq(substream);
1481 snd_power_unlock(card);
1482 tout = schedule_timeout(10 * HZ);
1483 snd_power_lock(card);
1484 snd_pcm_stream_lock_irq(substream);
1485 if (tout == 0) {
1486 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1487 result = -ESTRPIPE;
1488 else {
1489 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1490 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1491 result = -EIO;
1492 }
1493 break;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001496
1497 snd_pcm_stream_unlock_irq(substream);
1498
1499 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 for (i = 0; i < num_drecs; i++) {
1501 d = &drec[i];
1502 runtime = d->substream->runtime;
1503 remove_wait_queue(&runtime->sleep, &d->wait);
1504 runtime->stop_threshold = d->stop_threshold;
1505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (drec != &drec_tmp)
1508 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 return result;
1512}
1513
1514/*
1515 * drop ioctl
1516 *
1517 * Immediately put all linked substreams into SETUP state.
1518 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001519static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai877211f2005-11-17 13:59:38 +01001521 struct snd_pcm_runtime *runtime;
1522 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int result = 0;
1524
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001525 if (PCM_RUNTIME_CHECK(substream))
1526 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 runtime = substream->runtime;
1528 card = substream->pcm->card;
1529
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001530 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001531 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1532 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EBADFD;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 snd_pcm_stream_lock_irq(substream);
1536 /* resume pause */
1537 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1538 snd_pcm_pause(substream, 0);
1539
1540 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1541 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1542 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return result;
1545}
1546
1547
1548/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549static struct file *snd_pcm_file_fd(int fd)
1550{
1551 struct file *file;
1552 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001553 unsigned int minor;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 file = fget(fd);
1556 if (!file)
1557 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001558 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!S_ISCHR(inode->i_mode) ||
1560 imajor(inode) != snd_major) {
1561 fput(file);
1562 return NULL;
1563 }
1564 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001565 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1566 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 fput(file);
1568 return NULL;
1569 }
1570 return file;
1571}
1572
1573/*
1574 * PCM link handling
1575 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001576static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int res = 0;
1579 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001580 struct snd_pcm_file *pcm_file;
1581 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 file = snd_pcm_file_fd(fd);
1584 if (!file)
1585 return -EBADFD;
1586 pcm_file = file->private_data;
1587 substream1 = pcm_file->substream;
1588 down_write(&snd_pcm_link_rwsem);
1589 write_lock_irq(&snd_pcm_link_rwlock);
1590 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1591 substream->runtime->status->state != substream1->runtime->status->state) {
1592 res = -EBADFD;
1593 goto _end;
1594 }
1595 if (snd_pcm_stream_linked(substream1)) {
1596 res = -EALREADY;
1597 goto _end;
1598 }
1599 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001600 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (substream->group == NULL) {
1602 res = -ENOMEM;
1603 goto _end;
1604 }
1605 spin_lock_init(&substream->group->lock);
1606 INIT_LIST_HEAD(&substream->group->substreams);
1607 list_add_tail(&substream->link_list, &substream->group->substreams);
1608 substream->group->count = 1;
1609 }
1610 list_add_tail(&substream1->link_list, &substream->group->substreams);
1611 substream->group->count++;
1612 substream1->group = substream->group;
1613 _end:
1614 write_unlock_irq(&snd_pcm_link_rwlock);
1615 up_write(&snd_pcm_link_rwsem);
1616 fput(file);
1617 return res;
1618}
1619
Takashi Iwai877211f2005-11-17 13:59:38 +01001620static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 substream->group = &substream->self_group;
1623 INIT_LIST_HEAD(&substream->self_group.substreams);
1624 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1625}
1626
Takashi Iwai877211f2005-11-17 13:59:38 +01001627static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwaief991b92007-02-22 12:52:53 +01001629 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int res = 0;
1631
1632 down_write(&snd_pcm_link_rwsem);
1633 write_lock_irq(&snd_pcm_link_rwlock);
1634 if (!snd_pcm_stream_linked(substream)) {
1635 res = -EALREADY;
1636 goto _end;
1637 }
1638 list_del(&substream->link_list);
1639 substream->group->count--;
1640 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001641 snd_pcm_group_for_each_entry(s, substream) {
1642 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 }
1645 kfree(substream->group);
1646 }
1647 relink_to_local(substream);
1648 _end:
1649 write_unlock_irq(&snd_pcm_link_rwlock);
1650 up_write(&snd_pcm_link_rwsem);
1651 return res;
1652}
1653
1654/*
1655 * hw configurator
1656 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001657static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1658 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai877211f2005-11-17 13:59:38 +01001660 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1662 hw_param_interval_c(params, rule->deps[1]), &t);
1663 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1664}
1665
Takashi Iwai877211f2005-11-17 13:59:38 +01001666static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1667 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Takashi Iwai877211f2005-11-17 13:59:38 +01001669 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1671 hw_param_interval_c(params, rule->deps[1]), &t);
1672 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1673}
1674
Takashi Iwai877211f2005-11-17 13:59:38 +01001675static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1676 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Takashi Iwai877211f2005-11-17 13:59:38 +01001678 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1680 hw_param_interval_c(params, rule->deps[1]),
1681 (unsigned long) rule->private, &t);
1682 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1683}
1684
Takashi Iwai877211f2005-11-17 13:59:38 +01001685static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1686 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwai877211f2005-11-17 13:59:38 +01001688 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1690 (unsigned long) rule->private,
1691 hw_param_interval_c(params, rule->deps[1]), &t);
1692 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1693}
1694
Takashi Iwai877211f2005-11-17 13:59:38 +01001695static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1696 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001699 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1700 struct snd_mask m;
1701 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 snd_mask_any(&m);
1703 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1704 int bits;
1705 if (! snd_mask_test(mask, k))
1706 continue;
1707 bits = snd_pcm_format_physical_width(k);
1708 if (bits <= 0)
1709 continue; /* ignore invalid formats */
1710 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1711 snd_mask_reset(&m, k);
1712 }
1713 return snd_mask_refine(mask, &m);
1714}
1715
Takashi Iwai877211f2005-11-17 13:59:38 +01001716static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1717 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Takashi Iwai877211f2005-11-17 13:59:38 +01001719 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned int k;
1721 t.min = UINT_MAX;
1722 t.max = 0;
1723 t.openmin = 0;
1724 t.openmax = 0;
1725 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1726 int bits;
1727 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1728 continue;
1729 bits = snd_pcm_format_physical_width(k);
1730 if (bits <= 0)
1731 continue; /* ignore invalid formats */
1732 if (t.min > (unsigned)bits)
1733 t.min = bits;
1734 if (t.max < (unsigned)bits)
1735 t.max = bits;
1736 }
1737 t.integer = 1;
1738 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1739}
1740
1741#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1742#error "Change this table"
1743#endif
1744
1745static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1746 48000, 64000, 88200, 96000, 176400, 192000 };
1747
Clemens Ladisch7653d552007-08-13 17:38:54 +02001748const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1749 .count = ARRAY_SIZE(rates),
1750 .list = rates,
1751};
1752
Takashi Iwai877211f2005-11-17 13:59:38 +01001753static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai877211f2005-11-17 13:59:38 +01001756 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001758 snd_pcm_known_rates.count,
1759 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Takashi Iwai877211f2005-11-17 13:59:38 +01001762static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1763 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwai877211f2005-11-17 13:59:38 +01001765 struct snd_interval t;
1766 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 t.min = 0;
1768 t.max = substream->buffer_bytes_max;
1769 t.openmin = 0;
1770 t.openmax = 0;
1771 t.integer = 1;
1772 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1773}
1774
Takashi Iwai877211f2005-11-17 13:59:38 +01001775int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai877211f2005-11-17 13:59:38 +01001777 struct snd_pcm_runtime *runtime = substream->runtime;
1778 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int k, err;
1780
1781 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1782 snd_mask_any(constrs_mask(constrs, k));
1783 }
1784
1785 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1786 snd_interval_any(constrs_interval(constrs, k));
1787 }
1788
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1794
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1796 snd_pcm_hw_rule_format, NULL,
1797 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1798 if (err < 0)
1799 return err;
1800 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1801 snd_pcm_hw_rule_sample_bits, NULL,
1802 SNDRV_PCM_HW_PARAM_FORMAT,
1803 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1804 if (err < 0)
1805 return err;
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1807 snd_pcm_hw_rule_div, NULL,
1808 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1809 if (err < 0)
1810 return err;
1811 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1812 snd_pcm_hw_rule_mul, NULL,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1814 if (err < 0)
1815 return err;
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1817 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1818 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1819 if (err < 0)
1820 return err;
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1822 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1823 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1827 snd_pcm_hw_rule_div, NULL,
1828 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1833 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1837 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1838 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1853 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1857 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1862 snd_pcm_hw_rule_mul, NULL,
1863 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1872 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1877 snd_pcm_hw_rule_muldivk, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1882 snd_pcm_hw_rule_muldivk, (void*) 8,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1894 if (err < 0)
1895 return err;
1896 return 0;
1897}
1898
Takashi Iwai877211f2005-11-17 13:59:38 +01001899int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwai877211f2005-11-17 13:59:38 +01001901 struct snd_pcm_runtime *runtime = substream->runtime;
1902 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int err;
1904 unsigned int mask = 0;
1905
1906 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1911 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1912 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1913 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1914 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1915 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1916 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1917 }
1918 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001919 if (err < 0)
1920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001923 if (err < 0)
1924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001927 if (err < 0)
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1931 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001932 if (err < 0)
1933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1936 hw->rate_min, hw->rate_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001937 if (err < 0)
1938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1941 hw->period_bytes_min, hw->period_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001942 if (err < 0)
1943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1946 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001947 if (err < 0)
1948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1951 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 if (err < 0)
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1956 snd_pcm_hw_rule_buffer_bytes_max, substream,
1957 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1958 if (err < 0)
1959 return err;
1960
1961 /* FIXME: remove */
1962 if (runtime->dma_bytes) {
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001964 if (err < 0)
1965 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
1968 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1969 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1970 snd_pcm_hw_rule_rate, hw,
1971 SNDRV_PCM_HW_PARAM_RATE, -1);
1972 if (err < 0)
1973 return err;
1974 }
1975
1976 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1978
1979 return 0;
1980}
1981
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001982static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001985}
1986
1987void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1988{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001989 substream->ref_count--;
1990 if (substream->ref_count > 0)
1991 return;
1992
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001993 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001994 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (substream->ops->hw_free != NULL)
1996 substream->ops->hw_free(substream);
1997 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001998 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Takashi Iwai15762742006-04-06 19:47:42 +02002000 if (substream->pcm_release) {
2001 substream->pcm_release(substream);
2002 substream->pcm_release = NULL;
2003 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002004 snd_pcm_detach_substream(substream);
2005}
2006
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002007EXPORT_SYMBOL(snd_pcm_release_substream);
2008
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002009int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2010 struct file *file,
2011 struct snd_pcm_substream **rsubstream)
2012{
2013 struct snd_pcm_substream *substream;
2014 int err;
2015
2016 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2017 if (err < 0)
2018 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002019 if (substream->ref_count > 1) {
2020 *rsubstream = substream;
2021 return 0;
2022 }
2023
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002024 err = snd_pcm_hw_constraints_init(substream);
2025 if (err < 0) {
2026 snd_printd("snd_pcm_hw_constraints_init failed\n");
2027 goto error;
2028 }
2029
2030 if ((err = substream->ops->open(substream)) < 0)
2031 goto error;
2032
2033 substream->hw_opened = 1;
2034
2035 err = snd_pcm_hw_constraints_complete(substream);
2036 if (err < 0) {
2037 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2038 goto error;
2039 }
2040
2041 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002043
2044 error:
2045 snd_pcm_release_substream(substream);
2046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002049EXPORT_SYMBOL(snd_pcm_open_substream);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002052 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002054 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm_file *pcm_file;
2057 struct snd_pcm_substream *substream;
2058 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002059 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002061 if (rpcm_file)
2062 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002064 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2065 if (err < 0)
2066 return err;
2067
Takashi Iwai548a6482006-07-31 16:51:51 +02002068 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2069 if (pcm_file == NULL) {
2070 snd_pcm_release_substream(substream);
2071 return -ENOMEM;
2072 }
2073 pcm_file->substream = substream;
2074 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002075 str = substream->pstr;
2076 substream->file = pcm_file;
2077 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002080 if (rpcm_file)
2081 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
2084
Clemens Ladischf87135f2005-11-20 14:06:59 +01002085static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Takashi Iwai877211f2005-11-17 13:59:38 +01002087 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002088
2089 pcm = snd_lookup_minor_data(iminor(inode),
2090 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2091 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2092}
2093
2094static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2095{
2096 struct snd_pcm *pcm;
2097
2098 pcm = snd_lookup_minor_data(iminor(inode),
2099 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2100 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2101}
2102
2103static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2104{
2105 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002106 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 wait_queue_t wait;
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (pcm == NULL) {
2110 err = -ENODEV;
2111 goto __error1;
2112 }
2113 err = snd_card_file_add(pcm->card, file);
2114 if (err < 0)
2115 goto __error1;
2116 if (!try_module_get(pcm->card->module)) {
2117 err = -EFAULT;
2118 goto __error2;
2119 }
2120 init_waitqueue_entry(&wait, current);
2121 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002122 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002124 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (err >= 0)
2126 break;
2127 if (err == -EAGAIN) {
2128 if (file->f_flags & O_NONBLOCK) {
2129 err = -EBUSY;
2130 break;
2131 }
2132 } else
2133 break;
2134 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002135 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002137 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (signal_pending(current)) {
2139 err = -ERESTARTSYS;
2140 break;
2141 }
2142 }
2143 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002144 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (err < 0)
2146 goto __error;
2147 return err;
2148
2149 __error:
2150 module_put(pcm->card->module);
2151 __error2:
2152 snd_card_file_remove(pcm->card, file);
2153 __error1:
2154 return err;
2155}
2156
2157static int snd_pcm_release(struct inode *inode, struct file *file)
2158{
Takashi Iwai877211f2005-11-17 13:59:38 +01002159 struct snd_pcm *pcm;
2160 struct snd_pcm_substream *substream;
2161 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 pcm_file = file->private_data;
2164 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002165 if (snd_BUG_ON(!substream))
2166 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002168 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002169 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002170 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002171 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 wake_up(&pcm->open_wait);
2173 module_put(pcm->card->module);
2174 snd_card_file_remove(pcm->card, file);
2175 return 0;
2176}
2177
Takashi Iwai877211f2005-11-17 13:59:38 +01002178static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2179 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Takashi Iwai877211f2005-11-17 13:59:38 +01002181 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 snd_pcm_sframes_t appl_ptr;
2183 snd_pcm_sframes_t ret;
2184 snd_pcm_sframes_t hw_avail;
2185
2186 if (frames == 0)
2187 return 0;
2188
2189 snd_pcm_stream_lock_irq(substream);
2190 switch (runtime->status->state) {
2191 case SNDRV_PCM_STATE_PREPARED:
2192 break;
2193 case SNDRV_PCM_STATE_DRAINING:
2194 case SNDRV_PCM_STATE_RUNNING:
2195 if (snd_pcm_update_hw_ptr(substream) >= 0)
2196 break;
2197 /* Fall through */
2198 case SNDRV_PCM_STATE_XRUN:
2199 ret = -EPIPE;
2200 goto __end;
2201 default:
2202 ret = -EBADFD;
2203 goto __end;
2204 }
2205
2206 hw_avail = snd_pcm_playback_hw_avail(runtime);
2207 if (hw_avail <= 0) {
2208 ret = 0;
2209 goto __end;
2210 }
2211 if (frames > (snd_pcm_uframes_t)hw_avail)
2212 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 appl_ptr = runtime->control->appl_ptr - frames;
2214 if (appl_ptr < 0)
2215 appl_ptr += runtime->boundary;
2216 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 ret = frames;
2218 __end:
2219 snd_pcm_stream_unlock_irq(substream);
2220 return ret;
2221}
2222
Takashi Iwai877211f2005-11-17 13:59:38 +01002223static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2224 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Takashi Iwai877211f2005-11-17 13:59:38 +01002226 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 snd_pcm_sframes_t appl_ptr;
2228 snd_pcm_sframes_t ret;
2229 snd_pcm_sframes_t hw_avail;
2230
2231 if (frames == 0)
2232 return 0;
2233
2234 snd_pcm_stream_lock_irq(substream);
2235 switch (runtime->status->state) {
2236 case SNDRV_PCM_STATE_PREPARED:
2237 case SNDRV_PCM_STATE_DRAINING:
2238 break;
2239 case SNDRV_PCM_STATE_RUNNING:
2240 if (snd_pcm_update_hw_ptr(substream) >= 0)
2241 break;
2242 /* Fall through */
2243 case SNDRV_PCM_STATE_XRUN:
2244 ret = -EPIPE;
2245 goto __end;
2246 default:
2247 ret = -EBADFD;
2248 goto __end;
2249 }
2250
2251 hw_avail = snd_pcm_capture_hw_avail(runtime);
2252 if (hw_avail <= 0) {
2253 ret = 0;
2254 goto __end;
2255 }
2256 if (frames > (snd_pcm_uframes_t)hw_avail)
2257 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 appl_ptr = runtime->control->appl_ptr - frames;
2259 if (appl_ptr < 0)
2260 appl_ptr += runtime->boundary;
2261 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 ret = frames;
2263 __end:
2264 snd_pcm_stream_unlock_irq(substream);
2265 return ret;
2266}
2267
Takashi Iwai877211f2005-11-17 13:59:38 +01002268static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2269 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Takashi Iwai877211f2005-11-17 13:59:38 +01002271 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 snd_pcm_sframes_t appl_ptr;
2273 snd_pcm_sframes_t ret;
2274 snd_pcm_sframes_t avail;
2275
2276 if (frames == 0)
2277 return 0;
2278
2279 snd_pcm_stream_lock_irq(substream);
2280 switch (runtime->status->state) {
2281 case SNDRV_PCM_STATE_PREPARED:
2282 case SNDRV_PCM_STATE_PAUSED:
2283 break;
2284 case SNDRV_PCM_STATE_DRAINING:
2285 case SNDRV_PCM_STATE_RUNNING:
2286 if (snd_pcm_update_hw_ptr(substream) >= 0)
2287 break;
2288 /* Fall through */
2289 case SNDRV_PCM_STATE_XRUN:
2290 ret = -EPIPE;
2291 goto __end;
2292 default:
2293 ret = -EBADFD;
2294 goto __end;
2295 }
2296
2297 avail = snd_pcm_playback_avail(runtime);
2298 if (avail <= 0) {
2299 ret = 0;
2300 goto __end;
2301 }
2302 if (frames > (snd_pcm_uframes_t)avail)
2303 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 appl_ptr = runtime->control->appl_ptr + frames;
2305 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2306 appl_ptr -= runtime->boundary;
2307 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 ret = frames;
2309 __end:
2310 snd_pcm_stream_unlock_irq(substream);
2311 return ret;
2312}
2313
Takashi Iwai877211f2005-11-17 13:59:38 +01002314static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2315 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
Takashi Iwai877211f2005-11-17 13:59:38 +01002317 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 snd_pcm_sframes_t appl_ptr;
2319 snd_pcm_sframes_t ret;
2320 snd_pcm_sframes_t avail;
2321
2322 if (frames == 0)
2323 return 0;
2324
2325 snd_pcm_stream_lock_irq(substream);
2326 switch (runtime->status->state) {
2327 case SNDRV_PCM_STATE_PREPARED:
2328 case SNDRV_PCM_STATE_DRAINING:
2329 case SNDRV_PCM_STATE_PAUSED:
2330 break;
2331 case SNDRV_PCM_STATE_RUNNING:
2332 if (snd_pcm_update_hw_ptr(substream) >= 0)
2333 break;
2334 /* Fall through */
2335 case SNDRV_PCM_STATE_XRUN:
2336 ret = -EPIPE;
2337 goto __end;
2338 default:
2339 ret = -EBADFD;
2340 goto __end;
2341 }
2342
2343 avail = snd_pcm_capture_avail(runtime);
2344 if (avail <= 0) {
2345 ret = 0;
2346 goto __end;
2347 }
2348 if (frames > (snd_pcm_uframes_t)avail)
2349 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 appl_ptr = runtime->control->appl_ptr + frames;
2351 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2352 appl_ptr -= runtime->boundary;
2353 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 ret = frames;
2355 __end:
2356 snd_pcm_stream_unlock_irq(substream);
2357 return ret;
2358}
2359
Takashi Iwai877211f2005-11-17 13:59:38 +01002360static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Takashi Iwai877211f2005-11-17 13:59:38 +01002362 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 int err;
2364
2365 snd_pcm_stream_lock_irq(substream);
2366 switch (runtime->status->state) {
2367 case SNDRV_PCM_STATE_DRAINING:
2368 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2369 goto __badfd;
2370 case SNDRV_PCM_STATE_RUNNING:
2371 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2372 break;
2373 /* Fall through */
2374 case SNDRV_PCM_STATE_PREPARED:
2375 case SNDRV_PCM_STATE_SUSPENDED:
2376 err = 0;
2377 break;
2378 case SNDRV_PCM_STATE_XRUN:
2379 err = -EPIPE;
2380 break;
2381 default:
2382 __badfd:
2383 err = -EBADFD;
2384 break;
2385 }
2386 snd_pcm_stream_unlock_irq(substream);
2387 return err;
2388}
2389
Takashi Iwai877211f2005-11-17 13:59:38 +01002390static int snd_pcm_delay(struct snd_pcm_substream *substream,
2391 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Takashi Iwai877211f2005-11-17 13:59:38 +01002393 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 int err;
2395 snd_pcm_sframes_t n = 0;
2396
2397 snd_pcm_stream_lock_irq(substream);
2398 switch (runtime->status->state) {
2399 case SNDRV_PCM_STATE_DRAINING:
2400 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2401 goto __badfd;
2402 case SNDRV_PCM_STATE_RUNNING:
2403 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2404 break;
2405 /* Fall through */
2406 case SNDRV_PCM_STATE_PREPARED:
2407 case SNDRV_PCM_STATE_SUSPENDED:
2408 err = 0;
2409 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2410 n = snd_pcm_playback_hw_avail(runtime);
2411 else
2412 n = snd_pcm_capture_avail(runtime);
2413 break;
2414 case SNDRV_PCM_STATE_XRUN:
2415 err = -EPIPE;
2416 break;
2417 default:
2418 __badfd:
2419 err = -EBADFD;
2420 break;
2421 }
2422 snd_pcm_stream_unlock_irq(substream);
2423 if (!err)
2424 if (put_user(n, res))
2425 err = -EFAULT;
2426 return err;
2427}
2428
Takashi Iwai877211f2005-11-17 13:59:38 +01002429static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2430 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Takashi Iwai877211f2005-11-17 13:59:38 +01002432 struct snd_pcm_runtime *runtime = substream->runtime;
2433 struct snd_pcm_sync_ptr sync_ptr;
2434 volatile struct snd_pcm_mmap_status *status;
2435 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int err;
2437
2438 memset(&sync_ptr, 0, sizeof(sync_ptr));
2439 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2440 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002441 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return -EFAULT;
2443 status = runtime->status;
2444 control = runtime->control;
2445 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2446 err = snd_pcm_hwsync(substream);
2447 if (err < 0)
2448 return err;
2449 }
2450 snd_pcm_stream_lock_irq(substream);
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2452 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2453 else
2454 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2455 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2456 control->avail_min = sync_ptr.c.control.avail_min;
2457 else
2458 sync_ptr.c.control.avail_min = control->avail_min;
2459 sync_ptr.s.status.state = status->state;
2460 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2461 sync_ptr.s.status.tstamp = status->tstamp;
2462 sync_ptr.s.status.suspended_state = status->suspended_state;
2463 snd_pcm_stream_unlock_irq(substream);
2464 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2465 return -EFAULT;
2466 return 0;
2467}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002468
2469static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2470{
2471 struct snd_pcm_runtime *runtime = substream->runtime;
2472 int arg;
2473
2474 if (get_user(arg, _arg))
2475 return -EFAULT;
2476 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2477 return -EINVAL;
2478 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2479 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2480 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2481 return 0;
2482}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Takashi Iwai0df63e42006-04-28 15:13:41 +02002484static int snd_pcm_common_ioctl1(struct file *file,
2485 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 unsigned int cmd, void __user *arg)
2487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 switch (cmd) {
2489 case SNDRV_PCM_IOCTL_PVERSION:
2490 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2491 case SNDRV_PCM_IOCTL_INFO:
2492 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002493 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2494 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002495 case SNDRV_PCM_IOCTL_TTSTAMP:
2496 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 case SNDRV_PCM_IOCTL_HW_REFINE:
2498 return snd_pcm_hw_refine_user(substream, arg);
2499 case SNDRV_PCM_IOCTL_HW_PARAMS:
2500 return snd_pcm_hw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_HW_FREE:
2502 return snd_pcm_hw_free(substream);
2503 case SNDRV_PCM_IOCTL_SW_PARAMS:
2504 return snd_pcm_sw_params_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_STATUS:
2506 return snd_pcm_status_user(substream, arg);
2507 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2508 return snd_pcm_channel_info_user(substream, arg);
2509 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002510 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 case SNDRV_PCM_IOCTL_RESET:
2512 return snd_pcm_reset(substream);
2513 case SNDRV_PCM_IOCTL_START:
2514 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2515 case SNDRV_PCM_IOCTL_LINK:
2516 return snd_pcm_link(substream, (int)(unsigned long) arg);
2517 case SNDRV_PCM_IOCTL_UNLINK:
2518 return snd_pcm_unlink(substream);
2519 case SNDRV_PCM_IOCTL_RESUME:
2520 return snd_pcm_resume(substream);
2521 case SNDRV_PCM_IOCTL_XRUN:
2522 return snd_pcm_xrun(substream);
2523 case SNDRV_PCM_IOCTL_HWSYNC:
2524 return snd_pcm_hwsync(substream);
2525 case SNDRV_PCM_IOCTL_DELAY:
2526 return snd_pcm_delay(substream, arg);
2527 case SNDRV_PCM_IOCTL_SYNC_PTR:
2528 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002529#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2531 return snd_pcm_hw_refine_old_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2533 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002534#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 case SNDRV_PCM_IOCTL_DRAIN:
2536 return snd_pcm_drain(substream);
2537 case SNDRV_PCM_IOCTL_DROP:
2538 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002539 case SNDRV_PCM_IOCTL_PAUSE:
2540 {
2541 int res;
2542 snd_pcm_stream_lock_irq(substream);
2543 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2544 snd_pcm_stream_unlock_irq(substream);
2545 return res;
2546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 snd_printd("unknown ioctl = 0x%x\n", cmd);
2549 return -ENOTTY;
2550}
2551
Takashi Iwai0df63e42006-04-28 15:13:41 +02002552static int snd_pcm_playback_ioctl1(struct file *file,
2553 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 unsigned int cmd, void __user *arg)
2555{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002556 if (snd_BUG_ON(!substream))
2557 return -ENXIO;
2558 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2559 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 switch (cmd) {
2561 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2562 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002563 struct snd_xferi xferi;
2564 struct snd_xferi __user *_xferi = arg;
2565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 snd_pcm_sframes_t result;
2567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2568 return -EBADFD;
2569 if (put_user(0, &_xferi->result))
2570 return -EFAULT;
2571 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2572 return -EFAULT;
2573 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2574 __put_user(result, &_xferi->result);
2575 return result < 0 ? result : 0;
2576 }
2577 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2578 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002579 struct snd_xfern xfern;
2580 struct snd_xfern __user *_xfern = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 void __user **bufs;
2583 snd_pcm_sframes_t result;
2584 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2585 return -EBADFD;
2586 if (runtime->channels > 128)
2587 return -EINVAL;
2588 if (put_user(0, &_xfern->result))
2589 return -EFAULT;
2590 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2591 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002592
2593 bufs = memdup_user(xfern.bufs,
2594 sizeof(void *) * runtime->channels);
2595 if (IS_ERR(bufs))
2596 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2598 kfree(bufs);
2599 __put_user(result, &_xfern->result);
2600 return result < 0 ? result : 0;
2601 }
2602 case SNDRV_PCM_IOCTL_REWIND:
2603 {
2604 snd_pcm_uframes_t frames;
2605 snd_pcm_uframes_t __user *_frames = arg;
2606 snd_pcm_sframes_t result;
2607 if (get_user(frames, _frames))
2608 return -EFAULT;
2609 if (put_user(0, _frames))
2610 return -EFAULT;
2611 result = snd_pcm_playback_rewind(substream, frames);
2612 __put_user(result, _frames);
2613 return result < 0 ? result : 0;
2614 }
2615 case SNDRV_PCM_IOCTL_FORWARD:
2616 {
2617 snd_pcm_uframes_t frames;
2618 snd_pcm_uframes_t __user *_frames = arg;
2619 snd_pcm_sframes_t result;
2620 if (get_user(frames, _frames))
2621 return -EFAULT;
2622 if (put_user(0, _frames))
2623 return -EFAULT;
2624 result = snd_pcm_playback_forward(substream, frames);
2625 __put_user(result, _frames);
2626 return result < 0 ? result : 0;
2627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002629 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Takashi Iwai0df63e42006-04-28 15:13:41 +02002632static int snd_pcm_capture_ioctl1(struct file *file,
2633 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 unsigned int cmd, void __user *arg)
2635{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002636 if (snd_BUG_ON(!substream))
2637 return -ENXIO;
2638 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 switch (cmd) {
2641 case SNDRV_PCM_IOCTL_READI_FRAMES:
2642 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002643 struct snd_xferi xferi;
2644 struct snd_xferi __user *_xferi = arg;
2645 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 snd_pcm_sframes_t result;
2647 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2648 return -EBADFD;
2649 if (put_user(0, &_xferi->result))
2650 return -EFAULT;
2651 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2652 return -EFAULT;
2653 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2654 __put_user(result, &_xferi->result);
2655 return result < 0 ? result : 0;
2656 }
2657 case SNDRV_PCM_IOCTL_READN_FRAMES:
2658 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002659 struct snd_xfern xfern;
2660 struct snd_xfern __user *_xfern = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 void *bufs;
2663 snd_pcm_sframes_t result;
2664 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2665 return -EBADFD;
2666 if (runtime->channels > 128)
2667 return -EINVAL;
2668 if (put_user(0, &_xfern->result))
2669 return -EFAULT;
2670 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2671 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002672
2673 bufs = memdup_user(xfern.bufs,
2674 sizeof(void *) * runtime->channels);
2675 if (IS_ERR(bufs))
2676 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2678 kfree(bufs);
2679 __put_user(result, &_xfern->result);
2680 return result < 0 ? result : 0;
2681 }
2682 case SNDRV_PCM_IOCTL_REWIND:
2683 {
2684 snd_pcm_uframes_t frames;
2685 snd_pcm_uframes_t __user *_frames = arg;
2686 snd_pcm_sframes_t result;
2687 if (get_user(frames, _frames))
2688 return -EFAULT;
2689 if (put_user(0, _frames))
2690 return -EFAULT;
2691 result = snd_pcm_capture_rewind(substream, frames);
2692 __put_user(result, _frames);
2693 return result < 0 ? result : 0;
2694 }
2695 case SNDRV_PCM_IOCTL_FORWARD:
2696 {
2697 snd_pcm_uframes_t frames;
2698 snd_pcm_uframes_t __user *_frames = arg;
2699 snd_pcm_sframes_t result;
2700 if (get_user(frames, _frames))
2701 return -EFAULT;
2702 if (put_user(0, _frames))
2703 return -EFAULT;
2704 result = snd_pcm_capture_forward(substream, frames);
2705 __put_user(result, _frames);
2706 return result < 0 ? result : 0;
2707 }
2708 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002709 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Takashi Iwai877211f2005-11-17 13:59:38 +01002712static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2713 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Takashi Iwai877211f2005-11-17 13:59:38 +01002715 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 pcm_file = file->private_data;
2718
2719 if (((cmd >> 8) & 0xff) != 'A')
2720 return -ENOTTY;
2721
Takashi Iwai0df63e42006-04-28 15:13:41 +02002722 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2723 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Takashi Iwai877211f2005-11-17 13:59:38 +01002726static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2727 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Takashi Iwai877211f2005-11-17 13:59:38 +01002729 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 pcm_file = file->private_data;
2732
2733 if (((cmd >> 8) & 0xff) != 'A')
2734 return -ENOTTY;
2735
Takashi Iwai0df63e42006-04-28 15:13:41 +02002736 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2737 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Takashi Iwai877211f2005-11-17 13:59:38 +01002740int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 unsigned int cmd, void *arg)
2742{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002743 mm_segment_t fs;
2744 int result;
2745
2746 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 switch (substream->stream) {
2748 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002749 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2750 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002751 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002753 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2754 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002757 result = -EINVAL;
2758 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002760 snd_leave_user(fs);
2761 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002764EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2765
Takashi Iwai877211f2005-11-17 13:59:38 +01002766static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2767 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Takashi Iwai877211f2005-11-17 13:59:38 +01002769 struct snd_pcm_file *pcm_file;
2770 struct snd_pcm_substream *substream;
2771 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 snd_pcm_sframes_t result;
2773
2774 pcm_file = file->private_data;
2775 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002776 if (PCM_RUNTIME_CHECK(substream))
2777 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 runtime = substream->runtime;
2779 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2780 return -EBADFD;
2781 if (!frame_aligned(runtime, count))
2782 return -EINVAL;
2783 count = bytes_to_frames(runtime, count);
2784 result = snd_pcm_lib_read(substream, buf, count);
2785 if (result > 0)
2786 result = frames_to_bytes(runtime, result);
2787 return result;
2788}
2789
Takashi Iwai877211f2005-11-17 13:59:38 +01002790static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2791 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Takashi Iwai877211f2005-11-17 13:59:38 +01002793 struct snd_pcm_file *pcm_file;
2794 struct snd_pcm_substream *substream;
2795 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 snd_pcm_sframes_t result;
2797
2798 pcm_file = file->private_data;
2799 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002800 if (PCM_RUNTIME_CHECK(substream))
2801 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002803 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2804 return -EBADFD;
2805 if (!frame_aligned(runtime, count))
2806 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 count = bytes_to_frames(runtime, count);
2808 result = snd_pcm_lib_write(substream, buf, count);
2809 if (result > 0)
2810 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return result;
2812}
2813
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002814static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2815 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817{
Takashi Iwai877211f2005-11-17 13:59:38 +01002818 struct snd_pcm_file *pcm_file;
2819 struct snd_pcm_substream *substream;
2820 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 snd_pcm_sframes_t result;
2822 unsigned long i;
2823 void __user **bufs;
2824 snd_pcm_uframes_t frames;
2825
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002826 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002828 if (PCM_RUNTIME_CHECK(substream))
2829 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 runtime = substream->runtime;
2831 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2832 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002833 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002835 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002837 frames = bytes_to_samples(runtime, iov->iov_len);
2838 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (bufs == NULL)
2840 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002841 for (i = 0; i < nr_segs; ++i)
2842 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 result = snd_pcm_lib_readv(substream, bufs, frames);
2844 if (result > 0)
2845 result = frames_to_bytes(runtime, result);
2846 kfree(bufs);
2847 return result;
2848}
2849
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002850static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2851 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Takashi Iwai877211f2005-11-17 13:59:38 +01002853 struct snd_pcm_file *pcm_file;
2854 struct snd_pcm_substream *substream;
2855 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 snd_pcm_sframes_t result;
2857 unsigned long i;
2858 void __user **bufs;
2859 snd_pcm_uframes_t frames;
2860
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002861 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002863 if (PCM_RUNTIME_CHECK(substream))
2864 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002869 !frame_aligned(runtime, iov->iov_len))
2870 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002871 frames = bytes_to_samples(runtime, iov->iov_len);
2872 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (bufs == NULL)
2874 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002875 for (i = 0; i < nr_segs; ++i)
2876 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 result = snd_pcm_lib_writev(substream, bufs, frames);
2878 if (result > 0)
2879 result = frames_to_bytes(runtime, result);
2880 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return result;
2882}
2883
2884static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2885{
Takashi Iwai877211f2005-11-17 13:59:38 +01002886 struct snd_pcm_file *pcm_file;
2887 struct snd_pcm_substream *substream;
2888 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 unsigned int mask;
2890 snd_pcm_uframes_t avail;
2891
2892 pcm_file = file->private_data;
2893
2894 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002895 if (PCM_RUNTIME_CHECK(substream))
2896 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 runtime = substream->runtime;
2898
2899 poll_wait(file, &runtime->sleep, wait);
2900
2901 snd_pcm_stream_lock_irq(substream);
2902 avail = snd_pcm_playback_avail(runtime);
2903 switch (runtime->status->state) {
2904 case SNDRV_PCM_STATE_RUNNING:
2905 case SNDRV_PCM_STATE_PREPARED:
2906 case SNDRV_PCM_STATE_PAUSED:
2907 if (avail >= runtime->control->avail_min) {
2908 mask = POLLOUT | POLLWRNORM;
2909 break;
2910 }
2911 /* Fall through */
2912 case SNDRV_PCM_STATE_DRAINING:
2913 mask = 0;
2914 break;
2915 default:
2916 mask = POLLOUT | POLLWRNORM | POLLERR;
2917 break;
2918 }
2919 snd_pcm_stream_unlock_irq(substream);
2920 return mask;
2921}
2922
2923static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2924{
Takashi Iwai877211f2005-11-17 13:59:38 +01002925 struct snd_pcm_file *pcm_file;
2926 struct snd_pcm_substream *substream;
2927 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned int mask;
2929 snd_pcm_uframes_t avail;
2930
2931 pcm_file = file->private_data;
2932
2933 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002934 if (PCM_RUNTIME_CHECK(substream))
2935 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 runtime = substream->runtime;
2937
2938 poll_wait(file, &runtime->sleep, wait);
2939
2940 snd_pcm_stream_lock_irq(substream);
2941 avail = snd_pcm_capture_avail(runtime);
2942 switch (runtime->status->state) {
2943 case SNDRV_PCM_STATE_RUNNING:
2944 case SNDRV_PCM_STATE_PREPARED:
2945 case SNDRV_PCM_STATE_PAUSED:
2946 if (avail >= runtime->control->avail_min) {
2947 mask = POLLIN | POLLRDNORM;
2948 break;
2949 }
2950 mask = 0;
2951 break;
2952 case SNDRV_PCM_STATE_DRAINING:
2953 if (avail > 0) {
2954 mask = POLLIN | POLLRDNORM;
2955 break;
2956 }
2957 /* Fall through */
2958 default:
2959 mask = POLLIN | POLLRDNORM | POLLERR;
2960 break;
2961 }
2962 snd_pcm_stream_unlock_irq(substream);
2963 return mask;
2964}
2965
2966/*
2967 * mmap support
2968 */
2969
2970/*
2971 * Only on coherent architectures, we can mmap the status and the control records
2972 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2973 */
2974#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2975/*
2976 * mmap status record
2977 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002978static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2979 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
Takashi Iwai877211f2005-11-17 13:59:38 +01002981 struct snd_pcm_substream *substream = area->vm_private_data;
2982 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002985 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002987 vmf->page = virt_to_page(runtime->status);
2988 get_page(vmf->page);
2989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
2992static struct vm_operations_struct snd_pcm_vm_ops_status =
2993{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002994 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995};
2996
Takashi Iwai877211f2005-11-17 13:59:38 +01002997static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct vm_area_struct *area)
2999{
Takashi Iwai877211f2005-11-17 13:59:38 +01003000 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 long size;
3002 if (!(area->vm_flags & VM_READ))
3003 return -EINVAL;
3004 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003006 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return -EINVAL;
3008 area->vm_ops = &snd_pcm_vm_ops_status;
3009 area->vm_private_data = substream;
3010 area->vm_flags |= VM_RESERVED;
3011 return 0;
3012}
3013
3014/*
3015 * mmap control record
3016 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003017static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3018 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Takashi Iwai877211f2005-11-17 13:59:38 +01003020 struct snd_pcm_substream *substream = area->vm_private_data;
3021 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003024 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003026 vmf->page = virt_to_page(runtime->control);
3027 get_page(vmf->page);
3028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029}
3030
3031static struct vm_operations_struct snd_pcm_vm_ops_control =
3032{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003033 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034};
3035
Takashi Iwai877211f2005-11-17 13:59:38 +01003036static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 struct vm_area_struct *area)
3038{
Takashi Iwai877211f2005-11-17 13:59:38 +01003039 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 long size;
3041 if (!(area->vm_flags & VM_READ))
3042 return -EINVAL;
3043 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003045 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return -EINVAL;
3047 area->vm_ops = &snd_pcm_vm_ops_control;
3048 area->vm_private_data = substream;
3049 area->vm_flags |= VM_RESERVED;
3050 return 0;
3051}
3052#else /* ! coherent mmap */
3053/*
3054 * don't support mmap for status and control records.
3055 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003056static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct vm_area_struct *area)
3058{
3059 return -ENXIO;
3060}
Takashi Iwai877211f2005-11-17 13:59:38 +01003061static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 struct vm_area_struct *area)
3063{
3064 return -ENXIO;
3065}
3066#endif /* coherent mmap */
3067
3068/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003069 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003071static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3072 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Takashi Iwai877211f2005-11-17 13:59:38 +01003074 struct snd_pcm_substream *substream = area->vm_private_data;
3075 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 unsigned long offset;
3077 struct page * page;
3078 void *vaddr;
3079 size_t dma_bytes;
3080
3081 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003082 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003084 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3086 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003087 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (substream->ops->page) {
3089 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003090 if (!page)
3091 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 } else {
3093 vaddr = runtime->dma_area + offset;
3094 page = virt_to_page(vaddr);
3095 }
Nick Pigginb5810032005-10-29 18:16:12 -07003096 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003097 vmf->page = page;
3098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
3101static struct vm_operations_struct snd_pcm_vm_ops_data =
3102{
3103 .open = snd_pcm_mmap_data_open,
3104 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003105 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106};
3107
3108/*
3109 * mmap the DMA buffer on RAM
3110 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003111static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3112 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 area->vm_ops = &snd_pcm_vm_ops_data;
3115 area->vm_private_data = substream;
3116 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003117 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return 0;
3119}
3120
3121/*
3122 * mmap the DMA buffer on I/O memory area
3123 */
3124#if SNDRV_PCM_INFO_MMAP_IOMEM
3125static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3126{
3127 .open = snd_pcm_mmap_data_open,
3128 .close = snd_pcm_mmap_data_close,
3129};
3130
Takashi Iwai877211f2005-11-17 13:59:38 +01003131int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3132 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
3134 long size;
3135 unsigned long offset;
3136
3137#ifdef pgprot_noncached
3138 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3139#endif
3140 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3141 area->vm_private_data = substream;
3142 area->vm_flags |= VM_IO;
3143 size = area->vm_end - area->vm_start;
3144 offset = area->vm_pgoff << PAGE_SHIFT;
3145 if (io_remap_pfn_range(area, area->vm_start,
3146 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3147 size, area->vm_page_prot))
3148 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003149 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return 0;
3151}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003152
3153EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154#endif /* SNDRV_PCM_INFO_MMAP */
3155
3156/*
3157 * mmap DMA buffer
3158 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003159int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 struct vm_area_struct *area)
3161{
Takashi Iwai877211f2005-11-17 13:59:38 +01003162 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 long size;
3164 unsigned long offset;
3165 size_t dma_bytes;
3166
3167 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3168 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3169 return -EINVAL;
3170 } else {
3171 if (!(area->vm_flags & VM_READ))
3172 return -EINVAL;
3173 }
3174 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3176 return -EBADFD;
3177 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3178 return -ENXIO;
3179 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3180 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3181 return -EINVAL;
3182 size = area->vm_end - area->vm_start;
3183 offset = area->vm_pgoff << PAGE_SHIFT;
3184 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3185 if ((size_t)size > dma_bytes)
3186 return -EINVAL;
3187 if (offset > dma_bytes - size)
3188 return -EINVAL;
3189
3190 if (substream->ops->mmap)
3191 return substream->ops->mmap(substream, area);
3192 else
3193 return snd_pcm_default_mmap(substream, area);
3194}
3195
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003196EXPORT_SYMBOL(snd_pcm_mmap_data);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3199{
Takashi Iwai877211f2005-11-17 13:59:38 +01003200 struct snd_pcm_file * pcm_file;
3201 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 unsigned long offset;
3203
3204 pcm_file = file->private_data;
3205 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003206 if (PCM_RUNTIME_CHECK(substream))
3207 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 offset = area->vm_pgoff << PAGE_SHIFT;
3210 switch (offset) {
3211 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003212 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 return -ENXIO;
3214 return snd_pcm_mmap_status(substream, file, area);
3215 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003216 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return -ENXIO;
3218 return snd_pcm_mmap_control(substream, file, area);
3219 default:
3220 return snd_pcm_mmap_data(substream, file, area);
3221 }
3222 return 0;
3223}
3224
3225static int snd_pcm_fasync(int fd, struct file * file, int on)
3226{
Takashi Iwai877211f2005-11-17 13:59:38 +01003227 struct snd_pcm_file * pcm_file;
3228 struct snd_pcm_substream *substream;
3229 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003230 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003232 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 pcm_file = file->private_data;
3234 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003235 if (PCM_RUNTIME_CHECK(substream))
3236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003239out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003240 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244/*
3245 * ioctl32 compat
3246 */
3247#ifdef CONFIG_COMPAT
3248#include "pcm_compat.c"
3249#else
3250#define snd_pcm_ioctl_compat NULL
3251#endif
3252
3253/*
3254 * To be removed helpers to keep binary compatibility
3255 */
3256
Takashi Iwai59d48582005-12-01 10:51:58 +01003257#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3259#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3260
Takashi Iwai877211f2005-11-17 13:59:38 +01003261static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3262 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 unsigned int i;
3265
3266 memset(params, 0, sizeof(*params));
3267 params->flags = oparams->flags;
3268 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3269 params->masks[i].bits[0] = oparams->masks[i];
3270 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3271 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3272 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3273 params->info = oparams->info;
3274 params->msbits = oparams->msbits;
3275 params->rate_num = oparams->rate_num;
3276 params->rate_den = oparams->rate_den;
3277 params->fifo_size = oparams->fifo_size;
3278}
3279
Takashi Iwai877211f2005-11-17 13:59:38 +01003280static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3281 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282{
3283 unsigned int i;
3284
3285 memset(oparams, 0, sizeof(*oparams));
3286 oparams->flags = params->flags;
3287 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3288 oparams->masks[i] = params->masks[i].bits[0];
3289 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3290 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3291 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3292 oparams->info = params->info;
3293 oparams->msbits = params->msbits;
3294 oparams->rate_num = params->rate_num;
3295 oparams->rate_den = params->rate_den;
3296 oparams->fifo_size = params->fifo_size;
3297}
3298
Takashi Iwai877211f2005-11-17 13:59:38 +01003299static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3300 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301{
Takashi Iwai877211f2005-11-17 13:59:38 +01003302 struct snd_pcm_hw_params *params;
3303 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 int err;
3305
3306 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003307 if (!params)
3308 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Li Zefanef44a1e2009-04-10 09:43:08 +08003310 oparams = memdup_user(_oparams, sizeof(*oparams));
3311 if (IS_ERR(oparams)) {
3312 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 goto out;
3314 }
3315 snd_pcm_hw_convert_from_old_params(params, oparams);
3316 err = snd_pcm_hw_refine(substream, params);
3317 snd_pcm_hw_convert_to_old_params(oparams, params);
3318 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3319 if (!err)
3320 err = -EFAULT;
3321 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003322
3323 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324out:
3325 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return err;
3327}
3328
Takashi Iwai877211f2005-11-17 13:59:38 +01003329static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3330 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Takashi Iwai877211f2005-11-17 13:59:38 +01003332 struct snd_pcm_hw_params *params;
3333 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 int err;
3335
3336 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003337 if (!params)
3338 return -ENOMEM;
3339
3340 oparams = memdup_user(_oparams, sizeof(*oparams));
3341 if (IS_ERR(oparams)) {
3342 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 goto out;
3344 }
3345 snd_pcm_hw_convert_from_old_params(params, oparams);
3346 err = snd_pcm_hw_params(substream, params);
3347 snd_pcm_hw_convert_to_old_params(oparams, params);
3348 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3349 if (!err)
3350 err = -EFAULT;
3351 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003352
3353 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354out:
3355 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return err;
3357}
Takashi Iwai59d48582005-12-01 10:51:58 +01003358#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Cliff Cai70036092008-09-03 10:54:36 +02003360#ifndef CONFIG_MMU
3361unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3362 unsigned long len, unsigned long pgoff,
3363 unsigned long flags)
3364{
3365 return 0;
3366}
3367#else
3368# define dummy_get_unmapped_area NULL
3369#endif
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/*
3372 * Register section
3373 */
3374
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003375const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003377 .owner = THIS_MODULE,
3378 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003379 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003380 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003381 .release = snd_pcm_release,
3382 .poll = snd_pcm_playback_poll,
3383 .unlocked_ioctl = snd_pcm_playback_ioctl,
3384 .compat_ioctl = snd_pcm_ioctl_compat,
3385 .mmap = snd_pcm_mmap,
3386 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003387 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 },
3389 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003390 .owner = THIS_MODULE,
3391 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003392 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003393 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003394 .release = snd_pcm_release,
3395 .poll = snd_pcm_capture_poll,
3396 .unlocked_ioctl = snd_pcm_capture_ioctl,
3397 .compat_ioctl = snd_pcm_ioctl_compat,
3398 .mmap = snd_pcm_mmap,
3399 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003400 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 }
3402};