blob: b11118fae5ac2d94cef9d962b9d0280f375fca2d [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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07008 * the Free Software Foundation; only version 2 of the License.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mm.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
25#include <linux/time.h>
Jean Pihete8db0be2011-08-25 15:35:03 +020026#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010028#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/core.h>
30#include <sound/control.h>
Asish Bhattacharya9048f752011-11-01 20:33:44 +053031#include <sound/compress_offload.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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:
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300464 /* hardware might be unusable from this time,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 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 &&
Liam Girdwoodad2581e2011-02-01 21:31:31 +0000846 !substream->hw_no_buffer &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 !snd_pcm_playback_data(substream))
848 return -EPIPE;
849 runtime->trigger_master = substream;
850 return 0;
851}
852
Takashi Iwai877211f2005-11-17 13:59:38 +0100853static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 if (substream->runtime->trigger_master != substream)
856 return 0;
857 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
858}
859
Takashi Iwai877211f2005-11-17 13:59:38 +0100860static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 if (substream->runtime->trigger_master == substream)
863 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
864}
865
Takashi Iwai877211f2005-11-17 13:59:38 +0100866static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Takashi Iwai877211f2005-11-17 13:59:38 +0100868 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200870 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200871 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
872 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 runtime->status->state = state;
874 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
875 runtime->silence_size > 0)
876 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100878 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
879 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882static struct action_ops snd_pcm_action_start = {
883 .pre_action = snd_pcm_pre_start,
884 .do_action = snd_pcm_do_start,
885 .undo_action = snd_pcm_undo_start,
886 .post_action = snd_pcm_post_start
887};
888
889/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700890 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200891 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100893int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 return snd_pcm_action(&snd_pcm_action_start, substream,
896 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899/*
900 * stop callbacks
901 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100902static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai877211f2005-11-17 13:59:38 +0100904 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
906 return -EBADFD;
907 runtime->trigger_master = substream;
908 return 0;
909}
910
Takashi Iwai877211f2005-11-17 13:59:38 +0100911static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 if (substream->runtime->trigger_master == substream &&
914 snd_pcm_running(substream))
915 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
916 return 0; /* unconditonally stop all substreams */
917}
918
Takashi Iwai877211f2005-11-17 13:59:38 +0100919static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (runtime->status->state != state) {
923 snd_pcm_trigger_tstamp(substream);
924 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
926 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100930 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
933static struct action_ops snd_pcm_action_stop = {
934 .pre_action = snd_pcm_pre_stop,
935 .do_action = snd_pcm_do_stop,
936 .post_action = snd_pcm_post_stop
937};
938
939/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700940 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200941 * @substream: the PCM substream instance
942 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700944 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
Clemens Ladischfea952e2011-02-14 11:00:47 +0100946int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
949}
950
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200951EXPORT_SYMBOL(snd_pcm_stop);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700954 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200955 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700957 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 * Unlike snd_pcm_stop(), this affects only the given stream.
959 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100960int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Takashi Iwai877211f2005-11-17 13:59:38 +0100962 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
963 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
967 * pause callbacks
968 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100969static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Takashi Iwai877211f2005-11-17 13:59:38 +0100971 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
973 return -ENOSYS;
974 if (push) {
975 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
976 return -EBADFD;
977 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
978 return -EBADFD;
979 runtime->trigger_master = substream;
980 return 0;
981}
982
Takashi Iwai877211f2005-11-17 13:59:38 +0100983static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
985 if (substream->runtime->trigger_master != substream)
986 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200987 /* some drivers might use hw_ptr to recover from the pause -
988 update the hw_ptr now */
989 if (push)
990 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200991 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400992 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200993 * delta, effectively to skip the check once.
994 */
995 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return substream->ops->trigger(substream,
997 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
998 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
999}
1000
Takashi Iwai877211f2005-11-17 13:59:38 +01001001static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
1003 if (substream->runtime->trigger_master == substream)
1004 substream->ops->trigger(substream,
1005 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1006 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1007}
1008
Takashi Iwai877211f2005-11-17 13:59:38 +01001009static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Takashi Iwai877211f2005-11-17 13:59:38 +01001011 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 snd_pcm_trigger_tstamp(substream);
1013 if (push) {
1014 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1015 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001016 snd_timer_notify(substream->timer,
1017 SNDRV_TIMER_EVENT_MPAUSE,
1018 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001020 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 } else {
1022 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001024 snd_timer_notify(substream->timer,
1025 SNDRV_TIMER_EVENT_MCONTINUE,
1026 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028}
1029
1030static struct action_ops snd_pcm_action_pause = {
1031 .pre_action = snd_pcm_pre_pause,
1032 .do_action = snd_pcm_do_pause,
1033 .undo_action = snd_pcm_undo_pause,
1034 .post_action = snd_pcm_post_pause
1035};
1036
1037/*
1038 * Push/release the pause for all linked streams.
1039 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001040static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1043}
1044
1045#ifdef CONFIG_PM
1046/* suspend */
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1052 return -EBUSY;
1053 runtime->trigger_master = substream;
1054 return 0;
1055}
1056
Takashi Iwai877211f2005-11-17 13:59:38 +01001057static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (runtime->trigger_master != substream)
1061 return 0;
1062 if (! snd_pcm_running(substream))
1063 return 0;
1064 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1065 return 0; /* suspend unconditionally */
1066}
1067
Takashi Iwai877211f2005-11-17 13:59:38 +01001068static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai877211f2005-11-17 13:59:38 +01001070 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 snd_pcm_trigger_tstamp(substream);
1072 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001073 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1074 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 runtime->status->suspended_state = runtime->status->state;
1076 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001078 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
1081static struct action_ops snd_pcm_action_suspend = {
1082 .pre_action = snd_pcm_pre_suspend,
1083 .do_action = snd_pcm_do_suspend,
1084 .post_action = snd_pcm_post_suspend
1085};
1086
1087/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001088 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001089 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 * After this call, all streams are changed to SUSPENDED state.
1092 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001093int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 int err;
1096 unsigned long flags;
1097
Takashi Iwai603bf522005-11-17 15:59:14 +01001098 if (! substream)
1099 return 0;
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 snd_pcm_stream_lock_irqsave(substream, flags);
1102 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1103 snd_pcm_stream_unlock_irqrestore(substream, flags);
1104 return err;
1105}
1106
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001107EXPORT_SYMBOL(snd_pcm_suspend);
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001110 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001111 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 * After this call, all streams are changed to SUSPENDED state.
1114 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001115int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Takashi Iwai877211f2005-11-17 13:59:38 +01001117 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int stream, err = 0;
1119
Takashi Iwai603bf522005-11-17 15:59:14 +01001120 if (! pcm)
1121 return 0;
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001124 for (substream = pcm->streams[stream].substream;
1125 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /* FIXME: the open/close code should lock this as well */
1127 if (substream->runtime == NULL)
1128 continue;
1129 err = snd_pcm_suspend(substream);
1130 if (err < 0 && err != -EBUSY)
1131 return err;
1132 }
1133 }
1134 return 0;
1135}
1136
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001137EXPORT_SYMBOL(snd_pcm_suspend_all);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/* resume */
1140
Takashi Iwai877211f2005-11-17 13:59:38 +01001141static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Takashi Iwai877211f2005-11-17 13:59:38 +01001143 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1145 return -ENOSYS;
1146 runtime->trigger_master = substream;
1147 return 0;
1148}
1149
Takashi Iwai877211f2005-11-17 13:59:38 +01001150static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Takashi Iwai877211f2005-11-17 13:59:38 +01001152 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (runtime->trigger_master != substream)
1154 return 0;
1155 /* DMA not running previously? */
1156 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1157 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1158 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1159 return 0;
1160 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1161}
1162
Takashi Iwai877211f2005-11-17 13:59:38 +01001163static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 if (substream->runtime->trigger_master == substream &&
1166 snd_pcm_running(substream))
1167 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1168}
1169
Takashi Iwai877211f2005-11-17 13:59:38 +01001170static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Takashi Iwai877211f2005-11-17 13:59:38 +01001172 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 snd_pcm_trigger_tstamp(substream);
1174 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001175 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1176 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
1180static struct action_ops snd_pcm_action_resume = {
1181 .pre_action = snd_pcm_pre_resume,
1182 .do_action = snd_pcm_do_resume,
1183 .undo_action = snd_pcm_undo_resume,
1184 .post_action = snd_pcm_post_resume
1185};
1186
Takashi Iwai877211f2005-11-17 13:59:38 +01001187static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Takashi Iwai877211f2005-11-17 13:59:38 +01001189 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int res;
1191
1192 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001193 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1195 snd_power_unlock(card);
1196 return res;
1197}
1198
1199#else
1200
Takashi Iwai877211f2005-11-17 13:59:38 +01001201static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 return -ENOSYS;
1204}
1205
1206#endif /* CONFIG_PM */
1207
1208/*
1209 * xrun ioctl
1210 *
1211 * Change the RUNNING stream(s) to XRUN state.
1212 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001213static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Takashi Iwai877211f2005-11-17 13:59:38 +01001215 struct snd_card *card = substream->pcm->card;
1216 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int result;
1218
1219 snd_power_lock(card);
1220 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001221 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (result < 0)
1223 goto _unlock;
1224 }
1225
1226 snd_pcm_stream_lock_irq(substream);
1227 switch (runtime->status->state) {
1228 case SNDRV_PCM_STATE_XRUN:
1229 result = 0; /* already there */
1230 break;
1231 case SNDRV_PCM_STATE_RUNNING:
1232 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1233 break;
1234 default:
1235 result = -EBADFD;
1236 }
1237 snd_pcm_stream_unlock_irq(substream);
1238 _unlock:
1239 snd_power_unlock(card);
1240 return result;
1241}
1242
1243/*
1244 * reset ioctl
1245 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001246static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 switch (runtime->status->state) {
1250 case SNDRV_PCM_STATE_RUNNING:
1251 case SNDRV_PCM_STATE_PREPARED:
1252 case SNDRV_PCM_STATE_PAUSED:
1253 case SNDRV_PCM_STATE_SUSPENDED:
1254 return 0;
1255 default:
1256 return -EBADFD;
1257 }
1258}
1259
Takashi Iwai877211f2005-11-17 13:59:38 +01001260static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Takashi Iwai877211f2005-11-17 13:59:38 +01001262 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1264 if (err < 0)
1265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001267 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1268 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 runtime->silence_start = runtime->status->hw_ptr;
1270 runtime->silence_filled = 0;
1271 return 0;
1272}
1273
Takashi Iwai877211f2005-11-17 13:59:38 +01001274static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Takashi Iwai877211f2005-11-17 13:59:38 +01001276 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 runtime->control->appl_ptr = runtime->status->hw_ptr;
1278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1279 runtime->silence_size > 0)
1280 snd_pcm_playback_silence(substream, ULONG_MAX);
1281}
1282
1283static struct action_ops snd_pcm_action_reset = {
1284 .pre_action = snd_pcm_pre_reset,
1285 .do_action = snd_pcm_do_reset,
1286 .post_action = snd_pcm_post_reset
1287};
1288
Takashi Iwai877211f2005-11-17 13:59:38 +01001289static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1292}
1293
1294/*
1295 * prepare ioctl
1296 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001297/* we use the second argument for updating f_flags */
1298static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1299 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwai877211f2005-11-17 13:59:38 +01001301 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001302 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1303 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EBADFD;
1305 if (snd_pcm_running(substream))
1306 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001307 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
Takashi Iwai877211f2005-11-17 13:59:38 +01001311static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int err;
1314 err = substream->ops->prepare(substream);
1315 if (err < 0)
1316 return err;
1317 return snd_pcm_do_reset(substream, 0);
1318}
1319
Takashi Iwai877211f2005-11-17 13:59:38 +01001320static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Takashi Iwai877211f2005-11-17 13:59:38 +01001322 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 runtime->control->appl_ptr = runtime->status->hw_ptr;
1324 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1325}
1326
1327static struct action_ops snd_pcm_action_prepare = {
1328 .pre_action = snd_pcm_pre_prepare,
1329 .do_action = snd_pcm_do_prepare,
1330 .post_action = snd_pcm_post_prepare
1331};
1332
1333/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001334 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001335 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001336 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001338static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1339 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001342 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001343 int f_flags;
1344
1345 if (file)
1346 f_flags = file->f_flags;
1347 else
1348 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001351 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001352 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1353 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 snd_power_unlock(card);
1355 return res;
1356}
1357
1358/*
1359 * drain ioctl
1360 */
1361
Takashi Iwai877211f2005-11-17 13:59:38 +01001362static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 substream->runtime->trigger_master = substream;
1365 return 0;
1366}
1367
Takashi Iwai877211f2005-11-17 13:59:38 +01001368static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Takashi Iwai877211f2005-11-17 13:59:38 +01001370 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1372 switch (runtime->status->state) {
1373 case SNDRV_PCM_STATE_PREPARED:
1374 /* start playback stream if possible */
1375 if (! snd_pcm_playback_empty(substream)) {
1376 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1377 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1378 }
1379 break;
1380 case SNDRV_PCM_STATE_RUNNING:
1381 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1382 break;
1383 default:
1384 break;
1385 }
1386 } else {
1387 /* stop running stream */
1388 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001389 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001391 snd_pcm_do_stop(substream, new_state);
1392 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394 }
1395 return 0;
1396}
1397
Takashi Iwai877211f2005-11-17 13:59:38 +01001398static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400}
1401
1402static struct action_ops snd_pcm_action_drain_init = {
1403 .pre_action = snd_pcm_pre_drain_init,
1404 .do_action = snd_pcm_do_drain_init,
1405 .post_action = snd_pcm_post_drain_init
1406};
1407
Takashi Iwai877211f2005-11-17 13:59:38 +01001408static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410/*
1411 * Drain the stream(s).
1412 * When the substream is linked, sync until the draining of all playback streams
1413 * is finished.
1414 * After this call, all streams are supposed to be either SETUP or DRAINING
1415 * (capture only) state.
1416 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001417static int snd_pcm_drain(struct snd_pcm_substream *substream,
1418 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Takashi Iwai877211f2005-11-17 13:59:38 +01001420 struct snd_card *card;
1421 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001422 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001423 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001425 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 card = substream->pcm->card;
1428 runtime = substream->runtime;
1429
1430 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1431 return -EBADFD;
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 snd_power_lock(card);
1434 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001435 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001436 if (result < 0) {
1437 snd_power_unlock(card);
1438 return result;
1439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001442 if (file) {
1443 if (file->f_flags & O_NONBLOCK)
1444 nonblock = 1;
1445 } else if (substream->f_flags & O_NONBLOCK)
1446 nonblock = 1;
1447
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001448 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001449 snd_pcm_stream_lock_irq(substream);
1450 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001451 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001452 snd_pcm_pause(substream, 0);
1453
1454 /* pre-start/stop - all running streams are changed to DRAINING state */
1455 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001456 if (result < 0)
1457 goto unlock;
1458 /* in non-blocking, we don't wait in ioctl but let caller poll */
1459 if (nonblock) {
1460 result = -EAGAIN;
1461 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 for (;;) {
1465 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001466 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (signal_pending(current)) {
1468 result = -ERESTARTSYS;
1469 break;
1470 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001471 /* find a substream to drain */
1472 to_check = NULL;
1473 snd_pcm_group_for_each_entry(s, substream) {
1474 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1475 continue;
1476 runtime = s->runtime;
1477 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1478 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001479 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001480 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001481 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001482 if (!to_check)
1483 break; /* all drained */
1484 init_waitqueue_entry(&wait, current);
1485 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 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);
Takashi Iwaif2b36142011-05-26 08:09:38 +02001489 if (runtime->no_period_wakeup)
1490 tout = MAX_SCHEDULE_TIMEOUT;
1491 else {
1492 tout = 10;
1493 if (runtime->rate) {
1494 long t = runtime->period_size * 2 / runtime->rate;
1495 tout = max(t, tout);
1496 }
1497 tout = msecs_to_jiffies(tout * 1000);
1498 }
1499 tout = schedule_timeout_interruptible(tout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001501 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001503 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (tout == 0) {
1505 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1506 result = -ESTRPIPE;
1507 else {
1508 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1509 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1510 result = -EIO;
1511 }
1512 break;
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001515
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001516 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001517 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001518 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 return result;
1522}
1523
Asish Bhattacharya9048f752011-11-01 20:33:44 +05301524static int snd_compressed_ioctl(struct snd_pcm_substream *substream,
1525 unsigned int cmd, void __user *arg)
1526{
1527 struct snd_pcm_runtime *runtime;
1528 int err = 0;
1529
1530 if (PCM_RUNTIME_CHECK(substream))
1531 return -ENXIO;
1532 runtime = substream->runtime;
Swaminathan Sathappan362a17c2012-04-25 18:09:46 -07001533 pr_debug("%s called with cmd = %d\n", __func__, cmd);
Asish Bhattacharya9048f752011-11-01 20:33:44 +05301534 err = substream->ops->ioctl(substream, cmd, arg);
1535 return err;
1536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/*
1538 * drop ioctl
1539 *
1540 * Immediately put all linked substreams into SETUP state.
1541 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001542static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Takashi Iwai877211f2005-11-17 13:59:38 +01001544 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 int result = 0;
1546
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001547 if (PCM_RUNTIME_CHECK(substream))
1548 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001551 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001552 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1553 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return -EBADFD;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 snd_pcm_stream_lock_irq(substream);
1557 /* resume pause */
1558 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1559 snd_pcm_pause(substream, 0);
1560
1561 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1562 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1563 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return result;
1566}
1567
1568
1569/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570static struct file *snd_pcm_file_fd(int fd)
1571{
1572 struct file *file;
1573 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001574 unsigned int minor;
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 file = fget(fd);
1577 if (!file)
1578 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001579 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (!S_ISCHR(inode->i_mode) ||
1581 imajor(inode) != snd_major) {
1582 fput(file);
1583 return NULL;
1584 }
1585 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001586 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1587 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 fput(file);
1589 return NULL;
1590 }
1591 return file;
1592}
1593
1594/*
1595 * PCM link handling
1596 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001597static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 int res = 0;
1600 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001601 struct snd_pcm_file *pcm_file;
1602 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001603 struct snd_pcm_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 file = snd_pcm_file_fd(fd);
1606 if (!file)
1607 return -EBADFD;
1608 pcm_file = file->private_data;
1609 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001610 group = kmalloc(sizeof(*group), GFP_KERNEL);
1611 if (!group) {
1612 res = -ENOMEM;
1613 goto _nolock;
1614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 down_write(&snd_pcm_link_rwsem);
1616 write_lock_irq(&snd_pcm_link_rwlock);
1617 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1618 substream->runtime->status->state != substream1->runtime->status->state) {
1619 res = -EBADFD;
1620 goto _end;
1621 }
1622 if (snd_pcm_stream_linked(substream1)) {
1623 res = -EALREADY;
1624 goto _end;
1625 }
1626 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001627 substream->group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 spin_lock_init(&substream->group->lock);
1629 INIT_LIST_HEAD(&substream->group->substreams);
1630 list_add_tail(&substream->link_list, &substream->group->substreams);
1631 substream->group->count = 1;
1632 }
1633 list_add_tail(&substream1->link_list, &substream->group->substreams);
1634 substream->group->count++;
1635 substream1->group = substream->group;
1636 _end:
1637 write_unlock_irq(&snd_pcm_link_rwlock);
1638 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001639 _nolock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 fput(file);
Takashi Iwai16625912012-03-13 15:55:43 +01001641 if (res < 0)
1642 kfree(group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return res;
1644}
1645
Takashi Iwai877211f2005-11-17 13:59:38 +01001646static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 substream->group = &substream->self_group;
1649 INIT_LIST_HEAD(&substream->self_group.substreams);
1650 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1651}
1652
Takashi Iwai877211f2005-11-17 13:59:38 +01001653static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwaief991b92007-02-22 12:52:53 +01001655 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int res = 0;
1657
1658 down_write(&snd_pcm_link_rwsem);
1659 write_lock_irq(&snd_pcm_link_rwlock);
1660 if (!snd_pcm_stream_linked(substream)) {
1661 res = -EALREADY;
1662 goto _end;
1663 }
1664 list_del(&substream->link_list);
1665 substream->group->count--;
1666 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001667 snd_pcm_group_for_each_entry(s, substream) {
1668 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
1670 }
1671 kfree(substream->group);
1672 }
1673 relink_to_local(substream);
1674 _end:
1675 write_unlock_irq(&snd_pcm_link_rwlock);
1676 up_write(&snd_pcm_link_rwsem);
1677 return res;
1678}
1679
1680/*
1681 * hw configurator
1682 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001683static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1684 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
Takashi Iwai877211f2005-11-17 13:59:38 +01001686 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1688 hw_param_interval_c(params, rule->deps[1]), &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690}
1691
Takashi Iwai877211f2005-11-17 13:59:38 +01001692static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai877211f2005-11-17 13:59:38 +01001695 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1697 hw_param_interval_c(params, rule->deps[1]), &t);
1698 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1699}
1700
Takashi Iwai877211f2005-11-17 13:59:38 +01001701static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1702 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Takashi Iwai877211f2005-11-17 13:59:38 +01001704 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1706 hw_param_interval_c(params, rule->deps[1]),
1707 (unsigned long) rule->private, &t);
1708 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1709}
1710
Takashi Iwai877211f2005-11-17 13:59:38 +01001711static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1712 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwai877211f2005-11-17 13:59:38 +01001714 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1716 (unsigned long) rule->private,
1717 hw_param_interval_c(params, rule->deps[1]), &t);
1718 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1719}
1720
Takashi Iwai877211f2005-11-17 13:59:38 +01001721static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1722 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001725 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1726 struct snd_mask m;
1727 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 snd_mask_any(&m);
1729 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1730 int bits;
1731 if (! snd_mask_test(mask, k))
1732 continue;
1733 bits = snd_pcm_format_physical_width(k);
1734 if (bits <= 0)
1735 continue; /* ignore invalid formats */
1736 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1737 snd_mask_reset(&m, k);
1738 }
1739 return snd_mask_refine(mask, &m);
1740}
1741
Takashi Iwai877211f2005-11-17 13:59:38 +01001742static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1743 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Takashi Iwai877211f2005-11-17 13:59:38 +01001745 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 unsigned int k;
1747 t.min = UINT_MAX;
1748 t.max = 0;
1749 t.openmin = 0;
1750 t.openmax = 0;
1751 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1752 int bits;
1753 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1754 continue;
1755 bits = snd_pcm_format_physical_width(k);
1756 if (bits <= 0)
1757 continue; /* ignore invalid formats */
1758 if (t.min > (unsigned)bits)
1759 t.min = bits;
1760 if (t.max < (unsigned)bits)
1761 t.max = bits;
1762 }
1763 t.integer = 1;
1764 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1765}
1766
1767#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1768#error "Change this table"
1769#endif
1770
1771static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1772 48000, 64000, 88200, 96000, 176400, 192000 };
1773
Clemens Ladisch7653d552007-08-13 17:38:54 +02001774const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1775 .count = ARRAY_SIZE(rates),
1776 .list = rates,
1777};
1778
Takashi Iwai877211f2005-11-17 13:59:38 +01001779static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1780 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Takashi Iwai877211f2005-11-17 13:59:38 +01001782 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001784 snd_pcm_known_rates.count,
1785 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Takashi Iwai877211f2005-11-17 13:59:38 +01001788static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1789 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Takashi Iwai877211f2005-11-17 13:59:38 +01001791 struct snd_interval t;
1792 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 t.min = 0;
1794 t.max = substream->buffer_bytes_max;
1795 t.openmin = 0;
1796 t.openmax = 0;
1797 t.integer = 1;
1798 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1799}
1800
Takashi Iwai877211f2005-11-17 13:59:38 +01001801int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Takashi Iwai877211f2005-11-17 13:59:38 +01001803 struct snd_pcm_runtime *runtime = substream->runtime;
1804 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 int k, err;
1806
1807 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1808 snd_mask_any(constrs_mask(constrs, k));
1809 }
1810
1811 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1812 snd_interval_any(constrs_interval(constrs, k));
1813 }
1814
1815 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1816 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1817 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1818 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1819 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1820
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1822 snd_pcm_hw_rule_format, NULL,
1823 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1827 snd_pcm_hw_rule_sample_bits, NULL,
1828 SNDRV_PCM_HW_PARAM_FORMAT,
1829 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1830 if (err < 0)
1831 return err;
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1833 snd_pcm_hw_rule_div, NULL,
1834 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1835 if (err < 0)
1836 return err;
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1838 snd_pcm_hw_rule_mul, NULL,
1839 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1840 if (err < 0)
1841 return err;
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1843 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1844 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1845 if (err < 0)
1846 return err;
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1848 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1849 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1850 if (err < 0)
1851 return err;
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1853 snd_pcm_hw_rule_div, NULL,
1854 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1855 if (err < 0)
1856 return err;
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1859 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1860 if (err < 0)
1861 return err;
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1863 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1864 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1865 if (err < 0)
1866 return err;
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1868 snd_pcm_hw_rule_div, NULL,
1869 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1870 if (err < 0)
1871 return err;
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1873 snd_pcm_hw_rule_div, NULL,
1874 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1875 if (err < 0)
1876 return err;
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1878 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1879 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1880 if (err < 0)
1881 return err;
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1883 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1884 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1885 if (err < 0)
1886 return err;
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1888 snd_pcm_hw_rule_mul, NULL,
1889 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1890 if (err < 0)
1891 return err;
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1893 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1894 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1895 if (err < 0)
1896 return err;
1897 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1898 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1899 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1900 if (err < 0)
1901 return err;
1902 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1903 snd_pcm_hw_rule_muldivk, (void*) 8,
1904 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1905 if (err < 0)
1906 return err;
1907 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1908 snd_pcm_hw_rule_muldivk, (void*) 8,
1909 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1910 if (err < 0)
1911 return err;
1912 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1913 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1914 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1915 if (err < 0)
1916 return err;
1917 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1918 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1919 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1920 if (err < 0)
1921 return err;
1922 return 0;
1923}
1924
Takashi Iwai877211f2005-11-17 13:59:38 +01001925int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Takashi Iwai877211f2005-11-17 13:59:38 +01001927 struct snd_pcm_runtime *runtime = substream->runtime;
1928 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 int err;
1930 unsigned int mask = 0;
1931
1932 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1933 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1934 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1935 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1936 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1937 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1938 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1939 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1940 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1941 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1942 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1943 }
1944 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001945 if (err < 0)
1946 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001949 if (err < 0)
1950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001953 if (err < 0)
1954 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1957 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001958 if (err < 0)
1959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1962 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001963 if (err < 0)
1964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1967 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001968 if (err < 0)
1969 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1972 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001973 if (err < 0)
1974 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1977 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001978 if (err < 0)
1979 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1982 snd_pcm_hw_rule_buffer_bytes_max, substream,
1983 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1984 if (err < 0)
1985 return err;
1986
1987 /* FIXME: remove */
1988 if (runtime->dma_bytes) {
1989 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001990 if (err < 0)
1991 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
1994 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1996 snd_pcm_hw_rule_rate, hw,
1997 SNDRV_PCM_HW_PARAM_RATE, -1);
1998 if (err < 0)
1999 return err;
2000 }
2001
2002 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2004
2005 return 0;
2006}
2007
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002011}
2012
2013void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2014{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002015 substream->ref_count--;
2016 if (substream->ref_count > 0)
2017 return;
2018
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002020 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (substream->ops->hw_free != NULL)
2022 substream->ops->hw_free(substream);
2023 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002024 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002026 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2027 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002028 if (substream->pcm_release) {
2029 substream->pcm_release(substream);
2030 substream->pcm_release = NULL;
2031 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002032 snd_pcm_detach_substream(substream);
2033}
2034
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002035EXPORT_SYMBOL(snd_pcm_release_substream);
2036
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002037int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2038 struct file *file,
2039 struct snd_pcm_substream **rsubstream)
2040{
2041 struct snd_pcm_substream *substream;
2042 int err;
2043
2044 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2045 if (err < 0)
2046 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002047 if (substream->ref_count > 1) {
2048 *rsubstream = substream;
2049 return 0;
2050 }
2051
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002052 err = snd_pcm_hw_constraints_init(substream);
2053 if (err < 0) {
2054 snd_printd("snd_pcm_hw_constraints_init failed\n");
2055 goto error;
2056 }
2057
Liam Girdwood8ccfb4a2011-02-16 16:50:12 +00002058 if (substream->ops == NULL) {
2059 snd_printd("cannot open back end PCMs directly\n");
2060 err = -ENODEV;
2061 goto error;
2062 }
2063
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002064 if ((err = substream->ops->open(substream)) < 0)
2065 goto error;
2066
2067 substream->hw_opened = 1;
2068
2069 err = snd_pcm_hw_constraints_complete(substream);
2070 if (err < 0) {
2071 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2072 goto error;
2073 }
2074
2075 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002077
2078 error:
2079 snd_pcm_release_substream(substream);
2080 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002083EXPORT_SYMBOL(snd_pcm_open_substream);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002086 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002087 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Takashi Iwai877211f2005-11-17 13:59:38 +01002089 struct snd_pcm_file *pcm_file;
2090 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002091 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002093 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2094 if (err < 0)
2095 return err;
2096
Takashi Iwai548a6482006-07-31 16:51:51 +02002097 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2098 if (pcm_file == NULL) {
2099 snd_pcm_release_substream(substream);
2100 return -ENOMEM;
2101 }
2102 pcm_file->substream = substream;
2103 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002104 substream->file = pcm_file;
2105 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 0;
2110}
2111
Clemens Ladischf87135f2005-11-20 14:06:59 +01002112static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Takashi Iwai877211f2005-11-17 13:59:38 +01002114 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002115 int err = nonseekable_open(inode, file);
2116 if (err < 0)
2117 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002118 pcm = snd_lookup_minor_data(iminor(inode),
2119 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2120 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2121}
2122
2123static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2124{
2125 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002126 int err = nonseekable_open(inode, file);
2127 if (err < 0)
2128 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002129 pcm = snd_lookup_minor_data(iminor(inode),
2130 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2131 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2132}
2133
2134static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2135{
2136 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 wait_queue_t wait;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (pcm == NULL) {
2140 err = -ENODEV;
2141 goto __error1;
2142 }
2143 err = snd_card_file_add(pcm->card, file);
2144 if (err < 0)
2145 goto __error1;
2146 if (!try_module_get(pcm->card->module)) {
2147 err = -EFAULT;
2148 goto __error2;
2149 }
2150 init_waitqueue_entry(&wait, current);
2151 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002152 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002154 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (err >= 0)
2156 break;
2157 if (err == -EAGAIN) {
2158 if (file->f_flags & O_NONBLOCK) {
2159 err = -EBUSY;
2160 break;
2161 }
2162 } else
2163 break;
2164 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002165 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002167 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (signal_pending(current)) {
2169 err = -ERESTARTSYS;
2170 break;
2171 }
2172 }
2173 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002174 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (err < 0)
2176 goto __error;
2177 return err;
2178
2179 __error:
2180 module_put(pcm->card->module);
2181 __error2:
2182 snd_card_file_remove(pcm->card, file);
2183 __error1:
2184 return err;
2185}
2186
2187static int snd_pcm_release(struct inode *inode, struct file *file)
2188{
Takashi Iwai877211f2005-11-17 13:59:38 +01002189 struct snd_pcm *pcm;
2190 struct snd_pcm_substream *substream;
2191 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 pcm_file = file->private_data;
2194 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002195 if (snd_BUG_ON(!substream))
2196 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002198 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002199 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002200 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002201 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 wake_up(&pcm->open_wait);
2203 module_put(pcm->card->module);
2204 snd_card_file_remove(pcm->card, file);
2205 return 0;
2206}
2207
Takashi Iwai877211f2005-11-17 13:59:38 +01002208static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2209 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Takashi Iwai877211f2005-11-17 13:59:38 +01002211 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_pcm_sframes_t appl_ptr;
2213 snd_pcm_sframes_t ret;
2214 snd_pcm_sframes_t hw_avail;
2215
2216 if (frames == 0)
2217 return 0;
2218
2219 snd_pcm_stream_lock_irq(substream);
2220 switch (runtime->status->state) {
2221 case SNDRV_PCM_STATE_PREPARED:
2222 break;
2223 case SNDRV_PCM_STATE_DRAINING:
2224 case SNDRV_PCM_STATE_RUNNING:
2225 if (snd_pcm_update_hw_ptr(substream) >= 0)
2226 break;
2227 /* Fall through */
2228 case SNDRV_PCM_STATE_XRUN:
2229 ret = -EPIPE;
2230 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002231 case SNDRV_PCM_STATE_SUSPENDED:
2232 ret = -ESTRPIPE;
2233 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 default:
2235 ret = -EBADFD;
2236 goto __end;
2237 }
2238
2239 hw_avail = snd_pcm_playback_hw_avail(runtime);
2240 if (hw_avail <= 0) {
2241 ret = 0;
2242 goto __end;
2243 }
2244 if (frames > (snd_pcm_uframes_t)hw_avail)
2245 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 appl_ptr = runtime->control->appl_ptr - frames;
2247 if (appl_ptr < 0)
2248 appl_ptr += runtime->boundary;
2249 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 ret = frames;
2251 __end:
2252 snd_pcm_stream_unlock_irq(substream);
2253 return ret;
2254}
2255
Takashi Iwai877211f2005-11-17 13:59:38 +01002256static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2257 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Takashi Iwai877211f2005-11-17 13:59:38 +01002259 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 snd_pcm_sframes_t appl_ptr;
2261 snd_pcm_sframes_t ret;
2262 snd_pcm_sframes_t hw_avail;
2263
2264 if (frames == 0)
2265 return 0;
2266
2267 snd_pcm_stream_lock_irq(substream);
2268 switch (runtime->status->state) {
2269 case SNDRV_PCM_STATE_PREPARED:
2270 case SNDRV_PCM_STATE_DRAINING:
2271 break;
2272 case SNDRV_PCM_STATE_RUNNING:
2273 if (snd_pcm_update_hw_ptr(substream) >= 0)
2274 break;
2275 /* Fall through */
2276 case SNDRV_PCM_STATE_XRUN:
2277 ret = -EPIPE;
2278 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002279 case SNDRV_PCM_STATE_SUSPENDED:
2280 ret = -ESTRPIPE;
2281 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 default:
2283 ret = -EBADFD;
2284 goto __end;
2285 }
2286
2287 hw_avail = snd_pcm_capture_hw_avail(runtime);
2288 if (hw_avail <= 0) {
2289 ret = 0;
2290 goto __end;
2291 }
2292 if (frames > (snd_pcm_uframes_t)hw_avail)
2293 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 appl_ptr = runtime->control->appl_ptr - frames;
2295 if (appl_ptr < 0)
2296 appl_ptr += runtime->boundary;
2297 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 ret = frames;
2299 __end:
2300 snd_pcm_stream_unlock_irq(substream);
2301 return ret;
2302}
2303
Takashi Iwai877211f2005-11-17 13:59:38 +01002304static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2305 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
Takashi Iwai877211f2005-11-17 13:59:38 +01002307 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 snd_pcm_sframes_t appl_ptr;
2309 snd_pcm_sframes_t ret;
2310 snd_pcm_sframes_t avail;
2311
2312 if (frames == 0)
2313 return 0;
2314
2315 snd_pcm_stream_lock_irq(substream);
2316 switch (runtime->status->state) {
2317 case SNDRV_PCM_STATE_PREPARED:
2318 case SNDRV_PCM_STATE_PAUSED:
2319 break;
2320 case SNDRV_PCM_STATE_DRAINING:
2321 case SNDRV_PCM_STATE_RUNNING:
2322 if (snd_pcm_update_hw_ptr(substream) >= 0)
2323 break;
2324 /* Fall through */
2325 case SNDRV_PCM_STATE_XRUN:
2326 ret = -EPIPE;
2327 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002328 case SNDRV_PCM_STATE_SUSPENDED:
2329 ret = -ESTRPIPE;
2330 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 default:
2332 ret = -EBADFD;
2333 goto __end;
2334 }
2335
2336 avail = snd_pcm_playback_avail(runtime);
2337 if (avail <= 0) {
2338 ret = 0;
2339 goto __end;
2340 }
2341 if (frames > (snd_pcm_uframes_t)avail)
2342 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 appl_ptr = runtime->control->appl_ptr + frames;
2344 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2345 appl_ptr -= runtime->boundary;
2346 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 ret = frames;
2348 __end:
2349 snd_pcm_stream_unlock_irq(substream);
2350 return ret;
2351}
2352
Takashi Iwai877211f2005-11-17 13:59:38 +01002353static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2354 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Takashi Iwai877211f2005-11-17 13:59:38 +01002356 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 snd_pcm_sframes_t appl_ptr;
2358 snd_pcm_sframes_t ret;
2359 snd_pcm_sframes_t avail;
2360
2361 if (frames == 0)
2362 return 0;
2363
2364 snd_pcm_stream_lock_irq(substream);
2365 switch (runtime->status->state) {
2366 case SNDRV_PCM_STATE_PREPARED:
2367 case SNDRV_PCM_STATE_DRAINING:
2368 case SNDRV_PCM_STATE_PAUSED:
2369 break;
2370 case SNDRV_PCM_STATE_RUNNING:
2371 if (snd_pcm_update_hw_ptr(substream) >= 0)
2372 break;
2373 /* Fall through */
2374 case SNDRV_PCM_STATE_XRUN:
2375 ret = -EPIPE;
2376 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002377 case SNDRV_PCM_STATE_SUSPENDED:
2378 ret = -ESTRPIPE;
2379 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 default:
2381 ret = -EBADFD;
2382 goto __end;
2383 }
2384
2385 avail = snd_pcm_capture_avail(runtime);
2386 if (avail <= 0) {
2387 ret = 0;
2388 goto __end;
2389 }
2390 if (frames > (snd_pcm_uframes_t)avail)
2391 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 appl_ptr = runtime->control->appl_ptr + frames;
2393 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2394 appl_ptr -= runtime->boundary;
2395 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 ret = frames;
2397 __end:
2398 snd_pcm_stream_unlock_irq(substream);
2399 return ret;
2400}
2401
Takashi Iwai877211f2005-11-17 13:59:38 +01002402static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Takashi Iwai877211f2005-11-17 13:59:38 +01002404 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int err;
2406
2407 snd_pcm_stream_lock_irq(substream);
2408 switch (runtime->status->state) {
2409 case SNDRV_PCM_STATE_DRAINING:
2410 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2411 goto __badfd;
2412 case SNDRV_PCM_STATE_RUNNING:
2413 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2414 break;
2415 /* Fall through */
2416 case SNDRV_PCM_STATE_PREPARED:
2417 case SNDRV_PCM_STATE_SUSPENDED:
2418 err = 0;
2419 break;
2420 case SNDRV_PCM_STATE_XRUN:
2421 err = -EPIPE;
2422 break;
2423 default:
2424 __badfd:
2425 err = -EBADFD;
2426 break;
2427 }
2428 snd_pcm_stream_unlock_irq(substream);
2429 return err;
2430}
2431
Takashi Iwai877211f2005-11-17 13:59:38 +01002432static int snd_pcm_delay(struct snd_pcm_substream *substream,
2433 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Takashi Iwai877211f2005-11-17 13:59:38 +01002435 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 int err;
2437 snd_pcm_sframes_t n = 0;
2438
2439 snd_pcm_stream_lock_irq(substream);
2440 switch (runtime->status->state) {
2441 case SNDRV_PCM_STATE_DRAINING:
2442 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2443 goto __badfd;
2444 case SNDRV_PCM_STATE_RUNNING:
2445 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2446 break;
2447 /* Fall through */
2448 case SNDRV_PCM_STATE_PREPARED:
2449 case SNDRV_PCM_STATE_SUSPENDED:
2450 err = 0;
2451 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2452 n = snd_pcm_playback_hw_avail(runtime);
2453 else
2454 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002455 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 break;
2457 case SNDRV_PCM_STATE_XRUN:
2458 err = -EPIPE;
2459 break;
2460 default:
2461 __badfd:
2462 err = -EBADFD;
2463 break;
2464 }
2465 snd_pcm_stream_unlock_irq(substream);
2466 if (!err)
2467 if (put_user(n, res))
2468 err = -EFAULT;
2469 return err;
2470}
2471
Takashi Iwai877211f2005-11-17 13:59:38 +01002472static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2473 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Takashi Iwai877211f2005-11-17 13:59:38 +01002475 struct snd_pcm_runtime *runtime = substream->runtime;
2476 struct snd_pcm_sync_ptr sync_ptr;
2477 volatile struct snd_pcm_mmap_status *status;
2478 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 int err;
2480
2481 memset(&sync_ptr, 0, sizeof(sync_ptr));
2482 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2483 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002484 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 -07002485 return -EFAULT;
2486 status = runtime->status;
2487 control = runtime->control;
2488 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2489 err = snd_pcm_hwsync(substream);
2490 if (err < 0)
2491 return err;
2492 }
2493 snd_pcm_stream_lock_irq(substream);
2494 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2495 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2496 else
2497 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2498 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2499 control->avail_min = sync_ptr.c.control.avail_min;
2500 else
2501 sync_ptr.c.control.avail_min = control->avail_min;
2502 sync_ptr.s.status.state = status->state;
2503 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2504 sync_ptr.s.status.tstamp = status->tstamp;
2505 sync_ptr.s.status.suspended_state = status->suspended_state;
2506 snd_pcm_stream_unlock_irq(substream);
2507 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2508 return -EFAULT;
2509 return 0;
2510}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002511
2512static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2513{
2514 struct snd_pcm_runtime *runtime = substream->runtime;
2515 int arg;
2516
2517 if (get_user(arg, _arg))
2518 return -EFAULT;
2519 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2520 return -EINVAL;
2521 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2522 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2523 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2524 return 0;
2525}
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002526
Takashi Iwai0df63e42006-04-28 15:13:41 +02002527static int snd_pcm_common_ioctl1(struct file *file,
2528 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 unsigned int cmd, void __user *arg)
2530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 switch (cmd) {
2532 case SNDRV_PCM_IOCTL_PVERSION:
2533 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2534 case SNDRV_PCM_IOCTL_INFO:
2535 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002536 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2537 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002538 case SNDRV_PCM_IOCTL_TTSTAMP:
2539 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 case SNDRV_PCM_IOCTL_HW_REFINE:
2541 return snd_pcm_hw_refine_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_HW_PARAMS:
2543 return snd_pcm_hw_params_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_HW_FREE:
2545 return snd_pcm_hw_free(substream);
2546 case SNDRV_PCM_IOCTL_SW_PARAMS:
2547 return snd_pcm_sw_params_user(substream, arg);
2548 case SNDRV_PCM_IOCTL_STATUS:
2549 return snd_pcm_status_user(substream, arg);
2550 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2551 return snd_pcm_channel_info_user(substream, arg);
2552 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002553 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 case SNDRV_PCM_IOCTL_RESET:
2555 return snd_pcm_reset(substream);
2556 case SNDRV_PCM_IOCTL_START:
2557 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2558 case SNDRV_PCM_IOCTL_LINK:
2559 return snd_pcm_link(substream, (int)(unsigned long) arg);
2560 case SNDRV_PCM_IOCTL_UNLINK:
2561 return snd_pcm_unlink(substream);
2562 case SNDRV_PCM_IOCTL_RESUME:
2563 return snd_pcm_resume(substream);
2564 case SNDRV_PCM_IOCTL_XRUN:
2565 return snd_pcm_xrun(substream);
2566 case SNDRV_PCM_IOCTL_HWSYNC:
2567 return snd_pcm_hwsync(substream);
2568 case SNDRV_PCM_IOCTL_DELAY:
2569 return snd_pcm_delay(substream, arg);
2570 case SNDRV_PCM_IOCTL_SYNC_PTR:
2571 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002572#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2574 return snd_pcm_hw_refine_old_user(substream, arg);
2575 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2576 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002577#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002579 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 case SNDRV_PCM_IOCTL_DROP:
2581 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002582 case SNDRV_PCM_IOCTL_PAUSE:
2583 {
2584 int res;
2585 snd_pcm_stream_lock_irq(substream);
2586 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2587 snd_pcm_stream_unlock_irq(substream);
2588 return res;
2589 }
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302590 case SNDRV_COMPRESS_GET_CAPS:
2591 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2592 case SNDRV_COMPRESS_SET_PARAMS:
2593 case SNDRV_COMPRESS_GET_PARAMS:
2594 case SNDRV_COMPRESS_TSTAMP:
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002595 case SNDRV_COMPRESS_DRAIN:
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302596 return snd_compressed_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 snd_printd("unknown ioctl = 0x%x\n", cmd);
2599 return -ENOTTY;
2600}
2601
Takashi Iwai0df63e42006-04-28 15:13:41 +02002602static int snd_pcm_playback_ioctl1(struct file *file,
2603 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 unsigned int cmd, void __user *arg)
2605{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002606 if (snd_BUG_ON(!substream))
2607 return -ENXIO;
2608 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2609 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 switch (cmd) {
2611 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2612 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002613 struct snd_xferi xferi;
2614 struct snd_xferi __user *_xferi = arg;
2615 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 snd_pcm_sframes_t result;
2617 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2618 return -EBADFD;
2619 if (put_user(0, &_xferi->result))
2620 return -EFAULT;
2621 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2622 return -EFAULT;
2623 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2624 __put_user(result, &_xferi->result);
2625 return result < 0 ? result : 0;
2626 }
2627 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2628 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002629 struct snd_xfern xfern;
2630 struct snd_xfern __user *_xfern = arg;
2631 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 void __user **bufs;
2633 snd_pcm_sframes_t result;
2634 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2635 return -EBADFD;
2636 if (runtime->channels > 128)
2637 return -EINVAL;
2638 if (put_user(0, &_xfern->result))
2639 return -EFAULT;
2640 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2641 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002642
2643 bufs = memdup_user(xfern.bufs,
2644 sizeof(void *) * runtime->channels);
2645 if (IS_ERR(bufs))
2646 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2648 kfree(bufs);
2649 __put_user(result, &_xfern->result);
2650 return result < 0 ? result : 0;
2651 }
2652 case SNDRV_PCM_IOCTL_REWIND:
2653 {
2654 snd_pcm_uframes_t frames;
2655 snd_pcm_uframes_t __user *_frames = arg;
2656 snd_pcm_sframes_t result;
2657 if (get_user(frames, _frames))
2658 return -EFAULT;
2659 if (put_user(0, _frames))
2660 return -EFAULT;
2661 result = snd_pcm_playback_rewind(substream, frames);
2662 __put_user(result, _frames);
2663 return result < 0 ? result : 0;
2664 }
2665 case SNDRV_PCM_IOCTL_FORWARD:
2666 {
2667 snd_pcm_uframes_t frames;
2668 snd_pcm_uframes_t __user *_frames = arg;
2669 snd_pcm_sframes_t result;
2670 if (get_user(frames, _frames))
2671 return -EFAULT;
2672 if (put_user(0, _frames))
2673 return -EFAULT;
2674 result = snd_pcm_playback_forward(substream, frames);
2675 __put_user(result, _frames);
2676 return result < 0 ? result : 0;
2677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002679 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Takashi Iwai0df63e42006-04-28 15:13:41 +02002682static int snd_pcm_capture_ioctl1(struct file *file,
2683 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 unsigned int cmd, void __user *arg)
2685{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002686 if (snd_BUG_ON(!substream))
2687 return -ENXIO;
2688 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2689 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 switch (cmd) {
2691 case SNDRV_PCM_IOCTL_READI_FRAMES:
2692 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002693 struct snd_xferi xferi;
2694 struct snd_xferi __user *_xferi = arg;
2695 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 snd_pcm_sframes_t result;
2697 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2698 return -EBADFD;
2699 if (put_user(0, &_xferi->result))
2700 return -EFAULT;
2701 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2702 return -EFAULT;
2703 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2704 __put_user(result, &_xferi->result);
2705 return result < 0 ? result : 0;
2706 }
2707 case SNDRV_PCM_IOCTL_READN_FRAMES:
2708 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002709 struct snd_xfern xfern;
2710 struct snd_xfern __user *_xfern = arg;
2711 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 void *bufs;
2713 snd_pcm_sframes_t result;
2714 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2715 return -EBADFD;
2716 if (runtime->channels > 128)
2717 return -EINVAL;
2718 if (put_user(0, &_xfern->result))
2719 return -EFAULT;
2720 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2721 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002722
2723 bufs = memdup_user(xfern.bufs,
2724 sizeof(void *) * runtime->channels);
2725 if (IS_ERR(bufs))
2726 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2728 kfree(bufs);
2729 __put_user(result, &_xfern->result);
2730 return result < 0 ? result : 0;
2731 }
2732 case SNDRV_PCM_IOCTL_REWIND:
2733 {
2734 snd_pcm_uframes_t frames;
2735 snd_pcm_uframes_t __user *_frames = arg;
2736 snd_pcm_sframes_t result;
2737 if (get_user(frames, _frames))
2738 return -EFAULT;
2739 if (put_user(0, _frames))
2740 return -EFAULT;
2741 result = snd_pcm_capture_rewind(substream, frames);
2742 __put_user(result, _frames);
2743 return result < 0 ? result : 0;
2744 }
2745 case SNDRV_PCM_IOCTL_FORWARD:
2746 {
2747 snd_pcm_uframes_t frames;
2748 snd_pcm_uframes_t __user *_frames = arg;
2749 snd_pcm_sframes_t result;
2750 if (get_user(frames, _frames))
2751 return -EFAULT;
2752 if (put_user(0, _frames))
2753 return -EFAULT;
2754 result = snd_pcm_capture_forward(substream, frames);
2755 __put_user(result, _frames);
2756 return result < 0 ? result : 0;
2757 }
2758 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002759 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760}
2761
Takashi Iwai877211f2005-11-17 13:59:38 +01002762static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2763 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Takashi Iwai877211f2005-11-17 13:59:38 +01002765 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 pcm_file = file->private_data;
2768
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302769 if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C'))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -ENOTTY;
2771
Takashi Iwai0df63e42006-04-28 15:13:41 +02002772 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2773 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Takashi Iwai877211f2005-11-17 13:59:38 +01002776static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2777 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Takashi Iwai877211f2005-11-17 13:59:38 +01002779 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 pcm_file = file->private_data;
2782
Subhash Chandra Bose Naripeddyfecae382012-06-11 23:26:01 -07002783 if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C'))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return -ENOTTY;
2785
Takashi Iwai0df63e42006-04-28 15:13:41 +02002786 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2787 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Takashi Iwai877211f2005-11-17 13:59:38 +01002790int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 unsigned int cmd, void *arg)
2792{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002793 mm_segment_t fs;
2794 int result;
2795
2796 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 switch (substream->stream) {
2798 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002799 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2800 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002803 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2804 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002807 result = -EINVAL;
2808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002810 snd_leave_user(fs);
2811 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002814EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2815
Takashi Iwai877211f2005-11-17 13:59:38 +01002816static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2817 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Takashi Iwai877211f2005-11-17 13:59:38 +01002819 struct snd_pcm_file *pcm_file;
2820 struct snd_pcm_substream *substream;
2821 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 snd_pcm_sframes_t result;
2823
2824 pcm_file = file->private_data;
2825 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002826 if (PCM_RUNTIME_CHECK(substream))
2827 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 runtime = substream->runtime;
2829 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2830 return -EBADFD;
2831 if (!frame_aligned(runtime, count))
2832 return -EINVAL;
2833 count = bytes_to_frames(runtime, count);
2834 result = snd_pcm_lib_read(substream, buf, count);
2835 if (result > 0)
2836 result = frames_to_bytes(runtime, result);
2837 return result;
2838}
2839
Takashi Iwai877211f2005-11-17 13:59:38 +01002840static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2841 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Takashi Iwai877211f2005-11-17 13:59:38 +01002843 struct snd_pcm_file *pcm_file;
2844 struct snd_pcm_substream *substream;
2845 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 snd_pcm_sframes_t result;
2847
2848 pcm_file = file->private_data;
2849 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002850 if (PCM_RUNTIME_CHECK(substream))
2851 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002853 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2854 return -EBADFD;
2855 if (!frame_aligned(runtime, count))
2856 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 count = bytes_to_frames(runtime, count);
2858 result = snd_pcm_lib_write(substream, buf, count);
2859 if (result > 0)
2860 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return result;
2862}
2863
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002864static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2865 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
2867{
Takashi Iwai877211f2005-11-17 13:59:38 +01002868 struct snd_pcm_file *pcm_file;
2869 struct snd_pcm_substream *substream;
2870 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 snd_pcm_sframes_t result;
2872 unsigned long i;
2873 void __user **bufs;
2874 snd_pcm_uframes_t frames;
2875
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002876 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002878 if (PCM_RUNTIME_CHECK(substream))
2879 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 runtime = substream->runtime;
2881 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2882 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002883 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002885 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 frames = bytes_to_samples(runtime, iov->iov_len);
2888 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (bufs == NULL)
2890 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002891 for (i = 0; i < nr_segs; ++i)
2892 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 result = snd_pcm_lib_readv(substream, bufs, frames);
2894 if (result > 0)
2895 result = frames_to_bytes(runtime, result);
2896 kfree(bufs);
2897 return result;
2898}
2899
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002900static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2901 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Takashi Iwai877211f2005-11-17 13:59:38 +01002903 struct snd_pcm_file *pcm_file;
2904 struct snd_pcm_substream *substream;
2905 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 snd_pcm_sframes_t result;
2907 unsigned long i;
2908 void __user **bufs;
2909 snd_pcm_uframes_t frames;
2910
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002911 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002913 if (PCM_RUNTIME_CHECK(substream))
2914 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002916 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2917 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002918 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002919 !frame_aligned(runtime, iov->iov_len))
2920 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002921 frames = bytes_to_samples(runtime, iov->iov_len);
2922 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (bufs == NULL)
2924 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002925 for (i = 0; i < nr_segs; ++i)
2926 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 result = snd_pcm_lib_writev(substream, bufs, frames);
2928 if (result > 0)
2929 result = frames_to_bytes(runtime, result);
2930 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return result;
2932}
2933
2934static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2935{
Takashi Iwai877211f2005-11-17 13:59:38 +01002936 struct snd_pcm_file *pcm_file;
2937 struct snd_pcm_substream *substream;
2938 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 unsigned int mask;
2940 snd_pcm_uframes_t avail;
2941
2942 pcm_file = file->private_data;
2943
2944 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002945 if (PCM_RUNTIME_CHECK(substream))
2946 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 runtime = substream->runtime;
2948
2949 poll_wait(file, &runtime->sleep, wait);
2950
2951 snd_pcm_stream_lock_irq(substream);
2952 avail = snd_pcm_playback_avail(runtime);
2953 switch (runtime->status->state) {
2954 case SNDRV_PCM_STATE_RUNNING:
2955 case SNDRV_PCM_STATE_PREPARED:
2956 case SNDRV_PCM_STATE_PAUSED:
2957 if (avail >= runtime->control->avail_min) {
2958 mask = POLLOUT | POLLWRNORM;
2959 break;
2960 }
2961 /* Fall through */
2962 case SNDRV_PCM_STATE_DRAINING:
2963 mask = 0;
2964 break;
2965 default:
2966 mask = POLLOUT | POLLWRNORM | POLLERR;
2967 break;
2968 }
2969 snd_pcm_stream_unlock_irq(substream);
2970 return mask;
2971}
2972
2973static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2974{
Takashi Iwai877211f2005-11-17 13:59:38 +01002975 struct snd_pcm_file *pcm_file;
2976 struct snd_pcm_substream *substream;
2977 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 unsigned int mask;
2979 snd_pcm_uframes_t avail;
2980
2981 pcm_file = file->private_data;
2982
2983 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002984 if (PCM_RUNTIME_CHECK(substream))
2985 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 runtime = substream->runtime;
2987
2988 poll_wait(file, &runtime->sleep, wait);
2989
2990 snd_pcm_stream_lock_irq(substream);
2991 avail = snd_pcm_capture_avail(runtime);
2992 switch (runtime->status->state) {
2993 case SNDRV_PCM_STATE_RUNNING:
2994 case SNDRV_PCM_STATE_PREPARED:
2995 case SNDRV_PCM_STATE_PAUSED:
2996 if (avail >= runtime->control->avail_min) {
2997 mask = POLLIN | POLLRDNORM;
2998 break;
2999 }
3000 mask = 0;
3001 break;
3002 case SNDRV_PCM_STATE_DRAINING:
3003 if (avail > 0) {
3004 mask = POLLIN | POLLRDNORM;
3005 break;
3006 }
3007 /* Fall through */
3008 default:
3009 mask = POLLIN | POLLRDNORM | POLLERR;
3010 break;
3011 }
3012 snd_pcm_stream_unlock_irq(substream);
3013 return mask;
3014}
3015
3016/*
3017 * mmap support
3018 */
3019
3020/*
3021 * Only on coherent architectures, we can mmap the status and the control records
3022 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3023 */
3024#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3025/*
3026 * mmap status record
3027 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003028static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3029 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Takashi Iwai877211f2005-11-17 13:59:38 +01003031 struct snd_pcm_substream *substream = area->vm_private_data;
3032 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003035 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003037 vmf->page = virt_to_page(runtime->status);
3038 get_page(vmf->page);
3039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003042static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003044 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045};
3046
Takashi Iwai877211f2005-11-17 13:59:38 +01003047static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 struct vm_area_struct *area)
3049{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 long size;
3051 if (!(area->vm_flags & VM_READ))
3052 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003054 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return -EINVAL;
3056 area->vm_ops = &snd_pcm_vm_ops_status;
3057 area->vm_private_data = substream;
3058 area->vm_flags |= VM_RESERVED;
3059 return 0;
3060}
3061
3062/*
3063 * mmap control record
3064 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003065static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3066 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Takashi Iwai877211f2005-11-17 13:59:38 +01003068 struct snd_pcm_substream *substream = area->vm_private_data;
3069 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003072 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003074 vmf->page = virt_to_page(runtime->control);
3075 get_page(vmf->page);
3076 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003079static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003081 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082};
3083
Takashi Iwai877211f2005-11-17 13:59:38 +01003084static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct vm_area_struct *area)
3086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 long size;
3088 if (!(area->vm_flags & VM_READ))
3089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003091 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return -EINVAL;
3093 area->vm_ops = &snd_pcm_vm_ops_control;
3094 area->vm_private_data = substream;
3095 area->vm_flags |= VM_RESERVED;
3096 return 0;
3097}
3098#else /* ! coherent mmap */
3099/*
3100 * don't support mmap for status and control records.
3101 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003102static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 struct vm_area_struct *area)
3104{
3105 return -ENXIO;
3106}
Takashi Iwai877211f2005-11-17 13:59:38 +01003107static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 struct vm_area_struct *area)
3109{
3110 return -ENXIO;
3111}
3112#endif /* coherent mmap */
3113
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003114static inline struct page *
3115snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3116{
3117 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003118#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3119 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3120 return virt_to_page(CAC_ADDR(vaddr));
3121#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003122#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3123 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3124 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3125 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3126 /* assume dma_handle set via pfn_to_phys() in
3127 * mm/dma-noncoherent.c
3128 */
3129 return pfn_to_page(addr >> PAGE_SHIFT);
3130 }
3131#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003132 return virt_to_page(vaddr);
3133}
3134
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003136 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003138static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3139 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
Takashi Iwai877211f2005-11-17 13:59:38 +01003141 struct snd_pcm_substream *substream = area->vm_private_data;
3142 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 unsigned long offset;
3144 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 size_t dma_bytes;
3146
3147 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003148 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003150 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3152 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003153 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003154 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003156 else
3157 page = snd_pcm_default_page_ops(substream, offset);
3158 if (!page)
3159 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003160 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003161 vmf->page = page;
3162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
Takashi Iwai657b1982009-11-26 12:40:21 +01003165static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3166 .open = snd_pcm_mmap_data_open,
3167 .close = snd_pcm_mmap_data_close,
3168};
3169
3170static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 .open = snd_pcm_mmap_data_open,
3172 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003173 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174};
3175
Takashi Iwai657b1982009-11-26 12:40:21 +01003176#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3177/* This should be defined / handled globally! */
3178#ifdef CONFIG_ARM
3179#define ARCH_HAS_DMA_MMAP_COHERENT
3180#endif
3181#endif
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183/*
3184 * mmap the DMA buffer on RAM
3185 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003186int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3187 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003190#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3191 if (!substream->ops->page &&
3192 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3193 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3194 area,
3195 substream->runtime->dma_area,
3196 substream->runtime->dma_addr,
3197 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003198#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3199 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3200 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3201 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003202#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3203 /* mmap with fault handler */
3204 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return 0;
3206}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003207EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209/*
3210 * mmap the DMA buffer on I/O memory area
3211 */
3212#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003213int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3214 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215{
3216 long size;
3217 unsigned long offset;
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 area->vm_flags |= VM_IO;
3221 size = area->vm_end - area->vm_start;
3222 offset = area->vm_pgoff << PAGE_SHIFT;
3223 if (io_remap_pfn_range(area, area->vm_start,
3224 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3225 size, area->vm_page_prot))
3226 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return 0;
3228}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003229
3230EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231#endif /* SNDRV_PCM_INFO_MMAP */
3232
3233/*
3234 * mmap DMA buffer
3235 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003236int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct vm_area_struct *area)
3238{
Takashi Iwai877211f2005-11-17 13:59:38 +01003239 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 long size;
3241 unsigned long offset;
3242 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003243 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3246 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3247 return -EINVAL;
3248 } else {
3249 if (!(area->vm_flags & VM_READ))
3250 return -EINVAL;
3251 }
3252 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3254 return -EBADFD;
3255 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3256 return -ENXIO;
3257 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3258 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3259 return -EINVAL;
3260 size = area->vm_end - area->vm_start;
3261 offset = area->vm_pgoff << PAGE_SHIFT;
3262 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3263 if ((size_t)size > dma_bytes)
3264 return -EINVAL;
3265 if (offset > dma_bytes - size)
3266 return -EINVAL;
3267
Takashi Iwai657b1982009-11-26 12:40:21 +01003268 area->vm_ops = &snd_pcm_vm_ops_data;
3269 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003271 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003273 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003274 if (!err)
3275 atomic_inc(&substream->mmap_count);
3276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003279EXPORT_SYMBOL(snd_pcm_mmap_data);
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3282{
Takashi Iwai877211f2005-11-17 13:59:38 +01003283 struct snd_pcm_file * pcm_file;
3284 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 unsigned long offset;
3286
3287 pcm_file = file->private_data;
3288 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003289 if (PCM_RUNTIME_CHECK(substream))
3290 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 offset = area->vm_pgoff << PAGE_SHIFT;
3293 switch (offset) {
3294 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003295 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 return -ENXIO;
3297 return snd_pcm_mmap_status(substream, file, area);
3298 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003299 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return -ENXIO;
3301 return snd_pcm_mmap_control(substream, file, area);
3302 default:
3303 return snd_pcm_mmap_data(substream, file, area);
3304 }
3305 return 0;
3306}
3307
3308static int snd_pcm_fasync(int fd, struct file * file, int on)
3309{
Takashi Iwai877211f2005-11-17 13:59:38 +01003310 struct snd_pcm_file * pcm_file;
3311 struct snd_pcm_substream *substream;
3312 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314 pcm_file = file->private_data;
3315 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003316 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003317 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003319 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
3322/*
3323 * ioctl32 compat
3324 */
3325#ifdef CONFIG_COMPAT
3326#include "pcm_compat.c"
3327#else
3328#define snd_pcm_ioctl_compat NULL
3329#endif
3330
3331/*
3332 * To be removed helpers to keep binary compatibility
3333 */
3334
Takashi Iwai59d48582005-12-01 10:51:58 +01003335#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3337#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3338
Takashi Iwai877211f2005-11-17 13:59:38 +01003339static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3340 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
3342 unsigned int i;
3343
3344 memset(params, 0, sizeof(*params));
3345 params->flags = oparams->flags;
3346 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3347 params->masks[i].bits[0] = oparams->masks[i];
3348 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3349 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3350 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3351 params->info = oparams->info;
3352 params->msbits = oparams->msbits;
3353 params->rate_num = oparams->rate_num;
3354 params->rate_den = oparams->rate_den;
3355 params->fifo_size = oparams->fifo_size;
3356}
3357
Takashi Iwai877211f2005-11-17 13:59:38 +01003358static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3359 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
3361 unsigned int i;
3362
3363 memset(oparams, 0, sizeof(*oparams));
3364 oparams->flags = params->flags;
3365 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3366 oparams->masks[i] = params->masks[i].bits[0];
3367 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3368 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3369 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3370 oparams->info = params->info;
3371 oparams->msbits = params->msbits;
3372 oparams->rate_num = params->rate_num;
3373 oparams->rate_den = params->rate_den;
3374 oparams->fifo_size = params->fifo_size;
3375}
3376
Takashi Iwai877211f2005-11-17 13:59:38 +01003377static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3378 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379{
Takashi Iwai877211f2005-11-17 13:59:38 +01003380 struct snd_pcm_hw_params *params;
3381 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 int err;
3383
3384 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003385 if (!params)
3386 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
Li Zefanef44a1e2009-04-10 09:43:08 +08003388 oparams = memdup_user(_oparams, sizeof(*oparams));
3389 if (IS_ERR(oparams)) {
3390 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 goto out;
3392 }
3393 snd_pcm_hw_convert_from_old_params(params, oparams);
3394 err = snd_pcm_hw_refine(substream, params);
3395 snd_pcm_hw_convert_to_old_params(oparams, params);
3396 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3397 if (!err)
3398 err = -EFAULT;
3399 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003400
3401 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402out:
3403 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return err;
3405}
3406
Takashi Iwai877211f2005-11-17 13:59:38 +01003407static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3408 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
Takashi Iwai877211f2005-11-17 13:59:38 +01003410 struct snd_pcm_hw_params *params;
3411 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 int err;
3413
3414 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003415 if (!params)
3416 return -ENOMEM;
3417
3418 oparams = memdup_user(_oparams, sizeof(*oparams));
3419 if (IS_ERR(oparams)) {
3420 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 goto out;
3422 }
3423 snd_pcm_hw_convert_from_old_params(params, oparams);
3424 err = snd_pcm_hw_params(substream, params);
3425 snd_pcm_hw_convert_to_old_params(oparams, params);
3426 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3427 if (!err)
3428 err = -EFAULT;
3429 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003430
3431 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432out:
3433 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return err;
3435}
Takashi Iwai59d48582005-12-01 10:51:58 +01003436#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Cliff Cai70036092008-09-03 10:54:36 +02003438#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003439static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3440 unsigned long addr,
3441 unsigned long len,
3442 unsigned long pgoff,
3443 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003444{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003445 struct snd_pcm_file *pcm_file = file->private_data;
3446 struct snd_pcm_substream *substream = pcm_file->substream;
3447 struct snd_pcm_runtime *runtime = substream->runtime;
3448 unsigned long offset = pgoff << PAGE_SHIFT;
3449
3450 switch (offset) {
3451 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3452 return (unsigned long)runtime->status;
3453 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3454 return (unsigned long)runtime->control;
3455 default:
3456 return (unsigned long)runtime->dma_area + offset;
3457 }
Cliff Cai70036092008-09-03 10:54:36 +02003458}
3459#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003460# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003461#endif
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463/*
3464 * Register section
3465 */
3466
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003467const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003469 .owner = THIS_MODULE,
3470 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003471 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003472 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003473 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003474 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003475 .poll = snd_pcm_playback_poll,
3476 .unlocked_ioctl = snd_pcm_playback_ioctl,
3477 .compat_ioctl = snd_pcm_ioctl_compat,
3478 .mmap = snd_pcm_mmap,
3479 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003480 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 },
3482 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003483 .owner = THIS_MODULE,
3484 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003485 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003486 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003487 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003488 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003489 .poll = snd_pcm_capture_poll,
3490 .unlocked_ioctl = snd_pcm_capture_ioctl,
3491 .compat_ioctl = snd_pcm_ioctl_compat,
3492 .mmap = snd_pcm_mmap,
3493 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003494 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496};