blob: 3fc33deabe356912e5b192909d0c0c3a7a46567e [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
22#include <sound/driver.h>
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/file.h>
25#include <linux/slab.h>
26#include <linux/time.h>
Takashi Iwai9442e692006-09-30 23:27:19 -070027#include <linux/latency.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/info.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/timer.h>
35#include <sound/minors.h>
36#include <asm/io.h>
37
38/*
39 * Compatibility
40 */
41
Takashi Iwai877211f2005-11-17 13:59:38 +010042struct snd_pcm_hw_params_old {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
Takashi Iwai877211f2005-11-17 13:59:38 +010046 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 SNDRV_PCM_HW_PARAM_SAMPLE_BITS + 1];
48 unsigned int rmask;
49 unsigned int cmask;
50 unsigned int info;
51 unsigned int msbits;
52 unsigned int rate_num;
53 unsigned int rate_den;
Takashi Iwai877211f2005-11-17 13:59:38 +010054 snd_pcm_uframes_t fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 unsigned char reserved[64];
56};
57
Takashi Iwai59d48582005-12-01 10:51:58 +010058#ifdef CONFIG_SND_SUPPORT_OLD_API
Takashi Iwai877211f2005-11-17 13:59:38 +010059#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
60#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Takashi Iwai877211f2005-11-17 13:59:38 +010062static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params_old __user * _oparams);
64static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
65 struct snd_pcm_hw_params_old __user * _oparams);
Takashi Iwai59d48582005-12-01 10:51:58 +010066#endif
Clemens Ladischf87135f2005-11-20 14:06:59 +010067static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 *
71 */
72
73DEFINE_RWLOCK(snd_pcm_link_rwlock);
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020074EXPORT_SYMBOL(snd_pcm_link_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwaie88e8ae62006-04-28 15:13:40 +020076static DECLARE_RWSEM(snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static inline mm_segment_t snd_enter_user(void)
79{
80 mm_segment_t fs = get_fs();
81 set_fs(get_ds());
82 return fs;
83}
84
85static inline void snd_leave_user(mm_segment_t fs)
86{
87 set_fs(fs);
88}
89
90
91
Takashi Iwai877211f2005-11-17 13:59:38 +010092int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Takashi Iwai877211f2005-11-17 13:59:38 +010094 struct snd_pcm_runtime *runtime;
95 struct snd_pcm *pcm = substream->pcm;
96 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 snd_assert(substream != NULL, return -ENXIO);
99 memset(info, 0, sizeof(*info));
100 info->card = pcm->card->number;
101 info->device = pcm->device;
102 info->stream = substream->stream;
103 info->subdevice = substream->number;
104 strlcpy(info->id, pcm->id, sizeof(info->id));
105 strlcpy(info->name, pcm->name, sizeof(info->name));
106 info->dev_class = pcm->dev_class;
107 info->dev_subclass = pcm->dev_subclass;
108 info->subdevices_count = pstr->substream_count;
109 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
110 strlcpy(info->subname, substream->name, sizeof(info->subname));
111 runtime = substream->runtime;
112 /* AB: FIXME!!! This is definitely nonsense */
113 if (runtime) {
114 info->sync = runtime->sync;
115 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
116 }
117 return 0;
118}
119
Takashi Iwai877211f2005-11-17 13:59:38 +0100120int snd_pcm_info_user(struct snd_pcm_substream *substream,
121 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Takashi Iwai877211f2005-11-17 13:59:38 +0100123 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int err;
125
126 info = kmalloc(sizeof(*info), GFP_KERNEL);
127 if (! info)
128 return -ENOMEM;
129 err = snd_pcm_info(substream, info);
130 if (err >= 0) {
131 if (copy_to_user(_info, info, sizeof(*info)))
132 err = -EFAULT;
133 }
134 kfree(info);
135 return err;
136}
137
138#undef RULES_DEBUG
139
140#ifdef RULES_DEBUG
141#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
142char *snd_pcm_hw_param_names[] = {
143 HW_PARAM(ACCESS),
144 HW_PARAM(FORMAT),
145 HW_PARAM(SUBFORMAT),
146 HW_PARAM(SAMPLE_BITS),
147 HW_PARAM(FRAME_BITS),
148 HW_PARAM(CHANNELS),
149 HW_PARAM(RATE),
150 HW_PARAM(PERIOD_TIME),
151 HW_PARAM(PERIOD_SIZE),
152 HW_PARAM(PERIOD_BYTES),
153 HW_PARAM(PERIODS),
154 HW_PARAM(BUFFER_TIME),
155 HW_PARAM(BUFFER_SIZE),
156 HW_PARAM(BUFFER_BYTES),
157 HW_PARAM(TICK_TIME),
158};
159#endif
160
Takashi Iwai877211f2005-11-17 13:59:38 +0100161int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
162 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100165 struct snd_pcm_hardware *hw;
166 struct snd_interval *i = NULL;
167 struct snd_mask *m = NULL;
168 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 unsigned int rstamps[constrs->rules_num];
170 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
171 unsigned int stamp = 2;
172 int changed, again;
173
174 params->info = 0;
175 params->fifo_size = 0;
176 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
177 params->msbits = 0;
178 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
179 params->rate_num = 0;
180 params->rate_den = 0;
181 }
182
183 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
184 m = hw_param_mask(params, k);
185 if (snd_mask_empty(m))
186 return -EINVAL;
187 if (!(params->rmask & (1 << k)))
188 continue;
189#ifdef RULES_DEBUG
190 printk("%s = ", snd_pcm_hw_param_names[k]);
191 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192#endif
193 changed = snd_mask_refine(m, constrs_mask(constrs, k));
194#ifdef RULES_DEBUG
195 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
196#endif
197 if (changed)
198 params->cmask |= 1 << k;
199 if (changed < 0)
200 return changed;
201 }
202
203 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
204 i = hw_param_interval(params, k);
205 if (snd_interval_empty(i))
206 return -EINVAL;
207 if (!(params->rmask & (1 << k)))
208 continue;
209#ifdef RULES_DEBUG
210 printk("%s = ", snd_pcm_hw_param_names[k]);
211 if (i->empty)
212 printk("empty");
213 else
214 printk("%c%u %u%c",
215 i->openmin ? '(' : '[', i->min,
216 i->max, i->openmax ? ')' : ']');
217 printk(" -> ");
218#endif
219 changed = snd_interval_refine(i, constrs_interval(constrs, k));
220#ifdef RULES_DEBUG
221 if (i->empty)
222 printk("empty\n");
223 else
224 printk("%c%u %u%c\n",
225 i->openmin ? '(' : '[', i->min,
226 i->max, i->openmax ? ')' : ']');
227#endif
228 if (changed)
229 params->cmask |= 1 << k;
230 if (changed < 0)
231 return changed;
232 }
233
234 for (k = 0; k < constrs->rules_num; k++)
235 rstamps[k] = 0;
236 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
237 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
238 do {
239 again = 0;
240 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100241 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned int d;
243 int doit = 0;
244 if (r->cond && !(r->cond & params->flags))
245 continue;
246 for (d = 0; r->deps[d] >= 0; d++) {
247 if (vstamps[r->deps[d]] > rstamps[k]) {
248 doit = 1;
249 break;
250 }
251 }
252 if (!doit)
253 continue;
254#ifdef RULES_DEBUG
255 printk("Rule %d [%p]: ", k, r->func);
256 if (r->var >= 0) {
257 printk("%s = ", snd_pcm_hw_param_names[r->var]);
258 if (hw_is_mask(r->var)) {
259 m = hw_param_mask(params, r->var);
260 printk("%x", *m->bits);
261 } else {
262 i = hw_param_interval(params, r->var);
263 if (i->empty)
264 printk("empty");
265 else
266 printk("%c%u %u%c",
267 i->openmin ? '(' : '[', i->min,
268 i->max, i->openmax ? ')' : ']');
269 }
270 }
271#endif
272 changed = r->func(params, r);
273#ifdef RULES_DEBUG
274 if (r->var >= 0) {
275 printk(" -> ");
276 if (hw_is_mask(r->var))
277 printk("%x", *m->bits);
278 else {
279 if (i->empty)
280 printk("empty");
281 else
282 printk("%c%u %u%c",
283 i->openmin ? '(' : '[', i->min,
284 i->max, i->openmax ? ')' : ']');
285 }
286 }
287 printk("\n");
288#endif
289 rstamps[k] = stamp;
290 if (changed && r->var >= 0) {
291 params->cmask |= (1 << r->var);
292 vstamps[r->var] = stamp;
293 again = 1;
294 }
295 if (changed < 0)
296 return changed;
297 stamp++;
298 }
299 } while (again);
300 if (!params->msbits) {
301 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
302 if (snd_interval_single(i))
303 params->msbits = snd_interval_value(i);
304 }
305
306 if (!params->rate_den) {
307 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
308 if (snd_interval_single(i)) {
309 params->rate_num = snd_interval_value(i);
310 params->rate_den = 1;
311 }
312 }
313
314 hw = &substream->runtime->hw;
315 if (!params->info)
316 params->info = hw->info;
317 if (!params->fifo_size)
318 params->fifo_size = hw->fifo_size;
319 params->rmask = 0;
320 return 0;
321}
322
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200323EXPORT_SYMBOL(snd_pcm_hw_refine);
324
Takashi Iwai877211f2005-11-17 13:59:38 +0100325static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
326 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Takashi Iwai877211f2005-11-17 13:59:38 +0100328 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int err;
330
331 params = kmalloc(sizeof(*params), GFP_KERNEL);
332 if (!params) {
333 err = -ENOMEM;
334 goto out;
335 }
336 if (copy_from_user(params, _params, sizeof(*params))) {
337 err = -EFAULT;
338 goto out;
339 }
340 err = snd_pcm_hw_refine(substream, params);
341 if (copy_to_user(_params, params, sizeof(*params))) {
342 if (!err)
343 err = -EFAULT;
344 }
345out:
346 kfree(params);
347 return err;
348}
349
Takashi Iwai9442e692006-09-30 23:27:19 -0700350static int period_to_usecs(struct snd_pcm_runtime *runtime)
351{
352 int usecs;
353
354 if (! runtime->rate)
355 return -1; /* invalid */
356
357 /* take 75% of period time as the deadline */
358 usecs = (750000 / runtime->rate) * runtime->period_size;
359 usecs += ((750000 % runtime->rate) * runtime->period_size) /
360 runtime->rate;
361
362 return usecs;
363}
364
Takashi Iwai877211f2005-11-17 13:59:38 +0100365static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
366 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Takashi Iwai877211f2005-11-17 13:59:38 +0100368 struct snd_pcm_runtime *runtime;
Takashi Iwai9442e692006-09-30 23:27:19 -0700369 int err, usecs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned int bits;
371 snd_pcm_uframes_t frames;
372
373 snd_assert(substream != NULL, return -ENXIO);
374 runtime = substream->runtime;
375 snd_assert(runtime != NULL, return -ENXIO);
376 snd_pcm_stream_lock_irq(substream);
377 switch (runtime->status->state) {
378 case SNDRV_PCM_STATE_OPEN:
379 case SNDRV_PCM_STATE_SETUP:
380 case SNDRV_PCM_STATE_PREPARED:
381 break;
382 default:
383 snd_pcm_stream_unlock_irq(substream);
384 return -EBADFD;
385 }
386 snd_pcm_stream_unlock_irq(substream);
387#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
388 if (!substream->oss.oss)
389#endif
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200390 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EBADFD;
392
393 params->rmask = ~0U;
394 err = snd_pcm_hw_refine(substream, params);
395 if (err < 0)
396 goto _error;
397
398 err = snd_pcm_hw_params_choose(substream, params);
399 if (err < 0)
400 goto _error;
401
402 if (substream->ops->hw_params != NULL) {
403 err = substream->ops->hw_params(substream, params);
404 if (err < 0)
405 goto _error;
406 }
407
408 runtime->access = params_access(params);
409 runtime->format = params_format(params);
410 runtime->subformat = params_subformat(params);
411 runtime->channels = params_channels(params);
412 runtime->rate = params_rate(params);
413 runtime->period_size = params_period_size(params);
414 runtime->periods = params_periods(params);
415 runtime->buffer_size = params_buffer_size(params);
416 runtime->tick_time = params_tick_time(params);
417 runtime->info = params->info;
418 runtime->rate_num = params->rate_num;
419 runtime->rate_den = params->rate_den;
420
421 bits = snd_pcm_format_physical_width(runtime->format);
422 runtime->sample_bits = bits;
423 bits *= runtime->channels;
424 runtime->frame_bits = bits;
425 frames = 1;
426 while (bits % 8 != 0) {
427 bits *= 2;
428 frames *= 2;
429 }
430 runtime->byte_align = bits / 8;
431 runtime->min_align = frames;
432
433 /* Default sw params */
434 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
435 runtime->period_step = 1;
436 runtime->sleep_min = 0;
437 runtime->control->avail_min = runtime->period_size;
438 runtime->xfer_align = runtime->period_size;
439 runtime->start_threshold = 1;
440 runtime->stop_threshold = runtime->buffer_size;
441 runtime->silence_threshold = 0;
442 runtime->silence_size = 0;
443 runtime->boundary = runtime->buffer_size;
444 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
445 runtime->boundary *= 2;
446
447 snd_pcm_timer_resolution_change(substream);
448 runtime->status->state = SNDRV_PCM_STATE_SETUP;
Takashi Iwai9442e692006-09-30 23:27:19 -0700449
450 remove_acceptable_latency(substream->latency_id);
451 if ((usecs = period_to_usecs(runtime)) >= 0)
452 set_acceptable_latency(substream->latency_id, usecs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454 _error:
455 /* hardware might be unuseable from this time,
456 so we force application to retry to set
457 the correct hardware parameter settings */
458 runtime->status->state = SNDRV_PCM_STATE_OPEN;
459 if (substream->ops->hw_free != NULL)
460 substream->ops->hw_free(substream);
461 return err;
462}
463
Takashi Iwai877211f2005-11-17 13:59:38 +0100464static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
465 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Takashi Iwai877211f2005-11-17 13:59:38 +0100467 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int err;
469
470 params = kmalloc(sizeof(*params), GFP_KERNEL);
471 if (!params) {
472 err = -ENOMEM;
473 goto out;
474 }
475 if (copy_from_user(params, _params, sizeof(*params))) {
476 err = -EFAULT;
477 goto out;
478 }
479 err = snd_pcm_hw_params(substream, params);
480 if (copy_to_user(_params, params, sizeof(*params))) {
481 if (!err)
482 err = -EFAULT;
483 }
484out:
485 kfree(params);
486 return err;
487}
488
Takashi Iwai877211f2005-11-17 13:59:38 +0100489static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwai877211f2005-11-17 13:59:38 +0100491 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int result = 0;
493
494 snd_assert(substream != NULL, return -ENXIO);
495 runtime = substream->runtime;
496 snd_assert(runtime != NULL, return -ENXIO);
497 snd_pcm_stream_lock_irq(substream);
498 switch (runtime->status->state) {
499 case SNDRV_PCM_STATE_SETUP:
500 case SNDRV_PCM_STATE_PREPARED:
501 break;
502 default:
503 snd_pcm_stream_unlock_irq(substream);
504 return -EBADFD;
505 }
506 snd_pcm_stream_unlock_irq(substream);
Takashi Iwai9c323fc2006-04-28 15:13:41 +0200507 if (atomic_read(&substream->mmap_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EBADFD;
509 if (substream->ops->hw_free)
510 result = substream->ops->hw_free(substream);
511 runtime->status->state = SNDRV_PCM_STATE_OPEN;
Takashi Iwai9442e692006-09-30 23:27:19 -0700512 remove_acceptable_latency(substream->latency_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return result;
514}
515
Takashi Iwai877211f2005-11-17 13:59:38 +0100516static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
517 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Takashi Iwai877211f2005-11-17 13:59:38 +0100519 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 snd_assert(substream != NULL, return -ENXIO);
522 runtime = substream->runtime;
523 snd_assert(runtime != NULL, return -ENXIO);
524 snd_pcm_stream_lock_irq(substream);
525 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
526 snd_pcm_stream_unlock_irq(substream);
527 return -EBADFD;
528 }
529 snd_pcm_stream_unlock_irq(substream);
530
531 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
532 return -EINVAL;
533 if (params->avail_min == 0)
534 return -EINVAL;
535 if (params->xfer_align == 0 ||
536 params->xfer_align % runtime->min_align != 0)
537 return -EINVAL;
538 if (params->silence_size >= runtime->boundary) {
539 if (params->silence_threshold != 0)
540 return -EINVAL;
541 } else {
542 if (params->silence_size > params->silence_threshold)
543 return -EINVAL;
544 if (params->silence_threshold > runtime->buffer_size)
545 return -EINVAL;
546 }
547 snd_pcm_stream_lock_irq(substream);
548 runtime->tstamp_mode = params->tstamp_mode;
549 runtime->sleep_min = params->sleep_min;
550 runtime->period_step = params->period_step;
551 runtime->control->avail_min = params->avail_min;
552 runtime->start_threshold = params->start_threshold;
553 runtime->stop_threshold = params->stop_threshold;
554 runtime->silence_threshold = params->silence_threshold;
555 runtime->silence_size = params->silence_size;
556 runtime->xfer_align = params->xfer_align;
557 params->boundary = runtime->boundary;
558 if (snd_pcm_running(substream)) {
559 if (runtime->sleep_min)
560 snd_pcm_tick_prepare(substream);
561 else
562 snd_pcm_tick_set(substream, 0);
563 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
564 runtime->silence_size > 0)
565 snd_pcm_playback_silence(substream, ULONG_MAX);
566 wake_up(&runtime->sleep);
567 }
568 snd_pcm_stream_unlock_irq(substream);
569 return 0;
570}
571
Takashi Iwai877211f2005-11-17 13:59:38 +0100572static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
573 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Takashi Iwai877211f2005-11-17 13:59:38 +0100575 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int err;
577 if (copy_from_user(&params, _params, sizeof(params)))
578 return -EFAULT;
579 err = snd_pcm_sw_params(substream, &params);
580 if (copy_to_user(_params, &params, sizeof(params)))
581 return -EFAULT;
582 return err;
583}
584
Takashi Iwai877211f2005-11-17 13:59:38 +0100585int snd_pcm_status(struct snd_pcm_substream *substream,
586 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Takashi Iwai877211f2005-11-17 13:59:38 +0100588 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 snd_pcm_stream_lock_irq(substream);
591 status->state = runtime->status->state;
592 status->suspended_state = runtime->status->suspended_state;
593 if (status->state == SNDRV_PCM_STATE_OPEN)
594 goto _end;
595 status->trigger_tstamp = runtime->trigger_tstamp;
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100596 if (snd_pcm_running(substream))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 snd_pcm_update_hw_ptr(substream);
Jaroslav Kyselaa713b582008-01-08 12:24:01 +0100598 snd_pcm_gettime(runtime, &status->tstamp);
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 ||
604 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
605 status->delay = runtime->buffer_size - status->avail;
606 else
607 status->delay = 0;
608 } else {
609 status->avail = snd_pcm_capture_avail(runtime);
610 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
611 status->delay = status->avail;
612 else
613 status->delay = 0;
614 }
615 status->avail_max = runtime->avail_max;
616 status->overrange = runtime->overrange;
617 runtime->avail_max = 0;
618 runtime->overrange = 0;
619 _end:
620 snd_pcm_stream_unlock_irq(substream);
621 return 0;
622}
623
Takashi Iwai877211f2005-11-17 13:59:38 +0100624static int snd_pcm_status_user(struct snd_pcm_substream *substream,
625 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Takashi Iwai877211f2005-11-17 13:59:38 +0100627 struct snd_pcm_status status;
628 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int res;
630
631 snd_assert(substream != NULL, return -ENXIO);
632 runtime = substream->runtime;
633 memset(&status, 0, sizeof(status));
634 res = snd_pcm_status(substream, &status);
635 if (res < 0)
636 return res;
637 if (copy_to_user(_status, &status, sizeof(status)))
638 return -EFAULT;
639 return 0;
640}
641
Takashi Iwai877211f2005-11-17 13:59:38 +0100642static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
643 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Takashi Iwai877211f2005-11-17 13:59:38 +0100645 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 unsigned int channel;
647
648 snd_assert(substream != NULL, return -ENXIO);
649 channel = info->channel;
650 runtime = substream->runtime;
651 snd_pcm_stream_lock_irq(substream);
652 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
653 snd_pcm_stream_unlock_irq(substream);
654 return -EBADFD;
655 }
656 snd_pcm_stream_unlock_irq(substream);
657 if (channel >= runtime->channels)
658 return -EINVAL;
659 memset(info, 0, sizeof(*info));
660 info->channel = channel;
661 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
662}
663
Takashi Iwai877211f2005-11-17 13:59:38 +0100664static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
665 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Takashi Iwai877211f2005-11-17 13:59:38 +0100667 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 int res;
669
670 if (copy_from_user(&info, _info, sizeof(info)))
671 return -EFAULT;
672 res = snd_pcm_channel_info(substream, &info);
673 if (res < 0)
674 return res;
675 if (copy_to_user(_info, &info, sizeof(info)))
676 return -EFAULT;
677 return 0;
678}
679
Takashi Iwai877211f2005-11-17 13:59:38 +0100680static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Takashi Iwai877211f2005-11-17 13:59:38 +0100682 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (runtime->trigger_master == NULL)
684 return;
685 if (runtime->trigger_master == substream) {
Jaroslav Kyselab751eef2007-12-13 10:19:42 +0100686 snd_pcm_gettime(runtime, &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 } else {
688 snd_pcm_trigger_tstamp(runtime->trigger_master);
689 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
690 }
691 runtime->trigger_master = NULL;
692}
693
694struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100695 int (*pre_action)(struct snd_pcm_substream *substream, int state);
696 int (*do_action)(struct snd_pcm_substream *substream, int state);
697 void (*undo_action)(struct snd_pcm_substream *substream, int state);
698 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699};
700
701/*
702 * this functions is core for handling of linked stream
703 * Note: the stream state might be changed also on failure
704 * Note2: call with calling stream lock + link lock
705 */
706static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100707 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int state, int do_lock)
709{
Takashi Iwai877211f2005-11-17 13:59:38 +0100710 struct snd_pcm_substream *s = NULL;
711 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int res = 0;
713
Takashi Iwaief991b92007-02-22 12:52:53 +0100714 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200716 spin_lock_nested(&s->self_group.lock,
717 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 res = ops->pre_action(s, state);
719 if (res < 0)
720 goto _unlock;
721 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100722 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 res = ops->do_action(s, state);
724 if (res < 0) {
725 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100726 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (s1 == s) /* failed stream */
728 break;
729 ops->undo_action(s1, state);
730 }
731 }
732 s = NULL; /* unlock all */
733 goto _unlock;
734 }
735 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100736 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ops->post_action(s, state);
738 }
739 _unlock:
740 if (do_lock) {
741 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100742 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (s1 != substream)
744 spin_unlock(&s1->self_group.lock);
745 if (s1 == s) /* end */
746 break;
747 }
748 }
749 return res;
750}
751
752/*
753 * Note: call with stream lock
754 */
755static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100756 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int state)
758{
759 int res;
760
761 res = ops->pre_action(substream, state);
762 if (res < 0)
763 return res;
764 res = ops->do_action(substream, state);
765 if (res == 0)
766 ops->post_action(substream, state);
767 else if (ops->undo_action)
768 ops->undo_action(substream, state);
769 return res;
770}
771
772/*
773 * Note: call with stream lock
774 */
775static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100776 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 int state)
778{
779 int res;
780
781 if (snd_pcm_stream_linked(substream)) {
782 if (!spin_trylock(&substream->group->lock)) {
783 spin_unlock(&substream->self_group.lock);
784 spin_lock(&substream->group->lock);
785 spin_lock(&substream->self_group.lock);
786 }
787 res = snd_pcm_action_group(ops, substream, state, 1);
788 spin_unlock(&substream->group->lock);
789 } else {
790 res = snd_pcm_action_single(ops, substream, state);
791 }
792 return res;
793}
794
795/*
796 * Note: don't use any locks before
797 */
798static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100799 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int state)
801{
802 int res;
803
804 read_lock_irq(&snd_pcm_link_rwlock);
805 if (snd_pcm_stream_linked(substream)) {
806 spin_lock(&substream->group->lock);
807 spin_lock(&substream->self_group.lock);
808 res = snd_pcm_action_group(ops, substream, state, 1);
809 spin_unlock(&substream->self_group.lock);
810 spin_unlock(&substream->group->lock);
811 } else {
812 spin_lock(&substream->self_group.lock);
813 res = snd_pcm_action_single(ops, substream, state);
814 spin_unlock(&substream->self_group.lock);
815 }
816 read_unlock_irq(&snd_pcm_link_rwlock);
817 return res;
818}
819
820/*
821 */
822static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100823 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 int state)
825{
826 int res;
827
828 down_read(&snd_pcm_link_rwsem);
829 if (snd_pcm_stream_linked(substream))
830 res = snd_pcm_action_group(ops, substream, state, 0);
831 else
832 res = snd_pcm_action_single(ops, substream, state);
833 up_read(&snd_pcm_link_rwsem);
834 return res;
835}
836
837/*
838 * start callbacks
839 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100840static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Takashi Iwai877211f2005-11-17 13:59:38 +0100842 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
844 return -EBADFD;
845 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
846 !snd_pcm_playback_data(substream))
847 return -EPIPE;
848 runtime->trigger_master = substream;
849 return 0;
850}
851
Takashi Iwai877211f2005-11-17 13:59:38 +0100852static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 if (substream->runtime->trigger_master != substream)
855 return 0;
856 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
857}
858
Takashi Iwai877211f2005-11-17 13:59:38 +0100859static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 if (substream->runtime->trigger_master == substream)
862 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
863}
864
Takashi Iwai877211f2005-11-17 13:59:38 +0100865static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Takashi Iwai877211f2005-11-17 13:59:38 +0100867 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 snd_pcm_trigger_tstamp(substream);
869 runtime->status->state = state;
870 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
871 runtime->silence_size > 0)
872 snd_pcm_playback_silence(substream, ULONG_MAX);
873 if (runtime->sleep_min)
874 snd_pcm_tick_prepare(substream);
875 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100876 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
877 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880static struct action_ops snd_pcm_action_start = {
881 .pre_action = snd_pcm_pre_start,
882 .do_action = snd_pcm_do_start,
883 .undo_action = snd_pcm_undo_start,
884 .post_action = snd_pcm_post_start
885};
886
887/**
888 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200889 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 *
891 * Start all linked streams.
892 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100893int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Takashi Iwai877211f2005-11-17 13:59:38 +0100895 return snd_pcm_action(&snd_pcm_action_start, substream,
896 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899/*
900 * stop callbacks
901 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100902static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwai877211f2005-11-17 13:59:38 +0100904 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
906 return -EBADFD;
907 runtime->trigger_master = substream;
908 return 0;
909}
910
Takashi Iwai877211f2005-11-17 13:59:38 +0100911static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 if (substream->runtime->trigger_master == substream &&
914 snd_pcm_running(substream))
915 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
916 return 0; /* unconditonally stop all substreams */
917}
918
Takashi Iwai877211f2005-11-17 13:59:38 +0100919static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Takashi Iwai877211f2005-11-17 13:59:38 +0100921 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (runtime->status->state != state) {
923 snd_pcm_trigger_tstamp(substream);
924 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100925 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
926 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 runtime->status->state = state;
928 snd_pcm_tick_set(substream, 0);
929 }
930 wake_up(&runtime->sleep);
931}
932
933static struct action_ops snd_pcm_action_stop = {
934 .pre_action = snd_pcm_pre_stop,
935 .do_action = snd_pcm_do_stop,
936 .post_action = snd_pcm_post_stop
937};
938
939/**
940 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200941 * @substream: the PCM substream instance
942 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 *
944 * Try to stop all running streams in the substream group.
945 * The state of each stream is changed to the given value after that unconditionally.
946 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100947int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
950}
951
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200952EXPORT_SYMBOL(snd_pcm_stop);
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/**
955 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200956 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 *
958 * Stop the DMA only when the given stream is playback.
959 * The state is changed to SETUP.
960 * Unlike snd_pcm_stop(), this affects only the given stream.
961 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100962int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Takashi Iwai877211f2005-11-17 13:59:38 +0100964 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
965 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968/*
969 * pause callbacks
970 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100971static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Takashi Iwai877211f2005-11-17 13:59:38 +0100973 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
975 return -ENOSYS;
976 if (push) {
977 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
978 return -EBADFD;
979 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
980 return -EBADFD;
981 runtime->trigger_master = substream;
982 return 0;
983}
984
Takashi Iwai877211f2005-11-17 13:59:38 +0100985static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 if (substream->runtime->trigger_master != substream)
988 return 0;
989 return substream->ops->trigger(substream,
990 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
991 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
992}
993
Takashi Iwai877211f2005-11-17 13:59:38 +0100994static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995{
996 if (substream->runtime->trigger_master == substream)
997 substream->ops->trigger(substream,
998 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
999 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1000}
1001
Takashi Iwai877211f2005-11-17 13:59:38 +01001002static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Takashi Iwai877211f2005-11-17 13:59:38 +01001004 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 snd_pcm_trigger_tstamp(substream);
1006 if (push) {
1007 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1008 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 snd_timer_notify(substream->timer,
1010 SNDRV_TIMER_EVENT_MPAUSE,
1011 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 snd_pcm_tick_set(substream, 0);
1013 wake_up(&runtime->sleep);
1014 } else {
1015 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1016 if (runtime->sleep_min)
1017 snd_pcm_tick_prepare(substream);
1018 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001019 snd_timer_notify(substream->timer,
1020 SNDRV_TIMER_EVENT_MCONTINUE,
1021 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023}
1024
1025static struct action_ops snd_pcm_action_pause = {
1026 .pre_action = snd_pcm_pre_pause,
1027 .do_action = snd_pcm_do_pause,
1028 .undo_action = snd_pcm_undo_pause,
1029 .post_action = snd_pcm_post_pause
1030};
1031
1032/*
1033 * Push/release the pause for all linked streams.
1034 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001035static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1038}
1039
1040#ifdef CONFIG_PM
1041/* suspend */
1042
Takashi Iwai877211f2005-11-17 13:59:38 +01001043static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Takashi Iwai877211f2005-11-17 13:59:38 +01001045 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1047 return -EBUSY;
1048 runtime->trigger_master = substream;
1049 return 0;
1050}
1051
Takashi Iwai877211f2005-11-17 13:59:38 +01001052static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Takashi Iwai877211f2005-11-17 13:59:38 +01001054 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (runtime->trigger_master != substream)
1056 return 0;
1057 if (! snd_pcm_running(substream))
1058 return 0;
1059 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1060 return 0; /* suspend unconditionally */
1061}
1062
Takashi Iwai877211f2005-11-17 13:59:38 +01001063static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Takashi Iwai877211f2005-11-17 13:59:38 +01001065 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 snd_pcm_trigger_tstamp(substream);
1067 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001068 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1069 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 runtime->status->suspended_state = runtime->status->state;
1071 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1072 snd_pcm_tick_set(substream, 0);
1073 wake_up(&runtime->sleep);
1074}
1075
1076static struct action_ops snd_pcm_action_suspend = {
1077 .pre_action = snd_pcm_pre_suspend,
1078 .do_action = snd_pcm_do_suspend,
1079 .post_action = snd_pcm_post_suspend
1080};
1081
1082/**
1083 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001084 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * Trigger SUSPEND to all linked streams.
1087 * After this call, all streams are changed to SUSPENDED state.
1088 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001089int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 int err;
1092 unsigned long flags;
1093
Takashi Iwai603bf5242005-11-17 15:59:14 +01001094 if (! substream)
1095 return 0;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 snd_pcm_stream_lock_irqsave(substream, flags);
1098 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1099 snd_pcm_stream_unlock_irqrestore(substream, flags);
1100 return err;
1101}
1102
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001103EXPORT_SYMBOL(snd_pcm_suspend);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105/**
1106 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001107 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 *
1109 * Trigger SUSPEND to all substreams in the given pcm.
1110 * After this call, all streams are changed to SUSPENDED state.
1111 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001112int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
Takashi Iwai877211f2005-11-17 13:59:38 +01001114 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int stream, err = 0;
1116
Takashi Iwai603bf5242005-11-17 15:59:14 +01001117 if (! pcm)
1118 return 0;
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001121 for (substream = pcm->streams[stream].substream;
1122 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 /* FIXME: the open/close code should lock this as well */
1124 if (substream->runtime == NULL)
1125 continue;
1126 err = snd_pcm_suspend(substream);
1127 if (err < 0 && err != -EBUSY)
1128 return err;
1129 }
1130 }
1131 return 0;
1132}
1133
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001134EXPORT_SYMBOL(snd_pcm_suspend_all);
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136/* resume */
1137
Takashi Iwai877211f2005-11-17 13:59:38 +01001138static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
Takashi Iwai877211f2005-11-17 13:59:38 +01001140 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1142 return -ENOSYS;
1143 runtime->trigger_master = substream;
1144 return 0;
1145}
1146
Takashi Iwai877211f2005-11-17 13:59:38 +01001147static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Takashi Iwai877211f2005-11-17 13:59:38 +01001149 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (runtime->trigger_master != substream)
1151 return 0;
1152 /* DMA not running previously? */
1153 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1154 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1155 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1156 return 0;
1157 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1158}
1159
Takashi Iwai877211f2005-11-17 13:59:38 +01001160static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 if (substream->runtime->trigger_master == substream &&
1163 snd_pcm_running(substream))
1164 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1165}
1166
Takashi Iwai877211f2005-11-17 13:59:38 +01001167static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 snd_pcm_trigger_tstamp(substream);
1171 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001172 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1173 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 runtime->status->state = runtime->status->suspended_state;
1175 if (runtime->sleep_min)
1176 snd_pcm_tick_prepare(substream);
1177}
1178
1179static struct action_ops snd_pcm_action_resume = {
1180 .pre_action = snd_pcm_pre_resume,
1181 .do_action = snd_pcm_do_resume,
1182 .undo_action = snd_pcm_undo_resume,
1183 .post_action = snd_pcm_post_resume
1184};
1185
Takashi Iwai877211f2005-11-17 13:59:38 +01001186static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwai877211f2005-11-17 13:59:38 +01001188 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int res;
1190
1191 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001192 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1194 snd_power_unlock(card);
1195 return res;
1196}
1197
1198#else
1199
Takashi Iwai877211f2005-11-17 13:59:38 +01001200static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
1202 return -ENOSYS;
1203}
1204
1205#endif /* CONFIG_PM */
1206
1207/*
1208 * xrun ioctl
1209 *
1210 * Change the RUNNING stream(s) to XRUN state.
1211 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001212static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Takashi Iwai877211f2005-11-17 13:59:38 +01001214 struct snd_card *card = substream->pcm->card;
1215 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int result;
1217
1218 snd_power_lock(card);
1219 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001220 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (result < 0)
1222 goto _unlock;
1223 }
1224
1225 snd_pcm_stream_lock_irq(substream);
1226 switch (runtime->status->state) {
1227 case SNDRV_PCM_STATE_XRUN:
1228 result = 0; /* already there */
1229 break;
1230 case SNDRV_PCM_STATE_RUNNING:
1231 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1232 break;
1233 default:
1234 result = -EBADFD;
1235 }
1236 snd_pcm_stream_unlock_irq(substream);
1237 _unlock:
1238 snd_power_unlock(card);
1239 return result;
1240}
1241
1242/*
1243 * reset ioctl
1244 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001245static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Takashi Iwai877211f2005-11-17 13:59:38 +01001247 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 switch (runtime->status->state) {
1249 case SNDRV_PCM_STATE_RUNNING:
1250 case SNDRV_PCM_STATE_PREPARED:
1251 case SNDRV_PCM_STATE_PAUSED:
1252 case SNDRV_PCM_STATE_SUSPENDED:
1253 return 0;
1254 default:
1255 return -EBADFD;
1256 }
1257}
1258
Takashi Iwai877211f2005-11-17 13:59:38 +01001259static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Takashi Iwai877211f2005-11-17 13:59:38 +01001261 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1263 if (err < 0)
1264 return err;
1265 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1266 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001267 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1268 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 runtime->silence_start = runtime->status->hw_ptr;
1270 runtime->silence_filled = 0;
1271 return 0;
1272}
1273
Takashi Iwai877211f2005-11-17 13:59:38 +01001274static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Takashi Iwai877211f2005-11-17 13:59:38 +01001276 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 runtime->control->appl_ptr = runtime->status->hw_ptr;
1278 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1279 runtime->silence_size > 0)
1280 snd_pcm_playback_silence(substream, ULONG_MAX);
1281}
1282
1283static struct action_ops snd_pcm_action_reset = {
1284 .pre_action = snd_pcm_pre_reset,
1285 .do_action = snd_pcm_do_reset,
1286 .post_action = snd_pcm_post_reset
1287};
1288
Takashi Iwai877211f2005-11-17 13:59:38 +01001289static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1292}
1293
1294/*
1295 * prepare ioctl
1296 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001297/* we use the second argument for updating f_flags */
1298static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1299 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Takashi Iwai877211f2005-11-17 13:59:38 +01001301 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001302 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1303 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return -EBADFD;
1305 if (snd_pcm_running(substream))
1306 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001307 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309}
1310
Takashi Iwai877211f2005-11-17 13:59:38 +01001311static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 int err;
1314 err = substream->ops->prepare(substream);
1315 if (err < 0)
1316 return err;
1317 return snd_pcm_do_reset(substream, 0);
1318}
1319
Takashi Iwai877211f2005-11-17 13:59:38 +01001320static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Takashi Iwai877211f2005-11-17 13:59:38 +01001322 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 runtime->control->appl_ptr = runtime->status->hw_ptr;
1324 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1325}
1326
1327static struct action_ops snd_pcm_action_prepare = {
1328 .pre_action = snd_pcm_pre_prepare,
1329 .do_action = snd_pcm_do_prepare,
1330 .post_action = snd_pcm_post_prepare
1331};
1332
1333/**
1334 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001335 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001336 * @file: file to refer f_flags
Takashi Iwaidf8db932005-09-07 13:38:19 +02001337 *
1338 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001340static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1341 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001344 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001345 int f_flags;
1346
1347 if (file)
1348 f_flags = file->f_flags;
1349 else
1350 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001353 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001354 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1355 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 snd_power_unlock(card);
1357 return res;
1358}
1359
1360/*
1361 * drain ioctl
1362 */
1363
Takashi Iwai877211f2005-11-17 13:59:38 +01001364static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001366 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EAGAIN;
1368 substream->runtime->trigger_master = substream;
1369 return 0;
1370}
1371
Takashi Iwai877211f2005-11-17 13:59:38 +01001372static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Takashi Iwai877211f2005-11-17 13:59:38 +01001374 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1376 switch (runtime->status->state) {
1377 case SNDRV_PCM_STATE_PREPARED:
1378 /* start playback stream if possible */
1379 if (! snd_pcm_playback_empty(substream)) {
1380 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1381 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1382 }
1383 break;
1384 case SNDRV_PCM_STATE_RUNNING:
1385 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1386 break;
1387 default:
1388 break;
1389 }
1390 } else {
1391 /* stop running stream */
1392 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001393 int new_state = snd_pcm_capture_avail(runtime) > 0 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
Marcin Åšlusarzb05e5782007-12-14 12:50:16 +01001395 snd_pcm_do_stop(substream, new_state);
1396 snd_pcm_post_stop(substream, new_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 }
1399 return 0;
1400}
1401
Takashi Iwai877211f2005-11-17 13:59:38 +01001402static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404}
1405
1406static struct action_ops snd_pcm_action_drain_init = {
1407 .pre_action = snd_pcm_pre_drain_init,
1408 .do_action = snd_pcm_do_drain_init,
1409 .post_action = snd_pcm_post_drain_init
1410};
1411
1412struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001413 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 wait_queue_t wait;
1415 snd_pcm_uframes_t stop_threshold;
1416};
1417
Takashi Iwai877211f2005-11-17 13:59:38 +01001418static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420/*
1421 * Drain the stream(s).
1422 * When the substream is linked, sync until the draining of all playback streams
1423 * is finished.
1424 * After this call, all streams are supposed to be either SETUP or DRAINING
1425 * (capture only) state.
1426 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001427static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Takashi Iwai877211f2005-11-17 13:59:38 +01001429 struct snd_card *card;
1430 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001431 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 int result = 0;
1433 int i, num_drecs;
1434 struct drain_rec *drec, drec_tmp, *d;
1435
1436 snd_assert(substream != NULL, return -ENXIO);
1437 card = substream->pcm->card;
1438 runtime = substream->runtime;
1439
1440 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1441 return -EBADFD;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 snd_power_lock(card);
1444 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001445 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001446 if (result < 0) {
1447 snd_power_unlock(card);
1448 return result;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
1452 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001453 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (snd_pcm_stream_linked(substream)) {
1455 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1456 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001457 up_read(&snd_pcm_link_rwsem);
1458 snd_power_unlock(card);
1459 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 } else
1462 drec = &drec_tmp;
1463
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001464 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001466 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1469 d = &drec[num_drecs++];
1470 d->substream = s;
1471 init_waitqueue_entry(&d->wait, current);
1472 add_wait_queue(&runtime->sleep, &d->wait);
1473 /* stop_threshold fixup to avoid endless loop when
1474 * stop_threshold > buffer_size
1475 */
1476 d->stop_threshold = runtime->stop_threshold;
1477 if (runtime->stop_threshold > runtime->buffer_size)
1478 runtime->stop_threshold = runtime->buffer_size;
1479 }
1480 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001481 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001482
1483 snd_pcm_stream_lock_irq(substream);
1484 /* resume pause */
Takashi Iwai1a7fa542007-07-06 12:27:25 +02001485 if (substream->runtime->status->state == SNDRV_PCM_STATE_PAUSED)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001486 snd_pcm_pause(substream, 0);
1487
1488 /* pre-start/stop - all running streams are changed to DRAINING state */
1489 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1490 if (result < 0) {
1491 snd_pcm_stream_unlock_irq(substream);
1492 goto _error;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 for (;;) {
1496 long tout;
1497 if (signal_pending(current)) {
1498 result = -ERESTARTSYS;
1499 break;
1500 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001501 /* all finished? */
1502 for (i = 0; i < num_drecs; i++) {
1503 runtime = drec[i].substream->runtime;
1504 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1505 break;
1506 }
1507 if (i == num_drecs)
1508 break; /* yes, all drained */
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 set_current_state(TASK_INTERRUPTIBLE);
1511 snd_pcm_stream_unlock_irq(substream);
1512 snd_power_unlock(card);
1513 tout = schedule_timeout(10 * HZ);
1514 snd_power_lock(card);
1515 snd_pcm_stream_lock_irq(substream);
1516 if (tout == 0) {
1517 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1518 result = -ESTRPIPE;
1519 else {
1520 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1521 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1522 result = -EIO;
1523 }
1524 break;
1525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001527
1528 snd_pcm_stream_unlock_irq(substream);
1529
1530 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 for (i = 0; i < num_drecs; i++) {
1532 d = &drec[i];
1533 runtime = d->substream->runtime;
1534 remove_wait_queue(&runtime->sleep, &d->wait);
1535 runtime->stop_threshold = d->stop_threshold;
1536 }
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (drec != &drec_tmp)
1539 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 return result;
1543}
1544
1545/*
1546 * drop ioctl
1547 *
1548 * Immediately put all linked substreams into SETUP state.
1549 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001550static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Takashi Iwai877211f2005-11-17 13:59:38 +01001552 struct snd_pcm_runtime *runtime;
1553 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 int result = 0;
1555
1556 snd_assert(substream != NULL, return -ENXIO);
1557 runtime = substream->runtime;
1558 card = substream->pcm->card;
1559
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001560 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1561 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 return -EBADFD;
1563
1564 snd_power_lock(card);
1565 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001566 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (result < 0)
1568 goto _unlock;
1569 }
1570
1571 snd_pcm_stream_lock_irq(substream);
1572 /* resume pause */
1573 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1574 snd_pcm_pause(substream, 0);
1575
1576 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1577 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1578 snd_pcm_stream_unlock_irq(substream);
1579 _unlock:
1580 snd_power_unlock(card);
1581 return result;
1582}
1583
1584
1585/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static struct file *snd_pcm_file_fd(int fd)
1587{
1588 struct file *file;
1589 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001590 unsigned int minor;
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 file = fget(fd);
1593 if (!file)
1594 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001595 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!S_ISCHR(inode->i_mode) ||
1597 imajor(inode) != snd_major) {
1598 fput(file);
1599 return NULL;
1600 }
1601 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001602 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1603 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 fput(file);
1605 return NULL;
1606 }
1607 return file;
1608}
1609
1610/*
1611 * PCM link handling
1612 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001613static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 int res = 0;
1616 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001617 struct snd_pcm_file *pcm_file;
1618 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
1620 file = snd_pcm_file_fd(fd);
1621 if (!file)
1622 return -EBADFD;
1623 pcm_file = file->private_data;
1624 substream1 = pcm_file->substream;
1625 down_write(&snd_pcm_link_rwsem);
1626 write_lock_irq(&snd_pcm_link_rwlock);
1627 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1628 substream->runtime->status->state != substream1->runtime->status->state) {
1629 res = -EBADFD;
1630 goto _end;
1631 }
1632 if (snd_pcm_stream_linked(substream1)) {
1633 res = -EALREADY;
1634 goto _end;
1635 }
1636 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001637 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 if (substream->group == NULL) {
1639 res = -ENOMEM;
1640 goto _end;
1641 }
1642 spin_lock_init(&substream->group->lock);
1643 INIT_LIST_HEAD(&substream->group->substreams);
1644 list_add_tail(&substream->link_list, &substream->group->substreams);
1645 substream->group->count = 1;
1646 }
1647 list_add_tail(&substream1->link_list, &substream->group->substreams);
1648 substream->group->count++;
1649 substream1->group = substream->group;
1650 _end:
1651 write_unlock_irq(&snd_pcm_link_rwlock);
1652 up_write(&snd_pcm_link_rwsem);
1653 fput(file);
1654 return res;
1655}
1656
Takashi Iwai877211f2005-11-17 13:59:38 +01001657static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 substream->group = &substream->self_group;
1660 INIT_LIST_HEAD(&substream->self_group.substreams);
1661 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1662}
1663
Takashi Iwai877211f2005-11-17 13:59:38 +01001664static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Takashi Iwaief991b92007-02-22 12:52:53 +01001666 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 int res = 0;
1668
1669 down_write(&snd_pcm_link_rwsem);
1670 write_lock_irq(&snd_pcm_link_rwlock);
1671 if (!snd_pcm_stream_linked(substream)) {
1672 res = -EALREADY;
1673 goto _end;
1674 }
1675 list_del(&substream->link_list);
1676 substream->group->count--;
1677 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001678 snd_pcm_group_for_each_entry(s, substream) {
1679 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 break;
1681 }
1682 kfree(substream->group);
1683 }
1684 relink_to_local(substream);
1685 _end:
1686 write_unlock_irq(&snd_pcm_link_rwlock);
1687 up_write(&snd_pcm_link_rwsem);
1688 return res;
1689}
1690
1691/*
1692 * hw configurator
1693 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001694static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1695 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Takashi Iwai877211f2005-11-17 13:59:38 +01001697 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1699 hw_param_interval_c(params, rule->deps[1]), &t);
1700 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1701}
1702
Takashi Iwai877211f2005-11-17 13:59:38 +01001703static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1704 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
Takashi Iwai877211f2005-11-17 13:59:38 +01001706 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1708 hw_param_interval_c(params, rule->deps[1]), &t);
1709 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1710}
1711
Takashi Iwai877211f2005-11-17 13:59:38 +01001712static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1713 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Takashi Iwai877211f2005-11-17 13:59:38 +01001715 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1717 hw_param_interval_c(params, rule->deps[1]),
1718 (unsigned long) rule->private, &t);
1719 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1720}
1721
Takashi Iwai877211f2005-11-17 13:59:38 +01001722static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1723 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Takashi Iwai877211f2005-11-17 13:59:38 +01001725 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1727 (unsigned long) rule->private,
1728 hw_param_interval_c(params, rule->deps[1]), &t);
1729 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1730}
1731
Takashi Iwai877211f2005-11-17 13:59:38 +01001732static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1733 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001736 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1737 struct snd_mask m;
1738 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 snd_mask_any(&m);
1740 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1741 int bits;
1742 if (! snd_mask_test(mask, k))
1743 continue;
1744 bits = snd_pcm_format_physical_width(k);
1745 if (bits <= 0)
1746 continue; /* ignore invalid formats */
1747 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1748 snd_mask_reset(&m, k);
1749 }
1750 return snd_mask_refine(mask, &m);
1751}
1752
Takashi Iwai877211f2005-11-17 13:59:38 +01001753static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Takashi Iwai877211f2005-11-17 13:59:38 +01001756 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned int k;
1758 t.min = UINT_MAX;
1759 t.max = 0;
1760 t.openmin = 0;
1761 t.openmax = 0;
1762 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1763 int bits;
1764 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1765 continue;
1766 bits = snd_pcm_format_physical_width(k);
1767 if (bits <= 0)
1768 continue; /* ignore invalid formats */
1769 if (t.min > (unsigned)bits)
1770 t.min = bits;
1771 if (t.max < (unsigned)bits)
1772 t.max = bits;
1773 }
1774 t.integer = 1;
1775 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1776}
1777
1778#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1779#error "Change this table"
1780#endif
1781
1782static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1783 48000, 64000, 88200, 96000, 176400, 192000 };
1784
Clemens Ladisch7653d552007-08-13 17:38:54 +02001785const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {
1786 .count = ARRAY_SIZE(rates),
1787 .list = rates,
1788};
1789
Takashi Iwai877211f2005-11-17 13:59:38 +01001790static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1791 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Takashi Iwai877211f2005-11-17 13:59:38 +01001793 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return snd_interval_list(hw_param_interval(params, rule->var),
Clemens Ladisch7653d552007-08-13 17:38:54 +02001795 snd_pcm_known_rates.count,
1796 snd_pcm_known_rates.list, hw->rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Takashi Iwai877211f2005-11-17 13:59:38 +01001799static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1800 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Takashi Iwai877211f2005-11-17 13:59:38 +01001802 struct snd_interval t;
1803 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 t.min = 0;
1805 t.max = substream->buffer_bytes_max;
1806 t.openmin = 0;
1807 t.openmax = 0;
1808 t.integer = 1;
1809 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1810}
1811
Takashi Iwai877211f2005-11-17 13:59:38 +01001812int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Takashi Iwai877211f2005-11-17 13:59:38 +01001814 struct snd_pcm_runtime *runtime = substream->runtime;
1815 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 int k, err;
1817
1818 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1819 snd_mask_any(constrs_mask(constrs, k));
1820 }
1821
1822 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1823 snd_interval_any(constrs_interval(constrs, k));
1824 }
1825
1826 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1827 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1828 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1829 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1830 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1831
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1833 snd_pcm_hw_rule_format, NULL,
1834 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1835 if (err < 0)
1836 return err;
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1838 snd_pcm_hw_rule_sample_bits, NULL,
1839 SNDRV_PCM_HW_PARAM_FORMAT,
1840 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1841 if (err < 0)
1842 return err;
1843 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1844 snd_pcm_hw_rule_div, NULL,
1845 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1846 if (err < 0)
1847 return err;
1848 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1849 snd_pcm_hw_rule_mul, NULL,
1850 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1851 if (err < 0)
1852 return err;
1853 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1854 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1855 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1856 if (err < 0)
1857 return err;
1858 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1859 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1860 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1861 if (err < 0)
1862 return err;
1863 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1864 snd_pcm_hw_rule_div, NULL,
1865 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1866 if (err < 0)
1867 return err;
1868 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1869 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1870 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1871 if (err < 0)
1872 return err;
1873 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1874 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1875 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1876 if (err < 0)
1877 return err;
1878 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1879 snd_pcm_hw_rule_div, NULL,
1880 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1881 if (err < 0)
1882 return err;
1883 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1884 snd_pcm_hw_rule_div, NULL,
1885 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1886 if (err < 0)
1887 return err;
1888 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1889 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1890 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1891 if (err < 0)
1892 return err;
1893 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1894 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1895 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1896 if (err < 0)
1897 return err;
1898 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1899 snd_pcm_hw_rule_mul, NULL,
1900 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1901 if (err < 0)
1902 return err;
1903 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1904 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1905 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1906 if (err < 0)
1907 return err;
1908 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1909 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1910 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1911 if (err < 0)
1912 return err;
1913 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1914 snd_pcm_hw_rule_muldivk, (void*) 8,
1915 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1916 if (err < 0)
1917 return err;
1918 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1919 snd_pcm_hw_rule_muldivk, (void*) 8,
1920 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1921 if (err < 0)
1922 return err;
1923 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1924 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1925 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1926 if (err < 0)
1927 return err;
1928 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1929 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1930 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1931 if (err < 0)
1932 return err;
1933 return 0;
1934}
1935
Takashi Iwai877211f2005-11-17 13:59:38 +01001936int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
Takashi Iwai877211f2005-11-17 13:59:38 +01001938 struct snd_pcm_runtime *runtime = substream->runtime;
1939 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int err;
1941 unsigned int mask = 0;
1942
1943 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1944 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1945 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1946 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1947 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1948 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1949 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1950 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1951 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1952 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1953 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1954 }
1955 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1956 snd_assert(err >= 0, return -EINVAL);
1957
1958 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1959 snd_assert(err >= 0, return -EINVAL);
1960
1961 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1962 snd_assert(err >= 0, return -EINVAL);
1963
1964 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1965 hw->channels_min, hw->channels_max);
1966 snd_assert(err >= 0, return -EINVAL);
1967
1968 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1969 hw->rate_min, hw->rate_max);
1970 snd_assert(err >= 0, return -EINVAL);
1971
1972 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1973 hw->period_bytes_min, hw->period_bytes_max);
1974 snd_assert(err >= 0, return -EINVAL);
1975
1976 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1977 hw->periods_min, hw->periods_max);
1978 snd_assert(err >= 0, return -EINVAL);
1979
1980 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1981 hw->period_bytes_min, hw->buffer_bytes_max);
1982 snd_assert(err >= 0, return -EINVAL);
1983
1984 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1985 snd_pcm_hw_rule_buffer_bytes_max, substream,
1986 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1987 if (err < 0)
1988 return err;
1989
1990 /* FIXME: remove */
1991 if (runtime->dma_bytes) {
1992 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1993 snd_assert(err >= 0, return -EINVAL);
1994 }
1995
1996 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1997 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1998 snd_pcm_hw_rule_rate, hw,
1999 SNDRV_PCM_HW_PARAM_RATE, -1);
2000 if (err < 0)
2001 return err;
2002 }
2003
2004 /* FIXME: this belong to lowlevel */
2005 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2006 1000000 / HZ, 1000000 / HZ);
2007 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2008
2009 return 0;
2010}
2011
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002012static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002015}
2016
2017void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2018{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002019 substream->ref_count--;
2020 if (substream->ref_count > 0)
2021 return;
2022
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002023 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002024 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (substream->ops->hw_free != NULL)
2026 substream->ops->hw_free(substream);
2027 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002028 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Takashi Iwai15762742006-04-06 19:47:42 +02002030 if (substream->pcm_release) {
2031 substream->pcm_release(substream);
2032 substream->pcm_release = NULL;
2033 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002034 snd_pcm_detach_substream(substream);
2035}
2036
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002037EXPORT_SYMBOL(snd_pcm_release_substream);
2038
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002039int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2040 struct file *file,
2041 struct snd_pcm_substream **rsubstream)
2042{
2043 struct snd_pcm_substream *substream;
2044 int err;
2045
2046 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2047 if (err < 0)
2048 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002049 if (substream->ref_count > 1) {
2050 *rsubstream = substream;
2051 return 0;
2052 }
2053
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002054 err = snd_pcm_hw_constraints_init(substream);
2055 if (err < 0) {
2056 snd_printd("snd_pcm_hw_constraints_init failed\n");
2057 goto error;
2058 }
2059
2060 if ((err = substream->ops->open(substream)) < 0)
2061 goto error;
2062
2063 substream->hw_opened = 1;
2064
2065 err = snd_pcm_hw_constraints_complete(substream);
2066 if (err < 0) {
2067 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2068 goto error;
2069 }
2070
2071 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002073
2074 error:
2075 snd_pcm_release_substream(substream);
2076 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077}
2078
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002079EXPORT_SYMBOL(snd_pcm_open_substream);
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002082 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002084 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Takashi Iwai877211f2005-11-17 13:59:38 +01002086 struct snd_pcm_file *pcm_file;
2087 struct snd_pcm_substream *substream;
2088 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002089 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 snd_assert(rpcm_file != NULL, return -EINVAL);
2092 *rpcm_file = NULL;
2093
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002094 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2095 if (err < 0)
2096 return err;
2097
Takashi Iwai548a6482006-07-31 16:51:51 +02002098 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2099 if (pcm_file == NULL) {
2100 snd_pcm_release_substream(substream);
2101 return -ENOMEM;
2102 }
2103 pcm_file->substream = substream;
2104 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002105 str = substream->pstr;
2106 substream->file = pcm_file;
2107 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 file->private_data = pcm_file;
2110 *rpcm_file = pcm_file;
2111 return 0;
2112}
2113
Clemens Ladischf87135f2005-11-20 14:06:59 +01002114static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Takashi Iwai877211f2005-11-17 13:59:38 +01002116 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002117
2118 pcm = snd_lookup_minor_data(iminor(inode),
2119 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2120 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2121}
2122
2123static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2124{
2125 struct snd_pcm *pcm;
2126
2127 pcm = snd_lookup_minor_data(iminor(inode),
2128 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2129 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2130}
2131
2132static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2133{
2134 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002135 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 wait_queue_t wait;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (pcm == NULL) {
2139 err = -ENODEV;
2140 goto __error1;
2141 }
2142 err = snd_card_file_add(pcm->card, file);
2143 if (err < 0)
2144 goto __error1;
2145 if (!try_module_get(pcm->card->module)) {
2146 err = -EFAULT;
2147 goto __error2;
2148 }
2149 init_waitqueue_entry(&wait, current);
2150 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002151 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002153 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (err >= 0)
2155 break;
2156 if (err == -EAGAIN) {
2157 if (file->f_flags & O_NONBLOCK) {
2158 err = -EBUSY;
2159 break;
2160 }
2161 } else
2162 break;
2163 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002164 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002166 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (signal_pending(current)) {
2168 err = -ERESTARTSYS;
2169 break;
2170 }
2171 }
2172 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002173 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (err < 0)
2175 goto __error;
2176 return err;
2177
2178 __error:
2179 module_put(pcm->card->module);
2180 __error2:
2181 snd_card_file_remove(pcm->card, file);
2182 __error1:
2183 return err;
2184}
2185
2186static int snd_pcm_release(struct inode *inode, struct file *file)
2187{
Takashi Iwai877211f2005-11-17 13:59:38 +01002188 struct snd_pcm *pcm;
2189 struct snd_pcm_substream *substream;
2190 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 pcm_file = file->private_data;
2193 substream = pcm_file->substream;
2194 snd_assert(substream != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 pcm = substream->pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002197 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002198 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002199 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002200 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 wake_up(&pcm->open_wait);
2202 module_put(pcm->card->module);
2203 snd_card_file_remove(pcm->card, file);
2204 return 0;
2205}
2206
Takashi Iwai877211f2005-11-17 13:59:38 +01002207static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2208 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209{
Takashi Iwai877211f2005-11-17 13:59:38 +01002210 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 snd_pcm_sframes_t appl_ptr;
2212 snd_pcm_sframes_t ret;
2213 snd_pcm_sframes_t hw_avail;
2214
2215 if (frames == 0)
2216 return 0;
2217
2218 snd_pcm_stream_lock_irq(substream);
2219 switch (runtime->status->state) {
2220 case SNDRV_PCM_STATE_PREPARED:
2221 break;
2222 case SNDRV_PCM_STATE_DRAINING:
2223 case SNDRV_PCM_STATE_RUNNING:
2224 if (snd_pcm_update_hw_ptr(substream) >= 0)
2225 break;
2226 /* Fall through */
2227 case SNDRV_PCM_STATE_XRUN:
2228 ret = -EPIPE;
2229 goto __end;
2230 default:
2231 ret = -EBADFD;
2232 goto __end;
2233 }
2234
2235 hw_avail = snd_pcm_playback_hw_avail(runtime);
2236 if (hw_avail <= 0) {
2237 ret = 0;
2238 goto __end;
2239 }
2240 if (frames > (snd_pcm_uframes_t)hw_avail)
2241 frames = hw_avail;
2242 else
2243 frames -= frames % runtime->xfer_align;
2244 appl_ptr = runtime->control->appl_ptr - frames;
2245 if (appl_ptr < 0)
2246 appl_ptr += runtime->boundary;
2247 runtime->control->appl_ptr = appl_ptr;
2248 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2249 runtime->sleep_min)
2250 snd_pcm_tick_prepare(substream);
2251 ret = frames;
2252 __end:
2253 snd_pcm_stream_unlock_irq(substream);
2254 return ret;
2255}
2256
Takashi Iwai877211f2005-11-17 13:59:38 +01002257static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2258 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Takashi Iwai877211f2005-11-17 13:59:38 +01002260 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 snd_pcm_sframes_t appl_ptr;
2262 snd_pcm_sframes_t ret;
2263 snd_pcm_sframes_t hw_avail;
2264
2265 if (frames == 0)
2266 return 0;
2267
2268 snd_pcm_stream_lock_irq(substream);
2269 switch (runtime->status->state) {
2270 case SNDRV_PCM_STATE_PREPARED:
2271 case SNDRV_PCM_STATE_DRAINING:
2272 break;
2273 case SNDRV_PCM_STATE_RUNNING:
2274 if (snd_pcm_update_hw_ptr(substream) >= 0)
2275 break;
2276 /* Fall through */
2277 case SNDRV_PCM_STATE_XRUN:
2278 ret = -EPIPE;
2279 goto __end;
2280 default:
2281 ret = -EBADFD;
2282 goto __end;
2283 }
2284
2285 hw_avail = snd_pcm_capture_hw_avail(runtime);
2286 if (hw_avail <= 0) {
2287 ret = 0;
2288 goto __end;
2289 }
2290 if (frames > (snd_pcm_uframes_t)hw_avail)
2291 frames = hw_avail;
2292 else
2293 frames -= frames % runtime->xfer_align;
2294 appl_ptr = runtime->control->appl_ptr - frames;
2295 if (appl_ptr < 0)
2296 appl_ptr += runtime->boundary;
2297 runtime->control->appl_ptr = appl_ptr;
2298 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2299 runtime->sleep_min)
2300 snd_pcm_tick_prepare(substream);
2301 ret = frames;
2302 __end:
2303 snd_pcm_stream_unlock_irq(substream);
2304 return ret;
2305}
2306
Takashi Iwai877211f2005-11-17 13:59:38 +01002307static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2308 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Takashi Iwai877211f2005-11-17 13:59:38 +01002310 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 snd_pcm_sframes_t appl_ptr;
2312 snd_pcm_sframes_t ret;
2313 snd_pcm_sframes_t avail;
2314
2315 if (frames == 0)
2316 return 0;
2317
2318 snd_pcm_stream_lock_irq(substream);
2319 switch (runtime->status->state) {
2320 case SNDRV_PCM_STATE_PREPARED:
2321 case SNDRV_PCM_STATE_PAUSED:
2322 break;
2323 case SNDRV_PCM_STATE_DRAINING:
2324 case SNDRV_PCM_STATE_RUNNING:
2325 if (snd_pcm_update_hw_ptr(substream) >= 0)
2326 break;
2327 /* Fall through */
2328 case SNDRV_PCM_STATE_XRUN:
2329 ret = -EPIPE;
2330 goto __end;
2331 default:
2332 ret = -EBADFD;
2333 goto __end;
2334 }
2335
2336 avail = snd_pcm_playback_avail(runtime);
2337 if (avail <= 0) {
2338 ret = 0;
2339 goto __end;
2340 }
2341 if (frames > (snd_pcm_uframes_t)avail)
2342 frames = avail;
2343 else
2344 frames -= frames % runtime->xfer_align;
2345 appl_ptr = runtime->control->appl_ptr + frames;
2346 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2347 appl_ptr -= runtime->boundary;
2348 runtime->control->appl_ptr = appl_ptr;
2349 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2350 runtime->sleep_min)
2351 snd_pcm_tick_prepare(substream);
2352 ret = frames;
2353 __end:
2354 snd_pcm_stream_unlock_irq(substream);
2355 return ret;
2356}
2357
Takashi Iwai877211f2005-11-17 13:59:38 +01002358static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2359 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Takashi Iwai877211f2005-11-17 13:59:38 +01002361 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 snd_pcm_sframes_t appl_ptr;
2363 snd_pcm_sframes_t ret;
2364 snd_pcm_sframes_t avail;
2365
2366 if (frames == 0)
2367 return 0;
2368
2369 snd_pcm_stream_lock_irq(substream);
2370 switch (runtime->status->state) {
2371 case SNDRV_PCM_STATE_PREPARED:
2372 case SNDRV_PCM_STATE_DRAINING:
2373 case SNDRV_PCM_STATE_PAUSED:
2374 break;
2375 case SNDRV_PCM_STATE_RUNNING:
2376 if (snd_pcm_update_hw_ptr(substream) >= 0)
2377 break;
2378 /* Fall through */
2379 case SNDRV_PCM_STATE_XRUN:
2380 ret = -EPIPE;
2381 goto __end;
2382 default:
2383 ret = -EBADFD;
2384 goto __end;
2385 }
2386
2387 avail = snd_pcm_capture_avail(runtime);
2388 if (avail <= 0) {
2389 ret = 0;
2390 goto __end;
2391 }
2392 if (frames > (snd_pcm_uframes_t)avail)
2393 frames = avail;
2394 else
2395 frames -= frames % runtime->xfer_align;
2396 appl_ptr = runtime->control->appl_ptr + frames;
2397 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2398 appl_ptr -= runtime->boundary;
2399 runtime->control->appl_ptr = appl_ptr;
2400 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2401 runtime->sleep_min)
2402 snd_pcm_tick_prepare(substream);
2403 ret = frames;
2404 __end:
2405 snd_pcm_stream_unlock_irq(substream);
2406 return ret;
2407}
2408
Takashi Iwai877211f2005-11-17 13:59:38 +01002409static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410{
Takashi Iwai877211f2005-11-17 13:59:38 +01002411 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 int err;
2413
2414 snd_pcm_stream_lock_irq(substream);
2415 switch (runtime->status->state) {
2416 case SNDRV_PCM_STATE_DRAINING:
2417 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2418 goto __badfd;
2419 case SNDRV_PCM_STATE_RUNNING:
2420 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2421 break;
2422 /* Fall through */
2423 case SNDRV_PCM_STATE_PREPARED:
2424 case SNDRV_PCM_STATE_SUSPENDED:
2425 err = 0;
2426 break;
2427 case SNDRV_PCM_STATE_XRUN:
2428 err = -EPIPE;
2429 break;
2430 default:
2431 __badfd:
2432 err = -EBADFD;
2433 break;
2434 }
2435 snd_pcm_stream_unlock_irq(substream);
2436 return err;
2437}
2438
Takashi Iwai877211f2005-11-17 13:59:38 +01002439static int snd_pcm_delay(struct snd_pcm_substream *substream,
2440 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
Takashi Iwai877211f2005-11-17 13:59:38 +01002442 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 int err;
2444 snd_pcm_sframes_t n = 0;
2445
2446 snd_pcm_stream_lock_irq(substream);
2447 switch (runtime->status->state) {
2448 case SNDRV_PCM_STATE_DRAINING:
2449 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2450 goto __badfd;
2451 case SNDRV_PCM_STATE_RUNNING:
2452 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2453 break;
2454 /* Fall through */
2455 case SNDRV_PCM_STATE_PREPARED:
2456 case SNDRV_PCM_STATE_SUSPENDED:
2457 err = 0;
2458 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2459 n = snd_pcm_playback_hw_avail(runtime);
2460 else
2461 n = snd_pcm_capture_avail(runtime);
2462 break;
2463 case SNDRV_PCM_STATE_XRUN:
2464 err = -EPIPE;
2465 break;
2466 default:
2467 __badfd:
2468 err = -EBADFD;
2469 break;
2470 }
2471 snd_pcm_stream_unlock_irq(substream);
2472 if (!err)
2473 if (put_user(n, res))
2474 err = -EFAULT;
2475 return err;
2476}
2477
Takashi Iwai877211f2005-11-17 13:59:38 +01002478static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2479 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Takashi Iwai877211f2005-11-17 13:59:38 +01002481 struct snd_pcm_runtime *runtime = substream->runtime;
2482 struct snd_pcm_sync_ptr sync_ptr;
2483 volatile struct snd_pcm_mmap_status *status;
2484 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 int err;
2486
2487 memset(&sync_ptr, 0, sizeof(sync_ptr));
2488 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2489 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002490 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 -07002491 return -EFAULT;
2492 status = runtime->status;
2493 control = runtime->control;
2494 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2495 err = snd_pcm_hwsync(substream);
2496 if (err < 0)
2497 return err;
2498 }
2499 snd_pcm_stream_lock_irq(substream);
2500 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2501 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2502 else
2503 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2504 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2505 control->avail_min = sync_ptr.c.control.avail_min;
2506 else
2507 sync_ptr.c.control.avail_min = control->avail_min;
2508 sync_ptr.s.status.state = status->state;
2509 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2510 sync_ptr.s.status.tstamp = status->tstamp;
2511 sync_ptr.s.status.suspended_state = status->suspended_state;
2512 snd_pcm_stream_unlock_irq(substream);
2513 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2514 return -EFAULT;
2515 return 0;
2516}
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002517
2518static int snd_pcm_tstamp(struct snd_pcm_substream *substream, int __user *_arg)
2519{
2520 struct snd_pcm_runtime *runtime = substream->runtime;
2521 int arg;
2522
2523 if (get_user(arg, _arg))
2524 return -EFAULT;
2525 if (arg < 0 || arg > SNDRV_PCM_TSTAMP_TYPE_LAST)
2526 return -EINVAL;
2527 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_GETTIMEOFDAY;
2528 if (arg == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
2529 runtime->tstamp_type = SNDRV_PCM_TSTAMP_TYPE_MONOTONIC;
2530 return 0;
2531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Takashi Iwai0df63e42006-04-28 15:13:41 +02002533static int snd_pcm_common_ioctl1(struct file *file,
2534 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 unsigned int cmd, void __user *arg)
2536{
2537 snd_assert(substream != NULL, return -ENXIO);
2538
2539 switch (cmd) {
2540 case SNDRV_PCM_IOCTL_PVERSION:
2541 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2542 case SNDRV_PCM_IOCTL_INFO:
2543 return snd_pcm_info_user(substream, arg);
Jaroslav Kysela28e9e472007-12-17 09:02:22 +01002544 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
2545 return 0;
Jaroslav Kyselab751eef2007-12-13 10:19:42 +01002546 case SNDRV_PCM_IOCTL_TTSTAMP:
2547 return snd_pcm_tstamp(substream, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 case SNDRV_PCM_IOCTL_HW_REFINE:
2549 return snd_pcm_hw_refine_user(substream, arg);
2550 case SNDRV_PCM_IOCTL_HW_PARAMS:
2551 return snd_pcm_hw_params_user(substream, arg);
2552 case SNDRV_PCM_IOCTL_HW_FREE:
2553 return snd_pcm_hw_free(substream);
2554 case SNDRV_PCM_IOCTL_SW_PARAMS:
2555 return snd_pcm_sw_params_user(substream, arg);
2556 case SNDRV_PCM_IOCTL_STATUS:
2557 return snd_pcm_status_user(substream, arg);
2558 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2559 return snd_pcm_channel_info_user(substream, arg);
2560 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002561 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 case SNDRV_PCM_IOCTL_RESET:
2563 return snd_pcm_reset(substream);
2564 case SNDRV_PCM_IOCTL_START:
2565 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2566 case SNDRV_PCM_IOCTL_LINK:
2567 return snd_pcm_link(substream, (int)(unsigned long) arg);
2568 case SNDRV_PCM_IOCTL_UNLINK:
2569 return snd_pcm_unlink(substream);
2570 case SNDRV_PCM_IOCTL_RESUME:
2571 return snd_pcm_resume(substream);
2572 case SNDRV_PCM_IOCTL_XRUN:
2573 return snd_pcm_xrun(substream);
2574 case SNDRV_PCM_IOCTL_HWSYNC:
2575 return snd_pcm_hwsync(substream);
2576 case SNDRV_PCM_IOCTL_DELAY:
2577 return snd_pcm_delay(substream, arg);
2578 case SNDRV_PCM_IOCTL_SYNC_PTR:
2579 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002580#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2582 return snd_pcm_hw_refine_old_user(substream, arg);
2583 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2584 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002585#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 case SNDRV_PCM_IOCTL_DRAIN:
2587 return snd_pcm_drain(substream);
2588 case SNDRV_PCM_IOCTL_DROP:
2589 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002590 case SNDRV_PCM_IOCTL_PAUSE:
2591 {
2592 int res;
2593 snd_pcm_stream_lock_irq(substream);
2594 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2595 snd_pcm_stream_unlock_irq(substream);
2596 return res;
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599 snd_printd("unknown ioctl = 0x%x\n", cmd);
2600 return -ENOTTY;
2601}
2602
Takashi Iwai0df63e42006-04-28 15:13:41 +02002603static int snd_pcm_playback_ioctl1(struct file *file,
2604 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 unsigned int cmd, void __user *arg)
2606{
2607 snd_assert(substream != NULL, return -ENXIO);
2608 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2609 switch (cmd) {
2610 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2611 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002612 struct snd_xferi xferi;
2613 struct snd_xferi __user *_xferi = arg;
2614 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 snd_pcm_sframes_t result;
2616 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2617 return -EBADFD;
2618 if (put_user(0, &_xferi->result))
2619 return -EFAULT;
2620 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2621 return -EFAULT;
2622 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2623 __put_user(result, &_xferi->result);
2624 return result < 0 ? result : 0;
2625 }
2626 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2627 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002628 struct snd_xfern xfern;
2629 struct snd_xfern __user *_xfern = arg;
2630 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 void __user **bufs;
2632 snd_pcm_sframes_t result;
2633 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2634 return -EBADFD;
2635 if (runtime->channels > 128)
2636 return -EINVAL;
2637 if (put_user(0, &_xfern->result))
2638 return -EFAULT;
2639 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2640 return -EFAULT;
2641 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2642 if (bufs == NULL)
2643 return -ENOMEM;
2644 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2645 kfree(bufs);
2646 return -EFAULT;
2647 }
2648 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2649 kfree(bufs);
2650 __put_user(result, &_xfern->result);
2651 return result < 0 ? result : 0;
2652 }
2653 case SNDRV_PCM_IOCTL_REWIND:
2654 {
2655 snd_pcm_uframes_t frames;
2656 snd_pcm_uframes_t __user *_frames = arg;
2657 snd_pcm_sframes_t result;
2658 if (get_user(frames, _frames))
2659 return -EFAULT;
2660 if (put_user(0, _frames))
2661 return -EFAULT;
2662 result = snd_pcm_playback_rewind(substream, frames);
2663 __put_user(result, _frames);
2664 return result < 0 ? result : 0;
2665 }
2666 case SNDRV_PCM_IOCTL_FORWARD:
2667 {
2668 snd_pcm_uframes_t frames;
2669 snd_pcm_uframes_t __user *_frames = arg;
2670 snd_pcm_sframes_t result;
2671 if (get_user(frames, _frames))
2672 return -EFAULT;
2673 if (put_user(0, _frames))
2674 return -EFAULT;
2675 result = snd_pcm_playback_forward(substream, frames);
2676 __put_user(result, _frames);
2677 return result < 0 ? result : 0;
2678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002680 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Takashi Iwai0df63e42006-04-28 15:13:41 +02002683static int snd_pcm_capture_ioctl1(struct file *file,
2684 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 unsigned int cmd, void __user *arg)
2686{
2687 snd_assert(substream != NULL, return -ENXIO);
2688 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2689 switch (cmd) {
2690 case SNDRV_PCM_IOCTL_READI_FRAMES:
2691 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002692 struct snd_xferi xferi;
2693 struct snd_xferi __user *_xferi = arg;
2694 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 snd_pcm_sframes_t result;
2696 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2697 return -EBADFD;
2698 if (put_user(0, &_xferi->result))
2699 return -EFAULT;
2700 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2701 return -EFAULT;
2702 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2703 __put_user(result, &_xferi->result);
2704 return result < 0 ? result : 0;
2705 }
2706 case SNDRV_PCM_IOCTL_READN_FRAMES:
2707 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002708 struct snd_xfern xfern;
2709 struct snd_xfern __user *_xfern = arg;
2710 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 void *bufs;
2712 snd_pcm_sframes_t result;
2713 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2714 return -EBADFD;
2715 if (runtime->channels > 128)
2716 return -EINVAL;
2717 if (put_user(0, &_xfern->result))
2718 return -EFAULT;
2719 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2720 return -EFAULT;
2721 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2722 if (bufs == NULL)
2723 return -ENOMEM;
2724 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2725 kfree(bufs);
2726 return -EFAULT;
2727 }
2728 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2729 kfree(bufs);
2730 __put_user(result, &_xfern->result);
2731 return result < 0 ? result : 0;
2732 }
2733 case SNDRV_PCM_IOCTL_REWIND:
2734 {
2735 snd_pcm_uframes_t frames;
2736 snd_pcm_uframes_t __user *_frames = arg;
2737 snd_pcm_sframes_t result;
2738 if (get_user(frames, _frames))
2739 return -EFAULT;
2740 if (put_user(0, _frames))
2741 return -EFAULT;
2742 result = snd_pcm_capture_rewind(substream, frames);
2743 __put_user(result, _frames);
2744 return result < 0 ? result : 0;
2745 }
2746 case SNDRV_PCM_IOCTL_FORWARD:
2747 {
2748 snd_pcm_uframes_t frames;
2749 snd_pcm_uframes_t __user *_frames = arg;
2750 snd_pcm_sframes_t result;
2751 if (get_user(frames, _frames))
2752 return -EFAULT;
2753 if (put_user(0, _frames))
2754 return -EFAULT;
2755 result = snd_pcm_capture_forward(substream, frames);
2756 __put_user(result, _frames);
2757 return result < 0 ? result : 0;
2758 }
2759 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002760 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Takashi Iwai877211f2005-11-17 13:59:38 +01002763static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2764 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Takashi Iwai877211f2005-11-17 13:59:38 +01002766 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 pcm_file = file->private_data;
2769
2770 if (((cmd >> 8) & 0xff) != 'A')
2771 return -ENOTTY;
2772
Takashi Iwai0df63e42006-04-28 15:13:41 +02002773 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2774 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
Takashi Iwai877211f2005-11-17 13:59:38 +01002777static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2778 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Takashi Iwai877211f2005-11-17 13:59:38 +01002780 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 pcm_file = file->private_data;
2783
2784 if (((cmd >> 8) & 0xff) != 'A')
2785 return -ENOTTY;
2786
Takashi Iwai0df63e42006-04-28 15:13:41 +02002787 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2788 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Takashi Iwai877211f2005-11-17 13:59:38 +01002791int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 unsigned int cmd, void *arg)
2793{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002794 mm_segment_t fs;
2795 int result;
2796
2797 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 switch (substream->stream) {
2799 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002800 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2801 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002804 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2805 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002808 result = -EINVAL;
2809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002811 snd_leave_user(fs);
2812 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002815EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2816
Takashi Iwai877211f2005-11-17 13:59:38 +01002817static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2818 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Takashi Iwai877211f2005-11-17 13:59:38 +01002820 struct snd_pcm_file *pcm_file;
2821 struct snd_pcm_substream *substream;
2822 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 snd_pcm_sframes_t result;
2824
2825 pcm_file = file->private_data;
2826 substream = pcm_file->substream;
2827 snd_assert(substream != NULL, return -ENXIO);
2828 runtime = substream->runtime;
2829 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2830 return -EBADFD;
2831 if (!frame_aligned(runtime, count))
2832 return -EINVAL;
2833 count = bytes_to_frames(runtime, count);
2834 result = snd_pcm_lib_read(substream, buf, count);
2835 if (result > 0)
2836 result = frames_to_bytes(runtime, result);
2837 return result;
2838}
2839
Takashi Iwai877211f2005-11-17 13:59:38 +01002840static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2841 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Takashi Iwai877211f2005-11-17 13:59:38 +01002843 struct snd_pcm_file *pcm_file;
2844 struct snd_pcm_substream *substream;
2845 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 snd_pcm_sframes_t result;
2847
2848 pcm_file = file->private_data;
2849 substream = pcm_file->substream;
2850 snd_assert(substream != NULL, result = -ENXIO; goto end);
2851 runtime = substream->runtime;
2852 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2853 result = -EBADFD;
2854 goto end;
2855 }
2856 if (!frame_aligned(runtime, count)) {
2857 result = -EINVAL;
2858 goto end;
2859 }
2860 count = bytes_to_frames(runtime, count);
2861 result = snd_pcm_lib_write(substream, buf, count);
2862 if (result > 0)
2863 result = frames_to_bytes(runtime, result);
2864 end:
2865 return result;
2866}
2867
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2869 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871{
Takashi Iwai877211f2005-11-17 13:59:38 +01002872 struct snd_pcm_file *pcm_file;
2873 struct snd_pcm_substream *substream;
2874 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 snd_pcm_sframes_t result;
2876 unsigned long i;
2877 void __user **bufs;
2878 snd_pcm_uframes_t frames;
2879
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002880 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 substream = pcm_file->substream;
2882 snd_assert(substream != NULL, return -ENXIO);
2883 runtime = substream->runtime;
2884 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2885 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002886 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002888 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002890 frames = bytes_to_samples(runtime, iov->iov_len);
2891 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (bufs == NULL)
2893 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002894 for (i = 0; i < nr_segs; ++i)
2895 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 result = snd_pcm_lib_readv(substream, bufs, frames);
2897 if (result > 0)
2898 result = frames_to_bytes(runtime, result);
2899 kfree(bufs);
2900 return result;
2901}
2902
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002903static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2904 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Takashi Iwai877211f2005-11-17 13:59:38 +01002906 struct snd_pcm_file *pcm_file;
2907 struct snd_pcm_substream *substream;
2908 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 snd_pcm_sframes_t result;
2910 unsigned long i;
2911 void __user **bufs;
2912 snd_pcm_uframes_t frames;
2913
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002914 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 substream = pcm_file->substream;
2916 snd_assert(substream != NULL, result = -ENXIO; goto end);
2917 runtime = substream->runtime;
2918 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2919 result = -EBADFD;
2920 goto end;
2921 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002922 if (nr_segs > 128 || nr_segs != runtime->channels ||
2923 !frame_aligned(runtime, iov->iov_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 result = -EINVAL;
2925 goto end;
2926 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002927 frames = bytes_to_samples(runtime, iov->iov_len);
2928 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 if (bufs == NULL)
2930 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002931 for (i = 0; i < nr_segs; ++i)
2932 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 result = snd_pcm_lib_writev(substream, bufs, frames);
2934 if (result > 0)
2935 result = frames_to_bytes(runtime, result);
2936 kfree(bufs);
2937 end:
2938 return result;
2939}
2940
2941static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2942{
Takashi Iwai877211f2005-11-17 13:59:38 +01002943 struct snd_pcm_file *pcm_file;
2944 struct snd_pcm_substream *substream;
2945 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 unsigned int mask;
2947 snd_pcm_uframes_t avail;
2948
2949 pcm_file = file->private_data;
2950
2951 substream = pcm_file->substream;
2952 snd_assert(substream != NULL, return -ENXIO);
2953 runtime = substream->runtime;
2954
2955 poll_wait(file, &runtime->sleep, wait);
2956
2957 snd_pcm_stream_lock_irq(substream);
2958 avail = snd_pcm_playback_avail(runtime);
2959 switch (runtime->status->state) {
2960 case SNDRV_PCM_STATE_RUNNING:
2961 case SNDRV_PCM_STATE_PREPARED:
2962 case SNDRV_PCM_STATE_PAUSED:
2963 if (avail >= runtime->control->avail_min) {
2964 mask = POLLOUT | POLLWRNORM;
2965 break;
2966 }
2967 /* Fall through */
2968 case SNDRV_PCM_STATE_DRAINING:
2969 mask = 0;
2970 break;
2971 default:
2972 mask = POLLOUT | POLLWRNORM | POLLERR;
2973 break;
2974 }
2975 snd_pcm_stream_unlock_irq(substream);
2976 return mask;
2977}
2978
2979static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2980{
Takashi Iwai877211f2005-11-17 13:59:38 +01002981 struct snd_pcm_file *pcm_file;
2982 struct snd_pcm_substream *substream;
2983 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 unsigned int mask;
2985 snd_pcm_uframes_t avail;
2986
2987 pcm_file = file->private_data;
2988
2989 substream = pcm_file->substream;
2990 snd_assert(substream != NULL, return -ENXIO);
2991 runtime = substream->runtime;
2992
2993 poll_wait(file, &runtime->sleep, wait);
2994
2995 snd_pcm_stream_lock_irq(substream);
2996 avail = snd_pcm_capture_avail(runtime);
2997 switch (runtime->status->state) {
2998 case SNDRV_PCM_STATE_RUNNING:
2999 case SNDRV_PCM_STATE_PREPARED:
3000 case SNDRV_PCM_STATE_PAUSED:
3001 if (avail >= runtime->control->avail_min) {
3002 mask = POLLIN | POLLRDNORM;
3003 break;
3004 }
3005 mask = 0;
3006 break;
3007 case SNDRV_PCM_STATE_DRAINING:
3008 if (avail > 0) {
3009 mask = POLLIN | POLLRDNORM;
3010 break;
3011 }
3012 /* Fall through */
3013 default:
3014 mask = POLLIN | POLLRDNORM | POLLERR;
3015 break;
3016 }
3017 snd_pcm_stream_unlock_irq(substream);
3018 return mask;
3019}
3020
3021/*
3022 * mmap support
3023 */
3024
3025/*
3026 * Only on coherent architectures, we can mmap the status and the control records
3027 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3028 */
3029#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3030/*
3031 * mmap status record
3032 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003033static int snd_pcm_mmap_status_fault(struct vm_area_struct *area,
3034 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035{
Takashi Iwai877211f2005-11-17 13:59:38 +01003036 struct snd_pcm_substream *substream = area->vm_private_data;
3037 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003040 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003042 vmf->page = virt_to_page(runtime->status);
3043 get_page(vmf->page);
3044 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
3047static struct vm_operations_struct snd_pcm_vm_ops_status =
3048{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003049 .fault = snd_pcm_mmap_status_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050};
3051
Takashi Iwai877211f2005-11-17 13:59:38 +01003052static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 struct vm_area_struct *area)
3054{
Takashi Iwai877211f2005-11-17 13:59:38 +01003055 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 long size;
3057 if (!(area->vm_flags & VM_READ))
3058 return -EINVAL;
3059 runtime = substream->runtime;
3060 snd_assert(runtime != NULL, return -EAGAIN);
3061 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003062 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return -EINVAL;
3064 area->vm_ops = &snd_pcm_vm_ops_status;
3065 area->vm_private_data = substream;
3066 area->vm_flags |= VM_RESERVED;
3067 return 0;
3068}
3069
3070/*
3071 * mmap control record
3072 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003073static int snd_pcm_mmap_control_fault(struct vm_area_struct *area,
3074 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Takashi Iwai877211f2005-11-17 13:59:38 +01003076 struct snd_pcm_substream *substream = area->vm_private_data;
3077 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
3079 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003080 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003082 vmf->page = virt_to_page(runtime->control);
3083 get_page(vmf->page);
3084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
3087static struct vm_operations_struct snd_pcm_vm_ops_control =
3088{
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003089 .fault = snd_pcm_mmap_control_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090};
3091
Takashi Iwai877211f2005-11-17 13:59:38 +01003092static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 struct vm_area_struct *area)
3094{
Takashi Iwai877211f2005-11-17 13:59:38 +01003095 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 long size;
3097 if (!(area->vm_flags & VM_READ))
3098 return -EINVAL;
3099 runtime = substream->runtime;
3100 snd_assert(runtime != NULL, return -EAGAIN);
3101 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003102 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return -EINVAL;
3104 area->vm_ops = &snd_pcm_vm_ops_control;
3105 area->vm_private_data = substream;
3106 area->vm_flags |= VM_RESERVED;
3107 return 0;
3108}
3109#else /* ! coherent mmap */
3110/*
3111 * don't support mmap for status and control records.
3112 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003113static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 struct vm_area_struct *area)
3115{
3116 return -ENXIO;
3117}
Takashi Iwai877211f2005-11-17 13:59:38 +01003118static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct vm_area_struct *area)
3120{
3121 return -ENXIO;
3122}
3123#endif /* coherent mmap */
3124
3125/*
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003126 * fault callback for mmapping a RAM page
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 */
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003128static int snd_pcm_mmap_data_fault(struct vm_area_struct *area,
3129 struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Takashi Iwai877211f2005-11-17 13:59:38 +01003131 struct snd_pcm_substream *substream = area->vm_private_data;
3132 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 unsigned long offset;
3134 struct page * page;
3135 void *vaddr;
3136 size_t dma_bytes;
3137
3138 if (substream == NULL)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003139 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 runtime = substream->runtime;
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003141 offset = vmf->pgoff << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3143 if (offset > dma_bytes - PAGE_SIZE)
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003144 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 if (substream->ops->page) {
3146 page = substream->ops->page(substream, offset);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003147 if (!page)
3148 return VM_FAULT_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 } else {
3150 vaddr = runtime->dma_area + offset;
3151 page = virt_to_page(vaddr);
3152 }
Nick Pigginb5810032005-10-29 18:16:12 -07003153 get_page(page);
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003154 vmf->page = page;
3155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
3158static struct vm_operations_struct snd_pcm_vm_ops_data =
3159{
3160 .open = snd_pcm_mmap_data_open,
3161 .close = snd_pcm_mmap_data_close,
Nick Piggin3ad5afc2007-12-13 16:15:00 +01003162 .fault = snd_pcm_mmap_data_fault,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163};
3164
3165/*
3166 * mmap the DMA buffer on RAM
3167 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003168static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3169 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
3171 area->vm_ops = &snd_pcm_vm_ops_data;
3172 area->vm_private_data = substream;
3173 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003174 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return 0;
3176}
3177
3178/*
3179 * mmap the DMA buffer on I/O memory area
3180 */
3181#if SNDRV_PCM_INFO_MMAP_IOMEM
3182static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3183{
3184 .open = snd_pcm_mmap_data_open,
3185 .close = snd_pcm_mmap_data_close,
3186};
3187
Takashi Iwai877211f2005-11-17 13:59:38 +01003188int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3189 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 long size;
3192 unsigned long offset;
3193
3194#ifdef pgprot_noncached
3195 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3196#endif
3197 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3198 area->vm_private_data = substream;
3199 area->vm_flags |= VM_IO;
3200 size = area->vm_end - area->vm_start;
3201 offset = area->vm_pgoff << PAGE_SHIFT;
3202 if (io_remap_pfn_range(area, area->vm_start,
3203 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3204 size, area->vm_page_prot))
3205 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003206 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return 0;
3208}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003209
3210EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211#endif /* SNDRV_PCM_INFO_MMAP */
3212
3213/*
3214 * mmap DMA buffer
3215 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003216int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 struct vm_area_struct *area)
3218{
Takashi Iwai877211f2005-11-17 13:59:38 +01003219 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 long size;
3221 unsigned long offset;
3222 size_t dma_bytes;
3223
3224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3225 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3226 return -EINVAL;
3227 } else {
3228 if (!(area->vm_flags & VM_READ))
3229 return -EINVAL;
3230 }
3231 runtime = substream->runtime;
3232 snd_assert(runtime != NULL, return -EAGAIN);
3233 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3234 return -EBADFD;
3235 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3236 return -ENXIO;
3237 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3238 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3239 return -EINVAL;
3240 size = area->vm_end - area->vm_start;
3241 offset = area->vm_pgoff << PAGE_SHIFT;
3242 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3243 if ((size_t)size > dma_bytes)
3244 return -EINVAL;
3245 if (offset > dma_bytes - size)
3246 return -EINVAL;
3247
3248 if (substream->ops->mmap)
3249 return substream->ops->mmap(substream, area);
3250 else
3251 return snd_pcm_default_mmap(substream, area);
3252}
3253
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003254EXPORT_SYMBOL(snd_pcm_mmap_data);
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3257{
Takashi Iwai877211f2005-11-17 13:59:38 +01003258 struct snd_pcm_file * pcm_file;
3259 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 unsigned long offset;
3261
3262 pcm_file = file->private_data;
3263 substream = pcm_file->substream;
3264 snd_assert(substream != NULL, return -ENXIO);
3265
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 int err;
3288
3289 pcm_file = file->private_data;
3290 substream = pcm_file->substream;
3291 snd_assert(substream != NULL, return -ENXIO);
3292 runtime = substream->runtime;
3293
3294 err = fasync_helper(fd, file, on, &runtime->fasync);
3295 if (err < 0)
3296 return err;
3297 return 0;
3298}
3299
3300/*
3301 * ioctl32 compat
3302 */
3303#ifdef CONFIG_COMPAT
3304#include "pcm_compat.c"
3305#else
3306#define snd_pcm_ioctl_compat NULL
3307#endif
3308
3309/*
3310 * To be removed helpers to keep binary compatibility
3311 */
3312
Takashi Iwai59d48582005-12-01 10:51:58 +01003313#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3315#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3316
Takashi Iwai877211f2005-11-17 13:59:38 +01003317static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3318 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 unsigned int i;
3321
3322 memset(params, 0, sizeof(*params));
3323 params->flags = oparams->flags;
3324 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3325 params->masks[i].bits[0] = oparams->masks[i];
3326 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3327 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3328 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3329 params->info = oparams->info;
3330 params->msbits = oparams->msbits;
3331 params->rate_num = oparams->rate_num;
3332 params->rate_den = oparams->rate_den;
3333 params->fifo_size = oparams->fifo_size;
3334}
3335
Takashi Iwai877211f2005-11-17 13:59:38 +01003336static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3337 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 unsigned int i;
3340
3341 memset(oparams, 0, sizeof(*oparams));
3342 oparams->flags = params->flags;
3343 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3344 oparams->masks[i] = params->masks[i].bits[0];
3345 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3346 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3347 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3348 oparams->info = params->info;
3349 oparams->msbits = params->msbits;
3350 oparams->rate_num = params->rate_num;
3351 oparams->rate_den = params->rate_den;
3352 oparams->fifo_size = params->fifo_size;
3353}
3354
Takashi Iwai877211f2005-11-17 13:59:38 +01003355static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3356 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Takashi Iwai877211f2005-11-17 13:59:38 +01003358 struct snd_pcm_hw_params *params;
3359 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 int err;
3361
3362 params = kmalloc(sizeof(*params), GFP_KERNEL);
3363 if (!params) {
3364 err = -ENOMEM;
3365 goto out;
3366 }
3367 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3368 if (!oparams) {
3369 err = -ENOMEM;
3370 goto out;
3371 }
3372
3373 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3374 err = -EFAULT;
3375 goto out;
3376 }
3377 snd_pcm_hw_convert_from_old_params(params, oparams);
3378 err = snd_pcm_hw_refine(substream, params);
3379 snd_pcm_hw_convert_to_old_params(oparams, params);
3380 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3381 if (!err)
3382 err = -EFAULT;
3383 }
3384out:
3385 kfree(params);
3386 kfree(oparams);
3387 return err;
3388}
3389
Takashi Iwai877211f2005-11-17 13:59:38 +01003390static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3391 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
Takashi Iwai877211f2005-11-17 13:59:38 +01003393 struct snd_pcm_hw_params *params;
3394 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 int err;
3396
3397 params = kmalloc(sizeof(*params), GFP_KERNEL);
3398 if (!params) {
3399 err = -ENOMEM;
3400 goto out;
3401 }
3402 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3403 if (!oparams) {
3404 err = -ENOMEM;
3405 goto out;
3406 }
3407 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3408 err = -EFAULT;
3409 goto out;
3410 }
3411 snd_pcm_hw_convert_from_old_params(params, oparams);
3412 err = snd_pcm_hw_params(substream, params);
3413 snd_pcm_hw_convert_to_old_params(oparams, params);
3414 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3415 if (!err)
3416 err = -EFAULT;
3417 }
3418out:
3419 kfree(params);
3420 kfree(oparams);
3421 return err;
3422}
Takashi Iwai59d48582005-12-01 10:51:58 +01003423#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
3425/*
3426 * Register section
3427 */
3428
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003429const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003431 .owner = THIS_MODULE,
3432 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003433 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003434 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003435 .release = snd_pcm_release,
3436 .poll = snd_pcm_playback_poll,
3437 .unlocked_ioctl = snd_pcm_playback_ioctl,
3438 .compat_ioctl = snd_pcm_ioctl_compat,
3439 .mmap = snd_pcm_mmap,
3440 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 },
3442 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003443 .owner = THIS_MODULE,
3444 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003445 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003446 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003447 .release = snd_pcm_release,
3448 .poll = snd_pcm_capture_poll,
3449 .unlocked_ioctl = snd_pcm_capture_ioctl,
3450 .compat_ioctl = snd_pcm_ioctl_compat,
3451 .mmap = snd_pcm_mmap,
3452 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
3454};