blob: 8e022fc70f5524031b437b4dfc4dce7a9ca416bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/file.h>
24#include <linux/slab.h>
Jonathan Corbet2db9f0a2008-06-23 17:40:43 -060025#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/time.h>
Mark Grossf011e2e2008-02-04 22:30:09 -080027#include <linux/pm_qos_params.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
Takashi Iwai657b1982009-11-26 12:40:21 +010029#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <sound/core.h>
31#include <sound/control.h>
32#include <sound/info.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/timer.h>
36#include <sound/minors.h>
37#include <asm/io.h>
Takashi Iwai9fe17b52010-05-12 10:32:42 +020038#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
39#include <dma-coherence.h>
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Compatibility
44 */
45
Takashi Iwai877211f2005-11-17 13:59:38 +010046struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned int flags;
48 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
49 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010050 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
52 unsigned int rmask;
53 unsigned int cmask;
54 unsigned int info;
55 unsigned int msbits;
56 unsigned int rate_num;
57 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010058 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned char reserved[64];
60};
61
Takashi Iwai59d48582005-12-01 10:51:58 +010062#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010063#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
64#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Takashi Iwai877211f2005-11-17 13:59:38 +010066static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
67 struct snd_pcm_hw_params_old __user * _oparams);
68static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
69 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010070#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010071static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73/*
74 *
75 */
76
77DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020078EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020080static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82static inline mm_segment_t snd_enter_user(void)
83{
84 mm_segment_t fs = get_fs();
85 set_fs(get_ds());
86 return fs;
87}
88
89static inline void snd_leave_user(mm_segment_t fs)
90{
91 set_fs(fs);
92}
93
94
95
Takashi Iwai877211f2005-11-17 13:59:38 +010096int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Takashi Iwai877211f2005-11-17 13:59:38 +010098 struct snd_pcm_runtime *runtime;
99 struct snd_pcm *pcm = substream->pcm;
100 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 memset(info, 0, sizeof(*info));
103 info->card = pcm->card->number;
104 info->device = pcm->device;
105 info->stream = substream->stream;
106 info->subdevice = substream->number;
107 strlcpy(info->id, pcm->id, sizeof(info->id));
108 strlcpy(info->name, pcm->name, sizeof(info->name));
109 info->dev_class = pcm->dev_class;
110 info->dev_subclass = pcm->dev_subclass;
111 info->subdevices_count = pstr->substream_count;
112 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
113 strlcpy(info->subname, substream->name, sizeof(info->subname));
114 runtime = substream->runtime;
115 /* AB: FIXME!!! This is definitely nonsense */
116 if (runtime) {
117 info->sync = runtime->sync;
118 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
119 }
120 return 0;
121}
122
Takashi Iwai877211f2005-11-17 13:59:38 +0100123int snd_pcm_info_user(struct snd_pcm_substream *substream,
124 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Takashi Iwai877211f2005-11-17 13:59:38 +0100126 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int err;
128
129 info = kmalloc(sizeof(*info), GFP_KERNEL);
130 if (! info)
131 return -ENOMEM;
132 err = snd_pcm_info(substream, info);
133 if (err >= 0) {
134 if (copy_to_user(_info, info, sizeof(*info)))
135 err = -EFAULT;
136 }
137 kfree(info);
138 return err;
139}
140
141#undef RULES_DEBUG
142
143#ifdef RULES_DEBUG
144#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
Joe Perches47023ec2010-09-13 21:24:02 -0700145static const char * const snd_pcm_hw_param_names[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 HW_PARAM(ACCESS),
147 HW_PARAM(FORMAT),
148 HW_PARAM(SUBFORMAT),
149 HW_PARAM(SAMPLE_BITS),
150 HW_PARAM(FRAME_BITS),
151 HW_PARAM(CHANNELS),
152 HW_PARAM(RATE),
153 HW_PARAM(PERIOD_TIME),
154 HW_PARAM(PERIOD_SIZE),
155 HW_PARAM(PERIOD_BYTES),
156 HW_PARAM(PERIODS),
157 HW_PARAM(BUFFER_TIME),
158 HW_PARAM(BUFFER_SIZE),
159 HW_PARAM(BUFFER_BYTES),
160 HW_PARAM(TICK_TIME),
161};
162#endif
163
Takashi Iwai877211f2005-11-17 13:59:38 +0100164int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
165 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100168 struct snd_pcm_hardware *hw;
169 struct snd_interval *i = NULL;
170 struct snd_mask *m = NULL;
171 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 unsigned int rstamps[constrs->rules_num];
173 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
174 unsigned int stamp = 2;
175 int changed, again;
176
177 params->info = 0;
178 params->fifo_size = 0;
179 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
180 params->msbits = 0;
181 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
182 params->rate_num = 0;
183 params->rate_den = 0;
184 }
185
186 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
187 m = hw_param_mask(params, k);
188 if (snd_mask_empty(m))
189 return -EINVAL;
190 if (!(params->rmask & (1 << k)))
191 continue;
192#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100193 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 changed = snd_mask_refine(m, constrs_mask(constrs, k));
197#ifdef RULES_DEBUG
198 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
199#endif
200 if (changed)
201 params->cmask |= 1 << k;
202 if (changed < 0)
203 return changed;
204 }
205
206 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
207 i = hw_param_interval(params, k);
208 if (snd_interval_empty(i))
209 return -EINVAL;
210 if (!(params->rmask & (1 << k)))
211 continue;
212#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100213 printk(KERN_DEBUG "%s = ", snd_pcm_hw_param_names[k]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (i->empty)
215 printk("empty");
216 else
217 printk("%c%u %u%c",
218 i->openmin ? '(' : '[', i->min,
219 i->max, i->openmax ? ')' : ']');
220 printk(" -> ");
221#endif
222 changed = snd_interval_refine(i, constrs_interval(constrs, k));
223#ifdef RULES_DEBUG
224 if (i->empty)
225 printk("empty\n");
226 else
227 printk("%c%u %u%c\n",
228 i->openmin ? '(' : '[', i->min,
229 i->max, i->openmax ? ')' : ']');
230#endif
231 if (changed)
232 params->cmask |= 1 << k;
233 if (changed < 0)
234 return changed;
235 }
236
237 for (k = 0; k < constrs->rules_num; k++)
238 rstamps[k] = 0;
239 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
240 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
241 do {
242 again = 0;
243 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100244 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int d;
246 int doit = 0;
247 if (r->cond && !(r->cond & params->flags))
248 continue;
249 for (d = 0; r->deps[d] >= 0; d++) {
250 if (vstamps[r->deps[d]] > rstamps[k]) {
251 doit = 1;
252 break;
253 }
254 }
255 if (!doit)
256 continue;
257#ifdef RULES_DEBUG
Takashi Iwai006de2672009-02-05 15:51:04 +0100258 printk(KERN_DEBUG "Rule %d [%p]: ", k, r->func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (r->var >= 0) {
260 printk("%s = ", snd_pcm_hw_param_names[r->var]);
261 if (hw_is_mask(r->var)) {
262 m = hw_param_mask(params, r->var);
263 printk("%x", *m->bits);
264 } else {
265 i = hw_param_interval(params, r->var);
266 if (i->empty)
267 printk("empty");
268 else
269 printk("%c%u %u%c",
270 i->openmin ? '(' : '[', i->min,
271 i->max, i->openmax ? ')' : ']');
272 }
273 }
274#endif
275 changed = r->func(params, r);
276#ifdef RULES_DEBUG
277 if (r->var >= 0) {
278 printk(" -> ");
279 if (hw_is_mask(r->var))
280 printk("%x", *m->bits);
281 else {
282 if (i->empty)
283 printk("empty");
284 else
285 printk("%c%u %u%c",
286 i->openmin ? '(' : '[', i->min,
287 i->max, i->openmax ? ')' : ']');
288 }
289 }
290 printk("\n");
291#endif
292 rstamps[k] = stamp;
293 if (changed && r->var >= 0) {
294 params->cmask |= (1 << r->var);
295 vstamps[r->var] = stamp;
296 again = 1;
297 }
298 if (changed < 0)
299 return changed;
300 stamp++;
301 }
302 } while (again);
303 if (!params->msbits) {
304 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
305 if (snd_interval_single(i))
306 params->msbits = snd_interval_value(i);
307 }
308
309 if (!params->rate_den) {
310 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
311 if (snd_interval_single(i)) {
312 params->rate_num = snd_interval_value(i);
313 params->rate_den = 1;
314 }
315 }
316
317 hw = &substream->runtime->hw;
318 if (!params->info)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200319 params->info = hw->info & ~SNDRV_PCM_INFO_FIFO_IN_FRAMES;
320 if (!params->fifo_size) {
Jaroslav Kysela3be522a2010-02-16 11:55:43 +0100321 m = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
322 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
323 if (snd_mask_min(m) == snd_mask_max(m) &&
324 snd_interval_min(i) == snd_interval_max(i)) {
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200325 changed = substream->ops->ioctl(substream,
326 SNDRV_PCM_IOCTL1_FIFO_SIZE, params);
Mariusz Kozlowski8bd9bca2009-06-21 20:26:59 +0200327 if (changed < 0)
Jaroslav Kysela8bea8692009-04-27 09:44:40 +0200328 return changed;
329 }
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 params->rmask = 0;
332 return 0;
333}
334
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200335EXPORT_SYMBOL(snd_pcm_hw_refine);
336
Takashi Iwai877211f2005-11-17 13:59:38 +0100337static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
338 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Takashi Iwai877211f2005-11-17 13:59:38 +0100340 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 int err;
342
Li Zefanef44a1e2009-04-10 09:43:08 +0800343 params = memdup_user(_params, sizeof(*params));
344 if (IS_ERR(params))
345 return PTR_ERR(params);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 err = snd_pcm_hw_refine(substream, params);
348 if (copy_to_user(_params, params, sizeof(*params))) {
349 if (!err)
350 err = -EFAULT;
351 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 kfree(params);
354 return err;
355}
356
Takashi Iwai9442e692006-09-30 23:27:19 -0700357static int period_to_usecs(struct snd_pcm_runtime *runtime)
358{
359 int usecs;
360
361 if (! runtime->rate)
362 return -1; /* invalid */
363
364 /* take 75% of period time as the deadline */
365 usecs = (750000 / runtime->rate) * runtime->period_size;
366 usecs += ((750000 % runtime->rate) * runtime->period_size) /
367 runtime->rate;
368
369 return usecs;
370}
371
Takashi Iwai877211f2005-11-17 13:59:38 +0100372static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
373 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Takashi Iwai877211f2005-11-17 13:59:38 +0100375 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700376 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned int bits;
378 snd_pcm_uframes_t frames;
379
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200380 if (PCM_RUNTIME_CHECK(substream))
381 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_pcm_stream_lock_irq(substream);
384 switch (runtime->status->state) {
385 case SNDRV_PCM_STATE_OPEN:
386 case SNDRV_PCM_STATE_SETUP:
387 case SNDRV_PCM_STATE_PREPARED:
388 break;
389 default:
390 snd_pcm_stream_unlock_irq(substream);
391 return -EBADFD;
392 }
393 snd_pcm_stream_unlock_irq(substream);
394#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
395 if (!substream->oss.oss)
396#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200397 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EBADFD;
399
400 params->rmask = ~0U;
401 err = snd_pcm_hw_refine(substream, params);
402 if (err < 0)
403 goto _error;
404
405 err = snd_pcm_hw_params_choose(substream, params);
406 if (err < 0)
407 goto _error;
408
409 if (substream->ops->hw_params != NULL) {
410 err = substream->ops->hw_params(substream, params);
411 if (err < 0)
412 goto _error;
413 }
414
415 runtime->access = params_access(params);
416 runtime->format = params_format(params);
417 runtime->subformat = params_subformat(params);
418 runtime->channels = params_channels(params);
419 runtime->rate = params_rate(params);
420 runtime->period_size = params_period_size(params);
421 runtime->periods = params_periods(params);
422 runtime->buffer_size = params_buffer_size(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 runtime->info = params->info;
424 runtime->rate_num = params->rate_num;
425 runtime->rate_den = params->rate_den;
426
427 bits = snd_pcm_format_physical_width(runtime->format);
428 runtime->sample_bits = bits;
429 bits *= runtime->channels;
430 runtime->frame_bits = bits;
431 frames = 1;
432 while (bits % 8 != 0) {
433 bits *= 2;
434 frames *= 2;
435 }
436 runtime->byte_align = bits / 8;
437 runtime->min_align = frames;
438
439 /* Default sw params */
440 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
441 runtime->period_step = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 runtime->control->avail_min = runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 runtime->start_threshold = 1;
444 runtime->stop_threshold = runtime->buffer_size;
445 runtime->silence_threshold = 0;
446 runtime->silence_size = 0;
Clemens Ladischead40462010-05-21 09:15:59 +0200447 runtime->boundary = runtime->buffer_size;
448 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
449 runtime->boundary *= 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 snd_pcm_timer_resolution_change(substream);
452 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700453
James Bottomley82f68252010-07-05 22:53:06 +0200454 if (pm_qos_request_active(&substream->latency_pm_qos_req))
455 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai9442e692006-09-30 23:27:19 -0700456 if ((usecs = period_to_usecs(runtime)) >= 0)
James Bottomley82f68252010-07-05 22:53:06 +0200457 pm_qos_add_request(&substream->latency_pm_qos_req,
458 PM_QOS_CPU_DMA_LATENCY, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460 _error:
461 /* hardware might be unuseable from this time,
462 so we force application to retry to set
463 the correct hardware parameter settings */
464 runtime->status->state = SNDRV_PCM_STATE_OPEN;
465 if (substream->ops->hw_free != NULL)
466 substream->ops->hw_free(substream);
467 return err;
468}
469
Takashi Iwai877211f2005-11-17 13:59:38 +0100470static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Takashi Iwai877211f2005-11-17 13:59:38 +0100473 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int err;
475
Li Zefanef44a1e2009-04-10 09:43:08 +0800476 params = memdup_user(_params, sizeof(*params));
477 if (IS_ERR(params))
478 return PTR_ERR(params);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 err = snd_pcm_hw_params(substream, params);
481 if (copy_to_user(_params, params, sizeof(*params))) {
482 if (!err)
483 err = -EFAULT;
484 }
Li Zefanef44a1e2009-04-10 09:43:08 +0800485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 kfree(params);
487 return err;
488}
489
Takashi Iwai877211f2005-11-17 13:59:38 +0100490static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Takashi Iwai877211f2005-11-17 13:59:38 +0100492 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int result = 0;
494
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200495 if (PCM_RUNTIME_CHECK(substream))
496 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 snd_pcm_stream_lock_irq(substream);
499 switch (runtime->status->state) {
500 case SNDRV_PCM_STATE_SETUP:
501 case SNDRV_PCM_STATE_PREPARED:
502 break;
503 default:
504 snd_pcm_stream_unlock_irq(substream);
505 return -EBADFD;
506 }
507 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200508 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return -EBADFD;
510 if (substream->ops->hw_free)
511 result = substream->ops->hw_free(substream);
512 runtime->status->state = SNDRV_PCM_STATE_OPEN;
James Bottomley82f68252010-07-05 22:53:06 +0200513 pm_qos_remove_request(&substream->latency_pm_qos_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return result;
515}
516
Takashi Iwai877211f2005-11-17 13:59:38 +0100517static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
518 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Takashi Iwai877211f2005-11-17 13:59:38 +0100520 struct snd_pcm_runtime *runtime;
Jaroslav Kysela12509322010-01-07 15:36:31 +0100521 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Takashi Iwai7eaa9432008-08-08 17:09:09 +0200523 if (PCM_RUNTIME_CHECK(substream))
524 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 snd_pcm_stream_lock_irq(substream);
527 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
528 snd_pcm_stream_unlock_irq(substream);
529 return -EBADFD;
530 }
531 snd_pcm_stream_unlock_irq(substream);
532
533 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
534 return -EINVAL;
535 if (params->avail_min == 0)
536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (params->silence_size >= runtime->boundary) {
538 if (params->silence_threshold != 0)
539 return -EINVAL;
540 } else {
541 if (params->silence_size > params->silence_threshold)
542 return -EINVAL;
543 if (params->silence_threshold > runtime->buffer_size)
544 return -EINVAL;
545 }
Jaroslav Kysela12509322010-01-07 15:36:31 +0100546 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 snd_pcm_stream_lock_irq(substream);
548 runtime->tstamp_mode = params->tstamp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 runtime->period_step = params->period_step;
550 runtime->control->avail_min = params->avail_min;
551 runtime->start_threshold = params->start_threshold;
552 runtime->stop_threshold = params->stop_threshold;
553 runtime->silence_threshold = params->silence_threshold;
554 runtime->silence_size = params->silence_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 params->boundary = runtime->boundary;
556 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
558 runtime->silence_size > 0)
559 snd_pcm_playback_silence(substream, ULONG_MAX);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100560 err = snd_pcm_update_state(substream, runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 snd_pcm_stream_unlock_irq(substream);
Jaroslav Kysela12509322010-01-07 15:36:31 +0100563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Takashi Iwai877211f2005-11-17 13:59:38 +0100566static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
567 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Takashi Iwai877211f2005-11-17 13:59:38 +0100569 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 int err;
571 if (copy_from_user(&params, _params, sizeof(params)))
572 return -EFAULT;
573 err = snd_pcm_sw_params(substream, &params);
574 if (copy_to_user(_params, &params, sizeof(params)))
575 return -EFAULT;
576 return err;
577}
578
Takashi Iwai877211f2005-11-17 13:59:38 +0100579int snd_pcm_status(struct snd_pcm_substream *substream,
580 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwai877211f2005-11-17 13:59:38 +0100582 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 snd_pcm_stream_lock_irq(substream);
585 status->state = runtime->status->state;
586 status->suspended_state = runtime->status->suspended_state;
587 if (status->state == SNDRV_PCM_STATE_OPEN)
588 goto _end;
589 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100590 if (snd_pcm_running(substream)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 snd_pcm_update_hw_ptr(substream);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100592 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE) {
593 status->tstamp = runtime->status->tstamp;
594 goto _tstamp_end;
595 }
596 }
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100597 snd_pcm_gettime(runtime, &status->tstamp);
Jaroslav Kysela8c121582008-01-11 08:45:08 +0100598 _tstamp_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 status->appl_ptr = runtime->control->appl_ptr;
600 status->hw_ptr = runtime->status->hw_ptr;
601 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
602 status->avail = snd_pcm_playback_avail(runtime);
603 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200604 runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 status->delay = runtime->buffer_size - status->avail;
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200606 status->delay += runtime->delay;
607 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 status->delay = 0;
609 } else {
610 status->avail = snd_pcm_capture_avail(runtime);
611 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +0200612 status->delay = status->avail + runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 else
614 status->delay = 0;
615 }
616 status->avail_max = runtime->avail_max;
617 status->overrange = runtime->overrange;
618 runtime->avail_max = 0;
619 runtime->overrange = 0;
620 _end:
621 snd_pcm_stream_unlock_irq(substream);
622 return 0;
623}
624
Takashi Iwai877211f2005-11-17 13:59:38 +0100625static int snd_pcm_status_user(struct snd_pcm_substream *substream,
626 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Takashi Iwai877211f2005-11-17 13:59:38 +0100628 struct snd_pcm_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int res;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 memset(&status, 0, sizeof(status));
632 res = snd_pcm_status(substream, &status);
633 if (res < 0)
634 return res;
635 if (copy_to_user(_status, &status, sizeof(status)))
636 return -EFAULT;
637 return 0;
638}
639
Takashi Iwai877211f2005-11-17 13:59:38 +0100640static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
641 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Takashi Iwai877211f2005-11-17 13:59:38 +0100643 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 unsigned int channel;
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 channel = info->channel;
647 runtime = substream->runtime;
648 snd_pcm_stream_lock_irq(substream);
649 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
650 snd_pcm_stream_unlock_irq(substream);
651 return -EBADFD;
652 }
653 snd_pcm_stream_unlock_irq(substream);
654 if (channel >= runtime->channels)
655 return -EINVAL;
656 memset(info, 0, sizeof(*info));
657 info->channel = channel;
658 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
659}
660
Takashi Iwai877211f2005-11-17 13:59:38 +0100661static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
662 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai877211f2005-11-17 13:59:38 +0100664 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 int res;
666
667 if (copy_from_user(&info, _info, sizeof(info)))
668 return -EFAULT;
669 res = snd_pcm_channel_info(substream, &info);
670 if (res < 0)
671 return res;
672 if (copy_to_user(_info, &info, sizeof(info)))
673 return -EFAULT;
674 return 0;
675}
676
Takashi Iwai877211f2005-11-17 13:59:38 +0100677static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Takashi Iwai877211f2005-11-17 13:59:38 +0100679 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (runtime->trigger_master == NULL)
681 return;
682 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100683 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 } else {
685 snd_pcm_trigger_tstamp(runtime->trigger_master);
686 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
687 }
688 runtime->trigger_master = NULL;
689}
690
691struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100692 int (*pre_action)(struct snd_pcm_substream *substream, int state);
693 int (*do_action)(struct snd_pcm_substream *substream, int state);
694 void (*undo_action)(struct snd_pcm_substream *substream, int state);
695 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696};
697
698/*
699 * this functions is core for handling of linked stream
700 * Note: the stream state might be changed also on failure
701 * Note2: call with calling stream lock + link lock
702 */
703static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100704 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int state, int do_lock)
706{
Takashi Iwai877211f2005-11-17 13:59:38 +0100707 struct snd_pcm_substream *s = NULL;
708 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 int res = 0;
710
Takashi Iwaief991b92007-02-22 12:52:53 +0100711 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200713 spin_lock_nested(&s->self_group.lock,
714 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 res = ops->pre_action(s, state);
716 if (res < 0)
717 goto _unlock;
718 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100719 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 res = ops->do_action(s, state);
721 if (res < 0) {
722 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100723 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (s1 == s) /* failed stream */
725 break;
726 ops->undo_action(s1, state);
727 }
728 }
729 s = NULL; /* unlock all */
730 goto _unlock;
731 }
732 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100733 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ops->post_action(s, state);
735 }
736 _unlock:
737 if (do_lock) {
738 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100739 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (s1 != substream)
741 spin_unlock(&s1->self_group.lock);
742 if (s1 == s) /* end */
743 break;
744 }
745 }
746 return res;
747}
748
749/*
750 * Note: call with stream lock
751 */
752static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100753 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 int state)
755{
756 int res;
757
758 res = ops->pre_action(substream, state);
759 if (res < 0)
760 return res;
761 res = ops->do_action(substream, state);
762 if (res == 0)
763 ops->post_action(substream, state);
764 else if (ops->undo_action)
765 ops->undo_action(substream, state);
766 return res;
767}
768
769/*
770 * Note: call with stream lock
771 */
772static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100773 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 int state)
775{
776 int res;
777
778 if (snd_pcm_stream_linked(substream)) {
779 if (!spin_trylock(&substream->group->lock)) {
780 spin_unlock(&substream->self_group.lock);
781 spin_lock(&substream->group->lock);
782 spin_lock(&substream->self_group.lock);
783 }
784 res = snd_pcm_action_group(ops, substream, state, 1);
785 spin_unlock(&substream->group->lock);
786 } else {
787 res = snd_pcm_action_single(ops, substream, state);
788 }
789 return res;
790}
791
792/*
793 * Note: don't use any locks before
794 */
795static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100796 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 int state)
798{
799 int res;
800
801 read_lock_irq(&snd_pcm_link_rwlock);
802 if (snd_pcm_stream_linked(substream)) {
803 spin_lock(&substream->group->lock);
804 spin_lock(&substream->self_group.lock);
805 res = snd_pcm_action_group(ops, substream, state, 1);
806 spin_unlock(&substream->self_group.lock);
807 spin_unlock(&substream->group->lock);
808 } else {
809 spin_lock(&substream->self_group.lock);
810 res = snd_pcm_action_single(ops, substream, state);
811 spin_unlock(&substream->self_group.lock);
812 }
813 read_unlock_irq(&snd_pcm_link_rwlock);
814 return res;
815}
816
817/*
818 */
819static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100820 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 int state)
822{
823 int res;
824
825 down_read(&snd_pcm_link_rwsem);
826 if (snd_pcm_stream_linked(substream))
827 res = snd_pcm_action_group(ops, substream, state, 0);
828 else
829 res = snd_pcm_action_single(ops, substream, state);
830 up_read(&snd_pcm_link_rwsem);
831 return res;
832}
833
834/*
835 * start callbacks
836 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100837static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Takashi Iwai877211f2005-11-17 13:59:38 +0100839 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
841 return -EBADFD;
842 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
843 !snd_pcm_playback_data(substream))
844 return -EPIPE;
845 runtime->trigger_master = substream;
846 return 0;
847}
848
Takashi Iwai877211f2005-11-17 13:59:38 +0100849static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 if (substream->runtime->trigger_master != substream)
852 return 0;
853 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
854}
855
Takashi Iwai877211f2005-11-17 13:59:38 +0100856static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 if (substream->runtime->trigger_master == substream)
859 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
860}
861
Takashi Iwai877211f2005-11-17 13:59:38 +0100862static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Takashi Iwai877211f2005-11-17 13:59:38 +0100864 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 snd_pcm_trigger_tstamp(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200866 runtime->hw_ptr_jiffies = jiffies;
Jaroslav Kyselabd76af02010-08-18 14:16:54 +0200867 runtime->hw_ptr_buffer_jiffies = (runtime->buffer_size * HZ) /
868 runtime->rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 runtime->status->state = state;
870 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
871 runtime->silence_size > 0)
872 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100874 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
875 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878static struct action_ops snd_pcm_action_start = {
879 .pre_action = snd_pcm_pre_start,
880 .do_action = snd_pcm_do_start,
881 .undo_action = snd_pcm_undo_start,
882 .post_action = snd_pcm_post_start
883};
884
885/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700886 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200887 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100889int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Takashi Iwai877211f2005-11-17 13:59:38 +0100891 return snd_pcm_action(&snd_pcm_action_start, substream,
892 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * stop callbacks
897 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100898static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Takashi Iwai877211f2005-11-17 13:59:38 +0100900 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
902 return -EBADFD;
903 runtime->trigger_master = substream;
904 return 0;
905}
906
Takashi Iwai877211f2005-11-17 13:59:38 +0100907static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 if (substream->runtime->trigger_master == substream &&
910 snd_pcm_running(substream))
911 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
912 return 0; /* unconditonally stop all substreams */
913}
914
Takashi Iwai877211f2005-11-17 13:59:38 +0100915static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Takashi Iwai877211f2005-11-17 13:59:38 +0100917 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (runtime->status->state != state) {
919 snd_pcm_trigger_tstamp(substream);
920 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
922 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
925 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100926 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929static struct action_ops snd_pcm_action_stop = {
930 .pre_action = snd_pcm_pre_stop,
931 .do_action = snd_pcm_do_stop,
932 .post_action = snd_pcm_post_stop
933};
934
935/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700936 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200937 * @substream: the PCM substream instance
938 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700940 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100942int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
945}
946
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200947EXPORT_SYMBOL(snd_pcm_stop);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700950 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200951 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700953 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * Unlike snd_pcm_stop(), this affects only the given stream.
955 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100956int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Takashi Iwai877211f2005-11-17 13:59:38 +0100958 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
959 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
962/*
963 * pause callbacks
964 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100965static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Takashi Iwai877211f2005-11-17 13:59:38 +0100967 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
969 return -ENOSYS;
970 if (push) {
971 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
972 return -EBADFD;
973 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
974 return -EBADFD;
975 runtime->trigger_master = substream;
976 return 0;
977}
978
Takashi Iwai877211f2005-11-17 13:59:38 +0100979static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 if (substream->runtime->trigger_master != substream)
982 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200983 /* some drivers might use hw_ptr to recover from the pause -
984 update the hw_ptr now */
985 if (push)
986 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200987 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400988 * a delta between the current jiffies, this gives a large enough
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200989 * delta, effectively to skip the check once.
990 */
991 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return substream->ops->trigger(substream,
993 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
994 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
995}
996
Takashi Iwai877211f2005-11-17 13:59:38 +0100997static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 if (substream->runtime->trigger_master == substream)
1000 substream->ops->trigger(substream,
1001 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1002 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1003}
1004
Takashi Iwai877211f2005-11-17 13:59:38 +01001005static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Takashi Iwai877211f2005-11-17 13:59:38 +01001007 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 snd_pcm_trigger_tstamp(substream);
1009 if (push) {
1010 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1011 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001012 snd_timer_notify(substream->timer,
1013 SNDRV_TIMER_EVENT_MPAUSE,
1014 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001016 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 } else {
1018 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001020 snd_timer_notify(substream->timer,
1021 SNDRV_TIMER_EVENT_MCONTINUE,
1022 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024}
1025
1026static struct action_ops snd_pcm_action_pause = {
1027 .pre_action = snd_pcm_pre_pause,
1028 .do_action = snd_pcm_do_pause,
1029 .undo_action = snd_pcm_undo_pause,
1030 .post_action = snd_pcm_post_pause
1031};
1032
1033/*
1034 * Push/release the pause for all linked streams.
1035 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001036static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
1038 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1039}
1040
1041#ifdef CONFIG_PM
1042/* suspend */
1043
Takashi Iwai877211f2005-11-17 13:59:38 +01001044static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Takashi Iwai877211f2005-11-17 13:59:38 +01001046 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1048 return -EBUSY;
1049 runtime->trigger_master = substream;
1050 return 0;
1051}
1052
Takashi Iwai877211f2005-11-17 13:59:38 +01001053static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Takashi Iwai877211f2005-11-17 13:59:38 +01001055 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (runtime->trigger_master != substream)
1057 return 0;
1058 if (! snd_pcm_running(substream))
1059 return 0;
1060 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1061 return 0; /* suspend unconditionally */
1062}
1063
Takashi Iwai877211f2005-11-17 13:59:38 +01001064static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Takashi Iwai877211f2005-11-17 13:59:38 +01001066 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 snd_pcm_trigger_tstamp(substream);
1068 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001069 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1070 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 runtime->status->suspended_state = runtime->status->state;
1072 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001074 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077static struct action_ops snd_pcm_action_suspend = {
1078 .pre_action = snd_pcm_pre_suspend,
1079 .do_action = snd_pcm_do_suspend,
1080 .post_action = snd_pcm_post_suspend
1081};
1082
1083/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001084 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001085 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 * After this call, all streams are changed to SUSPENDED state.
1088 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001089int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int err;
1092 unsigned long flags;
1093
Takashi Iwai603bf522005-11-17 15:59:14 +01001094 if (! substream)
1095 return 0;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 snd_pcm_stream_lock_irqsave(substream, flags);
1098 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1099 snd_pcm_stream_unlock_irqrestore(substream, flags);
1100 return err;
1101}
1102
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001103EXPORT_SYMBOL(snd_pcm_suspend);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001106 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001107 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * After this call, all streams are changed to SUSPENDED state.
1110 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001111int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Takashi Iwai877211f2005-11-17 13:59:38 +01001113 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int stream, err = 0;
1115
Takashi Iwai603bf522005-11-17 15:59:14 +01001116 if (! pcm)
1117 return 0;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001120 for (substream = pcm->streams[stream].substream;
1121 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 /* FIXME: the open/close code should lock this as well */
1123 if (substream->runtime == NULL)
1124 continue;
1125 err = snd_pcm_suspend(substream);
1126 if (err < 0 && err != -EBUSY)
1127 return err;
1128 }
1129 }
1130 return 0;
1131}
1132
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001133EXPORT_SYMBOL(snd_pcm_suspend_all);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/* resume */
1136
Takashi Iwai877211f2005-11-17 13:59:38 +01001137static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Takashi Iwai877211f2005-11-17 13:59:38 +01001139 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1141 return -ENOSYS;
1142 runtime->trigger_master = substream;
1143 return 0;
1144}
1145
Takashi Iwai877211f2005-11-17 13:59:38 +01001146static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Takashi Iwai877211f2005-11-17 13:59:38 +01001148 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (runtime->trigger_master != substream)
1150 return 0;
1151 /* DMA not running previously? */
1152 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1153 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1154 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1155 return 0;
1156 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1157}
1158
Takashi Iwai877211f2005-11-17 13:59:38 +01001159static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
1161 if (substream->runtime->trigger_master == substream &&
1162 snd_pcm_running(substream))
1163 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1164}
1165
Takashi Iwai877211f2005-11-17 13:59:38 +01001166static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Takashi Iwai877211f2005-11-17 13:59:38 +01001168 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 snd_pcm_trigger_tstamp(substream);
1170 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001171 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1172 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176static struct action_ops snd_pcm_action_resume = {
1177 .pre_action = snd_pcm_pre_resume,
1178 .do_action = snd_pcm_do_resume,
1179 .undo_action = snd_pcm_undo_resume,
1180 .post_action = snd_pcm_post_resume
1181};
1182
Takashi Iwai877211f2005-11-17 13:59:38 +01001183static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Takashi Iwai877211f2005-11-17 13:59:38 +01001185 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 int res;
1187
1188 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001189 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1191 snd_power_unlock(card);
1192 return res;
1193}
1194
1195#else
1196
Takashi Iwai877211f2005-11-17 13:59:38 +01001197static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 return -ENOSYS;
1200}
1201
1202#endif /* CONFIG_PM */
1203
1204/*
1205 * xrun ioctl
1206 *
1207 * Change the RUNNING stream(s) to XRUN state.
1208 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001209static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Takashi Iwai877211f2005-11-17 13:59:38 +01001211 struct snd_card *card = substream->pcm->card;
1212 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int result;
1214
1215 snd_power_lock(card);
1216 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001217 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (result < 0)
1219 goto _unlock;
1220 }
1221
1222 snd_pcm_stream_lock_irq(substream);
1223 switch (runtime->status->state) {
1224 case SNDRV_PCM_STATE_XRUN:
1225 result = 0; /* already there */
1226 break;
1227 case SNDRV_PCM_STATE_RUNNING:
1228 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1229 break;
1230 default:
1231 result = -EBADFD;
1232 }
1233 snd_pcm_stream_unlock_irq(substream);
1234 _unlock:
1235 snd_power_unlock(card);
1236 return result;
1237}
1238
1239/*
1240 * reset ioctl
1241 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001242static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Takashi Iwai877211f2005-11-17 13:59:38 +01001244 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 switch (runtime->status->state) {
1246 case SNDRV_PCM_STATE_RUNNING:
1247 case SNDRV_PCM_STATE_PREPARED:
1248 case SNDRV_PCM_STATE_PAUSED:
1249 case SNDRV_PCM_STATE_SUSPENDED:
1250 return 0;
1251 default:
1252 return -EBADFD;
1253 }
1254}
1255
Takashi Iwai877211f2005-11-17 13:59:38 +01001256static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwai877211f2005-11-17 13:59:38 +01001258 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1260 if (err < 0)
1261 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001263 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1264 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 runtime->silence_start = runtime->status->hw_ptr;
1266 runtime->silence_filled = 0;
1267 return 0;
1268}
1269
Takashi Iwai877211f2005-11-17 13:59:38 +01001270static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Takashi Iwai877211f2005-11-17 13:59:38 +01001272 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 runtime->control->appl_ptr = runtime->status->hw_ptr;
1274 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1275 runtime->silence_size > 0)
1276 snd_pcm_playback_silence(substream, ULONG_MAX);
1277}
1278
1279static struct action_ops snd_pcm_action_reset = {
1280 .pre_action = snd_pcm_pre_reset,
1281 .do_action = snd_pcm_do_reset,
1282 .post_action = snd_pcm_post_reset
1283};
1284
Takashi Iwai877211f2005-11-17 13:59:38 +01001285static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1288}
1289
1290/*
1291 * prepare ioctl
1292 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001293/* we use the second argument for updating f_flags */
1294static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1295 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Takashi Iwai877211f2005-11-17 13:59:38 +01001297 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001298 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1299 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return -EBADFD;
1301 if (snd_pcm_running(substream))
1302 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001303 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return 0;
1305}
1306
Takashi Iwai877211f2005-11-17 13:59:38 +01001307static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 int err;
1310 err = substream->ops->prepare(substream);
1311 if (err < 0)
1312 return err;
1313 return snd_pcm_do_reset(substream, 0);
1314}
1315
Takashi Iwai877211f2005-11-17 13:59:38 +01001316static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Takashi Iwai877211f2005-11-17 13:59:38 +01001318 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 runtime->control->appl_ptr = runtime->status->hw_ptr;
1320 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1321}
1322
1323static struct action_ops snd_pcm_action_prepare = {
1324 .pre_action = snd_pcm_pre_prepare,
1325 .do_action = snd_pcm_do_prepare,
1326 .post_action = snd_pcm_post_prepare
1327};
1328
1329/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001330 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001331 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001332 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001334static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1335 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001338 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001339 int f_flags;
1340
1341 if (file)
1342 f_flags = file->f_flags;
1343 else
1344 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001347 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001348 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1349 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 snd_power_unlock(card);
1351 return res;
1352}
1353
1354/*
1355 * drain ioctl
1356 */
1357
Takashi Iwai877211f2005-11-17 13:59:38 +01001358static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 substream->runtime->trigger_master = substream;
1361 return 0;
1362}
1363
Takashi Iwai877211f2005-11-17 13:59:38 +01001364static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwai877211f2005-11-17 13:59:38 +01001366 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1368 switch (runtime->status->state) {
1369 case SNDRV_PCM_STATE_PREPARED:
1370 /* start playback stream if possible */
1371 if (! snd_pcm_playback_empty(substream)) {
1372 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1373 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1374 }
1375 break;
1376 case SNDRV_PCM_STATE_RUNNING:
1377 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1378 break;
1379 default:
1380 break;
1381 }
1382 } else {
1383 /* stop running stream */
1384 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001385 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001387 snd_pcm_do_stop(substream, new_state);
1388 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 }
1391 return 0;
1392}
1393
Takashi Iwai877211f2005-11-17 13:59:38 +01001394static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396}
1397
1398static struct action_ops snd_pcm_action_drain_init = {
1399 .pre_action = snd_pcm_pre_drain_init,
1400 .do_action = snd_pcm_do_drain_init,
1401 .post_action = snd_pcm_post_drain_init
1402};
1403
Takashi Iwai877211f2005-11-17 13:59:38 +01001404static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406/*
1407 * Drain the stream(s).
1408 * When the substream is linked, sync until the draining of all playback streams
1409 * is finished.
1410 * After this call, all streams are supposed to be either SETUP or DRAINING
1411 * (capture only) state.
1412 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001413static int snd_pcm_drain(struct snd_pcm_substream *substream,
1414 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwai877211f2005-11-17 13:59:38 +01001416 struct snd_card *card;
1417 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001418 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001419 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001421 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 card = substream->pcm->card;
1424 runtime = substream->runtime;
1425
1426 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1427 return -EBADFD;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 snd_power_lock(card);
1430 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001431 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001432 if (result < 0) {
1433 snd_power_unlock(card);
1434 return result;
1435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001438 if (file) {
1439 if (file->f_flags & O_NONBLOCK)
1440 nonblock = 1;
1441 } else if (substream->f_flags & O_NONBLOCK)
1442 nonblock = 1;
1443
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001444 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001445 snd_pcm_stream_lock_irq(substream);
1446 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001447 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001448 snd_pcm_pause(substream, 0);
1449
1450 /* pre-start/stop - all running streams are changed to DRAINING state */
1451 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001452 if (result < 0)
1453 goto unlock;
1454 /* in non-blocking, we don't wait in ioctl but let caller poll */
1455 if (nonblock) {
1456 result = -EAGAIN;
1457 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 for (;;) {
1461 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001462 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (signal_pending(current)) {
1464 result = -ERESTARTSYS;
1465 break;
1466 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001467 /* find a substream to drain */
1468 to_check = NULL;
1469 snd_pcm_group_for_each_entry(s, substream) {
1470 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1471 continue;
1472 runtime = s->runtime;
1473 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1474 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001475 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001476 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001477 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001478 if (!to_check)
1479 break; /* all drained */
1480 init_waitqueue_entry(&wait, current);
1481 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 set_current_state(TASK_INTERRUPTIBLE);
1483 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001484 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 snd_power_unlock(card);
1486 tout = schedule_timeout(10 * HZ);
1487 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001488 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001490 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (tout == 0) {
1492 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1493 result = -ESTRPIPE;
1494 else {
1495 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1496 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1497 result = -EIO;
1498 }
1499 break;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001502
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001503 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001504 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001505 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
1508 return result;
1509}
1510
1511/*
1512 * drop ioctl
1513 *
1514 * Immediately put all linked substreams into SETUP state.
1515 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001516static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
Takashi Iwai877211f2005-11-17 13:59:38 +01001518 struct snd_pcm_runtime *runtime;
1519 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int result = 0;
1521
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001522 if (PCM_RUNTIME_CHECK(substream))
1523 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 runtime = substream->runtime;
1525 card = substream->pcm->card;
1526
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001527 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001528 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1529 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return -EBADFD;
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 snd_pcm_stream_lock_irq(substream);
1533 /* resume pause */
1534 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1535 snd_pcm_pause(substream, 0);
1536
1537 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1538 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1539 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return result;
1542}
1543
1544
1545/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static struct file *snd_pcm_file_fd(int fd)
1547{
1548 struct file *file;
1549 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001550 unsigned int minor;
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 file = fget(fd);
1553 if (!file)
1554 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001555 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (!S_ISCHR(inode->i_mode) ||
1557 imajor(inode) != snd_major) {
1558 fput(file);
1559 return NULL;
1560 }
1561 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001562 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1563 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 fput(file);
1565 return NULL;
1566 }
1567 return file;
1568}
1569
1570/*
1571 * PCM link handling
1572 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001573static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
1575 int res = 0;
1576 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001577 struct snd_pcm_file *pcm_file;
1578 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 file = snd_pcm_file_fd(fd);
1581 if (!file)
1582 return -EBADFD;
1583 pcm_file = file->private_data;
1584 substream1 = pcm_file->substream;
1585 down_write(&snd_pcm_link_rwsem);
1586 write_lock_irq(&snd_pcm_link_rwlock);
1587 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1588 substream->runtime->status->state != substream1->runtime->status->state) {
1589 res = -EBADFD;
1590 goto _end;
1591 }
1592 if (snd_pcm_stream_linked(substream1)) {
1593 res = -EALREADY;
1594 goto _end;
1595 }
1596 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001597 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (substream->group == NULL) {
1599 res = -ENOMEM;
1600 goto _end;
1601 }
1602 spin_lock_init(&substream->group->lock);
1603 INIT_LIST_HEAD(&substream->group->substreams);
1604 list_add_tail(&substream->link_list, &substream->group->substreams);
1605 substream->group->count = 1;
1606 }
1607 list_add_tail(&substream1->link_list, &substream->group->substreams);
1608 substream->group->count++;
1609 substream1->group = substream->group;
1610 _end:
1611 write_unlock_irq(&snd_pcm_link_rwlock);
1612 up_write(&snd_pcm_link_rwsem);
1613 fput(file);
1614 return res;
1615}
1616
Takashi Iwai877211f2005-11-17 13:59:38 +01001617static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
1619 substream->group = &substream->self_group;
1620 INIT_LIST_HEAD(&substream->self_group.substreams);
1621 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1622}
1623
Takashi Iwai877211f2005-11-17 13:59:38 +01001624static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwaief991b92007-02-22 12:52:53 +01001626 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int res = 0;
1628
1629 down_write(&snd_pcm_link_rwsem);
1630 write_lock_irq(&snd_pcm_link_rwlock);
1631 if (!snd_pcm_stream_linked(substream)) {
1632 res = -EALREADY;
1633 goto _end;
1634 }
1635 list_del(&substream->link_list);
1636 substream->group->count--;
1637 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001638 snd_pcm_group_for_each_entry(s, substream) {
1639 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
1641 }
1642 kfree(substream->group);
1643 }
1644 relink_to_local(substream);
1645 _end:
1646 write_unlock_irq(&snd_pcm_link_rwlock);
1647 up_write(&snd_pcm_link_rwsem);
1648 return res;
1649}
1650
1651/*
1652 * hw configurator
1653 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001654static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1655 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
Takashi Iwai877211f2005-11-17 13:59:38 +01001657 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1659 hw_param_interval_c(params, rule->deps[1]), &t);
1660 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1661}
1662
Takashi Iwai877211f2005-11-17 13:59:38 +01001663static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1664 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Takashi Iwai877211f2005-11-17 13:59:38 +01001666 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1668 hw_param_interval_c(params, rule->deps[1]), &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1670}
1671
Takashi Iwai877211f2005-11-17 13:59:38 +01001672static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Takashi Iwai877211f2005-11-17 13:59:38 +01001675 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1677 hw_param_interval_c(params, rule->deps[1]),
1678 (unsigned long) rule->private, &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1680}
1681
Takashi Iwai877211f2005-11-17 13:59:38 +01001682static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai877211f2005-11-17 13:59:38 +01001685 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1687 (unsigned long) rule->private,
1688 hw_param_interval_c(params, rule->deps[1]), &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690}
1691
Takashi Iwai877211f2005-11-17 13:59:38 +01001692static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001696 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1697 struct snd_mask m;
1698 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 snd_mask_any(&m);
1700 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1701 int bits;
1702 if (! snd_mask_test(mask, k))
1703 continue;
1704 bits = snd_pcm_format_physical_width(k);
1705 if (bits <= 0)
1706 continue; /* ignore invalid formats */
1707 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1708 snd_mask_reset(&m, k);
1709 }
1710 return snd_mask_refine(mask, &m);
1711}
1712
Takashi Iwai877211f2005-11-17 13:59:38 +01001713static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1714 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Takashi Iwai877211f2005-11-17 13:59:38 +01001716 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 unsigned int k;
1718 t.min = UINT_MAX;
1719 t.max = 0;
1720 t.openmin = 0;
1721 t.openmax = 0;
1722 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1723 int bits;
1724 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1725 continue;
1726 bits = snd_pcm_format_physical_width(k);
1727 if (bits <= 0)
1728 continue; /* ignore invalid formats */
1729 if (t.min > (unsigned)bits)
1730 t.min = bits;
1731 if (t.max < (unsigned)bits)
1732 t.max = bits;
1733 }
1734 t.integer = 1;
1735 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1736}
1737
1738#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1739#error "Change this table"
1740#endif
1741
1742static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1743 48000, 64000, 88200, 96000, 176400, 192000 };
1744
Clemens Ladisch7653d552007-08-13 17:38:54 +02001745const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1746 .count = ARRAY_SIZE(rates),
1747 .list = rates,
1748};
1749
Takashi Iwai877211f2005-11-17 13:59:38 +01001750static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1751 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Takashi Iwai877211f2005-11-17 13:59:38 +01001753 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001755 snd_pcm_known_rates.count,
1756 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Takashi Iwai877211f2005-11-17 13:59:38 +01001759static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1760 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Takashi Iwai877211f2005-11-17 13:59:38 +01001762 struct snd_interval t;
1763 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 t.min = 0;
1765 t.max = substream->buffer_bytes_max;
1766 t.openmin = 0;
1767 t.openmax = 0;
1768 t.integer = 1;
1769 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1770}
1771
Takashi Iwai877211f2005-11-17 13:59:38 +01001772int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Takashi Iwai877211f2005-11-17 13:59:38 +01001774 struct snd_pcm_runtime *runtime = substream->runtime;
1775 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int k, err;
1777
1778 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1779 snd_mask_any(constrs_mask(constrs, k));
1780 }
1781
1782 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1783 snd_interval_any(constrs_interval(constrs, k));
1784 }
1785
1786 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1787 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1788 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1789 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1791
1792 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1793 snd_pcm_hw_rule_format, NULL,
1794 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1795 if (err < 0)
1796 return err;
1797 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1798 snd_pcm_hw_rule_sample_bits, NULL,
1799 SNDRV_PCM_HW_PARAM_FORMAT,
1800 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1801 if (err < 0)
1802 return err;
1803 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1804 snd_pcm_hw_rule_div, NULL,
1805 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1806 if (err < 0)
1807 return err;
1808 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1809 snd_pcm_hw_rule_mul, NULL,
1810 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1811 if (err < 0)
1812 return err;
1813 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1814 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1815 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1816 if (err < 0)
1817 return err;
1818 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1819 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1820 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1821 if (err < 0)
1822 return err;
1823 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1824 snd_pcm_hw_rule_div, NULL,
1825 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1826 if (err < 0)
1827 return err;
1828 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1829 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1830 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1831 if (err < 0)
1832 return err;
1833 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1834 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1835 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1836 if (err < 0)
1837 return err;
1838 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1839 snd_pcm_hw_rule_div, NULL,
1840 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1844 snd_pcm_hw_rule_div, NULL,
1845 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1849 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1850 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1854 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1855 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1859 snd_pcm_hw_rule_mul, NULL,
1860 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1864 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1865 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1869 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1874 snd_pcm_hw_rule_muldivk, (void*) 8,
1875 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1879 snd_pcm_hw_rule_muldivk, (void*) 8,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1881 if (err < 0)
1882 return err;
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1884 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1885 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1886 if (err < 0)
1887 return err;
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1889 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1890 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1891 if (err < 0)
1892 return err;
1893 return 0;
1894}
1895
Takashi Iwai877211f2005-11-17 13:59:38 +01001896int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Takashi Iwai877211f2005-11-17 13:59:38 +01001898 struct snd_pcm_runtime *runtime = substream->runtime;
1899 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 int err;
1901 unsigned int mask = 0;
1902
1903 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1904 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1905 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1906 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1907 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1908 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1911 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1912 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1913 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1914 }
1915 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001916 if (err < 0)
1917 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001920 if (err < 0)
1921 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001924 if (err < 0)
1925 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1928 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001929 if (err < 0)
1930 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1933 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001934 if (err < 0)
1935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1938 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001939 if (err < 0)
1940 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1943 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001944 if (err < 0)
1945 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1948 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001949 if (err < 0)
1950 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1953 snd_pcm_hw_rule_buffer_bytes_max, substream,
1954 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1955 if (err < 0)
1956 return err;
1957
1958 /* FIXME: remove */
1959 if (runtime->dma_bytes) {
1960 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001961 if (err < 0)
1962 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
1965 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1966 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1967 snd_pcm_hw_rule_rate, hw,
1968 SNDRV_PCM_HW_PARAM_RATE, -1);
1969 if (err < 0)
1970 return err;
1971 }
1972
1973 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1975
1976 return 0;
1977}
1978
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001979static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001982}
1983
1984void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1985{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001986 substream->ref_count--;
1987 if (substream->ref_count > 0)
1988 return;
1989
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001990 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001991 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (substream->ops->hw_free != NULL)
1993 substream->ops->hw_free(substream);
1994 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001995 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Takashi Iwai8699a0b2010-09-16 22:52:32 +02001997 if (pm_qos_request_active(&substream->latency_pm_qos_req))
1998 pm_qos_remove_request(&substream->latency_pm_qos_req);
Takashi Iwai15762742006-04-06 19:47:42 +02001999 if (substream->pcm_release) {
2000 substream->pcm_release(substream);
2001 substream->pcm_release = NULL;
2002 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002003 snd_pcm_detach_substream(substream);
2004}
2005
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002006EXPORT_SYMBOL(snd_pcm_release_substream);
2007
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002008int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2009 struct file *file,
2010 struct snd_pcm_substream **rsubstream)
2011{
2012 struct snd_pcm_substream *substream;
2013 int err;
2014
2015 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2016 if (err < 0)
2017 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002018 if (substream->ref_count > 1) {
2019 *rsubstream = substream;
2020 return 0;
2021 }
2022
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002023 err = snd_pcm_hw_constraints_init(substream);
2024 if (err < 0) {
2025 snd_printd("snd_pcm_hw_constraints_init failed\n");
2026 goto error;
2027 }
2028
2029 if ((err = substream->ops->open(substream)) < 0)
2030 goto error;
2031
2032 substream->hw_opened = 1;
2033
2034 err = snd_pcm_hw_constraints_complete(substream);
2035 if (err < 0) {
2036 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2037 goto error;
2038 }
2039
2040 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002042
2043 error:
2044 snd_pcm_release_substream(substream);
2045 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002048EXPORT_SYMBOL(snd_pcm_open_substream);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002051 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002053 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
Takashi Iwai877211f2005-11-17 13:59:38 +01002055 struct snd_pcm_file *pcm_file;
2056 struct snd_pcm_substream *substream;
2057 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002058 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002060 if (rpcm_file)
2061 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002063 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2064 if (err < 0)
2065 return err;
2066
Takashi Iwai548a6482006-07-31 16:51:51 +02002067 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2068 if (pcm_file == NULL) {
2069 snd_pcm_release_substream(substream);
2070 return -ENOMEM;
2071 }
2072 pcm_file->substream = substream;
2073 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002074 str = substream->pstr;
2075 substream->file = pcm_file;
2076 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002079 if (rpcm_file)
2080 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
Clemens Ladischf87135f2005-11-20 14:06:59 +01002084static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai877211f2005-11-17 13:59:38 +01002086 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002087 int err = nonseekable_open(inode, file);
2088 if (err < 0)
2089 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002090 pcm = snd_lookup_minor_data(iminor(inode),
2091 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2092 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2093}
2094
2095static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2096{
2097 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002098 int err = nonseekable_open(inode, file);
2099 if (err < 0)
2100 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002101 pcm = snd_lookup_minor_data(iminor(inode),
2102 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2103 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2104}
2105
2106static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2107{
2108 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002109 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 wait_queue_t wait;
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (pcm == NULL) {
2113 err = -ENODEV;
2114 goto __error1;
2115 }
2116 err = snd_card_file_add(pcm->card, file);
2117 if (err < 0)
2118 goto __error1;
2119 if (!try_module_get(pcm->card->module)) {
2120 err = -EFAULT;
2121 goto __error2;
2122 }
2123 init_waitqueue_entry(&wait, current);
2124 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002125 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002127 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (err >= 0)
2129 break;
2130 if (err == -EAGAIN) {
2131 if (file->f_flags & O_NONBLOCK) {
2132 err = -EBUSY;
2133 break;
2134 }
2135 } else
2136 break;
2137 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002138 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002140 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (signal_pending(current)) {
2142 err = -ERESTARTSYS;
2143 break;
2144 }
2145 }
2146 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002147 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (err < 0)
2149 goto __error;
2150 return err;
2151
2152 __error:
2153 module_put(pcm->card->module);
2154 __error2:
2155 snd_card_file_remove(pcm->card, file);
2156 __error1:
2157 return err;
2158}
2159
2160static int snd_pcm_release(struct inode *inode, struct file *file)
2161{
Takashi Iwai877211f2005-11-17 13:59:38 +01002162 struct snd_pcm *pcm;
2163 struct snd_pcm_substream *substream;
2164 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 pcm_file = file->private_data;
2167 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002168 if (snd_BUG_ON(!substream))
2169 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002171 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002172 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002173 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002174 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 wake_up(&pcm->open_wait);
2176 module_put(pcm->card->module);
2177 snd_card_file_remove(pcm->card, file);
2178 return 0;
2179}
2180
Takashi Iwai877211f2005-11-17 13:59:38 +01002181static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2182 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Takashi Iwai877211f2005-11-17 13:59:38 +01002184 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 snd_pcm_sframes_t appl_ptr;
2186 snd_pcm_sframes_t ret;
2187 snd_pcm_sframes_t hw_avail;
2188
2189 if (frames == 0)
2190 return 0;
2191
2192 snd_pcm_stream_lock_irq(substream);
2193 switch (runtime->status->state) {
2194 case SNDRV_PCM_STATE_PREPARED:
2195 break;
2196 case SNDRV_PCM_STATE_DRAINING:
2197 case SNDRV_PCM_STATE_RUNNING:
2198 if (snd_pcm_update_hw_ptr(substream) >= 0)
2199 break;
2200 /* Fall through */
2201 case SNDRV_PCM_STATE_XRUN:
2202 ret = -EPIPE;
2203 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002204 case SNDRV_PCM_STATE_SUSPENDED:
2205 ret = -ESTRPIPE;
2206 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 default:
2208 ret = -EBADFD;
2209 goto __end;
2210 }
2211
2212 hw_avail = snd_pcm_playback_hw_avail(runtime);
2213 if (hw_avail <= 0) {
2214 ret = 0;
2215 goto __end;
2216 }
2217 if (frames > (snd_pcm_uframes_t)hw_avail)
2218 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 appl_ptr = runtime->control->appl_ptr - frames;
2220 if (appl_ptr < 0)
2221 appl_ptr += runtime->boundary;
2222 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 ret = frames;
2224 __end:
2225 snd_pcm_stream_unlock_irq(substream);
2226 return ret;
2227}
2228
Takashi Iwai877211f2005-11-17 13:59:38 +01002229static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2230 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Takashi Iwai877211f2005-11-17 13:59:38 +01002232 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 snd_pcm_sframes_t appl_ptr;
2234 snd_pcm_sframes_t ret;
2235 snd_pcm_sframes_t hw_avail;
2236
2237 if (frames == 0)
2238 return 0;
2239
2240 snd_pcm_stream_lock_irq(substream);
2241 switch (runtime->status->state) {
2242 case SNDRV_PCM_STATE_PREPARED:
2243 case SNDRV_PCM_STATE_DRAINING:
2244 break;
2245 case SNDRV_PCM_STATE_RUNNING:
2246 if (snd_pcm_update_hw_ptr(substream) >= 0)
2247 break;
2248 /* Fall through */
2249 case SNDRV_PCM_STATE_XRUN:
2250 ret = -EPIPE;
2251 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002252 case SNDRV_PCM_STATE_SUSPENDED:
2253 ret = -ESTRPIPE;
2254 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 default:
2256 ret = -EBADFD;
2257 goto __end;
2258 }
2259
2260 hw_avail = snd_pcm_capture_hw_avail(runtime);
2261 if (hw_avail <= 0) {
2262 ret = 0;
2263 goto __end;
2264 }
2265 if (frames > (snd_pcm_uframes_t)hw_avail)
2266 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 appl_ptr = runtime->control->appl_ptr - frames;
2268 if (appl_ptr < 0)
2269 appl_ptr += runtime->boundary;
2270 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 ret = frames;
2272 __end:
2273 snd_pcm_stream_unlock_irq(substream);
2274 return ret;
2275}
2276
Takashi Iwai877211f2005-11-17 13:59:38 +01002277static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2278 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Takashi Iwai877211f2005-11-17 13:59:38 +01002280 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 snd_pcm_sframes_t appl_ptr;
2282 snd_pcm_sframes_t ret;
2283 snd_pcm_sframes_t avail;
2284
2285 if (frames == 0)
2286 return 0;
2287
2288 snd_pcm_stream_lock_irq(substream);
2289 switch (runtime->status->state) {
2290 case SNDRV_PCM_STATE_PREPARED:
2291 case SNDRV_PCM_STATE_PAUSED:
2292 break;
2293 case SNDRV_PCM_STATE_DRAINING:
2294 case SNDRV_PCM_STATE_RUNNING:
2295 if (snd_pcm_update_hw_ptr(substream) >= 0)
2296 break;
2297 /* Fall through */
2298 case SNDRV_PCM_STATE_XRUN:
2299 ret = -EPIPE;
2300 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002301 case SNDRV_PCM_STATE_SUSPENDED:
2302 ret = -ESTRPIPE;
2303 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 default:
2305 ret = -EBADFD;
2306 goto __end;
2307 }
2308
2309 avail = snd_pcm_playback_avail(runtime);
2310 if (avail <= 0) {
2311 ret = 0;
2312 goto __end;
2313 }
2314 if (frames > (snd_pcm_uframes_t)avail)
2315 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 appl_ptr = runtime->control->appl_ptr + frames;
2317 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2318 appl_ptr -= runtime->boundary;
2319 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 ret = frames;
2321 __end:
2322 snd_pcm_stream_unlock_irq(substream);
2323 return ret;
2324}
2325
Takashi Iwai877211f2005-11-17 13:59:38 +01002326static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2327 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Takashi Iwai877211f2005-11-17 13:59:38 +01002329 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 snd_pcm_sframes_t appl_ptr;
2331 snd_pcm_sframes_t ret;
2332 snd_pcm_sframes_t avail;
2333
2334 if (frames == 0)
2335 return 0;
2336
2337 snd_pcm_stream_lock_irq(substream);
2338 switch (runtime->status->state) {
2339 case SNDRV_PCM_STATE_PREPARED:
2340 case SNDRV_PCM_STATE_DRAINING:
2341 case SNDRV_PCM_STATE_PAUSED:
2342 break;
2343 case SNDRV_PCM_STATE_RUNNING:
2344 if (snd_pcm_update_hw_ptr(substream) >= 0)
2345 break;
2346 /* Fall through */
2347 case SNDRV_PCM_STATE_XRUN:
2348 ret = -EPIPE;
2349 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002350 case SNDRV_PCM_STATE_SUSPENDED:
2351 ret = -ESTRPIPE;
2352 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 default:
2354 ret = -EBADFD;
2355 goto __end;
2356 }
2357
2358 avail = snd_pcm_capture_avail(runtime);
2359 if (avail <= 0) {
2360 ret = 0;
2361 goto __end;
2362 }
2363 if (frames > (snd_pcm_uframes_t)avail)
2364 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 appl_ptr = runtime->control->appl_ptr + frames;
2366 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2367 appl_ptr -= runtime->boundary;
2368 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 ret = frames;
2370 __end:
2371 snd_pcm_stream_unlock_irq(substream);
2372 return ret;
2373}
2374
Takashi Iwai877211f2005-11-17 13:59:38 +01002375static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Takashi Iwai877211f2005-11-17 13:59:38 +01002377 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 int err;
2379
2380 snd_pcm_stream_lock_irq(substream);
2381 switch (runtime->status->state) {
2382 case SNDRV_PCM_STATE_DRAINING:
2383 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2384 goto __badfd;
2385 case SNDRV_PCM_STATE_RUNNING:
2386 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2387 break;
2388 /* Fall through */
2389 case SNDRV_PCM_STATE_PREPARED:
2390 case SNDRV_PCM_STATE_SUSPENDED:
2391 err = 0;
2392 break;
2393 case SNDRV_PCM_STATE_XRUN:
2394 err = -EPIPE;
2395 break;
2396 default:
2397 __badfd:
2398 err = -EBADFD;
2399 break;
2400 }
2401 snd_pcm_stream_unlock_irq(substream);
2402 return err;
2403}
2404
Takashi Iwai877211f2005-11-17 13:59:38 +01002405static int snd_pcm_delay(struct snd_pcm_substream *substream,
2406 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Takashi Iwai877211f2005-11-17 13:59:38 +01002408 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 int err;
2410 snd_pcm_sframes_t n = 0;
2411
2412 snd_pcm_stream_lock_irq(substream);
2413 switch (runtime->status->state) {
2414 case SNDRV_PCM_STATE_DRAINING:
2415 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2416 goto __badfd;
2417 case SNDRV_PCM_STATE_RUNNING:
2418 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2419 break;
2420 /* Fall through */
2421 case SNDRV_PCM_STATE_PREPARED:
2422 case SNDRV_PCM_STATE_SUSPENDED:
2423 err = 0;
2424 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2425 n = snd_pcm_playback_hw_avail(runtime);
2426 else
2427 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002428 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 break;
2430 case SNDRV_PCM_STATE_XRUN:
2431 err = -EPIPE;
2432 break;
2433 default:
2434 __badfd:
2435 err = -EBADFD;
2436 break;
2437 }
2438 snd_pcm_stream_unlock_irq(substream);
2439 if (!err)
2440 if (put_user(n, res))
2441 err = -EFAULT;
2442 return err;
2443}
2444
Takashi Iwai877211f2005-11-17 13:59:38 +01002445static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2446 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Takashi Iwai877211f2005-11-17 13:59:38 +01002448 struct snd_pcm_runtime *runtime = substream->runtime;
2449 struct snd_pcm_sync_ptr sync_ptr;
2450 volatile struct snd_pcm_mmap_status *status;
2451 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 int err;
2453
2454 memset(&sync_ptr, 0, sizeof(sync_ptr));
2455 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2456 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002457 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 -07002458 return -EFAULT;
2459 status = runtime->status;
2460 control = runtime->control;
2461 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2462 err = snd_pcm_hwsync(substream);
2463 if (err < 0)
2464 return err;
2465 }
2466 snd_pcm_stream_lock_irq(substream);
2467 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2468 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2469 else
2470 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2471 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2472 control->avail_min = sync_ptr.c.control.avail_min;
2473 else
2474 sync_ptr.c.control.avail_min = control->avail_min;
2475 sync_ptr.s.status.state = status->state;
2476 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2477 sync_ptr.s.status.tstamp = status->tstamp;
2478 sync_ptr.s.status.suspended_state = status->suspended_state;
2479 snd_pcm_stream_unlock_irq(substream);
2480 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2481 return -EFAULT;
2482 return 0;
2483}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002484
2485static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2486{
2487 struct snd_pcm_runtime *runtime = substream->runtime;
2488 int arg;
2489
2490 if (get_user(arg, _arg))
2491 return -EFAULT;
2492 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2493 return -EINVAL;
2494 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2495 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2496 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2497 return 0;
2498}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Takashi Iwai0df63e42006-04-28 15:13:41 +02002500static int snd_pcm_common_ioctl1(struct file *file,
2501 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 unsigned int cmd, void __user *arg)
2503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 switch (cmd) {
2505 case SNDRV_PCM_IOCTL_PVERSION:
2506 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2507 case SNDRV_PCM_IOCTL_INFO:
2508 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002509 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2510 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002511 case SNDRV_PCM_IOCTL_TTSTAMP:
2512 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 case SNDRV_PCM_IOCTL_HW_REFINE:
2514 return snd_pcm_hw_refine_user(substream, arg);
2515 case SNDRV_PCM_IOCTL_HW_PARAMS:
2516 return snd_pcm_hw_params_user(substream, arg);
2517 case SNDRV_PCM_IOCTL_HW_FREE:
2518 return snd_pcm_hw_free(substream);
2519 case SNDRV_PCM_IOCTL_SW_PARAMS:
2520 return snd_pcm_sw_params_user(substream, arg);
2521 case SNDRV_PCM_IOCTL_STATUS:
2522 return snd_pcm_status_user(substream, arg);
2523 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2524 return snd_pcm_channel_info_user(substream, arg);
2525 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002526 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 case SNDRV_PCM_IOCTL_RESET:
2528 return snd_pcm_reset(substream);
2529 case SNDRV_PCM_IOCTL_START:
2530 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2531 case SNDRV_PCM_IOCTL_LINK:
2532 return snd_pcm_link(substream, (int)(unsigned long) arg);
2533 case SNDRV_PCM_IOCTL_UNLINK:
2534 return snd_pcm_unlink(substream);
2535 case SNDRV_PCM_IOCTL_RESUME:
2536 return snd_pcm_resume(substream);
2537 case SNDRV_PCM_IOCTL_XRUN:
2538 return snd_pcm_xrun(substream);
2539 case SNDRV_PCM_IOCTL_HWSYNC:
2540 return snd_pcm_hwsync(substream);
2541 case SNDRV_PCM_IOCTL_DELAY:
2542 return snd_pcm_delay(substream, arg);
2543 case SNDRV_PCM_IOCTL_SYNC_PTR:
2544 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002545#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2547 return snd_pcm_hw_refine_old_user(substream, arg);
2548 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2549 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002552 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 case SNDRV_PCM_IOCTL_DROP:
2554 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002555 case SNDRV_PCM_IOCTL_PAUSE:
2556 {
2557 int res;
2558 snd_pcm_stream_lock_irq(substream);
2559 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2560 snd_pcm_stream_unlock_irq(substream);
2561 return res;
2562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 snd_printd("unknown ioctl = 0x%x\n", cmd);
2565 return -ENOTTY;
2566}
2567
Takashi Iwai0df63e42006-04-28 15:13:41 +02002568static int snd_pcm_playback_ioctl1(struct file *file,
2569 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned int cmd, void __user *arg)
2571{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002572 if (snd_BUG_ON(!substream))
2573 return -ENXIO;
2574 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2575 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 switch (cmd) {
2577 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2578 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002579 struct snd_xferi xferi;
2580 struct snd_xferi __user *_xferi = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 snd_pcm_sframes_t result;
2583 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2584 return -EBADFD;
2585 if (put_user(0, &_xferi->result))
2586 return -EFAULT;
2587 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2588 return -EFAULT;
2589 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2590 __put_user(result, &_xferi->result);
2591 return result < 0 ? result : 0;
2592 }
2593 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2594 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002595 struct snd_xfern xfern;
2596 struct snd_xfern __user *_xfern = arg;
2597 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 void __user **bufs;
2599 snd_pcm_sframes_t result;
2600 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2601 return -EBADFD;
2602 if (runtime->channels > 128)
2603 return -EINVAL;
2604 if (put_user(0, &_xfern->result))
2605 return -EFAULT;
2606 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2607 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002608
2609 bufs = memdup_user(xfern.bufs,
2610 sizeof(void *) * runtime->channels);
2611 if (IS_ERR(bufs))
2612 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2614 kfree(bufs);
2615 __put_user(result, &_xfern->result);
2616 return result < 0 ? result : 0;
2617 }
2618 case SNDRV_PCM_IOCTL_REWIND:
2619 {
2620 snd_pcm_uframes_t frames;
2621 snd_pcm_uframes_t __user *_frames = arg;
2622 snd_pcm_sframes_t result;
2623 if (get_user(frames, _frames))
2624 return -EFAULT;
2625 if (put_user(0, _frames))
2626 return -EFAULT;
2627 result = snd_pcm_playback_rewind(substream, frames);
2628 __put_user(result, _frames);
2629 return result < 0 ? result : 0;
2630 }
2631 case SNDRV_PCM_IOCTL_FORWARD:
2632 {
2633 snd_pcm_uframes_t frames;
2634 snd_pcm_uframes_t __user *_frames = arg;
2635 snd_pcm_sframes_t result;
2636 if (get_user(frames, _frames))
2637 return -EFAULT;
2638 if (put_user(0, _frames))
2639 return -EFAULT;
2640 result = snd_pcm_playback_forward(substream, frames);
2641 __put_user(result, _frames);
2642 return result < 0 ? result : 0;
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002645 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Takashi Iwai0df63e42006-04-28 15:13:41 +02002648static int snd_pcm_capture_ioctl1(struct file *file,
2649 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 unsigned int cmd, void __user *arg)
2651{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002652 if (snd_BUG_ON(!substream))
2653 return -ENXIO;
2654 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 switch (cmd) {
2657 case SNDRV_PCM_IOCTL_READI_FRAMES:
2658 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002659 struct snd_xferi xferi;
2660 struct snd_xferi __user *_xferi = arg;
2661 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 snd_pcm_sframes_t result;
2663 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2664 return -EBADFD;
2665 if (put_user(0, &_xferi->result))
2666 return -EFAULT;
2667 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2668 return -EFAULT;
2669 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2670 __put_user(result, &_xferi->result);
2671 return result < 0 ? result : 0;
2672 }
2673 case SNDRV_PCM_IOCTL_READN_FRAMES:
2674 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002675 struct snd_xfern xfern;
2676 struct snd_xfern __user *_xfern = arg;
2677 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 void *bufs;
2679 snd_pcm_sframes_t result;
2680 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2681 return -EBADFD;
2682 if (runtime->channels > 128)
2683 return -EINVAL;
2684 if (put_user(0, &_xfern->result))
2685 return -EFAULT;
2686 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2687 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002688
2689 bufs = memdup_user(xfern.bufs,
2690 sizeof(void *) * runtime->channels);
2691 if (IS_ERR(bufs))
2692 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2694 kfree(bufs);
2695 __put_user(result, &_xfern->result);
2696 return result < 0 ? result : 0;
2697 }
2698 case SNDRV_PCM_IOCTL_REWIND:
2699 {
2700 snd_pcm_uframes_t frames;
2701 snd_pcm_uframes_t __user *_frames = arg;
2702 snd_pcm_sframes_t result;
2703 if (get_user(frames, _frames))
2704 return -EFAULT;
2705 if (put_user(0, _frames))
2706 return -EFAULT;
2707 result = snd_pcm_capture_rewind(substream, frames);
2708 __put_user(result, _frames);
2709 return result < 0 ? result : 0;
2710 }
2711 case SNDRV_PCM_IOCTL_FORWARD:
2712 {
2713 snd_pcm_uframes_t frames;
2714 snd_pcm_uframes_t __user *_frames = arg;
2715 snd_pcm_sframes_t result;
2716 if (get_user(frames, _frames))
2717 return -EFAULT;
2718 if (put_user(0, _frames))
2719 return -EFAULT;
2720 result = snd_pcm_capture_forward(substream, frames);
2721 __put_user(result, _frames);
2722 return result < 0 ? result : 0;
2723 }
2724 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002725 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Takashi Iwai877211f2005-11-17 13:59:38 +01002728static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2729 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Takashi Iwai877211f2005-11-17 13:59:38 +01002731 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 pcm_file = file->private_data;
2734
2735 if (((cmd >> 8) & 0xff) != 'A')
2736 return -ENOTTY;
2737
Takashi Iwai0df63e42006-04-28 15:13:41 +02002738 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2739 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
Takashi Iwai877211f2005-11-17 13:59:38 +01002742static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2743 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Takashi Iwai877211f2005-11-17 13:59:38 +01002745 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 pcm_file = file->private_data;
2748
2749 if (((cmd >> 8) & 0xff) != 'A')
2750 return -ENOTTY;
2751
Takashi Iwai0df63e42006-04-28 15:13:41 +02002752 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2753 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
Takashi Iwai877211f2005-11-17 13:59:38 +01002756int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 unsigned int cmd, void *arg)
2758{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002759 mm_segment_t fs;
2760 int result;
2761
2762 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 switch (substream->stream) {
2764 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002765 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2766 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002769 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2770 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002771 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002773 result = -EINVAL;
2774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002776 snd_leave_user(fs);
2777 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002780EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2781
Takashi Iwai877211f2005-11-17 13:59:38 +01002782static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2783 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784{
Takashi Iwai877211f2005-11-17 13:59:38 +01002785 struct snd_pcm_file *pcm_file;
2786 struct snd_pcm_substream *substream;
2787 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 snd_pcm_sframes_t result;
2789
2790 pcm_file = file->private_data;
2791 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002792 if (PCM_RUNTIME_CHECK(substream))
2793 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 runtime = substream->runtime;
2795 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2796 return -EBADFD;
2797 if (!frame_aligned(runtime, count))
2798 return -EINVAL;
2799 count = bytes_to_frames(runtime, count);
2800 result = snd_pcm_lib_read(substream, buf, count);
2801 if (result > 0)
2802 result = frames_to_bytes(runtime, result);
2803 return result;
2804}
2805
Takashi Iwai877211f2005-11-17 13:59:38 +01002806static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2807 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Takashi Iwai877211f2005-11-17 13:59:38 +01002809 struct snd_pcm_file *pcm_file;
2810 struct snd_pcm_substream *substream;
2811 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 snd_pcm_sframes_t result;
2813
2814 pcm_file = file->private_data;
2815 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002816 if (PCM_RUNTIME_CHECK(substream))
2817 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2820 return -EBADFD;
2821 if (!frame_aligned(runtime, count))
2822 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 count = bytes_to_frames(runtime, count);
2824 result = snd_pcm_lib_write(substream, buf, count);
2825 if (result > 0)
2826 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return result;
2828}
2829
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002830static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2831 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833{
Takashi Iwai877211f2005-11-17 13:59:38 +01002834 struct snd_pcm_file *pcm_file;
2835 struct snd_pcm_substream *substream;
2836 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 snd_pcm_sframes_t result;
2838 unsigned long i;
2839 void __user **bufs;
2840 snd_pcm_uframes_t frames;
2841
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002842 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002844 if (PCM_RUNTIME_CHECK(substream))
2845 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 runtime = substream->runtime;
2847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2848 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002849 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002851 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002853 frames = bytes_to_samples(runtime, iov->iov_len);
2854 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (bufs == NULL)
2856 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002857 for (i = 0; i < nr_segs; ++i)
2858 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 result = snd_pcm_lib_readv(substream, bufs, frames);
2860 if (result > 0)
2861 result = frames_to_bytes(runtime, result);
2862 kfree(bufs);
2863 return result;
2864}
2865
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002866static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2867 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
Takashi Iwai877211f2005-11-17 13:59:38 +01002869 struct snd_pcm_file *pcm_file;
2870 struct snd_pcm_substream *substream;
2871 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 snd_pcm_sframes_t result;
2873 unsigned long i;
2874 void __user **bufs;
2875 snd_pcm_uframes_t frames;
2876
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002877 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002879 if (PCM_RUNTIME_CHECK(substream))
2880 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002882 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2883 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002884 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002885 !frame_aligned(runtime, iov->iov_len))
2886 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 frames = bytes_to_samples(runtime, iov->iov_len);
2888 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (bufs == NULL)
2890 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002891 for (i = 0; i < nr_segs; ++i)
2892 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 result = snd_pcm_lib_writev(substream, bufs, frames);
2894 if (result > 0)
2895 result = frames_to_bytes(runtime, result);
2896 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return result;
2898}
2899
2900static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2901{
Takashi Iwai877211f2005-11-17 13:59:38 +01002902 struct snd_pcm_file *pcm_file;
2903 struct snd_pcm_substream *substream;
2904 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 unsigned int mask;
2906 snd_pcm_uframes_t avail;
2907
2908 pcm_file = file->private_data;
2909
2910 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002911 if (PCM_RUNTIME_CHECK(substream))
2912 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 runtime = substream->runtime;
2914
2915 poll_wait(file, &runtime->sleep, wait);
2916
2917 snd_pcm_stream_lock_irq(substream);
2918 avail = snd_pcm_playback_avail(runtime);
2919 switch (runtime->status->state) {
2920 case SNDRV_PCM_STATE_RUNNING:
2921 case SNDRV_PCM_STATE_PREPARED:
2922 case SNDRV_PCM_STATE_PAUSED:
2923 if (avail >= runtime->control->avail_min) {
2924 mask = POLLOUT | POLLWRNORM;
2925 break;
2926 }
2927 /* Fall through */
2928 case SNDRV_PCM_STATE_DRAINING:
2929 mask = 0;
2930 break;
2931 default:
2932 mask = POLLOUT | POLLWRNORM | POLLERR;
2933 break;
2934 }
2935 snd_pcm_stream_unlock_irq(substream);
2936 return mask;
2937}
2938
2939static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2940{
Takashi Iwai877211f2005-11-17 13:59:38 +01002941 struct snd_pcm_file *pcm_file;
2942 struct snd_pcm_substream *substream;
2943 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 unsigned int mask;
2945 snd_pcm_uframes_t avail;
2946
2947 pcm_file = file->private_data;
2948
2949 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002950 if (PCM_RUNTIME_CHECK(substream))
2951 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 runtime = substream->runtime;
2953
2954 poll_wait(file, &runtime->sleep, wait);
2955
2956 snd_pcm_stream_lock_irq(substream);
2957 avail = snd_pcm_capture_avail(runtime);
2958 switch (runtime->status->state) {
2959 case SNDRV_PCM_STATE_RUNNING:
2960 case SNDRV_PCM_STATE_PREPARED:
2961 case SNDRV_PCM_STATE_PAUSED:
2962 if (avail >= runtime->control->avail_min) {
2963 mask = POLLIN | POLLRDNORM;
2964 break;
2965 }
2966 mask = 0;
2967 break;
2968 case SNDRV_PCM_STATE_DRAINING:
2969 if (avail > 0) {
2970 mask = POLLIN | POLLRDNORM;
2971 break;
2972 }
2973 /* Fall through */
2974 default:
2975 mask = POLLIN | POLLRDNORM | POLLERR;
2976 break;
2977 }
2978 snd_pcm_stream_unlock_irq(substream);
2979 return mask;
2980}
2981
2982/*
2983 * mmap support
2984 */
2985
2986/*
2987 * Only on coherent architectures, we can mmap the status and the control records
2988 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2989 */
2990#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2991/*
2992 * mmap status record
2993 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002994static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2995 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Takashi Iwai877211f2005-11-17 13:59:38 +01002997 struct snd_pcm_substream *substream = area->vm_private_data;
2998 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003001 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003003 vmf->page = virt_to_page(runtime->status);
3004 get_page(vmf->page);
3005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003008static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003010 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011};
3012
Takashi Iwai877211f2005-11-17 13:59:38 +01003013static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 struct vm_area_struct *area)
3015{
Takashi Iwai877211f2005-11-17 13:59:38 +01003016 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 long size;
3018 if (!(area->vm_flags & VM_READ))
3019 return -EINVAL;
3020 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003022 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return -EINVAL;
3024 area->vm_ops = &snd_pcm_vm_ops_status;
3025 area->vm_private_data = substream;
3026 area->vm_flags |= VM_RESERVED;
3027 return 0;
3028}
3029
3030/*
3031 * mmap control record
3032 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003033static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3034 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Takashi Iwai877211f2005-11-17 13:59:38 +01003036 struct snd_pcm_substream *substream = area->vm_private_data;
3037 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003040 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003042 vmf->page = virt_to_page(runtime->control);
3043 get_page(vmf->page);
3044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003047static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003049 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050};
3051
Takashi Iwai877211f2005-11-17 13:59:38 +01003052static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct vm_area_struct *area)
3054{
Takashi Iwai877211f2005-11-17 13:59:38 +01003055 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 long size;
3057 if (!(area->vm_flags & VM_READ))
3058 return -EINVAL;
3059 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003061 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return -EINVAL;
3063 area->vm_ops = &snd_pcm_vm_ops_control;
3064 area->vm_private_data = substream;
3065 area->vm_flags |= VM_RESERVED;
3066 return 0;
3067}
3068#else /* ! coherent mmap */
3069/*
3070 * don't support mmap for status and control records.
3071 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003072static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct vm_area_struct *area)
3074{
3075 return -ENXIO;
3076}
Takashi Iwai877211f2005-11-17 13:59:38 +01003077static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct vm_area_struct *area)
3079{
3080 return -ENXIO;
3081}
3082#endif /* coherent mmap */
3083
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003084static inline struct page *
3085snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3086{
3087 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003088#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3089 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3090 return virt_to_page(CAC_ADDR(vaddr));
3091#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003092#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3093 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3094 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3095 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3096 /* assume dma_handle set via pfn_to_phys() in
3097 * mm/dma-noncoherent.c
3098 */
3099 return pfn_to_page(addr >> PAGE_SHIFT);
3100 }
3101#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003102 return virt_to_page(vaddr);
3103}
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003106 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003108static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3109 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110{
Takashi Iwai877211f2005-11-17 13:59:38 +01003111 struct snd_pcm_substream *substream = area->vm_private_data;
3112 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 unsigned long offset;
3114 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 size_t dma_bytes;
3116
3117 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003118 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003120 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3122 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003123 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003124 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003126 else
3127 page = snd_pcm_default_page_ops(substream, offset);
3128 if (!page)
3129 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003130 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003131 vmf->page = page;
3132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133}
3134
Takashi Iwai657b1982009-11-26 12:40:21 +01003135static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3136 .open = snd_pcm_mmap_data_open,
3137 .close = snd_pcm_mmap_data_close,
3138};
3139
3140static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 .open = snd_pcm_mmap_data_open,
3142 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003143 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144};
3145
Takashi Iwai657b1982009-11-26 12:40:21 +01003146#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3147/* This should be defined / handled globally! */
3148#ifdef CONFIG_ARM
3149#define ARCH_HAS_DMA_MMAP_COHERENT
3150#endif
3151#endif
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153/*
3154 * mmap the DMA buffer on RAM
3155 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003156static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3157 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003160#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3161 if (!substream->ops->page &&
3162 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3163 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3164 area,
3165 substream->runtime->dma_area,
3166 substream->runtime->dma_addr,
3167 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003168#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3169 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3170 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3171 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003172#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3173 /* mmap with fault handler */
3174 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
3176}
3177
3178/*
3179 * mmap the DMA buffer on I/O memory area
3180 */
3181#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003182int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3183 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
3185 long size;
3186 unsigned long offset;
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 area->vm_flags |= VM_IO;
3190 size = area->vm_end - area->vm_start;
3191 offset = area->vm_pgoff << PAGE_SHIFT;
3192 if (io_remap_pfn_range(area, area->vm_start,
3193 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3194 size, area->vm_page_prot))
3195 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return 0;
3197}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003198
3199EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200#endif /* SNDRV_PCM_INFO_MMAP */
3201
Takashi Iwaic32d9772010-01-18 14:58:57 +01003202/* mmap callback with pgprot_noncached */
3203int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3204 struct vm_area_struct *area)
3205{
3206 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3207 return snd_pcm_default_mmap(substream, area);
3208}
3209EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211/*
3212 * mmap DMA buffer
3213 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003214int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct vm_area_struct *area)
3216{
Takashi Iwai877211f2005-11-17 13:59:38 +01003217 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 long size;
3219 unsigned long offset;
3220 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003221 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
3223 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3224 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3225 return -EINVAL;
3226 } else {
3227 if (!(area->vm_flags & VM_READ))
3228 return -EINVAL;
3229 }
3230 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3232 return -EBADFD;
3233 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3234 return -ENXIO;
3235 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3236 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3237 return -EINVAL;
3238 size = area->vm_end - area->vm_start;
3239 offset = area->vm_pgoff << PAGE_SHIFT;
3240 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3241 if ((size_t)size > dma_bytes)
3242 return -EINVAL;
3243 if (offset > dma_bytes - size)
3244 return -EINVAL;
3245
Takashi Iwai657b1982009-11-26 12:40:21 +01003246 area->vm_ops = &snd_pcm_vm_ops_data;
3247 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003249 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003251 err = snd_pcm_default_mmap(substream, area);
3252 if (!err)
3253 atomic_inc(&substream->mmap_count);
3254 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003257EXPORT_SYMBOL(snd_pcm_mmap_data);
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3260{
Takashi Iwai877211f2005-11-17 13:59:38 +01003261 struct snd_pcm_file * pcm_file;
3262 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 unsigned long offset;
3264
3265 pcm_file = file->private_data;
3266 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003267 if (PCM_RUNTIME_CHECK(substream))
3268 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 offset = area->vm_pgoff << PAGE_SHIFT;
3271 switch (offset) {
3272 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003273 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return -ENXIO;
3275 return snd_pcm_mmap_status(substream, file, area);
3276 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003277 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 return -ENXIO;
3279 return snd_pcm_mmap_control(substream, file, area);
3280 default:
3281 return snd_pcm_mmap_data(substream, file, area);
3282 }
3283 return 0;
3284}
3285
3286static int snd_pcm_fasync(int fd, struct file * file, int on)
3287{
Takashi Iwai877211f2005-11-17 13:59:38 +01003288 struct snd_pcm_file * pcm_file;
3289 struct snd_pcm_substream *substream;
3290 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 pcm_file = file->private_data;
3293 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003294 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003295 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003297 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300/*
3301 * ioctl32 compat
3302 */
3303#ifdef CONFIG_COMPAT
3304#include "pcm_compat.c"
3305#else
3306#define snd_pcm_ioctl_compat NULL
3307#endif
3308
3309/*
3310 * To be removed helpers to keep binary compatibility
3311 */
3312
Takashi Iwai59d48582005-12-01 10:51:58 +01003313#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3315#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3316
Takashi Iwai877211f2005-11-17 13:59:38 +01003317static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3318 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 unsigned int i;
3321
3322 memset(params, 0, sizeof(*params));
3323 params->flags = oparams->flags;
3324 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3325 params->masks[i].bits[0] = oparams->masks[i];
3326 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3327 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3328 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3329 params->info = oparams->info;
3330 params->msbits = oparams->msbits;
3331 params->rate_num = oparams->rate_num;
3332 params->rate_den = oparams->rate_den;
3333 params->fifo_size = oparams->fifo_size;
3334}
3335
Takashi Iwai877211f2005-11-17 13:59:38 +01003336static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3337 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 unsigned int i;
3340
3341 memset(oparams, 0, sizeof(*oparams));
3342 oparams->flags = params->flags;
3343 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3344 oparams->masks[i] = params->masks[i].bits[0];
3345 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3346 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3347 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3348 oparams->info = params->info;
3349 oparams->msbits = params->msbits;
3350 oparams->rate_num = params->rate_num;
3351 oparams->rate_den = params->rate_den;
3352 oparams->fifo_size = params->fifo_size;
3353}
3354
Takashi Iwai877211f2005-11-17 13:59:38 +01003355static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Takashi Iwai877211f2005-11-17 13:59:38 +01003358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 int err;
3361
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003363 if (!params)
3364 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Li Zefanef44a1e2009-04-10 09:43:08 +08003366 oparams = memdup_user(_oparams, sizeof(*oparams));
3367 if (IS_ERR(oparams)) {
3368 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 goto out;
3370 }
3371 snd_pcm_hw_convert_from_old_params(params, oparams);
3372 err = snd_pcm_hw_refine(substream, params);
3373 snd_pcm_hw_convert_to_old_params(oparams, params);
3374 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3375 if (!err)
3376 err = -EFAULT;
3377 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003378
3379 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380out:
3381 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return err;
3383}
3384
Takashi Iwai877211f2005-11-17 13:59:38 +01003385static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3386 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
Takashi Iwai877211f2005-11-17 13:59:38 +01003388 struct snd_pcm_hw_params *params;
3389 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 int err;
3391
3392 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003393 if (!params)
3394 return -ENOMEM;
3395
3396 oparams = memdup_user(_oparams, sizeof(*oparams));
3397 if (IS_ERR(oparams)) {
3398 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 goto out;
3400 }
3401 snd_pcm_hw_convert_from_old_params(params, oparams);
3402 err = snd_pcm_hw_params(substream, params);
3403 snd_pcm_hw_convert_to_old_params(oparams, params);
3404 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3405 if (!err)
3406 err = -EFAULT;
3407 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003408
3409 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410out:
3411 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return err;
3413}
Takashi Iwai59d48582005-12-01 10:51:58 +01003414#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Cliff Cai70036092008-09-03 10:54:36 +02003416#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003417static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3418 unsigned long addr,
3419 unsigned long len,
3420 unsigned long pgoff,
3421 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003422{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003423 struct snd_pcm_file *pcm_file = file->private_data;
3424 struct snd_pcm_substream *substream = pcm_file->substream;
3425 struct snd_pcm_runtime *runtime = substream->runtime;
3426 unsigned long offset = pgoff << PAGE_SHIFT;
3427
3428 switch (offset) {
3429 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3430 return (unsigned long)runtime->status;
3431 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3432 return (unsigned long)runtime->control;
3433 default:
3434 return (unsigned long)runtime->dma_area + offset;
3435 }
Cliff Cai70036092008-09-03 10:54:36 +02003436}
3437#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003438# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003439#endif
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441/*
3442 * Register section
3443 */
3444
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003445const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003447 .owner = THIS_MODULE,
3448 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003449 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003450 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003451 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003452 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003453 .poll = snd_pcm_playback_poll,
3454 .unlocked_ioctl = snd_pcm_playback_ioctl,
3455 .compat_ioctl = snd_pcm_ioctl_compat,
3456 .mmap = snd_pcm_mmap,
3457 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003458 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 },
3460 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003461 .owner = THIS_MODULE,
3462 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003463 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003464 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003465 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003466 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003467 .poll = snd_pcm_capture_poll,
3468 .unlocked_ioctl = snd_pcm_capture_ioctl,
3469 .compat_ioctl = snd_pcm_ioctl_compat,
3470 .mmap = snd_pcm_mmap,
3471 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003472 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
3474};