blob: 134fc6c2e08dc01eeda84a730545b0532f0588fe [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
145char *snd_pcm_hw_param_names[] = {
146 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 runtime->status->state = state;
868 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
869 runtime->silence_size > 0)
870 snd_pcm_playback_silence(substream, ULONG_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
873 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}
875
876static struct action_ops snd_pcm_action_start = {
877 .pre_action = snd_pcm_pre_start,
878 .do_action = snd_pcm_do_start,
879 .undo_action = snd_pcm_undo_start,
880 .post_action = snd_pcm_post_start
881};
882
883/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700884 * snd_pcm_start - start all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +0200885 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100887int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Takashi Iwai877211f2005-11-17 13:59:38 +0100889 return snd_pcm_action(&snd_pcm_action_start, substream,
890 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893/*
894 * stop callbacks
895 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100896static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Takashi Iwai877211f2005-11-17 13:59:38 +0100898 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
900 return -EBADFD;
901 runtime->trigger_master = substream;
902 return 0;
903}
904
Takashi Iwai877211f2005-11-17 13:59:38 +0100905static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 if (substream->runtime->trigger_master == substream &&
908 snd_pcm_running(substream))
909 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
910 return 0; /* unconditonally stop all substreams */
911}
912
Takashi Iwai877211f2005-11-17 13:59:38 +0100913static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Takashi Iwai877211f2005-11-17 13:59:38 +0100915 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (runtime->status->state != state) {
917 snd_pcm_trigger_tstamp(substream);
918 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100919 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
920 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 runtime->status->state = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +0100924 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
927static struct action_ops snd_pcm_action_stop = {
928 .pre_action = snd_pcm_pre_stop,
929 .do_action = snd_pcm_do_stop,
930 .post_action = snd_pcm_post_stop
931};
932
933/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700934 * snd_pcm_stop - try to stop all running streams in the substream group
Takashi Iwaidf8db932005-09-07 13:38:19 +0200935 * @substream: the PCM substream instance
936 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700938 * The state of each stream is then changed to the given state unconditionally.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100940int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
942 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
943}
944
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200945EXPORT_SYMBOL(snd_pcm_stop);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700948 * snd_pcm_drain_done - stop the DMA only when the given stream is playback
Takashi Iwaidf8db932005-09-07 13:38:19 +0200949 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 *
Randy Dunlap1c85cc62008-10-15 14:38:40 -0700951 * After stopping, the state is changed to SETUP.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * Unlike snd_pcm_stop(), this affects only the given stream.
953 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100954int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Takashi Iwai877211f2005-11-17 13:59:38 +0100956 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
957 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
960/*
961 * pause callbacks
962 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100963static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwai877211f2005-11-17 13:59:38 +0100965 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
967 return -ENOSYS;
968 if (push) {
969 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
970 return -EBADFD;
971 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
972 return -EBADFD;
973 runtime->trigger_master = substream;
974 return 0;
975}
976
Takashi Iwai877211f2005-11-17 13:59:38 +0100977static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 if (substream->runtime->trigger_master != substream)
980 return 0;
Jaroslav Kysela56385a12010-08-18 14:08:17 +0200981 /* some drivers might use hw_ptr to recover from the pause -
982 update the hw_ptr now */
983 if (push)
984 snd_pcm_update_hw_ptr(substream);
Takashi Iwai6af3fb72009-05-27 10:49:26 +0200985 /* The jiffies check in snd_pcm_update_hw_ptr*() is done by
986 * a delta betwen the current jiffies, this gives a large enough
987 * delta, effectively to skip the check once.
988 */
989 substream->runtime->hw_ptr_jiffies = jiffies - HZ * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return substream->ops->trigger(substream,
991 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
992 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
993}
994
Takashi Iwai877211f2005-11-17 13:59:38 +0100995static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 if (substream->runtime->trigger_master == substream)
998 substream->ops->trigger(substream,
999 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1000 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1001}
1002
Takashi Iwai877211f2005-11-17 13:59:38 +01001003static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Takashi Iwai877211f2005-11-17 13:59:38 +01001005 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 snd_pcm_trigger_tstamp(substream);
1007 if (push) {
1008 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1009 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001010 snd_timer_notify(substream->timer,
1011 SNDRV_TIMER_EVENT_MPAUSE,
1012 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001014 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 } else {
1016 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001018 snd_timer_notify(substream->timer,
1019 SNDRV_TIMER_EVENT_MCONTINUE,
1020 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
1022}
1023
1024static struct action_ops snd_pcm_action_pause = {
1025 .pre_action = snd_pcm_pre_pause,
1026 .do_action = snd_pcm_do_pause,
1027 .undo_action = snd_pcm_undo_pause,
1028 .post_action = snd_pcm_post_pause
1029};
1030
1031/*
1032 * Push/release the pause for all linked streams.
1033 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001034static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1037}
1038
1039#ifdef CONFIG_PM
1040/* suspend */
1041
Takashi Iwai877211f2005-11-17 13:59:38 +01001042static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
Takashi Iwai877211f2005-11-17 13:59:38 +01001044 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1046 return -EBUSY;
1047 runtime->trigger_master = substream;
1048 return 0;
1049}
1050
Takashi Iwai877211f2005-11-17 13:59:38 +01001051static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Takashi Iwai877211f2005-11-17 13:59:38 +01001053 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (runtime->trigger_master != substream)
1055 return 0;
1056 if (! snd_pcm_running(substream))
1057 return 0;
1058 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1059 return 0; /* suspend unconditionally */
1060}
1061
Takashi Iwai877211f2005-11-17 13:59:38 +01001062static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Takashi Iwai877211f2005-11-17 13:59:38 +01001064 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 snd_pcm_trigger_tstamp(substream);
1066 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001067 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1068 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 runtime->status->suspended_state = runtime->status->state;
1070 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 wake_up(&runtime->sleep);
Jaroslav Kyselac91a9882010-01-21 10:32:15 +01001072 wake_up(&runtime->tsleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static struct action_ops snd_pcm_action_suspend = {
1076 .pre_action = snd_pcm_pre_suspend,
1077 .do_action = snd_pcm_do_suspend,
1078 .post_action = snd_pcm_post_suspend
1079};
1080
1081/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001082 * snd_pcm_suspend - trigger SUSPEND to all linked streams
Takashi Iwaidf8db932005-09-07 13:38:19 +02001083 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * After this call, all streams are changed to SUSPENDED state.
1086 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001087int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
1089 int err;
1090 unsigned long flags;
1091
Takashi Iwai603bf5242005-11-17 15:59:14 +01001092 if (! substream)
1093 return 0;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 snd_pcm_stream_lock_irqsave(substream, flags);
1096 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1097 snd_pcm_stream_unlock_irqrestore(substream, flags);
1098 return err;
1099}
1100
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001101EXPORT_SYMBOL(snd_pcm_suspend);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001104 * snd_pcm_suspend_all - trigger SUSPEND to all substreams in the given pcm
Takashi Iwaidf8db932005-09-07 13:38:19 +02001105 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 * After this call, all streams are changed to SUSPENDED state.
1108 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001109int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
Takashi Iwai877211f2005-11-17 13:59:38 +01001111 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 int stream, err = 0;
1113
Takashi Iwai603bf5242005-11-17 15:59:14 +01001114 if (! pcm)
1115 return 0;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001118 for (substream = pcm->streams[stream].substream;
1119 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /* FIXME: the open/close code should lock this as well */
1121 if (substream->runtime == NULL)
1122 continue;
1123 err = snd_pcm_suspend(substream);
1124 if (err < 0 && err != -EBUSY)
1125 return err;
1126 }
1127 }
1128 return 0;
1129}
1130
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001131EXPORT_SYMBOL(snd_pcm_suspend_all);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/* resume */
1134
Takashi Iwai877211f2005-11-17 13:59:38 +01001135static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Takashi Iwai877211f2005-11-17 13:59:38 +01001137 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1139 return -ENOSYS;
1140 runtime->trigger_master = substream;
1141 return 0;
1142}
1143
Takashi Iwai877211f2005-11-17 13:59:38 +01001144static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Takashi Iwai877211f2005-11-17 13:59:38 +01001146 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (runtime->trigger_master != substream)
1148 return 0;
1149 /* DMA not running previously? */
1150 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1151 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1152 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1153 return 0;
1154 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1155}
1156
Takashi Iwai877211f2005-11-17 13:59:38 +01001157static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 if (substream->runtime->trigger_master == substream &&
1160 snd_pcm_running(substream))
1161 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1162}
1163
Takashi Iwai877211f2005-11-17 13:59:38 +01001164static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Takashi Iwai877211f2005-11-17 13:59:38 +01001166 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 snd_pcm_trigger_tstamp(substream);
1168 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1170 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 runtime->status->state = runtime->status->suspended_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static struct action_ops snd_pcm_action_resume = {
1175 .pre_action = snd_pcm_pre_resume,
1176 .do_action = snd_pcm_do_resume,
1177 .undo_action = snd_pcm_undo_resume,
1178 .post_action = snd_pcm_post_resume
1179};
1180
Takashi Iwai877211f2005-11-17 13:59:38 +01001181static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Takashi Iwai877211f2005-11-17 13:59:38 +01001183 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int res;
1185
1186 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001187 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1189 snd_power_unlock(card);
1190 return res;
1191}
1192
1193#else
1194
Takashi Iwai877211f2005-11-17 13:59:38 +01001195static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 return -ENOSYS;
1198}
1199
1200#endif /* CONFIG_PM */
1201
1202/*
1203 * xrun ioctl
1204 *
1205 * Change the RUNNING stream(s) to XRUN state.
1206 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001207static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Takashi Iwai877211f2005-11-17 13:59:38 +01001209 struct snd_card *card = substream->pcm->card;
1210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int result;
1212
1213 snd_power_lock(card);
1214 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001215 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (result < 0)
1217 goto _unlock;
1218 }
1219
1220 snd_pcm_stream_lock_irq(substream);
1221 switch (runtime->status->state) {
1222 case SNDRV_PCM_STATE_XRUN:
1223 result = 0; /* already there */
1224 break;
1225 case SNDRV_PCM_STATE_RUNNING:
1226 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1227 break;
1228 default:
1229 result = -EBADFD;
1230 }
1231 snd_pcm_stream_unlock_irq(substream);
1232 _unlock:
1233 snd_power_unlock(card);
1234 return result;
1235}
1236
1237/*
1238 * reset ioctl
1239 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001240static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Takashi Iwai877211f2005-11-17 13:59:38 +01001242 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 switch (runtime->status->state) {
1244 case SNDRV_PCM_STATE_RUNNING:
1245 case SNDRV_PCM_STATE_PREPARED:
1246 case SNDRV_PCM_STATE_PAUSED:
1247 case SNDRV_PCM_STATE_SUSPENDED:
1248 return 0;
1249 default:
1250 return -EBADFD;
1251 }
1252}
1253
Takashi Iwai877211f2005-11-17 13:59:38 +01001254static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Takashi Iwai877211f2005-11-17 13:59:38 +01001256 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1258 if (err < 0)
1259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 runtime->hw_ptr_base = 0;
Jaroslav Kyselae7636922010-01-26 17:08:24 +01001261 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1262 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 runtime->silence_start = runtime->status->hw_ptr;
1264 runtime->silence_filled = 0;
1265 return 0;
1266}
1267
Takashi Iwai877211f2005-11-17 13:59:38 +01001268static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Takashi Iwai877211f2005-11-17 13:59:38 +01001270 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 runtime->control->appl_ptr = runtime->status->hw_ptr;
1272 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1273 runtime->silence_size > 0)
1274 snd_pcm_playback_silence(substream, ULONG_MAX);
1275}
1276
1277static struct action_ops snd_pcm_action_reset = {
1278 .pre_action = snd_pcm_pre_reset,
1279 .do_action = snd_pcm_do_reset,
1280 .post_action = snd_pcm_post_reset
1281};
1282
Takashi Iwai877211f2005-11-17 13:59:38 +01001283static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1286}
1287
1288/*
1289 * prepare ioctl
1290 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001291/* we use the second argument for updating f_flags */
1292static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1293 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Takashi Iwai877211f2005-11-17 13:59:38 +01001295 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001296 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1297 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EBADFD;
1299 if (snd_pcm_running(substream))
1300 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001301 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return 0;
1303}
1304
Takashi Iwai877211f2005-11-17 13:59:38 +01001305static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 int err;
1308 err = substream->ops->prepare(substream);
1309 if (err < 0)
1310 return err;
1311 return snd_pcm_do_reset(substream, 0);
1312}
1313
Takashi Iwai877211f2005-11-17 13:59:38 +01001314static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Takashi Iwai877211f2005-11-17 13:59:38 +01001316 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 runtime->control->appl_ptr = runtime->status->hw_ptr;
1318 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1319}
1320
1321static struct action_ops snd_pcm_action_prepare = {
1322 .pre_action = snd_pcm_pre_prepare,
1323 .do_action = snd_pcm_do_prepare,
1324 .post_action = snd_pcm_post_prepare
1325};
1326
1327/**
Randy Dunlap1c85cc62008-10-15 14:38:40 -07001328 * snd_pcm_prepare - prepare the PCM substream to be triggerable
Takashi Iwaidf8db932005-09-07 13:38:19 +02001329 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001330 * @file: file to refer f_flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001332static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1333 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001336 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001337 int f_flags;
1338
1339 if (file)
1340 f_flags = file->f_flags;
1341 else
1342 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001345 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001346 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1347 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 snd_power_unlock(card);
1349 return res;
1350}
1351
1352/*
1353 * drain ioctl
1354 */
1355
Takashi Iwai877211f2005-11-17 13:59:38 +01001356static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 substream->runtime->trigger_master = substream;
1359 return 0;
1360}
1361
Takashi Iwai877211f2005-11-17 13:59:38 +01001362static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Takashi Iwai877211f2005-11-17 13:59:38 +01001364 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1366 switch (runtime->status->state) {
1367 case SNDRV_PCM_STATE_PREPARED:
1368 /* start playback stream if possible */
1369 if (! snd_pcm_playback_empty(substream)) {
1370 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1371 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1372 }
1373 break;
1374 case SNDRV_PCM_STATE_RUNNING:
1375 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1376 break;
1377 default:
1378 break;
1379 }
1380 } else {
1381 /* stop running stream */
1382 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001383 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Ślusarzb05e5782007-12-14 12:50:16 +01001385 snd_pcm_do_stop(substream, new_state);
1386 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 }
1388 }
1389 return 0;
1390}
1391
Takashi Iwai877211f2005-11-17 13:59:38 +01001392static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394}
1395
1396static struct action_ops snd_pcm_action_drain_init = {
1397 .pre_action = snd_pcm_pre_drain_init,
1398 .do_action = snd_pcm_do_drain_init,
1399 .post_action = snd_pcm_post_drain_init
1400};
1401
Takashi Iwai877211f2005-11-17 13:59:38 +01001402static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404/*
1405 * Drain the stream(s).
1406 * When the substream is linked, sync until the draining of all playback streams
1407 * is finished.
1408 * After this call, all streams are supposed to be either SETUP or DRAINING
1409 * (capture only) state.
1410 */
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001411static int snd_pcm_drain(struct snd_pcm_substream *substream,
1412 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Takashi Iwai877211f2005-11-17 13:59:38 +01001414 struct snd_card *card;
1415 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001416 struct snd_pcm_substream *s;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001417 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int result = 0;
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001419 int nonblock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 card = substream->pcm->card;
1422 runtime = substream->runtime;
1423
1424 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1425 return -EBADFD;
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 snd_power_lock(card);
1428 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001429 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001430 if (result < 0) {
1431 snd_power_unlock(card);
1432 return result;
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001436 if (file) {
1437 if (file->f_flags & O_NONBLOCK)
1438 nonblock = 1;
1439 } else if (substream->f_flags & O_NONBLOCK)
1440 nonblock = 1;
1441
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001442 down_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001443 snd_pcm_stream_lock_irq(substream);
1444 /* resume pause */
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001445 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001446 snd_pcm_pause(substream, 0);
1447
1448 /* pre-start/stop - all running streams are changed to DRAINING state */
1449 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001450 if (result < 0)
1451 goto unlock;
1452 /* in non-blocking, we don't wait in ioctl but let caller poll */
1453 if (nonblock) {
1454 result = -EAGAIN;
1455 goto unlock;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 for (;;) {
1459 long tout;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001460 struct snd_pcm_runtime *to_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (signal_pending(current)) {
1462 result = -ERESTARTSYS;
1463 break;
1464 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001465 /* find a substream to drain */
1466 to_check = NULL;
1467 snd_pcm_group_for_each_entry(s, substream) {
1468 if (s->stream != SNDRV_PCM_STREAM_PLAYBACK)
1469 continue;
1470 runtime = s->runtime;
1471 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
1472 to_check = runtime;
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001473 break;
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001474 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001475 }
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001476 if (!to_check)
1477 break; /* all drained */
1478 init_waitqueue_entry(&wait, current);
1479 add_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 set_current_state(TASK_INTERRUPTIBLE);
1481 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001482 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 snd_power_unlock(card);
1484 tout = schedule_timeout(10 * HZ);
1485 snd_power_lock(card);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001486 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 snd_pcm_stream_lock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001488 remove_wait_queue(&to_check->sleep, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (tout == 0) {
1490 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1491 result = -ESTRPIPE;
1492 else {
1493 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1494 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1495 result = -EIO;
1496 }
1497 break;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001500
Takashi Iwai4cdc1152009-08-20 16:40:16 +02001501 unlock:
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001502 snd_pcm_stream_unlock_irq(substream);
Takashi Iwaid3a7dcf2009-09-17 18:46:26 +02001503 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 return result;
1507}
1508
1509/*
1510 * drop ioctl
1511 *
1512 * Immediately put all linked substreams into SETUP state.
1513 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001514static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Takashi Iwai877211f2005-11-17 13:59:38 +01001516 struct snd_pcm_runtime *runtime;
1517 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 int result = 0;
1519
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001520 if (PCM_RUNTIME_CHECK(substream))
1521 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 runtime = substream->runtime;
1523 card = substream->pcm->card;
1524
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001526 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED ||
1527 runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return -EBADFD;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 snd_pcm_stream_lock_irq(substream);
1531 /* resume pause */
1532 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1533 snd_pcm_pause(substream, 0);
1534
1535 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1536 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1537 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai24e8fc42008-09-25 17:51:11 +02001538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return result;
1540}
1541
1542
1543/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544static struct file *snd_pcm_file_fd(int fd)
1545{
1546 struct file *file;
1547 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001548 unsigned int minor;
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 file = fget(fd);
1551 if (!file)
1552 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001553 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (!S_ISCHR(inode->i_mode) ||
1555 imajor(inode) != snd_major) {
1556 fput(file);
1557 return NULL;
1558 }
1559 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001560 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1561 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 fput(file);
1563 return NULL;
1564 }
1565 return file;
1566}
1567
1568/*
1569 * PCM link handling
1570 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001571static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 int res = 0;
1574 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001575 struct snd_pcm_file *pcm_file;
1576 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 file = snd_pcm_file_fd(fd);
1579 if (!file)
1580 return -EBADFD;
1581 pcm_file = file->private_data;
1582 substream1 = pcm_file->substream;
1583 down_write(&snd_pcm_link_rwsem);
1584 write_lock_irq(&snd_pcm_link_rwlock);
1585 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1586 substream->runtime->status->state != substream1->runtime->status->state) {
1587 res = -EBADFD;
1588 goto _end;
1589 }
1590 if (snd_pcm_stream_linked(substream1)) {
1591 res = -EALREADY;
1592 goto _end;
1593 }
1594 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001595 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (substream->group == NULL) {
1597 res = -ENOMEM;
1598 goto _end;
1599 }
1600 spin_lock_init(&substream->group->lock);
1601 INIT_LIST_HEAD(&substream->group->substreams);
1602 list_add_tail(&substream->link_list, &substream->group->substreams);
1603 substream->group->count = 1;
1604 }
1605 list_add_tail(&substream1->link_list, &substream->group->substreams);
1606 substream->group->count++;
1607 substream1->group = substream->group;
1608 _end:
1609 write_unlock_irq(&snd_pcm_link_rwlock);
1610 up_write(&snd_pcm_link_rwsem);
1611 fput(file);
1612 return res;
1613}
1614
Takashi Iwai877211f2005-11-17 13:59:38 +01001615static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 substream->group = &substream->self_group;
1618 INIT_LIST_HEAD(&substream->self_group.substreams);
1619 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1620}
1621
Takashi Iwai877211f2005-11-17 13:59:38 +01001622static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Takashi Iwaief991b92007-02-22 12:52:53 +01001624 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 int res = 0;
1626
1627 down_write(&snd_pcm_link_rwsem);
1628 write_lock_irq(&snd_pcm_link_rwlock);
1629 if (!snd_pcm_stream_linked(substream)) {
1630 res = -EALREADY;
1631 goto _end;
1632 }
1633 list_del(&substream->link_list);
1634 substream->group->count--;
1635 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001636 snd_pcm_group_for_each_entry(s, substream) {
1637 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 break;
1639 }
1640 kfree(substream->group);
1641 }
1642 relink_to_local(substream);
1643 _end:
1644 write_unlock_irq(&snd_pcm_link_rwlock);
1645 up_write(&snd_pcm_link_rwsem);
1646 return res;
1647}
1648
1649/*
1650 * hw configurator
1651 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001652static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1653 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Takashi Iwai877211f2005-11-17 13:59:38 +01001655 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1657 hw_param_interval_c(params, rule->deps[1]), &t);
1658 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1659}
1660
Takashi Iwai877211f2005-11-17 13:59:38 +01001661static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1662 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Takashi Iwai877211f2005-11-17 13:59:38 +01001664 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1666 hw_param_interval_c(params, rule->deps[1]), &t);
1667 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1668}
1669
Takashi Iwai877211f2005-11-17 13:59:38 +01001670static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1671 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
Takashi Iwai877211f2005-11-17 13:59:38 +01001673 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1675 hw_param_interval_c(params, rule->deps[1]),
1676 (unsigned long) rule->private, &t);
1677 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1678}
1679
Takashi Iwai877211f2005-11-17 13:59:38 +01001680static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1681 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Takashi Iwai877211f2005-11-17 13:59:38 +01001683 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1685 (unsigned long) rule->private,
1686 hw_param_interval_c(params, rule->deps[1]), &t);
1687 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1688}
1689
Takashi Iwai877211f2005-11-17 13:59:38 +01001690static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1691 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001694 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1695 struct snd_mask m;
1696 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 snd_mask_any(&m);
1698 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1699 int bits;
1700 if (! snd_mask_test(mask, k))
1701 continue;
1702 bits = snd_pcm_format_physical_width(k);
1703 if (bits <= 0)
1704 continue; /* ignore invalid formats */
1705 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1706 snd_mask_reset(&m, k);
1707 }
1708 return snd_mask_refine(mask, &m);
1709}
1710
Takashi Iwai877211f2005-11-17 13:59:38 +01001711static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1712 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwai877211f2005-11-17 13:59:38 +01001714 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned int k;
1716 t.min = UINT_MAX;
1717 t.max = 0;
1718 t.openmin = 0;
1719 t.openmax = 0;
1720 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1721 int bits;
1722 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1723 continue;
1724 bits = snd_pcm_format_physical_width(k);
1725 if (bits <= 0)
1726 continue; /* ignore invalid formats */
1727 if (t.min > (unsigned)bits)
1728 t.min = bits;
1729 if (t.max < (unsigned)bits)
1730 t.max = bits;
1731 }
1732 t.integer = 1;
1733 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1734}
1735
1736#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1737#error "Change this table"
1738#endif
1739
1740static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1741 48000, 64000, 88200, 96000, 176400, 192000 };
1742
Clemens Ladisch7653d552007-08-13 17:38:54 +02001743const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1744 .count = ARRAY_SIZE(rates),
1745 .list = rates,
1746};
1747
Takashi Iwai877211f2005-11-17 13:59:38 +01001748static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1749 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Takashi Iwai877211f2005-11-17 13:59:38 +01001751 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001753 snd_pcm_known_rates.count,
1754 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Takashi Iwai877211f2005-11-17 13:59:38 +01001757static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1758 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Takashi Iwai877211f2005-11-17 13:59:38 +01001760 struct snd_interval t;
1761 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 t.min = 0;
1763 t.max = substream->buffer_bytes_max;
1764 t.openmin = 0;
1765 t.openmax = 0;
1766 t.integer = 1;
1767 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1768}
1769
Takashi Iwai877211f2005-11-17 13:59:38 +01001770int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Takashi Iwai877211f2005-11-17 13:59:38 +01001772 struct snd_pcm_runtime *runtime = substream->runtime;
1773 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int k, err;
1775
1776 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1777 snd_mask_any(constrs_mask(constrs, k));
1778 }
1779
1780 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1781 snd_interval_any(constrs_interval(constrs, k));
1782 }
1783
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1785 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1786 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1787 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1788 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1789
1790 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1791 snd_pcm_hw_rule_format, NULL,
1792 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1793 if (err < 0)
1794 return err;
1795 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1796 snd_pcm_hw_rule_sample_bits, NULL,
1797 SNDRV_PCM_HW_PARAM_FORMAT,
1798 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1799 if (err < 0)
1800 return err;
1801 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1802 snd_pcm_hw_rule_div, NULL,
1803 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1804 if (err < 0)
1805 return err;
1806 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1807 snd_pcm_hw_rule_mul, NULL,
1808 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1809 if (err < 0)
1810 return err;
1811 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1812 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1813 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1814 if (err < 0)
1815 return err;
1816 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1817 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1818 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1819 if (err < 0)
1820 return err;
1821 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1822 snd_pcm_hw_rule_div, NULL,
1823 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1824 if (err < 0)
1825 return err;
1826 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1827 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1828 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1829 if (err < 0)
1830 return err;
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1832 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1833 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1837 snd_pcm_hw_rule_div, NULL,
1838 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1839 if (err < 0)
1840 return err;
1841 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1842 snd_pcm_hw_rule_div, NULL,
1843 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1844 if (err < 0)
1845 return err;
1846 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1847 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1848 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1849 if (err < 0)
1850 return err;
1851 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1852 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1853 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1854 if (err < 0)
1855 return err;
1856 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1857 snd_pcm_hw_rule_mul, NULL,
1858 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1859 if (err < 0)
1860 return err;
1861 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1862 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1863 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1864 if (err < 0)
1865 return err;
1866 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1867 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1868 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1869 if (err < 0)
1870 return err;
1871 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1872 snd_pcm_hw_rule_muldivk, (void*) 8,
1873 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1874 if (err < 0)
1875 return err;
1876 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1877 snd_pcm_hw_rule_muldivk, (void*) 8,
1878 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1879 if (err < 0)
1880 return err;
1881 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1882 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1883 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1884 if (err < 0)
1885 return err;
1886 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1887 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1888 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1889 if (err < 0)
1890 return err;
1891 return 0;
1892}
1893
Takashi Iwai877211f2005-11-17 13:59:38 +01001894int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Takashi Iwai877211f2005-11-17 13:59:38 +01001896 struct snd_pcm_runtime *runtime = substream->runtime;
1897 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int err;
1899 unsigned int mask = 0;
1900
1901 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1902 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1903 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1904 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1905 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1906 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1907 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1908 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1909 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1910 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1911 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1912 }
1913 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001914 if (err < 0)
1915 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001918 if (err < 0)
1919 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001922 if (err < 0)
1923 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1926 hw->channels_min, hw->channels_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001927 if (err < 0)
1928 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1931 hw->rate_min, hw->rate_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001932 if (err < 0)
1933 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1936 hw->period_bytes_min, hw->period_bytes_max);
Guennadi Liakhovetski8b90ca02009-12-24 01:17:46 +01001937 if (err < 0)
1938 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1941 hw->periods_min, hw->periods_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001942 if (err < 0)
1943 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1946 hw->period_bytes_min, hw->buffer_bytes_max);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001947 if (err < 0)
1948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1951 snd_pcm_hw_rule_buffer_bytes_max, substream,
1952 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1953 if (err < 0)
1954 return err;
1955
1956 /* FIXME: remove */
1957 if (runtime->dma_bytes) {
1958 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
Takashi Iwai7eaa9432008-08-08 17:09:09 +02001959 if (err < 0)
1960 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1964 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1965 snd_pcm_hw_rule_rate, hw,
1966 SNDRV_PCM_HW_PARAM_RATE, -1);
1967 if (err < 0)
1968 return err;
1969 }
1970
1971 /* FIXME: this belong to lowlevel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1973
1974 return 0;
1975}
1976
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001977static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001980}
1981
1982void snd_pcm_release_substream(struct snd_pcm_substream *substream)
1983{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001984 substream->ref_count--;
1985 if (substream->ref_count > 0)
1986 return;
1987
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001988 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001989 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (substream->ops->hw_free != NULL)
1991 substream->ops->hw_free(substream);
1992 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001993 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Takashi Iwai15762742006-04-06 19:47:42 +02001995 if (substream->pcm_release) {
1996 substream->pcm_release(substream);
1997 substream->pcm_release = NULL;
1998 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02001999 snd_pcm_detach_substream(substream);
2000}
2001
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002002EXPORT_SYMBOL(snd_pcm_release_substream);
2003
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002004int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2005 struct file *file,
2006 struct snd_pcm_substream **rsubstream)
2007{
2008 struct snd_pcm_substream *substream;
2009 int err;
2010
2011 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2012 if (err < 0)
2013 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002014 if (substream->ref_count > 1) {
2015 *rsubstream = substream;
2016 return 0;
2017 }
2018
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002019 err = snd_pcm_hw_constraints_init(substream);
2020 if (err < 0) {
2021 snd_printd("snd_pcm_hw_constraints_init failed\n");
2022 goto error;
2023 }
2024
2025 if ((err = substream->ops->open(substream)) < 0)
2026 goto error;
2027
2028 substream->hw_opened = 1;
2029
2030 err = snd_pcm_hw_constraints_complete(substream);
2031 if (err < 0) {
2032 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2033 goto error;
2034 }
2035
2036 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002038
2039 error:
2040 snd_pcm_release_substream(substream);
2041 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002044EXPORT_SYMBOL(snd_pcm_open_substream);
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002047 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002049 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Takashi Iwai877211f2005-11-17 13:59:38 +01002051 struct snd_pcm_file *pcm_file;
2052 struct snd_pcm_substream *substream;
2053 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002054 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002056 if (rpcm_file)
2057 *rpcm_file = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002059 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2060 if (err < 0)
2061 return err;
2062
Takashi Iwai548a6482006-07-31 16:51:51 +02002063 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2064 if (pcm_file == NULL) {
2065 snd_pcm_release_substream(substream);
2066 return -ENOMEM;
2067 }
2068 pcm_file->substream = substream;
2069 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002070 str = substream->pstr;
2071 substream->file = pcm_file;
2072 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 file->private_data = pcm_file;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002075 if (rpcm_file)
2076 *rpcm_file = pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return 0;
2078}
2079
Clemens Ladischf87135f2005-11-20 14:06:59 +01002080static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Takashi Iwai877211f2005-11-17 13:59:38 +01002082 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002083 int err = nonseekable_open(inode, file);
2084 if (err < 0)
2085 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002086 pcm = snd_lookup_minor_data(iminor(inode),
2087 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2088 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2089}
2090
2091static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2092{
2093 struct snd_pcm *pcm;
Takashi Iwai02f48652010-04-13 11:49:04 +02002094 int err = nonseekable_open(inode, file);
2095 if (err < 0)
2096 return err;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002097 pcm = snd_lookup_minor_data(iminor(inode),
2098 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2099 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2100}
2101
2102static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2103{
2104 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002105 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 wait_queue_t wait;
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (pcm == NULL) {
2109 err = -ENODEV;
2110 goto __error1;
2111 }
2112 err = snd_card_file_add(pcm->card, file);
2113 if (err < 0)
2114 goto __error1;
2115 if (!try_module_get(pcm->card->module)) {
2116 err = -EFAULT;
2117 goto __error2;
2118 }
2119 init_waitqueue_entry(&wait, current);
2120 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002121 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002123 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (err >= 0)
2125 break;
2126 if (err == -EAGAIN) {
2127 if (file->f_flags & O_NONBLOCK) {
2128 err = -EBUSY;
2129 break;
2130 }
2131 } else
2132 break;
2133 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002134 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002136 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (signal_pending(current)) {
2138 err = -ERESTARTSYS;
2139 break;
2140 }
2141 }
2142 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002143 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (err < 0)
2145 goto __error;
2146 return err;
2147
2148 __error:
2149 module_put(pcm->card->module);
2150 __error2:
2151 snd_card_file_remove(pcm->card, file);
2152 __error1:
2153 return err;
2154}
2155
2156static int snd_pcm_release(struct inode *inode, struct file *file)
2157{
Takashi Iwai877211f2005-11-17 13:59:38 +01002158 struct snd_pcm *pcm;
2159 struct snd_pcm_substream *substream;
2160 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 pcm_file = file->private_data;
2163 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002164 if (snd_BUG_ON(!substream))
2165 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 pcm = substream->pcm;
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002167 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002168 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002169 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002170 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 wake_up(&pcm->open_wait);
2172 module_put(pcm->card->module);
2173 snd_card_file_remove(pcm->card, file);
2174 return 0;
2175}
2176
Takashi Iwai877211f2005-11-17 13:59:38 +01002177static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2178 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Takashi Iwai877211f2005-11-17 13:59:38 +01002180 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 snd_pcm_sframes_t appl_ptr;
2182 snd_pcm_sframes_t ret;
2183 snd_pcm_sframes_t hw_avail;
2184
2185 if (frames == 0)
2186 return 0;
2187
2188 snd_pcm_stream_lock_irq(substream);
2189 switch (runtime->status->state) {
2190 case SNDRV_PCM_STATE_PREPARED:
2191 break;
2192 case SNDRV_PCM_STATE_DRAINING:
2193 case SNDRV_PCM_STATE_RUNNING:
2194 if (snd_pcm_update_hw_ptr(substream) >= 0)
2195 break;
2196 /* Fall through */
2197 case SNDRV_PCM_STATE_XRUN:
2198 ret = -EPIPE;
2199 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002200 case SNDRV_PCM_STATE_SUSPENDED:
2201 ret = -ESTRPIPE;
2202 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 default:
2204 ret = -EBADFD;
2205 goto __end;
2206 }
2207
2208 hw_avail = snd_pcm_playback_hw_avail(runtime);
2209 if (hw_avail <= 0) {
2210 ret = 0;
2211 goto __end;
2212 }
2213 if (frames > (snd_pcm_uframes_t)hw_avail)
2214 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 appl_ptr = runtime->control->appl_ptr - frames;
2216 if (appl_ptr < 0)
2217 appl_ptr += runtime->boundary;
2218 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 ret = frames;
2220 __end:
2221 snd_pcm_stream_unlock_irq(substream);
2222 return ret;
2223}
2224
Takashi Iwai877211f2005-11-17 13:59:38 +01002225static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2226 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227{
Takashi Iwai877211f2005-11-17 13:59:38 +01002228 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 snd_pcm_sframes_t appl_ptr;
2230 snd_pcm_sframes_t ret;
2231 snd_pcm_sframes_t hw_avail;
2232
2233 if (frames == 0)
2234 return 0;
2235
2236 snd_pcm_stream_lock_irq(substream);
2237 switch (runtime->status->state) {
2238 case SNDRV_PCM_STATE_PREPARED:
2239 case SNDRV_PCM_STATE_DRAINING:
2240 break;
2241 case SNDRV_PCM_STATE_RUNNING:
2242 if (snd_pcm_update_hw_ptr(substream) >= 0)
2243 break;
2244 /* Fall through */
2245 case SNDRV_PCM_STATE_XRUN:
2246 ret = -EPIPE;
2247 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002248 case SNDRV_PCM_STATE_SUSPENDED:
2249 ret = -ESTRPIPE;
2250 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 default:
2252 ret = -EBADFD;
2253 goto __end;
2254 }
2255
2256 hw_avail = snd_pcm_capture_hw_avail(runtime);
2257 if (hw_avail <= 0) {
2258 ret = 0;
2259 goto __end;
2260 }
2261 if (frames > (snd_pcm_uframes_t)hw_avail)
2262 frames = hw_avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 appl_ptr = runtime->control->appl_ptr - frames;
2264 if (appl_ptr < 0)
2265 appl_ptr += runtime->boundary;
2266 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 ret = frames;
2268 __end:
2269 snd_pcm_stream_unlock_irq(substream);
2270 return ret;
2271}
2272
Takashi Iwai877211f2005-11-17 13:59:38 +01002273static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2274 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Takashi Iwai877211f2005-11-17 13:59:38 +01002276 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 snd_pcm_sframes_t appl_ptr;
2278 snd_pcm_sframes_t ret;
2279 snd_pcm_sframes_t avail;
2280
2281 if (frames == 0)
2282 return 0;
2283
2284 snd_pcm_stream_lock_irq(substream);
2285 switch (runtime->status->state) {
2286 case SNDRV_PCM_STATE_PREPARED:
2287 case SNDRV_PCM_STATE_PAUSED:
2288 break;
2289 case SNDRV_PCM_STATE_DRAINING:
2290 case SNDRV_PCM_STATE_RUNNING:
2291 if (snd_pcm_update_hw_ptr(substream) >= 0)
2292 break;
2293 /* Fall through */
2294 case SNDRV_PCM_STATE_XRUN:
2295 ret = -EPIPE;
2296 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002297 case SNDRV_PCM_STATE_SUSPENDED:
2298 ret = -ESTRPIPE;
2299 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 default:
2301 ret = -EBADFD;
2302 goto __end;
2303 }
2304
2305 avail = snd_pcm_playback_avail(runtime);
2306 if (avail <= 0) {
2307 ret = 0;
2308 goto __end;
2309 }
2310 if (frames > (snd_pcm_uframes_t)avail)
2311 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 appl_ptr = runtime->control->appl_ptr + frames;
2313 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2314 appl_ptr -= runtime->boundary;
2315 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ret = frames;
2317 __end:
2318 snd_pcm_stream_unlock_irq(substream);
2319 return ret;
2320}
2321
Takashi Iwai877211f2005-11-17 13:59:38 +01002322static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2323 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Takashi Iwai877211f2005-11-17 13:59:38 +01002325 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 snd_pcm_sframes_t appl_ptr;
2327 snd_pcm_sframes_t ret;
2328 snd_pcm_sframes_t avail;
2329
2330 if (frames == 0)
2331 return 0;
2332
2333 snd_pcm_stream_lock_irq(substream);
2334 switch (runtime->status->state) {
2335 case SNDRV_PCM_STATE_PREPARED:
2336 case SNDRV_PCM_STATE_DRAINING:
2337 case SNDRV_PCM_STATE_PAUSED:
2338 break;
2339 case SNDRV_PCM_STATE_RUNNING:
2340 if (snd_pcm_update_hw_ptr(substream) >= 0)
2341 break;
2342 /* Fall through */
2343 case SNDRV_PCM_STATE_XRUN:
2344 ret = -EPIPE;
2345 goto __end;
Lubomir Rintel51840402009-08-02 18:14:44 +02002346 case SNDRV_PCM_STATE_SUSPENDED:
2347 ret = -ESTRPIPE;
2348 goto __end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 default:
2350 ret = -EBADFD;
2351 goto __end;
2352 }
2353
2354 avail = snd_pcm_capture_avail(runtime);
2355 if (avail <= 0) {
2356 ret = 0;
2357 goto __end;
2358 }
2359 if (frames > (snd_pcm_uframes_t)avail)
2360 frames = avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 appl_ptr = runtime->control->appl_ptr + frames;
2362 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2363 appl_ptr -= runtime->boundary;
2364 runtime->control->appl_ptr = appl_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ret = frames;
2366 __end:
2367 snd_pcm_stream_unlock_irq(substream);
2368 return ret;
2369}
2370
Takashi Iwai877211f2005-11-17 13:59:38 +01002371static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
Takashi Iwai877211f2005-11-17 13:59:38 +01002373 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 int err;
2375
2376 snd_pcm_stream_lock_irq(substream);
2377 switch (runtime->status->state) {
2378 case SNDRV_PCM_STATE_DRAINING:
2379 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2380 goto __badfd;
2381 case SNDRV_PCM_STATE_RUNNING:
2382 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2383 break;
2384 /* Fall through */
2385 case SNDRV_PCM_STATE_PREPARED:
2386 case SNDRV_PCM_STATE_SUSPENDED:
2387 err = 0;
2388 break;
2389 case SNDRV_PCM_STATE_XRUN:
2390 err = -EPIPE;
2391 break;
2392 default:
2393 __badfd:
2394 err = -EBADFD;
2395 break;
2396 }
2397 snd_pcm_stream_unlock_irq(substream);
2398 return err;
2399}
2400
Takashi Iwai877211f2005-11-17 13:59:38 +01002401static int snd_pcm_delay(struct snd_pcm_substream *substream,
2402 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403{
Takashi Iwai877211f2005-11-17 13:59:38 +01002404 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 int err;
2406 snd_pcm_sframes_t n = 0;
2407
2408 snd_pcm_stream_lock_irq(substream);
2409 switch (runtime->status->state) {
2410 case SNDRV_PCM_STATE_DRAINING:
2411 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2412 goto __badfd;
2413 case SNDRV_PCM_STATE_RUNNING:
2414 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2415 break;
2416 /* Fall through */
2417 case SNDRV_PCM_STATE_PREPARED:
2418 case SNDRV_PCM_STATE_SUSPENDED:
2419 err = 0;
2420 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2421 n = snd_pcm_playback_hw_avail(runtime);
2422 else
2423 n = snd_pcm_capture_avail(runtime);
Takashi Iwai4bbe1dd2008-10-13 03:07:14 +02002424 n += runtime->delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 break;
2426 case SNDRV_PCM_STATE_XRUN:
2427 err = -EPIPE;
2428 break;
2429 default:
2430 __badfd:
2431 err = -EBADFD;
2432 break;
2433 }
2434 snd_pcm_stream_unlock_irq(substream);
2435 if (!err)
2436 if (put_user(n, res))
2437 err = -EFAULT;
2438 return err;
2439}
2440
Takashi Iwai877211f2005-11-17 13:59:38 +01002441static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2442 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Takashi Iwai877211f2005-11-17 13:59:38 +01002444 struct snd_pcm_runtime *runtime = substream->runtime;
2445 struct snd_pcm_sync_ptr sync_ptr;
2446 volatile struct snd_pcm_mmap_status *status;
2447 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 int err;
2449
2450 memset(&sync_ptr, 0, sizeof(sync_ptr));
2451 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2452 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002453 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 -07002454 return -EFAULT;
2455 status = runtime->status;
2456 control = runtime->control;
2457 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2458 err = snd_pcm_hwsync(substream);
2459 if (err < 0)
2460 return err;
2461 }
2462 snd_pcm_stream_lock_irq(substream);
2463 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2464 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2465 else
2466 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2467 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2468 control->avail_min = sync_ptr.c.control.avail_min;
2469 else
2470 sync_ptr.c.control.avail_min = control->avail_min;
2471 sync_ptr.s.status.state = status->state;
2472 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2473 sync_ptr.s.status.tstamp = status->tstamp;
2474 sync_ptr.s.status.suspended_state = status->suspended_state;
2475 snd_pcm_stream_unlock_irq(substream);
2476 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2477 return -EFAULT;
2478 return 0;
2479}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002480
2481static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2482{
2483 struct snd_pcm_runtime *runtime = substream->runtime;
2484 int arg;
2485
2486 if (get_user(arg, _arg))
2487 return -EFAULT;
2488 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2489 return -EINVAL;
2490 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2491 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2492 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2493 return 0;
2494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Takashi Iwai0df63e42006-04-28 15:13:41 +02002496static int snd_pcm_common_ioctl1(struct file *file,
2497 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 unsigned int cmd, void __user *arg)
2499{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 switch (cmd) {
2501 case SNDRV_PCM_IOCTL_PVERSION:
2502 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2503 case SNDRV_PCM_IOCTL_INFO:
2504 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002505 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2506 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002507 case SNDRV_PCM_IOCTL_TTSTAMP:
2508 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 case SNDRV_PCM_IOCTL_HW_REFINE:
2510 return snd_pcm_hw_refine_user(substream, arg);
2511 case SNDRV_PCM_IOCTL_HW_PARAMS:
2512 return snd_pcm_hw_params_user(substream, arg);
2513 case SNDRV_PCM_IOCTL_HW_FREE:
2514 return snd_pcm_hw_free(substream);
2515 case SNDRV_PCM_IOCTL_SW_PARAMS:
2516 return snd_pcm_sw_params_user(substream, arg);
2517 case SNDRV_PCM_IOCTL_STATUS:
2518 return snd_pcm_status_user(substream, arg);
2519 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2520 return snd_pcm_channel_info_user(substream, arg);
2521 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002522 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 case SNDRV_PCM_IOCTL_RESET:
2524 return snd_pcm_reset(substream);
2525 case SNDRV_PCM_IOCTL_START:
2526 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2527 case SNDRV_PCM_IOCTL_LINK:
2528 return snd_pcm_link(substream, (int)(unsigned long) arg);
2529 case SNDRV_PCM_IOCTL_UNLINK:
2530 return snd_pcm_unlink(substream);
2531 case SNDRV_PCM_IOCTL_RESUME:
2532 return snd_pcm_resume(substream);
2533 case SNDRV_PCM_IOCTL_XRUN:
2534 return snd_pcm_xrun(substream);
2535 case SNDRV_PCM_IOCTL_HWSYNC:
2536 return snd_pcm_hwsync(substream);
2537 case SNDRV_PCM_IOCTL_DELAY:
2538 return snd_pcm_delay(substream, arg);
2539 case SNDRV_PCM_IOCTL_SYNC_PTR:
2540 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002541#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2543 return snd_pcm_hw_refine_old_user(substream, arg);
2544 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2545 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 case SNDRV_PCM_IOCTL_DRAIN:
Takashi Iwai4cdc1152009-08-20 16:40:16 +02002548 return snd_pcm_drain(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 case SNDRV_PCM_IOCTL_DROP:
2550 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002551 case SNDRV_PCM_IOCTL_PAUSE:
2552 {
2553 int res;
2554 snd_pcm_stream_lock_irq(substream);
2555 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2556 snd_pcm_stream_unlock_irq(substream);
2557 return res;
2558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 }
2560 snd_printd("unknown ioctl = 0x%x\n", cmd);
2561 return -ENOTTY;
2562}
2563
Takashi Iwai0df63e42006-04-28 15:13:41 +02002564static int snd_pcm_playback_ioctl1(struct file *file,
2565 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 unsigned int cmd, void __user *arg)
2567{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002568 if (snd_BUG_ON(!substream))
2569 return -ENXIO;
2570 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
2571 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 switch (cmd) {
2573 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2574 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002575 struct snd_xferi xferi;
2576 struct snd_xferi __user *_xferi = arg;
2577 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 snd_pcm_sframes_t result;
2579 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2580 return -EBADFD;
2581 if (put_user(0, &_xferi->result))
2582 return -EFAULT;
2583 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2584 return -EFAULT;
2585 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2586 __put_user(result, &_xferi->result);
2587 return result < 0 ? result : 0;
2588 }
2589 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2590 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002591 struct snd_xfern xfern;
2592 struct snd_xfern __user *_xfern = arg;
2593 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 void __user **bufs;
2595 snd_pcm_sframes_t result;
2596 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2597 return -EBADFD;
2598 if (runtime->channels > 128)
2599 return -EINVAL;
2600 if (put_user(0, &_xfern->result))
2601 return -EFAULT;
2602 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2603 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002604
2605 bufs = memdup_user(xfern.bufs,
2606 sizeof(void *) * runtime->channels);
2607 if (IS_ERR(bufs))
2608 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2610 kfree(bufs);
2611 __put_user(result, &_xfern->result);
2612 return result < 0 ? result : 0;
2613 }
2614 case SNDRV_PCM_IOCTL_REWIND:
2615 {
2616 snd_pcm_uframes_t frames;
2617 snd_pcm_uframes_t __user *_frames = arg;
2618 snd_pcm_sframes_t result;
2619 if (get_user(frames, _frames))
2620 return -EFAULT;
2621 if (put_user(0, _frames))
2622 return -EFAULT;
2623 result = snd_pcm_playback_rewind(substream, frames);
2624 __put_user(result, _frames);
2625 return result < 0 ? result : 0;
2626 }
2627 case SNDRV_PCM_IOCTL_FORWARD:
2628 {
2629 snd_pcm_uframes_t frames;
2630 snd_pcm_uframes_t __user *_frames = arg;
2631 snd_pcm_sframes_t result;
2632 if (get_user(frames, _frames))
2633 return -EFAULT;
2634 if (put_user(0, _frames))
2635 return -EFAULT;
2636 result = snd_pcm_playback_forward(substream, frames);
2637 __put_user(result, _frames);
2638 return result < 0 ? result : 0;
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002641 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
Takashi Iwai0df63e42006-04-28 15:13:41 +02002644static int snd_pcm_capture_ioctl1(struct file *file,
2645 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 unsigned int cmd, void __user *arg)
2647{
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002648 if (snd_BUG_ON(!substream))
2649 return -ENXIO;
2650 if (snd_BUG_ON(substream->stream != SNDRV_PCM_STREAM_CAPTURE))
2651 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 switch (cmd) {
2653 case SNDRV_PCM_IOCTL_READI_FRAMES:
2654 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002655 struct snd_xferi xferi;
2656 struct snd_xferi __user *_xferi = arg;
2657 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 snd_pcm_sframes_t result;
2659 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2660 return -EBADFD;
2661 if (put_user(0, &_xferi->result))
2662 return -EFAULT;
2663 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2664 return -EFAULT;
2665 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2666 __put_user(result, &_xferi->result);
2667 return result < 0 ? result : 0;
2668 }
2669 case SNDRV_PCM_IOCTL_READN_FRAMES:
2670 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002671 struct snd_xfern xfern;
2672 struct snd_xfern __user *_xfern = arg;
2673 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 void *bufs;
2675 snd_pcm_sframes_t result;
2676 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2677 return -EBADFD;
2678 if (runtime->channels > 128)
2679 return -EINVAL;
2680 if (put_user(0, &_xfern->result))
2681 return -EFAULT;
2682 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2683 return -EFAULT;
Li Zefanef44a1e2009-04-10 09:43:08 +08002684
2685 bufs = memdup_user(xfern.bufs,
2686 sizeof(void *) * runtime->channels);
2687 if (IS_ERR(bufs))
2688 return PTR_ERR(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2690 kfree(bufs);
2691 __put_user(result, &_xfern->result);
2692 return result < 0 ? result : 0;
2693 }
2694 case SNDRV_PCM_IOCTL_REWIND:
2695 {
2696 snd_pcm_uframes_t frames;
2697 snd_pcm_uframes_t __user *_frames = arg;
2698 snd_pcm_sframes_t result;
2699 if (get_user(frames, _frames))
2700 return -EFAULT;
2701 if (put_user(0, _frames))
2702 return -EFAULT;
2703 result = snd_pcm_capture_rewind(substream, frames);
2704 __put_user(result, _frames);
2705 return result < 0 ? result : 0;
2706 }
2707 case SNDRV_PCM_IOCTL_FORWARD:
2708 {
2709 snd_pcm_uframes_t frames;
2710 snd_pcm_uframes_t __user *_frames = arg;
2711 snd_pcm_sframes_t result;
2712 if (get_user(frames, _frames))
2713 return -EFAULT;
2714 if (put_user(0, _frames))
2715 return -EFAULT;
2716 result = snd_pcm_capture_forward(substream, frames);
2717 __put_user(result, _frames);
2718 return result < 0 ? result : 0;
2719 }
2720 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002721 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Takashi Iwai877211f2005-11-17 13:59:38 +01002724static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2725 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Takashi Iwai877211f2005-11-17 13:59:38 +01002727 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 pcm_file = file->private_data;
2730
2731 if (((cmd >> 8) & 0xff) != 'A')
2732 return -ENOTTY;
2733
Takashi Iwai0df63e42006-04-28 15:13:41 +02002734 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2735 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Takashi Iwai877211f2005-11-17 13:59:38 +01002738static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2739 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Takashi Iwai877211f2005-11-17 13:59:38 +01002741 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743 pcm_file = file->private_data;
2744
2745 if (((cmd >> 8) & 0xff) != 'A')
2746 return -ENOTTY;
2747
Takashi Iwai0df63e42006-04-28 15:13:41 +02002748 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2749 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Takashi Iwai877211f2005-11-17 13:59:38 +01002752int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 unsigned int cmd, void *arg)
2754{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002755 mm_segment_t fs;
2756 int result;
2757
2758 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 switch (substream->stream) {
2760 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002761 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2762 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002765 result = snd_pcm_capture_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 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002769 result = -EINVAL;
2770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002772 snd_leave_user(fs);
2773 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002776EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2777
Takashi Iwai877211f2005-11-17 13:59:38 +01002778static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2779 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
Takashi Iwai877211f2005-11-17 13:59:38 +01002781 struct snd_pcm_file *pcm_file;
2782 struct snd_pcm_substream *substream;
2783 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 snd_pcm_sframes_t result;
2785
2786 pcm_file = file->private_data;
2787 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002788 if (PCM_RUNTIME_CHECK(substream))
2789 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 runtime = substream->runtime;
2791 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2792 return -EBADFD;
2793 if (!frame_aligned(runtime, count))
2794 return -EINVAL;
2795 count = bytes_to_frames(runtime, count);
2796 result = snd_pcm_lib_read(substream, buf, count);
2797 if (result > 0)
2798 result = frames_to_bytes(runtime, result);
2799 return result;
2800}
2801
Takashi Iwai877211f2005-11-17 13:59:38 +01002802static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2803 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
Takashi Iwai877211f2005-11-17 13:59:38 +01002805 struct snd_pcm_file *pcm_file;
2806 struct snd_pcm_substream *substream;
2807 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 snd_pcm_sframes_t result;
2809
2810 pcm_file = file->private_data;
2811 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002812 if (PCM_RUNTIME_CHECK(substream))
2813 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002815 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2816 return -EBADFD;
2817 if (!frame_aligned(runtime, count))
2818 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 count = bytes_to_frames(runtime, count);
2820 result = snd_pcm_lib_write(substream, buf, count);
2821 if (result > 0)
2822 result = frames_to_bytes(runtime, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return result;
2824}
2825
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002826static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2827 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829{
Takashi Iwai877211f2005-11-17 13:59:38 +01002830 struct snd_pcm_file *pcm_file;
2831 struct snd_pcm_substream *substream;
2832 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 snd_pcm_sframes_t result;
2834 unsigned long i;
2835 void __user **bufs;
2836 snd_pcm_uframes_t frames;
2837
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002838 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002840 if (PCM_RUNTIME_CHECK(substream))
2841 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 runtime = substream->runtime;
2843 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2844 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002845 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002847 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002849 frames = bytes_to_samples(runtime, iov->iov_len);
2850 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (bufs == NULL)
2852 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002853 for (i = 0; i < nr_segs; ++i)
2854 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 result = snd_pcm_lib_readv(substream, bufs, frames);
2856 if (result > 0)
2857 result = frames_to_bytes(runtime, result);
2858 kfree(bufs);
2859 return result;
2860}
2861
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002862static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2863 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Takashi Iwai877211f2005-11-17 13:59:38 +01002865 struct snd_pcm_file *pcm_file;
2866 struct snd_pcm_substream *substream;
2867 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 snd_pcm_sframes_t result;
2869 unsigned long i;
2870 void __user **bufs;
2871 snd_pcm_uframes_t frames;
2872
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002873 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002875 if (PCM_RUNTIME_CHECK(substream))
2876 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 runtime = substream->runtime;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002878 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2879 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002880 if (nr_segs > 128 || nr_segs != runtime->channels ||
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002881 !frame_aligned(runtime, iov->iov_len))
2882 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002883 frames = bytes_to_samples(runtime, iov->iov_len);
2884 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (bufs == NULL)
2886 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002887 for (i = 0; i < nr_segs; ++i)
2888 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 result = snd_pcm_lib_writev(substream, bufs, frames);
2890 if (result > 0)
2891 result = frames_to_bytes(runtime, result);
2892 kfree(bufs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return result;
2894}
2895
2896static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2897{
Takashi Iwai877211f2005-11-17 13:59:38 +01002898 struct snd_pcm_file *pcm_file;
2899 struct snd_pcm_substream *substream;
2900 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 unsigned int mask;
2902 snd_pcm_uframes_t avail;
2903
2904 pcm_file = file->private_data;
2905
2906 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002907 if (PCM_RUNTIME_CHECK(substream))
2908 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 runtime = substream->runtime;
2910
2911 poll_wait(file, &runtime->sleep, wait);
2912
2913 snd_pcm_stream_lock_irq(substream);
2914 avail = snd_pcm_playback_avail(runtime);
2915 switch (runtime->status->state) {
2916 case SNDRV_PCM_STATE_RUNNING:
2917 case SNDRV_PCM_STATE_PREPARED:
2918 case SNDRV_PCM_STATE_PAUSED:
2919 if (avail >= runtime->control->avail_min) {
2920 mask = POLLOUT | POLLWRNORM;
2921 break;
2922 }
2923 /* Fall through */
2924 case SNDRV_PCM_STATE_DRAINING:
2925 mask = 0;
2926 break;
2927 default:
2928 mask = POLLOUT | POLLWRNORM | POLLERR;
2929 break;
2930 }
2931 snd_pcm_stream_unlock_irq(substream);
2932 return mask;
2933}
2934
2935static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2936{
Takashi Iwai877211f2005-11-17 13:59:38 +01002937 struct snd_pcm_file *pcm_file;
2938 struct snd_pcm_substream *substream;
2939 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 unsigned int mask;
2941 snd_pcm_uframes_t avail;
2942
2943 pcm_file = file->private_data;
2944
2945 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02002946 if (PCM_RUNTIME_CHECK(substream))
2947 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 runtime = substream->runtime;
2949
2950 poll_wait(file, &runtime->sleep, wait);
2951
2952 snd_pcm_stream_lock_irq(substream);
2953 avail = snd_pcm_capture_avail(runtime);
2954 switch (runtime->status->state) {
2955 case SNDRV_PCM_STATE_RUNNING:
2956 case SNDRV_PCM_STATE_PREPARED:
2957 case SNDRV_PCM_STATE_PAUSED:
2958 if (avail >= runtime->control->avail_min) {
2959 mask = POLLIN | POLLRDNORM;
2960 break;
2961 }
2962 mask = 0;
2963 break;
2964 case SNDRV_PCM_STATE_DRAINING:
2965 if (avail > 0) {
2966 mask = POLLIN | POLLRDNORM;
2967 break;
2968 }
2969 /* Fall through */
2970 default:
2971 mask = POLLIN | POLLRDNORM | POLLERR;
2972 break;
2973 }
2974 snd_pcm_stream_unlock_irq(substream);
2975 return mask;
2976}
2977
2978/*
2979 * mmap support
2980 */
2981
2982/*
2983 * Only on coherent architectures, we can mmap the status and the control records
2984 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2985 */
2986#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2987/*
2988 * mmap status record
2989 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002990static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
2991 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
Takashi Iwai877211f2005-11-17 13:59:38 +01002993 struct snd_pcm_substream *substream = area->vm_private_data;
2994 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002997 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01002999 vmf->page = virt_to_page(runtime->status);
3000 get_page(vmf->page);
3001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003004static const struct vm_operations_struct snd_pcm_vm_ops_status =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003006 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007};
3008
Takashi Iwai877211f2005-11-17 13:59:38 +01003009static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 struct vm_area_struct *area)
3011{
Takashi Iwai877211f2005-11-17 13:59:38 +01003012 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 long size;
3014 if (!(area->vm_flags & VM_READ))
3015 return -EINVAL;
3016 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003018 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 return -EINVAL;
3020 area->vm_ops = &snd_pcm_vm_ops_status;
3021 area->vm_private_data = substream;
3022 area->vm_flags |= VM_RESERVED;
3023 return 0;
3024}
3025
3026/*
3027 * mmap control record
3028 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003029static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3030 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Takashi Iwai877211f2005-11-17 13:59:38 +01003032 struct snd_pcm_substream *substream = area->vm_private_data;
3033 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003036 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003038 vmf->page = virt_to_page(runtime->control);
3039 get_page(vmf->page);
3040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003043static const struct vm_operations_struct snd_pcm_vm_ops_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003045 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046};
3047
Takashi Iwai877211f2005-11-17 13:59:38 +01003048static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct vm_area_struct *area)
3050{
Takashi Iwai877211f2005-11-17 13:59:38 +01003051 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 long size;
3053 if (!(area->vm_flags & VM_READ))
3054 return -EINVAL;
3055 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003057 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return -EINVAL;
3059 area->vm_ops = &snd_pcm_vm_ops_control;
3060 area->vm_private_data = substream;
3061 area->vm_flags |= VM_RESERVED;
3062 return 0;
3063}
3064#else /* ! coherent mmap */
3065/*
3066 * don't support mmap for status and control records.
3067 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003068static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 struct vm_area_struct *area)
3070{
3071 return -ENXIO;
3072}
Takashi Iwai877211f2005-11-17 13:59:38 +01003073static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct vm_area_struct *area)
3075{
3076 return -ENXIO;
3077}
3078#endif /* coherent mmap */
3079
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003080static inline struct page *
3081snd_pcm_default_page_ops(struct snd_pcm_substream *substream, unsigned long ofs)
3082{
3083 void *vaddr = substream->runtime->dma_area + ofs;
Takashi Iwai66b6cfa2009-11-26 12:50:01 +01003084#if defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3085 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3086 return virt_to_page(CAC_ADDR(vaddr));
3087#endif
Takashi Iwai6985c882009-11-26 15:04:24 +01003088#if defined(CONFIG_PPC32) && defined(CONFIG_NOT_COHERENT_CACHE)
3089 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV) {
3090 dma_addr_t addr = substream->runtime->dma_addr + ofs;
3091 addr -= get_dma_offset(substream->dma_buffer.dev.dev);
3092 /* assume dma_handle set via pfn_to_phys() in
3093 * mm/dma-noncoherent.c
3094 */
3095 return pfn_to_page(addr >> PAGE_SHIFT);
3096 }
3097#endif
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003098 return virt_to_page(vaddr);
3099}
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003102 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003104static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3105 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Takashi Iwai877211f2005-11-17 13:59:38 +01003107 struct snd_pcm_substream *substream = area->vm_private_data;
3108 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 unsigned long offset;
3110 struct page * page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 size_t dma_bytes;
3112
3113 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003114 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003116 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3118 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003119 return VM_FAULT_SIGBUS;
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003120 if (substream->ops->page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 page = substream->ops->page(substream, offset);
Takashi Iwai9eb4a062009-11-26 12:43:39 +01003122 else
3123 page = snd_pcm_default_page_ops(substream, offset);
3124 if (!page)
3125 return VM_FAULT_SIGBUS;
Nick Pigginb5810032005-10-29 18:16:12 -07003126 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003127 vmf->page = page;
3128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Takashi Iwai657b1982009-11-26 12:40:21 +01003131static const struct vm_operations_struct snd_pcm_vm_ops_data = {
3132 .open = snd_pcm_mmap_data_open,
3133 .close = snd_pcm_mmap_data_close,
3134};
3135
3136static const struct vm_operations_struct snd_pcm_vm_ops_data_fault = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 .open = snd_pcm_mmap_data_open,
3138 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003139 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140};
3141
Takashi Iwai657b1982009-11-26 12:40:21 +01003142#ifndef ARCH_HAS_DMA_MMAP_COHERENT
3143/* This should be defined / handled globally! */
3144#ifdef CONFIG_ARM
3145#define ARCH_HAS_DMA_MMAP_COHERENT
3146#endif
3147#endif
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149/*
3150 * mmap the DMA buffer on RAM
3151 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003152static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3153 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 area->vm_flags |= VM_RESERVED;
Takashi Iwai657b1982009-11-26 12:40:21 +01003156#ifdef ARCH_HAS_DMA_MMAP_COHERENT
3157 if (!substream->ops->page &&
3158 substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV)
3159 return dma_mmap_coherent(substream->dma_buffer.dev.dev,
3160 area,
3161 substream->runtime->dma_area,
3162 substream->runtime->dma_addr,
3163 area->vm_end - area->vm_start);
Takashi Iwai9fe17b52010-05-12 10:32:42 +02003164#elif defined(CONFIG_MIPS) && defined(CONFIG_DMA_NONCOHERENT)
3165 if (substream->dma_buffer.dev.type == SNDRV_DMA_TYPE_DEV &&
3166 !plat_device_is_coherent(substream->dma_buffer.dev.dev))
3167 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Takashi Iwai657b1982009-11-26 12:40:21 +01003168#endif /* ARCH_HAS_DMA_MMAP_COHERENT */
3169 /* mmap with fault handler */
3170 area->vm_ops = &snd_pcm_vm_ops_data_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return 0;
3172}
3173
3174/*
3175 * mmap the DMA buffer on I/O memory area
3176 */
3177#if SNDRV_PCM_INFO_MMAP_IOMEM
Takashi Iwai877211f2005-11-17 13:59:38 +01003178int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3179 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 long size;
3182 unsigned long offset;
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 area->vm_flags |= VM_IO;
3186 size = area->vm_end - area->vm_start;
3187 offset = area->vm_pgoff << PAGE_SHIFT;
3188 if (io_remap_pfn_range(area, area->vm_start,
3189 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3190 size, area->vm_page_prot))
3191 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 return 0;
3193}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003194
3195EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196#endif /* SNDRV_PCM_INFO_MMAP */
3197
Takashi Iwaic32d9772010-01-18 14:58:57 +01003198/* mmap callback with pgprot_noncached */
3199int snd_pcm_lib_mmap_noncached(struct snd_pcm_substream *substream,
3200 struct vm_area_struct *area)
3201{
3202 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3203 return snd_pcm_default_mmap(substream, area);
3204}
3205EXPORT_SYMBOL(snd_pcm_lib_mmap_noncached);
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207/*
3208 * mmap DMA buffer
3209 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003210int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct vm_area_struct *area)
3212{
Takashi Iwai877211f2005-11-17 13:59:38 +01003213 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 long size;
3215 unsigned long offset;
3216 size_t dma_bytes;
Takashi Iwai657b1982009-11-26 12:40:21 +01003217 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3220 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3221 return -EINVAL;
3222 } else {
3223 if (!(area->vm_flags & VM_READ))
3224 return -EINVAL;
3225 }
3226 runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3228 return -EBADFD;
3229 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3230 return -ENXIO;
3231 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3232 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3233 return -EINVAL;
3234 size = area->vm_end - area->vm_start;
3235 offset = area->vm_pgoff << PAGE_SHIFT;
3236 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3237 if ((size_t)size > dma_bytes)
3238 return -EINVAL;
3239 if (offset > dma_bytes - size)
3240 return -EINVAL;
3241
Takashi Iwai657b1982009-11-26 12:40:21 +01003242 area->vm_ops = &snd_pcm_vm_ops_data;
3243 area->vm_private_data = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (substream->ops->mmap)
Takashi Iwai657b1982009-11-26 12:40:21 +01003245 err = substream->ops->mmap(substream, area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 else
Takashi Iwai657b1982009-11-26 12:40:21 +01003247 err = snd_pcm_default_mmap(substream, area);
3248 if (!err)
3249 atomic_inc(&substream->mmap_count);
3250 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003253EXPORT_SYMBOL(snd_pcm_mmap_data);
3254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3256{
Takashi Iwai877211f2005-11-17 13:59:38 +01003257 struct snd_pcm_file * pcm_file;
3258 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 unsigned long offset;
3260
3261 pcm_file = file->private_data;
3262 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003263 if (PCM_RUNTIME_CHECK(substream))
3264 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 offset = area->vm_pgoff << PAGE_SHIFT;
3267 switch (offset) {
3268 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003269 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return -ENXIO;
3271 return snd_pcm_mmap_status(substream, file, area);
3272 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
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_control(substream, file, area);
3276 default:
3277 return snd_pcm_mmap_data(substream, file, area);
3278 }
3279 return 0;
3280}
3281
3282static int snd_pcm_fasync(int fd, struct file * file, int on)
3283{
Takashi Iwai877211f2005-11-17 13:59:38 +01003284 struct snd_pcm_file * pcm_file;
3285 struct snd_pcm_substream *substream;
3286 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 pcm_file = file->private_data;
3289 substream = pcm_file->substream;
Takashi Iwai7eaa9432008-08-08 17:09:09 +02003290 if (PCM_RUNTIME_CHECK(substream))
Takashi Iwaid05468b2010-04-07 18:29:46 +02003291 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 runtime = substream->runtime;
Takashi Iwaid05468b2010-04-07 18:29:46 +02003293 return fasync_helper(fd, file, on, &runtime->fasync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
3296/*
3297 * ioctl32 compat
3298 */
3299#ifdef CONFIG_COMPAT
3300#include "pcm_compat.c"
3301#else
3302#define snd_pcm_ioctl_compat NULL
3303#endif
3304
3305/*
3306 * To be removed helpers to keep binary compatibility
3307 */
3308
Takashi Iwai59d48582005-12-01 10:51:58 +01003309#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3311#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3312
Takashi Iwai877211f2005-11-17 13:59:38 +01003313static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3314 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 unsigned int i;
3317
3318 memset(params, 0, sizeof(*params));
3319 params->flags = oparams->flags;
3320 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3321 params->masks[i].bits[0] = oparams->masks[i];
3322 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3323 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3324 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3325 params->info = oparams->info;
3326 params->msbits = oparams->msbits;
3327 params->rate_num = oparams->rate_num;
3328 params->rate_den = oparams->rate_den;
3329 params->fifo_size = oparams->fifo_size;
3330}
3331
Takashi Iwai877211f2005-11-17 13:59:38 +01003332static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3333 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
3335 unsigned int i;
3336
3337 memset(oparams, 0, sizeof(*oparams));
3338 oparams->flags = params->flags;
3339 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3340 oparams->masks[i] = params->masks[i].bits[0];
3341 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3342 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3343 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3344 oparams->info = params->info;
3345 oparams->msbits = params->msbits;
3346 oparams->rate_num = params->rate_num;
3347 oparams->rate_den = params->rate_den;
3348 oparams->fifo_size = params->fifo_size;
3349}
3350
Takashi Iwai877211f2005-11-17 13:59:38 +01003351static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3352 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
Takashi Iwai877211f2005-11-17 13:59:38 +01003354 struct snd_pcm_hw_params *params;
3355 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 int err;
3357
3358 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003359 if (!params)
3360 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
Li Zefanef44a1e2009-04-10 09:43:08 +08003362 oparams = memdup_user(_oparams, sizeof(*oparams));
3363 if (IS_ERR(oparams)) {
3364 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 goto out;
3366 }
3367 snd_pcm_hw_convert_from_old_params(params, oparams);
3368 err = snd_pcm_hw_refine(substream, params);
3369 snd_pcm_hw_convert_to_old_params(oparams, params);
3370 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3371 if (!err)
3372 err = -EFAULT;
3373 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003374
3375 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376out:
3377 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return err;
3379}
3380
Takashi Iwai877211f2005-11-17 13:59:38 +01003381static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3382 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Takashi Iwai877211f2005-11-17 13:59:38 +01003384 struct snd_pcm_hw_params *params;
3385 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 int err;
3387
3388 params = kmalloc(sizeof(*params), GFP_KERNEL);
Li Zefanef44a1e2009-04-10 09:43:08 +08003389 if (!params)
3390 return -ENOMEM;
3391
3392 oparams = memdup_user(_oparams, sizeof(*oparams));
3393 if (IS_ERR(oparams)) {
3394 err = PTR_ERR(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 goto out;
3396 }
3397 snd_pcm_hw_convert_from_old_params(params, oparams);
3398 err = snd_pcm_hw_params(substream, params);
3399 snd_pcm_hw_convert_to_old_params(oparams, params);
3400 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3401 if (!err)
3402 err = -EFAULT;
3403 }
Li Zefanef44a1e2009-04-10 09:43:08 +08003404
3405 kfree(oparams);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406out:
3407 kfree(params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return err;
3409}
Takashi Iwai59d48582005-12-01 10:51:58 +01003410#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Cliff Cai70036092008-09-03 10:54:36 +02003412#ifndef CONFIG_MMU
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003413static unsigned long snd_pcm_get_unmapped_area(struct file *file,
3414 unsigned long addr,
3415 unsigned long len,
3416 unsigned long pgoff,
3417 unsigned long flags)
Cliff Cai70036092008-09-03 10:54:36 +02003418{
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003419 struct snd_pcm_file *pcm_file = file->private_data;
3420 struct snd_pcm_substream *substream = pcm_file->substream;
3421 struct snd_pcm_runtime *runtime = substream->runtime;
3422 unsigned long offset = pgoff << PAGE_SHIFT;
3423
3424 switch (offset) {
3425 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3426 return (unsigned long)runtime->status;
3427 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3428 return (unsigned long)runtime->control;
3429 default:
3430 return (unsigned long)runtime->dma_area + offset;
3431 }
Cliff Cai70036092008-09-03 10:54:36 +02003432}
3433#else
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003434# define snd_pcm_get_unmapped_area NULL
Cliff Cai70036092008-09-03 10:54:36 +02003435#endif
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437/*
3438 * Register section
3439 */
3440
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003441const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003443 .owner = THIS_MODULE,
3444 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003445 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003446 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003447 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003448 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003449 .poll = snd_pcm_playback_poll,
3450 .unlocked_ioctl = snd_pcm_playback_ioctl,
3451 .compat_ioctl = snd_pcm_ioctl_compat,
3452 .mmap = snd_pcm_mmap,
3453 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003454 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 },
3456 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003457 .owner = THIS_MODULE,
3458 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003459 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003460 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003461 .release = snd_pcm_release,
Takashi Iwai02f48652010-04-13 11:49:04 +02003462 .llseek = no_llseek,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003463 .poll = snd_pcm_capture_poll,
3464 .unlocked_ioctl = snd_pcm_capture_ioctl,
3465 .compat_ioctl = snd_pcm_ioctl_compat,
3466 .mmap = snd_pcm_mmap,
3467 .fasync = snd_pcm_fasync,
Daniel Glöckner55c63bd2010-03-09 12:57:52 -05003468 .get_unmapped_area = snd_pcm_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
3470};