blob: a96733a5beb88aa6cdd556d7c2f19d8797f0bd51 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4 *
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;
596 if (snd_pcm_running(substream)) {
597 snd_pcm_update_hw_ptr(substream);
598 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
599 status->tstamp = runtime->status->tstamp;
600 else
Takashi Iwai07799e72005-10-10 11:49:49 +0200601 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 } else
Takashi Iwai07799e72005-10-10 11:49:49 +0200603 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 status->appl_ptr = runtime->control->appl_ptr;
605 status->hw_ptr = runtime->status->hw_ptr;
606 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
607 status->avail = snd_pcm_playback_avail(runtime);
608 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
609 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
610 status->delay = runtime->buffer_size - status->avail;
611 else
612 status->delay = 0;
613 } else {
614 status->avail = snd_pcm_capture_avail(runtime);
615 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
616 status->delay = status->avail;
617 else
618 status->delay = 0;
619 }
620 status->avail_max = runtime->avail_max;
621 status->overrange = runtime->overrange;
622 runtime->avail_max = 0;
623 runtime->overrange = 0;
624 _end:
625 snd_pcm_stream_unlock_irq(substream);
626 return 0;
627}
628
Takashi Iwai877211f2005-11-17 13:59:38 +0100629static int snd_pcm_status_user(struct snd_pcm_substream *substream,
630 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Takashi Iwai877211f2005-11-17 13:59:38 +0100632 struct snd_pcm_status status;
633 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 int res;
635
636 snd_assert(substream != NULL, return -ENXIO);
637 runtime = substream->runtime;
638 memset(&status, 0, sizeof(status));
639 res = snd_pcm_status(substream, &status);
640 if (res < 0)
641 return res;
642 if (copy_to_user(_status, &status, sizeof(status)))
643 return -EFAULT;
644 return 0;
645}
646
Takashi Iwai877211f2005-11-17 13:59:38 +0100647static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
648 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Takashi Iwai877211f2005-11-17 13:59:38 +0100650 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 unsigned int channel;
652
653 snd_assert(substream != NULL, return -ENXIO);
654 channel = info->channel;
655 runtime = substream->runtime;
656 snd_pcm_stream_lock_irq(substream);
657 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
658 snd_pcm_stream_unlock_irq(substream);
659 return -EBADFD;
660 }
661 snd_pcm_stream_unlock_irq(substream);
662 if (channel >= runtime->channels)
663 return -EINVAL;
664 memset(info, 0, sizeof(*info));
665 info->channel = channel;
666 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
667}
668
Takashi Iwai877211f2005-11-17 13:59:38 +0100669static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
670 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Takashi Iwai877211f2005-11-17 13:59:38 +0100672 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int res;
674
675 if (copy_from_user(&info, _info, sizeof(info)))
676 return -EFAULT;
677 res = snd_pcm_channel_info(substream, &info);
678 if (res < 0)
679 return res;
680 if (copy_to_user(_info, &info, sizeof(info)))
681 return -EFAULT;
682 return 0;
683}
684
Takashi Iwai877211f2005-11-17 13:59:38 +0100685static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Takashi Iwai877211f2005-11-17 13:59:38 +0100687 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (runtime->trigger_master == NULL)
689 return;
690 if (runtime->trigger_master == substream) {
Takashi Iwai07799e72005-10-10 11:49:49 +0200691 getnstimeofday(&runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 } else {
693 snd_pcm_trigger_tstamp(runtime->trigger_master);
694 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
695 }
696 runtime->trigger_master = NULL;
697}
698
699struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100700 int (*pre_action)(struct snd_pcm_substream *substream, int state);
701 int (*do_action)(struct snd_pcm_substream *substream, int state);
702 void (*undo_action)(struct snd_pcm_substream *substream, int state);
703 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704};
705
706/*
707 * this functions is core for handling of linked stream
708 * Note: the stream state might be changed also on failure
709 * Note2: call with calling stream lock + link lock
710 */
711static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100712 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int state, int do_lock)
714{
Takashi Iwai877211f2005-11-17 13:59:38 +0100715 struct snd_pcm_substream *s = NULL;
716 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 int res = 0;
718
Takashi Iwaief991b92007-02-22 12:52:53 +0100719 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (do_lock && s != substream)
Frederik Deweerdt208eee22007-04-05 16:57:41 +0200721 spin_lock_nested(&s->self_group.lock,
722 SINGLE_DEPTH_NESTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 res = ops->pre_action(s, state);
724 if (res < 0)
725 goto _unlock;
726 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100727 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 res = ops->do_action(s, state);
729 if (res < 0) {
730 if (ops->undo_action) {
Takashi Iwaief991b92007-02-22 12:52:53 +0100731 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (s1 == s) /* failed stream */
733 break;
734 ops->undo_action(s1, state);
735 }
736 }
737 s = NULL; /* unlock all */
738 goto _unlock;
739 }
740 }
Takashi Iwaief991b92007-02-22 12:52:53 +0100741 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ops->post_action(s, state);
743 }
744 _unlock:
745 if (do_lock) {
746 /* unlock streams */
Takashi Iwaief991b92007-02-22 12:52:53 +0100747 snd_pcm_group_for_each_entry(s1, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (s1 != substream)
749 spin_unlock(&s1->self_group.lock);
750 if (s1 == s) /* end */
751 break;
752 }
753 }
754 return res;
755}
756
757/*
758 * Note: call with stream lock
759 */
760static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100761 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 int state)
763{
764 int res;
765
766 res = ops->pre_action(substream, state);
767 if (res < 0)
768 return res;
769 res = ops->do_action(substream, state);
770 if (res == 0)
771 ops->post_action(substream, state);
772 else if (ops->undo_action)
773 ops->undo_action(substream, state);
774 return res;
775}
776
777/*
778 * Note: call with stream lock
779 */
780static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100781 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 int state)
783{
784 int res;
785
786 if (snd_pcm_stream_linked(substream)) {
787 if (!spin_trylock(&substream->group->lock)) {
788 spin_unlock(&substream->self_group.lock);
789 spin_lock(&substream->group->lock);
790 spin_lock(&substream->self_group.lock);
791 }
792 res = snd_pcm_action_group(ops, substream, state, 1);
793 spin_unlock(&substream->group->lock);
794 } else {
795 res = snd_pcm_action_single(ops, substream, state);
796 }
797 return res;
798}
799
800/*
801 * Note: don't use any locks before
802 */
803static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100804 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 int state)
806{
807 int res;
808
809 read_lock_irq(&snd_pcm_link_rwlock);
810 if (snd_pcm_stream_linked(substream)) {
811 spin_lock(&substream->group->lock);
812 spin_lock(&substream->self_group.lock);
813 res = snd_pcm_action_group(ops, substream, state, 1);
814 spin_unlock(&substream->self_group.lock);
815 spin_unlock(&substream->group->lock);
816 } else {
817 spin_lock(&substream->self_group.lock);
818 res = snd_pcm_action_single(ops, substream, state);
819 spin_unlock(&substream->self_group.lock);
820 }
821 read_unlock_irq(&snd_pcm_link_rwlock);
822 return res;
823}
824
825/*
826 */
827static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100828 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int state)
830{
831 int res;
832
833 down_read(&snd_pcm_link_rwsem);
834 if (snd_pcm_stream_linked(substream))
835 res = snd_pcm_action_group(ops, substream, state, 0);
836 else
837 res = snd_pcm_action_single(ops, substream, state);
838 up_read(&snd_pcm_link_rwsem);
839 return res;
840}
841
842/*
843 * start callbacks
844 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100845static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Takashi Iwai877211f2005-11-17 13:59:38 +0100847 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
849 return -EBADFD;
850 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
851 !snd_pcm_playback_data(substream))
852 return -EPIPE;
853 runtime->trigger_master = substream;
854 return 0;
855}
856
Takashi Iwai877211f2005-11-17 13:59:38 +0100857static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 if (substream->runtime->trigger_master != substream)
860 return 0;
861 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
862}
863
Takashi Iwai877211f2005-11-17 13:59:38 +0100864static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 if (substream->runtime->trigger_master == substream)
867 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
868}
869
Takashi Iwai877211f2005-11-17 13:59:38 +0100870static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Takashi Iwai877211f2005-11-17 13:59:38 +0100872 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 snd_pcm_trigger_tstamp(substream);
874 runtime->status->state = state;
875 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
876 runtime->silence_size > 0)
877 snd_pcm_playback_silence(substream, ULONG_MAX);
878 if (runtime->sleep_min)
879 snd_pcm_tick_prepare(substream);
880 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100881 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
882 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
885static struct action_ops snd_pcm_action_start = {
886 .pre_action = snd_pcm_pre_start,
887 .do_action = snd_pcm_do_start,
888 .undo_action = snd_pcm_undo_start,
889 .post_action = snd_pcm_post_start
890};
891
892/**
893 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200894 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 *
896 * Start all linked streams.
897 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100898int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Takashi Iwai877211f2005-11-17 13:59:38 +0100900 return snd_pcm_action(&snd_pcm_action_start, substream,
901 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
905 * stop callbacks
906 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100907static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Takashi Iwai877211f2005-11-17 13:59:38 +0100909 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
911 return -EBADFD;
912 runtime->trigger_master = substream;
913 return 0;
914}
915
Takashi Iwai877211f2005-11-17 13:59:38 +0100916static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 if (substream->runtime->trigger_master == substream &&
919 snd_pcm_running(substream))
920 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
921 return 0; /* unconditonally stop all substreams */
922}
923
Takashi Iwai877211f2005-11-17 13:59:38 +0100924static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Takashi Iwai877211f2005-11-17 13:59:38 +0100926 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (runtime->status->state != state) {
928 snd_pcm_trigger_tstamp(substream);
929 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100930 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
931 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 runtime->status->state = state;
933 snd_pcm_tick_set(substream, 0);
934 }
935 wake_up(&runtime->sleep);
936}
937
938static struct action_ops snd_pcm_action_stop = {
939 .pre_action = snd_pcm_pre_stop,
940 .do_action = snd_pcm_do_stop,
941 .post_action = snd_pcm_post_stop
942};
943
944/**
945 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200946 * @substream: the PCM substream instance
947 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
949 * Try to stop all running streams in the substream group.
950 * The state of each stream is changed to the given value after that unconditionally.
951 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100952int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
955}
956
Takashi Iwaie88e8ae62006-04-28 15:13:40 +0200957EXPORT_SYMBOL(snd_pcm_stop);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/**
960 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200961 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 *
963 * Stop the DMA only when the given stream is playback.
964 * The state is changed to SETUP.
965 * Unlike snd_pcm_stop(), this affects only the given stream.
966 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100967int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Takashi Iwai877211f2005-11-17 13:59:38 +0100969 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
970 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973/*
974 * pause callbacks
975 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100976static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Takashi Iwai877211f2005-11-17 13:59:38 +0100978 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
980 return -ENOSYS;
981 if (push) {
982 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
983 return -EBADFD;
984 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
985 return -EBADFD;
986 runtime->trigger_master = substream;
987 return 0;
988}
989
Takashi Iwai877211f2005-11-17 13:59:38 +0100990static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
992 if (substream->runtime->trigger_master != substream)
993 return 0;
994 return substream->ops->trigger(substream,
995 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
996 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
997}
998
Takashi Iwai877211f2005-11-17 13:59:38 +0100999static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 if (substream->runtime->trigger_master == substream)
1002 substream->ops->trigger(substream,
1003 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
1004 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
1005}
1006
Takashi Iwai877211f2005-11-17 13:59:38 +01001007static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Takashi Iwai877211f2005-11-17 13:59:38 +01001009 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 snd_pcm_trigger_tstamp(substream);
1011 if (push) {
1012 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
1013 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001014 snd_timer_notify(substream->timer,
1015 SNDRV_TIMER_EVENT_MPAUSE,
1016 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 snd_pcm_tick_set(substream, 0);
1018 wake_up(&runtime->sleep);
1019 } else {
1020 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1021 if (runtime->sleep_min)
1022 snd_pcm_tick_prepare(substream);
1023 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001024 snd_timer_notify(substream->timer,
1025 SNDRV_TIMER_EVENT_MCONTINUE,
1026 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028}
1029
1030static struct action_ops snd_pcm_action_pause = {
1031 .pre_action = snd_pcm_pre_pause,
1032 .do_action = snd_pcm_do_pause,
1033 .undo_action = snd_pcm_undo_pause,
1034 .post_action = snd_pcm_post_pause
1035};
1036
1037/*
1038 * Push/release the pause for all linked streams.
1039 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001040static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1043}
1044
1045#ifdef CONFIG_PM
1046/* suspend */
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Takashi Iwai877211f2005-11-17 13:59:38 +01001050 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1052 return -EBUSY;
1053 runtime->trigger_master = substream;
1054 return 0;
1055}
1056
Takashi Iwai877211f2005-11-17 13:59:38 +01001057static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Takashi Iwai877211f2005-11-17 13:59:38 +01001059 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (runtime->trigger_master != substream)
1061 return 0;
1062 if (! snd_pcm_running(substream))
1063 return 0;
1064 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1065 return 0; /* suspend unconditionally */
1066}
1067
Takashi Iwai877211f2005-11-17 13:59:38 +01001068static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Takashi Iwai877211f2005-11-17 13:59:38 +01001070 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 snd_pcm_trigger_tstamp(substream);
1072 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001073 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1074 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 runtime->status->suspended_state = runtime->status->state;
1076 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1077 snd_pcm_tick_set(substream, 0);
1078 wake_up(&runtime->sleep);
1079}
1080
1081static struct action_ops snd_pcm_action_suspend = {
1082 .pre_action = snd_pcm_pre_suspend,
1083 .do_action = snd_pcm_do_suspend,
1084 .post_action = snd_pcm_post_suspend
1085};
1086
1087/**
1088 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001089 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 *
1091 * Trigger SUSPEND to all linked streams.
1092 * After this call, all streams are changed to SUSPENDED state.
1093 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001094int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 int err;
1097 unsigned long flags;
1098
Takashi Iwai603bf522005-11-17 15:59:14 +01001099 if (! substream)
1100 return 0;
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 snd_pcm_stream_lock_irqsave(substream, flags);
1103 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1104 snd_pcm_stream_unlock_irqrestore(substream, flags);
1105 return err;
1106}
1107
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001108EXPORT_SYMBOL(snd_pcm_suspend);
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/**
1111 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001112 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
1114 * Trigger SUSPEND to all substreams in the given pcm.
1115 * After this call, all streams are changed to SUSPENDED state.
1116 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001117int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Takashi Iwai877211f2005-11-17 13:59:38 +01001119 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int stream, err = 0;
1121
Takashi Iwai603bf522005-11-17 15:59:14 +01001122 if (! pcm)
1123 return 0;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001126 for (substream = pcm->streams[stream].substream;
1127 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /* FIXME: the open/close code should lock this as well */
1129 if (substream->runtime == NULL)
1130 continue;
1131 err = snd_pcm_suspend(substream);
1132 if (err < 0 && err != -EBUSY)
1133 return err;
1134 }
1135 }
1136 return 0;
1137}
1138
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02001139EXPORT_SYMBOL(snd_pcm_suspend_all);
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141/* resume */
1142
Takashi Iwai877211f2005-11-17 13:59:38 +01001143static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
Takashi Iwai877211f2005-11-17 13:59:38 +01001145 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1147 return -ENOSYS;
1148 runtime->trigger_master = substream;
1149 return 0;
1150}
1151
Takashi Iwai877211f2005-11-17 13:59:38 +01001152static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Takashi Iwai877211f2005-11-17 13:59:38 +01001154 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (runtime->trigger_master != substream)
1156 return 0;
1157 /* DMA not running previously? */
1158 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1159 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1160 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1161 return 0;
1162 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1163}
1164
Takashi Iwai877211f2005-11-17 13:59:38 +01001165static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 if (substream->runtime->trigger_master == substream &&
1168 snd_pcm_running(substream))
1169 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1170}
1171
Takashi Iwai877211f2005-11-17 13:59:38 +01001172static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Takashi Iwai877211f2005-11-17 13:59:38 +01001174 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 snd_pcm_trigger_tstamp(substream);
1176 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001177 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1178 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 runtime->status->state = runtime->status->suspended_state;
1180 if (runtime->sleep_min)
1181 snd_pcm_tick_prepare(substream);
1182}
1183
1184static struct action_ops snd_pcm_action_resume = {
1185 .pre_action = snd_pcm_pre_resume,
1186 .do_action = snd_pcm_do_resume,
1187 .undo_action = snd_pcm_undo_resume,
1188 .post_action = snd_pcm_post_resume
1189};
1190
Takashi Iwai877211f2005-11-17 13:59:38 +01001191static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Takashi Iwai877211f2005-11-17 13:59:38 +01001193 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int res;
1195
1196 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001197 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1199 snd_power_unlock(card);
1200 return res;
1201}
1202
1203#else
1204
Takashi Iwai877211f2005-11-17 13:59:38 +01001205static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 return -ENOSYS;
1208}
1209
1210#endif /* CONFIG_PM */
1211
1212/*
1213 * xrun ioctl
1214 *
1215 * Change the RUNNING stream(s) to XRUN state.
1216 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001217static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Takashi Iwai877211f2005-11-17 13:59:38 +01001219 struct snd_card *card = substream->pcm->card;
1220 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int result;
1222
1223 snd_power_lock(card);
1224 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001225 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (result < 0)
1227 goto _unlock;
1228 }
1229
1230 snd_pcm_stream_lock_irq(substream);
1231 switch (runtime->status->state) {
1232 case SNDRV_PCM_STATE_XRUN:
1233 result = 0; /* already there */
1234 break;
1235 case SNDRV_PCM_STATE_RUNNING:
1236 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1237 break;
1238 default:
1239 result = -EBADFD;
1240 }
1241 snd_pcm_stream_unlock_irq(substream);
1242 _unlock:
1243 snd_power_unlock(card);
1244 return result;
1245}
1246
1247/*
1248 * reset ioctl
1249 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001250static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Takashi Iwai877211f2005-11-17 13:59:38 +01001252 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 switch (runtime->status->state) {
1254 case SNDRV_PCM_STATE_RUNNING:
1255 case SNDRV_PCM_STATE_PREPARED:
1256 case SNDRV_PCM_STATE_PAUSED:
1257 case SNDRV_PCM_STATE_SUSPENDED:
1258 return 0;
1259 default:
1260 return -EBADFD;
1261 }
1262}
1263
Takashi Iwai877211f2005-11-17 13:59:38 +01001264static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Takashi Iwai877211f2005-11-17 13:59:38 +01001266 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1268 if (err < 0)
1269 return err;
1270 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1271 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001272 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1273 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 runtime->silence_start = runtime->status->hw_ptr;
1275 runtime->silence_filled = 0;
1276 return 0;
1277}
1278
Takashi Iwai877211f2005-11-17 13:59:38 +01001279static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Takashi Iwai877211f2005-11-17 13:59:38 +01001281 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 runtime->control->appl_ptr = runtime->status->hw_ptr;
1283 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1284 runtime->silence_size > 0)
1285 snd_pcm_playback_silence(substream, ULONG_MAX);
1286}
1287
1288static struct action_ops snd_pcm_action_reset = {
1289 .pre_action = snd_pcm_pre_reset,
1290 .do_action = snd_pcm_do_reset,
1291 .post_action = snd_pcm_post_reset
1292};
1293
Takashi Iwai877211f2005-11-17 13:59:38 +01001294static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1297}
1298
1299/*
1300 * prepare ioctl
1301 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001302/* we use the second argument for updating f_flags */
1303static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream,
1304 int f_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Takashi Iwai877211f2005-11-17 13:59:38 +01001306 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001307 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1308 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -EBADFD;
1310 if (snd_pcm_running(substream))
1311 return -EBUSY;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001312 substream->f_flags = f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
1314}
1315
Takashi Iwai877211f2005-11-17 13:59:38 +01001316static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 int err;
1319 err = substream->ops->prepare(substream);
1320 if (err < 0)
1321 return err;
1322 return snd_pcm_do_reset(substream, 0);
1323}
1324
Takashi Iwai877211f2005-11-17 13:59:38 +01001325static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Takashi Iwai877211f2005-11-17 13:59:38 +01001327 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 runtime->control->appl_ptr = runtime->status->hw_ptr;
1329 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1330}
1331
1332static struct action_ops snd_pcm_action_prepare = {
1333 .pre_action = snd_pcm_pre_prepare,
1334 .do_action = snd_pcm_do_prepare,
1335 .post_action = snd_pcm_post_prepare
1336};
1337
1338/**
1339 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001340 * @substream: the PCM substream instance
Takashi Iwai0df63e42006-04-28 15:13:41 +02001341 * @file: file to refer f_flags
Takashi Iwaidf8db932005-09-07 13:38:19 +02001342 *
1343 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Takashi Iwai0df63e42006-04-28 15:13:41 +02001345static int snd_pcm_prepare(struct snd_pcm_substream *substream,
1346 struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001349 struct snd_card *card = substream->pcm->card;
Takashi Iwai0df63e42006-04-28 15:13:41 +02001350 int f_flags;
1351
1352 if (file)
1353 f_flags = file->f_flags;
1354 else
1355 f_flags = substream->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001358 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Takashi Iwai0df63e42006-04-28 15:13:41 +02001359 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare,
1360 substream, f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 snd_power_unlock(card);
1362 return res;
1363}
1364
1365/*
1366 * drain ioctl
1367 */
1368
Takashi Iwai877211f2005-11-17 13:59:38 +01001369static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Takashi Iwai0df63e42006-04-28 15:13:41 +02001371 if (substream->f_flags & O_NONBLOCK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EAGAIN;
1373 substream->runtime->trigger_master = substream;
1374 return 0;
1375}
1376
Takashi Iwai877211f2005-11-17 13:59:38 +01001377static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Takashi Iwai877211f2005-11-17 13:59:38 +01001379 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1381 switch (runtime->status->state) {
1382 case SNDRV_PCM_STATE_PREPARED:
1383 /* start playback stream if possible */
1384 if (! snd_pcm_playback_empty(substream)) {
1385 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1386 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1387 }
1388 break;
1389 case SNDRV_PCM_STATE_RUNNING:
1390 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1391 break;
1392 default:
1393 break;
1394 }
1395 } else {
1396 /* stop running stream */
1397 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1398 int state = snd_pcm_capture_avail(runtime) > 0 ?
1399 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1400 snd_pcm_do_stop(substream, state);
1401 snd_pcm_post_stop(substream, state);
1402 }
1403 }
1404 return 0;
1405}
1406
Takashi Iwai877211f2005-11-17 13:59:38 +01001407static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409}
1410
1411static struct action_ops snd_pcm_action_drain_init = {
1412 .pre_action = snd_pcm_pre_drain_init,
1413 .do_action = snd_pcm_do_drain_init,
1414 .post_action = snd_pcm_post_drain_init
1415};
1416
1417struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001418 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 wait_queue_t wait;
1420 snd_pcm_uframes_t stop_threshold;
1421};
1422
Takashi Iwai877211f2005-11-17 13:59:38 +01001423static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425/*
1426 * Drain the stream(s).
1427 * When the substream is linked, sync until the draining of all playback streams
1428 * is finished.
1429 * After this call, all streams are supposed to be either SETUP or DRAINING
1430 * (capture only) state.
1431 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001432static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Takashi Iwai877211f2005-11-17 13:59:38 +01001434 struct snd_card *card;
1435 struct snd_pcm_runtime *runtime;
Takashi Iwaief991b92007-02-22 12:52:53 +01001436 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int result = 0;
1438 int i, num_drecs;
1439 struct drain_rec *drec, drec_tmp, *d;
1440
1441 snd_assert(substream != NULL, return -ENXIO);
1442 card = substream->pcm->card;
1443 runtime = substream->runtime;
1444
1445 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1446 return -EBADFD;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 snd_power_lock(card);
1449 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001450 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001451 if (result < 0) {
1452 snd_power_unlock(card);
1453 return result;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
1457 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001458 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (snd_pcm_stream_linked(substream)) {
1460 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1461 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001462 up_read(&snd_pcm_link_rwsem);
1463 snd_power_unlock(card);
1464 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 } else
1467 drec = &drec_tmp;
1468
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001469 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 num_drecs = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +01001471 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1474 d = &drec[num_drecs++];
1475 d->substream = s;
1476 init_waitqueue_entry(&d->wait, current);
1477 add_wait_queue(&runtime->sleep, &d->wait);
1478 /* stop_threshold fixup to avoid endless loop when
1479 * stop_threshold > buffer_size
1480 */
1481 d->stop_threshold = runtime->stop_threshold;
1482 if (runtime->stop_threshold > runtime->buffer_size)
1483 runtime->stop_threshold = runtime->buffer_size;
1484 }
1485 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001486 up_read(&snd_pcm_link_rwsem);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001487
1488 snd_pcm_stream_lock_irq(substream);
1489 /* resume pause */
1490 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1491 snd_pcm_pause(substream, 0);
1492
1493 /* pre-start/stop - all running streams are changed to DRAINING state */
1494 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1495 if (result < 0) {
1496 snd_pcm_stream_unlock_irq(substream);
1497 goto _error;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 for (;;) {
1501 long tout;
1502 if (signal_pending(current)) {
1503 result = -ERESTARTSYS;
1504 break;
1505 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001506 /* all finished? */
1507 for (i = 0; i < num_drecs; i++) {
1508 runtime = drec[i].substream->runtime;
1509 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1510 break;
1511 }
1512 if (i == num_drecs)
1513 break; /* yes, all drained */
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 set_current_state(TASK_INTERRUPTIBLE);
1516 snd_pcm_stream_unlock_irq(substream);
1517 snd_power_unlock(card);
1518 tout = schedule_timeout(10 * HZ);
1519 snd_power_lock(card);
1520 snd_pcm_stream_lock_irq(substream);
1521 if (tout == 0) {
1522 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1523 result = -ESTRPIPE;
1524 else {
1525 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1526 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1527 result = -EIO;
1528 }
1529 break;
1530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001532
1533 snd_pcm_stream_unlock_irq(substream);
1534
1535 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 for (i = 0; i < num_drecs; i++) {
1537 d = &drec[i];
1538 runtime = d->substream->runtime;
1539 remove_wait_queue(&runtime->sleep, &d->wait);
1540 runtime->stop_threshold = d->stop_threshold;
1541 }
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (drec != &drec_tmp)
1544 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 return result;
1548}
1549
1550/*
1551 * drop ioctl
1552 *
1553 * Immediately put all linked substreams into SETUP state.
1554 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001555static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Takashi Iwai877211f2005-11-17 13:59:38 +01001557 struct snd_pcm_runtime *runtime;
1558 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 int result = 0;
1560
1561 snd_assert(substream != NULL, return -ENXIO);
1562 runtime = substream->runtime;
1563 card = substream->pcm->card;
1564
Takashi Iwaide1b8b92006-11-08 15:41:29 +01001565 if (runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1566 runtime->status->state == SNDRV_PCM_STATE_DISCONNECTED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EBADFD;
1568
1569 snd_power_lock(card);
1570 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001571 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (result < 0)
1573 goto _unlock;
1574 }
1575
1576 snd_pcm_stream_lock_irq(substream);
1577 /* resume pause */
1578 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1579 snd_pcm_pause(substream, 0);
1580
1581 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1582 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1583 snd_pcm_stream_unlock_irq(substream);
1584 _unlock:
1585 snd_power_unlock(card);
1586 return result;
1587}
1588
1589
1590/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591static struct file *snd_pcm_file_fd(int fd)
1592{
1593 struct file *file;
1594 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001595 unsigned int minor;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 file = fget(fd);
1598 if (!file)
1599 return NULL;
Josef Sipek7bc56322006-12-08 02:37:40 -08001600 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!S_ISCHR(inode->i_mode) ||
1602 imajor(inode) != snd_major) {
1603 fput(file);
1604 return NULL;
1605 }
1606 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001607 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1608 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 fput(file);
1610 return NULL;
1611 }
1612 return file;
1613}
1614
1615/*
1616 * PCM link handling
1617 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001618static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 int res = 0;
1621 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001622 struct snd_pcm_file *pcm_file;
1623 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 file = snd_pcm_file_fd(fd);
1626 if (!file)
1627 return -EBADFD;
1628 pcm_file = file->private_data;
1629 substream1 = pcm_file->substream;
1630 down_write(&snd_pcm_link_rwsem);
1631 write_lock_irq(&snd_pcm_link_rwlock);
1632 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1633 substream->runtime->status->state != substream1->runtime->status->state) {
1634 res = -EBADFD;
1635 goto _end;
1636 }
1637 if (snd_pcm_stream_linked(substream1)) {
1638 res = -EALREADY;
1639 goto _end;
1640 }
1641 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001642 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (substream->group == NULL) {
1644 res = -ENOMEM;
1645 goto _end;
1646 }
1647 spin_lock_init(&substream->group->lock);
1648 INIT_LIST_HEAD(&substream->group->substreams);
1649 list_add_tail(&substream->link_list, &substream->group->substreams);
1650 substream->group->count = 1;
1651 }
1652 list_add_tail(&substream1->link_list, &substream->group->substreams);
1653 substream->group->count++;
1654 substream1->group = substream->group;
1655 _end:
1656 write_unlock_irq(&snd_pcm_link_rwlock);
1657 up_write(&snd_pcm_link_rwsem);
1658 fput(file);
1659 return res;
1660}
1661
Takashi Iwai877211f2005-11-17 13:59:38 +01001662static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 substream->group = &substream->self_group;
1665 INIT_LIST_HEAD(&substream->self_group.substreams);
1666 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1667}
1668
Takashi Iwai877211f2005-11-17 13:59:38 +01001669static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Takashi Iwaief991b92007-02-22 12:52:53 +01001671 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int res = 0;
1673
1674 down_write(&snd_pcm_link_rwsem);
1675 write_lock_irq(&snd_pcm_link_rwlock);
1676 if (!snd_pcm_stream_linked(substream)) {
1677 res = -EALREADY;
1678 goto _end;
1679 }
1680 list_del(&substream->link_list);
1681 substream->group->count--;
1682 if (substream->group->count == 1) { /* detach the last stream, too */
Takashi Iwaief991b92007-02-22 12:52:53 +01001683 snd_pcm_group_for_each_entry(s, substream) {
1684 relink_to_local(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686 }
1687 kfree(substream->group);
1688 }
1689 relink_to_local(substream);
1690 _end:
1691 write_unlock_irq(&snd_pcm_link_rwlock);
1692 up_write(&snd_pcm_link_rwsem);
1693 return res;
1694}
1695
1696/*
1697 * hw configurator
1698 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001699static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1700 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Takashi Iwai877211f2005-11-17 13:59:38 +01001702 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1704 hw_param_interval_c(params, rule->deps[1]), &t);
1705 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1706}
1707
Takashi Iwai877211f2005-11-17 13:59:38 +01001708static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1709 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Takashi Iwai877211f2005-11-17 13:59:38 +01001711 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1713 hw_param_interval_c(params, rule->deps[1]), &t);
1714 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1715}
1716
Takashi Iwai877211f2005-11-17 13:59:38 +01001717static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1718 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
Takashi Iwai877211f2005-11-17 13:59:38 +01001720 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1722 hw_param_interval_c(params, rule->deps[1]),
1723 (unsigned long) rule->private, &t);
1724 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1725}
1726
Takashi Iwai877211f2005-11-17 13:59:38 +01001727static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1728 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Takashi Iwai877211f2005-11-17 13:59:38 +01001730 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1732 (unsigned long) rule->private,
1733 hw_param_interval_c(params, rule->deps[1]), &t);
1734 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1735}
1736
Takashi Iwai877211f2005-11-17 13:59:38 +01001737static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1738 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
1740 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001741 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1742 struct snd_mask m;
1743 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 snd_mask_any(&m);
1745 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1746 int bits;
1747 if (! snd_mask_test(mask, k))
1748 continue;
1749 bits = snd_pcm_format_physical_width(k);
1750 if (bits <= 0)
1751 continue; /* ignore invalid formats */
1752 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1753 snd_mask_reset(&m, k);
1754 }
1755 return snd_mask_refine(mask, &m);
1756}
1757
Takashi Iwai877211f2005-11-17 13:59:38 +01001758static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1759 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Takashi Iwai877211f2005-11-17 13:59:38 +01001761 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned int k;
1763 t.min = UINT_MAX;
1764 t.max = 0;
1765 t.openmin = 0;
1766 t.openmax = 0;
1767 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1768 int bits;
1769 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1770 continue;
1771 bits = snd_pcm_format_physical_width(k);
1772 if (bits <= 0)
1773 continue; /* ignore invalid formats */
1774 if (t.min > (unsigned)bits)
1775 t.min = bits;
1776 if (t.max < (unsigned)bits)
1777 t.max = bits;
1778 }
1779 t.integer = 1;
1780 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1781}
1782
1783#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1784#error "Change this table"
1785#endif
1786
1787static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1788 48000, 64000, 88200, 96000, 176400, 192000 };
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),
1795 ARRAY_SIZE(rates), rates, hw->rates);
1796}
1797
Takashi Iwai877211f2005-11-17 13:59:38 +01001798static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1799 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Takashi Iwai877211f2005-11-17 13:59:38 +01001801 struct snd_interval t;
1802 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 t.min = 0;
1804 t.max = substream->buffer_bytes_max;
1805 t.openmin = 0;
1806 t.openmax = 0;
1807 t.integer = 1;
1808 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1809}
1810
Takashi Iwai877211f2005-11-17 13:59:38 +01001811int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Takashi Iwai877211f2005-11-17 13:59:38 +01001813 struct snd_pcm_runtime *runtime = substream->runtime;
1814 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int k, err;
1816
1817 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1818 snd_mask_any(constrs_mask(constrs, k));
1819 }
1820
1821 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1822 snd_interval_any(constrs_interval(constrs, k));
1823 }
1824
1825 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1826 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1827 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1828 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1829 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1830
1831 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1832 snd_pcm_hw_rule_format, NULL,
1833 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1834 if (err < 0)
1835 return err;
1836 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1837 snd_pcm_hw_rule_sample_bits, NULL,
1838 SNDRV_PCM_HW_PARAM_FORMAT,
1839 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1840 if (err < 0)
1841 return err;
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1843 snd_pcm_hw_rule_div, NULL,
1844 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1845 if (err < 0)
1846 return err;
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1848 snd_pcm_hw_rule_mul, NULL,
1849 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1850 if (err < 0)
1851 return err;
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1853 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1854 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1855 if (err < 0)
1856 return err;
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1859 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1860 if (err < 0)
1861 return err;
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1863 snd_pcm_hw_rule_div, NULL,
1864 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1865 if (err < 0)
1866 return err;
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1868 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1869 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1870 if (err < 0)
1871 return err;
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1873 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1875 if (err < 0)
1876 return err;
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1878 snd_pcm_hw_rule_div, NULL,
1879 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1880 if (err < 0)
1881 return err;
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1883 snd_pcm_hw_rule_div, NULL,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1885 if (err < 0)
1886 return err;
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1888 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1889 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1890 if (err < 0)
1891 return err;
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1893 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1894 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1895 if (err < 0)
1896 return err;
1897 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1898 snd_pcm_hw_rule_mul, NULL,
1899 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1900 if (err < 0)
1901 return err;
1902 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1903 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1904 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1905 if (err < 0)
1906 return err;
1907 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1908 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1909 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1910 if (err < 0)
1911 return err;
1912 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1913 snd_pcm_hw_rule_muldivk, (void*) 8,
1914 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1915 if (err < 0)
1916 return err;
1917 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1918 snd_pcm_hw_rule_muldivk, (void*) 8,
1919 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1920 if (err < 0)
1921 return err;
1922 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1923 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1924 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1925 if (err < 0)
1926 return err;
1927 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1928 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1929 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1930 if (err < 0)
1931 return err;
1932 return 0;
1933}
1934
Takashi Iwai877211f2005-11-17 13:59:38 +01001935int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Takashi Iwai877211f2005-11-17 13:59:38 +01001937 struct snd_pcm_runtime *runtime = substream->runtime;
1938 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 int err;
1940 unsigned int mask = 0;
1941
1942 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1943 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1944 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1945 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1946 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1947 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1948 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1949 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1950 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1951 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1952 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1953 }
1954 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1955 snd_assert(err >= 0, return -EINVAL);
1956
1957 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1958 snd_assert(err >= 0, return -EINVAL);
1959
1960 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1961 snd_assert(err >= 0, return -EINVAL);
1962
1963 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1964 hw->channels_min, hw->channels_max);
1965 snd_assert(err >= 0, return -EINVAL);
1966
1967 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1968 hw->rate_min, hw->rate_max);
1969 snd_assert(err >= 0, return -EINVAL);
1970
1971 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1972 hw->period_bytes_min, hw->period_bytes_max);
1973 snd_assert(err >= 0, return -EINVAL);
1974
1975 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1976 hw->periods_min, hw->periods_max);
1977 snd_assert(err >= 0, return -EINVAL);
1978
1979 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1980 hw->period_bytes_min, hw->buffer_bytes_max);
1981 snd_assert(err >= 0, return -EINVAL);
1982
1983 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1984 snd_pcm_hw_rule_buffer_bytes_max, substream,
1985 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1986 if (err < 0)
1987 return err;
1988
1989 /* FIXME: remove */
1990 if (runtime->dma_bytes) {
1991 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1992 snd_assert(err >= 0, return -EINVAL);
1993 }
1994
1995 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1996 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1997 snd_pcm_hw_rule_rate, hw,
1998 SNDRV_PCM_HW_PARAM_RATE, -1);
1999 if (err < 0)
2000 return err;
2001 }
2002
2003 /* FIXME: this belong to lowlevel */
2004 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
2005 1000000 / HZ, 1000000 / HZ);
2006 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
2007
2008 return 0;
2009}
2010
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002011static void pcm_release_private(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 snd_pcm_unlink(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002014}
2015
2016void snd_pcm_release_substream(struct snd_pcm_substream *substream)
2017{
Takashi Iwai0df63e42006-04-28 15:13:41 +02002018 substream->ref_count--;
2019 if (substream->ref_count > 0)
2020 return;
2021
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002022 snd_pcm_drop(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002023 if (substream->hw_opened) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (substream->ops->hw_free != NULL)
2025 substream->ops->hw_free(substream);
2026 substream->ops->close(substream);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002027 substream->hw_opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 }
Takashi Iwai15762742006-04-06 19:47:42 +02002029 if (substream->pcm_release) {
2030 substream->pcm_release(substream);
2031 substream->pcm_release = NULL;
2032 }
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002033 snd_pcm_detach_substream(substream);
2034}
2035
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002036EXPORT_SYMBOL(snd_pcm_release_substream);
2037
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002038int snd_pcm_open_substream(struct snd_pcm *pcm, int stream,
2039 struct file *file,
2040 struct snd_pcm_substream **rsubstream)
2041{
2042 struct snd_pcm_substream *substream;
2043 int err;
2044
2045 err = snd_pcm_attach_substream(pcm, stream, file, &substream);
2046 if (err < 0)
2047 return err;
Takashi Iwai0df63e42006-04-28 15:13:41 +02002048 if (substream->ref_count > 1) {
2049 *rsubstream = substream;
2050 return 0;
2051 }
2052
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002053 err = snd_pcm_hw_constraints_init(substream);
2054 if (err < 0) {
2055 snd_printd("snd_pcm_hw_constraints_init failed\n");
2056 goto error;
2057 }
2058
2059 if ((err = substream->ops->open(substream)) < 0)
2060 goto error;
2061
2062 substream->hw_opened = 1;
2063
2064 err = snd_pcm_hw_constraints_complete(substream);
2065 if (err < 0) {
2066 snd_printd("snd_pcm_hw_constraints_complete failed\n");
2067 goto error;
2068 }
2069
2070 *rsubstream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002072
2073 error:
2074 snd_pcm_release_substream(substream);
2075 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002078EXPORT_SYMBOL(snd_pcm_open_substream);
2079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002081 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002083 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
Takashi Iwai877211f2005-11-17 13:59:38 +01002085 struct snd_pcm_file *pcm_file;
2086 struct snd_pcm_substream *substream;
2087 struct snd_pcm_str *str;
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002088 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 snd_assert(rpcm_file != NULL, return -EINVAL);
2091 *rpcm_file = NULL;
2092
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002093 err = snd_pcm_open_substream(pcm, stream, file, &substream);
2094 if (err < 0)
2095 return err;
2096
Takashi Iwai548a6482006-07-31 16:51:51 +02002097 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
2098 if (pcm_file == NULL) {
2099 snd_pcm_release_substream(substream);
2100 return -ENOMEM;
2101 }
2102 pcm_file->substream = substream;
2103 if (substream->ref_count == 1) {
Takashi Iwai0df63e42006-04-28 15:13:41 +02002104 str = substream->pstr;
2105 substream->file = pcm_file;
2106 substream->pcm_release = pcm_release_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 file->private_data = pcm_file;
2109 *rpcm_file = pcm_file;
2110 return 0;
2111}
2112
Clemens Ladischf87135f2005-11-20 14:06:59 +01002113static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Takashi Iwai877211f2005-11-17 13:59:38 +01002115 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002116
2117 pcm = snd_lookup_minor_data(iminor(inode),
2118 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2119 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2120}
2121
2122static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2123{
2124 struct snd_pcm *pcm;
2125
2126 pcm = snd_lookup_minor_data(iminor(inode),
2127 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2128 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2129}
2130
2131static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2132{
2133 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002134 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 wait_queue_t wait;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (pcm == NULL) {
2138 err = -ENODEV;
2139 goto __error1;
2140 }
2141 err = snd_card_file_add(pcm->card, file);
2142 if (err < 0)
2143 goto __error1;
2144 if (!try_module_get(pcm->card->module)) {
2145 err = -EFAULT;
2146 goto __error2;
2147 }
2148 init_waitqueue_entry(&wait, current);
2149 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002150 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002152 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (err >= 0)
2154 break;
2155 if (err == -EAGAIN) {
2156 if (file->f_flags & O_NONBLOCK) {
2157 err = -EBUSY;
2158 break;
2159 }
2160 } else
2161 break;
2162 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002163 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002165 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (signal_pending(current)) {
2167 err = -ERESTARTSYS;
2168 break;
2169 }
2170 }
2171 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002172 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (err < 0)
2174 goto __error;
2175 return err;
2176
2177 __error:
2178 module_put(pcm->card->module);
2179 __error2:
2180 snd_card_file_remove(pcm->card, file);
2181 __error1:
2182 return err;
2183}
2184
2185static int snd_pcm_release(struct inode *inode, struct file *file)
2186{
Takashi Iwai877211f2005-11-17 13:59:38 +01002187 struct snd_pcm *pcm;
2188 struct snd_pcm_substream *substream;
2189 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 pcm_file = file->private_data;
2192 substream = pcm_file->substream;
2193 snd_assert(substream != NULL, return -ENXIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 pcm = substream->pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002196 mutex_lock(&pcm->open_mutex);
Takashi Iwai3bf75f92006-03-27 16:40:49 +02002197 snd_pcm_release_substream(substream);
Takashi Iwai548a6482006-07-31 16:51:51 +02002198 kfree(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002199 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 wake_up(&pcm->open_wait);
2201 module_put(pcm->card->module);
2202 snd_card_file_remove(pcm->card, file);
2203 return 0;
2204}
2205
Takashi Iwai877211f2005-11-17 13:59:38 +01002206static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2207 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Takashi Iwai877211f2005-11-17 13:59:38 +01002209 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 snd_pcm_sframes_t appl_ptr;
2211 snd_pcm_sframes_t ret;
2212 snd_pcm_sframes_t hw_avail;
2213
2214 if (frames == 0)
2215 return 0;
2216
2217 snd_pcm_stream_lock_irq(substream);
2218 switch (runtime->status->state) {
2219 case SNDRV_PCM_STATE_PREPARED:
2220 break;
2221 case SNDRV_PCM_STATE_DRAINING:
2222 case SNDRV_PCM_STATE_RUNNING:
2223 if (snd_pcm_update_hw_ptr(substream) >= 0)
2224 break;
2225 /* Fall through */
2226 case SNDRV_PCM_STATE_XRUN:
2227 ret = -EPIPE;
2228 goto __end;
2229 default:
2230 ret = -EBADFD;
2231 goto __end;
2232 }
2233
2234 hw_avail = snd_pcm_playback_hw_avail(runtime);
2235 if (hw_avail <= 0) {
2236 ret = 0;
2237 goto __end;
2238 }
2239 if (frames > (snd_pcm_uframes_t)hw_avail)
2240 frames = hw_avail;
2241 else
2242 frames -= frames % runtime->xfer_align;
2243 appl_ptr = runtime->control->appl_ptr - frames;
2244 if (appl_ptr < 0)
2245 appl_ptr += runtime->boundary;
2246 runtime->control->appl_ptr = appl_ptr;
2247 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2248 runtime->sleep_min)
2249 snd_pcm_tick_prepare(substream);
2250 ret = frames;
2251 __end:
2252 snd_pcm_stream_unlock_irq(substream);
2253 return ret;
2254}
2255
Takashi Iwai877211f2005-11-17 13:59:38 +01002256static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2257 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Takashi Iwai877211f2005-11-17 13:59:38 +01002259 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 snd_pcm_sframes_t appl_ptr;
2261 snd_pcm_sframes_t ret;
2262 snd_pcm_sframes_t hw_avail;
2263
2264 if (frames == 0)
2265 return 0;
2266
2267 snd_pcm_stream_lock_irq(substream);
2268 switch (runtime->status->state) {
2269 case SNDRV_PCM_STATE_PREPARED:
2270 case SNDRV_PCM_STATE_DRAINING:
2271 break;
2272 case SNDRV_PCM_STATE_RUNNING:
2273 if (snd_pcm_update_hw_ptr(substream) >= 0)
2274 break;
2275 /* Fall through */
2276 case SNDRV_PCM_STATE_XRUN:
2277 ret = -EPIPE;
2278 goto __end;
2279 default:
2280 ret = -EBADFD;
2281 goto __end;
2282 }
2283
2284 hw_avail = snd_pcm_capture_hw_avail(runtime);
2285 if (hw_avail <= 0) {
2286 ret = 0;
2287 goto __end;
2288 }
2289 if (frames > (snd_pcm_uframes_t)hw_avail)
2290 frames = hw_avail;
2291 else
2292 frames -= frames % runtime->xfer_align;
2293 appl_ptr = runtime->control->appl_ptr - frames;
2294 if (appl_ptr < 0)
2295 appl_ptr += runtime->boundary;
2296 runtime->control->appl_ptr = appl_ptr;
2297 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2298 runtime->sleep_min)
2299 snd_pcm_tick_prepare(substream);
2300 ret = frames;
2301 __end:
2302 snd_pcm_stream_unlock_irq(substream);
2303 return ret;
2304}
2305
Takashi Iwai877211f2005-11-17 13:59:38 +01002306static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2307 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Takashi Iwai877211f2005-11-17 13:59:38 +01002309 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 snd_pcm_sframes_t appl_ptr;
2311 snd_pcm_sframes_t ret;
2312 snd_pcm_sframes_t avail;
2313
2314 if (frames == 0)
2315 return 0;
2316
2317 snd_pcm_stream_lock_irq(substream);
2318 switch (runtime->status->state) {
2319 case SNDRV_PCM_STATE_PREPARED:
2320 case SNDRV_PCM_STATE_PAUSED:
2321 break;
2322 case SNDRV_PCM_STATE_DRAINING:
2323 case SNDRV_PCM_STATE_RUNNING:
2324 if (snd_pcm_update_hw_ptr(substream) >= 0)
2325 break;
2326 /* Fall through */
2327 case SNDRV_PCM_STATE_XRUN:
2328 ret = -EPIPE;
2329 goto __end;
2330 default:
2331 ret = -EBADFD;
2332 goto __end;
2333 }
2334
2335 avail = snd_pcm_playback_avail(runtime);
2336 if (avail <= 0) {
2337 ret = 0;
2338 goto __end;
2339 }
2340 if (frames > (snd_pcm_uframes_t)avail)
2341 frames = avail;
2342 else
2343 frames -= frames % runtime->xfer_align;
2344 appl_ptr = runtime->control->appl_ptr + frames;
2345 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2346 appl_ptr -= runtime->boundary;
2347 runtime->control->appl_ptr = appl_ptr;
2348 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2349 runtime->sleep_min)
2350 snd_pcm_tick_prepare(substream);
2351 ret = frames;
2352 __end:
2353 snd_pcm_stream_unlock_irq(substream);
2354 return ret;
2355}
2356
Takashi Iwai877211f2005-11-17 13:59:38 +01002357static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2358 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Takashi Iwai877211f2005-11-17 13:59:38 +01002360 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 snd_pcm_sframes_t appl_ptr;
2362 snd_pcm_sframes_t ret;
2363 snd_pcm_sframes_t avail;
2364
2365 if (frames == 0)
2366 return 0;
2367
2368 snd_pcm_stream_lock_irq(substream);
2369 switch (runtime->status->state) {
2370 case SNDRV_PCM_STATE_PREPARED:
2371 case SNDRV_PCM_STATE_DRAINING:
2372 case SNDRV_PCM_STATE_PAUSED:
2373 break;
2374 case SNDRV_PCM_STATE_RUNNING:
2375 if (snd_pcm_update_hw_ptr(substream) >= 0)
2376 break;
2377 /* Fall through */
2378 case SNDRV_PCM_STATE_XRUN:
2379 ret = -EPIPE;
2380 goto __end;
2381 default:
2382 ret = -EBADFD;
2383 goto __end;
2384 }
2385
2386 avail = snd_pcm_capture_avail(runtime);
2387 if (avail <= 0) {
2388 ret = 0;
2389 goto __end;
2390 }
2391 if (frames > (snd_pcm_uframes_t)avail)
2392 frames = avail;
2393 else
2394 frames -= frames % runtime->xfer_align;
2395 appl_ptr = runtime->control->appl_ptr + frames;
2396 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2397 appl_ptr -= runtime->boundary;
2398 runtime->control->appl_ptr = appl_ptr;
2399 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2400 runtime->sleep_min)
2401 snd_pcm_tick_prepare(substream);
2402 ret = frames;
2403 __end:
2404 snd_pcm_stream_unlock_irq(substream);
2405 return ret;
2406}
2407
Takashi Iwai877211f2005-11-17 13:59:38 +01002408static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Takashi Iwai877211f2005-11-17 13:59:38 +01002410 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 int err;
2412
2413 snd_pcm_stream_lock_irq(substream);
2414 switch (runtime->status->state) {
2415 case SNDRV_PCM_STATE_DRAINING:
2416 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2417 goto __badfd;
2418 case SNDRV_PCM_STATE_RUNNING:
2419 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2420 break;
2421 /* Fall through */
2422 case SNDRV_PCM_STATE_PREPARED:
2423 case SNDRV_PCM_STATE_SUSPENDED:
2424 err = 0;
2425 break;
2426 case SNDRV_PCM_STATE_XRUN:
2427 err = -EPIPE;
2428 break;
2429 default:
2430 __badfd:
2431 err = -EBADFD;
2432 break;
2433 }
2434 snd_pcm_stream_unlock_irq(substream);
2435 return err;
2436}
2437
Takashi Iwai877211f2005-11-17 13:59:38 +01002438static int snd_pcm_delay(struct snd_pcm_substream *substream,
2439 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Takashi Iwai877211f2005-11-17 13:59:38 +01002441 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 int err;
2443 snd_pcm_sframes_t n = 0;
2444
2445 snd_pcm_stream_lock_irq(substream);
2446 switch (runtime->status->state) {
2447 case SNDRV_PCM_STATE_DRAINING:
2448 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2449 goto __badfd;
2450 case SNDRV_PCM_STATE_RUNNING:
2451 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2452 break;
2453 /* Fall through */
2454 case SNDRV_PCM_STATE_PREPARED:
2455 case SNDRV_PCM_STATE_SUSPENDED:
2456 err = 0;
2457 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2458 n = snd_pcm_playback_hw_avail(runtime);
2459 else
2460 n = snd_pcm_capture_avail(runtime);
2461 break;
2462 case SNDRV_PCM_STATE_XRUN:
2463 err = -EPIPE;
2464 break;
2465 default:
2466 __badfd:
2467 err = -EBADFD;
2468 break;
2469 }
2470 snd_pcm_stream_unlock_irq(substream);
2471 if (!err)
2472 if (put_user(n, res))
2473 err = -EFAULT;
2474 return err;
2475}
2476
Takashi Iwai877211f2005-11-17 13:59:38 +01002477static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2478 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Takashi Iwai877211f2005-11-17 13:59:38 +01002480 struct snd_pcm_runtime *runtime = substream->runtime;
2481 struct snd_pcm_sync_ptr sync_ptr;
2482 volatile struct snd_pcm_mmap_status *status;
2483 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 int err;
2485
2486 memset(&sync_ptr, 0, sizeof(sync_ptr));
2487 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2488 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002489 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 -07002490 return -EFAULT;
2491 status = runtime->status;
2492 control = runtime->control;
2493 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2494 err = snd_pcm_hwsync(substream);
2495 if (err < 0)
2496 return err;
2497 }
2498 snd_pcm_stream_lock_irq(substream);
2499 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2500 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2501 else
2502 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2503 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2504 control->avail_min = sync_ptr.c.control.avail_min;
2505 else
2506 sync_ptr.c.control.avail_min = control->avail_min;
2507 sync_ptr.s.status.state = status->state;
2508 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2509 sync_ptr.s.status.tstamp = status->tstamp;
2510 sync_ptr.s.status.suspended_state = status->suspended_state;
2511 snd_pcm_stream_unlock_irq(substream);
2512 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2513 return -EFAULT;
2514 return 0;
2515}
2516
Takashi Iwai0df63e42006-04-28 15:13:41 +02002517static int snd_pcm_common_ioctl1(struct file *file,
2518 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 unsigned int cmd, void __user *arg)
2520{
2521 snd_assert(substream != NULL, return -ENXIO);
2522
2523 switch (cmd) {
2524 case SNDRV_PCM_IOCTL_PVERSION:
2525 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2526 case SNDRV_PCM_IOCTL_INFO:
2527 return snd_pcm_info_user(substream, arg);
Takashi Iwai07799e72005-10-10 11:49:49 +02002528 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 case SNDRV_PCM_IOCTL_HW_REFINE:
2531 return snd_pcm_hw_refine_user(substream, arg);
2532 case SNDRV_PCM_IOCTL_HW_PARAMS:
2533 return snd_pcm_hw_params_user(substream, arg);
2534 case SNDRV_PCM_IOCTL_HW_FREE:
2535 return snd_pcm_hw_free(substream);
2536 case SNDRV_PCM_IOCTL_SW_PARAMS:
2537 return snd_pcm_sw_params_user(substream, arg);
2538 case SNDRV_PCM_IOCTL_STATUS:
2539 return snd_pcm_status_user(substream, arg);
2540 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2541 return snd_pcm_channel_info_user(substream, arg);
2542 case SNDRV_PCM_IOCTL_PREPARE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002543 return snd_pcm_prepare(substream, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 case SNDRV_PCM_IOCTL_RESET:
2545 return snd_pcm_reset(substream);
2546 case SNDRV_PCM_IOCTL_START:
2547 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2548 case SNDRV_PCM_IOCTL_LINK:
2549 return snd_pcm_link(substream, (int)(unsigned long) arg);
2550 case SNDRV_PCM_IOCTL_UNLINK:
2551 return snd_pcm_unlink(substream);
2552 case SNDRV_PCM_IOCTL_RESUME:
2553 return snd_pcm_resume(substream);
2554 case SNDRV_PCM_IOCTL_XRUN:
2555 return snd_pcm_xrun(substream);
2556 case SNDRV_PCM_IOCTL_HWSYNC:
2557 return snd_pcm_hwsync(substream);
2558 case SNDRV_PCM_IOCTL_DELAY:
2559 return snd_pcm_delay(substream, arg);
2560 case SNDRV_PCM_IOCTL_SYNC_PTR:
2561 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002562#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2564 return snd_pcm_hw_refine_old_user(substream, arg);
2565 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2566 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002567#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 case SNDRV_PCM_IOCTL_DRAIN:
2569 return snd_pcm_drain(substream);
2570 case SNDRV_PCM_IOCTL_DROP:
2571 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002572 case SNDRV_PCM_IOCTL_PAUSE:
2573 {
2574 int res;
2575 snd_pcm_stream_lock_irq(substream);
2576 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2577 snd_pcm_stream_unlock_irq(substream);
2578 return res;
2579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581 snd_printd("unknown ioctl = 0x%x\n", cmd);
2582 return -ENOTTY;
2583}
2584
Takashi Iwai0df63e42006-04-28 15:13:41 +02002585static int snd_pcm_playback_ioctl1(struct file *file,
2586 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 unsigned int cmd, void __user *arg)
2588{
2589 snd_assert(substream != NULL, return -ENXIO);
2590 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2591 switch (cmd) {
2592 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2593 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002594 struct snd_xferi xferi;
2595 struct snd_xferi __user *_xferi = arg;
2596 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 snd_pcm_sframes_t result;
2598 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2599 return -EBADFD;
2600 if (put_user(0, &_xferi->result))
2601 return -EFAULT;
2602 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2603 return -EFAULT;
2604 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2605 __put_user(result, &_xferi->result);
2606 return result < 0 ? result : 0;
2607 }
2608 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2609 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002610 struct snd_xfern xfern;
2611 struct snd_xfern __user *_xfern = arg;
2612 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 void __user **bufs;
2614 snd_pcm_sframes_t result;
2615 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2616 return -EBADFD;
2617 if (runtime->channels > 128)
2618 return -EINVAL;
2619 if (put_user(0, &_xfern->result))
2620 return -EFAULT;
2621 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2622 return -EFAULT;
2623 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2624 if (bufs == NULL)
2625 return -ENOMEM;
2626 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2627 kfree(bufs);
2628 return -EFAULT;
2629 }
2630 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2631 kfree(bufs);
2632 __put_user(result, &_xfern->result);
2633 return result < 0 ? result : 0;
2634 }
2635 case SNDRV_PCM_IOCTL_REWIND:
2636 {
2637 snd_pcm_uframes_t frames;
2638 snd_pcm_uframes_t __user *_frames = arg;
2639 snd_pcm_sframes_t result;
2640 if (get_user(frames, _frames))
2641 return -EFAULT;
2642 if (put_user(0, _frames))
2643 return -EFAULT;
2644 result = snd_pcm_playback_rewind(substream, frames);
2645 __put_user(result, _frames);
2646 return result < 0 ? result : 0;
2647 }
2648 case SNDRV_PCM_IOCTL_FORWARD:
2649 {
2650 snd_pcm_uframes_t frames;
2651 snd_pcm_uframes_t __user *_frames = arg;
2652 snd_pcm_sframes_t result;
2653 if (get_user(frames, _frames))
2654 return -EFAULT;
2655 if (put_user(0, _frames))
2656 return -EFAULT;
2657 result = snd_pcm_playback_forward(substream, frames);
2658 __put_user(result, _frames);
2659 return result < 0 ? result : 0;
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002662 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663}
2664
Takashi Iwai0df63e42006-04-28 15:13:41 +02002665static int snd_pcm_capture_ioctl1(struct file *file,
2666 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 unsigned int cmd, void __user *arg)
2668{
2669 snd_assert(substream != NULL, return -ENXIO);
2670 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2671 switch (cmd) {
2672 case SNDRV_PCM_IOCTL_READI_FRAMES:
2673 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002674 struct snd_xferi xferi;
2675 struct snd_xferi __user *_xferi = arg;
2676 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 snd_pcm_sframes_t result;
2678 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2679 return -EBADFD;
2680 if (put_user(0, &_xferi->result))
2681 return -EFAULT;
2682 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2683 return -EFAULT;
2684 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2685 __put_user(result, &_xferi->result);
2686 return result < 0 ? result : 0;
2687 }
2688 case SNDRV_PCM_IOCTL_READN_FRAMES:
2689 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002690 struct snd_xfern xfern;
2691 struct snd_xfern __user *_xfern = arg;
2692 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 void *bufs;
2694 snd_pcm_sframes_t result;
2695 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2696 return -EBADFD;
2697 if (runtime->channels > 128)
2698 return -EINVAL;
2699 if (put_user(0, &_xfern->result))
2700 return -EFAULT;
2701 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2702 return -EFAULT;
2703 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2704 if (bufs == NULL)
2705 return -ENOMEM;
2706 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2707 kfree(bufs);
2708 return -EFAULT;
2709 }
2710 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2711 kfree(bufs);
2712 __put_user(result, &_xfern->result);
2713 return result < 0 ? result : 0;
2714 }
2715 case SNDRV_PCM_IOCTL_REWIND:
2716 {
2717 snd_pcm_uframes_t frames;
2718 snd_pcm_uframes_t __user *_frames = arg;
2719 snd_pcm_sframes_t result;
2720 if (get_user(frames, _frames))
2721 return -EFAULT;
2722 if (put_user(0, _frames))
2723 return -EFAULT;
2724 result = snd_pcm_capture_rewind(substream, frames);
2725 __put_user(result, _frames);
2726 return result < 0 ? result : 0;
2727 }
2728 case SNDRV_PCM_IOCTL_FORWARD:
2729 {
2730 snd_pcm_uframes_t frames;
2731 snd_pcm_uframes_t __user *_frames = arg;
2732 snd_pcm_sframes_t result;
2733 if (get_user(frames, _frames))
2734 return -EFAULT;
2735 if (put_user(0, _frames))
2736 return -EFAULT;
2737 result = snd_pcm_capture_forward(substream, frames);
2738 __put_user(result, _frames);
2739 return result < 0 ? result : 0;
2740 }
2741 }
Takashi Iwai0df63e42006-04-28 15:13:41 +02002742 return snd_pcm_common_ioctl1(file, substream, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Takashi Iwai877211f2005-11-17 13:59:38 +01002745static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2746 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Takashi Iwai877211f2005-11-17 13:59:38 +01002748 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 pcm_file = file->private_data;
2751
2752 if (((cmd >> 8) & 0xff) != 'A')
2753 return -ENOTTY;
2754
Takashi Iwai0df63e42006-04-28 15:13:41 +02002755 return snd_pcm_playback_ioctl1(file, pcm_file->substream, cmd,
2756 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Takashi Iwai877211f2005-11-17 13:59:38 +01002759static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2760 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Takashi Iwai877211f2005-11-17 13:59:38 +01002762 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 pcm_file = file->private_data;
2765
2766 if (((cmd >> 8) & 0xff) != 'A')
2767 return -ENOTTY;
2768
Takashi Iwai0df63e42006-04-28 15:13:41 +02002769 return snd_pcm_capture_ioctl1(file, pcm_file->substream, cmd,
2770 (void __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771}
2772
Takashi Iwai877211f2005-11-17 13:59:38 +01002773int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned int cmd, void *arg)
2775{
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002776 mm_segment_t fs;
2777 int result;
2778
2779 fs = snd_enter_user();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 switch (substream->stream) {
2781 case SNDRV_PCM_STREAM_PLAYBACK:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002782 result = snd_pcm_playback_ioctl1(NULL, substream, cmd,
2783 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002784 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 case SNDRV_PCM_STREAM_CAPTURE:
Takashi Iwai0df63e42006-04-28 15:13:41 +02002786 result = snd_pcm_capture_ioctl1(NULL, substream, cmd,
2787 (void __user *)arg);
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002788 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 default:
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002790 result = -EINVAL;
2791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Takashi Iwaibf1bbb52006-03-27 16:22:45 +02002793 snd_leave_user(fs);
2794 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
2796
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02002797EXPORT_SYMBOL(snd_pcm_kernel_ioctl);
2798
Takashi Iwai877211f2005-11-17 13:59:38 +01002799static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2800 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
Takashi Iwai877211f2005-11-17 13:59:38 +01002802 struct snd_pcm_file *pcm_file;
2803 struct snd_pcm_substream *substream;
2804 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 snd_pcm_sframes_t result;
2806
2807 pcm_file = file->private_data;
2808 substream = pcm_file->substream;
2809 snd_assert(substream != NULL, return -ENXIO);
2810 runtime = substream->runtime;
2811 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2812 return -EBADFD;
2813 if (!frame_aligned(runtime, count))
2814 return -EINVAL;
2815 count = bytes_to_frames(runtime, count);
2816 result = snd_pcm_lib_read(substream, buf, count);
2817 if (result > 0)
2818 result = frames_to_bytes(runtime, result);
2819 return result;
2820}
2821
Takashi Iwai877211f2005-11-17 13:59:38 +01002822static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2823 size_t count, loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Takashi Iwai877211f2005-11-17 13:59:38 +01002825 struct snd_pcm_file *pcm_file;
2826 struct snd_pcm_substream *substream;
2827 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 snd_pcm_sframes_t result;
2829
2830 pcm_file = file->private_data;
2831 substream = pcm_file->substream;
2832 snd_assert(substream != NULL, result = -ENXIO; goto end);
2833 runtime = substream->runtime;
2834 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2835 result = -EBADFD;
2836 goto end;
2837 }
2838 if (!frame_aligned(runtime, count)) {
2839 result = -EINVAL;
2840 goto end;
2841 }
2842 count = bytes_to_frames(runtime, count);
2843 result = snd_pcm_lib_write(substream, buf, count);
2844 if (result > 0)
2845 result = frames_to_bytes(runtime, result);
2846 end:
2847 return result;
2848}
2849
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002850static ssize_t snd_pcm_aio_read(struct kiocb *iocb, const struct iovec *iov,
2851 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
2853{
Takashi Iwai877211f2005-11-17 13:59:38 +01002854 struct snd_pcm_file *pcm_file;
2855 struct snd_pcm_substream *substream;
2856 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 snd_pcm_sframes_t result;
2858 unsigned long i;
2859 void __user **bufs;
2860 snd_pcm_uframes_t frames;
2861
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002862 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 substream = pcm_file->substream;
2864 snd_assert(substream != NULL, return -ENXIO);
2865 runtime = substream->runtime;
2866 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2867 return -EBADFD;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002868 if (nr_segs > 1024 || nr_segs != runtime->channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002870 if (!frame_aligned(runtime, iov->iov_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -EINVAL;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002872 frames = bytes_to_samples(runtime, iov->iov_len);
2873 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (bufs == NULL)
2875 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002876 for (i = 0; i < nr_segs; ++i)
2877 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 result = snd_pcm_lib_readv(substream, bufs, frames);
2879 if (result > 0)
2880 result = frames_to_bytes(runtime, result);
2881 kfree(bufs);
2882 return result;
2883}
2884
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002885static ssize_t snd_pcm_aio_write(struct kiocb *iocb, const struct iovec *iov,
2886 unsigned long nr_segs, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
Takashi Iwai877211f2005-11-17 13:59:38 +01002888 struct snd_pcm_file *pcm_file;
2889 struct snd_pcm_substream *substream;
2890 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 snd_pcm_sframes_t result;
2892 unsigned long i;
2893 void __user **bufs;
2894 snd_pcm_uframes_t frames;
2895
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002896 pcm_file = iocb->ki_filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 substream = pcm_file->substream;
2898 snd_assert(substream != NULL, result = -ENXIO; goto end);
2899 runtime = substream->runtime;
2900 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2901 result = -EBADFD;
2902 goto end;
2903 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002904 if (nr_segs > 128 || nr_segs != runtime->channels ||
2905 !frame_aligned(runtime, iov->iov_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 result = -EINVAL;
2907 goto end;
2908 }
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002909 frames = bytes_to_samples(runtime, iov->iov_len);
2910 bufs = kmalloc(sizeof(void *) * nr_segs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (bufs == NULL)
2912 return -ENOMEM;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002913 for (i = 0; i < nr_segs; ++i)
2914 bufs[i] = iov[i].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 result = snd_pcm_lib_writev(substream, bufs, frames);
2916 if (result > 0)
2917 result = frames_to_bytes(runtime, result);
2918 kfree(bufs);
2919 end:
2920 return result;
2921}
2922
2923static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2924{
Takashi Iwai877211f2005-11-17 13:59:38 +01002925 struct snd_pcm_file *pcm_file;
2926 struct snd_pcm_substream *substream;
2927 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned int mask;
2929 snd_pcm_uframes_t avail;
2930
2931 pcm_file = file->private_data;
2932
2933 substream = pcm_file->substream;
2934 snd_assert(substream != NULL, return -ENXIO);
2935 runtime = substream->runtime;
2936
2937 poll_wait(file, &runtime->sleep, wait);
2938
2939 snd_pcm_stream_lock_irq(substream);
2940 avail = snd_pcm_playback_avail(runtime);
2941 switch (runtime->status->state) {
2942 case SNDRV_PCM_STATE_RUNNING:
2943 case SNDRV_PCM_STATE_PREPARED:
2944 case SNDRV_PCM_STATE_PAUSED:
2945 if (avail >= runtime->control->avail_min) {
2946 mask = POLLOUT | POLLWRNORM;
2947 break;
2948 }
2949 /* Fall through */
2950 case SNDRV_PCM_STATE_DRAINING:
2951 mask = 0;
2952 break;
2953 default:
2954 mask = POLLOUT | POLLWRNORM | POLLERR;
2955 break;
2956 }
2957 snd_pcm_stream_unlock_irq(substream);
2958 return mask;
2959}
2960
2961static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2962{
Takashi Iwai877211f2005-11-17 13:59:38 +01002963 struct snd_pcm_file *pcm_file;
2964 struct snd_pcm_substream *substream;
2965 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 unsigned int mask;
2967 snd_pcm_uframes_t avail;
2968
2969 pcm_file = file->private_data;
2970
2971 substream = pcm_file->substream;
2972 snd_assert(substream != NULL, return -ENXIO);
2973 runtime = substream->runtime;
2974
2975 poll_wait(file, &runtime->sleep, wait);
2976
2977 snd_pcm_stream_lock_irq(substream);
2978 avail = snd_pcm_capture_avail(runtime);
2979 switch (runtime->status->state) {
2980 case SNDRV_PCM_STATE_RUNNING:
2981 case SNDRV_PCM_STATE_PREPARED:
2982 case SNDRV_PCM_STATE_PAUSED:
2983 if (avail >= runtime->control->avail_min) {
2984 mask = POLLIN | POLLRDNORM;
2985 break;
2986 }
2987 mask = 0;
2988 break;
2989 case SNDRV_PCM_STATE_DRAINING:
2990 if (avail > 0) {
2991 mask = POLLIN | POLLRDNORM;
2992 break;
2993 }
2994 /* Fall through */
2995 default:
2996 mask = POLLIN | POLLRDNORM | POLLERR;
2997 break;
2998 }
2999 snd_pcm_stream_unlock_irq(substream);
3000 return mask;
3001}
3002
3003/*
3004 * mmap support
3005 */
3006
3007/*
3008 * Only on coherent architectures, we can mmap the status and the control records
3009 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
3010 */
3011#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
3012/*
3013 * mmap status record
3014 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003015static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
3016 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Takashi Iwai877211f2005-11-17 13:59:38 +01003018 struct snd_pcm_substream *substream = area->vm_private_data;
3019 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct page * page;
3021
3022 if (substream == NULL)
Nick Piggincd54e7e2006-12-06 20:31:53 -08003023 return NOPAGE_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 runtime = substream->runtime;
3025 page = virt_to_page(runtime->status);
Nick Pigginb5810032005-10-29 18:16:12 -07003026 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (type)
3028 *type = VM_FAULT_MINOR;
3029 return page;
3030}
3031
3032static struct vm_operations_struct snd_pcm_vm_ops_status =
3033{
3034 .nopage = snd_pcm_mmap_status_nopage,
3035};
3036
Takashi Iwai877211f2005-11-17 13:59:38 +01003037static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct vm_area_struct *area)
3039{
Takashi Iwai877211f2005-11-17 13:59:38 +01003040 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 long size;
3042 if (!(area->vm_flags & VM_READ))
3043 return -EINVAL;
3044 runtime = substream->runtime;
3045 snd_assert(runtime != NULL, return -EAGAIN);
3046 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003047 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return -EINVAL;
3049 area->vm_ops = &snd_pcm_vm_ops_status;
3050 area->vm_private_data = substream;
3051 area->vm_flags |= VM_RESERVED;
3052 return 0;
3053}
3054
3055/*
3056 * mmap control record
3057 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003058static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3059 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Takashi Iwai877211f2005-11-17 13:59:38 +01003061 struct snd_pcm_substream *substream = area->vm_private_data;
3062 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 struct page * page;
3064
3065 if (substream == NULL)
Nick Piggincd54e7e2006-12-06 20:31:53 -08003066 return NOPAGE_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 runtime = substream->runtime;
3068 page = virt_to_page(runtime->control);
Nick Pigginb5810032005-10-29 18:16:12 -07003069 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (type)
3071 *type = VM_FAULT_MINOR;
3072 return page;
3073}
3074
3075static struct vm_operations_struct snd_pcm_vm_ops_control =
3076{
3077 .nopage = snd_pcm_mmap_control_nopage,
3078};
3079
Takashi Iwai877211f2005-11-17 13:59:38 +01003080static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct vm_area_struct *area)
3082{
Takashi Iwai877211f2005-11-17 13:59:38 +01003083 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 long size;
3085 if (!(area->vm_flags & VM_READ))
3086 return -EINVAL;
3087 runtime = substream->runtime;
3088 snd_assert(runtime != NULL, return -EAGAIN);
3089 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003090 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return -EINVAL;
3092 area->vm_ops = &snd_pcm_vm_ops_control;
3093 area->vm_private_data = substream;
3094 area->vm_flags |= VM_RESERVED;
3095 return 0;
3096}
3097#else /* ! coherent mmap */
3098/*
3099 * don't support mmap for status and control records.
3100 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003101static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct vm_area_struct *area)
3103{
3104 return -ENXIO;
3105}
Takashi Iwai877211f2005-11-17 13:59:38 +01003106static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 struct vm_area_struct *area)
3108{
3109 return -ENXIO;
3110}
3111#endif /* coherent mmap */
3112
3113/*
3114 * nopage callback for mmapping a RAM page
3115 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003116static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3117 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Takashi Iwai877211f2005-11-17 13:59:38 +01003119 struct snd_pcm_substream *substream = area->vm_private_data;
3120 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 unsigned long offset;
3122 struct page * page;
3123 void *vaddr;
3124 size_t dma_bytes;
3125
3126 if (substream == NULL)
Nick Piggincd54e7e2006-12-06 20:31:53 -08003127 return NOPAGE_SIGBUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 runtime = substream->runtime;
3129 offset = area->vm_pgoff << PAGE_SHIFT;
3130 offset += address - area->vm_start;
Nick Piggincd54e7e2006-12-06 20:31:53 -08003131 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_SIGBUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3133 if (offset > dma_bytes - PAGE_SIZE)
3134 return NOPAGE_SIGBUS;
3135 if (substream->ops->page) {
3136 page = substream->ops->page(substream, offset);
3137 if (! page)
Nick Piggincd54e7e2006-12-06 20:31:53 -08003138 return NOPAGE_OOM; /* XXX: is this really due to OOM? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 } else {
3140 vaddr = runtime->dma_area + offset;
3141 page = virt_to_page(vaddr);
3142 }
Nick Pigginb5810032005-10-29 18:16:12 -07003143 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (type)
3145 *type = VM_FAULT_MINOR;
3146 return page;
3147}
3148
3149static struct vm_operations_struct snd_pcm_vm_ops_data =
3150{
3151 .open = snd_pcm_mmap_data_open,
3152 .close = snd_pcm_mmap_data_close,
3153 .nopage = snd_pcm_mmap_data_nopage,
3154};
3155
3156/*
3157 * mmap the DMA buffer on RAM
3158 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003159static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3160 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 area->vm_ops = &snd_pcm_vm_ops_data;
3163 area->vm_private_data = substream;
3164 area->vm_flags |= VM_RESERVED;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003165 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return 0;
3167}
3168
3169/*
3170 * mmap the DMA buffer on I/O memory area
3171 */
3172#if SNDRV_PCM_INFO_MMAP_IOMEM
3173static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3174{
3175 .open = snd_pcm_mmap_data_open,
3176 .close = snd_pcm_mmap_data_close,
3177};
3178
Takashi Iwai877211f2005-11-17 13:59:38 +01003179int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3180 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
3182 long size;
3183 unsigned long offset;
3184
3185#ifdef pgprot_noncached
3186 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3187#endif
3188 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3189 area->vm_private_data = substream;
3190 area->vm_flags |= VM_IO;
3191 size = area->vm_end - area->vm_start;
3192 offset = area->vm_pgoff << PAGE_SHIFT;
3193 if (io_remap_pfn_range(area, area->vm_start,
3194 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3195 size, area->vm_page_prot))
3196 return -EAGAIN;
Takashi Iwai9c323fc2006-04-28 15:13:41 +02003197 atomic_inc(&substream->mmap_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 return 0;
3199}
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003200
3201EXPORT_SYMBOL(snd_pcm_lib_mmap_iomem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202#endif /* SNDRV_PCM_INFO_MMAP */
3203
3204/*
3205 * mmap DMA buffer
3206 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003207int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 struct vm_area_struct *area)
3209{
Takashi Iwai877211f2005-11-17 13:59:38 +01003210 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 long size;
3212 unsigned long offset;
3213 size_t dma_bytes;
3214
3215 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3216 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3217 return -EINVAL;
3218 } else {
3219 if (!(area->vm_flags & VM_READ))
3220 return -EINVAL;
3221 }
3222 runtime = substream->runtime;
3223 snd_assert(runtime != NULL, return -EAGAIN);
3224 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3225 return -EBADFD;
3226 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3227 return -ENXIO;
3228 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3229 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3230 return -EINVAL;
3231 size = area->vm_end - area->vm_start;
3232 offset = area->vm_pgoff << PAGE_SHIFT;
3233 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3234 if ((size_t)size > dma_bytes)
3235 return -EINVAL;
3236 if (offset > dma_bytes - size)
3237 return -EINVAL;
3238
3239 if (substream->ops->mmap)
3240 return substream->ops->mmap(substream, area);
3241 else
3242 return snd_pcm_default_mmap(substream, area);
3243}
3244
Takashi Iwaie88e8ae62006-04-28 15:13:40 +02003245EXPORT_SYMBOL(snd_pcm_mmap_data);
3246
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3248{
Takashi Iwai877211f2005-11-17 13:59:38 +01003249 struct snd_pcm_file * pcm_file;
3250 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 unsigned long offset;
3252
3253 pcm_file = file->private_data;
3254 substream = pcm_file->substream;
3255 snd_assert(substream != NULL, return -ENXIO);
3256
3257 offset = area->vm_pgoff << PAGE_SHIFT;
3258 switch (offset) {
3259 case SNDRV_PCM_MMAP_OFFSET_STATUS:
Takashi Iwai548a6482006-07-31 16:51:51 +02003260 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 return -ENXIO;
3262 return snd_pcm_mmap_status(substream, file, area);
3263 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
Takashi Iwai548a6482006-07-31 16:51:51 +02003264 if (pcm_file->no_compat_mmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return -ENXIO;
3266 return snd_pcm_mmap_control(substream, file, area);
3267 default:
3268 return snd_pcm_mmap_data(substream, file, area);
3269 }
3270 return 0;
3271}
3272
3273static int snd_pcm_fasync(int fd, struct file * file, int on)
3274{
Takashi Iwai877211f2005-11-17 13:59:38 +01003275 struct snd_pcm_file * pcm_file;
3276 struct snd_pcm_substream *substream;
3277 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 int err;
3279
3280 pcm_file = file->private_data;
3281 substream = pcm_file->substream;
3282 snd_assert(substream != NULL, return -ENXIO);
3283 runtime = substream->runtime;
3284
3285 err = fasync_helper(fd, file, on, &runtime->fasync);
3286 if (err < 0)
3287 return err;
3288 return 0;
3289}
3290
3291/*
3292 * ioctl32 compat
3293 */
3294#ifdef CONFIG_COMPAT
3295#include "pcm_compat.c"
3296#else
3297#define snd_pcm_ioctl_compat NULL
3298#endif
3299
3300/*
3301 * To be removed helpers to keep binary compatibility
3302 */
3303
Takashi Iwai59d48582005-12-01 10:51:58 +01003304#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3306#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3307
Takashi Iwai877211f2005-11-17 13:59:38 +01003308static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3309 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 unsigned int i;
3312
3313 memset(params, 0, sizeof(*params));
3314 params->flags = oparams->flags;
3315 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3316 params->masks[i].bits[0] = oparams->masks[i];
3317 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3318 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3319 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3320 params->info = oparams->info;
3321 params->msbits = oparams->msbits;
3322 params->rate_num = oparams->rate_num;
3323 params->rate_den = oparams->rate_den;
3324 params->fifo_size = oparams->fifo_size;
3325}
3326
Takashi Iwai877211f2005-11-17 13:59:38 +01003327static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3328 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329{
3330 unsigned int i;
3331
3332 memset(oparams, 0, sizeof(*oparams));
3333 oparams->flags = params->flags;
3334 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3335 oparams->masks[i] = params->masks[i].bits[0];
3336 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3337 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3338 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3339 oparams->info = params->info;
3340 oparams->msbits = params->msbits;
3341 oparams->rate_num = params->rate_num;
3342 oparams->rate_den = params->rate_den;
3343 oparams->fifo_size = params->fifo_size;
3344}
3345
Takashi Iwai877211f2005-11-17 13:59:38 +01003346static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3347 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Takashi Iwai877211f2005-11-17 13:59:38 +01003349 struct snd_pcm_hw_params *params;
3350 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 int err;
3352
3353 params = kmalloc(sizeof(*params), GFP_KERNEL);
3354 if (!params) {
3355 err = -ENOMEM;
3356 goto out;
3357 }
3358 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3359 if (!oparams) {
3360 err = -ENOMEM;
3361 goto out;
3362 }
3363
3364 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3365 err = -EFAULT;
3366 goto out;
3367 }
3368 snd_pcm_hw_convert_from_old_params(params, oparams);
3369 err = snd_pcm_hw_refine(substream, params);
3370 snd_pcm_hw_convert_to_old_params(oparams, params);
3371 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3372 if (!err)
3373 err = -EFAULT;
3374 }
3375out:
3376 kfree(params);
3377 kfree(oparams);
3378 return err;
3379}
3380
Takashi Iwai877211f2005-11-17 13:59:38 +01003381static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3382 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
Takashi Iwai877211f2005-11-17 13:59:38 +01003384 struct snd_pcm_hw_params *params;
3385 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 int err;
3387
3388 params = kmalloc(sizeof(*params), GFP_KERNEL);
3389 if (!params) {
3390 err = -ENOMEM;
3391 goto out;
3392 }
3393 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3394 if (!oparams) {
3395 err = -ENOMEM;
3396 goto out;
3397 }
3398 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3399 err = -EFAULT;
3400 goto out;
3401 }
3402 snd_pcm_hw_convert_from_old_params(params, oparams);
3403 err = snd_pcm_hw_params(substream, params);
3404 snd_pcm_hw_convert_to_old_params(oparams, params);
3405 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3406 if (!err)
3407 err = -EFAULT;
3408 }
3409out:
3410 kfree(params);
3411 kfree(oparams);
3412 return err;
3413}
Takashi Iwai59d48582005-12-01 10:51:58 +01003414#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
3416/*
3417 * Register section
3418 */
3419
Arjan van de Ven9c2e08c2007-02-12 00:55:37 -08003420const struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003422 .owner = THIS_MODULE,
3423 .write = snd_pcm_write,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003424 .aio_write = snd_pcm_aio_write,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003425 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003426 .release = snd_pcm_release,
3427 .poll = snd_pcm_playback_poll,
3428 .unlocked_ioctl = snd_pcm_playback_ioctl,
3429 .compat_ioctl = snd_pcm_ioctl_compat,
3430 .mmap = snd_pcm_mmap,
3431 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 },
3433 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003434 .owner = THIS_MODULE,
3435 .read = snd_pcm_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07003436 .aio_read = snd_pcm_aio_read,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003437 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003438 .release = snd_pcm_release,
3439 .poll = snd_pcm_capture_poll,
3440 .unlocked_ioctl = snd_pcm_capture_ioctl,
3441 .compat_ioctl = snd_pcm_ioctl_compat,
3442 .mmap = snd_pcm_mmap,
3443 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445};