blob: f91a439f675cd5fdf10404ed2bada575888e2797 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -060025#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080027#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010029#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
37#include <asm/io.h>
Takashi Iwai9fe17b52010-05-12 10:32:42 +020038#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39#include <dma-coherence.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Compatibility
44 */
45
Takashi Iwai877211f2005-11-17 13:59:38 +010046struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned int flags;
48 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
49 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010050 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rmask;
53 unsigned int cmask;
54 unsigned int info;
55 unsigned int msbits;
56 unsigned int rate_num;
57 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010058 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned char reserved[64];
60};
61
Takashi Iwai59d48582005-12-01 10:51:58 +010062#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010063#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Takashi Iwai877211f2005-11-17 13:59:38 +010066static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010070#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010071static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 *
75 */
76
77DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020078EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020080static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static inline mm_segment_t snd_enter_user(void)
83{
84 mm_segment_t fs = get_fs();
85 set_fs(get_ds());
86 return fs;
87}
88
89static inline void snd_leave_user(mm_segment_t fs)
90{
91 set_fs(fs);
92}
93
94
95
Takashi Iwai877211f2005-11-17 13:59:38 +010096int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Takashi Iwai877211f2005-11-17 13:59:38 +010098 struct snd_pcm_runtime *runtime;
99 struct snd_pcm *pcm = substream->pcm;
100 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 memset(info, 0, sizeof(*info));
103 info->card = pcm->card->number;
104 info->device = pcm->device;
105 info->stream = substream->stream;
106 info->subdevice = substream->number;
107 strlcpy(info->id, pcm->id, sizeof(info->id));
108 strlcpy(info->name, pcm->name, sizeof(info->name));
109 info->dev_class = pcm->dev_class;
110 info->dev_subclass = pcm->dev_subclass;
111 info->subdevices_count = pstr->substream_count;
112 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
113 strlcpy(info->subname, substream->name, sizeof(info->subname));
114 runtime = substream->runtime;
115 /* AB: FIXME!!! This is definitely nonsense */
116 if (runtime) {
117 info->sync = runtime->sync;
118 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 }
120 return 0;
121}
122
Takashi Iwai877211f2005-11-17 13:59:38 +0100123int snd_pcm_info_user(struct snd_pcm_substream *substream,
124 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwai877211f2005-11-17 13:59:38 +0100126 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int err;
128
129 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 if (! info)
131 return -ENOMEM;
132 err = snd_pcm_info(substream, info);
133 if (err >= 0) {
134 if (copy_to_user(_info, info, sizeof(*info)))
135 err = -EFAULT;
136 }
137 kfree(info);
138 return err;
139}
140
141#undef RULES_DEBUG
142
143#ifdef RULES_DEBUG
144#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700145static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 HW_PARAM(ACCESS),
147 HW_PARAM(FORMAT),
148 HW_PARAM(SUBFORMAT),
149 HW_PARAM(SAMPLE_BITS),
150 HW_PARAM(FRAME_BITS),
151 HW_PARAM(CHANNELS),
152 HW_PARAM(RATE),
153 HW_PARAM(PERIOD_TIME),
154 HW_PARAM(PERIOD_SIZE),
155 HW_PARAM(PERIOD_BYTES),
156 HW_PARAM(PERIODS),
157 HW_PARAM(BUFFER_TIME),
158 HW_PARAM(BUFFER_SIZE),
159 HW_PARAM(BUFFER_BYTES),
160 HW_PARAM(TICK_TIME),
161};
162#endif
163
Takashi Iwai877211f2005-11-17 13:59:38 +0100164int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
165 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100168 struct snd_pcm_hardware *hw;
169 struct snd_interval *i = NULL;
170 struct snd_mask *m = NULL;
171 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int rstamps[constrs->rules_num];
173 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
174 unsigned int stamp = 2;
175 int changed, again;
176
177 params->info = 0;
178 params->fifo_size = 0;
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
180 params->msbits = 0;
181 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
182 params->rate_num = 0;
183 params->rate_den = 0;
184 }
185
186 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
187 m = hw_param_mask(params, k);
188 if (snd_mask_empty(m))
189 return -EINVAL;
190 if (!(params->rmask & (1 << k)))
191 continue;
192#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100193 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 changed = snd_mask_refine(m, constrs_mask(constrs, k));
197#ifdef RULES_DEBUG
198 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199#endif
200 if (changed)
201 params->cmask |= 1 << k;
202 if (changed < 0)
203 return changed;
204 }
205
206 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
207 i = hw_param_interval(params, k);
208 if (snd_interval_empty(i))
209 return -EINVAL;
210 if (!(params->rmask & (1 << k)))
211 continue;
212#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100213 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (i->empty)
215 printk("empty");
216 else
217 printk("%c%u %u%c",
218 i->openmin ? '(' : '[', i->min,
219 i->max, i->openmax ? ')' : ']');
220 printk(" -> ");
221#endif
222 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223#ifdef RULES_DEBUG
224 if (i->empty)
225 printk("empty\n");
226 else
227 printk("%c%u %u%c\n",
228 i->openmin ? '(' : '[', i->min,
229 i->max, i->openmax ? ')' : ']');
230#endif
231 if (changed)
232 params->cmask |= 1 << k;
233 if (changed < 0)
234 return changed;
235 }
236
237 for (k = 0; k < constrs->rules_num; k++)
238 rstamps[k] = 0;
239 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
240 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 do {
242 again = 0;
243 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100244 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int d;
246 int doit = 0;
247 if (r->cond && !(r->cond & params->flags))
248 continue;
249 for (d = 0; r->deps[d] >= 0; d++) {
250 if (vstamps[r->deps[d]] > rstamps[k]) {
251 doit = 1;
252 break;
253 }
254 }
255 if (!doit)
256 continue;
257#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100258 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (r->var >= 0) {
260 printk("%s = ", snd_pcm_hw_param_names[r->var]);
261 if (hw_is_mask(r->var)) {
262 m = hw_param_mask(params, r->var);
263 printk("%x", *m->bits);
264 } else {
265 i = hw_param_interval(params, r->var);
266 if (i->empty)
267 printk("empty");
268 else
269 printk("%c%u %u%c",
270 i->openmin ? '(' : '[', i->min,
271 i->max, i->openmax ? ')' : ']');
272 }
273 }
274#endif
275 changed = r->func(params, r);
276#ifdef RULES_DEBUG
277 if (r->var >= 0) {
278 printk(" -> ");
279 if (hw_is_mask(r->var))
280 printk("%x", *m->bits);
281 else {
282 if (i->empty)
283 printk("empty");
284 else
285 printk("%c%u %u%c",
286 i->openmin ? '(' : '[', i->min,
287 i->max, i->openmax ? ')' : ']');
288 }
289 }
290 printk("\n");
291#endif
292 rstamps[k] = stamp;
293 if (changed && r->var >= 0) {
294 params->cmask |= (1 << r->var);
295 vstamps[r->var] = stamp;
296 again = 1;
297 }
298 if (changed < 0)
299 return changed;
300 stamp++;
301 }
302 } while (again);
303 if (!params->msbits) {
304 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
305 if (snd_interval_single(i))
306 params->msbits = snd_interval_value(i);
307 }
308
309 if (!params->rate_den) {
310 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
311 if (snd_interval_single(i)) {
312 params->rate_num = snd_interval_value(i);
313 params->rate_den = 1;
314 }
315 }
316
317 hw = &substream->runtime->hw;
318 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200319 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
320 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100321 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
322 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
323 if (snd_mask_min(m) == snd_mask_max(m) &&
324 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200325 changed = substream->ops->ioctl(substream,
326 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200327 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200328 return changed;
329 }
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 params->rmask = 0;
332 return 0;
333}
334
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200335EXPORT_SYMBOL(snd_pcm_hw_refine);
336
Takashi Iwai877211f2005-11-17 13:59:38 +0100337static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
338 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Takashi Iwai877211f2005-11-17 13:59:38 +0100340 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
342
Li Zefanef44a1e2009-04-10 09:43:08 +0800343 params = memdup_user(_params, sizeof(*params));
344 if (IS_ERR(params))
345 return PTR_ERR(params);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 err = snd_pcm_hw_refine(substream, params);
348 if (copy_to_user(_params, params, sizeof(*params))) {
349 if (!err)
350 err = -EFAULT;
351 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 kfree(params);
354 return err;
355}
356
Takashi Iwai9442e692006-09-30 23:27:19 -0700357static int period_to_usecs(struct snd_pcm_runtime *runtime)
358{
359 int usecs;
360
361 if (! runtime->rate)
362 return -1; /* invalid */
363
364 /* take 75% of period time as the deadline */
365 usecs = (750000 / runtime->rate) * runtime->period_size;
366 usecs += ((750000 % runtime->rate) * runtime->period_size) /
367 runtime->rate;
368
369 return usecs;
370}
371
Takashi Iwai877211f2005-11-17 13:59:38 +0100372static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai877211f2005-11-17 13:59:38 +0100375 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700376 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned int bits;
378 snd_pcm_uframes_t frames;
379
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200380 if (PCM_RUNTIME_CHECK(substream))
381 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_pcm_stream_lock_irq(substream);
384 switch (runtime->status->state) {
385 case SNDRV_PCM_STATE_OPEN:
386 case SNDRV_PCM_STATE_SETUP:
387 case SNDRV_PCM_STATE_PREPARED:
388 break;
389 default:
390 snd_pcm_stream_unlock_irq(substream);
391 return -EBADFD;
392 }
393 snd_pcm_stream_unlock_irq(substream);
394#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
395 if (!substream->oss.oss)
396#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200397 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EBADFD;
399
400 params->rmask = ~0U;
401 err = snd_pcm_hw_refine(substream, params);
402 if (err < 0)
403 goto _error;
404
405 err = snd_pcm_hw_params_choose(substream, params);
406 if (err < 0)
407 goto _error;
408
409 if (substream->ops->hw_params != NULL) {
410 err = substream->ops->hw_params(substream, params);
411 if (err < 0)
412 goto _error;
413 }
414
415 runtime->access = params_access(params);
416 runtime->format = params_format(params);
417 runtime->subformat = params_subformat(params);
418 runtime->channels = params_channels(params);
419 runtime->rate = params_rate(params);
420 runtime->period_size = params_period_size(params);
421 runtime->periods = params_periods(params);
422 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 runtime->info = params->info;
424 runtime->rate_num = params->rate_num;
425 runtime->rate_den = params->rate_den;
Clemens Ladischab69a492010-11-15 10:46:23 +0100426 runtime->no_period_wakeup =
427 (params->info & SNDRV_PCM_INFO_NO_PERIOD_WAKEUP) &&
428 (params->flags & SNDRV_PCM_HW_PARAMS_NO_PERIOD_WAKEUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 bits = snd_pcm_format_physical_width(runtime->format);
431 runtime->sample_bits = bits;
432 bits *= runtime->channels;
433 runtime->frame_bits = bits;
434 frames = 1;
435 while (bits % 8 != 0) {
436 bits *= 2;
437 frames *= 2;
438 }
439 runtime->byte_align = bits / 8;
440 runtime->min_align = frames;
441
442 /* Default sw params */
443 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
444 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 runtime->start_threshold = 1;
447 runtime->stop_threshold = runtime->buffer_size;
448 runtime->silence_threshold = 0;
449 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200450 runtime->boundary = runtime->buffer_size;
451 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
452 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 snd_pcm_timer_resolution_change(substream);
455 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700456
James Bottomley82f68252010-07-05 22:53:06 +0200457 if (pm_qos_request_active(&substream->latency_pm_qos_req))
458 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700459 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200460 pm_qos_add_request(&substream->latency_pm_qos_req,
461 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463 _error:
464 /* hardware might be unuseable from this time,
465 so we force application to retry to set
466 the correct hardware parameter settings */
467 runtime->status->state = SNDRV_PCM_STATE_OPEN;
468 if (substream->ops->hw_free != NULL)
469 substream->ops->hw_free(substream);
470 return err;
471}
472
Takashi Iwai877211f2005-11-17 13:59:38 +0100473static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
474 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Takashi Iwai877211f2005-11-17 13:59:38 +0100476 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int err;
478
Li Zefanef44a1e2009-04-10 09:43:08 +0800479 params = memdup_user(_params, sizeof(*params));
480 if (IS_ERR(params))
481 return PTR_ERR(params);
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 err = snd_pcm_hw_params(substream, params);
484 if (copy_to_user(_params, params, sizeof(*params))) {
485 if (!err)
486 err = -EFAULT;
487 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 kfree(params);
490 return err;
491}
492
Takashi Iwai877211f2005-11-17 13:59:38 +0100493static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Takashi Iwai877211f2005-11-17 13:59:38 +0100495 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int result = 0;
497
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200498 if (PCM_RUNTIME_CHECK(substream))
499 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 snd_pcm_stream_lock_irq(substream);
502 switch (runtime->status->state) {
503 case SNDRV_PCM_STATE_SETUP:
504 case SNDRV_PCM_STATE_PREPARED:
505 break;
506 default:
507 snd_pcm_stream_unlock_irq(substream);
508 return -EBADFD;
509 }
510 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200511 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EBADFD;
513 if (substream->ops->hw_free)
514 result = substream->ops->hw_free(substream);
515 runtime->status->state = SNDRV_PCM_STATE_OPEN;
James Bottomley82f68252010-07-05 22:53:06 +0200516 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return result;
518}
519
Takashi Iwai877211f2005-11-17 13:59:38 +0100520static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
521 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Takashi Iwai877211f2005-11-17 13:59:38 +0100523 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200526 if (PCM_RUNTIME_CHECK(substream))
527 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 snd_pcm_stream_lock_irq(substream);
530 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
531 snd_pcm_stream_unlock_irq(substream);
532 return -EBADFD;
533 }
534 snd_pcm_stream_unlock_irq(substream);
535
536 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
537 return -EINVAL;
538 if (params->avail_min == 0)
539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (params->silence_size >= runtime->boundary) {
541 if (params->silence_threshold != 0)
542 return -EINVAL;
543 } else {
544 if (params->silence_size > params->silence_threshold)
545 return -EINVAL;
546 if (params->silence_threshold > runtime->buffer_size)
547 return -EINVAL;
548 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100549 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 snd_pcm_stream_lock_irq(substream);
551 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 runtime->period_step = params->period_step;
553 runtime->control->avail_min = params->avail_min;
554 runtime->start_threshold = params->start_threshold;
555 runtime->stop_threshold = params->stop_threshold;
556 runtime->silence_threshold = params->silence_threshold;
557 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 params->boundary = runtime->boundary;
559 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
561 runtime->silence_size > 0)
562 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100563 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100566 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Takashi Iwai877211f2005-11-17 13:59:38 +0100569static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
570 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Takashi Iwai877211f2005-11-17 13:59:38 +0100572 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err;
574 if (copy_from_user(&params, _params, sizeof(params)))
575 return -EFAULT;
576 err = snd_pcm_sw_params(substream, &params);
577 if (copy_to_user(_params, &params, sizeof(params)))
578 return -EFAULT;
579 return err;
580}
581
Takashi Iwai877211f2005-11-17 13:59:38 +0100582int snd_pcm_status(struct snd_pcm_substream *substream,
583 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Takashi Iwai877211f2005-11-17 13:59:38 +0100585 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 snd_pcm_stream_lock_irq(substream);
588 status->state = runtime->status->state;
589 status->suspended_state = runtime->status->suspended_state;
590 if (status->state == SNDRV_PCM_STATE_OPEN)
591 goto _end;
592 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100593 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100595 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
596 status->tstamp = runtime->status->tstamp;
597 goto _tstamp_end;
598 }
599 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100600 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100601 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 status->appl_ptr = runtime->control->appl_ptr;
603 status->hw_ptr = runtime->status->hw_ptr;
604 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
605 status->avail = snd_pcm_playback_avail(runtime);
606 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200607 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200609 status->delay += runtime->delay;
610 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 status->delay = 0;
612 } else {
613 status->avail = snd_pcm_capture_avail(runtime);
614 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200615 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 else
617 status->delay = 0;
618 }
619 status->avail_max = runtime->avail_max;
620 status->overrange = runtime->overrange;
621 runtime->avail_max = 0;
622 runtime->overrange = 0;
623 _end:
624 snd_pcm_stream_unlock_irq(substream);
625 return 0;
626}
627
Takashi Iwai877211f2005-11-17 13:59:38 +0100628static int snd_pcm_status_user(struct snd_pcm_substream *substream,
629 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Takashi Iwai877211f2005-11-17 13:59:38 +0100631 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int res;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 memset(&status, 0, sizeof(status));
635 res = snd_pcm_status(substream, &status);
636 if (res < 0)
637 return res;
638 if (copy_to_user(_status, &status, sizeof(status)))
639 return -EFAULT;
640 return 0;
641}
642
Takashi Iwai877211f2005-11-17 13:59:38 +0100643static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
644 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Takashi Iwai877211f2005-11-17 13:59:38 +0100646 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned int channel;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 channel = info->channel;
650 runtime = substream->runtime;
651 snd_pcm_stream_lock_irq(substream);
652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
653 snd_pcm_stream_unlock_irq(substream);
654 return -EBADFD;
655 }
656 snd_pcm_stream_unlock_irq(substream);
657 if (channel >= runtime->channels)
658 return -EINVAL;
659 memset(info, 0, sizeof(*info));
660 info->channel = channel;
661 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
662}
663
Takashi Iwai877211f2005-11-17 13:59:38 +0100664static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
665 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai877211f2005-11-17 13:59:38 +0100667 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int res;
669
670 if (copy_from_user(&info, _info, sizeof(info)))
671 return -EFAULT;
672 res = snd_pcm_channel_info(substream, &info);
673 if (res < 0)
674 return res;
675 if (copy_to_user(_info, &info, sizeof(info)))
676 return -EFAULT;
677 return 0;
678}
679
Takashi Iwai877211f2005-11-17 13:59:38 +0100680static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwai877211f2005-11-17 13:59:38 +0100682 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (runtime->trigger_master == NULL)
684 return;
685 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100686 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
688 snd_pcm_trigger_tstamp(runtime->trigger_master);
689 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
690 }
691 runtime->trigger_master = NULL;
692}
693
694struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 int (*pre_action)(struct snd_pcm_substream *substream, int state);
696 int (*do_action)(struct snd_pcm_substream *substream, int state);
697 void (*undo_action)(struct snd_pcm_substream *substream, int state);
698 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/*
702 * this functions is core for handling of linked stream
703 * Note: the stream state might be changed also on failure
704 * Note2: call with calling stream lock + link lock
705 */
706static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100707 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int state, int do_lock)
709{
Takashi Iwai877211f2005-11-17 13:59:38 +0100710 struct snd_pcm_substream *s = NULL;
711 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int res = 0;
713
Takashi Iwaief991b92007-02-22 12:52:53 +0100714 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200716 spin_lock_nested(&s->self_group.lock,
717 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 res = ops->pre_action(s, state);
719 if (res < 0)
720 goto _unlock;
721 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100722 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 res = ops->do_action(s, state);
724 if (res < 0) {
725 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100726 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (s1 == s) /* failed stream */
728 break;
729 ops->undo_action(s1, state);
730 }
731 }
732 s = NULL; /* unlock all */
733 goto _unlock;
734 }
735 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100736 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ops->post_action(s, state);
738 }
739 _unlock:
740 if (do_lock) {
741 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100742 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (s1 != substream)
744 spin_unlock(&s1->self_group.lock);
745 if (s1 == s) /* end */
746 break;
747 }
748 }
749 return res;
750}
751
752/*
753 * Note: call with stream lock
754 */
755static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100756 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int state)
758{
759 int res;
760
761 res = ops->pre_action(substream, state);
762 if (res < 0)
763 return res;
764 res = ops->do_action(substream, state);
765 if (res == 0)
766 ops->post_action(substream, state);
767 else if (ops->undo_action)
768 ops->undo_action(substream, state);
769 return res;
770}
771
772/*
773 * Note: call with stream lock
774 */
775static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100776 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int state)
778{
779 int res;
780
781 if (snd_pcm_stream_linked(substream)) {
782 if (!spin_trylock(&substream->group->lock)) {
783 spin_unlock(&substream->self_group.lock);
784 spin_lock(&substream->group->lock);
785 spin_lock(&substream->self_group.lock);
786 }
787 res = snd_pcm_action_group(ops, substream, state, 1);
788 spin_unlock(&substream->group->lock);
789 } else {
790 res = snd_pcm_action_single(ops, substream, state);
791 }
792 return res;
793}
794
795/*
796 * Note: don't use any locks before
797 */
798static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100799 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int state)
801{
802 int res;
803
804 read_lock_irq(&snd_pcm_link_rwlock);
805 if (snd_pcm_stream_linked(substream)) {
806 spin_lock(&substream->group->lock);
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_group(ops, substream, state, 1);
809 spin_unlock(&substream->self_group.lock);
810 spin_unlock(&substream->group->lock);
811 } else {
812 spin_lock(&substream->self_group.lock);
813 res = snd_pcm_action_single(ops, substream, state);
814 spin_unlock(&substream->self_group.lock);
815 }
816 read_unlock_irq(&snd_pcm_link_rwlock);
817 return res;
818}
819
820/*
821 */
822static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100823 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int state)
825{
826 int res;
827
828 down_read(&snd_pcm_link_rwsem);
829 if (snd_pcm_stream_linked(substream))
830 res = snd_pcm_action_group(ops, substream, state, 0);
831 else
832 res = snd_pcm_action_single(ops, substream, state);
833 up_read(&snd_pcm_link_rwsem);
834 return res;
835}
836
837/*
838 * start callbacks
839 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100840static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai877211f2005-11-17 13:59:38 +0100842 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
844 return -EBADFD;
845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
846 !snd_pcm_playback_data(substream))
847 return -EPIPE;
848 runtime->trigger_master = substream;
849 return 0;
850}
851
Takashi Iwai877211f2005-11-17 13:59:38 +0100852static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 if (substream->runtime->trigger_master != substream)
855 return 0;
856 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
857}
858
Takashi Iwai877211f2005-11-17 13:59:38 +0100859static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 if (substream->runtime->trigger_master == substream)
862 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
863}
864
Takashi Iwai877211f2005-11-17 13:59:38 +0100865static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200869 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200870 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
871 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 runtime->status->state = state;
873 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
874 runtime->silence_size > 0)
875 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100877 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
878 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static struct action_ops snd_pcm_action_start = {
882 .pre_action = snd_pcm_pre_start,
883 .do_action = snd_pcm_do_start,
884 .undo_action = snd_pcm_undo_start,
885 .post_action = snd_pcm_post_start
886};
887
888/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700889 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200890 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100892int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Takashi Iwai877211f2005-11-17 13:59:38 +0100894 return snd_pcm_action(&snd_pcm_action_start, substream,
895 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
898/*
899 * stop callbacks
900 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100901static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Takashi Iwai877211f2005-11-17 13:59:38 +0100903 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
905 return -EBADFD;
906 runtime->trigger_master = substream;
907 return 0;
908}
909
Takashi Iwai877211f2005-11-17 13:59:38 +0100910static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 if (substream->runtime->trigger_master == substream &&
913 snd_pcm_running(substream))
914 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
915 return 0; /* unconditonally stop all substreams */
916}
917
Takashi Iwai877211f2005-11-17 13:59:38 +0100918static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Takashi Iwai877211f2005-11-17 13:59:38 +0100920 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (runtime->status->state != state) {
922 snd_pcm_trigger_tstamp(substream);
923 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100924 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
925 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100929 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
932static struct action_ops snd_pcm_action_stop = {
933 .pre_action = snd_pcm_pre_stop,
934 .do_action = snd_pcm_do_stop,
935 .post_action = snd_pcm_post_stop
936};
937
938/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700939 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200940 * @substream: the PCM substream instance
941 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700943 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100945int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
948}
949
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200950EXPORT_SYMBOL(snd_pcm_stop);
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700953 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200954 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700956 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * Unlike snd_pcm_stop(), this affects only the given stream.
958 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100959int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Takashi Iwai877211f2005-11-17 13:59:38 +0100961 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
962 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*
966 * pause callbacks
967 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100968static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Takashi Iwai877211f2005-11-17 13:59:38 +0100970 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
972 return -ENOSYS;
973 if (push) {
974 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
975 return -EBADFD;
976 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
977 return -EBADFD;
978 runtime->trigger_master = substream;
979 return 0;
980}
981
Takashi Iwai877211f2005-11-17 13:59:38 +0100982static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
984 if (substream->runtime->trigger_master != substream)
985 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200986 /* some drivers might use hw_ptr to recover from the pause -
987 update the hw_ptr now */
988 if (push)
989 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200990 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
991 * a delta betwen the current jiffies, this gives a large enough
992 * delta, effectively to skip the check once.
993 */
994 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return substream->ops->trigger(substream,
996 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
997 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
998}
999
Takashi Iwai877211f2005-11-17 13:59:38 +01001000static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 if (substream->runtime->trigger_master == substream)
1003 substream->ops->trigger(substream,
1004 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1005 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1006}
1007
Takashi Iwai877211f2005-11-17 13:59:38 +01001008static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Takashi Iwai877211f2005-11-17 13:59:38 +01001010 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 snd_pcm_trigger_tstamp(substream);
1012 if (push) {
1013 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1014 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001015 snd_timer_notify(substream->timer,
1016 SNDRV_TIMER_EVENT_MPAUSE,
1017 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001019 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 } else {
1021 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001023 snd_timer_notify(substream->timer,
1024 SNDRV_TIMER_EVENT_MCONTINUE,
1025 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027}
1028
1029static struct action_ops snd_pcm_action_pause = {
1030 .pre_action = snd_pcm_pre_pause,
1031 .do_action = snd_pcm_do_pause,
1032 .undo_action = snd_pcm_undo_pause,
1033 .post_action = snd_pcm_post_pause
1034};
1035
1036/*
1037 * Push/release the pause for all linked streams.
1038 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001039static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1042}
1043
1044#ifdef CONFIG_PM
1045/* suspend */
1046
Takashi Iwai877211f2005-11-17 13:59:38 +01001047static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Takashi Iwai877211f2005-11-17 13:59:38 +01001049 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1051 return -EBUSY;
1052 runtime->trigger_master = substream;
1053 return 0;
1054}
1055
Takashi Iwai877211f2005-11-17 13:59:38 +01001056static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Takashi Iwai877211f2005-11-17 13:59:38 +01001058 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (runtime->trigger_master != substream)
1060 return 0;
1061 if (! snd_pcm_running(substream))
1062 return 0;
1063 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1064 return 0; /* suspend unconditionally */
1065}
1066
Takashi Iwai877211f2005-11-17 13:59:38 +01001067static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
Takashi Iwai877211f2005-11-17 13:59:38 +01001069 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 snd_pcm_trigger_tstamp(substream);
1071 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001072 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1073 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 runtime->status->suspended_state = runtime->status->state;
1075 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001077 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
1080static struct action_ops snd_pcm_action_suspend = {
1081 .pre_action = snd_pcm_pre_suspend,
1082 .do_action = snd_pcm_do_suspend,
1083 .post_action = snd_pcm_post_suspend
1084};
1085
1086/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001087 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001088 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * After this call, all streams are changed to SUSPENDED state.
1091 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001092int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 int err;
1095 unsigned long flags;
1096
Takashi Iwai603bf522005-11-17 15:59:14 +01001097 if (! substream)
1098 return 0;
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 snd_pcm_stream_lock_irqsave(substream, flags);
1101 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1102 snd_pcm_stream_unlock_irqrestore(substream, flags);
1103 return err;
1104}
1105
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001106EXPORT_SYMBOL(snd_pcm_suspend);
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001109 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001110 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * After this call, all streams are changed to SUSPENDED state.
1113 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001114int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Takashi Iwai877211f2005-11-17 13:59:38 +01001116 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int stream, err = 0;
1118
Takashi Iwai603bf522005-11-17 15:59:14 +01001119 if (! pcm)
1120 return 0;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001123 for (substream = pcm->streams[stream].substream;
1124 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 /* FIXME: the open/close code should lock this as well */
1126 if (substream->runtime == NULL)
1127 continue;
1128 err = snd_pcm_suspend(substream);
1129 if (err < 0 && err != -EBUSY)
1130 return err;
1131 }
1132 }
1133 return 0;
1134}
1135
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001136EXPORT_SYMBOL(snd_pcm_suspend_all);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/* resume */
1139
Takashi Iwai877211f2005-11-17 13:59:38 +01001140static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Takashi Iwai877211f2005-11-17 13:59:38 +01001142 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1144 return -ENOSYS;
1145 runtime->trigger_master = substream;
1146 return 0;
1147}
1148
Takashi Iwai877211f2005-11-17 13:59:38 +01001149static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Takashi Iwai877211f2005-11-17 13:59:38 +01001151 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (runtime->trigger_master != substream)
1153 return 0;
1154 /* DMA not running previously? */
1155 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1156 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1157 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1158 return 0;
1159 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1160}
1161
Takashi Iwai877211f2005-11-17 13:59:38 +01001162static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 if (substream->runtime->trigger_master == substream &&
1165 snd_pcm_running(substream))
1166 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1167}
1168
Takashi Iwai877211f2005-11-17 13:59:38 +01001169static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Takashi Iwai877211f2005-11-17 13:59:38 +01001171 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 snd_pcm_trigger_tstamp(substream);
1173 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1175 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static struct action_ops snd_pcm_action_resume = {
1180 .pre_action = snd_pcm_pre_resume,
1181 .do_action = snd_pcm_do_resume,
1182 .undo_action = snd_pcm_undo_resume,
1183 .post_action = snd_pcm_post_resume
1184};
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwai877211f2005-11-17 13:59:38 +01001188 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int res;
1190
1191 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001192 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1194 snd_power_unlock(card);
1195 return res;
1196}
1197
1198#else
1199
Takashi Iwai877211f2005-11-17 13:59:38 +01001200static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 return -ENOSYS;
1203}
1204
1205#endif /* CONFIG_PM */
1206
1207/*
1208 * xrun ioctl
1209 *
1210 * Change the RUNNING stream(s) to XRUN state.
1211 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001212static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai877211f2005-11-17 13:59:38 +01001214 struct snd_card *card = substream->pcm->card;
1215 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int result;
1217
1218 snd_power_lock(card);
1219 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001220 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (result < 0)
1222 goto _unlock;
1223 }
1224
1225 snd_pcm_stream_lock_irq(substream);
1226 switch (runtime->status->state) {
1227 case SNDRV_PCM_STATE_XRUN:
1228 result = 0; /* already there */
1229 break;
1230 case SNDRV_PCM_STATE_RUNNING:
1231 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1232 break;
1233 default:
1234 result = -EBADFD;
1235 }
1236 snd_pcm_stream_unlock_irq(substream);
1237 _unlock:
1238 snd_power_unlock(card);
1239 return result;
1240}
1241
1242/*
1243 * reset ioctl
1244 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai877211f2005-11-17 13:59:38 +01001247 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 switch (runtime->status->state) {
1249 case SNDRV_PCM_STATE_RUNNING:
1250 case SNDRV_PCM_STATE_PREPARED:
1251 case SNDRV_PCM_STATE_PAUSED:
1252 case SNDRV_PCM_STATE_SUSPENDED:
1253 return 0;
1254 default:
1255 return -EBADFD;
1256 }
1257}
1258
Takashi Iwai877211f2005-11-17 13:59:38 +01001259static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwai877211f2005-11-17 13:59:38 +01001261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1263 if (err < 0)
1264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001266 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1267 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 runtime->silence_start = runtime->status->hw_ptr;
1269 runtime->silence_filled = 0;
1270 return 0;
1271}
1272
Takashi Iwai877211f2005-11-17 13:59:38 +01001273static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Takashi Iwai877211f2005-11-17 13:59:38 +01001275 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 runtime->control->appl_ptr = runtime->status->hw_ptr;
1277 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1278 runtime->silence_size > 0)
1279 snd_pcm_playback_silence(substream, ULONG_MAX);
1280}
1281
1282static struct action_ops snd_pcm_action_reset = {
1283 .pre_action = snd_pcm_pre_reset,
1284 .do_action = snd_pcm_do_reset,
1285 .post_action = snd_pcm_post_reset
1286};
1287
Takashi Iwai877211f2005-11-17 13:59:38 +01001288static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1291}
1292
1293/*
1294 * prepare ioctl
1295 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001296/* we use the second argument for updating f_flags */
1297static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1298 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Takashi Iwai877211f2005-11-17 13:59:38 +01001300 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001301 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1302 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EBADFD;
1304 if (snd_pcm_running(substream))
1305 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001306 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308}
1309
Takashi Iwai877211f2005-11-17 13:59:38 +01001310static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 int err;
1313 err = substream->ops->prepare(substream);
1314 if (err < 0)
1315 return err;
1316 return snd_pcm_do_reset(substream, 0);
1317}
1318
Takashi Iwai877211f2005-11-17 13:59:38 +01001319static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
Takashi Iwai877211f2005-11-17 13:59:38 +01001321 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 runtime->control->appl_ptr = runtime->status->hw_ptr;
1323 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1324}
1325
1326static struct action_ops snd_pcm_action_prepare = {
1327 .pre_action = snd_pcm_pre_prepare,
1328 .do_action = snd_pcm_do_prepare,
1329 .post_action = snd_pcm_post_prepare
1330};
1331
1332/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001333 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001334 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001335 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001337static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1338 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001341 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001342 int f_flags;
1343
1344 if (file)
1345 f_flags = file->f_flags;
1346 else
1347 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001350 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001351 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1352 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 snd_power_unlock(card);
1354 return res;
1355}
1356
1357/*
1358 * drain ioctl
1359 */
1360
Takashi Iwai877211f2005-11-17 13:59:38 +01001361static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 substream->runtime->trigger_master = substream;
1364 return 0;
1365}
1366
Takashi Iwai877211f2005-11-17 13:59:38 +01001367static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Takashi Iwai877211f2005-11-17 13:59:38 +01001369 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1371 switch (runtime->status->state) {
1372 case SNDRV_PCM_STATE_PREPARED:
1373 /* start playback stream if possible */
1374 if (! snd_pcm_playback_empty(substream)) {
1375 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1376 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1377 }
1378 break;
1379 case SNDRV_PCM_STATE_RUNNING:
1380 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1381 break;
1382 default:
1383 break;
1384 }
1385 } else {
1386 /* stop running stream */
1387 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001388 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001390 snd_pcm_do_stop(substream, new_state);
1391 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 }
1394 return 0;
1395}
1396
Takashi Iwai877211f2005-11-17 13:59:38 +01001397static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399}
1400
1401static struct action_ops snd_pcm_action_drain_init = {
1402 .pre_action = snd_pcm_pre_drain_init,
1403 .do_action = snd_pcm_do_drain_init,
1404 .post_action = snd_pcm_post_drain_init
1405};
1406
Takashi Iwai877211f2005-11-17 13:59:38 +01001407static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409/*
1410 * Drain the stream(s).
1411 * When the substream is linked, sync until the draining of all playback streams
1412 * is finished.
1413 * After this call, all streams are supposed to be either SETUP or DRAINING
1414 * (capture only) state.
1415 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001416static int snd_pcm_drain(struct snd_pcm_substream *substream,
1417 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Takashi Iwai877211f2005-11-17 13:59:38 +01001419 struct snd_card *card;
1420 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001421 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001422 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001424 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 card = substream->pcm->card;
1427 runtime = substream->runtime;
1428
1429 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1430 return -EBADFD;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 snd_power_lock(card);
1433 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001434 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001435 if (result < 0) {
1436 snd_power_unlock(card);
1437 return result;
1438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001441 if (file) {
1442 if (file->f_flags & O_NONBLOCK)
1443 nonblock = 1;
1444 } else if (substream->f_flags & O_NONBLOCK)
1445 nonblock = 1;
1446
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001447 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001448 snd_pcm_stream_lock_irq(substream);
1449 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001450 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001451 snd_pcm_pause(substream, 0);
1452
1453 /* pre-start/stop - all running streams are changed to DRAINING state */
1454 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001455 if (result < 0)
1456 goto unlock;
1457 /* in non-blocking, we don't wait in ioctl but let caller poll */
1458 if (nonblock) {
1459 result = -EAGAIN;
1460 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 for (;;) {
1464 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001465 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (signal_pending(current)) {
1467 result = -ERESTARTSYS;
1468 break;
1469 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001470 /* find a substream to drain */
1471 to_check = NULL;
1472 snd_pcm_group_for_each_entry(s, substream) {
1473 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1474 continue;
1475 runtime = s->runtime;
1476 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1477 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001478 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001479 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001480 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001481 if (!to_check)
1482 break; /* all drained */
1483 init_waitqueue_entry(&wait, current);
1484 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 set_current_state(TASK_INTERRUPTIBLE);
1486 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001487 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 snd_power_unlock(card);
1489 tout = schedule_timeout(10 * HZ);
1490 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001491 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001493 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (tout == 0) {
1495 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1496 result = -ESTRPIPE;
1497 else {
1498 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1499 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1500 result = -EIO;
1501 }
1502 break;
1503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001505
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001506 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001507 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001508 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 return result;
1512}
1513
1514/*
1515 * drop ioctl
1516 *
1517 * Immediately put all linked substreams into SETUP state.
1518 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001519static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Takashi Iwai877211f2005-11-17 13:59:38 +01001521 struct snd_pcm_runtime *runtime;
1522 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 int result = 0;
1524
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001525 if (PCM_RUNTIME_CHECK(substream))
1526 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 runtime = substream->runtime;
1528 card = substream->pcm->card;
1529
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001530 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001531 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1532 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EBADFD;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 snd_pcm_stream_lock_irq(substream);
1536 /* resume pause */
1537 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1538 snd_pcm_pause(substream, 0);
1539
1540 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1541 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1542 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return result;
1545}
1546
1547
1548/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549static struct file *snd_pcm_file_fd(int fd)
1550{
1551 struct file *file;
1552 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001553 unsigned int minor;
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 file = fget(fd);
1556 if (!file)
1557 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001558 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!S_ISCHR(inode->i_mode) ||
1560 imajor(inode) != snd_major) {
1561 fput(file);
1562 return NULL;
1563 }
1564 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001565 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1566 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 fput(file);
1568 return NULL;
1569 }
1570 return file;
1571}
1572
1573/*
1574 * PCM link handling
1575 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001576static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int res = 0;
1579 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001580 struct snd_pcm_file *pcm_file;
1581 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583 file = snd_pcm_file_fd(fd);
1584 if (!file)
1585 return -EBADFD;
1586 pcm_file = file->private_data;
1587 substream1 = pcm_file->substream;
1588 down_write(&snd_pcm_link_rwsem);
1589 write_lock_irq(&snd_pcm_link_rwlock);
1590 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1591 substream->runtime->status->state != substream1->runtime->status->state) {
1592 res = -EBADFD;
1593 goto _end;
1594 }
1595 if (snd_pcm_stream_linked(substream1)) {
1596 res = -EALREADY;
1597 goto _end;
1598 }
1599 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001600 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (substream->group == NULL) {
1602 res = -ENOMEM;
1603 goto _end;
1604 }
1605 spin_lock_init(&substream->group->lock);
1606 INIT_LIST_HEAD(&substream->group->substreams);
1607 list_add_tail(&substream->link_list, &substream->group->substreams);
1608 substream->group->count = 1;
1609 }
1610 list_add_tail(&substream1->link_list, &substream->group->substreams);
1611 substream->group->count++;
1612 substream1->group = substream->group;
1613 _end:
1614 write_unlock_irq(&snd_pcm_link_rwlock);
1615 up_write(&snd_pcm_link_rwsem);
1616 fput(file);
1617 return res;
1618}
1619
Takashi Iwai877211f2005-11-17 13:59:38 +01001620static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 substream->group = &substream->self_group;
1623 INIT_LIST_HEAD(&substream->self_group.substreams);
1624 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1625}
1626
Takashi Iwai877211f2005-11-17 13:59:38 +01001627static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwaief991b92007-02-22 12:52:53 +01001629 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int res = 0;
1631
1632 down_write(&snd_pcm_link_rwsem);
1633 write_lock_irq(&snd_pcm_link_rwlock);
1634 if (!snd_pcm_stream_linked(substream)) {
1635 res = -EALREADY;
1636 goto _end;
1637 }
1638 list_del(&substream->link_list);
1639 substream->group->count--;
1640 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001641 snd_pcm_group_for_each_entry(s, substream) {
1642 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 }
1645 kfree(substream->group);
1646 }
1647 relink_to_local(substream);
1648 _end:
1649 write_unlock_irq(&snd_pcm_link_rwlock);
1650 up_write(&snd_pcm_link_rwsem);
1651 return res;
1652}
1653
1654/*
1655 * hw configurator
1656 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001657static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1658 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Takashi Iwai877211f2005-11-17 13:59:38 +01001660 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1662 hw_param_interval_c(params, rule->deps[1]), &t);
1663 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1664}
1665
Takashi Iwai877211f2005-11-17 13:59:38 +01001666static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1667 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Takashi Iwai877211f2005-11-17 13:59:38 +01001669 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1671 hw_param_interval_c(params, rule->deps[1]), &t);
1672 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1673}
1674
Takashi Iwai877211f2005-11-17 13:59:38 +01001675static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1676 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
Takashi Iwai877211f2005-11-17 13:59:38 +01001678 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1680 hw_param_interval_c(params, rule->deps[1]),
1681 (unsigned long) rule->private, &t);
1682 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1683}
1684
Takashi Iwai877211f2005-11-17 13:59:38 +01001685static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1686 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Takashi Iwai877211f2005-11-17 13:59:38 +01001688 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1690 (unsigned long) rule->private,
1691 hw_param_interval_c(params, rule->deps[1]), &t);
1692 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1693}
1694
Takashi Iwai877211f2005-11-17 13:59:38 +01001695static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1696 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001699 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1700 struct snd_mask m;
1701 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 snd_mask_any(&m);
1703 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1704 int bits;
1705 if (! snd_mask_test(mask, k))
1706 continue;
1707 bits = snd_pcm_format_physical_width(k);
1708 if (bits <= 0)
1709 continue; /* ignore invalid formats */
1710 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1711 snd_mask_reset(&m, k);
1712 }
1713 return snd_mask_refine(mask, &m);
1714}
1715
Takashi Iwai877211f2005-11-17 13:59:38 +01001716static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1717 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Takashi Iwai877211f2005-11-17 13:59:38 +01001719 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 unsigned int k;
1721 t.min = UINT_MAX;
1722 t.max = 0;
1723 t.openmin = 0;
1724 t.openmax = 0;
1725 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1726 int bits;
1727 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1728 continue;
1729 bits = snd_pcm_format_physical_width(k);
1730 if (bits <= 0)
1731 continue; /* ignore invalid formats */
1732 if (t.min > (unsigned)bits)
1733 t.min = bits;
1734 if (t.max < (unsigned)bits)
1735 t.max = bits;
1736 }
1737 t.integer = 1;
1738 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1739}
1740
1741#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1742#error "Change this table"
1743#endif
1744
1745static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1746 48000, 64000, 88200, 96000, 176400, 192000 };
1747
Clemens Ladisch7653d552007-08-13 17:38:54 +02001748const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1749 .count = ARRAY_SIZE(rates),
1750 .list = rates,
1751};
1752
Takashi Iwai877211f2005-11-17 13:59:38 +01001753static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai877211f2005-11-17 13:59:38 +01001756 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001758 snd_pcm_known_rates.count,
1759 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
Takashi Iwai877211f2005-11-17 13:59:38 +01001762static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1763 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Takashi Iwai877211f2005-11-17 13:59:38 +01001765 struct snd_interval t;
1766 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 t.min = 0;
1768 t.max = substream->buffer_bytes_max;
1769 t.openmin = 0;
1770 t.openmax = 0;
1771 t.integer = 1;
1772 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1773}
1774
Takashi Iwai877211f2005-11-17 13:59:38 +01001775int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Takashi Iwai877211f2005-11-17 13:59:38 +01001777 struct snd_pcm_runtime *runtime = substream->runtime;
1778 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 int k, err;
1780
1781 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1782 snd_mask_any(constrs_mask(constrs, k));
1783 }
1784
1785 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1786 snd_interval_any(constrs_interval(constrs, k));
1787 }
1788
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1794
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1796 snd_pcm_hw_rule_format, NULL,
1797 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1798 if (err < 0)
1799 return err;
1800 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1801 snd_pcm_hw_rule_sample_bits, NULL,
1802 SNDRV_PCM_HW_PARAM_FORMAT,
1803 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1804 if (err < 0)
1805 return err;
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1807 snd_pcm_hw_rule_div, NULL,
1808 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1809 if (err < 0)
1810 return err;
1811 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1812 snd_pcm_hw_rule_mul, NULL,
1813 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1814 if (err < 0)
1815 return err;
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1817 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1818 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1819 if (err < 0)
1820 return err;
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1822 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1823 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1827 snd_pcm_hw_rule_div, NULL,
1828 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1833 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1837 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1838 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1852 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1853 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1857 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1858 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1862 snd_pcm_hw_rule_mul, NULL,
1863 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1867 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1868 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1872 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1877 snd_pcm_hw_rule_muldivk, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1882 snd_pcm_hw_rule_muldivk, (void*) 8,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1893 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1894 if (err < 0)
1895 return err;
1896 return 0;
1897}
1898
Takashi Iwai877211f2005-11-17 13:59:38 +01001899int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Takashi Iwai877211f2005-11-17 13:59:38 +01001901 struct snd_pcm_runtime *runtime = substream->runtime;
1902 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int err;
1904 unsigned int mask = 0;
1905
1906 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1911 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1912 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1913 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1914 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1915 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1916 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1917 }
1918 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001919 if (err < 0)
1920 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001923 if (err < 0)
1924 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001927 if (err < 0)
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1931 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001932 if (err < 0)
1933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1936 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001937 if (err < 0)
1938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1941 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001942 if (err < 0)
1943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1946 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001947 if (err < 0)
1948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1951 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001952 if (err < 0)
1953 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1956 snd_pcm_hw_rule_buffer_bytes_max, substream,
1957 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1958 if (err < 0)
1959 return err;
1960
1961 /* FIXME: remove */
1962 if (runtime->dma_bytes) {
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001964 if (err < 0)
1965 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967
1968 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1969 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1970 snd_pcm_hw_rule_rate, hw,
1971 SNDRV_PCM_HW_PARAM_RATE, -1);
1972 if (err < 0)
1973 return err;
1974 }
1975
1976 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1978
1979 return 0;
1980}
1981
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001982static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001985}
1986
1987void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1988{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001989 substream->ref_count--;
1990 if (substream->ref_count > 0)
1991 return;
1992
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001993 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001994 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (substream->ops->hw_free != NULL)
1996 substream->ops->hw_free(substream);
1997 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001998 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002000 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2001 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002002 if (substream->pcm_release) {
2003 substream->pcm_release(substream);
2004 substream->pcm_release = NULL;
2005 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002006 snd_pcm_detach_substream(substream);
2007}
2008
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002009EXPORT_SYMBOL(snd_pcm_release_substream);
2010
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002011int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2012 struct file *file,
2013 struct snd_pcm_substream **rsubstream)
2014{
2015 struct snd_pcm_substream *substream;
2016 int err;
2017
2018 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2019 if (err < 0)
2020 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002021 if (substream->ref_count > 1) {
2022 *rsubstream = substream;
2023 return 0;
2024 }
2025
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002026 err = snd_pcm_hw_constraints_init(substream);
2027 if (err < 0) {
2028 snd_printd("snd_pcm_hw_constraints_init failed\n");
2029 goto error;
2030 }
2031
2032 if ((err = substream->ops->open(substream)) < 0)
2033 goto error;
2034
2035 substream->hw_opened = 1;
2036
2037 err = snd_pcm_hw_constraints_complete(substream);
2038 if (err < 0) {
2039 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2040 goto error;
2041 }
2042
2043 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002045
2046 error:
2047 snd_pcm_release_substream(substream);
2048 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002051EXPORT_SYMBOL(snd_pcm_open_substream);
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002054 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002056 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
Takashi Iwai877211f2005-11-17 13:59:38 +01002058 struct snd_pcm_file *pcm_file;
2059 struct snd_pcm_substream *substream;
2060 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002061 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002063 if (rpcm_file)
2064 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002066 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2067 if (err < 0)
2068 return err;
2069
Takashi Iwai548a6482006-07-31 16:51:51 +02002070 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2071 if (pcm_file == NULL) {
2072 snd_pcm_release_substream(substream);
2073 return -ENOMEM;
2074 }
2075 pcm_file->substream = substream;
2076 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002077 str = substream->pstr;
2078 substream->file = pcm_file;
2079 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002082 if (rpcm_file)
2083 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return 0;
2085}
2086
Clemens Ladischf87135f2005-11-20 14:06:59 +01002087static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Takashi Iwai877211f2005-11-17 13:59:38 +01002089 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002090 int err = nonseekable_open(inode, file);
2091 if (err < 0)
2092 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002093 pcm = snd_lookup_minor_data(iminor(inode),
2094 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2095 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2096}
2097
2098static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2099{
2100 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002101 int err = nonseekable_open(inode, file);
2102 if (err < 0)
2103 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002104 pcm = snd_lookup_minor_data(iminor(inode),
2105 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2106 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2107}
2108
2109static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2110{
2111 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002112 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 wait_queue_t wait;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (pcm == NULL) {
2116 err = -ENODEV;
2117 goto __error1;
2118 }
2119 err = snd_card_file_add(pcm->card, file);
2120 if (err < 0)
2121 goto __error1;
2122 if (!try_module_get(pcm->card->module)) {
2123 err = -EFAULT;
2124 goto __error2;
2125 }
2126 init_waitqueue_entry(&wait, current);
2127 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002128 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002130 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (err >= 0)
2132 break;
2133 if (err == -EAGAIN) {
2134 if (file->f_flags & O_NONBLOCK) {
2135 err = -EBUSY;
2136 break;
2137 }
2138 } else
2139 break;
2140 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002141 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002143 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (signal_pending(current)) {
2145 err = -ERESTARTSYS;
2146 break;
2147 }
2148 }
2149 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002150 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (err < 0)
2152 goto __error;
2153 return err;
2154
2155 __error:
2156 module_put(pcm->card->module);
2157 __error2:
2158 snd_card_file_remove(pcm->card, file);
2159 __error1:
2160 return err;
2161}
2162
2163static int snd_pcm_release(struct inode *inode, struct file *file)
2164{
Takashi Iwai877211f2005-11-17 13:59:38 +01002165 struct snd_pcm *pcm;
2166 struct snd_pcm_substream *substream;
2167 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 pcm_file = file->private_data;
2170 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002171 if (snd_BUG_ON(!substream))
2172 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002174 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002175 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002176 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002177 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 wake_up(&pcm->open_wait);
2179 module_put(pcm->card->module);
2180 snd_card_file_remove(pcm->card, file);
2181 return 0;
2182}
2183
Takashi Iwai877211f2005-11-17 13:59:38 +01002184static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2185 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Takashi Iwai877211f2005-11-17 13:59:38 +01002187 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 snd_pcm_sframes_t appl_ptr;
2189 snd_pcm_sframes_t ret;
2190 snd_pcm_sframes_t hw_avail;
2191
2192 if (frames == 0)
2193 return 0;
2194
2195 snd_pcm_stream_lock_irq(substream);
2196 switch (runtime->status->state) {
2197 case SNDRV_PCM_STATE_PREPARED:
2198 break;
2199 case SNDRV_PCM_STATE_DRAINING:
2200 case SNDRV_PCM_STATE_RUNNING:
2201 if (snd_pcm_update_hw_ptr(substream) >= 0)
2202 break;
2203 /* Fall through */
2204 case SNDRV_PCM_STATE_XRUN:
2205 ret = -EPIPE;
2206 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002207 case SNDRV_PCM_STATE_SUSPENDED:
2208 ret = -ESTRPIPE;
2209 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 default:
2211 ret = -EBADFD;
2212 goto __end;
2213 }
2214
2215 hw_avail = snd_pcm_playback_hw_avail(runtime);
2216 if (hw_avail <= 0) {
2217 ret = 0;
2218 goto __end;
2219 }
2220 if (frames > (snd_pcm_uframes_t)hw_avail)
2221 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 appl_ptr = runtime->control->appl_ptr - frames;
2223 if (appl_ptr < 0)
2224 appl_ptr += runtime->boundary;
2225 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 ret = frames;
2227 __end:
2228 snd_pcm_stream_unlock_irq(substream);
2229 return ret;
2230}
2231
Takashi Iwai877211f2005-11-17 13:59:38 +01002232static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2233 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Takashi Iwai877211f2005-11-17 13:59:38 +01002235 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 snd_pcm_sframes_t appl_ptr;
2237 snd_pcm_sframes_t ret;
2238 snd_pcm_sframes_t hw_avail;
2239
2240 if (frames == 0)
2241 return 0;
2242
2243 snd_pcm_stream_lock_irq(substream);
2244 switch (runtime->status->state) {
2245 case SNDRV_PCM_STATE_PREPARED:
2246 case SNDRV_PCM_STATE_DRAINING:
2247 break;
2248 case SNDRV_PCM_STATE_RUNNING:
2249 if (snd_pcm_update_hw_ptr(substream) >= 0)
2250 break;
2251 /* Fall through */
2252 case SNDRV_PCM_STATE_XRUN:
2253 ret = -EPIPE;
2254 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002255 case SNDRV_PCM_STATE_SUSPENDED:
2256 ret = -ESTRPIPE;
2257 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 default:
2259 ret = -EBADFD;
2260 goto __end;
2261 }
2262
2263 hw_avail = snd_pcm_capture_hw_avail(runtime);
2264 if (hw_avail <= 0) {
2265 ret = 0;
2266 goto __end;
2267 }
2268 if (frames > (snd_pcm_uframes_t)hw_avail)
2269 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 appl_ptr = runtime->control->appl_ptr - frames;
2271 if (appl_ptr < 0)
2272 appl_ptr += runtime->boundary;
2273 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 ret = frames;
2275 __end:
2276 snd_pcm_stream_unlock_irq(substream);
2277 return ret;
2278}
2279
Takashi Iwai877211f2005-11-17 13:59:38 +01002280static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2281 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Takashi Iwai877211f2005-11-17 13:59:38 +01002283 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 snd_pcm_sframes_t appl_ptr;
2285 snd_pcm_sframes_t ret;
2286 snd_pcm_sframes_t avail;
2287
2288 if (frames == 0)
2289 return 0;
2290
2291 snd_pcm_stream_lock_irq(substream);
2292 switch (runtime->status->state) {
2293 case SNDRV_PCM_STATE_PREPARED:
2294 case SNDRV_PCM_STATE_PAUSED:
2295 break;
2296 case SNDRV_PCM_STATE_DRAINING:
2297 case SNDRV_PCM_STATE_RUNNING:
2298 if (snd_pcm_update_hw_ptr(substream) >= 0)
2299 break;
2300 /* Fall through */
2301 case SNDRV_PCM_STATE_XRUN:
2302 ret = -EPIPE;
2303 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002304 case SNDRV_PCM_STATE_SUSPENDED:
2305 ret = -ESTRPIPE;
2306 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 default:
2308 ret = -EBADFD;
2309 goto __end;
2310 }
2311
2312 avail = snd_pcm_playback_avail(runtime);
2313 if (avail <= 0) {
2314 ret = 0;
2315 goto __end;
2316 }
2317 if (frames > (snd_pcm_uframes_t)avail)
2318 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 appl_ptr = runtime->control->appl_ptr + frames;
2320 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2321 appl_ptr -= runtime->boundary;
2322 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 ret = frames;
2324 __end:
2325 snd_pcm_stream_unlock_irq(substream);
2326 return ret;
2327}
2328
Takashi Iwai877211f2005-11-17 13:59:38 +01002329static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2330 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331{
Takashi Iwai877211f2005-11-17 13:59:38 +01002332 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 snd_pcm_sframes_t appl_ptr;
2334 snd_pcm_sframes_t ret;
2335 snd_pcm_sframes_t avail;
2336
2337 if (frames == 0)
2338 return 0;
2339
2340 snd_pcm_stream_lock_irq(substream);
2341 switch (runtime->status->state) {
2342 case SNDRV_PCM_STATE_PREPARED:
2343 case SNDRV_PCM_STATE_DRAINING:
2344 case SNDRV_PCM_STATE_PAUSED:
2345 break;
2346 case SNDRV_PCM_STATE_RUNNING:
2347 if (snd_pcm_update_hw_ptr(substream) >= 0)
2348 break;
2349 /* Fall through */
2350 case SNDRV_PCM_STATE_XRUN:
2351 ret = -EPIPE;
2352 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002353 case SNDRV_PCM_STATE_SUSPENDED:
2354 ret = -ESTRPIPE;
2355 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 default:
2357 ret = -EBADFD;
2358 goto __end;
2359 }
2360
2361 avail = snd_pcm_capture_avail(runtime);
2362 if (avail <= 0) {
2363 ret = 0;
2364 goto __end;
2365 }
2366 if (frames > (snd_pcm_uframes_t)avail)
2367 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 appl_ptr = runtime->control->appl_ptr + frames;
2369 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2370 appl_ptr -= runtime->boundary;
2371 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 ret = frames;
2373 __end:
2374 snd_pcm_stream_unlock_irq(substream);
2375 return ret;
2376}
2377
Takashi Iwai877211f2005-11-17 13:59:38 +01002378static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Takashi Iwai877211f2005-11-17 13:59:38 +01002380 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 int err;
2382
2383 snd_pcm_stream_lock_irq(substream);
2384 switch (runtime->status->state) {
2385 case SNDRV_PCM_STATE_DRAINING:
2386 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2387 goto __badfd;
2388 case SNDRV_PCM_STATE_RUNNING:
2389 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2390 break;
2391 /* Fall through */
2392 case SNDRV_PCM_STATE_PREPARED:
2393 case SNDRV_PCM_STATE_SUSPENDED:
2394 err = 0;
2395 break;
2396 case SNDRV_PCM_STATE_XRUN:
2397 err = -EPIPE;
2398 break;
2399 default:
2400 __badfd:
2401 err = -EBADFD;
2402 break;
2403 }
2404 snd_pcm_stream_unlock_irq(substream);
2405 return err;
2406}
2407
Takashi Iwai877211f2005-11-17 13:59:38 +01002408static int snd_pcm_delay(struct snd_pcm_substream *substream,
2409 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Takashi Iwai877211f2005-11-17 13:59:38 +01002411 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
2413 snd_pcm_sframes_t n = 0;
2414
2415 snd_pcm_stream_lock_irq(substream);
2416 switch (runtime->status->state) {
2417 case SNDRV_PCM_STATE_DRAINING:
2418 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2419 goto __badfd;
2420 case SNDRV_PCM_STATE_RUNNING:
2421 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2422 break;
2423 /* Fall through */
2424 case SNDRV_PCM_STATE_PREPARED:
2425 case SNDRV_PCM_STATE_SUSPENDED:
2426 err = 0;
2427 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2428 n = snd_pcm_playback_hw_avail(runtime);
2429 else
2430 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002431 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 break;
2433 case SNDRV_PCM_STATE_XRUN:
2434 err = -EPIPE;
2435 break;
2436 default:
2437 __badfd:
2438 err = -EBADFD;
2439 break;
2440 }
2441 snd_pcm_stream_unlock_irq(substream);
2442 if (!err)
2443 if (put_user(n, res))
2444 err = -EFAULT;
2445 return err;
2446}
2447
Takashi Iwai877211f2005-11-17 13:59:38 +01002448static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2449 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Takashi Iwai877211f2005-11-17 13:59:38 +01002451 struct snd_pcm_runtime *runtime = substream->runtime;
2452 struct snd_pcm_sync_ptr sync_ptr;
2453 volatile struct snd_pcm_mmap_status *status;
2454 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 int err;
2456
2457 memset(&sync_ptr, 0, sizeof(sync_ptr));
2458 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2459 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002460 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 -07002461 return -EFAULT;
2462 status = runtime->status;
2463 control = runtime->control;
2464 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2465 err = snd_pcm_hwsync(substream);
2466 if (err < 0)
2467 return err;
2468 }
2469 snd_pcm_stream_lock_irq(substream);
2470 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2471 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2472 else
2473 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2474 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2475 control->avail_min = sync_ptr.c.control.avail_min;
2476 else
2477 sync_ptr.c.control.avail_min = control->avail_min;
2478 sync_ptr.s.status.state = status->state;
2479 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2480 sync_ptr.s.status.tstamp = status->tstamp;
2481 sync_ptr.s.status.suspended_state = status->suspended_state;
2482 snd_pcm_stream_unlock_irq(substream);
2483 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2484 return -EFAULT;
2485 return 0;
2486}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002487
2488static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2489{
2490 struct snd_pcm_runtime *runtime = substream->runtime;
2491 int arg;
2492
2493 if (get_user(arg, _arg))
2494 return -EFAULT;
2495 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2496 return -EINVAL;
2497 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2498 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2499 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2500 return 0;
2501}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Takashi Iwai0df63e42006-04-28 15:13:41 +02002503static int snd_pcm_common_ioctl1(struct file *file,
2504 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 unsigned int cmd, void __user *arg)
2506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 switch (cmd) {
2508 case SNDRV_PCM_IOCTL_PVERSION:
2509 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2510 case SNDRV_PCM_IOCTL_INFO:
2511 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002512 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2513 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002514 case SNDRV_PCM_IOCTL_TTSTAMP:
2515 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 case SNDRV_PCM_IOCTL_HW_REFINE:
2517 return snd_pcm_hw_refine_user(substream, arg);
2518 case SNDRV_PCM_IOCTL_HW_PARAMS:
2519 return snd_pcm_hw_params_user(substream, arg);
2520 case SNDRV_PCM_IOCTL_HW_FREE:
2521 return snd_pcm_hw_free(substream);
2522 case SNDRV_PCM_IOCTL_SW_PARAMS:
2523 return snd_pcm_sw_params_user(substream, arg);
2524 case SNDRV_PCM_IOCTL_STATUS:
2525 return snd_pcm_status_user(substream, arg);
2526 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2527 return snd_pcm_channel_info_user(substream, arg);
2528 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002529 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 case SNDRV_PCM_IOCTL_RESET:
2531 return snd_pcm_reset(substream);
2532 case SNDRV_PCM_IOCTL_START:
2533 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2534 case SNDRV_PCM_IOCTL_LINK:
2535 return snd_pcm_link(substream, (int)(unsigned long) arg);
2536 case SNDRV_PCM_IOCTL_UNLINK:
2537 return snd_pcm_unlink(substream);
2538 case SNDRV_PCM_IOCTL_RESUME:
2539 return snd_pcm_resume(substream);
2540 case SNDRV_PCM_IOCTL_XRUN:
2541 return snd_pcm_xrun(substream);
2542 case SNDRV_PCM_IOCTL_HWSYNC:
2543 return snd_pcm_hwsync(substream);
2544 case SNDRV_PCM_IOCTL_DELAY:
2545 return snd_pcm_delay(substream, arg);
2546 case SNDRV_PCM_IOCTL_SYNC_PTR:
2547 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002548#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2550 return snd_pcm_hw_refine_old_user(substream, arg);
2551 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2552 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002553#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002555 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 case SNDRV_PCM_IOCTL_DROP:
2557 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002558 case SNDRV_PCM_IOCTL_PAUSE:
2559 {
2560 int res;
2561 snd_pcm_stream_lock_irq(substream);
2562 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2563 snd_pcm_stream_unlock_irq(substream);
2564 return res;
2565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 snd_printd("unknown ioctl = 0x%x\n", cmd);
2568 return -ENOTTY;
2569}
2570
Takashi Iwai0df63e42006-04-28 15:13:41 +02002571static int snd_pcm_playback_ioctl1(struct file *file,
2572 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 unsigned int cmd, void __user *arg)
2574{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002575 if (snd_BUG_ON(!substream))
2576 return -ENXIO;
2577 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 switch (cmd) {
2580 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2581 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002582 struct snd_xferi xferi;
2583 struct snd_xferi __user *_xferi = arg;
2584 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 snd_pcm_sframes_t result;
2586 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2587 return -EBADFD;
2588 if (put_user(0, &_xferi->result))
2589 return -EFAULT;
2590 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2591 return -EFAULT;
2592 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2593 __put_user(result, &_xferi->result);
2594 return result < 0 ? result : 0;
2595 }
2596 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2597 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002598 struct snd_xfern xfern;
2599 struct snd_xfern __user *_xfern = arg;
2600 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 void __user **bufs;
2602 snd_pcm_sframes_t result;
2603 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2604 return -EBADFD;
2605 if (runtime->channels > 128)
2606 return -EINVAL;
2607 if (put_user(0, &_xfern->result))
2608 return -EFAULT;
2609 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2610 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002611
2612 bufs = memdup_user(xfern.bufs,
2613 sizeof(void *) * runtime->channels);
2614 if (IS_ERR(bufs))
2615 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2617 kfree(bufs);
2618 __put_user(result, &_xfern->result);
2619 return result < 0 ? result : 0;
2620 }
2621 case SNDRV_PCM_IOCTL_REWIND:
2622 {
2623 snd_pcm_uframes_t frames;
2624 snd_pcm_uframes_t __user *_frames = arg;
2625 snd_pcm_sframes_t result;
2626 if (get_user(frames, _frames))
2627 return -EFAULT;
2628 if (put_user(0, _frames))
2629 return -EFAULT;
2630 result = snd_pcm_playback_rewind(substream, frames);
2631 __put_user(result, _frames);
2632 return result < 0 ? result : 0;
2633 }
2634 case SNDRV_PCM_IOCTL_FORWARD:
2635 {
2636 snd_pcm_uframes_t frames;
2637 snd_pcm_uframes_t __user *_frames = arg;
2638 snd_pcm_sframes_t result;
2639 if (get_user(frames, _frames))
2640 return -EFAULT;
2641 if (put_user(0, _frames))
2642 return -EFAULT;
2643 result = snd_pcm_playback_forward(substream, frames);
2644 __put_user(result, _frames);
2645 return result < 0 ? result : 0;
2646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002648 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}
2650
Takashi Iwai0df63e42006-04-28 15:13:41 +02002651static int snd_pcm_capture_ioctl1(struct file *file,
2652 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 unsigned int cmd, void __user *arg)
2654{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002655 if (snd_BUG_ON(!substream))
2656 return -ENXIO;
2657 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 switch (cmd) {
2660 case SNDRV_PCM_IOCTL_READI_FRAMES:
2661 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002662 struct snd_xferi xferi;
2663 struct snd_xferi __user *_xferi = arg;
2664 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 snd_pcm_sframes_t result;
2666 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2667 return -EBADFD;
2668 if (put_user(0, &_xferi->result))
2669 return -EFAULT;
2670 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2671 return -EFAULT;
2672 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2673 __put_user(result, &_xferi->result);
2674 return result < 0 ? result : 0;
2675 }
2676 case SNDRV_PCM_IOCTL_READN_FRAMES:
2677 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002678 struct snd_xfern xfern;
2679 struct snd_xfern __user *_xfern = arg;
2680 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 void *bufs;
2682 snd_pcm_sframes_t result;
2683 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2684 return -EBADFD;
2685 if (runtime->channels > 128)
2686 return -EINVAL;
2687 if (put_user(0, &_xfern->result))
2688 return -EFAULT;
2689 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2690 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002691
2692 bufs = memdup_user(xfern.bufs,
2693 sizeof(void *) * runtime->channels);
2694 if (IS_ERR(bufs))
2695 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2697 kfree(bufs);
2698 __put_user(result, &_xfern->result);
2699 return result < 0 ? result : 0;
2700 }
2701 case SNDRV_PCM_IOCTL_REWIND:
2702 {
2703 snd_pcm_uframes_t frames;
2704 snd_pcm_uframes_t __user *_frames = arg;
2705 snd_pcm_sframes_t result;
2706 if (get_user(frames, _frames))
2707 return -EFAULT;
2708 if (put_user(0, _frames))
2709 return -EFAULT;
2710 result = snd_pcm_capture_rewind(substream, frames);
2711 __put_user(result, _frames);
2712 return result < 0 ? result : 0;
2713 }
2714 case SNDRV_PCM_IOCTL_FORWARD:
2715 {
2716 snd_pcm_uframes_t frames;
2717 snd_pcm_uframes_t __user *_frames = arg;
2718 snd_pcm_sframes_t result;
2719 if (get_user(frames, _frames))
2720 return -EFAULT;
2721 if (put_user(0, _frames))
2722 return -EFAULT;
2723 result = snd_pcm_capture_forward(substream, frames);
2724 __put_user(result, _frames);
2725 return result < 0 ? result : 0;
2726 }
2727 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002728 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Takashi Iwai877211f2005-11-17 13:59:38 +01002731static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2732 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Takashi Iwai877211f2005-11-17 13:59:38 +01002734 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 pcm_file = file->private_data;
2737
2738 if (((cmd >> 8) & 0xff) != 'A')
2739 return -ENOTTY;
2740
Takashi Iwai0df63e42006-04-28 15:13:41 +02002741 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2742 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Takashi Iwai877211f2005-11-17 13:59:38 +01002745static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2746 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Takashi Iwai877211f2005-11-17 13:59:38 +01002748 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 pcm_file = file->private_data;
2751
2752 if (((cmd >> 8) & 0xff) != 'A')
2753 return -ENOTTY;
2754
Takashi Iwai0df63e42006-04-28 15:13:41 +02002755 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2756 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Takashi Iwai877211f2005-11-17 13:59:38 +01002759int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 unsigned int cmd, void *arg)
2761{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002762 mm_segment_t fs;
2763 int result;
2764
2765 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 switch (substream->stream) {
2767 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002768 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2769 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002772 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2773 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002776 result = -EINVAL;
2777 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002779 snd_leave_user(fs);
2780 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002783EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2784
Takashi Iwai877211f2005-11-17 13:59:38 +01002785static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2786 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Takashi Iwai877211f2005-11-17 13:59:38 +01002788 struct snd_pcm_file *pcm_file;
2789 struct snd_pcm_substream *substream;
2790 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 snd_pcm_sframes_t result;
2792
2793 pcm_file = file->private_data;
2794 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002795 if (PCM_RUNTIME_CHECK(substream))
2796 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 runtime = substream->runtime;
2798 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2799 return -EBADFD;
2800 if (!frame_aligned(runtime, count))
2801 return -EINVAL;
2802 count = bytes_to_frames(runtime, count);
2803 result = snd_pcm_lib_read(substream, buf, count);
2804 if (result > 0)
2805 result = frames_to_bytes(runtime, result);
2806 return result;
2807}
2808
Takashi Iwai877211f2005-11-17 13:59:38 +01002809static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2810 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Takashi Iwai877211f2005-11-17 13:59:38 +01002812 struct snd_pcm_file *pcm_file;
2813 struct snd_pcm_substream *substream;
2814 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 snd_pcm_sframes_t result;
2816
2817 pcm_file = file->private_data;
2818 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002819 if (PCM_RUNTIME_CHECK(substream))
2820 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002822 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2823 return -EBADFD;
2824 if (!frame_aligned(runtime, count))
2825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 count = bytes_to_frames(runtime, count);
2827 result = snd_pcm_lib_write(substream, buf, count);
2828 if (result > 0)
2829 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return result;
2831}
2832
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002833static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2834 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836{
Takashi Iwai877211f2005-11-17 13:59:38 +01002837 struct snd_pcm_file *pcm_file;
2838 struct snd_pcm_substream *substream;
2839 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 snd_pcm_sframes_t result;
2841 unsigned long i;
2842 void __user **bufs;
2843 snd_pcm_uframes_t frames;
2844
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002845 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002847 if (PCM_RUNTIME_CHECK(substream))
2848 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 runtime = substream->runtime;
2850 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2851 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002852 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002854 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002856 frames = bytes_to_samples(runtime, iov->iov_len);
2857 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (bufs == NULL)
2859 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002860 for (i = 0; i < nr_segs; ++i)
2861 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 result = snd_pcm_lib_readv(substream, bufs, frames);
2863 if (result > 0)
2864 result = frames_to_bytes(runtime, result);
2865 kfree(bufs);
2866 return result;
2867}
2868
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002869static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2870 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Takashi Iwai877211f2005-11-17 13:59:38 +01002872 struct snd_pcm_file *pcm_file;
2873 struct snd_pcm_substream *substream;
2874 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 snd_pcm_sframes_t result;
2876 unsigned long i;
2877 void __user **bufs;
2878 snd_pcm_uframes_t frames;
2879
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002880 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002882 if (PCM_RUNTIME_CHECK(substream))
2883 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002885 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2886 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002888 !frame_aligned(runtime, iov->iov_len))
2889 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002890 frames = bytes_to_samples(runtime, iov->iov_len);
2891 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (bufs == NULL)
2893 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002894 for (i = 0; i < nr_segs; ++i)
2895 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 result = snd_pcm_lib_writev(substream, bufs, frames);
2897 if (result > 0)
2898 result = frames_to_bytes(runtime, result);
2899 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return result;
2901}
2902
2903static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2904{
Takashi Iwai877211f2005-11-17 13:59:38 +01002905 struct snd_pcm_file *pcm_file;
2906 struct snd_pcm_substream *substream;
2907 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 unsigned int mask;
2909 snd_pcm_uframes_t avail;
2910
2911 pcm_file = file->private_data;
2912
2913 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002914 if (PCM_RUNTIME_CHECK(substream))
2915 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 runtime = substream->runtime;
2917
2918 poll_wait(file, &runtime->sleep, wait);
2919
2920 snd_pcm_stream_lock_irq(substream);
2921 avail = snd_pcm_playback_avail(runtime);
2922 switch (runtime->status->state) {
2923 case SNDRV_PCM_STATE_RUNNING:
2924 case SNDRV_PCM_STATE_PREPARED:
2925 case SNDRV_PCM_STATE_PAUSED:
2926 if (avail >= runtime->control->avail_min) {
2927 mask = POLLOUT | POLLWRNORM;
2928 break;
2929 }
2930 /* Fall through */
2931 case SNDRV_PCM_STATE_DRAINING:
2932 mask = 0;
2933 break;
2934 default:
2935 mask = POLLOUT | POLLWRNORM | POLLERR;
2936 break;
2937 }
2938 snd_pcm_stream_unlock_irq(substream);
2939 return mask;
2940}
2941
2942static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2943{
Takashi Iwai877211f2005-11-17 13:59:38 +01002944 struct snd_pcm_file *pcm_file;
2945 struct snd_pcm_substream *substream;
2946 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 unsigned int mask;
2948 snd_pcm_uframes_t avail;
2949
2950 pcm_file = file->private_data;
2951
2952 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002953 if (PCM_RUNTIME_CHECK(substream))
2954 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 runtime = substream->runtime;
2956
2957 poll_wait(file, &runtime->sleep, wait);
2958
2959 snd_pcm_stream_lock_irq(substream);
2960 avail = snd_pcm_capture_avail(runtime);
2961 switch (runtime->status->state) {
2962 case SNDRV_PCM_STATE_RUNNING:
2963 case SNDRV_PCM_STATE_PREPARED:
2964 case SNDRV_PCM_STATE_PAUSED:
2965 if (avail >= runtime->control->avail_min) {
2966 mask = POLLIN | POLLRDNORM;
2967 break;
2968 }
2969 mask = 0;
2970 break;
2971 case SNDRV_PCM_STATE_DRAINING:
2972 if (avail > 0) {
2973 mask = POLLIN | POLLRDNORM;
2974 break;
2975 }
2976 /* Fall through */
2977 default:
2978 mask = POLLIN | POLLRDNORM | POLLERR;
2979 break;
2980 }
2981 snd_pcm_stream_unlock_irq(substream);
2982 return mask;
2983}
2984
2985/*
2986 * mmap support
2987 */
2988
2989/*
2990 * Only on coherent architectures, we can mmap the status and the control records
2991 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2992 */
2993#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2994/*
2995 * mmap status record
2996 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002997static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2998 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Takashi Iwai877211f2005-11-17 13:59:38 +01003000 struct snd_pcm_substream *substream = area->vm_private_data;
3001 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003004 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003006 vmf->page = virt_to_page(runtime->status);
3007 get_page(vmf->page);
3008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003011static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003013 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014};
3015
Takashi Iwai877211f2005-11-17 13:59:38 +01003016static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 struct vm_area_struct *area)
3018{
Takashi Iwai877211f2005-11-17 13:59:38 +01003019 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 long size;
3021 if (!(area->vm_flags & VM_READ))
3022 return -EINVAL;
3023 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003025 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return -EINVAL;
3027 area->vm_ops = &snd_pcm_vm_ops_status;
3028 area->vm_private_data = substream;
3029 area->vm_flags |= VM_RESERVED;
3030 return 0;
3031}
3032
3033/*
3034 * mmap control record
3035 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003036static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3037 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038{
Takashi Iwai877211f2005-11-17 13:59:38 +01003039 struct snd_pcm_substream *substream = area->vm_private_data;
3040 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
3042 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003043 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003045 vmf->page = virt_to_page(runtime->control);
3046 get_page(vmf->page);
3047 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048}
3049
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003050static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003052 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053};
3054
Takashi Iwai877211f2005-11-17 13:59:38 +01003055static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 struct vm_area_struct *area)
3057{
Takashi Iwai877211f2005-11-17 13:59:38 +01003058 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 long size;
3060 if (!(area->vm_flags & VM_READ))
3061 return -EINVAL;
3062 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003064 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return -EINVAL;
3066 area->vm_ops = &snd_pcm_vm_ops_control;
3067 area->vm_private_data = substream;
3068 area->vm_flags |= VM_RESERVED;
3069 return 0;
3070}
3071#else /* ! coherent mmap */
3072/*
3073 * don't support mmap for status and control records.
3074 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003075static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct vm_area_struct *area)
3077{
3078 return -ENXIO;
3079}
Takashi Iwai877211f2005-11-17 13:59:38 +01003080static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct vm_area_struct *area)
3082{
3083 return -ENXIO;
3084}
3085#endif /* coherent mmap */
3086
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003087static inline struct page *
3088snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3089{
3090 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003091#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3092 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3093 return virt_to_page(CAC_ADDR(vaddr));
3094#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003095#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3096 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3097 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3098 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3099 /* assume dma_handle set via pfn_to_phys() in
3100 * mm/dma-noncoherent.c
3101 */
3102 return pfn_to_page(addr >> PAGE_SHIFT);
3103 }
3104#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003105 return virt_to_page(vaddr);
3106}
3107
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003109 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003111static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3112 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Takashi Iwai877211f2005-11-17 13:59:38 +01003114 struct snd_pcm_substream *substream = area->vm_private_data;
3115 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 unsigned long offset;
3117 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 size_t dma_bytes;
3119
3120 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003121 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003123 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3125 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003126 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003127 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003129 else
3130 page = snd_pcm_default_page_ops(substream, offset);
3131 if (!page)
3132 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003133 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003134 vmf->page = page;
3135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Takashi Iwai657b1982009-11-26 12:40:21 +01003138static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3139 .open = snd_pcm_mmap_data_open,
3140 .close = snd_pcm_mmap_data_close,
3141};
3142
3143static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 .open = snd_pcm_mmap_data_open,
3145 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003146 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147};
3148
Takashi Iwai657b1982009-11-26 12:40:21 +01003149#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3150/* This should be defined / handled globally! */
3151#ifdef CONFIG_ARM
3152#define ARCH_HAS_DMA_MMAP_COHERENT
3153#endif
3154#endif
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156/*
3157 * mmap the DMA buffer on RAM
3158 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003159static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3160 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003163#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3164 if (!substream->ops->page &&
3165 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3166 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3167 area,
3168 substream->runtime->dma_area,
3169 substream->runtime->dma_addr,
3170 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003171#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3172 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3173 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3174 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003175#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3176 /* mmap with fault handler */
3177 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return 0;
3179}
3180
3181/*
3182 * mmap the DMA buffer on I/O memory area
3183 */
3184#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003185int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3186 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 long size;
3189 unsigned long offset;
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 area->vm_flags |= VM_IO;
3193 size = area->vm_end - area->vm_start;
3194 offset = area->vm_pgoff << PAGE_SHIFT;
3195 if (io_remap_pfn_range(area, area->vm_start,
3196 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3197 size, area->vm_page_prot))
3198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 return 0;
3200}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003201
3202EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203#endif /* SNDRV_PCM_INFO_MMAP */
3204
Takashi Iwaic32d9772010-01-18 14:58:57 +01003205/* mmap callback with pgprot_noncached */
3206int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3207 struct vm_area_struct *area)
3208{
3209 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3210 return snd_pcm_default_mmap(substream, area);
3211}
3212EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214/*
3215 * mmap DMA buffer
3216 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003217int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct vm_area_struct *area)
3219{
Takashi Iwai877211f2005-11-17 13:59:38 +01003220 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 long size;
3222 unsigned long offset;
3223 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003224 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3227 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3228 return -EINVAL;
3229 } else {
3230 if (!(area->vm_flags & VM_READ))
3231 return -EINVAL;
3232 }
3233 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3235 return -EBADFD;
3236 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3237 return -ENXIO;
3238 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3239 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3240 return -EINVAL;
3241 size = area->vm_end - area->vm_start;
3242 offset = area->vm_pgoff << PAGE_SHIFT;
3243 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3244 if ((size_t)size > dma_bytes)
3245 return -EINVAL;
3246 if (offset > dma_bytes - size)
3247 return -EINVAL;
3248
Takashi Iwai657b1982009-11-26 12:40:21 +01003249 area->vm_ops = &snd_pcm_vm_ops_data;
3250 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003252 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003254 err = snd_pcm_default_mmap(substream, area);
3255 if (!err)
3256 atomic_inc(&substream->mmap_count);
3257 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003260EXPORT_SYMBOL(snd_pcm_mmap_data);
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3263{
Takashi Iwai877211f2005-11-17 13:59:38 +01003264 struct snd_pcm_file * pcm_file;
3265 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 unsigned long offset;
3267
3268 pcm_file = file->private_data;
3269 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003270 if (PCM_RUNTIME_CHECK(substream))
3271 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 offset = area->vm_pgoff << PAGE_SHIFT;
3274 switch (offset) {
3275 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003276 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return -ENXIO;
3278 return snd_pcm_mmap_status(substream, file, area);
3279 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003280 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return -ENXIO;
3282 return snd_pcm_mmap_control(substream, file, area);
3283 default:
3284 return snd_pcm_mmap_data(substream, file, area);
3285 }
3286 return 0;
3287}
3288
3289static int snd_pcm_fasync(int fd, struct file * file, int on)
3290{
Takashi Iwai877211f2005-11-17 13:59:38 +01003291 struct snd_pcm_file * pcm_file;
3292 struct snd_pcm_substream *substream;
3293 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295 pcm_file = file->private_data;
3296 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003297 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003298 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003300 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
3303/*
3304 * ioctl32 compat
3305 */
3306#ifdef CONFIG_COMPAT
3307#include "pcm_compat.c"
3308#else
3309#define snd_pcm_ioctl_compat NULL
3310#endif
3311
3312/*
3313 * To be removed helpers to keep binary compatibility
3314 */
3315
Takashi Iwai59d48582005-12-01 10:51:58 +01003316#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3318#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3319
Takashi Iwai877211f2005-11-17 13:59:38 +01003320static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3321 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
3323 unsigned int i;
3324
3325 memset(params, 0, sizeof(*params));
3326 params->flags = oparams->flags;
3327 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3328 params->masks[i].bits[0] = oparams->masks[i];
3329 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3330 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3331 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3332 params->info = oparams->info;
3333 params->msbits = oparams->msbits;
3334 params->rate_num = oparams->rate_num;
3335 params->rate_den = oparams->rate_den;
3336 params->fifo_size = oparams->fifo_size;
3337}
3338
Takashi Iwai877211f2005-11-17 13:59:38 +01003339static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3340 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
3342 unsigned int i;
3343
3344 memset(oparams, 0, sizeof(*oparams));
3345 oparams->flags = params->flags;
3346 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3347 oparams->masks[i] = params->masks[i].bits[0];
3348 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3349 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3350 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3351 oparams->info = params->info;
3352 oparams->msbits = params->msbits;
3353 oparams->rate_num = params->rate_num;
3354 oparams->rate_den = params->rate_den;
3355 oparams->fifo_size = params->fifo_size;
3356}
3357
Takashi Iwai877211f2005-11-17 13:59:38 +01003358static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3359 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
Takashi Iwai877211f2005-11-17 13:59:38 +01003361 struct snd_pcm_hw_params *params;
3362 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 int err;
3364
3365 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003366 if (!params)
3367 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Li Zefanef44a1e2009-04-10 09:43:08 +08003369 oparams = memdup_user(_oparams, sizeof(*oparams));
3370 if (IS_ERR(oparams)) {
3371 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto out;
3373 }
3374 snd_pcm_hw_convert_from_old_params(params, oparams);
3375 err = snd_pcm_hw_refine(substream, params);
3376 snd_pcm_hw_convert_to_old_params(oparams, params);
3377 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3378 if (!err)
3379 err = -EFAULT;
3380 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003381
3382 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383out:
3384 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 return err;
3386}
3387
Takashi Iwai877211f2005-11-17 13:59:38 +01003388static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3389 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Takashi Iwai877211f2005-11-17 13:59:38 +01003391 struct snd_pcm_hw_params *params;
3392 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 int err;
3394
3395 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003396 if (!params)
3397 return -ENOMEM;
3398
3399 oparams = memdup_user(_oparams, sizeof(*oparams));
3400 if (IS_ERR(oparams)) {
3401 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 goto out;
3403 }
3404 snd_pcm_hw_convert_from_old_params(params, oparams);
3405 err = snd_pcm_hw_params(substream, params);
3406 snd_pcm_hw_convert_to_old_params(oparams, params);
3407 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3408 if (!err)
3409 err = -EFAULT;
3410 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003411
3412 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413out:
3414 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return err;
3416}
Takashi Iwai59d48582005-12-01 10:51:58 +01003417#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Cliff Cai70036092008-09-03 10:54:36 +02003419#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003420static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3421 unsigned long addr,
3422 unsigned long len,
3423 unsigned long pgoff,
3424 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003425{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003426 struct snd_pcm_file *pcm_file = file->private_data;
3427 struct snd_pcm_substream *substream = pcm_file->substream;
3428 struct snd_pcm_runtime *runtime = substream->runtime;
3429 unsigned long offset = pgoff << PAGE_SHIFT;
3430
3431 switch (offset) {
3432 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3433 return (unsigned long)runtime->status;
3434 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3435 return (unsigned long)runtime->control;
3436 default:
3437 return (unsigned long)runtime->dma_area + offset;
3438 }
Cliff Cai70036092008-09-03 10:54:36 +02003439}
3440#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003441# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003442#endif
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444/*
3445 * Register section
3446 */
3447
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003448const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003450 .owner = THIS_MODULE,
3451 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003452 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003453 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003454 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003455 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003456 .poll = snd_pcm_playback_poll,
3457 .unlocked_ioctl = snd_pcm_playback_ioctl,
3458 .compat_ioctl = snd_pcm_ioctl_compat,
3459 .mmap = snd_pcm_mmap,
3460 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003461 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 },
3463 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003464 .owner = THIS_MODULE,
3465 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003466 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003467 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003468 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003469 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003470 .poll = snd_pcm_capture_poll,
3471 .unlocked_ioctl = snd_pcm_capture_ioctl,
3472 .compat_ioctl = snd_pcm_ioctl_compat,
3473 .mmap = snd_pcm_mmap,
3474 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003475 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
3477};