blob: cb769d415db7af60bca68cbccd583042f3d04c37 [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 ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200590 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200592 status->delay += runtime->delay;
593 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 status->delay = 0;
595 } else {
596 status->avail = snd_pcm_capture_avail(runtime);
597 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200598 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 else
600 status->delay = 0;
601 }
602 status->avail_max = runtime->avail_max;
603 status->overrange = runtime->overrange;
604 runtime->avail_max = 0;
605 runtime->overrange = 0;
606 _end:
607 snd_pcm_stream_unlock_irq(substream);
608 return 0;
609}
610
Takashi Iwai877211f2005-11-17 13:59:38 +0100611static int snd_pcm_status_user(struct snd_pcm_substream *substream,
612 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Takashi Iwai877211f2005-11-17 13:59:38 +0100614 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int res;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 memset(&status, 0, sizeof(status));
618 res = snd_pcm_status(substream, &status);
619 if (res < 0)
620 return res;
621 if (copy_to_user(_status, &status, sizeof(status)))
622 return -EFAULT;
623 return 0;
624}
625
Takashi Iwai877211f2005-11-17 13:59:38 +0100626static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
627 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Takashi Iwai877211f2005-11-17 13:59:38 +0100629 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 unsigned int channel;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 channel = info->channel;
633 runtime = substream->runtime;
634 snd_pcm_stream_lock_irq(substream);
635 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
636 snd_pcm_stream_unlock_irq(substream);
637 return -EBADFD;
638 }
639 snd_pcm_stream_unlock_irq(substream);
640 if (channel >= runtime->channels)
641 return -EINVAL;
642 memset(info, 0, sizeof(*info));
643 info->channel = channel;
644 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
645}
646
Takashi Iwai877211f2005-11-17 13:59:38 +0100647static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
648 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai877211f2005-11-17 13:59:38 +0100650 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int res;
652
653 if (copy_from_user(&info, _info, sizeof(info)))
654 return -EFAULT;
655 res = snd_pcm_channel_info(substream, &info);
656 if (res < 0)
657 return res;
658 if (copy_to_user(_info, &info, sizeof(info)))
659 return -EFAULT;
660 return 0;
661}
662
Takashi Iwai877211f2005-11-17 13:59:38 +0100663static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Takashi Iwai877211f2005-11-17 13:59:38 +0100665 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (runtime->trigger_master == NULL)
667 return;
668 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100669 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 } else {
671 snd_pcm_trigger_tstamp(runtime->trigger_master);
672 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
673 }
674 runtime->trigger_master = NULL;
675}
676
677struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100678 int (*pre_action)(struct snd_pcm_substream *substream, int state);
679 int (*do_action)(struct snd_pcm_substream *substream, int state);
680 void (*undo_action)(struct snd_pcm_substream *substream, int state);
681 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682};
683
684/*
685 * this functions is core for handling of linked stream
686 * Note: the stream state might be changed also on failure
687 * Note2: call with calling stream lock + link lock
688 */
689static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100690 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int state, int do_lock)
692{
Takashi Iwai877211f2005-11-17 13:59:38 +0100693 struct snd_pcm_substream *s = NULL;
694 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int res = 0;
696
Takashi Iwaief991b92007-02-22 12:52:53 +0100697 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200699 spin_lock_nested(&s->self_group.lock,
700 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 res = ops->pre_action(s, state);
702 if (res < 0)
703 goto _unlock;
704 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100705 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 res = ops->do_action(s, state);
707 if (res < 0) {
708 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100709 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (s1 == s) /* failed stream */
711 break;
712 ops->undo_action(s1, state);
713 }
714 }
715 s = NULL; /* unlock all */
716 goto _unlock;
717 }
718 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100719 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ops->post_action(s, state);
721 }
722 _unlock:
723 if (do_lock) {
724 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100725 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (s1 != substream)
727 spin_unlock(&s1->self_group.lock);
728 if (s1 == s) /* end */
729 break;
730 }
731 }
732 return res;
733}
734
735/*
736 * Note: call with stream lock
737 */
738static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100739 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 int state)
741{
742 int res;
743
744 res = ops->pre_action(substream, state);
745 if (res < 0)
746 return res;
747 res = ops->do_action(substream, state);
748 if (res == 0)
749 ops->post_action(substream, state);
750 else if (ops->undo_action)
751 ops->undo_action(substream, state);
752 return res;
753}
754
755/*
756 * Note: call with stream lock
757 */
758static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100759 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int state)
761{
762 int res;
763
764 if (snd_pcm_stream_linked(substream)) {
765 if (!spin_trylock(&substream->group->lock)) {
766 spin_unlock(&substream->self_group.lock);
767 spin_lock(&substream->group->lock);
768 spin_lock(&substream->self_group.lock);
769 }
770 res = snd_pcm_action_group(ops, substream, state, 1);
771 spin_unlock(&substream->group->lock);
772 } else {
773 res = snd_pcm_action_single(ops, substream, state);
774 }
775 return res;
776}
777
778/*
779 * Note: don't use any locks before
780 */
781static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100782 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 int state)
784{
785 int res;
786
787 read_lock_irq(&snd_pcm_link_rwlock);
788 if (snd_pcm_stream_linked(substream)) {
789 spin_lock(&substream->group->lock);
790 spin_lock(&substream->self_group.lock);
791 res = snd_pcm_action_group(ops, substream, state, 1);
792 spin_unlock(&substream->self_group.lock);
793 spin_unlock(&substream->group->lock);
794 } else {
795 spin_lock(&substream->self_group.lock);
796 res = snd_pcm_action_single(ops, substream, state);
797 spin_unlock(&substream->self_group.lock);
798 }
799 read_unlock_irq(&snd_pcm_link_rwlock);
800 return res;
801}
802
803/*
804 */
805static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100806 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int state)
808{
809 int res;
810
811 down_read(&snd_pcm_link_rwsem);
812 if (snd_pcm_stream_linked(substream))
813 res = snd_pcm_action_group(ops, substream, state, 0);
814 else
815 res = snd_pcm_action_single(ops, substream, state);
816 up_read(&snd_pcm_link_rwsem);
817 return res;
818}
819
820/*
821 * start callbacks
822 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100823static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Takashi Iwai877211f2005-11-17 13:59:38 +0100825 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
827 return -EBADFD;
828 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
829 !snd_pcm_playback_data(substream))
830 return -EPIPE;
831 runtime->trigger_master = substream;
832 return 0;
833}
834
Takashi Iwai877211f2005-11-17 13:59:38 +0100835static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 if (substream->runtime->trigger_master != substream)
838 return 0;
839 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
840}
841
Takashi Iwai877211f2005-11-17 13:59:38 +0100842static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 if (substream->runtime->trigger_master == substream)
845 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
846}
847
Takashi Iwai877211f2005-11-17 13:59:38 +0100848static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
Takashi Iwai877211f2005-11-17 13:59:38 +0100850 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 snd_pcm_trigger_tstamp(substream);
852 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;
965 return substream->ops->trigger(substream,
966 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
967 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
968}
969
Takashi Iwai877211f2005-11-17 13:59:38 +0100970static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 if (substream->runtime->trigger_master == substream)
973 substream->ops->trigger(substream,
974 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
975 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
976}
977
Takashi Iwai877211f2005-11-17 13:59:38 +0100978static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Takashi Iwai877211f2005-11-17 13:59:38 +0100980 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 snd_pcm_trigger_tstamp(substream);
982 if (push) {
983 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
984 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100985 snd_timer_notify(substream->timer,
986 SNDRV_TIMER_EVENT_MPAUSE,
987 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 wake_up(&runtime->sleep);
989 } else {
990 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100992 snd_timer_notify(substream->timer,
993 SNDRV_TIMER_EVENT_MCONTINUE,
994 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996}
997
998static struct action_ops snd_pcm_action_pause = {
999 .pre_action = snd_pcm_pre_pause,
1000 .do_action = snd_pcm_do_pause,
1001 .undo_action = snd_pcm_undo_pause,
1002 .post_action = snd_pcm_post_pause
1003};
1004
1005/*
1006 * Push/release the pause for all linked streams.
1007 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001008static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1011}
1012
1013#ifdef CONFIG_PM
1014/* suspend */
1015
Takashi Iwai877211f2005-11-17 13:59:38 +01001016static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Takashi Iwai877211f2005-11-17 13:59:38 +01001018 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1020 return -EBUSY;
1021 runtime->trigger_master = substream;
1022 return 0;
1023}
1024
Takashi Iwai877211f2005-11-17 13:59:38 +01001025static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Takashi Iwai877211f2005-11-17 13:59:38 +01001027 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (runtime->trigger_master != substream)
1029 return 0;
1030 if (! snd_pcm_running(substream))
1031 return 0;
1032 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1033 return 0; /* suspend unconditionally */
1034}
1035
Takashi Iwai877211f2005-11-17 13:59:38 +01001036static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Takashi Iwai877211f2005-11-17 13:59:38 +01001038 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 snd_pcm_trigger_tstamp(substream);
1040 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001041 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1042 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 runtime->status->suspended_state = runtime->status->state;
1044 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 wake_up(&runtime->sleep);
1046}
1047
1048static struct action_ops snd_pcm_action_suspend = {
1049 .pre_action = snd_pcm_pre_suspend,
1050 .do_action = snd_pcm_do_suspend,
1051 .post_action = snd_pcm_post_suspend
1052};
1053
1054/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001055 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001056 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 * After this call, all streams are changed to SUSPENDED state.
1059 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001060int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 int err;
1063 unsigned long flags;
1064
Takashi Iwai603bf522005-11-17 15:59:14 +01001065 if (! substream)
1066 return 0;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 snd_pcm_stream_lock_irqsave(substream, flags);
1069 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1070 snd_pcm_stream_unlock_irqrestore(substream, flags);
1071 return err;
1072}
1073
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001074EXPORT_SYMBOL(snd_pcm_suspend);
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001077 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001078 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * After this call, all streams are changed to SUSPENDED state.
1081 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001082int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Takashi Iwai877211f2005-11-17 13:59:38 +01001084 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int stream, err = 0;
1086
Takashi Iwai603bf522005-11-17 15:59:14 +01001087 if (! pcm)
1088 return 0;
1089
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001091 for (substream = pcm->streams[stream].substream;
1092 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* FIXME: the open/close code should lock this as well */
1094 if (substream->runtime == NULL)
1095 continue;
1096 err = snd_pcm_suspend(substream);
1097 if (err < 0 && err != -EBUSY)
1098 return err;
1099 }
1100 }
1101 return 0;
1102}
1103
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001104EXPORT_SYMBOL(snd_pcm_suspend_all);
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/* resume */
1107
Takashi Iwai877211f2005-11-17 13:59:38 +01001108static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Takashi Iwai877211f2005-11-17 13:59:38 +01001110 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1112 return -ENOSYS;
1113 runtime->trigger_master = substream;
1114 return 0;
1115}
1116
Takashi Iwai877211f2005-11-17 13:59:38 +01001117static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (runtime->trigger_master != substream)
1121 return 0;
1122 /* DMA not running previously? */
1123 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1124 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1125 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1126 return 0;
1127 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1128}
1129
Takashi Iwai877211f2005-11-17 13:59:38 +01001130static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 if (substream->runtime->trigger_master == substream &&
1133 snd_pcm_running(substream))
1134 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1135}
1136
Takashi Iwai877211f2005-11-17 13:59:38 +01001137static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Takashi Iwai877211f2005-11-17 13:59:38 +01001139 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 snd_pcm_trigger_tstamp(substream);
1141 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001142 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1143 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147static struct action_ops snd_pcm_action_resume = {
1148 .pre_action = snd_pcm_pre_resume,
1149 .do_action = snd_pcm_do_resume,
1150 .undo_action = snd_pcm_undo_resume,
1151 .post_action = snd_pcm_post_resume
1152};
1153
Takashi Iwai877211f2005-11-17 13:59:38 +01001154static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
Takashi Iwai877211f2005-11-17 13:59:38 +01001156 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 int res;
1158
1159 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001160 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1162 snd_power_unlock(card);
1163 return res;
1164}
1165
1166#else
1167
Takashi Iwai877211f2005-11-17 13:59:38 +01001168static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 return -ENOSYS;
1171}
1172
1173#endif /* CONFIG_PM */
1174
1175/*
1176 * xrun ioctl
1177 *
1178 * Change the RUNNING stream(s) to XRUN state.
1179 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001180static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Takashi Iwai877211f2005-11-17 13:59:38 +01001182 struct snd_card *card = substream->pcm->card;
1183 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int result;
1185
1186 snd_power_lock(card);
1187 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001188 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (result < 0)
1190 goto _unlock;
1191 }
1192
1193 snd_pcm_stream_lock_irq(substream);
1194 switch (runtime->status->state) {
1195 case SNDRV_PCM_STATE_XRUN:
1196 result = 0; /* already there */
1197 break;
1198 case SNDRV_PCM_STATE_RUNNING:
1199 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1200 break;
1201 default:
1202 result = -EBADFD;
1203 }
1204 snd_pcm_stream_unlock_irq(substream);
1205 _unlock:
1206 snd_power_unlock(card);
1207 return result;
1208}
1209
1210/*
1211 * reset ioctl
1212 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001213static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 switch (runtime->status->state) {
1217 case SNDRV_PCM_STATE_RUNNING:
1218 case SNDRV_PCM_STATE_PREPARED:
1219 case SNDRV_PCM_STATE_PAUSED:
1220 case SNDRV_PCM_STATE_SUSPENDED:
1221 return 0;
1222 default:
1223 return -EBADFD;
1224 }
1225}
1226
Takashi Iwai877211f2005-11-17 13:59:38 +01001227static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Takashi Iwai877211f2005-11-17 13:59:38 +01001229 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1231 if (err < 0)
1232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001234 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1235 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 runtime->silence_start = runtime->status->hw_ptr;
1237 runtime->silence_filled = 0;
1238 return 0;
1239}
1240
Takashi Iwai877211f2005-11-17 13:59:38 +01001241static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Takashi Iwai877211f2005-11-17 13:59:38 +01001243 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 runtime->control->appl_ptr = runtime->status->hw_ptr;
1245 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1246 runtime->silence_size > 0)
1247 snd_pcm_playback_silence(substream, ULONG_MAX);
1248}
1249
1250static struct action_ops snd_pcm_action_reset = {
1251 .pre_action = snd_pcm_pre_reset,
1252 .do_action = snd_pcm_do_reset,
1253 .post_action = snd_pcm_post_reset
1254};
1255
Takashi Iwai877211f2005-11-17 13:59:38 +01001256static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
1258 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1259}
1260
1261/*
1262 * prepare ioctl
1263 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001264/* we use the second argument for updating f_flags */
1265static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1266 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Takashi Iwai877211f2005-11-17 13:59:38 +01001268 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001269 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1270 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return -EBADFD;
1272 if (snd_pcm_running(substream))
1273 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001274 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276}
1277
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 int err;
1281 err = substream->ops->prepare(substream);
1282 if (err < 0)
1283 return err;
1284 return snd_pcm_do_reset(substream, 0);
1285}
1286
Takashi Iwai877211f2005-11-17 13:59:38 +01001287static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Takashi Iwai877211f2005-11-17 13:59:38 +01001289 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 runtime->control->appl_ptr = runtime->status->hw_ptr;
1291 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1292}
1293
1294static struct action_ops snd_pcm_action_prepare = {
1295 .pre_action = snd_pcm_pre_prepare,
1296 .do_action = snd_pcm_do_prepare,
1297 .post_action = snd_pcm_post_prepare
1298};
1299
1300/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001301 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001302 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001303 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001305static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1306 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001309 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001310 int f_flags;
1311
1312 if (file)
1313 f_flags = file->f_flags;
1314 else
1315 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001318 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001319 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1320 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 snd_power_unlock(card);
1322 return res;
1323}
1324
1325/*
1326 * drain ioctl
1327 */
1328
Takashi Iwai877211f2005-11-17 13:59:38 +01001329static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001331 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -EAGAIN;
1333 substream->runtime->trigger_master = substream;
1334 return 0;
1335}
1336
Takashi Iwai877211f2005-11-17 13:59:38 +01001337static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Takashi Iwai877211f2005-11-17 13:59:38 +01001339 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1341 switch (runtime->status->state) {
1342 case SNDRV_PCM_STATE_PREPARED:
1343 /* start playback stream if possible */
1344 if (! snd_pcm_playback_empty(substream)) {
1345 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1346 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1347 }
1348 break;
1349 case SNDRV_PCM_STATE_RUNNING:
1350 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1351 break;
1352 default:
1353 break;
1354 }
1355 } else {
1356 /* stop running stream */
1357 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001358 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001360 snd_pcm_do_stop(substream, new_state);
1361 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 }
1364 return 0;
1365}
1366
Takashi Iwai877211f2005-11-17 13:59:38 +01001367static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
1369}
1370
1371static struct action_ops snd_pcm_action_drain_init = {
1372 .pre_action = snd_pcm_pre_drain_init,
1373 .do_action = snd_pcm_do_drain_init,
1374 .post_action = snd_pcm_post_drain_init
1375};
1376
1377struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001378 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 wait_queue_t wait;
1380 snd_pcm_uframes_t stop_threshold;
1381};
1382
Takashi Iwai877211f2005-11-17 13:59:38 +01001383static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385/*
1386 * Drain the stream(s).
1387 * When the substream is linked, sync until the draining of all playback streams
1388 * is finished.
1389 * After this call, all streams are supposed to be either SETUP or DRAINING
1390 * (capture only) state.
1391 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001392static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Takashi Iwai877211f2005-11-17 13:59:38 +01001394 struct snd_card *card;
1395 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001396 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int result = 0;
1398 int i, num_drecs;
1399 struct drain_rec *drec, drec_tmp, *d;
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 card = substream->pcm->card;
1402 runtime = substream->runtime;
1403
1404 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1405 return -EBADFD;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 snd_power_lock(card);
1408 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001409 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001410 if (result < 0) {
1411 snd_power_unlock(card);
1412 return result;
1413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
1416 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001417 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (snd_pcm_stream_linked(substream)) {
1419 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1420 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001421 up_read(&snd_pcm_link_rwsem);
1422 snd_power_unlock(card);
1423 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 } else
1426 drec = &drec_tmp;
1427
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001428 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001430 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1433 d = &drec[num_drecs++];
1434 d->substream = s;
1435 init_waitqueue_entry(&d->wait, current);
1436 add_wait_queue(&runtime->sleep, &d->wait);
1437 /* stop_threshold fixup to avoid endless loop when
1438 * stop_threshold > buffer_size
1439 */
1440 d->stop_threshold = runtime->stop_threshold;
1441 if (runtime->stop_threshold > runtime->buffer_size)
1442 runtime->stop_threshold = runtime->buffer_size;
1443 }
1444 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001445 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001446
1447 snd_pcm_stream_lock_irq(substream);
1448 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001449 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001450 snd_pcm_pause(substream, 0);
1451
1452 /* pre-start/stop - all running streams are changed to DRAINING state */
1453 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1454 if (result < 0) {
1455 snd_pcm_stream_unlock_irq(substream);
1456 goto _error;
1457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 for (;;) {
1460 long tout;
1461 if (signal_pending(current)) {
1462 result = -ERESTARTSYS;
1463 break;
1464 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001465 /* all finished? */
1466 for (i = 0; i < num_drecs; i++) {
1467 runtime = drec[i].substream->runtime;
1468 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1469 break;
1470 }
1471 if (i == num_drecs)
1472 break; /* yes, all drained */
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 set_current_state(TASK_INTERRUPTIBLE);
1475 snd_pcm_stream_unlock_irq(substream);
1476 snd_power_unlock(card);
1477 tout = schedule_timeout(10 * HZ);
1478 snd_power_lock(card);
1479 snd_pcm_stream_lock_irq(substream);
1480 if (tout == 0) {
1481 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1482 result = -ESTRPIPE;
1483 else {
1484 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1485 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1486 result = -EIO;
1487 }
1488 break;
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001491
1492 snd_pcm_stream_unlock_irq(substream);
1493
1494 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 for (i = 0; i < num_drecs; i++) {
1496 d = &drec[i];
1497 runtime = d->substream->runtime;
1498 remove_wait_queue(&runtime->sleep, &d->wait);
1499 runtime->stop_threshold = d->stop_threshold;
1500 }
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (drec != &drec_tmp)
1503 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 return result;
1507}
1508
1509/*
1510 * drop ioctl
1511 *
1512 * Immediately put all linked substreams into SETUP state.
1513 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001514static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Takashi Iwai877211f2005-11-17 13:59:38 +01001516 struct snd_pcm_runtime *runtime;
1517 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int result = 0;
1519
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001520 if (PCM_RUNTIME_CHECK(substream))
1521 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 runtime = substream->runtime;
1523 card = substream->pcm->card;
1524
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001526 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1527 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return -EBADFD;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 snd_pcm_stream_lock_irq(substream);
1531 /* resume pause */
1532 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1533 snd_pcm_pause(substream, 0);
1534
1535 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1536 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1537 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return result;
1540}
1541
1542
1543/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544static struct file *snd_pcm_file_fd(int fd)
1545{
1546 struct file *file;
1547 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001548 unsigned int minor;
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 file = fget(fd);
1551 if (!file)
1552 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001553 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (!S_ISCHR(inode->i_mode) ||
1555 imajor(inode) != snd_major) {
1556 fput(file);
1557 return NULL;
1558 }
1559 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001560 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1561 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 fput(file);
1563 return NULL;
1564 }
1565 return file;
1566}
1567
1568/*
1569 * PCM link handling
1570 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001571static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 int res = 0;
1574 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001575 struct snd_pcm_file *pcm_file;
1576 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 file = snd_pcm_file_fd(fd);
1579 if (!file)
1580 return -EBADFD;
1581 pcm_file = file->private_data;
1582 substream1 = pcm_file->substream;
1583 down_write(&snd_pcm_link_rwsem);
1584 write_lock_irq(&snd_pcm_link_rwlock);
1585 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1586 substream->runtime->status->state != substream1->runtime->status->state) {
1587 res = -EBADFD;
1588 goto _end;
1589 }
1590 if (snd_pcm_stream_linked(substream1)) {
1591 res = -EALREADY;
1592 goto _end;
1593 }
1594 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001595 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (substream->group == NULL) {
1597 res = -ENOMEM;
1598 goto _end;
1599 }
1600 spin_lock_init(&substream->group->lock);
1601 INIT_LIST_HEAD(&substream->group->substreams);
1602 list_add_tail(&substream->link_list, &substream->group->substreams);
1603 substream->group->count = 1;
1604 }
1605 list_add_tail(&substream1->link_list, &substream->group->substreams);
1606 substream->group->count++;
1607 substream1->group = substream->group;
1608 _end:
1609 write_unlock_irq(&snd_pcm_link_rwlock);
1610 up_write(&snd_pcm_link_rwsem);
1611 fput(file);
1612 return res;
1613}
1614
Takashi Iwai877211f2005-11-17 13:59:38 +01001615static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 substream->group = &substream->self_group;
1618 INIT_LIST_HEAD(&substream->self_group.substreams);
1619 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1620}
1621
Takashi Iwai877211f2005-11-17 13:59:38 +01001622static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwaief991b92007-02-22 12:52:53 +01001624 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int res = 0;
1626
1627 down_write(&snd_pcm_link_rwsem);
1628 write_lock_irq(&snd_pcm_link_rwlock);
1629 if (!snd_pcm_stream_linked(substream)) {
1630 res = -EALREADY;
1631 goto _end;
1632 }
1633 list_del(&substream->link_list);
1634 substream->group->count--;
1635 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001636 snd_pcm_group_for_each_entry(s, substream) {
1637 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 break;
1639 }
1640 kfree(substream->group);
1641 }
1642 relink_to_local(substream);
1643 _end:
1644 write_unlock_irq(&snd_pcm_link_rwlock);
1645 up_write(&snd_pcm_link_rwsem);
1646 return res;
1647}
1648
1649/*
1650 * hw configurator
1651 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001652static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1653 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai877211f2005-11-17 13:59:38 +01001655 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1657 hw_param_interval_c(params, rule->deps[1]), &t);
1658 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1659}
1660
Takashi Iwai877211f2005-11-17 13:59:38 +01001661static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1662 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwai877211f2005-11-17 13:59:38 +01001664 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1666 hw_param_interval_c(params, rule->deps[1]), &t);
1667 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1668}
1669
Takashi Iwai877211f2005-11-17 13:59:38 +01001670static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1671 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Takashi Iwai877211f2005-11-17 13:59:38 +01001673 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1675 hw_param_interval_c(params, rule->deps[1]),
1676 (unsigned long) rule->private, &t);
1677 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1678}
1679
Takashi Iwai877211f2005-11-17 13:59:38 +01001680static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1681 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Takashi Iwai877211f2005-11-17 13:59:38 +01001683 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1685 (unsigned long) rule->private,
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1688}
1689
Takashi Iwai877211f2005-11-17 13:59:38 +01001690static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001694 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1695 struct snd_mask m;
1696 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 snd_mask_any(&m);
1698 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1699 int bits;
1700 if (! snd_mask_test(mask, k))
1701 continue;
1702 bits = snd_pcm_format_physical_width(k);
1703 if (bits <= 0)
1704 continue; /* ignore invalid formats */
1705 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1706 snd_mask_reset(&m, k);
1707 }
1708 return snd_mask_refine(mask, &m);
1709}
1710
Takashi Iwai877211f2005-11-17 13:59:38 +01001711static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1712 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwai877211f2005-11-17 13:59:38 +01001714 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned int k;
1716 t.min = UINT_MAX;
1717 t.max = 0;
1718 t.openmin = 0;
1719 t.openmax = 0;
1720 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1721 int bits;
1722 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1723 continue;
1724 bits = snd_pcm_format_physical_width(k);
1725 if (bits <= 0)
1726 continue; /* ignore invalid formats */
1727 if (t.min > (unsigned)bits)
1728 t.min = bits;
1729 if (t.max < (unsigned)bits)
1730 t.max = bits;
1731 }
1732 t.integer = 1;
1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1734}
1735
1736#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1737#error "Change this table"
1738#endif
1739
1740static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1741 48000, 64000, 88200, 96000, 176400, 192000 };
1742
Clemens Ladisch7653d552007-08-13 17:38:54 +02001743const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1744 .count = ARRAY_SIZE(rates),
1745 .list = rates,
1746};
1747
Takashi Iwai877211f2005-11-17 13:59:38 +01001748static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1749 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Takashi Iwai877211f2005-11-17 13:59:38 +01001751 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001753 snd_pcm_known_rates.count,
1754 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Takashi Iwai877211f2005-11-17 13:59:38 +01001757static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1758 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai877211f2005-11-17 13:59:38 +01001760 struct snd_interval t;
1761 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 t.min = 0;
1763 t.max = substream->buffer_bytes_max;
1764 t.openmin = 0;
1765 t.openmax = 0;
1766 t.integer = 1;
1767 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1768}
1769
Takashi Iwai877211f2005-11-17 13:59:38 +01001770int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwai877211f2005-11-17 13:59:38 +01001772 struct snd_pcm_runtime *runtime = substream->runtime;
1773 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int k, err;
1775
1776 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1777 snd_mask_any(constrs_mask(constrs, k));
1778 }
1779
1780 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1781 snd_interval_any(constrs_interval(constrs, k));
1782 }
1783
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1785 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1786 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1787 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1788 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1789
1790 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1791 snd_pcm_hw_rule_format, NULL,
1792 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1793 if (err < 0)
1794 return err;
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1796 snd_pcm_hw_rule_sample_bits, NULL,
1797 SNDRV_PCM_HW_PARAM_FORMAT,
1798 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1799 if (err < 0)
1800 return err;
1801 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1802 snd_pcm_hw_rule_div, NULL,
1803 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1804 if (err < 0)
1805 return err;
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1807 snd_pcm_hw_rule_mul, NULL,
1808 SNDRV_PCM_HW_PARAM_SAMPLE_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_mulkdiv, (void*) 8,
1813 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -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_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1819 if (err < 0)
1820 return err;
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1822 snd_pcm_hw_rule_div, NULL,
1823 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1827 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1828 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -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_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1837 snd_pcm_hw_rule_div, NULL,
1838 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -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_mulkdiv, (void*) 8,
1848 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -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_muldivk, (void*) 1000000,
1853 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1857 snd_pcm_hw_rule_mul, NULL,
1858 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -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_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -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_muldivk, (void*) 1000000,
1868 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1872 snd_pcm_hw_rule_muldivk, (void*) 8,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1877 snd_pcm_hw_rule_muldivk, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_BUFFER_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_PERIOD_TIME,
1882 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1889 if (err < 0)
1890 return err;
1891 return 0;
1892}
1893
Takashi Iwai877211f2005-11-17 13:59:38 +01001894int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Takashi Iwai877211f2005-11-17 13:59:38 +01001896 struct snd_pcm_runtime *runtime = substream->runtime;
1897 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int err;
1899 unsigned int mask = 0;
1900
1901 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1902 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1903 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1904 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1905 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1906 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1911 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1912 }
1913 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001914 if (err < 0)
1915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001918 if (err < 0)
1919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001922 if (err < 0)
1923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1926 hw->channels_min, hw->channels_max);
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_RATE,
1931 hw->rate_min, hw->rate_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_PERIOD_BYTES,
1936 hw->period_bytes_min, hw->period_bytes_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_PERIODS,
1941 hw->periods_min, hw->periods_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_BUFFER_BYTES,
1946 hw->period_bytes_min, hw->buffer_bytes_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_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1951 snd_pcm_hw_rule_buffer_bytes_max, substream,
1952 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1953 if (err < 0)
1954 return err;
1955
1956 /* FIXME: remove */
1957 if (runtime->dma_bytes) {
1958 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001959 if (err < 0)
1960 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1964 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1965 snd_pcm_hw_rule_rate, hw,
1966 SNDRV_PCM_HW_PARAM_RATE, -1);
1967 if (err < 0)
1968 return err;
1969 }
1970
1971 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1973
1974 return 0;
1975}
1976
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001977static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001980}
1981
1982void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1983{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001984 substream->ref_count--;
1985 if (substream->ref_count > 0)
1986 return;
1987
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001988 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001989 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (substream->ops->hw_free != NULL)
1991 substream->ops->hw_free(substream);
1992 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001993 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Takashi Iwai15762742006-04-06 19:47:42 +02001995 if (substream->pcm_release) {
1996 substream->pcm_release(substream);
1997 substream->pcm_release = NULL;
1998 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001999 snd_pcm_detach_substream(substream);
2000}
2001
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002002EXPORT_SYMBOL(snd_pcm_release_substream);
2003
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002004int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2005 struct file *file,
2006 struct snd_pcm_substream **rsubstream)
2007{
2008 struct snd_pcm_substream *substream;
2009 int err;
2010
2011 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2012 if (err < 0)
2013 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002014 if (substream->ref_count > 1) {
2015 *rsubstream = substream;
2016 return 0;
2017 }
2018
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 err = snd_pcm_hw_constraints_init(substream);
2020 if (err < 0) {
2021 snd_printd("snd_pcm_hw_constraints_init failed\n");
2022 goto error;
2023 }
2024
2025 if ((err = substream->ops->open(substream)) < 0)
2026 goto error;
2027
2028 substream->hw_opened = 1;
2029
2030 err = snd_pcm_hw_constraints_complete(substream);
2031 if (err < 0) {
2032 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2033 goto error;
2034 }
2035
2036 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002038
2039 error:
2040 snd_pcm_release_substream(substream);
2041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002044EXPORT_SYMBOL(snd_pcm_open_substream);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002049 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Takashi Iwai877211f2005-11-17 13:59:38 +01002051 struct snd_pcm_file *pcm_file;
2052 struct snd_pcm_substream *substream;
2053 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002054 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002056 if (rpcm_file)
2057 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002059 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2060 if (err < 0)
2061 return err;
2062
Takashi Iwai548a6482006-07-31 16:51:51 +02002063 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2064 if (pcm_file == NULL) {
2065 snd_pcm_release_substream(substream);
2066 return -ENOMEM;
2067 }
2068 pcm_file->substream = substream;
2069 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002070 str = substream->pstr;
2071 substream->file = pcm_file;
2072 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002075 if (rpcm_file)
2076 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
Clemens Ladischf87135f2005-11-20 14:06:59 +01002080static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Takashi Iwai877211f2005-11-17 13:59:38 +01002082 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002083
2084 pcm = snd_lookup_minor_data(iminor(inode),
2085 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2086 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2087}
2088
2089static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2090{
2091 struct snd_pcm *pcm;
2092
2093 pcm = snd_lookup_minor_data(iminor(inode),
2094 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2095 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2096}
2097
2098static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2099{
2100 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002101 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 wait_queue_t wait;
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (pcm == NULL) {
2105 err = -ENODEV;
2106 goto __error1;
2107 }
2108 err = snd_card_file_add(pcm->card, file);
2109 if (err < 0)
2110 goto __error1;
2111 if (!try_module_get(pcm->card->module)) {
2112 err = -EFAULT;
2113 goto __error2;
2114 }
2115 init_waitqueue_entry(&wait, current);
2116 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002117 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002119 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (err >= 0)
2121 break;
2122 if (err == -EAGAIN) {
2123 if (file->f_flags & O_NONBLOCK) {
2124 err = -EBUSY;
2125 break;
2126 }
2127 } else
2128 break;
2129 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002130 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002132 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (signal_pending(current)) {
2134 err = -ERESTARTSYS;
2135 break;
2136 }
2137 }
2138 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002139 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (err < 0)
2141 goto __error;
2142 return err;
2143
2144 __error:
2145 module_put(pcm->card->module);
2146 __error2:
2147 snd_card_file_remove(pcm->card, file);
2148 __error1:
2149 return err;
2150}
2151
2152static int snd_pcm_release(struct inode *inode, struct file *file)
2153{
Takashi Iwai877211f2005-11-17 13:59:38 +01002154 struct snd_pcm *pcm;
2155 struct snd_pcm_substream *substream;
2156 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 pcm_file = file->private_data;
2159 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002160 if (snd_BUG_ON(!substream))
2161 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002163 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002164 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002165 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002166 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 wake_up(&pcm->open_wait);
2168 module_put(pcm->card->module);
2169 snd_card_file_remove(pcm->card, file);
2170 return 0;
2171}
2172
Takashi Iwai877211f2005-11-17 13:59:38 +01002173static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2174 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Takashi Iwai877211f2005-11-17 13:59:38 +01002176 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 snd_pcm_sframes_t appl_ptr;
2178 snd_pcm_sframes_t ret;
2179 snd_pcm_sframes_t hw_avail;
2180
2181 if (frames == 0)
2182 return 0;
2183
2184 snd_pcm_stream_lock_irq(substream);
2185 switch (runtime->status->state) {
2186 case SNDRV_PCM_STATE_PREPARED:
2187 break;
2188 case SNDRV_PCM_STATE_DRAINING:
2189 case SNDRV_PCM_STATE_RUNNING:
2190 if (snd_pcm_update_hw_ptr(substream) >= 0)
2191 break;
2192 /* Fall through */
2193 case SNDRV_PCM_STATE_XRUN:
2194 ret = -EPIPE;
2195 goto __end;
2196 default:
2197 ret = -EBADFD;
2198 goto __end;
2199 }
2200
2201 hw_avail = snd_pcm_playback_hw_avail(runtime);
2202 if (hw_avail <= 0) {
2203 ret = 0;
2204 goto __end;
2205 }
2206 if (frames > (snd_pcm_uframes_t)hw_avail)
2207 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 appl_ptr = runtime->control->appl_ptr - frames;
2209 if (appl_ptr < 0)
2210 appl_ptr += runtime->boundary;
2211 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 ret = frames;
2213 __end:
2214 snd_pcm_stream_unlock_irq(substream);
2215 return ret;
2216}
2217
Takashi Iwai877211f2005-11-17 13:59:38 +01002218static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2219 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Takashi Iwai877211f2005-11-17 13:59:38 +01002221 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 snd_pcm_sframes_t appl_ptr;
2223 snd_pcm_sframes_t ret;
2224 snd_pcm_sframes_t hw_avail;
2225
2226 if (frames == 0)
2227 return 0;
2228
2229 snd_pcm_stream_lock_irq(substream);
2230 switch (runtime->status->state) {
2231 case SNDRV_PCM_STATE_PREPARED:
2232 case SNDRV_PCM_STATE_DRAINING:
2233 break;
2234 case SNDRV_PCM_STATE_RUNNING:
2235 if (snd_pcm_update_hw_ptr(substream) >= 0)
2236 break;
2237 /* Fall through */
2238 case SNDRV_PCM_STATE_XRUN:
2239 ret = -EPIPE;
2240 goto __end;
2241 default:
2242 ret = -EBADFD;
2243 goto __end;
2244 }
2245
2246 hw_avail = snd_pcm_capture_hw_avail(runtime);
2247 if (hw_avail <= 0) {
2248 ret = 0;
2249 goto __end;
2250 }
2251 if (frames > (snd_pcm_uframes_t)hw_avail)
2252 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 appl_ptr = runtime->control->appl_ptr - frames;
2254 if (appl_ptr < 0)
2255 appl_ptr += runtime->boundary;
2256 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 ret = frames;
2258 __end:
2259 snd_pcm_stream_unlock_irq(substream);
2260 return ret;
2261}
2262
Takashi Iwai877211f2005-11-17 13:59:38 +01002263static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2264 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Takashi Iwai877211f2005-11-17 13:59:38 +01002266 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 snd_pcm_sframes_t appl_ptr;
2268 snd_pcm_sframes_t ret;
2269 snd_pcm_sframes_t avail;
2270
2271 if (frames == 0)
2272 return 0;
2273
2274 snd_pcm_stream_lock_irq(substream);
2275 switch (runtime->status->state) {
2276 case SNDRV_PCM_STATE_PREPARED:
2277 case SNDRV_PCM_STATE_PAUSED:
2278 break;
2279 case SNDRV_PCM_STATE_DRAINING:
2280 case SNDRV_PCM_STATE_RUNNING:
2281 if (snd_pcm_update_hw_ptr(substream) >= 0)
2282 break;
2283 /* Fall through */
2284 case SNDRV_PCM_STATE_XRUN:
2285 ret = -EPIPE;
2286 goto __end;
2287 default:
2288 ret = -EBADFD;
2289 goto __end;
2290 }
2291
2292 avail = snd_pcm_playback_avail(runtime);
2293 if (avail <= 0) {
2294 ret = 0;
2295 goto __end;
2296 }
2297 if (frames > (snd_pcm_uframes_t)avail)
2298 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 appl_ptr = runtime->control->appl_ptr + frames;
2300 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2301 appl_ptr -= runtime->boundary;
2302 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 ret = frames;
2304 __end:
2305 snd_pcm_stream_unlock_irq(substream);
2306 return ret;
2307}
2308
Takashi Iwai877211f2005-11-17 13:59:38 +01002309static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2310 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Takashi Iwai877211f2005-11-17 13:59:38 +01002312 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 snd_pcm_sframes_t appl_ptr;
2314 snd_pcm_sframes_t ret;
2315 snd_pcm_sframes_t avail;
2316
2317 if (frames == 0)
2318 return 0;
2319
2320 snd_pcm_stream_lock_irq(substream);
2321 switch (runtime->status->state) {
2322 case SNDRV_PCM_STATE_PREPARED:
2323 case SNDRV_PCM_STATE_DRAINING:
2324 case SNDRV_PCM_STATE_PAUSED:
2325 break;
2326 case SNDRV_PCM_STATE_RUNNING:
2327 if (snd_pcm_update_hw_ptr(substream) >= 0)
2328 break;
2329 /* Fall through */
2330 case SNDRV_PCM_STATE_XRUN:
2331 ret = -EPIPE;
2332 goto __end;
2333 default:
2334 ret = -EBADFD;
2335 goto __end;
2336 }
2337
2338 avail = snd_pcm_capture_avail(runtime);
2339 if (avail <= 0) {
2340 ret = 0;
2341 goto __end;
2342 }
2343 if (frames > (snd_pcm_uframes_t)avail)
2344 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 appl_ptr = runtime->control->appl_ptr + frames;
2346 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2347 appl_ptr -= runtime->boundary;
2348 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 ret = frames;
2350 __end:
2351 snd_pcm_stream_unlock_irq(substream);
2352 return ret;
2353}
2354
Takashi Iwai877211f2005-11-17 13:59:38 +01002355static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
Takashi Iwai877211f2005-11-17 13:59:38 +01002357 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 int err;
2359
2360 snd_pcm_stream_lock_irq(substream);
2361 switch (runtime->status->state) {
2362 case SNDRV_PCM_STATE_DRAINING:
2363 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2364 goto __badfd;
2365 case SNDRV_PCM_STATE_RUNNING:
2366 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2367 break;
2368 /* Fall through */
2369 case SNDRV_PCM_STATE_PREPARED:
2370 case SNDRV_PCM_STATE_SUSPENDED:
2371 err = 0;
2372 break;
2373 case SNDRV_PCM_STATE_XRUN:
2374 err = -EPIPE;
2375 break;
2376 default:
2377 __badfd:
2378 err = -EBADFD;
2379 break;
2380 }
2381 snd_pcm_stream_unlock_irq(substream);
2382 return err;
2383}
2384
Takashi Iwai877211f2005-11-17 13:59:38 +01002385static int snd_pcm_delay(struct snd_pcm_substream *substream,
2386 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387{
Takashi Iwai877211f2005-11-17 13:59:38 +01002388 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 int err;
2390 snd_pcm_sframes_t n = 0;
2391
2392 snd_pcm_stream_lock_irq(substream);
2393 switch (runtime->status->state) {
2394 case SNDRV_PCM_STATE_DRAINING:
2395 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2396 goto __badfd;
2397 case SNDRV_PCM_STATE_RUNNING:
2398 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2399 break;
2400 /* Fall through */
2401 case SNDRV_PCM_STATE_PREPARED:
2402 case SNDRV_PCM_STATE_SUSPENDED:
2403 err = 0;
2404 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2405 n = snd_pcm_playback_hw_avail(runtime);
2406 else
2407 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002408 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410 case SNDRV_PCM_STATE_XRUN:
2411 err = -EPIPE;
2412 break;
2413 default:
2414 __badfd:
2415 err = -EBADFD;
2416 break;
2417 }
2418 snd_pcm_stream_unlock_irq(substream);
2419 if (!err)
2420 if (put_user(n, res))
2421 err = -EFAULT;
2422 return err;
2423}
2424
Takashi Iwai877211f2005-11-17 13:59:38 +01002425static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2426 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
Takashi Iwai877211f2005-11-17 13:59:38 +01002428 struct snd_pcm_runtime *runtime = substream->runtime;
2429 struct snd_pcm_sync_ptr sync_ptr;
2430 volatile struct snd_pcm_mmap_status *status;
2431 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int err;
2433
2434 memset(&sync_ptr, 0, sizeof(sync_ptr));
2435 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2436 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002437 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 -07002438 return -EFAULT;
2439 status = runtime->status;
2440 control = runtime->control;
2441 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2442 err = snd_pcm_hwsync(substream);
2443 if (err < 0)
2444 return err;
2445 }
2446 snd_pcm_stream_lock_irq(substream);
2447 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2448 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2449 else
2450 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2451 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2452 control->avail_min = sync_ptr.c.control.avail_min;
2453 else
2454 sync_ptr.c.control.avail_min = control->avail_min;
2455 sync_ptr.s.status.state = status->state;
2456 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2457 sync_ptr.s.status.tstamp = status->tstamp;
2458 sync_ptr.s.status.suspended_state = status->suspended_state;
2459 snd_pcm_stream_unlock_irq(substream);
2460 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2461 return -EFAULT;
2462 return 0;
2463}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002464
2465static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2466{
2467 struct snd_pcm_runtime *runtime = substream->runtime;
2468 int arg;
2469
2470 if (get_user(arg, _arg))
2471 return -EFAULT;
2472 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2473 return -EINVAL;
2474 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2475 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2476 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2477 return 0;
2478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Takashi Iwai0df63e42006-04-28 15:13:41 +02002480static int snd_pcm_common_ioctl1(struct file *file,
2481 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 unsigned int cmd, void __user *arg)
2483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 switch (cmd) {
2485 case SNDRV_PCM_IOCTL_PVERSION:
2486 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2487 case SNDRV_PCM_IOCTL_INFO:
2488 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002489 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2490 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002491 case SNDRV_PCM_IOCTL_TTSTAMP:
2492 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 case SNDRV_PCM_IOCTL_HW_REFINE:
2494 return snd_pcm_hw_refine_user(substream, arg);
2495 case SNDRV_PCM_IOCTL_HW_PARAMS:
2496 return snd_pcm_hw_params_user(substream, arg);
2497 case SNDRV_PCM_IOCTL_HW_FREE:
2498 return snd_pcm_hw_free(substream);
2499 case SNDRV_PCM_IOCTL_SW_PARAMS:
2500 return snd_pcm_sw_params_user(substream, arg);
2501 case SNDRV_PCM_IOCTL_STATUS:
2502 return snd_pcm_status_user(substream, arg);
2503 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2504 return snd_pcm_channel_info_user(substream, arg);
2505 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002506 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 case SNDRV_PCM_IOCTL_RESET:
2508 return snd_pcm_reset(substream);
2509 case SNDRV_PCM_IOCTL_START:
2510 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2511 case SNDRV_PCM_IOCTL_LINK:
2512 return snd_pcm_link(substream, (int)(unsigned long) arg);
2513 case SNDRV_PCM_IOCTL_UNLINK:
2514 return snd_pcm_unlink(substream);
2515 case SNDRV_PCM_IOCTL_RESUME:
2516 return snd_pcm_resume(substream);
2517 case SNDRV_PCM_IOCTL_XRUN:
2518 return snd_pcm_xrun(substream);
2519 case SNDRV_PCM_IOCTL_HWSYNC:
2520 return snd_pcm_hwsync(substream);
2521 case SNDRV_PCM_IOCTL_DELAY:
2522 return snd_pcm_delay(substream, arg);
2523 case SNDRV_PCM_IOCTL_SYNC_PTR:
2524 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002525#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2527 return snd_pcm_hw_refine_old_user(substream, arg);
2528 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2529 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 case SNDRV_PCM_IOCTL_DRAIN:
2532 return snd_pcm_drain(substream);
2533 case SNDRV_PCM_IOCTL_DROP:
2534 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002535 case SNDRV_PCM_IOCTL_PAUSE:
2536 {
2537 int res;
2538 snd_pcm_stream_lock_irq(substream);
2539 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2540 snd_pcm_stream_unlock_irq(substream);
2541 return res;
2542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 }
2544 snd_printd("unknown ioctl = 0x%x\n", cmd);
2545 return -ENOTTY;
2546}
2547
Takashi Iwai0df63e42006-04-28 15:13:41 +02002548static int snd_pcm_playback_ioctl1(struct file *file,
2549 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 unsigned int cmd, void __user *arg)
2551{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002552 if (snd_BUG_ON(!substream))
2553 return -ENXIO;
2554 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2555 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 switch (cmd) {
2557 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2558 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002559 struct snd_xferi xferi;
2560 struct snd_xferi __user *_xferi = arg;
2561 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 snd_pcm_sframes_t result;
2563 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2564 return -EBADFD;
2565 if (put_user(0, &_xferi->result))
2566 return -EFAULT;
2567 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2568 return -EFAULT;
2569 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2570 __put_user(result, &_xferi->result);
2571 return result < 0 ? result : 0;
2572 }
2573 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2574 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002575 struct snd_xfern xfern;
2576 struct snd_xfern __user *_xfern = arg;
2577 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 void __user **bufs;
2579 snd_pcm_sframes_t result;
2580 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2581 return -EBADFD;
2582 if (runtime->channels > 128)
2583 return -EINVAL;
2584 if (put_user(0, &_xfern->result))
2585 return -EFAULT;
2586 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2587 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002588
2589 bufs = memdup_user(xfern.bufs,
2590 sizeof(void *) * runtime->channels);
2591 if (IS_ERR(bufs))
2592 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2594 kfree(bufs);
2595 __put_user(result, &_xfern->result);
2596 return result < 0 ? result : 0;
2597 }
2598 case SNDRV_PCM_IOCTL_REWIND:
2599 {
2600 snd_pcm_uframes_t frames;
2601 snd_pcm_uframes_t __user *_frames = arg;
2602 snd_pcm_sframes_t result;
2603 if (get_user(frames, _frames))
2604 return -EFAULT;
2605 if (put_user(0, _frames))
2606 return -EFAULT;
2607 result = snd_pcm_playback_rewind(substream, frames);
2608 __put_user(result, _frames);
2609 return result < 0 ? result : 0;
2610 }
2611 case SNDRV_PCM_IOCTL_FORWARD:
2612 {
2613 snd_pcm_uframes_t frames;
2614 snd_pcm_uframes_t __user *_frames = arg;
2615 snd_pcm_sframes_t result;
2616 if (get_user(frames, _frames))
2617 return -EFAULT;
2618 if (put_user(0, _frames))
2619 return -EFAULT;
2620 result = snd_pcm_playback_forward(substream, frames);
2621 __put_user(result, _frames);
2622 return result < 0 ? result : 0;
2623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002625 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Takashi Iwai0df63e42006-04-28 15:13:41 +02002628static int snd_pcm_capture_ioctl1(struct file *file,
2629 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 unsigned int cmd, void __user *arg)
2631{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002632 if (snd_BUG_ON(!substream))
2633 return -ENXIO;
2634 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2635 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 switch (cmd) {
2637 case SNDRV_PCM_IOCTL_READI_FRAMES:
2638 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002639 struct snd_xferi xferi;
2640 struct snd_xferi __user *_xferi = arg;
2641 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 snd_pcm_sframes_t result;
2643 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2644 return -EBADFD;
2645 if (put_user(0, &_xferi->result))
2646 return -EFAULT;
2647 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2648 return -EFAULT;
2649 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2650 __put_user(result, &_xferi->result);
2651 return result < 0 ? result : 0;
2652 }
2653 case SNDRV_PCM_IOCTL_READN_FRAMES:
2654 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002655 struct snd_xfern xfern;
2656 struct snd_xfern __user *_xfern = arg;
2657 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 void *bufs;
2659 snd_pcm_sframes_t result;
2660 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2661 return -EBADFD;
2662 if (runtime->channels > 128)
2663 return -EINVAL;
2664 if (put_user(0, &_xfern->result))
2665 return -EFAULT;
2666 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2667 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002668
2669 bufs = memdup_user(xfern.bufs,
2670 sizeof(void *) * runtime->channels);
2671 if (IS_ERR(bufs))
2672 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2674 kfree(bufs);
2675 __put_user(result, &_xfern->result);
2676 return result < 0 ? result : 0;
2677 }
2678 case SNDRV_PCM_IOCTL_REWIND:
2679 {
2680 snd_pcm_uframes_t frames;
2681 snd_pcm_uframes_t __user *_frames = arg;
2682 snd_pcm_sframes_t result;
2683 if (get_user(frames, _frames))
2684 return -EFAULT;
2685 if (put_user(0, _frames))
2686 return -EFAULT;
2687 result = snd_pcm_capture_rewind(substream, frames);
2688 __put_user(result, _frames);
2689 return result < 0 ? result : 0;
2690 }
2691 case SNDRV_PCM_IOCTL_FORWARD:
2692 {
2693 snd_pcm_uframes_t frames;
2694 snd_pcm_uframes_t __user *_frames = arg;
2695 snd_pcm_sframes_t result;
2696 if (get_user(frames, _frames))
2697 return -EFAULT;
2698 if (put_user(0, _frames))
2699 return -EFAULT;
2700 result = snd_pcm_capture_forward(substream, frames);
2701 __put_user(result, _frames);
2702 return result < 0 ? result : 0;
2703 }
2704 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002705 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Takashi Iwai877211f2005-11-17 13:59:38 +01002708static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2709 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
Takashi Iwai877211f2005-11-17 13:59:38 +01002711 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 pcm_file = file->private_data;
2714
2715 if (((cmd >> 8) & 0xff) != 'A')
2716 return -ENOTTY;
2717
Takashi Iwai0df63e42006-04-28 15:13:41 +02002718 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2719 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720}
2721
Takashi Iwai877211f2005-11-17 13:59:38 +01002722static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2723 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Takashi Iwai877211f2005-11-17 13:59:38 +01002725 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 pcm_file = file->private_data;
2728
2729 if (((cmd >> 8) & 0xff) != 'A')
2730 return -ENOTTY;
2731
Takashi Iwai0df63e42006-04-28 15:13:41 +02002732 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2733 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
2735
Takashi Iwai877211f2005-11-17 13:59:38 +01002736int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 unsigned int cmd, void *arg)
2738{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002739 mm_segment_t fs;
2740 int result;
2741
2742 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 switch (substream->stream) {
2744 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002745 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2746 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002747 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002749 result = snd_pcm_capture_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 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002753 result = -EINVAL;
2754 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002756 snd_leave_user(fs);
2757 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002760EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2761
Takashi Iwai877211f2005-11-17 13:59:38 +01002762static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2763 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Takashi Iwai877211f2005-11-17 13:59:38 +01002765 struct snd_pcm_file *pcm_file;
2766 struct snd_pcm_substream *substream;
2767 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 snd_pcm_sframes_t result;
2769
2770 pcm_file = file->private_data;
2771 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002772 if (PCM_RUNTIME_CHECK(substream))
2773 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 runtime = substream->runtime;
2775 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2776 return -EBADFD;
2777 if (!frame_aligned(runtime, count))
2778 return -EINVAL;
2779 count = bytes_to_frames(runtime, count);
2780 result = snd_pcm_lib_read(substream, buf, count);
2781 if (result > 0)
2782 result = frames_to_bytes(runtime, result);
2783 return result;
2784}
2785
Takashi Iwai877211f2005-11-17 13:59:38 +01002786static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2787 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
Takashi Iwai877211f2005-11-17 13:59:38 +01002789 struct snd_pcm_file *pcm_file;
2790 struct snd_pcm_substream *substream;
2791 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 snd_pcm_sframes_t result;
2793
2794 pcm_file = file->private_data;
2795 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002796 if (PCM_RUNTIME_CHECK(substream))
2797 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002799 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2800 return -EBADFD;
2801 if (!frame_aligned(runtime, count))
2802 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 count = bytes_to_frames(runtime, count);
2804 result = snd_pcm_lib_write(substream, buf, count);
2805 if (result > 0)
2806 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return result;
2808}
2809
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002810static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2811 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
2813{
Takashi Iwai877211f2005-11-17 13:59:38 +01002814 struct snd_pcm_file *pcm_file;
2815 struct snd_pcm_substream *substream;
2816 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 snd_pcm_sframes_t result;
2818 unsigned long i;
2819 void __user **bufs;
2820 snd_pcm_uframes_t frames;
2821
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002822 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002824 if (PCM_RUNTIME_CHECK(substream))
2825 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 runtime = substream->runtime;
2827 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2828 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002829 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002831 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002833 frames = bytes_to_samples(runtime, iov->iov_len);
2834 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 if (bufs == NULL)
2836 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002837 for (i = 0; i < nr_segs; ++i)
2838 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 result = snd_pcm_lib_readv(substream, bufs, frames);
2840 if (result > 0)
2841 result = frames_to_bytes(runtime, result);
2842 kfree(bufs);
2843 return result;
2844}
2845
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002846static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2847 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Takashi Iwai877211f2005-11-17 13:59:38 +01002849 struct snd_pcm_file *pcm_file;
2850 struct snd_pcm_substream *substream;
2851 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 snd_pcm_sframes_t result;
2853 unsigned long i;
2854 void __user **bufs;
2855 snd_pcm_uframes_t frames;
2856
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002857 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002859 if (PCM_RUNTIME_CHECK(substream))
2860 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002862 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2863 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002864 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002865 !frame_aligned(runtime, iov->iov_len))
2866 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002867 frames = bytes_to_samples(runtime, iov->iov_len);
2868 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 if (bufs == NULL)
2870 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002871 for (i = 0; i < nr_segs; ++i)
2872 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 result = snd_pcm_lib_writev(substream, bufs, frames);
2874 if (result > 0)
2875 result = frames_to_bytes(runtime, result);
2876 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return result;
2878}
2879
2880static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2881{
Takashi Iwai877211f2005-11-17 13:59:38 +01002882 struct snd_pcm_file *pcm_file;
2883 struct snd_pcm_substream *substream;
2884 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 unsigned int mask;
2886 snd_pcm_uframes_t avail;
2887
2888 pcm_file = file->private_data;
2889
2890 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002891 if (PCM_RUNTIME_CHECK(substream))
2892 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 runtime = substream->runtime;
2894
2895 poll_wait(file, &runtime->sleep, wait);
2896
2897 snd_pcm_stream_lock_irq(substream);
2898 avail = snd_pcm_playback_avail(runtime);
2899 switch (runtime->status->state) {
2900 case SNDRV_PCM_STATE_RUNNING:
2901 case SNDRV_PCM_STATE_PREPARED:
2902 case SNDRV_PCM_STATE_PAUSED:
2903 if (avail >= runtime->control->avail_min) {
2904 mask = POLLOUT | POLLWRNORM;
2905 break;
2906 }
2907 /* Fall through */
2908 case SNDRV_PCM_STATE_DRAINING:
2909 mask = 0;
2910 break;
2911 default:
2912 mask = POLLOUT | POLLWRNORM | POLLERR;
2913 break;
2914 }
2915 snd_pcm_stream_unlock_irq(substream);
2916 return mask;
2917}
2918
2919static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2920{
Takashi Iwai877211f2005-11-17 13:59:38 +01002921 struct snd_pcm_file *pcm_file;
2922 struct snd_pcm_substream *substream;
2923 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 unsigned int mask;
2925 snd_pcm_uframes_t avail;
2926
2927 pcm_file = file->private_data;
2928
2929 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002930 if (PCM_RUNTIME_CHECK(substream))
2931 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 runtime = substream->runtime;
2933
2934 poll_wait(file, &runtime->sleep, wait);
2935
2936 snd_pcm_stream_lock_irq(substream);
2937 avail = snd_pcm_capture_avail(runtime);
2938 switch (runtime->status->state) {
2939 case SNDRV_PCM_STATE_RUNNING:
2940 case SNDRV_PCM_STATE_PREPARED:
2941 case SNDRV_PCM_STATE_PAUSED:
2942 if (avail >= runtime->control->avail_min) {
2943 mask = POLLIN | POLLRDNORM;
2944 break;
2945 }
2946 mask = 0;
2947 break;
2948 case SNDRV_PCM_STATE_DRAINING:
2949 if (avail > 0) {
2950 mask = POLLIN | POLLRDNORM;
2951 break;
2952 }
2953 /* Fall through */
2954 default:
2955 mask = POLLIN | POLLRDNORM | POLLERR;
2956 break;
2957 }
2958 snd_pcm_stream_unlock_irq(substream);
2959 return mask;
2960}
2961
2962/*
2963 * mmap support
2964 */
2965
2966/*
2967 * Only on coherent architectures, we can mmap the status and the control records
2968 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2969 */
2970#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2971/*
2972 * mmap status record
2973 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002974static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2975 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Takashi Iwai877211f2005-11-17 13:59:38 +01002977 struct snd_pcm_substream *substream = area->vm_private_data;
2978 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002981 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002983 vmf->page = virt_to_page(runtime->status);
2984 get_page(vmf->page);
2985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988static struct vm_operations_struct snd_pcm_vm_ops_status =
2989{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002990 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991};
2992
Takashi Iwai877211f2005-11-17 13:59:38 +01002993static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 struct vm_area_struct *area)
2995{
Takashi Iwai877211f2005-11-17 13:59:38 +01002996 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 long size;
2998 if (!(area->vm_flags & VM_READ))
2999 return -EINVAL;
3000 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003002 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return -EINVAL;
3004 area->vm_ops = &snd_pcm_vm_ops_status;
3005 area->vm_private_data = substream;
3006 area->vm_flags |= VM_RESERVED;
3007 return 0;
3008}
3009
3010/*
3011 * mmap control record
3012 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003013static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3014 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Takashi Iwai877211f2005-11-17 13:59:38 +01003016 struct snd_pcm_substream *substream = area->vm_private_data;
3017 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003020 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003022 vmf->page = virt_to_page(runtime->control);
3023 get_page(vmf->page);
3024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027static struct vm_operations_struct snd_pcm_vm_ops_control =
3028{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003029 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030};
3031
Takashi Iwai877211f2005-11-17 13:59:38 +01003032static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct vm_area_struct *area)
3034{
Takashi Iwai877211f2005-11-17 13:59:38 +01003035 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 long size;
3037 if (!(area->vm_flags & VM_READ))
3038 return -EINVAL;
3039 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003041 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 return -EINVAL;
3043 area->vm_ops = &snd_pcm_vm_ops_control;
3044 area->vm_private_data = substream;
3045 area->vm_flags |= VM_RESERVED;
3046 return 0;
3047}
3048#else /* ! coherent mmap */
3049/*
3050 * don't support mmap for status and control records.
3051 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003052static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct vm_area_struct *area)
3054{
3055 return -ENXIO;
3056}
Takashi Iwai877211f2005-11-17 13:59:38 +01003057static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct vm_area_struct *area)
3059{
3060 return -ENXIO;
3061}
3062#endif /* coherent mmap */
3063
3064/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003065 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003067static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3068 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Takashi Iwai877211f2005-11-17 13:59:38 +01003070 struct snd_pcm_substream *substream = area->vm_private_data;
3071 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 unsigned long offset;
3073 struct page * page;
3074 void *vaddr;
3075 size_t dma_bytes;
3076
3077 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003078 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003080 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3082 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003083 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (substream->ops->page) {
3085 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003086 if (!page)
3087 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 } else {
3089 vaddr = runtime->dma_area + offset;
3090 page = virt_to_page(vaddr);
3091 }
Nick Pigginb5810032005-10-29 18:16:12 -07003092 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003093 vmf->page = page;
3094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static struct vm_operations_struct snd_pcm_vm_ops_data =
3098{
3099 .open = snd_pcm_mmap_data_open,
3100 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003101 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102};
3103
3104/*
3105 * mmap the DMA buffer on RAM
3106 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003107static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3108 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 area->vm_ops = &snd_pcm_vm_ops_data;
3111 area->vm_private_data = substream;
3112 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003113 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return 0;
3115}
3116
3117/*
3118 * mmap the DMA buffer on I/O memory area
3119 */
3120#if SNDRV_PCM_INFO_MMAP_IOMEM
3121static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3122{
3123 .open = snd_pcm_mmap_data_open,
3124 .close = snd_pcm_mmap_data_close,
3125};
3126
Takashi Iwai877211f2005-11-17 13:59:38 +01003127int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3128 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 long size;
3131 unsigned long offset;
3132
3133#ifdef pgprot_noncached
3134 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3135#endif
3136 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3137 area->vm_private_data = substream;
3138 area->vm_flags |= VM_IO;
3139 size = area->vm_end - area->vm_start;
3140 offset = area->vm_pgoff << PAGE_SHIFT;
3141 if (io_remap_pfn_range(area, area->vm_start,
3142 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3143 size, area->vm_page_prot))
3144 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003145 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return 0;
3147}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003148
3149EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150#endif /* SNDRV_PCM_INFO_MMAP */
3151
3152/*
3153 * mmap DMA buffer
3154 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003155int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct vm_area_struct *area)
3157{
Takashi Iwai877211f2005-11-17 13:59:38 +01003158 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 long size;
3160 unsigned long offset;
3161 size_t dma_bytes;
3162
3163 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3164 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3165 return -EINVAL;
3166 } else {
3167 if (!(area->vm_flags & VM_READ))
3168 return -EINVAL;
3169 }
3170 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3172 return -EBADFD;
3173 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3174 return -ENXIO;
3175 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3176 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3177 return -EINVAL;
3178 size = area->vm_end - area->vm_start;
3179 offset = area->vm_pgoff << PAGE_SHIFT;
3180 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3181 if ((size_t)size > dma_bytes)
3182 return -EINVAL;
3183 if (offset > dma_bytes - size)
3184 return -EINVAL;
3185
3186 if (substream->ops->mmap)
3187 return substream->ops->mmap(substream, area);
3188 else
3189 return snd_pcm_default_mmap(substream, area);
3190}
3191
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003192EXPORT_SYMBOL(snd_pcm_mmap_data);
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3195{
Takashi Iwai877211f2005-11-17 13:59:38 +01003196 struct snd_pcm_file * pcm_file;
3197 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 unsigned long offset;
3199
3200 pcm_file = file->private_data;
3201 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003202 if (PCM_RUNTIME_CHECK(substream))
3203 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204
3205 offset = area->vm_pgoff << PAGE_SHIFT;
3206 switch (offset) {
3207 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003208 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return -ENXIO;
3210 return snd_pcm_mmap_status(substream, file, area);
3211 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
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_control(substream, file, area);
3215 default:
3216 return snd_pcm_mmap_data(substream, file, area);
3217 }
3218 return 0;
3219}
3220
3221static int snd_pcm_fasync(int fd, struct file * file, int on)
3222{
Takashi Iwai877211f2005-11-17 13:59:38 +01003223 struct snd_pcm_file * pcm_file;
3224 struct snd_pcm_substream *substream;
3225 struct snd_pcm_runtime *runtime;
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003226 int err = -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003228 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 pcm_file = file->private_data;
3230 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003231 if (PCM_RUNTIME_CHECK(substream))
3232 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 err = fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds685d87f2008-08-06 19:24:47 -07003235out:
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -06003236 unlock_kernel();
Jonathan Corbet60aa4922009-02-01 14:52:56 -07003237 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
3240/*
3241 * ioctl32 compat
3242 */
3243#ifdef CONFIG_COMPAT
3244#include "pcm_compat.c"
3245#else
3246#define snd_pcm_ioctl_compat NULL
3247#endif
3248
3249/*
3250 * To be removed helpers to keep binary compatibility
3251 */
3252
Takashi Iwai59d48582005-12-01 10:51:58 +01003253#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3255#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3256
Takashi Iwai877211f2005-11-17 13:59:38 +01003257static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3258 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
3260 unsigned int i;
3261
3262 memset(params, 0, sizeof(*params));
3263 params->flags = oparams->flags;
3264 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3265 params->masks[i].bits[0] = oparams->masks[i];
3266 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3267 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3268 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3269 params->info = oparams->info;
3270 params->msbits = oparams->msbits;
3271 params->rate_num = oparams->rate_num;
3272 params->rate_den = oparams->rate_den;
3273 params->fifo_size = oparams->fifo_size;
3274}
3275
Takashi Iwai877211f2005-11-17 13:59:38 +01003276static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3277 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 unsigned int i;
3280
3281 memset(oparams, 0, sizeof(*oparams));
3282 oparams->flags = params->flags;
3283 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3284 oparams->masks[i] = params->masks[i].bits[0];
3285 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3286 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3287 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3288 oparams->info = params->info;
3289 oparams->msbits = params->msbits;
3290 oparams->rate_num = params->rate_num;
3291 oparams->rate_den = params->rate_den;
3292 oparams->fifo_size = params->fifo_size;
3293}
3294
Takashi Iwai877211f2005-11-17 13:59:38 +01003295static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3296 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Takashi Iwai877211f2005-11-17 13:59:38 +01003298 struct snd_pcm_hw_params *params;
3299 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 int err;
3301
3302 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003303 if (!params)
3304 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Li Zefanef44a1e2009-04-10 09:43:08 +08003306 oparams = memdup_user(_oparams, sizeof(*oparams));
3307 if (IS_ERR(oparams)) {
3308 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 goto out;
3310 }
3311 snd_pcm_hw_convert_from_old_params(params, oparams);
3312 err = snd_pcm_hw_refine(substream, params);
3313 snd_pcm_hw_convert_to_old_params(oparams, params);
3314 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3315 if (!err)
3316 err = -EFAULT;
3317 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003318
3319 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320out:
3321 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return err;
3323}
3324
Takashi Iwai877211f2005-11-17 13:59:38 +01003325static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3326 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327{
Takashi Iwai877211f2005-11-17 13:59:38 +01003328 struct snd_pcm_hw_params *params;
3329 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 int err;
3331
3332 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003333 if (!params)
3334 return -ENOMEM;
3335
3336 oparams = memdup_user(_oparams, sizeof(*oparams));
3337 if (IS_ERR(oparams)) {
3338 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 goto out;
3340 }
3341 snd_pcm_hw_convert_from_old_params(params, oparams);
3342 err = snd_pcm_hw_params(substream, params);
3343 snd_pcm_hw_convert_to_old_params(oparams, params);
3344 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3345 if (!err)
3346 err = -EFAULT;
3347 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003348
3349 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350out:
3351 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 return err;
3353}
Takashi Iwai59d48582005-12-01 10:51:58 +01003354#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Cliff Cai70036092008-09-03 10:54:36 +02003356#ifndef CONFIG_MMU
3357unsigned long dummy_get_unmapped_area(struct file *file, unsigned long addr,
3358 unsigned long len, unsigned long pgoff,
3359 unsigned long flags)
3360{
3361 return 0;
3362}
3363#else
3364# define dummy_get_unmapped_area NULL
3365#endif
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367/*
3368 * Register section
3369 */
3370
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003371const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003373 .owner = THIS_MODULE,
3374 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003375 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003376 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003377 .release = snd_pcm_release,
3378 .poll = snd_pcm_playback_poll,
3379 .unlocked_ioctl = snd_pcm_playback_ioctl,
3380 .compat_ioctl = snd_pcm_ioctl_compat,
3381 .mmap = snd_pcm_mmap,
3382 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003383 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 },
3385 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003386 .owner = THIS_MODULE,
3387 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003388 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003389 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003390 .release = snd_pcm_release,
3391 .poll = snd_pcm_capture_poll,
3392 .unlocked_ioctl = snd_pcm_capture_ioctl,
3393 .compat_ioctl = snd_pcm_ioctl_compat,
3394 .mmap = snd_pcm_mmap,
3395 .fasync = snd_pcm_fasync,
Cliff Cai70036092008-09-03 10:54:36 +02003396 .get_unmapped_area = dummy_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398};