blob: 7403b401c28a3a7a1dd0375655299f705774a74f [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}
Joonwoo Parked3bab02013-02-13 15:59:44 -08001537
1538static int snd_user_ioctl(struct snd_pcm_substream *substream,
1539 unsigned int cmd, void __user *arg)
1540{
1541 struct snd_pcm_runtime *runtime;
1542 int err = 0;
1543
1544 if (PCM_RUNTIME_CHECK(substream))
1545 return -ENXIO;
1546 runtime = substream->runtime;
1547 err = substream->ops->ioctl(substream, cmd, arg);
1548 return err;
1549}
1550
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551/*
1552 * drop ioctl
1553 *
1554 * Immediately put all linked substreams into SETUP state.
1555 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001556static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Takashi Iwai877211f2005-11-17 13:59:38 +01001558 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int result = 0;
1560
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001561 if (PCM_RUNTIME_CHECK(substream))
1562 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001565 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001566 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1567 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -EBADFD;
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 snd_pcm_stream_lock_irq(substream);
1571 /* resume pause */
1572 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1573 snd_pcm_pause(substream, 0);
1574
1575 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1576 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1577 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 return result;
1580}
1581
1582
1583/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584static struct file *snd_pcm_file_fd(int fd)
1585{
1586 struct file *file;
1587 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001588 unsigned int minor;
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 file = fget(fd);
1591 if (!file)
1592 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001593 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!S_ISCHR(inode->i_mode) ||
1595 imajor(inode) != snd_major) {
1596 fput(file);
1597 return NULL;
1598 }
1599 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001600 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1601 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 fput(file);
1603 return NULL;
1604 }
1605 return file;
1606}
1607
1608/*
1609 * PCM link handling
1610 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001611static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 int res = 0;
1614 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001615 struct snd_pcm_file *pcm_file;
1616 struct snd_pcm_substream *substream1;
Takashi Iwai16625912012-03-13 15:55:43 +01001617 struct snd_pcm_group *group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619 file = snd_pcm_file_fd(fd);
1620 if (!file)
1621 return -EBADFD;
1622 pcm_file = file->private_data;
1623 substream1 = pcm_file->substream;
Takashi Iwai16625912012-03-13 15:55:43 +01001624 group = kmalloc(sizeof(*group), GFP_KERNEL);
1625 if (!group) {
1626 res = -ENOMEM;
1627 goto _nolock;
1628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 down_write(&snd_pcm_link_rwsem);
1630 write_lock_irq(&snd_pcm_link_rwlock);
1631 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1632 substream->runtime->status->state != substream1->runtime->status->state) {
1633 res = -EBADFD;
1634 goto _end;
1635 }
1636 if (snd_pcm_stream_linked(substream1)) {
1637 res = -EALREADY;
1638 goto _end;
1639 }
1640 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai16625912012-03-13 15:55:43 +01001641 substream->group = group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 spin_lock_init(&substream->group->lock);
1643 INIT_LIST_HEAD(&substream->group->substreams);
1644 list_add_tail(&substream->link_list, &substream->group->substreams);
1645 substream->group->count = 1;
1646 }
1647 list_add_tail(&substream1->link_list, &substream->group->substreams);
1648 substream->group->count++;
1649 substream1->group = substream->group;
1650 _end:
1651 write_unlock_irq(&snd_pcm_link_rwlock);
1652 up_write(&snd_pcm_link_rwsem);
Takashi Iwai16625912012-03-13 15:55:43 +01001653 _nolock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 fput(file);
Takashi Iwai16625912012-03-13 15:55:43 +01001655 if (res < 0)
1656 kfree(group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return res;
1658}
1659
Takashi Iwai877211f2005-11-17 13:59:38 +01001660static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
1662 substream->group = &substream->self_group;
1663 INIT_LIST_HEAD(&substream->self_group.substreams);
1664 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1665}
1666
Takashi Iwai877211f2005-11-17 13:59:38 +01001667static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Takashi Iwaief991b92007-02-22 12:52:53 +01001669 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 int res = 0;
1671
1672 down_write(&snd_pcm_link_rwsem);
1673 write_lock_irq(&snd_pcm_link_rwlock);
1674 if (!snd_pcm_stream_linked(substream)) {
1675 res = -EALREADY;
1676 goto _end;
1677 }
1678 list_del(&substream->link_list);
1679 substream->group->count--;
1680 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001681 snd_pcm_group_for_each_entry(s, substream) {
1682 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 break;
1684 }
1685 kfree(substream->group);
1686 }
1687 relink_to_local(substream);
1688 _end:
1689 write_unlock_irq(&snd_pcm_link_rwlock);
1690 up_write(&snd_pcm_link_rwsem);
1691 return res;
1692}
1693
1694/*
1695 * hw configurator
1696 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001697static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1698 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Takashi Iwai877211f2005-11-17 13:59:38 +01001700 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1702 hw_param_interval_c(params, rule->deps[1]), &t);
1703 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1704}
1705
Takashi Iwai877211f2005-11-17 13:59:38 +01001706static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1707 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Takashi Iwai877211f2005-11-17 13:59:38 +01001709 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1711 hw_param_interval_c(params, rule->deps[1]), &t);
1712 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1713}
1714
Takashi Iwai877211f2005-11-17 13:59:38 +01001715static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1716 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Takashi Iwai877211f2005-11-17 13:59:38 +01001718 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1720 hw_param_interval_c(params, rule->deps[1]),
1721 (unsigned long) rule->private, &t);
1722 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1723}
1724
Takashi Iwai877211f2005-11-17 13:59:38 +01001725static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1726 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Takashi Iwai877211f2005-11-17 13:59:38 +01001728 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1730 (unsigned long) rule->private,
1731 hw_param_interval_c(params, rule->deps[1]), &t);
1732 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1733}
1734
Takashi Iwai877211f2005-11-17 13:59:38 +01001735static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1736 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001739 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1740 struct snd_mask m;
1741 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 snd_mask_any(&m);
1743 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1744 int bits;
1745 if (! snd_mask_test(mask, k))
1746 continue;
1747 bits = snd_pcm_format_physical_width(k);
1748 if (bits <= 0)
1749 continue; /* ignore invalid formats */
1750 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1751 snd_mask_reset(&m, k);
1752 }
1753 return snd_mask_refine(mask, &m);
1754}
1755
Takashi Iwai877211f2005-11-17 13:59:38 +01001756static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1757 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Takashi Iwai877211f2005-11-17 13:59:38 +01001759 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned int k;
1761 t.min = UINT_MAX;
1762 t.max = 0;
1763 t.openmin = 0;
1764 t.openmax = 0;
1765 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1766 int bits;
1767 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1768 continue;
1769 bits = snd_pcm_format_physical_width(k);
1770 if (bits <= 0)
1771 continue; /* ignore invalid formats */
1772 if (t.min > (unsigned)bits)
1773 t.min = bits;
1774 if (t.max < (unsigned)bits)
1775 t.max = bits;
1776 }
1777 t.integer = 1;
1778 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1779}
1780
1781#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1782#error "Change this table"
1783#endif
1784
1785static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1786 48000, 64000, 88200, 96000, 176400, 192000 };
1787
Clemens Ladisch7653d552007-08-13 17:38:54 +02001788const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1789 .count = ARRAY_SIZE(rates),
1790 .list = rates,
1791};
1792
Takashi Iwai877211f2005-11-17 13:59:38 +01001793static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1794 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Takashi Iwai877211f2005-11-17 13:59:38 +01001796 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001798 snd_pcm_known_rates.count,
1799 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Takashi Iwai877211f2005-11-17 13:59:38 +01001802static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1803 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Takashi Iwai877211f2005-11-17 13:59:38 +01001805 struct snd_interval t;
1806 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 t.min = 0;
1808 t.max = substream->buffer_bytes_max;
1809 t.openmin = 0;
1810 t.openmax = 0;
1811 t.integer = 1;
1812 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1813}
1814
Takashi Iwai877211f2005-11-17 13:59:38 +01001815int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Takashi Iwai877211f2005-11-17 13:59:38 +01001817 struct snd_pcm_runtime *runtime = substream->runtime;
1818 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int k, err;
1820
1821 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1822 snd_mask_any(constrs_mask(constrs, k));
1823 }
1824
1825 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1826 snd_interval_any(constrs_interval(constrs, k));
1827 }
1828
1829 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1831 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1832 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1833 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1834
1835 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1836 snd_pcm_hw_rule_format, NULL,
1837 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1838 if (err < 0)
1839 return err;
1840 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1841 snd_pcm_hw_rule_sample_bits, NULL,
1842 SNDRV_PCM_HW_PARAM_FORMAT,
1843 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1847 snd_pcm_hw_rule_div, NULL,
1848 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1852 snd_pcm_hw_rule_mul, NULL,
1853 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1857 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1858 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1867 snd_pcm_hw_rule_div, NULL,
1868 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1872 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1877 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1878 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1882 snd_pcm_hw_rule_div, NULL,
1883 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1887 snd_pcm_hw_rule_div, NULL,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1889 if (err < 0)
1890 return err;
1891 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1892 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1893 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1894 if (err < 0)
1895 return err;
1896 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1897 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1898 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1899 if (err < 0)
1900 return err;
1901 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1902 snd_pcm_hw_rule_mul, NULL,
1903 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1904 if (err < 0)
1905 return err;
1906 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1907 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1908 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1909 if (err < 0)
1910 return err;
1911 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1912 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1913 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1914 if (err < 0)
1915 return err;
1916 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1917 snd_pcm_hw_rule_muldivk, (void*) 8,
1918 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1919 if (err < 0)
1920 return err;
1921 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1922 snd_pcm_hw_rule_muldivk, (void*) 8,
1923 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1924 if (err < 0)
1925 return err;
1926 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1927 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1928 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1929 if (err < 0)
1930 return err;
1931 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1932 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1933 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1934 if (err < 0)
1935 return err;
1936 return 0;
1937}
1938
Takashi Iwai877211f2005-11-17 13:59:38 +01001939int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Takashi Iwai877211f2005-11-17 13:59:38 +01001941 struct snd_pcm_runtime *runtime = substream->runtime;
1942 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int err;
1944 unsigned int mask = 0;
1945
1946 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1947 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1948 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1949 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1950 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1951 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1952 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1953 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1954 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1955 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1956 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1957 }
1958 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001959 if (err < 0)
1960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001963 if (err < 0)
1964 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001967 if (err < 0)
1968 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1971 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001972 if (err < 0)
1973 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1976 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001977 if (err < 0)
1978 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1981 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001982 if (err < 0)
1983 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1986 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001987 if (err < 0)
1988 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1991 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001992 if (err < 0)
1993 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1996 snd_pcm_hw_rule_buffer_bytes_max, substream,
1997 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1998 if (err < 0)
1999 return err;
2000
2001 /* FIXME: remove */
2002 if (runtime->dma_bytes) {
2003 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002004 if (err < 0)
2005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007
2008 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
2009 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2010 snd_pcm_hw_rule_rate, hw,
2011 SNDRV_PCM_HW_PARAM_RATE, -1);
2012 if (err < 0)
2013 return err;
2014 }
2015
2016 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2018
2019 return 0;
2020}
2021
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002022static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002025}
2026
2027void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2028{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002029 substream->ref_count--;
2030 if (substream->ref_count > 0)
2031 return;
2032
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002033 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (substream->ops->hw_free != NULL)
2036 substream->ops->hw_free(substream);
2037 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002038 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02002040 if (pm_qos_request_active(&substream->latency_pm_qos_req))
2041 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02002042 if (substream->pcm_release) {
2043 substream->pcm_release(substream);
2044 substream->pcm_release = NULL;
2045 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002046 snd_pcm_detach_substream(substream);
2047}
2048
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002049EXPORT_SYMBOL(snd_pcm_release_substream);
2050
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002051int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2052 struct file *file,
2053 struct snd_pcm_substream **rsubstream)
2054{
2055 struct snd_pcm_substream *substream;
2056 int err;
2057
2058 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2059 if (err < 0)
2060 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002061 if (substream->ref_count > 1) {
2062 *rsubstream = substream;
2063 return 0;
2064 }
2065
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002066 err = snd_pcm_hw_constraints_init(substream);
2067 if (err < 0) {
2068 snd_printd("snd_pcm_hw_constraints_init failed\n");
2069 goto error;
2070 }
2071
Liam Girdwood8ccfb4a2011-02-16 16:50:12 +00002072 if (substream->ops == NULL) {
2073 snd_printd("cannot open back end PCMs directly\n");
2074 err = -ENODEV;
2075 goto error;
2076 }
2077
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002078 if ((err = substream->ops->open(substream)) < 0)
2079 goto error;
2080
2081 substream->hw_opened = 1;
2082
2083 err = snd_pcm_hw_constraints_complete(substream);
2084 if (err < 0) {
2085 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2086 goto error;
2087 }
2088
2089 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002091
2092 error:
2093 snd_pcm_release_substream(substream);
2094 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002097EXPORT_SYMBOL(snd_pcm_open_substream);
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002100 struct snd_pcm *pcm,
Feng Tangffd3d5c2011-10-10 10:31:48 +08002101 int stream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Takashi Iwai877211f2005-11-17 13:59:38 +01002103 struct snd_pcm_file *pcm_file;
2104 struct snd_pcm_substream *substream;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002105 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002107 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2108 if (err < 0)
2109 return err;
2110
Takashi Iwai548a6482006-07-31 16:51:51 +02002111 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2112 if (pcm_file == NULL) {
2113 snd_pcm_release_substream(substream);
2114 return -ENOMEM;
2115 }
2116 pcm_file->substream = substream;
2117 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002118 substream->file = pcm_file;
2119 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 file->private_data = pcm_file;
Feng Tangffd3d5c2011-10-10 10:31:48 +08002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
2125
Clemens Ladischf87135f2005-11-20 14:06:59 +01002126static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
Takashi Iwai877211f2005-11-17 13:59:38 +01002128 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002129 int err = nonseekable_open(inode, file);
2130 if (err < 0)
2131 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002132 pcm = snd_lookup_minor_data(iminor(inode),
2133 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2134 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2135}
2136
2137static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2138{
2139 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002140 int err = nonseekable_open(inode, file);
2141 if (err < 0)
2142 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002143 pcm = snd_lookup_minor_data(iminor(inode),
2144 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2145 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2146}
2147
2148static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2149{
2150 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 wait_queue_t wait;
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (pcm == NULL) {
2154 err = -ENODEV;
2155 goto __error1;
2156 }
2157 err = snd_card_file_add(pcm->card, file);
2158 if (err < 0)
2159 goto __error1;
2160 if (!try_module_get(pcm->card->module)) {
2161 err = -EFAULT;
2162 goto __error2;
2163 }
2164 init_waitqueue_entry(&wait, current);
2165 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002166 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 while (1) {
Feng Tangffd3d5c2011-10-10 10:31:48 +08002168 err = snd_pcm_open_file(file, pcm, stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (err >= 0)
2170 break;
2171 if (err == -EAGAIN) {
2172 if (file->f_flags & O_NONBLOCK) {
2173 err = -EBUSY;
2174 break;
2175 }
2176 } else
2177 break;
2178 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002179 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002181 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (signal_pending(current)) {
2183 err = -ERESTARTSYS;
2184 break;
2185 }
2186 }
2187 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002188 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (err < 0)
2190 goto __error;
2191 return err;
2192
2193 __error:
2194 module_put(pcm->card->module);
2195 __error2:
2196 snd_card_file_remove(pcm->card, file);
2197 __error1:
2198 return err;
2199}
2200
2201static int snd_pcm_release(struct inode *inode, struct file *file)
2202{
Takashi Iwai877211f2005-11-17 13:59:38 +01002203 struct snd_pcm *pcm;
2204 struct snd_pcm_substream *substream;
2205 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 pcm_file = file->private_data;
2208 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002209 if (snd_BUG_ON(!substream))
2210 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002212 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002213 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002214 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002215 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 wake_up(&pcm->open_wait);
2217 module_put(pcm->card->module);
2218 snd_card_file_remove(pcm->card, file);
2219 return 0;
2220}
2221
Takashi Iwai877211f2005-11-17 13:59:38 +01002222static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2223 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Takashi Iwai877211f2005-11-17 13:59:38 +01002225 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 snd_pcm_sframes_t appl_ptr;
2227 snd_pcm_sframes_t ret;
2228 snd_pcm_sframes_t hw_avail;
2229
2230 if (frames == 0)
2231 return 0;
2232
2233 snd_pcm_stream_lock_irq(substream);
2234 switch (runtime->status->state) {
2235 case SNDRV_PCM_STATE_PREPARED:
2236 break;
2237 case SNDRV_PCM_STATE_DRAINING:
2238 case SNDRV_PCM_STATE_RUNNING:
2239 if (snd_pcm_update_hw_ptr(substream) >= 0)
2240 break;
2241 /* Fall through */
2242 case SNDRV_PCM_STATE_XRUN:
2243 ret = -EPIPE;
2244 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002245 case SNDRV_PCM_STATE_SUSPENDED:
2246 ret = -ESTRPIPE;
2247 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 default:
2249 ret = -EBADFD;
2250 goto __end;
2251 }
2252
2253 hw_avail = snd_pcm_playback_hw_avail(runtime);
2254 if (hw_avail <= 0) {
2255 ret = 0;
2256 goto __end;
2257 }
2258 if (frames > (snd_pcm_uframes_t)hw_avail)
2259 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 appl_ptr = runtime->control->appl_ptr - frames;
2261 if (appl_ptr < 0)
2262 appl_ptr += runtime->boundary;
2263 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 ret = frames;
2265 __end:
2266 snd_pcm_stream_unlock_irq(substream);
2267 return ret;
2268}
2269
Takashi Iwai877211f2005-11-17 13:59:38 +01002270static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2271 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
Takashi Iwai877211f2005-11-17 13:59:38 +01002273 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 snd_pcm_sframes_t appl_ptr;
2275 snd_pcm_sframes_t ret;
2276 snd_pcm_sframes_t hw_avail;
2277
2278 if (frames == 0)
2279 return 0;
2280
2281 snd_pcm_stream_lock_irq(substream);
2282 switch (runtime->status->state) {
2283 case SNDRV_PCM_STATE_PREPARED:
2284 case SNDRV_PCM_STATE_DRAINING:
2285 break;
2286 case SNDRV_PCM_STATE_RUNNING:
2287 if (snd_pcm_update_hw_ptr(substream) >= 0)
2288 break;
2289 /* Fall through */
2290 case SNDRV_PCM_STATE_XRUN:
2291 ret = -EPIPE;
2292 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002293 case SNDRV_PCM_STATE_SUSPENDED:
2294 ret = -ESTRPIPE;
2295 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 default:
2297 ret = -EBADFD;
2298 goto __end;
2299 }
2300
2301 hw_avail = snd_pcm_capture_hw_avail(runtime);
2302 if (hw_avail <= 0) {
2303 ret = 0;
2304 goto __end;
2305 }
2306 if (frames > (snd_pcm_uframes_t)hw_avail)
2307 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 appl_ptr = runtime->control->appl_ptr - frames;
2309 if (appl_ptr < 0)
2310 appl_ptr += runtime->boundary;
2311 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ret = frames;
2313 __end:
2314 snd_pcm_stream_unlock_irq(substream);
2315 return ret;
2316}
2317
Takashi Iwai877211f2005-11-17 13:59:38 +01002318static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2319 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Takashi Iwai877211f2005-11-17 13:59:38 +01002321 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 snd_pcm_sframes_t appl_ptr;
2323 snd_pcm_sframes_t ret;
2324 snd_pcm_sframes_t avail;
2325
2326 if (frames == 0)
2327 return 0;
2328
2329 snd_pcm_stream_lock_irq(substream);
2330 switch (runtime->status->state) {
2331 case SNDRV_PCM_STATE_PREPARED:
2332 case SNDRV_PCM_STATE_PAUSED:
2333 break;
2334 case SNDRV_PCM_STATE_DRAINING:
2335 case SNDRV_PCM_STATE_RUNNING:
2336 if (snd_pcm_update_hw_ptr(substream) >= 0)
2337 break;
2338 /* Fall through */
2339 case SNDRV_PCM_STATE_XRUN:
2340 ret = -EPIPE;
2341 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002342 case SNDRV_PCM_STATE_SUSPENDED:
2343 ret = -ESTRPIPE;
2344 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 default:
2346 ret = -EBADFD;
2347 goto __end;
2348 }
2349
2350 avail = snd_pcm_playback_avail(runtime);
2351 if (avail <= 0) {
2352 ret = 0;
2353 goto __end;
2354 }
2355 if (frames > (snd_pcm_uframes_t)avail)
2356 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 appl_ptr = runtime->control->appl_ptr + frames;
2358 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2359 appl_ptr -= runtime->boundary;
2360 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 ret = frames;
2362 __end:
2363 snd_pcm_stream_unlock_irq(substream);
2364 return ret;
2365}
2366
Takashi Iwai877211f2005-11-17 13:59:38 +01002367static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2368 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Takashi Iwai877211f2005-11-17 13:59:38 +01002370 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 snd_pcm_sframes_t appl_ptr;
2372 snd_pcm_sframes_t ret;
2373 snd_pcm_sframes_t avail;
2374
2375 if (frames == 0)
2376 return 0;
2377
2378 snd_pcm_stream_lock_irq(substream);
2379 switch (runtime->status->state) {
2380 case SNDRV_PCM_STATE_PREPARED:
2381 case SNDRV_PCM_STATE_DRAINING:
2382 case SNDRV_PCM_STATE_PAUSED:
2383 break;
2384 case SNDRV_PCM_STATE_RUNNING:
2385 if (snd_pcm_update_hw_ptr(substream) >= 0)
2386 break;
2387 /* Fall through */
2388 case SNDRV_PCM_STATE_XRUN:
2389 ret = -EPIPE;
2390 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002391 case SNDRV_PCM_STATE_SUSPENDED:
2392 ret = -ESTRPIPE;
2393 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 default:
2395 ret = -EBADFD;
2396 goto __end;
2397 }
2398
2399 avail = snd_pcm_capture_avail(runtime);
2400 if (avail <= 0) {
2401 ret = 0;
2402 goto __end;
2403 }
2404 if (frames > (snd_pcm_uframes_t)avail)
2405 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 appl_ptr = runtime->control->appl_ptr + frames;
2407 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2408 appl_ptr -= runtime->boundary;
2409 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 ret = frames;
2411 __end:
2412 snd_pcm_stream_unlock_irq(substream);
2413 return ret;
2414}
2415
Takashi Iwai877211f2005-11-17 13:59:38 +01002416static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Takashi Iwai877211f2005-11-17 13:59:38 +01002418 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 int err;
2420
2421 snd_pcm_stream_lock_irq(substream);
2422 switch (runtime->status->state) {
2423 case SNDRV_PCM_STATE_DRAINING:
2424 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2425 goto __badfd;
2426 case SNDRV_PCM_STATE_RUNNING:
2427 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2428 break;
2429 /* Fall through */
2430 case SNDRV_PCM_STATE_PREPARED:
2431 case SNDRV_PCM_STATE_SUSPENDED:
2432 err = 0;
2433 break;
2434 case SNDRV_PCM_STATE_XRUN:
2435 err = -EPIPE;
2436 break;
2437 default:
2438 __badfd:
2439 err = -EBADFD;
2440 break;
2441 }
2442 snd_pcm_stream_unlock_irq(substream);
2443 return err;
2444}
2445
Takashi Iwai877211f2005-11-17 13:59:38 +01002446static int snd_pcm_delay(struct snd_pcm_substream *substream,
2447 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448{
Takashi Iwai877211f2005-11-17 13:59:38 +01002449 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int err;
2451 snd_pcm_sframes_t n = 0;
2452
2453 snd_pcm_stream_lock_irq(substream);
2454 switch (runtime->status->state) {
2455 case SNDRV_PCM_STATE_DRAINING:
2456 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2457 goto __badfd;
2458 case SNDRV_PCM_STATE_RUNNING:
2459 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2460 break;
2461 /* Fall through */
2462 case SNDRV_PCM_STATE_PREPARED:
2463 case SNDRV_PCM_STATE_SUSPENDED:
2464 err = 0;
2465 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2466 n = snd_pcm_playback_hw_avail(runtime);
2467 else
2468 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002469 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 break;
2471 case SNDRV_PCM_STATE_XRUN:
2472 err = -EPIPE;
2473 break;
2474 default:
2475 __badfd:
2476 err = -EBADFD;
2477 break;
2478 }
2479 snd_pcm_stream_unlock_irq(substream);
2480 if (!err)
2481 if (put_user(n, res))
2482 err = -EFAULT;
2483 return err;
2484}
2485
Takashi Iwai877211f2005-11-17 13:59:38 +01002486static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2487 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Takashi Iwai877211f2005-11-17 13:59:38 +01002489 struct snd_pcm_runtime *runtime = substream->runtime;
2490 struct snd_pcm_sync_ptr sync_ptr;
2491 volatile struct snd_pcm_mmap_status *status;
2492 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 int err;
Santosh Mardic4fac022012-10-09 10:32:42 +05302494 snd_pcm_uframes_t hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 memset(&sync_ptr, 0, sizeof(sync_ptr));
2497 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2498 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002499 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 -07002500 return -EFAULT;
2501 status = runtime->status;
2502 control = runtime->control;
2503 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2504 err = snd_pcm_hwsync(substream);
2505 if (err < 0)
2506 return err;
2507 }
2508 snd_pcm_stream_lock_irq(substream);
2509 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2510 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2511 else
2512 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2513 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2514 control->avail_min = sync_ptr.c.control.avail_min;
2515 else
2516 sync_ptr.c.control.avail_min = control->avail_min;
Santosh Mardic4fac022012-10-09 10:32:42 +05302517
2518 if (runtime->render_flag & SNDRV_NON_DMA_MODE) {
2519 hw_avail = snd_pcm_playback_hw_avail(runtime);
2520 if ((hw_avail >= runtime->start_threshold)
2521 && (runtime->render_flag &
2522 SNDRV_RENDER_STOPPED)) {
2523 if (substream->ops->restart)
2524 substream->ops->restart(substream);
2525 }
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 sync_ptr.s.status.state = status->state;
2528 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2529 sync_ptr.s.status.tstamp = status->tstamp;
2530 sync_ptr.s.status.suspended_state = status->suspended_state;
2531 snd_pcm_stream_unlock_irq(substream);
2532 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2533 return -EFAULT;
2534 return 0;
2535}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002536
2537static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2538{
2539 struct snd_pcm_runtime *runtime = substream->runtime;
2540 int arg;
2541
2542 if (get_user(arg, _arg))
2543 return -EFAULT;
2544 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2545 return -EINVAL;
2546 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2547 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2548 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2549 return 0;
2550}
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002551
Takashi Iwai0df63e42006-04-28 15:13:41 +02002552static int snd_pcm_common_ioctl1(struct file *file,
2553 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 unsigned int cmd, void __user *arg)
2555{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 switch (cmd) {
2557 case SNDRV_PCM_IOCTL_PVERSION:
2558 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2559 case SNDRV_PCM_IOCTL_INFO:
2560 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002561 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2562 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002563 case SNDRV_PCM_IOCTL_TTSTAMP:
2564 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 case SNDRV_PCM_IOCTL_HW_REFINE:
2566 return snd_pcm_hw_refine_user(substream, arg);
2567 case SNDRV_PCM_IOCTL_HW_PARAMS:
2568 return snd_pcm_hw_params_user(substream, arg);
2569 case SNDRV_PCM_IOCTL_HW_FREE:
2570 return snd_pcm_hw_free(substream);
2571 case SNDRV_PCM_IOCTL_SW_PARAMS:
2572 return snd_pcm_sw_params_user(substream, arg);
2573 case SNDRV_PCM_IOCTL_STATUS:
2574 return snd_pcm_status_user(substream, arg);
2575 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2576 return snd_pcm_channel_info_user(substream, arg);
2577 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002578 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 case SNDRV_PCM_IOCTL_RESET:
2580 return snd_pcm_reset(substream);
2581 case SNDRV_PCM_IOCTL_START:
2582 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2583 case SNDRV_PCM_IOCTL_LINK:
2584 return snd_pcm_link(substream, (int)(unsigned long) arg);
2585 case SNDRV_PCM_IOCTL_UNLINK:
2586 return snd_pcm_unlink(substream);
2587 case SNDRV_PCM_IOCTL_RESUME:
2588 return snd_pcm_resume(substream);
2589 case SNDRV_PCM_IOCTL_XRUN:
2590 return snd_pcm_xrun(substream);
2591 case SNDRV_PCM_IOCTL_HWSYNC:
2592 return snd_pcm_hwsync(substream);
2593 case SNDRV_PCM_IOCTL_DELAY:
2594 return snd_pcm_delay(substream, arg);
2595 case SNDRV_PCM_IOCTL_SYNC_PTR:
2596 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002597#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2599 return snd_pcm_hw_refine_old_user(substream, arg);
2600 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2601 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002604 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 case SNDRV_PCM_IOCTL_DROP:
2606 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002607 case SNDRV_PCM_IOCTL_PAUSE:
2608 {
2609 int res;
2610 snd_pcm_stream_lock_irq(substream);
2611 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2612 snd_pcm_stream_unlock_irq(substream);
2613 return res;
2614 }
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302615 case SNDRV_COMPRESS_GET_CAPS:
2616 case SNDRV_COMPRESS_GET_CODEC_CAPS:
2617 case SNDRV_COMPRESS_SET_PARAMS:
2618 case SNDRV_COMPRESS_GET_PARAMS:
2619 case SNDRV_COMPRESS_TSTAMP:
Krishnankutty Kolathappilly6f52d712012-08-28 15:47:48 -07002620 case SNDRV_COMPRESS_DRAIN:
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302621 return snd_compressed_ioctl(substream, cmd, arg);
Joonwoo Parked3bab02013-02-13 15:59:44 -08002622 default:
2623 if (((cmd >> 8) & 0xff) == 'U')
2624 return snd_user_ioctl(substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626 snd_printd("unknown ioctl = 0x%x\n", cmd);
2627 return -ENOTTY;
2628}
2629
Takashi Iwai0df63e42006-04-28 15:13:41 +02002630static int snd_pcm_playback_ioctl1(struct file *file,
2631 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 unsigned int cmd, void __user *arg)
2633{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002634 if (snd_BUG_ON(!substream))
2635 return -ENXIO;
2636 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2637 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 switch (cmd) {
2639 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2640 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002641 struct snd_xferi xferi;
2642 struct snd_xferi __user *_xferi = arg;
2643 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 snd_pcm_sframes_t result;
2645 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2646 return -EBADFD;
2647 if (put_user(0, &_xferi->result))
2648 return -EFAULT;
2649 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2650 return -EFAULT;
2651 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2652 __put_user(result, &_xferi->result);
2653 return result < 0 ? result : 0;
2654 }
2655 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2656 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002657 struct snd_xfern xfern;
2658 struct snd_xfern __user *_xfern = arg;
2659 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 void __user **bufs;
2661 snd_pcm_sframes_t result;
2662 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2663 return -EBADFD;
2664 if (runtime->channels > 128)
2665 return -EINVAL;
2666 if (put_user(0, &_xfern->result))
2667 return -EFAULT;
2668 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2669 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002670
2671 bufs = memdup_user(xfern.bufs,
2672 sizeof(void *) * runtime->channels);
2673 if (IS_ERR(bufs))
2674 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2676 kfree(bufs);
2677 __put_user(result, &_xfern->result);
2678 return result < 0 ? result : 0;
2679 }
2680 case SNDRV_PCM_IOCTL_REWIND:
2681 {
2682 snd_pcm_uframes_t frames;
2683 snd_pcm_uframes_t __user *_frames = arg;
2684 snd_pcm_sframes_t result;
2685 if (get_user(frames, _frames))
2686 return -EFAULT;
2687 if (put_user(0, _frames))
2688 return -EFAULT;
2689 result = snd_pcm_playback_rewind(substream, frames);
2690 __put_user(result, _frames);
2691 return result < 0 ? result : 0;
2692 }
2693 case SNDRV_PCM_IOCTL_FORWARD:
2694 {
2695 snd_pcm_uframes_t frames;
2696 snd_pcm_uframes_t __user *_frames = arg;
2697 snd_pcm_sframes_t result;
2698 if (get_user(frames, _frames))
2699 return -EFAULT;
2700 if (put_user(0, _frames))
2701 return -EFAULT;
2702 result = snd_pcm_playback_forward(substream, frames);
2703 __put_user(result, _frames);
2704 return result < 0 ? result : 0;
2705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002707 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Takashi Iwai0df63e42006-04-28 15:13:41 +02002710static int snd_pcm_capture_ioctl1(struct file *file,
2711 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 unsigned int cmd, void __user *arg)
2713{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002714 if (snd_BUG_ON(!substream))
2715 return -ENXIO;
2716 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2717 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 switch (cmd) {
2719 case SNDRV_PCM_IOCTL_READI_FRAMES:
2720 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002721 struct snd_xferi xferi;
2722 struct snd_xferi __user *_xferi = arg;
2723 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 snd_pcm_sframes_t result;
2725 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2726 return -EBADFD;
2727 if (put_user(0, &_xferi->result))
2728 return -EFAULT;
2729 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2730 return -EFAULT;
2731 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2732 __put_user(result, &_xferi->result);
2733 return result < 0 ? result : 0;
2734 }
2735 case SNDRV_PCM_IOCTL_READN_FRAMES:
2736 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002737 struct snd_xfern xfern;
2738 struct snd_xfern __user *_xfern = arg;
2739 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 void *bufs;
2741 snd_pcm_sframes_t result;
2742 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2743 return -EBADFD;
2744 if (runtime->channels > 128)
2745 return -EINVAL;
2746 if (put_user(0, &_xfern->result))
2747 return -EFAULT;
2748 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2749 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002750
2751 bufs = memdup_user(xfern.bufs,
2752 sizeof(void *) * runtime->channels);
2753 if (IS_ERR(bufs))
2754 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2756 kfree(bufs);
2757 __put_user(result, &_xfern->result);
2758 return result < 0 ? result : 0;
2759 }
2760 case SNDRV_PCM_IOCTL_REWIND:
2761 {
2762 snd_pcm_uframes_t frames;
2763 snd_pcm_uframes_t __user *_frames = arg;
2764 snd_pcm_sframes_t result;
2765 if (get_user(frames, _frames))
2766 return -EFAULT;
2767 if (put_user(0, _frames))
2768 return -EFAULT;
2769 result = snd_pcm_capture_rewind(substream, frames);
2770 __put_user(result, _frames);
2771 return result < 0 ? result : 0;
2772 }
2773 case SNDRV_PCM_IOCTL_FORWARD:
2774 {
2775 snd_pcm_uframes_t frames;
2776 snd_pcm_uframes_t __user *_frames = arg;
2777 snd_pcm_sframes_t result;
2778 if (get_user(frames, _frames))
2779 return -EFAULT;
2780 if (put_user(0, _frames))
2781 return -EFAULT;
2782 result = snd_pcm_capture_forward(substream, frames);
2783 __put_user(result, _frames);
2784 return result < 0 ? result : 0;
2785 }
2786 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002787 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Takashi Iwai877211f2005-11-17 13:59:38 +01002790static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2791 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Takashi Iwai877211f2005-11-17 13:59:38 +01002793 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 pcm_file = file->private_data;
2796
Asish Bhattacharya9048f752011-11-01 20:33:44 +05302797 if ((((cmd >> 8) & 0xff) != 'A') && (((cmd >> 8) & 0xff) != 'C'))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return -ENOTTY;
2799
Takashi Iwai0df63e42006-04-28 15:13:41 +02002800 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2801 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Takashi Iwai877211f2005-11-17 13:59:38 +01002804static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2805 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
Takashi Iwai877211f2005-11-17 13:59:38 +01002807 struct snd_pcm_file *pcm_file;
Joonwoo Parked3bab02013-02-13 15:59:44 -08002808 unsigned char ioctl_magic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 pcm_file = file->private_data;
Joonwoo Parked3bab02013-02-13 15:59:44 -08002811 ioctl_magic = ((cmd >> 8) & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Joonwoo Parked3bab02013-02-13 15:59:44 -08002813 if (ioctl_magic != 'A' && ioctl_magic != 'C' && ioctl_magic != 'U')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return -ENOTTY;
2815
Takashi Iwai0df63e42006-04-28 15:13:41 +02002816 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2817 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
Takashi Iwai877211f2005-11-17 13:59:38 +01002820int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 unsigned int cmd, void *arg)
2822{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002823 mm_segment_t fs;
2824 int result;
2825
2826 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 switch (substream->stream) {
2828 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002829 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2830 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002833 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2834 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002837 result = -EINVAL;
2838 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002840 snd_leave_user(fs);
2841 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002844EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2845
Takashi Iwai877211f2005-11-17 13:59:38 +01002846static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2847 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
Takashi Iwai877211f2005-11-17 13:59:38 +01002849 struct snd_pcm_file *pcm_file;
2850 struct snd_pcm_substream *substream;
2851 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 snd_pcm_sframes_t result;
2853
2854 pcm_file = file->private_data;
2855 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002856 if (PCM_RUNTIME_CHECK(substream))
2857 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 runtime = substream->runtime;
2859 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2860 return -EBADFD;
2861 if (!frame_aligned(runtime, count))
2862 return -EINVAL;
2863 count = bytes_to_frames(runtime, count);
2864 result = snd_pcm_lib_read(substream, buf, count);
2865 if (result > 0)
2866 result = frames_to_bytes(runtime, result);
2867 return result;
2868}
2869
Takashi Iwai877211f2005-11-17 13:59:38 +01002870static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2871 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Takashi Iwai877211f2005-11-17 13:59:38 +01002873 struct snd_pcm_file *pcm_file;
2874 struct snd_pcm_substream *substream;
2875 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 snd_pcm_sframes_t result;
2877
2878 pcm_file = file->private_data;
2879 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002880 if (PCM_RUNTIME_CHECK(substream))
2881 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002883 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2884 return -EBADFD;
2885 if (!frame_aligned(runtime, count))
2886 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 count = bytes_to_frames(runtime, count);
2888 result = snd_pcm_lib_write(substream, buf, count);
2889 if (result > 0)
2890 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return result;
2892}
2893
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002894static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2895 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897{
Takashi Iwai877211f2005-11-17 13:59:38 +01002898 struct snd_pcm_file *pcm_file;
2899 struct snd_pcm_substream *substream;
2900 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 snd_pcm_sframes_t result;
2902 unsigned long i;
2903 void __user **bufs;
2904 snd_pcm_uframes_t frames;
2905
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002906 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002908 if (PCM_RUNTIME_CHECK(substream))
2909 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 runtime = substream->runtime;
2911 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2912 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002913 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002915 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002917 frames = bytes_to_samples(runtime, iov->iov_len);
2918 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (bufs == NULL)
2920 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002921 for (i = 0; i < nr_segs; ++i)
2922 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 result = snd_pcm_lib_readv(substream, bufs, frames);
2924 if (result > 0)
2925 result = frames_to_bytes(runtime, result);
2926 kfree(bufs);
2927 return result;
2928}
2929
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002930static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2931 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Takashi Iwai877211f2005-11-17 13:59:38 +01002933 struct snd_pcm_file *pcm_file;
2934 struct snd_pcm_substream *substream;
2935 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 snd_pcm_sframes_t result;
2937 unsigned long i;
2938 void __user **bufs;
2939 snd_pcm_uframes_t frames;
2940
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002941 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002943 if (PCM_RUNTIME_CHECK(substream))
2944 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002946 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2947 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002948 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002949 !frame_aligned(runtime, iov->iov_len))
2950 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002951 frames = bytes_to_samples(runtime, iov->iov_len);
2952 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (bufs == NULL)
2954 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002955 for (i = 0; i < nr_segs; ++i)
2956 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 result = snd_pcm_lib_writev(substream, bufs, frames);
2958 if (result > 0)
2959 result = frames_to_bytes(runtime, result);
2960 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return result;
2962}
2963
2964static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2965{
Takashi Iwai877211f2005-11-17 13:59:38 +01002966 struct snd_pcm_file *pcm_file;
2967 struct snd_pcm_substream *substream;
2968 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 unsigned int mask;
2970 snd_pcm_uframes_t avail;
2971
2972 pcm_file = file->private_data;
2973
2974 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002975 if (PCM_RUNTIME_CHECK(substream))
2976 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 runtime = substream->runtime;
2978
2979 poll_wait(file, &runtime->sleep, wait);
2980
2981 snd_pcm_stream_lock_irq(substream);
2982 avail = snd_pcm_playback_avail(runtime);
2983 switch (runtime->status->state) {
2984 case SNDRV_PCM_STATE_RUNNING:
2985 case SNDRV_PCM_STATE_PREPARED:
2986 case SNDRV_PCM_STATE_PAUSED:
2987 if (avail >= runtime->control->avail_min) {
2988 mask = POLLOUT | POLLWRNORM;
2989 break;
2990 }
2991 /* Fall through */
2992 case SNDRV_PCM_STATE_DRAINING:
2993 mask = 0;
2994 break;
2995 default:
2996 mask = POLLOUT | POLLWRNORM | POLLERR;
2997 break;
2998 }
2999 snd_pcm_stream_unlock_irq(substream);
3000 return mask;
3001}
3002
3003static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
3004{
Takashi Iwai877211f2005-11-17 13:59:38 +01003005 struct snd_pcm_file *pcm_file;
3006 struct snd_pcm_substream *substream;
3007 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 unsigned int mask;
3009 snd_pcm_uframes_t avail;
3010
3011 pcm_file = file->private_data;
3012
3013 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003014 if (PCM_RUNTIME_CHECK(substream))
3015 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 runtime = substream->runtime;
3017
3018 poll_wait(file, &runtime->sleep, wait);
3019
3020 snd_pcm_stream_lock_irq(substream);
3021 avail = snd_pcm_capture_avail(runtime);
3022 switch (runtime->status->state) {
3023 case SNDRV_PCM_STATE_RUNNING:
3024 case SNDRV_PCM_STATE_PREPARED:
3025 case SNDRV_PCM_STATE_PAUSED:
3026 if (avail >= runtime->control->avail_min) {
3027 mask = POLLIN | POLLRDNORM;
3028 break;
3029 }
3030 mask = 0;
3031 break;
3032 case SNDRV_PCM_STATE_DRAINING:
3033 if (avail > 0) {
3034 mask = POLLIN | POLLRDNORM;
3035 break;
3036 }
3037 /* Fall through */
3038 default:
3039 mask = POLLIN | POLLRDNORM | POLLERR;
3040 break;
3041 }
3042 snd_pcm_stream_unlock_irq(substream);
3043 return mask;
3044}
3045
3046/*
3047 * mmap support
3048 */
3049
3050/*
3051 * Only on coherent architectures, we can mmap the status and the control records
3052 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3053 */
3054#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3055/*
3056 * mmap status record
3057 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003058static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3059 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Takashi Iwai877211f2005-11-17 13:59:38 +01003061 struct snd_pcm_substream *substream = area->vm_private_data;
3062 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003065 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003067 vmf->page = virt_to_page(runtime->status);
3068 get_page(vmf->page);
3069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003072static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003074 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075};
3076
Takashi Iwai877211f2005-11-17 13:59:38 +01003077static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct vm_area_struct *area)
3079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 long size;
3081 if (!(area->vm_flags & VM_READ))
3082 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003084 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return -EINVAL;
3086 area->vm_ops = &snd_pcm_vm_ops_status;
3087 area->vm_private_data = substream;
3088 area->vm_flags |= VM_RESERVED;
3089 return 0;
3090}
3091
3092/*
3093 * mmap control record
3094 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003095static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3096 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Takashi Iwai877211f2005-11-17 13:59:38 +01003098 struct snd_pcm_substream *substream = area->vm_private_data;
3099 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003102 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104 vmf->page = virt_to_page(runtime->control);
3105 get_page(vmf->page);
3106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003109static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003111 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112};
3113
Takashi Iwai877211f2005-11-17 13:59:38 +01003114static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 struct vm_area_struct *area)
3116{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 long size;
3118 if (!(area->vm_flags & VM_READ))
3119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003121 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return -EINVAL;
3123 area->vm_ops = &snd_pcm_vm_ops_control;
3124 area->vm_private_data = substream;
3125 area->vm_flags |= VM_RESERVED;
3126 return 0;
3127}
3128#else /* ! coherent mmap */
3129/*
3130 * don't support mmap for status and control records.
3131 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003132static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct vm_area_struct *area)
3134{
3135 return -ENXIO;
3136}
Takashi Iwai877211f2005-11-17 13:59:38 +01003137static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 struct vm_area_struct *area)
3139{
3140 return -ENXIO;
3141}
3142#endif /* coherent mmap */
3143
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003144static inline struct page *
3145snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3146{
3147 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003148#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3149 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3150 return virt_to_page(CAC_ADDR(vaddr));
3151#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003152#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3153 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3154 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3155 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3156 /* assume dma_handle set via pfn_to_phys() in
3157 * mm/dma-noncoherent.c
3158 */
3159 return pfn_to_page(addr >> PAGE_SHIFT);
3160 }
3161#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003162 return virt_to_page(vaddr);
3163}
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003166 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003168static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3169 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Takashi Iwai877211f2005-11-17 13:59:38 +01003171 struct snd_pcm_substream *substream = area->vm_private_data;
3172 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 unsigned long offset;
3174 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 size_t dma_bytes;
3176
3177 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003178 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003180 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3182 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003183 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003184 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003186 else
3187 page = snd_pcm_default_page_ops(substream, offset);
3188 if (!page)
3189 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003190 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003191 vmf->page = page;
3192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Takashi Iwai657b1982009-11-26 12:40:21 +01003195static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3196 .open = snd_pcm_mmap_data_open,
3197 .close = snd_pcm_mmap_data_close,
3198};
3199
3200static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 .open = snd_pcm_mmap_data_open,
3202 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003203 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204};
3205
Takashi Iwai657b1982009-11-26 12:40:21 +01003206#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3207/* This should be defined / handled globally! */
3208#ifdef CONFIG_ARM
3209#define ARCH_HAS_DMA_MMAP_COHERENT
3210#endif
3211#endif
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213/*
3214 * mmap the DMA buffer on RAM
3215 */
Takashi Iwai18a2b962011-09-28 17:12:59 +02003216int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
3217 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003220#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3221 if (!substream->ops->page &&
3222 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3223 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3224 area,
3225 substream->runtime->dma_area,
3226 substream->runtime->dma_addr,
3227 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003228#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3229 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3230 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3231 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003232#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3233 /* mmap with fault handler */
3234 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 0;
3236}
Takashi Iwai18a2b962011-09-28 17:12:59 +02003237EXPORT_SYMBOL_GPL(snd_pcm_lib_default_mmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239/*
3240 * mmap the DMA buffer on I/O memory area
3241 */
3242#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003243int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3244 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
3246 long size;
3247 unsigned long offset;
3248
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 area->vm_flags |= VM_IO;
3251 size = area->vm_end - area->vm_start;
3252 offset = area->vm_pgoff << PAGE_SHIFT;
3253 if (io_remap_pfn_range(area, area->vm_start,
3254 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3255 size, area->vm_page_prot))
3256 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return 0;
3258}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003259
3260EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261#endif /* SNDRV_PCM_INFO_MMAP */
3262
3263/*
3264 * mmap DMA buffer
3265 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003266int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 struct vm_area_struct *area)
3268{
Takashi Iwai877211f2005-11-17 13:59:38 +01003269 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 long size;
3271 unsigned long offset;
3272 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003273 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3276 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3277 return -EINVAL;
3278 } else {
3279 if (!(area->vm_flags & VM_READ))
3280 return -EINVAL;
3281 }
3282 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3284 return -EBADFD;
3285 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3286 return -ENXIO;
3287 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3288 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3289 return -EINVAL;
3290 size = area->vm_end - area->vm_start;
3291 offset = area->vm_pgoff << PAGE_SHIFT;
3292 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3293 if ((size_t)size > dma_bytes)
3294 return -EINVAL;
3295 if (offset > dma_bytes - size)
3296 return -EINVAL;
3297
Takashi Iwai657b1982009-11-26 12:40:21 +01003298 area->vm_ops = &snd_pcm_vm_ops_data;
3299 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003301 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 else
Takashi Iwai18a2b962011-09-28 17:12:59 +02003303 err = snd_pcm_lib_default_mmap(substream, area);
Takashi Iwai657b1982009-11-26 12:40:21 +01003304 if (!err)
3305 atomic_inc(&substream->mmap_count);
3306 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003309EXPORT_SYMBOL(snd_pcm_mmap_data);
3310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3312{
Takashi Iwai877211f2005-11-17 13:59:38 +01003313 struct snd_pcm_file * pcm_file;
3314 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 unsigned long offset;
3316
3317 pcm_file = file->private_data;
3318 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003319 if (PCM_RUNTIME_CHECK(substream))
3320 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 offset = area->vm_pgoff << PAGE_SHIFT;
3323 switch (offset) {
3324 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003325 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 return -ENXIO;
3327 return snd_pcm_mmap_status(substream, file, area);
3328 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003329 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return -ENXIO;
3331 return snd_pcm_mmap_control(substream, file, area);
3332 default:
3333 return snd_pcm_mmap_data(substream, file, area);
3334 }
3335 return 0;
3336}
3337
3338static int snd_pcm_fasync(int fd, struct file * file, int on)
3339{
Takashi Iwai877211f2005-11-17 13:59:38 +01003340 struct snd_pcm_file * pcm_file;
3341 struct snd_pcm_substream *substream;
3342 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
3344 pcm_file = file->private_data;
3345 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003346 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003347 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003349 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
3352/*
3353 * ioctl32 compat
3354 */
3355#ifdef CONFIG_COMPAT
3356#include "pcm_compat.c"
3357#else
3358#define snd_pcm_ioctl_compat NULL
3359#endif
3360
3361/*
3362 * To be removed helpers to keep binary compatibility
3363 */
3364
Takashi Iwai59d48582005-12-01 10:51:58 +01003365#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3367#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3368
Takashi Iwai877211f2005-11-17 13:59:38 +01003369static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3370 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 unsigned int i;
3373
3374 memset(params, 0, sizeof(*params));
3375 params->flags = oparams->flags;
3376 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3377 params->masks[i].bits[0] = oparams->masks[i];
3378 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3379 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3380 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3381 params->info = oparams->info;
3382 params->msbits = oparams->msbits;
3383 params->rate_num = oparams->rate_num;
3384 params->rate_den = oparams->rate_den;
3385 params->fifo_size = oparams->fifo_size;
3386}
3387
Takashi Iwai877211f2005-11-17 13:59:38 +01003388static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3389 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 unsigned int i;
3392
3393 memset(oparams, 0, sizeof(*oparams));
3394 oparams->flags = params->flags;
3395 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3396 oparams->masks[i] = params->masks[i].bits[0];
3397 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3398 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3399 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3400 oparams->info = params->info;
3401 oparams->msbits = params->msbits;
3402 oparams->rate_num = params->rate_num;
3403 oparams->rate_den = params->rate_den;
3404 oparams->fifo_size = params->fifo_size;
3405}
3406
Takashi Iwai877211f2005-11-17 13:59:38 +01003407static int snd_pcm_hw_refine_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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Li Zefanef44a1e2009-04-10 09:43:08 +08003418 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_refine(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}
3436
Takashi Iwai877211f2005-11-17 13:59:38 +01003437static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3438 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Takashi Iwai877211f2005-11-17 13:59:38 +01003440 struct snd_pcm_hw_params *params;
3441 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 int err;
3443
3444 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003445 if (!params)
3446 return -ENOMEM;
3447
3448 oparams = memdup_user(_oparams, sizeof(*oparams));
3449 if (IS_ERR(oparams)) {
3450 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 goto out;
3452 }
3453 snd_pcm_hw_convert_from_old_params(params, oparams);
3454 err = snd_pcm_hw_params(substream, params);
3455 snd_pcm_hw_convert_to_old_params(oparams, params);
3456 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3457 if (!err)
3458 err = -EFAULT;
3459 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003460
3461 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462out:
3463 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return err;
3465}
Takashi Iwai59d48582005-12-01 10:51:58 +01003466#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
Cliff Cai70036092008-09-03 10:54:36 +02003468#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003469static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3470 unsigned long addr,
3471 unsigned long len,
3472 unsigned long pgoff,
3473 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003474{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003475 struct snd_pcm_file *pcm_file = file->private_data;
3476 struct snd_pcm_substream *substream = pcm_file->substream;
3477 struct snd_pcm_runtime *runtime = substream->runtime;
3478 unsigned long offset = pgoff << PAGE_SHIFT;
3479
3480 switch (offset) {
3481 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3482 return (unsigned long)runtime->status;
3483 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3484 return (unsigned long)runtime->control;
3485 default:
3486 return (unsigned long)runtime->dma_area + offset;
3487 }
Cliff Cai70036092008-09-03 10:54:36 +02003488}
3489#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003490# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003491#endif
3492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493/*
3494 * Register section
3495 */
3496
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003497const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003499 .owner = THIS_MODULE,
3500 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003501 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003502 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003503 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003504 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003505 .poll = snd_pcm_playback_poll,
3506 .unlocked_ioctl = snd_pcm_playback_ioctl,
3507 .compat_ioctl = snd_pcm_ioctl_compat,
3508 .mmap = snd_pcm_mmap,
3509 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003510 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 },
3512 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003513 .owner = THIS_MODULE,
3514 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003515 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003516 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003517 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003518 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003519 .poll = snd_pcm_capture_poll,
3520 .unlocked_ioctl = snd_pcm_capture_ioctl,
3521 .compat_ioctl = snd_pcm_ioctl_compat,
3522 .mmap = snd_pcm_mmap,
3523 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003524 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
3526};