blob: 7010eb271cc70851ccc43b68e4dad204373c9b61 [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>
24#include <linux/smp_lock.h>
25#include <linux/file.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#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);
74static DECLARE_RWSEM(snd_pcm_link_rwsem);
75
76
77static inline mm_segment_t snd_enter_user(void)
78{
79 mm_segment_t fs = get_fs();
80 set_fs(get_ds());
81 return fs;
82}
83
84static inline void snd_leave_user(mm_segment_t fs)
85{
86 set_fs(fs);
87}
88
89
90
Takashi Iwai877211f2005-11-17 13:59:38 +010091int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Takashi Iwai877211f2005-11-17 13:59:38 +010093 struct snd_pcm_runtime *runtime;
94 struct snd_pcm *pcm = substream->pcm;
95 struct snd_pcm_str *pstr = substream->pstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 snd_assert(substream != NULL, return -ENXIO);
98 memset(info, 0, sizeof(*info));
99 info->card = pcm->card->number;
100 info->device = pcm->device;
101 info->stream = substream->stream;
102 info->subdevice = substream->number;
103 strlcpy(info->id, pcm->id, sizeof(info->id));
104 strlcpy(info->name, pcm->name, sizeof(info->name));
105 info->dev_class = pcm->dev_class;
106 info->dev_subclass = pcm->dev_subclass;
107 info->subdevices_count = pstr->substream_count;
108 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
109 strlcpy(info->subname, substream->name, sizeof(info->subname));
110 runtime = substream->runtime;
111 /* AB: FIXME!!! This is definitely nonsense */
112 if (runtime) {
113 info->sync = runtime->sync;
114 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
115 }
116 return 0;
117}
118
Takashi Iwai877211f2005-11-17 13:59:38 +0100119int snd_pcm_info_user(struct snd_pcm_substream *substream,
120 struct snd_pcm_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Takashi Iwai877211f2005-11-17 13:59:38 +0100122 struct snd_pcm_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int err;
124
125 info = kmalloc(sizeof(*info), GFP_KERNEL);
126 if (! info)
127 return -ENOMEM;
128 err = snd_pcm_info(substream, info);
129 if (err >= 0) {
130 if (copy_to_user(_info, info, sizeof(*info)))
131 err = -EFAULT;
132 }
133 kfree(info);
134 return err;
135}
136
137#undef RULES_DEBUG
138
139#ifdef RULES_DEBUG
140#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
141char *snd_pcm_hw_param_names[] = {
142 HW_PARAM(ACCESS),
143 HW_PARAM(FORMAT),
144 HW_PARAM(SUBFORMAT),
145 HW_PARAM(SAMPLE_BITS),
146 HW_PARAM(FRAME_BITS),
147 HW_PARAM(CHANNELS),
148 HW_PARAM(RATE),
149 HW_PARAM(PERIOD_TIME),
150 HW_PARAM(PERIOD_SIZE),
151 HW_PARAM(PERIOD_BYTES),
152 HW_PARAM(PERIODS),
153 HW_PARAM(BUFFER_TIME),
154 HW_PARAM(BUFFER_SIZE),
155 HW_PARAM(BUFFER_BYTES),
156 HW_PARAM(TICK_TIME),
157};
158#endif
159
Takashi Iwai877211f2005-11-17 13:59:38 +0100160int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +0100164 struct snd_pcm_hardware *hw;
165 struct snd_interval *i = NULL;
166 struct snd_mask *m = NULL;
167 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int rstamps[constrs->rules_num];
169 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
170 unsigned int stamp = 2;
171 int changed, again;
172
173 params->info = 0;
174 params->fifo_size = 0;
175 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
176 params->msbits = 0;
177 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
178 params->rate_num = 0;
179 params->rate_den = 0;
180 }
181
182 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
183 m = hw_param_mask(params, k);
184 if (snd_mask_empty(m))
185 return -EINVAL;
186 if (!(params->rmask & (1 << k)))
187 continue;
188#ifdef RULES_DEBUG
189 printk("%s = ", snd_pcm_hw_param_names[k]);
190 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
191#endif
192 changed = snd_mask_refine(m, constrs_mask(constrs, k));
193#ifdef RULES_DEBUG
194 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
195#endif
196 if (changed)
197 params->cmask |= 1 << k;
198 if (changed < 0)
199 return changed;
200 }
201
202 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
203 i = hw_param_interval(params, k);
204 if (snd_interval_empty(i))
205 return -EINVAL;
206 if (!(params->rmask & (1 << k)))
207 continue;
208#ifdef RULES_DEBUG
209 printk("%s = ", snd_pcm_hw_param_names[k]);
210 if (i->empty)
211 printk("empty");
212 else
213 printk("%c%u %u%c",
214 i->openmin ? '(' : '[', i->min,
215 i->max, i->openmax ? ')' : ']');
216 printk(" -> ");
217#endif
218 changed = snd_interval_refine(i, constrs_interval(constrs, k));
219#ifdef RULES_DEBUG
220 if (i->empty)
221 printk("empty\n");
222 else
223 printk("%c%u %u%c\n",
224 i->openmin ? '(' : '[', i->min,
225 i->max, i->openmax ? ')' : ']');
226#endif
227 if (changed)
228 params->cmask |= 1 << k;
229 if (changed < 0)
230 return changed;
231 }
232
233 for (k = 0; k < constrs->rules_num; k++)
234 rstamps[k] = 0;
235 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
236 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
237 do {
238 again = 0;
239 for (k = 0; k < constrs->rules_num; k++) {
Takashi Iwai877211f2005-11-17 13:59:38 +0100240 struct snd_pcm_hw_rule *r = &constrs->rules[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned int d;
242 int doit = 0;
243 if (r->cond && !(r->cond & params->flags))
244 continue;
245 for (d = 0; r->deps[d] >= 0; d++) {
246 if (vstamps[r->deps[d]] > rstamps[k]) {
247 doit = 1;
248 break;
249 }
250 }
251 if (!doit)
252 continue;
253#ifdef RULES_DEBUG
254 printk("Rule %d [%p]: ", k, r->func);
255 if (r->var >= 0) {
256 printk("%s = ", snd_pcm_hw_param_names[r->var]);
257 if (hw_is_mask(r->var)) {
258 m = hw_param_mask(params, r->var);
259 printk("%x", *m->bits);
260 } else {
261 i = hw_param_interval(params, r->var);
262 if (i->empty)
263 printk("empty");
264 else
265 printk("%c%u %u%c",
266 i->openmin ? '(' : '[', i->min,
267 i->max, i->openmax ? ')' : ']');
268 }
269 }
270#endif
271 changed = r->func(params, r);
272#ifdef RULES_DEBUG
273 if (r->var >= 0) {
274 printk(" -> ");
275 if (hw_is_mask(r->var))
276 printk("%x", *m->bits);
277 else {
278 if (i->empty)
279 printk("empty");
280 else
281 printk("%c%u %u%c",
282 i->openmin ? '(' : '[', i->min,
283 i->max, i->openmax ? ')' : ']');
284 }
285 }
286 printk("\n");
287#endif
288 rstamps[k] = stamp;
289 if (changed && r->var >= 0) {
290 params->cmask |= (1 << r->var);
291 vstamps[r->var] = stamp;
292 again = 1;
293 }
294 if (changed < 0)
295 return changed;
296 stamp++;
297 }
298 } while (again);
299 if (!params->msbits) {
300 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
301 if (snd_interval_single(i))
302 params->msbits = snd_interval_value(i);
303 }
304
305 if (!params->rate_den) {
306 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
307 if (snd_interval_single(i)) {
308 params->rate_num = snd_interval_value(i);
309 params->rate_den = 1;
310 }
311 }
312
313 hw = &substream->runtime->hw;
314 if (!params->info)
315 params->info = hw->info;
316 if (!params->fifo_size)
317 params->fifo_size = hw->fifo_size;
318 params->rmask = 0;
319 return 0;
320}
321
Takashi Iwai877211f2005-11-17 13:59:38 +0100322static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
323 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Takashi Iwai877211f2005-11-17 13:59:38 +0100325 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int err;
327
328 params = kmalloc(sizeof(*params), GFP_KERNEL);
329 if (!params) {
330 err = -ENOMEM;
331 goto out;
332 }
333 if (copy_from_user(params, _params, sizeof(*params))) {
334 err = -EFAULT;
335 goto out;
336 }
337 err = snd_pcm_hw_refine(substream, params);
338 if (copy_to_user(_params, params, sizeof(*params))) {
339 if (!err)
340 err = -EFAULT;
341 }
342out:
343 kfree(params);
344 return err;
345}
346
Takashi Iwai877211f2005-11-17 13:59:38 +0100347static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
348 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Takashi Iwai877211f2005-11-17 13:59:38 +0100350 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int err;
352 unsigned int bits;
353 snd_pcm_uframes_t frames;
354
355 snd_assert(substream != NULL, return -ENXIO);
356 runtime = substream->runtime;
357 snd_assert(runtime != NULL, return -ENXIO);
358 snd_pcm_stream_lock_irq(substream);
359 switch (runtime->status->state) {
360 case SNDRV_PCM_STATE_OPEN:
361 case SNDRV_PCM_STATE_SETUP:
362 case SNDRV_PCM_STATE_PREPARED:
363 break;
364 default:
365 snd_pcm_stream_unlock_irq(substream);
366 return -EBADFD;
367 }
368 snd_pcm_stream_unlock_irq(substream);
369#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
370 if (!substream->oss.oss)
371#endif
372 if (atomic_read(&runtime->mmap_count))
373 return -EBADFD;
374
375 params->rmask = ~0U;
376 err = snd_pcm_hw_refine(substream, params);
377 if (err < 0)
378 goto _error;
379
380 err = snd_pcm_hw_params_choose(substream, params);
381 if (err < 0)
382 goto _error;
383
384 if (substream->ops->hw_params != NULL) {
385 err = substream->ops->hw_params(substream, params);
386 if (err < 0)
387 goto _error;
388 }
389
390 runtime->access = params_access(params);
391 runtime->format = params_format(params);
392 runtime->subformat = params_subformat(params);
393 runtime->channels = params_channels(params);
394 runtime->rate = params_rate(params);
395 runtime->period_size = params_period_size(params);
396 runtime->periods = params_periods(params);
397 runtime->buffer_size = params_buffer_size(params);
398 runtime->tick_time = params_tick_time(params);
399 runtime->info = params->info;
400 runtime->rate_num = params->rate_num;
401 runtime->rate_den = params->rate_den;
402
403 bits = snd_pcm_format_physical_width(runtime->format);
404 runtime->sample_bits = bits;
405 bits *= runtime->channels;
406 runtime->frame_bits = bits;
407 frames = 1;
408 while (bits % 8 != 0) {
409 bits *= 2;
410 frames *= 2;
411 }
412 runtime->byte_align = bits / 8;
413 runtime->min_align = frames;
414
415 /* Default sw params */
416 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
417 runtime->period_step = 1;
418 runtime->sleep_min = 0;
419 runtime->control->avail_min = runtime->period_size;
420 runtime->xfer_align = runtime->period_size;
421 runtime->start_threshold = 1;
422 runtime->stop_threshold = runtime->buffer_size;
423 runtime->silence_threshold = 0;
424 runtime->silence_size = 0;
425 runtime->boundary = runtime->buffer_size;
426 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
427 runtime->boundary *= 2;
428
429 snd_pcm_timer_resolution_change(substream);
430 runtime->status->state = SNDRV_PCM_STATE_SETUP;
431 return 0;
432 _error:
433 /* hardware might be unuseable from this time,
434 so we force application to retry to set
435 the correct hardware parameter settings */
436 runtime->status->state = SNDRV_PCM_STATE_OPEN;
437 if (substream->ops->hw_free != NULL)
438 substream->ops->hw_free(substream);
439 return err;
440}
441
Takashi Iwai877211f2005-11-17 13:59:38 +0100442static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
443 struct snd_pcm_hw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Takashi Iwai877211f2005-11-17 13:59:38 +0100445 struct snd_pcm_hw_params *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int err;
447
448 params = kmalloc(sizeof(*params), GFP_KERNEL);
449 if (!params) {
450 err = -ENOMEM;
451 goto out;
452 }
453 if (copy_from_user(params, _params, sizeof(*params))) {
454 err = -EFAULT;
455 goto out;
456 }
457 err = snd_pcm_hw_params(substream, params);
458 if (copy_to_user(_params, params, sizeof(*params))) {
459 if (!err)
460 err = -EFAULT;
461 }
462out:
463 kfree(params);
464 return err;
465}
466
Takashi Iwai877211f2005-11-17 13:59:38 +0100467static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Takashi Iwai877211f2005-11-17 13:59:38 +0100469 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int result = 0;
471
472 snd_assert(substream != NULL, return -ENXIO);
473 runtime = substream->runtime;
474 snd_assert(runtime != NULL, return -ENXIO);
475 snd_pcm_stream_lock_irq(substream);
476 switch (runtime->status->state) {
477 case SNDRV_PCM_STATE_SETUP:
478 case SNDRV_PCM_STATE_PREPARED:
479 break;
480 default:
481 snd_pcm_stream_unlock_irq(substream);
482 return -EBADFD;
483 }
484 snd_pcm_stream_unlock_irq(substream);
485 if (atomic_read(&runtime->mmap_count))
486 return -EBADFD;
487 if (substream->ops->hw_free)
488 result = substream->ops->hw_free(substream);
489 runtime->status->state = SNDRV_PCM_STATE_OPEN;
490 return result;
491}
492
Takashi Iwai877211f2005-11-17 13:59:38 +0100493static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
494 struct snd_pcm_sw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Takashi Iwai877211f2005-11-17 13:59:38 +0100496 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 snd_assert(substream != NULL, return -ENXIO);
499 runtime = substream->runtime;
500 snd_assert(runtime != NULL, return -ENXIO);
501 snd_pcm_stream_lock_irq(substream);
502 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
503 snd_pcm_stream_unlock_irq(substream);
504 return -EBADFD;
505 }
506 snd_pcm_stream_unlock_irq(substream);
507
508 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
509 return -EINVAL;
510 if (params->avail_min == 0)
511 return -EINVAL;
512 if (params->xfer_align == 0 ||
513 params->xfer_align % runtime->min_align != 0)
514 return -EINVAL;
515 if (params->silence_size >= runtime->boundary) {
516 if (params->silence_threshold != 0)
517 return -EINVAL;
518 } else {
519 if (params->silence_size > params->silence_threshold)
520 return -EINVAL;
521 if (params->silence_threshold > runtime->buffer_size)
522 return -EINVAL;
523 }
524 snd_pcm_stream_lock_irq(substream);
525 runtime->tstamp_mode = params->tstamp_mode;
526 runtime->sleep_min = params->sleep_min;
527 runtime->period_step = params->period_step;
528 runtime->control->avail_min = params->avail_min;
529 runtime->start_threshold = params->start_threshold;
530 runtime->stop_threshold = params->stop_threshold;
531 runtime->silence_threshold = params->silence_threshold;
532 runtime->silence_size = params->silence_size;
533 runtime->xfer_align = params->xfer_align;
534 params->boundary = runtime->boundary;
535 if (snd_pcm_running(substream)) {
536 if (runtime->sleep_min)
537 snd_pcm_tick_prepare(substream);
538 else
539 snd_pcm_tick_set(substream, 0);
540 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
541 runtime->silence_size > 0)
542 snd_pcm_playback_silence(substream, ULONG_MAX);
543 wake_up(&runtime->sleep);
544 }
545 snd_pcm_stream_unlock_irq(substream);
546 return 0;
547}
548
Takashi Iwai877211f2005-11-17 13:59:38 +0100549static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
550 struct snd_pcm_sw_params __user * _params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Takashi Iwai877211f2005-11-17 13:59:38 +0100552 struct snd_pcm_sw_params params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 int err;
554 if (copy_from_user(&params, _params, sizeof(params)))
555 return -EFAULT;
556 err = snd_pcm_sw_params(substream, &params);
557 if (copy_to_user(_params, &params, sizeof(params)))
558 return -EFAULT;
559 return err;
560}
561
Takashi Iwai877211f2005-11-17 13:59:38 +0100562int snd_pcm_status(struct snd_pcm_substream *substream,
563 struct snd_pcm_status *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Takashi Iwai877211f2005-11-17 13:59:38 +0100565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 snd_pcm_stream_lock_irq(substream);
568 status->state = runtime->status->state;
569 status->suspended_state = runtime->status->suspended_state;
570 if (status->state == SNDRV_PCM_STATE_OPEN)
571 goto _end;
572 status->trigger_tstamp = runtime->trigger_tstamp;
573 if (snd_pcm_running(substream)) {
574 snd_pcm_update_hw_ptr(substream);
575 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
576 status->tstamp = runtime->status->tstamp;
577 else
Takashi Iwai07799e72005-10-10 11:49:49 +0200578 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 } else
Takashi Iwai07799e72005-10-10 11:49:49 +0200580 getnstimeofday(&status->tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 status->appl_ptr = runtime->control->appl_ptr;
582 status->hw_ptr = runtime->status->hw_ptr;
583 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
584 status->avail = snd_pcm_playback_avail(runtime);
585 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
586 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
587 status->delay = runtime->buffer_size - status->avail;
588 else
589 status->delay = 0;
590 } else {
591 status->avail = snd_pcm_capture_avail(runtime);
592 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
593 status->delay = status->avail;
594 else
595 status->delay = 0;
596 }
597 status->avail_max = runtime->avail_max;
598 status->overrange = runtime->overrange;
599 runtime->avail_max = 0;
600 runtime->overrange = 0;
601 _end:
602 snd_pcm_stream_unlock_irq(substream);
603 return 0;
604}
605
Takashi Iwai877211f2005-11-17 13:59:38 +0100606static int snd_pcm_status_user(struct snd_pcm_substream *substream,
607 struct snd_pcm_status __user * _status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Takashi Iwai877211f2005-11-17 13:59:38 +0100609 struct snd_pcm_status status;
610 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int res;
612
613 snd_assert(substream != NULL, return -ENXIO);
614 runtime = substream->runtime;
615 memset(&status, 0, sizeof(status));
616 res = snd_pcm_status(substream, &status);
617 if (res < 0)
618 return res;
619 if (copy_to_user(_status, &status, sizeof(status)))
620 return -EFAULT;
621 return 0;
622}
623
Takashi Iwai877211f2005-11-17 13:59:38 +0100624static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
625 struct snd_pcm_channel_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Takashi Iwai877211f2005-11-17 13:59:38 +0100627 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned int channel;
629
630 snd_assert(substream != NULL, return -ENXIO);
631 channel = info->channel;
632 runtime = substream->runtime;
633 snd_pcm_stream_lock_irq(substream);
634 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
635 snd_pcm_stream_unlock_irq(substream);
636 return -EBADFD;
637 }
638 snd_pcm_stream_unlock_irq(substream);
639 if (channel >= runtime->channels)
640 return -EINVAL;
641 memset(info, 0, sizeof(*info));
642 info->channel = channel;
643 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
644}
645
Takashi Iwai877211f2005-11-17 13:59:38 +0100646static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
647 struct snd_pcm_channel_info __user * _info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai877211f2005-11-17 13:59:38 +0100649 struct snd_pcm_channel_info info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 int res;
651
652 if (copy_from_user(&info, _info, sizeof(info)))
653 return -EFAULT;
654 res = snd_pcm_channel_info(substream, &info);
655 if (res < 0)
656 return res;
657 if (copy_to_user(_info, &info, sizeof(info)))
658 return -EFAULT;
659 return 0;
660}
661
Takashi Iwai877211f2005-11-17 13:59:38 +0100662static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Takashi Iwai877211f2005-11-17 13:59:38 +0100664 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (runtime->trigger_master == NULL)
666 return;
667 if (runtime->trigger_master == substream) {
Takashi Iwai07799e72005-10-10 11:49:49 +0200668 getnstimeofday(&runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 } else {
670 snd_pcm_trigger_tstamp(runtime->trigger_master);
671 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
672 }
673 runtime->trigger_master = NULL;
674}
675
676struct action_ops {
Takashi Iwai877211f2005-11-17 13:59:38 +0100677 int (*pre_action)(struct snd_pcm_substream *substream, int state);
678 int (*do_action)(struct snd_pcm_substream *substream, int state);
679 void (*undo_action)(struct snd_pcm_substream *substream, int state);
680 void (*post_action)(struct snd_pcm_substream *substream, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682
683/*
684 * this functions is core for handling of linked stream
685 * Note: the stream state might be changed also on failure
686 * Note2: call with calling stream lock + link lock
687 */
688static int snd_pcm_action_group(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100689 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int state, int do_lock)
691{
692 struct list_head *pos;
Takashi Iwai877211f2005-11-17 13:59:38 +0100693 struct snd_pcm_substream *s = NULL;
694 struct snd_pcm_substream *s1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 int res = 0;
696
697 snd_pcm_group_for_each(pos, substream) {
698 s = snd_pcm_group_substream_entry(pos);
699 if (do_lock && s != substream)
700 spin_lock(&s->self_group.lock);
701 res = ops->pre_action(s, state);
702 if (res < 0)
703 goto _unlock;
704 }
705 snd_pcm_group_for_each(pos, substream) {
706 s = snd_pcm_group_substream_entry(pos);
707 res = ops->do_action(s, state);
708 if (res < 0) {
709 if (ops->undo_action) {
710 snd_pcm_group_for_each(pos, substream) {
711 s1 = snd_pcm_group_substream_entry(pos);
712 if (s1 == s) /* failed stream */
713 break;
714 ops->undo_action(s1, state);
715 }
716 }
717 s = NULL; /* unlock all */
718 goto _unlock;
719 }
720 }
721 snd_pcm_group_for_each(pos, substream) {
722 s = snd_pcm_group_substream_entry(pos);
723 ops->post_action(s, state);
724 }
725 _unlock:
726 if (do_lock) {
727 /* unlock streams */
728 snd_pcm_group_for_each(pos, substream) {
729 s1 = snd_pcm_group_substream_entry(pos);
730 if (s1 != substream)
731 spin_unlock(&s1->self_group.lock);
732 if (s1 == s) /* end */
733 break;
734 }
735 }
736 return res;
737}
738
739/*
740 * Note: call with stream lock
741 */
742static int snd_pcm_action_single(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100743 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 int state)
745{
746 int res;
747
748 res = ops->pre_action(substream, state);
749 if (res < 0)
750 return res;
751 res = ops->do_action(substream, state);
752 if (res == 0)
753 ops->post_action(substream, state);
754 else if (ops->undo_action)
755 ops->undo_action(substream, state);
756 return res;
757}
758
759/*
760 * Note: call with stream lock
761 */
762static int snd_pcm_action(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100763 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int state)
765{
766 int res;
767
768 if (snd_pcm_stream_linked(substream)) {
769 if (!spin_trylock(&substream->group->lock)) {
770 spin_unlock(&substream->self_group.lock);
771 spin_lock(&substream->group->lock);
772 spin_lock(&substream->self_group.lock);
773 }
774 res = snd_pcm_action_group(ops, substream, state, 1);
775 spin_unlock(&substream->group->lock);
776 } else {
777 res = snd_pcm_action_single(ops, substream, state);
778 }
779 return res;
780}
781
782/*
783 * Note: don't use any locks before
784 */
785static int snd_pcm_action_lock_irq(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100786 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int state)
788{
789 int res;
790
791 read_lock_irq(&snd_pcm_link_rwlock);
792 if (snd_pcm_stream_linked(substream)) {
793 spin_lock(&substream->group->lock);
794 spin_lock(&substream->self_group.lock);
795 res = snd_pcm_action_group(ops, substream, state, 1);
796 spin_unlock(&substream->self_group.lock);
797 spin_unlock(&substream->group->lock);
798 } else {
799 spin_lock(&substream->self_group.lock);
800 res = snd_pcm_action_single(ops, substream, state);
801 spin_unlock(&substream->self_group.lock);
802 }
803 read_unlock_irq(&snd_pcm_link_rwlock);
804 return res;
805}
806
807/*
808 */
809static int snd_pcm_action_nonatomic(struct action_ops *ops,
Takashi Iwai877211f2005-11-17 13:59:38 +0100810 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 int state)
812{
813 int res;
814
815 down_read(&snd_pcm_link_rwsem);
816 if (snd_pcm_stream_linked(substream))
817 res = snd_pcm_action_group(ops, substream, state, 0);
818 else
819 res = snd_pcm_action_single(ops, substream, state);
820 up_read(&snd_pcm_link_rwsem);
821 return res;
822}
823
824/*
825 * start callbacks
826 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100827static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Takashi Iwai877211f2005-11-17 13:59:38 +0100829 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
831 return -EBADFD;
832 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
833 !snd_pcm_playback_data(substream))
834 return -EPIPE;
835 runtime->trigger_master = substream;
836 return 0;
837}
838
Takashi Iwai877211f2005-11-17 13:59:38 +0100839static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 if (substream->runtime->trigger_master != substream)
842 return 0;
843 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
844}
845
Takashi Iwai877211f2005-11-17 13:59:38 +0100846static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 if (substream->runtime->trigger_master == substream)
849 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
850}
851
Takashi Iwai877211f2005-11-17 13:59:38 +0100852static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Takashi Iwai877211f2005-11-17 13:59:38 +0100854 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 snd_pcm_trigger_tstamp(substream);
856 runtime->status->state = state;
857 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
858 runtime->silence_size > 0)
859 snd_pcm_playback_silence(substream, ULONG_MAX);
860 if (runtime->sleep_min)
861 snd_pcm_tick_prepare(substream);
862 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100863 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
864 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867static struct action_ops snd_pcm_action_start = {
868 .pre_action = snd_pcm_pre_start,
869 .do_action = snd_pcm_do_start,
870 .undo_action = snd_pcm_undo_start,
871 .post_action = snd_pcm_post_start
872};
873
874/**
875 * snd_pcm_start
Takashi Iwaidf8db932005-09-07 13:38:19 +0200876 * @substream: the PCM substream instance
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 *
878 * Start all linked streams.
879 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100880int snd_pcm_start(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Takashi Iwai877211f2005-11-17 13:59:38 +0100882 return snd_pcm_action(&snd_pcm_action_start, substream,
883 SNDRV_PCM_STATE_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886/*
887 * stop callbacks
888 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100889static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Takashi Iwai877211f2005-11-17 13:59:38 +0100891 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
893 return -EBADFD;
894 runtime->trigger_master = substream;
895 return 0;
896}
897
Takashi Iwai877211f2005-11-17 13:59:38 +0100898static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 if (substream->runtime->trigger_master == substream &&
901 snd_pcm_running(substream))
902 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
903 return 0; /* unconditonally stop all substreams */
904}
905
Takashi Iwai877211f2005-11-17 13:59:38 +0100906static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Takashi Iwai877211f2005-11-17 13:59:38 +0100908 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (runtime->status->state != state) {
910 snd_pcm_trigger_tstamp(substream);
911 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100912 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
913 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 runtime->status->state = state;
915 snd_pcm_tick_set(substream, 0);
916 }
917 wake_up(&runtime->sleep);
918}
919
920static struct action_ops snd_pcm_action_stop = {
921 .pre_action = snd_pcm_pre_stop,
922 .do_action = snd_pcm_do_stop,
923 .post_action = snd_pcm_post_stop
924};
925
926/**
927 * snd_pcm_stop
Takashi Iwaidf8db932005-09-07 13:38:19 +0200928 * @substream: the PCM substream instance
929 * @state: PCM state after stopping the stream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 *
931 * Try to stop all running streams in the substream group.
932 * The state of each stream is changed to the given value after that unconditionally.
933 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100934int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
937}
938
939/**
940 * snd_pcm_drain_done
Takashi Iwaidf8db932005-09-07 13:38:19 +0200941 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 *
943 * Stop the DMA only when the given stream is playback.
944 * The state is changed to SETUP.
945 * Unlike snd_pcm_stop(), this affects only the given stream.
946 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100947int snd_pcm_drain_done(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Takashi Iwai877211f2005-11-17 13:59:38 +0100949 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
950 SNDRV_PCM_STATE_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953/*
954 * pause callbacks
955 */
Takashi Iwai877211f2005-11-17 13:59:38 +0100956static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Takashi Iwai877211f2005-11-17 13:59:38 +0100958 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
960 return -ENOSYS;
961 if (push) {
962 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
963 return -EBADFD;
964 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
965 return -EBADFD;
966 runtime->trigger_master = substream;
967 return 0;
968}
969
Takashi Iwai877211f2005-11-17 13:59:38 +0100970static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 if (substream->runtime->trigger_master != substream)
973 return 0;
974 return substream->ops->trigger(substream,
975 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
976 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
977}
978
Takashi Iwai877211f2005-11-17 13:59:38 +0100979static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 if (substream->runtime->trigger_master == substream)
982 substream->ops->trigger(substream,
983 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
984 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
985}
986
Takashi Iwai877211f2005-11-17 13:59:38 +0100987static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Takashi Iwai877211f2005-11-17 13:59:38 +0100989 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 snd_pcm_trigger_tstamp(substream);
991 if (push) {
992 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
993 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +0100994 snd_timer_notify(substream->timer,
995 SNDRV_TIMER_EVENT_MPAUSE,
996 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 snd_pcm_tick_set(substream, 0);
998 wake_up(&runtime->sleep);
999 } else {
1000 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
1001 if (runtime->sleep_min)
1002 snd_pcm_tick_prepare(substream);
1003 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001004 snd_timer_notify(substream->timer,
1005 SNDRV_TIMER_EVENT_MCONTINUE,
1006 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008}
1009
1010static struct action_ops snd_pcm_action_pause = {
1011 .pre_action = snd_pcm_pre_pause,
1012 .do_action = snd_pcm_do_pause,
1013 .undo_action = snd_pcm_undo_pause,
1014 .post_action = snd_pcm_post_pause
1015};
1016
1017/*
1018 * Push/release the pause for all linked streams.
1019 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001020static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
1022 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1023}
1024
1025#ifdef CONFIG_PM
1026/* suspend */
1027
Takashi Iwai877211f2005-11-17 13:59:38 +01001028static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Takashi Iwai877211f2005-11-17 13:59:38 +01001030 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1032 return -EBUSY;
1033 runtime->trigger_master = substream;
1034 return 0;
1035}
1036
Takashi Iwai877211f2005-11-17 13:59:38 +01001037static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Takashi Iwai877211f2005-11-17 13:59:38 +01001039 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (runtime->trigger_master != substream)
1041 return 0;
1042 if (! snd_pcm_running(substream))
1043 return 0;
1044 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1045 return 0; /* suspend unconditionally */
1046}
1047
Takashi Iwai877211f2005-11-17 13:59:38 +01001048static void snd_pcm_post_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 snd_pcm_trigger_tstamp(substream);
1052 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001053 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1054 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 runtime->status->suspended_state = runtime->status->state;
1056 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1057 snd_pcm_tick_set(substream, 0);
1058 wake_up(&runtime->sleep);
1059}
1060
1061static struct action_ops snd_pcm_action_suspend = {
1062 .pre_action = snd_pcm_pre_suspend,
1063 .do_action = snd_pcm_do_suspend,
1064 .post_action = snd_pcm_post_suspend
1065};
1066
1067/**
1068 * snd_pcm_suspend
Takashi Iwaidf8db932005-09-07 13:38:19 +02001069 * @substream: the PCM substream
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *
1071 * Trigger SUSPEND to all linked streams.
1072 * After this call, all streams are changed to SUSPENDED state.
1073 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001074int snd_pcm_suspend(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
1076 int err;
1077 unsigned long flags;
1078
Takashi Iwai603bf5242005-11-17 15:59:14 +01001079 if (! substream)
1080 return 0;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 snd_pcm_stream_lock_irqsave(substream, flags);
1083 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1084 snd_pcm_stream_unlock_irqrestore(substream, flags);
1085 return err;
1086}
1087
1088/**
1089 * snd_pcm_suspend_all
Takashi Iwaidf8db932005-09-07 13:38:19 +02001090 * @pcm: the PCM instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *
1092 * Trigger SUSPEND to all substreams in the given pcm.
1093 * After this call, all streams are changed to SUSPENDED state.
1094 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001095int snd_pcm_suspend_all(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Takashi Iwai877211f2005-11-17 13:59:38 +01001097 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 int stream, err = 0;
1099
Takashi Iwai603bf5242005-11-17 15:59:14 +01001100 if (! pcm)
1101 return 0;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 for (stream = 0; stream < 2; stream++) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001104 for (substream = pcm->streams[stream].substream;
1105 substream; substream = substream->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* FIXME: the open/close code should lock this as well */
1107 if (substream->runtime == NULL)
1108 continue;
1109 err = snd_pcm_suspend(substream);
1110 if (err < 0 && err != -EBUSY)
1111 return err;
1112 }
1113 }
1114 return 0;
1115}
1116
1117/* resume */
1118
Takashi Iwai877211f2005-11-17 13:59:38 +01001119static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Takashi Iwai877211f2005-11-17 13:59:38 +01001121 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1123 return -ENOSYS;
1124 runtime->trigger_master = substream;
1125 return 0;
1126}
1127
Takashi Iwai877211f2005-11-17 13:59:38 +01001128static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Takashi Iwai877211f2005-11-17 13:59:38 +01001130 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 if (runtime->trigger_master != substream)
1132 return 0;
1133 /* DMA not running previously? */
1134 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1135 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1136 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1137 return 0;
1138 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1139}
1140
Takashi Iwai877211f2005-11-17 13:59:38 +01001141static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 if (substream->runtime->trigger_master == substream &&
1144 snd_pcm_running(substream))
1145 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1146}
1147
Takashi Iwai877211f2005-11-17 13:59:38 +01001148static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Takashi Iwai877211f2005-11-17 13:59:38 +01001150 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 snd_pcm_trigger_tstamp(substream);
1152 if (substream->timer)
Takashi Iwai877211f2005-11-17 13:59:38 +01001153 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1154 &runtime->trigger_tstamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 runtime->status->state = runtime->status->suspended_state;
1156 if (runtime->sleep_min)
1157 snd_pcm_tick_prepare(substream);
1158}
1159
1160static struct action_ops snd_pcm_action_resume = {
1161 .pre_action = snd_pcm_pre_resume,
1162 .do_action = snd_pcm_do_resume,
1163 .undo_action = snd_pcm_undo_resume,
1164 .post_action = snd_pcm_post_resume
1165};
1166
Takashi Iwai877211f2005-11-17 13:59:38 +01001167static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Takashi Iwai877211f2005-11-17 13:59:38 +01001169 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 int res;
1171
1172 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001173 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1175 snd_power_unlock(card);
1176 return res;
1177}
1178
1179#else
1180
Takashi Iwai877211f2005-11-17 13:59:38 +01001181static int snd_pcm_resume(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 return -ENOSYS;
1184}
1185
1186#endif /* CONFIG_PM */
1187
1188/*
1189 * xrun ioctl
1190 *
1191 * Change the RUNNING stream(s) to XRUN state.
1192 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001193static int snd_pcm_xrun(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Takashi Iwai877211f2005-11-17 13:59:38 +01001195 struct snd_card *card = substream->pcm->card;
1196 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int result;
1198
1199 snd_power_lock(card);
1200 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001201 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (result < 0)
1203 goto _unlock;
1204 }
1205
1206 snd_pcm_stream_lock_irq(substream);
1207 switch (runtime->status->state) {
1208 case SNDRV_PCM_STATE_XRUN:
1209 result = 0; /* already there */
1210 break;
1211 case SNDRV_PCM_STATE_RUNNING:
1212 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1213 break;
1214 default:
1215 result = -EBADFD;
1216 }
1217 snd_pcm_stream_unlock_irq(substream);
1218 _unlock:
1219 snd_power_unlock(card);
1220 return result;
1221}
1222
1223/*
1224 * reset ioctl
1225 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001226static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227{
Takashi Iwai877211f2005-11-17 13:59:38 +01001228 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 switch (runtime->status->state) {
1230 case SNDRV_PCM_STATE_RUNNING:
1231 case SNDRV_PCM_STATE_PREPARED:
1232 case SNDRV_PCM_STATE_PAUSED:
1233 case SNDRV_PCM_STATE_SUSPENDED:
1234 return 0;
1235 default:
1236 return -EBADFD;
1237 }
1238}
1239
Takashi Iwai877211f2005-11-17 13:59:38 +01001240static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Takashi Iwai877211f2005-11-17 13:59:38 +01001242 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1244 if (err < 0)
1245 return err;
1246 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1247 runtime->hw_ptr_base = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01001248 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1249 runtime->status->hw_ptr % runtime->period_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 runtime->silence_start = runtime->status->hw_ptr;
1251 runtime->silence_filled = 0;
1252 return 0;
1253}
1254
Takashi Iwai877211f2005-11-17 13:59:38 +01001255static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Takashi Iwai877211f2005-11-17 13:59:38 +01001257 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 runtime->control->appl_ptr = runtime->status->hw_ptr;
1259 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1260 runtime->silence_size > 0)
1261 snd_pcm_playback_silence(substream, ULONG_MAX);
1262}
1263
1264static struct action_ops snd_pcm_action_reset = {
1265 .pre_action = snd_pcm_pre_reset,
1266 .do_action = snd_pcm_do_reset,
1267 .post_action = snd_pcm_post_reset
1268};
1269
Takashi Iwai877211f2005-11-17 13:59:38 +01001270static int snd_pcm_reset(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
1272 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1273}
1274
1275/*
1276 * prepare ioctl
1277 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001278static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Takashi Iwai877211f2005-11-17 13:59:38 +01001280 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1282 return -EBADFD;
1283 if (snd_pcm_running(substream))
1284 return -EBUSY;
1285 return 0;
1286}
1287
Takashi Iwai877211f2005-11-17 13:59:38 +01001288static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 int err;
1291 err = substream->ops->prepare(substream);
1292 if (err < 0)
1293 return err;
1294 return snd_pcm_do_reset(substream, 0);
1295}
1296
Takashi Iwai877211f2005-11-17 13:59:38 +01001297static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Takashi Iwai877211f2005-11-17 13:59:38 +01001299 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 runtime->control->appl_ptr = runtime->status->hw_ptr;
1301 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1302}
1303
1304static struct action_ops snd_pcm_action_prepare = {
1305 .pre_action = snd_pcm_pre_prepare,
1306 .do_action = snd_pcm_do_prepare,
1307 .post_action = snd_pcm_post_prepare
1308};
1309
1310/**
1311 * snd_pcm_prepare
Takashi Iwaidf8db932005-09-07 13:38:19 +02001312 * @substream: the PCM substream instance
1313 *
1314 * Prepare the PCM substream to be triggerable.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001316int snd_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 int res;
Takashi Iwai877211f2005-11-17 13:59:38 +01001319 struct snd_card *card = substream->pcm->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 snd_power_lock(card);
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001322 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0)) >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1324 snd_power_unlock(card);
1325 return res;
1326}
1327
1328/*
1329 * drain ioctl
1330 */
1331
Takashi Iwai877211f2005-11-17 13:59:38 +01001332static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
1334 if (substream->ffile->f_flags & O_NONBLOCK)
1335 return -EAGAIN;
1336 substream->runtime->trigger_master = substream;
1337 return 0;
1338}
1339
Takashi Iwai877211f2005-11-17 13:59:38 +01001340static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
Takashi Iwai877211f2005-11-17 13:59:38 +01001342 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1344 switch (runtime->status->state) {
1345 case SNDRV_PCM_STATE_PREPARED:
1346 /* start playback stream if possible */
1347 if (! snd_pcm_playback_empty(substream)) {
1348 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1349 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1350 }
1351 break;
1352 case SNDRV_PCM_STATE_RUNNING:
1353 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1354 break;
1355 default:
1356 break;
1357 }
1358 } else {
1359 /* stop running stream */
1360 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1361 int state = snd_pcm_capture_avail(runtime) > 0 ?
1362 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1363 snd_pcm_do_stop(substream, state);
1364 snd_pcm_post_stop(substream, state);
1365 }
1366 }
1367 return 0;
1368}
1369
Takashi Iwai877211f2005-11-17 13:59:38 +01001370static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
1372}
1373
1374static struct action_ops snd_pcm_action_drain_init = {
1375 .pre_action = snd_pcm_pre_drain_init,
1376 .do_action = snd_pcm_do_drain_init,
1377 .post_action = snd_pcm_post_drain_init
1378};
1379
1380struct drain_rec {
Takashi Iwai877211f2005-11-17 13:59:38 +01001381 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 wait_queue_t wait;
1383 snd_pcm_uframes_t stop_threshold;
1384};
1385
Takashi Iwai877211f2005-11-17 13:59:38 +01001386static int snd_pcm_drop(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388/*
1389 * Drain the stream(s).
1390 * When the substream is linked, sync until the draining of all playback streams
1391 * is finished.
1392 * After this call, all streams are supposed to be either SETUP or DRAINING
1393 * (capture only) state.
1394 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001395static int snd_pcm_drain(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Takashi Iwai877211f2005-11-17 13:59:38 +01001397 struct snd_card *card;
1398 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct list_head *pos;
1400 int result = 0;
1401 int i, num_drecs;
1402 struct drain_rec *drec, drec_tmp, *d;
1403
1404 snd_assert(substream != NULL, return -ENXIO);
1405 card = substream->pcm->card;
1406 runtime = substream->runtime;
1407
1408 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1409 return -EBADFD;
1410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 snd_power_lock(card);
1412 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001413 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001414 if (result < 0) {
1415 snd_power_unlock(card);
1416 return result;
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
1420 /* allocate temporary record for drain sync */
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001421 down_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (snd_pcm_stream_linked(substream)) {
1423 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1424 if (! drec) {
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001425 up_read(&snd_pcm_link_rwsem);
1426 snd_power_unlock(card);
1427 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 } else
1430 drec = &drec_tmp;
1431
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001432 /* count only playback streams */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 num_drecs = 0;
1434 snd_pcm_group_for_each(pos, substream) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001435 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 runtime = s->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1438 d = &drec[num_drecs++];
1439 d->substream = s;
1440 init_waitqueue_entry(&d->wait, current);
1441 add_wait_queue(&runtime->sleep, &d->wait);
1442 /* stop_threshold fixup to avoid endless loop when
1443 * stop_threshold > buffer_size
1444 */
1445 d->stop_threshold = runtime->stop_threshold;
1446 if (runtime->stop_threshold > runtime->buffer_size)
1447 runtime->stop_threshold = runtime->buffer_size;
1448 }
1449 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001450 up_read(&snd_pcm_link_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (! num_drecs)
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001452 goto _error;
1453
1454 snd_pcm_stream_lock_irq(substream);
1455 /* resume pause */
1456 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1457 snd_pcm_pause(substream, 0);
1458
1459 /* pre-start/stop - all running streams are changed to DRAINING state */
1460 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1461 if (result < 0) {
1462 snd_pcm_stream_unlock_irq(substream);
1463 goto _error;
1464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 for (;;) {
1467 long tout;
1468 if (signal_pending(current)) {
1469 result = -ERESTARTSYS;
1470 break;
1471 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001472 /* all finished? */
1473 for (i = 0; i < num_drecs; i++) {
1474 runtime = drec[i].substream->runtime;
1475 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1476 break;
1477 }
1478 if (i == num_drecs)
1479 break; /* yes, all drained */
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 set_current_state(TASK_INTERRUPTIBLE);
1482 snd_pcm_stream_unlock_irq(substream);
1483 snd_power_unlock(card);
1484 tout = schedule_timeout(10 * HZ);
1485 snd_power_lock(card);
1486 snd_pcm_stream_lock_irq(substream);
1487 if (tout == 0) {
1488 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1489 result = -ESTRPIPE;
1490 else {
1491 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1492 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1493 result = -EIO;
1494 }
1495 break;
1496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
Takashi Iwai21cb2a22005-05-31 14:35:31 +02001498
1499 snd_pcm_stream_unlock_irq(substream);
1500
1501 _error:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 for (i = 0; i < num_drecs; i++) {
1503 d = &drec[i];
1504 runtime = d->substream->runtime;
1505 remove_wait_queue(&runtime->sleep, &d->wait);
1506 runtime->stop_threshold = d->stop_threshold;
1507 }
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (drec != &drec_tmp)
1510 kfree(drec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 snd_power_unlock(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 return result;
1514}
1515
1516/*
1517 * drop ioctl
1518 *
1519 * Immediately put all linked substreams into SETUP state.
1520 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001521static int snd_pcm_drop(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Takashi Iwai877211f2005-11-17 13:59:38 +01001523 struct snd_pcm_runtime *runtime;
1524 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int result = 0;
1526
1527 snd_assert(substream != NULL, return -ENXIO);
1528 runtime = substream->runtime;
1529 card = substream->pcm->card;
1530
1531 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1532 return -EBADFD;
1533
1534 snd_power_lock(card);
1535 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
Takashi Iwaicbac4b02006-03-27 12:38:07 +02001536 result = snd_power_wait(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (result < 0)
1538 goto _unlock;
1539 }
1540
1541 snd_pcm_stream_lock_irq(substream);
1542 /* resume pause */
1543 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1544 snd_pcm_pause(substream, 0);
1545
1546 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1547 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1548 snd_pcm_stream_unlock_irq(substream);
1549 _unlock:
1550 snd_power_unlock(card);
1551 return result;
1552}
1553
1554
1555/* WARNING: Don't forget to fput back the file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static struct file *snd_pcm_file_fd(int fd)
1557{
1558 struct file *file;
1559 struct inode *inode;
Clemens Ladischf87135f2005-11-20 14:06:59 +01001560 unsigned int minor;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 file = fget(fd);
1563 if (!file)
1564 return NULL;
1565 inode = file->f_dentry->d_inode;
1566 if (!S_ISCHR(inode->i_mode) ||
1567 imajor(inode) != snd_major) {
1568 fput(file);
1569 return NULL;
1570 }
1571 minor = iminor(inode);
Clemens Ladischf87135f2005-11-20 14:06:59 +01001572 if (!snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_PLAYBACK) &&
1573 !snd_lookup_minor_data(minor, SNDRV_DEVICE_TYPE_PCM_CAPTURE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 fput(file);
1575 return NULL;
1576 }
1577 return file;
1578}
1579
1580/*
1581 * PCM link handling
1582 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001583static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 int res = 0;
1586 struct file *file;
Takashi Iwai877211f2005-11-17 13:59:38 +01001587 struct snd_pcm_file *pcm_file;
1588 struct snd_pcm_substream *substream1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 file = snd_pcm_file_fd(fd);
1591 if (!file)
1592 return -EBADFD;
1593 pcm_file = file->private_data;
1594 substream1 = pcm_file->substream;
1595 down_write(&snd_pcm_link_rwsem);
1596 write_lock_irq(&snd_pcm_link_rwlock);
1597 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1598 substream->runtime->status->state != substream1->runtime->status->state) {
1599 res = -EBADFD;
1600 goto _end;
1601 }
1602 if (snd_pcm_stream_linked(substream1)) {
1603 res = -EALREADY;
1604 goto _end;
1605 }
1606 if (!snd_pcm_stream_linked(substream)) {
Takashi Iwai877211f2005-11-17 13:59:38 +01001607 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (substream->group == NULL) {
1609 res = -ENOMEM;
1610 goto _end;
1611 }
1612 spin_lock_init(&substream->group->lock);
1613 INIT_LIST_HEAD(&substream->group->substreams);
1614 list_add_tail(&substream->link_list, &substream->group->substreams);
1615 substream->group->count = 1;
1616 }
1617 list_add_tail(&substream1->link_list, &substream->group->substreams);
1618 substream->group->count++;
1619 substream1->group = substream->group;
1620 _end:
1621 write_unlock_irq(&snd_pcm_link_rwlock);
1622 up_write(&snd_pcm_link_rwsem);
1623 fput(file);
1624 return res;
1625}
1626
Takashi Iwai877211f2005-11-17 13:59:38 +01001627static void relink_to_local(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 substream->group = &substream->self_group;
1630 INIT_LIST_HEAD(&substream->self_group.substreams);
1631 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1632}
1633
Takashi Iwai877211f2005-11-17 13:59:38 +01001634static int snd_pcm_unlink(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct list_head *pos;
1637 int res = 0;
1638
1639 down_write(&snd_pcm_link_rwsem);
1640 write_lock_irq(&snd_pcm_link_rwlock);
1641 if (!snd_pcm_stream_linked(substream)) {
1642 res = -EALREADY;
1643 goto _end;
1644 }
1645 list_del(&substream->link_list);
1646 substream->group->count--;
1647 if (substream->group->count == 1) { /* detach the last stream, too */
1648 snd_pcm_group_for_each(pos, substream) {
1649 relink_to_local(snd_pcm_group_substream_entry(pos));
1650 break;
1651 }
1652 kfree(substream->group);
1653 }
1654 relink_to_local(substream);
1655 _end:
1656 write_unlock_irq(&snd_pcm_link_rwlock);
1657 up_write(&snd_pcm_link_rwsem);
1658 return res;
1659}
1660
1661/*
1662 * hw configurator
1663 */
Takashi Iwai877211f2005-11-17 13:59:38 +01001664static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1665 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
Takashi Iwai877211f2005-11-17 13:59:38 +01001667 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1669 hw_param_interval_c(params, rule->deps[1]), &t);
1670 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1671}
1672
Takashi Iwai877211f2005-11-17 13:59:38 +01001673static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1674 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Takashi Iwai877211f2005-11-17 13:59:38 +01001676 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1678 hw_param_interval_c(params, rule->deps[1]), &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1680}
1681
Takashi Iwai877211f2005-11-17 13:59:38 +01001682static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Takashi Iwai877211f2005-11-17 13:59:38 +01001685 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1687 hw_param_interval_c(params, rule->deps[1]),
1688 (unsigned long) rule->private, &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690}
1691
Takashi Iwai877211f2005-11-17 13:59:38 +01001692static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Takashi Iwai877211f2005-11-17 13:59:38 +01001695 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1697 (unsigned long) rule->private,
1698 hw_param_interval_c(params, rule->deps[1]), &t);
1699 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1700}
1701
Takashi Iwai877211f2005-11-17 13:59:38 +01001702static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1703 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 unsigned int k;
Takashi Iwai877211f2005-11-17 13:59:38 +01001706 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1707 struct snd_mask m;
1708 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 snd_mask_any(&m);
1710 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1711 int bits;
1712 if (! snd_mask_test(mask, k))
1713 continue;
1714 bits = snd_pcm_format_physical_width(k);
1715 if (bits <= 0)
1716 continue; /* ignore invalid formats */
1717 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1718 snd_mask_reset(&m, k);
1719 }
1720 return snd_mask_refine(mask, &m);
1721}
1722
Takashi Iwai877211f2005-11-17 13:59:38 +01001723static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1724 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Takashi Iwai877211f2005-11-17 13:59:38 +01001726 struct snd_interval t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 unsigned int k;
1728 t.min = UINT_MAX;
1729 t.max = 0;
1730 t.openmin = 0;
1731 t.openmax = 0;
1732 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1733 int bits;
1734 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1735 continue;
1736 bits = snd_pcm_format_physical_width(k);
1737 if (bits <= 0)
1738 continue; /* ignore invalid formats */
1739 if (t.min > (unsigned)bits)
1740 t.min = bits;
1741 if (t.max < (unsigned)bits)
1742 t.max = bits;
1743 }
1744 t.integer = 1;
1745 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1746}
1747
1748#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1749#error "Change this table"
1750#endif
1751
1752static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1753 48000, 64000, 88200, 96000, 176400, 192000 };
1754
Takashi Iwai877211f2005-11-17 13:59:38 +01001755static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1756 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Takashi Iwai877211f2005-11-17 13:59:38 +01001758 struct snd_pcm_hardware *hw = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 return snd_interval_list(hw_param_interval(params, rule->var),
1760 ARRAY_SIZE(rates), rates, hw->rates);
1761}
1762
Takashi Iwai877211f2005-11-17 13:59:38 +01001763static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1764 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Takashi Iwai877211f2005-11-17 13:59:38 +01001766 struct snd_interval t;
1767 struct snd_pcm_substream *substream = rule->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 t.min = 0;
1769 t.max = substream->buffer_bytes_max;
1770 t.openmin = 0;
1771 t.openmax = 0;
1772 t.integer = 1;
1773 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1774}
1775
Takashi Iwai877211f2005-11-17 13:59:38 +01001776int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Takashi Iwai877211f2005-11-17 13:59:38 +01001778 struct snd_pcm_runtime *runtime = substream->runtime;
1779 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 int k, err;
1781
1782 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1783 snd_mask_any(constrs_mask(constrs, k));
1784 }
1785
1786 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1787 snd_interval_any(constrs_interval(constrs, k));
1788 }
1789
1790 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1791 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1792 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1793 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1794 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1795
1796 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1797 snd_pcm_hw_rule_format, NULL,
1798 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1799 if (err < 0)
1800 return err;
1801 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1802 snd_pcm_hw_rule_sample_bits, NULL,
1803 SNDRV_PCM_HW_PARAM_FORMAT,
1804 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1805 if (err < 0)
1806 return err;
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1808 snd_pcm_hw_rule_div, NULL,
1809 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1810 if (err < 0)
1811 return err;
1812 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1813 snd_pcm_hw_rule_mul, NULL,
1814 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1815 if (err < 0)
1816 return err;
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1818 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1819 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1820 if (err < 0)
1821 return err;
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1823 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1824 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1825 if (err < 0)
1826 return err;
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1828 snd_pcm_hw_rule_div, NULL,
1829 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1830 if (err < 0)
1831 return err;
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1833 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1834 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1835 if (err < 0)
1836 return err;
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1838 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1839 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1840 if (err < 0)
1841 return err;
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1843 snd_pcm_hw_rule_div, NULL,
1844 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1845 if (err < 0)
1846 return err;
1847 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1848 snd_pcm_hw_rule_div, NULL,
1849 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1850 if (err < 0)
1851 return err;
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1853 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1854 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1855 if (err < 0)
1856 return err;
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1858 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1859 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1860 if (err < 0)
1861 return err;
1862 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1863 snd_pcm_hw_rule_mul, NULL,
1864 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1865 if (err < 0)
1866 return err;
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1868 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1869 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1870 if (err < 0)
1871 return err;
1872 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1873 snd_pcm_hw_rule_muldivk, (void*) 1000000,
1874 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1875 if (err < 0)
1876 return err;
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1878 snd_pcm_hw_rule_muldivk, (void*) 8,
1879 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1880 if (err < 0)
1881 return err;
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1883 snd_pcm_hw_rule_muldivk, (void*) 8,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1885 if (err < 0)
1886 return err;
1887 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1888 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1889 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1890 if (err < 0)
1891 return err;
1892 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1893 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1894 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1895 if (err < 0)
1896 return err;
1897 return 0;
1898}
1899
Takashi Iwai877211f2005-11-17 13:59:38 +01001900int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Takashi Iwai877211f2005-11-17 13:59:38 +01001902 struct snd_pcm_runtime *runtime = substream->runtime;
1903 struct snd_pcm_hardware *hw = &runtime->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 int err;
1905 unsigned int mask = 0;
1906
1907 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1908 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1909 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1910 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1911 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1912 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1913 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1914 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1915 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1916 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1917 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1918 }
1919 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1920 snd_assert(err >= 0, return -EINVAL);
1921
1922 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1923 snd_assert(err >= 0, return -EINVAL);
1924
1925 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1926 snd_assert(err >= 0, return -EINVAL);
1927
1928 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1929 hw->channels_min, hw->channels_max);
1930 snd_assert(err >= 0, return -EINVAL);
1931
1932 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1933 hw->rate_min, hw->rate_max);
1934 snd_assert(err >= 0, return -EINVAL);
1935
1936 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1937 hw->period_bytes_min, hw->period_bytes_max);
1938 snd_assert(err >= 0, return -EINVAL);
1939
1940 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1941 hw->periods_min, hw->periods_max);
1942 snd_assert(err >= 0, return -EINVAL);
1943
1944 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1945 hw->period_bytes_min, hw->buffer_bytes_max);
1946 snd_assert(err >= 0, return -EINVAL);
1947
1948 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1949 snd_pcm_hw_rule_buffer_bytes_max, substream,
1950 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1951 if (err < 0)
1952 return err;
1953
1954 /* FIXME: remove */
1955 if (runtime->dma_bytes) {
1956 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1957 snd_assert(err >= 0, return -EINVAL);
1958 }
1959
1960 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1961 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1962 snd_pcm_hw_rule_rate, hw,
1963 SNDRV_PCM_HW_PARAM_RATE, -1);
1964 if (err < 0)
1965 return err;
1966 }
1967
1968 /* FIXME: this belong to lowlevel */
1969 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1970 1000000 / HZ, 1000000 / HZ);
1971 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1972
1973 return 0;
1974}
1975
Takashi Iwai877211f2005-11-17 13:59:38 +01001976static void snd_pcm_add_file(struct snd_pcm_str *str,
1977 struct snd_pcm_file *pcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 pcm_file->next = str->files;
1980 str->files = pcm_file;
1981}
1982
Takashi Iwai877211f2005-11-17 13:59:38 +01001983static void snd_pcm_remove_file(struct snd_pcm_str *str,
1984 struct snd_pcm_file *pcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Takashi Iwai877211f2005-11-17 13:59:38 +01001986 struct snd_pcm_file * pcm_file1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (str->files == pcm_file) {
1988 str->files = pcm_file->next;
1989 } else {
1990 pcm_file1 = str->files;
1991 while (pcm_file1 && pcm_file1->next != pcm_file)
1992 pcm_file1 = pcm_file1->next;
1993 if (pcm_file1 != NULL)
1994 pcm_file1->next = pcm_file->next;
1995 }
1996}
1997
Takashi Iwai877211f2005-11-17 13:59:38 +01001998static int snd_pcm_release_file(struct snd_pcm_file * pcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Takashi Iwai877211f2005-11-17 13:59:38 +01002000 struct snd_pcm_substream *substream;
2001 struct snd_pcm_runtime *runtime;
2002 struct snd_pcm_str * str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 snd_assert(pcm_file != NULL, return -ENXIO);
2005 substream = pcm_file->substream;
2006 snd_assert(substream != NULL, return -ENXIO);
2007 runtime = substream->runtime;
2008 str = substream->pstr;
2009 snd_pcm_unlink(substream);
Karsten Wiese443feb82005-08-10 11:18:19 +02002010 if (substream->ffile != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (substream->ops->hw_free != NULL)
2012 substream->ops->hw_free(substream);
2013 substream->ops->close(substream);
Karsten Wiese443feb82005-08-10 11:18:19 +02002014 substream->ffile = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 snd_pcm_remove_file(str, pcm_file);
2017 snd_pcm_release_substream(substream);
2018 kfree(pcm_file);
2019 return 0;
2020}
2021
2022static int snd_pcm_open_file(struct file *file,
Takashi Iwai877211f2005-11-17 13:59:38 +01002023 struct snd_pcm *pcm,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int stream,
Takashi Iwai877211f2005-11-17 13:59:38 +01002025 struct snd_pcm_file **rpcm_file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 int err = 0;
Takashi Iwai877211f2005-11-17 13:59:38 +01002028 struct snd_pcm_file *pcm_file;
2029 struct snd_pcm_substream *substream;
2030 struct snd_pcm_str *str;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 snd_assert(rpcm_file != NULL, return -EINVAL);
2033 *rpcm_file = NULL;
2034
Takashi Iwaica2c0962005-09-09 14:20:23 +02002035 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (pcm_file == NULL) {
2037 return -ENOMEM;
2038 }
2039
2040 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2041 kfree(pcm_file);
2042 return err;
2043 }
2044
2045 str = substream->pstr;
2046 substream->file = pcm_file;
2047 substream->no_mmap_ctrl = 0;
2048
2049 pcm_file->substream = substream;
2050
2051 snd_pcm_add_file(str, pcm_file);
2052
2053 err = snd_pcm_hw_constraints_init(substream);
2054 if (err < 0) {
2055 snd_printd("snd_pcm_hw_constraints_init failed\n");
2056 snd_pcm_release_file(pcm_file);
2057 return err;
2058 }
2059
2060 if ((err = substream->ops->open(substream)) < 0) {
2061 snd_pcm_release_file(pcm_file);
2062 return err;
2063 }
Karsten Wiese443feb82005-08-10 11:18:19 +02002064 substream->ffile = file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 err = snd_pcm_hw_constraints_complete(substream);
2067 if (err < 0) {
2068 snd_printd("snd_pcm_hw_constraints_complete failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 snd_pcm_release_file(pcm_file);
2070 return err;
2071 }
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 file->private_data = pcm_file;
2074 *rpcm_file = pcm_file;
2075 return 0;
2076}
2077
Clemens Ladischf87135f2005-11-20 14:06:59 +01002078static int snd_pcm_playback_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
Takashi Iwai877211f2005-11-17 13:59:38 +01002080 struct snd_pcm *pcm;
Clemens Ladischf87135f2005-11-20 14:06:59 +01002081
2082 pcm = snd_lookup_minor_data(iminor(inode),
2083 SNDRV_DEVICE_TYPE_PCM_PLAYBACK);
2084 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_PLAYBACK);
2085}
2086
2087static int snd_pcm_capture_open(struct inode *inode, struct file *file)
2088{
2089 struct snd_pcm *pcm;
2090
2091 pcm = snd_lookup_minor_data(iminor(inode),
2092 SNDRV_DEVICE_TYPE_PCM_CAPTURE);
2093 return snd_pcm_open(file, pcm, SNDRV_PCM_STREAM_CAPTURE);
2094}
2095
2096static int snd_pcm_open(struct file *file, struct snd_pcm *pcm, int stream)
2097{
2098 int err;
Takashi Iwai877211f2005-11-17 13:59:38 +01002099 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 wait_queue_t wait;
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 if (pcm == NULL) {
2103 err = -ENODEV;
2104 goto __error1;
2105 }
2106 err = snd_card_file_add(pcm->card, file);
2107 if (err < 0)
2108 goto __error1;
2109 if (!try_module_get(pcm->card->module)) {
2110 err = -EFAULT;
2111 goto __error2;
2112 }
2113 init_waitqueue_entry(&wait, current);
2114 add_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002115 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 while (1) {
Clemens Ladischf87135f2005-11-20 14:06:59 +01002117 err = snd_pcm_open_file(file, pcm, stream, &pcm_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (err >= 0)
2119 break;
2120 if (err == -EAGAIN) {
2121 if (file->f_flags & O_NONBLOCK) {
2122 err = -EBUSY;
2123 break;
2124 }
2125 } else
2126 break;
2127 set_current_state(TASK_INTERRUPTIBLE);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002128 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 schedule();
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002130 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (signal_pending(current)) {
2132 err = -ERESTARTSYS;
2133 break;
2134 }
2135 }
2136 remove_wait_queue(&pcm->open_wait, &wait);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002137 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (err < 0)
2139 goto __error;
2140 return err;
2141
2142 __error:
2143 module_put(pcm->card->module);
2144 __error2:
2145 snd_card_file_remove(pcm->card, file);
2146 __error1:
2147 return err;
2148}
2149
2150static int snd_pcm_release(struct inode *inode, struct file *file)
2151{
Takashi Iwai877211f2005-11-17 13:59:38 +01002152 struct snd_pcm *pcm;
2153 struct snd_pcm_substream *substream;
2154 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 pcm_file = file->private_data;
2157 substream = pcm_file->substream;
2158 snd_assert(substream != NULL, return -ENXIO);
2159 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2160 pcm = substream->pcm;
2161 snd_pcm_drop(substream);
2162 fasync_helper(-1, file, 0, &substream->runtime->fasync);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002163 mutex_lock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 snd_pcm_release_file(pcm_file);
Ingo Molnar1a60d4c2006-01-16 16:29:08 +01002165 mutex_unlock(&pcm->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 wake_up(&pcm->open_wait);
2167 module_put(pcm->card->module);
2168 snd_card_file_remove(pcm->card, file);
2169 return 0;
2170}
2171
Takashi Iwai877211f2005-11-17 13:59:38 +01002172static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2173 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Takashi Iwai877211f2005-11-17 13:59:38 +01002175 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 snd_pcm_sframes_t appl_ptr;
2177 snd_pcm_sframes_t ret;
2178 snd_pcm_sframes_t hw_avail;
2179
2180 if (frames == 0)
2181 return 0;
2182
2183 snd_pcm_stream_lock_irq(substream);
2184 switch (runtime->status->state) {
2185 case SNDRV_PCM_STATE_PREPARED:
2186 break;
2187 case SNDRV_PCM_STATE_DRAINING:
2188 case SNDRV_PCM_STATE_RUNNING:
2189 if (snd_pcm_update_hw_ptr(substream) >= 0)
2190 break;
2191 /* Fall through */
2192 case SNDRV_PCM_STATE_XRUN:
2193 ret = -EPIPE;
2194 goto __end;
2195 default:
2196 ret = -EBADFD;
2197 goto __end;
2198 }
2199
2200 hw_avail = snd_pcm_playback_hw_avail(runtime);
2201 if (hw_avail <= 0) {
2202 ret = 0;
2203 goto __end;
2204 }
2205 if (frames > (snd_pcm_uframes_t)hw_avail)
2206 frames = hw_avail;
2207 else
2208 frames -= frames % runtime->xfer_align;
2209 appl_ptr = runtime->control->appl_ptr - frames;
2210 if (appl_ptr < 0)
2211 appl_ptr += runtime->boundary;
2212 runtime->control->appl_ptr = appl_ptr;
2213 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2214 runtime->sleep_min)
2215 snd_pcm_tick_prepare(substream);
2216 ret = frames;
2217 __end:
2218 snd_pcm_stream_unlock_irq(substream);
2219 return ret;
2220}
2221
Takashi Iwai877211f2005-11-17 13:59:38 +01002222static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2223 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Takashi Iwai877211f2005-11-17 13:59:38 +01002225 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 snd_pcm_sframes_t appl_ptr;
2227 snd_pcm_sframes_t ret;
2228 snd_pcm_sframes_t hw_avail;
2229
2230 if (frames == 0)
2231 return 0;
2232
2233 snd_pcm_stream_lock_irq(substream);
2234 switch (runtime->status->state) {
2235 case SNDRV_PCM_STATE_PREPARED:
2236 case SNDRV_PCM_STATE_DRAINING:
2237 break;
2238 case SNDRV_PCM_STATE_RUNNING:
2239 if (snd_pcm_update_hw_ptr(substream) >= 0)
2240 break;
2241 /* Fall through */
2242 case SNDRV_PCM_STATE_XRUN:
2243 ret = -EPIPE;
2244 goto __end;
2245 default:
2246 ret = -EBADFD;
2247 goto __end;
2248 }
2249
2250 hw_avail = snd_pcm_capture_hw_avail(runtime);
2251 if (hw_avail <= 0) {
2252 ret = 0;
2253 goto __end;
2254 }
2255 if (frames > (snd_pcm_uframes_t)hw_avail)
2256 frames = hw_avail;
2257 else
2258 frames -= frames % runtime->xfer_align;
2259 appl_ptr = runtime->control->appl_ptr - frames;
2260 if (appl_ptr < 0)
2261 appl_ptr += runtime->boundary;
2262 runtime->control->appl_ptr = appl_ptr;
2263 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2264 runtime->sleep_min)
2265 snd_pcm_tick_prepare(substream);
2266 ret = frames;
2267 __end:
2268 snd_pcm_stream_unlock_irq(substream);
2269 return ret;
2270}
2271
Takashi Iwai877211f2005-11-17 13:59:38 +01002272static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2273 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
Takashi Iwai877211f2005-11-17 13:59:38 +01002275 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 snd_pcm_sframes_t appl_ptr;
2277 snd_pcm_sframes_t ret;
2278 snd_pcm_sframes_t avail;
2279
2280 if (frames == 0)
2281 return 0;
2282
2283 snd_pcm_stream_lock_irq(substream);
2284 switch (runtime->status->state) {
2285 case SNDRV_PCM_STATE_PREPARED:
2286 case SNDRV_PCM_STATE_PAUSED:
2287 break;
2288 case SNDRV_PCM_STATE_DRAINING:
2289 case SNDRV_PCM_STATE_RUNNING:
2290 if (snd_pcm_update_hw_ptr(substream) >= 0)
2291 break;
2292 /* Fall through */
2293 case SNDRV_PCM_STATE_XRUN:
2294 ret = -EPIPE;
2295 goto __end;
2296 default:
2297 ret = -EBADFD;
2298 goto __end;
2299 }
2300
2301 avail = snd_pcm_playback_avail(runtime);
2302 if (avail <= 0) {
2303 ret = 0;
2304 goto __end;
2305 }
2306 if (frames > (snd_pcm_uframes_t)avail)
2307 frames = avail;
2308 else
2309 frames -= frames % runtime->xfer_align;
2310 appl_ptr = runtime->control->appl_ptr + frames;
2311 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2312 appl_ptr -= runtime->boundary;
2313 runtime->control->appl_ptr = appl_ptr;
2314 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2315 runtime->sleep_min)
2316 snd_pcm_tick_prepare(substream);
2317 ret = frames;
2318 __end:
2319 snd_pcm_stream_unlock_irq(substream);
2320 return ret;
2321}
2322
Takashi Iwai877211f2005-11-17 13:59:38 +01002323static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2324 snd_pcm_uframes_t frames)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Takashi Iwai877211f2005-11-17 13:59:38 +01002326 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 snd_pcm_sframes_t appl_ptr;
2328 snd_pcm_sframes_t ret;
2329 snd_pcm_sframes_t avail;
2330
2331 if (frames == 0)
2332 return 0;
2333
2334 snd_pcm_stream_lock_irq(substream);
2335 switch (runtime->status->state) {
2336 case SNDRV_PCM_STATE_PREPARED:
2337 case SNDRV_PCM_STATE_DRAINING:
2338 case SNDRV_PCM_STATE_PAUSED:
2339 break;
2340 case SNDRV_PCM_STATE_RUNNING:
2341 if (snd_pcm_update_hw_ptr(substream) >= 0)
2342 break;
2343 /* Fall through */
2344 case SNDRV_PCM_STATE_XRUN:
2345 ret = -EPIPE;
2346 goto __end;
2347 default:
2348 ret = -EBADFD;
2349 goto __end;
2350 }
2351
2352 avail = snd_pcm_capture_avail(runtime);
2353 if (avail <= 0) {
2354 ret = 0;
2355 goto __end;
2356 }
2357 if (frames > (snd_pcm_uframes_t)avail)
2358 frames = avail;
2359 else
2360 frames -= frames % runtime->xfer_align;
2361 appl_ptr = runtime->control->appl_ptr + frames;
2362 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2363 appl_ptr -= runtime->boundary;
2364 runtime->control->appl_ptr = appl_ptr;
2365 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2366 runtime->sleep_min)
2367 snd_pcm_tick_prepare(substream);
2368 ret = frames;
2369 __end:
2370 snd_pcm_stream_unlock_irq(substream);
2371 return ret;
2372}
2373
Takashi Iwai877211f2005-11-17 13:59:38 +01002374static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Takashi Iwai877211f2005-11-17 13:59:38 +01002376 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 int err;
2378
2379 snd_pcm_stream_lock_irq(substream);
2380 switch (runtime->status->state) {
2381 case SNDRV_PCM_STATE_DRAINING:
2382 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2383 goto __badfd;
2384 case SNDRV_PCM_STATE_RUNNING:
2385 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2386 break;
2387 /* Fall through */
2388 case SNDRV_PCM_STATE_PREPARED:
2389 case SNDRV_PCM_STATE_SUSPENDED:
2390 err = 0;
2391 break;
2392 case SNDRV_PCM_STATE_XRUN:
2393 err = -EPIPE;
2394 break;
2395 default:
2396 __badfd:
2397 err = -EBADFD;
2398 break;
2399 }
2400 snd_pcm_stream_unlock_irq(substream);
2401 return err;
2402}
2403
Takashi Iwai877211f2005-11-17 13:59:38 +01002404static int snd_pcm_delay(struct snd_pcm_substream *substream,
2405 snd_pcm_sframes_t __user *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406{
Takashi Iwai877211f2005-11-17 13:59:38 +01002407 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int err;
2409 snd_pcm_sframes_t n = 0;
2410
2411 snd_pcm_stream_lock_irq(substream);
2412 switch (runtime->status->state) {
2413 case SNDRV_PCM_STATE_DRAINING:
2414 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2415 goto __badfd;
2416 case SNDRV_PCM_STATE_RUNNING:
2417 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2418 break;
2419 /* Fall through */
2420 case SNDRV_PCM_STATE_PREPARED:
2421 case SNDRV_PCM_STATE_SUSPENDED:
2422 err = 0;
2423 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2424 n = snd_pcm_playback_hw_avail(runtime);
2425 else
2426 n = snd_pcm_capture_avail(runtime);
2427 break;
2428 case SNDRV_PCM_STATE_XRUN:
2429 err = -EPIPE;
2430 break;
2431 default:
2432 __badfd:
2433 err = -EBADFD;
2434 break;
2435 }
2436 snd_pcm_stream_unlock_irq(substream);
2437 if (!err)
2438 if (put_user(n, res))
2439 err = -EFAULT;
2440 return err;
2441}
2442
Takashi Iwai877211f2005-11-17 13:59:38 +01002443static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2444 struct snd_pcm_sync_ptr __user *_sync_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Takashi Iwai877211f2005-11-17 13:59:38 +01002446 struct snd_pcm_runtime *runtime = substream->runtime;
2447 struct snd_pcm_sync_ptr sync_ptr;
2448 volatile struct snd_pcm_mmap_status *status;
2449 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int err;
2451
2452 memset(&sync_ptr, 0, sizeof(sync_ptr));
2453 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2454 return -EFAULT;
Takashi Iwai877211f2005-11-17 13:59:38 +01002455 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 -07002456 return -EFAULT;
2457 status = runtime->status;
2458 control = runtime->control;
2459 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2460 err = snd_pcm_hwsync(substream);
2461 if (err < 0)
2462 return err;
2463 }
2464 snd_pcm_stream_lock_irq(substream);
2465 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2466 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2467 else
2468 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2469 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2470 control->avail_min = sync_ptr.c.control.avail_min;
2471 else
2472 sync_ptr.c.control.avail_min = control->avail_min;
2473 sync_ptr.s.status.state = status->state;
2474 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2475 sync_ptr.s.status.tstamp = status->tstamp;
2476 sync_ptr.s.status.suspended_state = status->suspended_state;
2477 snd_pcm_stream_unlock_irq(substream);
2478 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2479 return -EFAULT;
2480 return 0;
2481}
2482
Takashi Iwai877211f2005-11-17 13:59:38 +01002483static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 unsigned int cmd, void __user *arg);
Takashi Iwai877211f2005-11-17 13:59:38 +01002485static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 unsigned int cmd, void __user *arg);
2487
Takashi Iwai877211f2005-11-17 13:59:38 +01002488static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 unsigned int cmd, void __user *arg)
2490{
2491 snd_assert(substream != NULL, return -ENXIO);
2492
2493 switch (cmd) {
2494 case SNDRV_PCM_IOCTL_PVERSION:
2495 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2496 case SNDRV_PCM_IOCTL_INFO:
2497 return snd_pcm_info_user(substream, arg);
Takashi Iwai07799e72005-10-10 11:49:49 +02002498 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 case SNDRV_PCM_IOCTL_HW_REFINE:
2501 return snd_pcm_hw_refine_user(substream, arg);
2502 case SNDRV_PCM_IOCTL_HW_PARAMS:
2503 return snd_pcm_hw_params_user(substream, arg);
2504 case SNDRV_PCM_IOCTL_HW_FREE:
2505 return snd_pcm_hw_free(substream);
2506 case SNDRV_PCM_IOCTL_SW_PARAMS:
2507 return snd_pcm_sw_params_user(substream, arg);
2508 case SNDRV_PCM_IOCTL_STATUS:
2509 return snd_pcm_status_user(substream, arg);
2510 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2511 return snd_pcm_channel_info_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_PREPARE:
2513 return snd_pcm_prepare(substream);
2514 case SNDRV_PCM_IOCTL_RESET:
2515 return snd_pcm_reset(substream);
2516 case SNDRV_PCM_IOCTL_START:
2517 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2518 case SNDRV_PCM_IOCTL_LINK:
2519 return snd_pcm_link(substream, (int)(unsigned long) arg);
2520 case SNDRV_PCM_IOCTL_UNLINK:
2521 return snd_pcm_unlink(substream);
2522 case SNDRV_PCM_IOCTL_RESUME:
2523 return snd_pcm_resume(substream);
2524 case SNDRV_PCM_IOCTL_XRUN:
2525 return snd_pcm_xrun(substream);
2526 case SNDRV_PCM_IOCTL_HWSYNC:
2527 return snd_pcm_hwsync(substream);
2528 case SNDRV_PCM_IOCTL_DELAY:
2529 return snd_pcm_delay(substream, arg);
2530 case SNDRV_PCM_IOCTL_SYNC_PTR:
2531 return snd_pcm_sync_ptr(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002532#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2534 return snd_pcm_hw_refine_old_user(substream, arg);
2535 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2536 return snd_pcm_hw_params_old_user(substream, arg);
Takashi Iwai59d48582005-12-01 10:51:58 +01002537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 case SNDRV_PCM_IOCTL_DRAIN:
2539 return snd_pcm_drain(substream);
2540 case SNDRV_PCM_IOCTL_DROP:
2541 return snd_pcm_drop(substream);
Takashi Iwaie661d0d2006-02-21 14:14:50 +01002542 case SNDRV_PCM_IOCTL_PAUSE:
2543 {
2544 int res;
2545 snd_pcm_stream_lock_irq(substream);
2546 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2547 snd_pcm_stream_unlock_irq(substream);
2548 return res;
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551 snd_printd("unknown ioctl = 0x%x\n", cmd);
2552 return -ENOTTY;
2553}
2554
Takashi Iwai877211f2005-11-17 13:59:38 +01002555static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 unsigned int cmd, void __user *arg)
2557{
2558 snd_assert(substream != NULL, return -ENXIO);
2559 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2560 switch (cmd) {
2561 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2562 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002563 struct snd_xferi xferi;
2564 struct snd_xferi __user *_xferi = arg;
2565 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 snd_pcm_sframes_t result;
2567 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2568 return -EBADFD;
2569 if (put_user(0, &_xferi->result))
2570 return -EFAULT;
2571 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2572 return -EFAULT;
2573 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2574 __put_user(result, &_xferi->result);
2575 return result < 0 ? result : 0;
2576 }
2577 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2578 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002579 struct snd_xfern xfern;
2580 struct snd_xfern __user *_xfern = arg;
2581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 void __user **bufs;
2583 snd_pcm_sframes_t result;
2584 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2585 return -EBADFD;
2586 if (runtime->channels > 128)
2587 return -EINVAL;
2588 if (put_user(0, &_xfern->result))
2589 return -EFAULT;
2590 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2591 return -EFAULT;
2592 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2593 if (bufs == NULL)
2594 return -ENOMEM;
2595 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2596 kfree(bufs);
2597 return -EFAULT;
2598 }
2599 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2600 kfree(bufs);
2601 __put_user(result, &_xfern->result);
2602 return result < 0 ? result : 0;
2603 }
2604 case SNDRV_PCM_IOCTL_REWIND:
2605 {
2606 snd_pcm_uframes_t frames;
2607 snd_pcm_uframes_t __user *_frames = arg;
2608 snd_pcm_sframes_t result;
2609 if (get_user(frames, _frames))
2610 return -EFAULT;
2611 if (put_user(0, _frames))
2612 return -EFAULT;
2613 result = snd_pcm_playback_rewind(substream, frames);
2614 __put_user(result, _frames);
2615 return result < 0 ? result : 0;
2616 }
2617 case SNDRV_PCM_IOCTL_FORWARD:
2618 {
2619 snd_pcm_uframes_t frames;
2620 snd_pcm_uframes_t __user *_frames = arg;
2621 snd_pcm_sframes_t result;
2622 if (get_user(frames, _frames))
2623 return -EFAULT;
2624 if (put_user(0, _frames))
2625 return -EFAULT;
2626 result = snd_pcm_playback_forward(substream, frames);
2627 __put_user(result, _frames);
2628 return result < 0 ? result : 0;
2629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631 return snd_pcm_common_ioctl1(substream, cmd, arg);
2632}
2633
Takashi Iwai877211f2005-11-17 13:59:38 +01002634static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 unsigned int cmd, void __user *arg)
2636{
2637 snd_assert(substream != NULL, return -ENXIO);
2638 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2639 switch (cmd) {
2640 case SNDRV_PCM_IOCTL_READI_FRAMES:
2641 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002642 struct snd_xferi xferi;
2643 struct snd_xferi __user *_xferi = arg;
2644 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 snd_pcm_sframes_t result;
2646 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2647 return -EBADFD;
2648 if (put_user(0, &_xferi->result))
2649 return -EFAULT;
2650 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2651 return -EFAULT;
2652 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2653 __put_user(result, &_xferi->result);
2654 return result < 0 ? result : 0;
2655 }
2656 case SNDRV_PCM_IOCTL_READN_FRAMES:
2657 {
Takashi Iwai877211f2005-11-17 13:59:38 +01002658 struct snd_xfern xfern;
2659 struct snd_xfern __user *_xfern = arg;
2660 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 void *bufs;
2662 snd_pcm_sframes_t result;
2663 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2664 return -EBADFD;
2665 if (runtime->channels > 128)
2666 return -EINVAL;
2667 if (put_user(0, &_xfern->result))
2668 return -EFAULT;
2669 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2670 return -EFAULT;
2671 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2672 if (bufs == NULL)
2673 return -ENOMEM;
2674 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2675 kfree(bufs);
2676 return -EFAULT;
2677 }
2678 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2679 kfree(bufs);
2680 __put_user(result, &_xfern->result);
2681 return result < 0 ? result : 0;
2682 }
2683 case SNDRV_PCM_IOCTL_REWIND:
2684 {
2685 snd_pcm_uframes_t frames;
2686 snd_pcm_uframes_t __user *_frames = arg;
2687 snd_pcm_sframes_t result;
2688 if (get_user(frames, _frames))
2689 return -EFAULT;
2690 if (put_user(0, _frames))
2691 return -EFAULT;
2692 result = snd_pcm_capture_rewind(substream, frames);
2693 __put_user(result, _frames);
2694 return result < 0 ? result : 0;
2695 }
2696 case SNDRV_PCM_IOCTL_FORWARD:
2697 {
2698 snd_pcm_uframes_t frames;
2699 snd_pcm_uframes_t __user *_frames = arg;
2700 snd_pcm_sframes_t result;
2701 if (get_user(frames, _frames))
2702 return -EFAULT;
2703 if (put_user(0, _frames))
2704 return -EFAULT;
2705 result = snd_pcm_capture_forward(substream, frames);
2706 __put_user(result, _frames);
2707 return result < 0 ? result : 0;
2708 }
2709 }
2710 return snd_pcm_common_ioctl1(substream, cmd, arg);
2711}
2712
Takashi Iwai877211f2005-11-17 13:59:38 +01002713static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2714 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Takashi Iwai877211f2005-11-17 13:59:38 +01002716 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 pcm_file = file->private_data;
2719
2720 if (((cmd >> 8) & 0xff) != 'A')
2721 return -ENOTTY;
2722
2723 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2724}
2725
Takashi Iwai877211f2005-11-17 13:59:38 +01002726static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2727 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
Takashi Iwai877211f2005-11-17 13:59:38 +01002729 struct snd_pcm_file *pcm_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 pcm_file = file->private_data;
2732
2733 if (((cmd >> 8) & 0xff) != 'A')
2734 return -ENOTTY;
2735
2736 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2737}
2738
Takashi Iwai877211f2005-11-17 13:59:38 +01002739int snd_pcm_kernel_playback_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 unsigned int cmd, void *arg)
2741{
2742 mm_segment_t fs;
2743 int result;
2744
2745 fs = snd_enter_user();
2746 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2747 snd_leave_user(fs);
2748 return result;
2749}
2750
Takashi Iwai877211f2005-11-17 13:59:38 +01002751int snd_pcm_kernel_capture_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 unsigned int cmd, void *arg)
2753{
2754 mm_segment_t fs;
2755 int result;
2756
2757 fs = snd_enter_user();
2758 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2759 snd_leave_user(fs);
2760 return result;
2761}
2762
Takashi Iwai877211f2005-11-17 13:59:38 +01002763int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 unsigned int cmd, void *arg)
2765{
2766 switch (substream->stream) {
2767 case SNDRV_PCM_STREAM_PLAYBACK:
2768 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2769 case SNDRV_PCM_STREAM_CAPTURE:
2770 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2771 default:
2772 return -EINVAL;
2773 }
2774}
2775
Takashi Iwai877211f2005-11-17 13:59:38 +01002776static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2777 loff_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
Takashi Iwai877211f2005-11-17 13:59:38 +01002779 struct snd_pcm_file *pcm_file;
2780 struct snd_pcm_substream *substream;
2781 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 snd_pcm_sframes_t result;
2783
2784 pcm_file = file->private_data;
2785 substream = pcm_file->substream;
2786 snd_assert(substream != NULL, return -ENXIO);
2787 runtime = substream->runtime;
2788 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2789 return -EBADFD;
2790 if (!frame_aligned(runtime, count))
2791 return -EINVAL;
2792 count = bytes_to_frames(runtime, count);
2793 result = snd_pcm_lib_read(substream, buf, count);
2794 if (result > 0)
2795 result = frames_to_bytes(runtime, result);
2796 return result;
2797}
2798
Takashi Iwai877211f2005-11-17 13:59:38 +01002799static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2800 size_t count, 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, result = -ENXIO; goto end);
2810 runtime = substream->runtime;
2811 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2812 result = -EBADFD;
2813 goto end;
2814 }
2815 if (!frame_aligned(runtime, count)) {
2816 result = -EINVAL;
2817 goto end;
2818 }
2819 count = bytes_to_frames(runtime, count);
2820 result = snd_pcm_lib_write(substream, buf, count);
2821 if (result > 0)
2822 result = frames_to_bytes(runtime, result);
2823 end:
2824 return result;
2825}
2826
2827static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2828 unsigned long count, loff_t * offset)
2829
2830{
Takashi Iwai877211f2005-11-17 13:59:38 +01002831 struct snd_pcm_file *pcm_file;
2832 struct snd_pcm_substream *substream;
2833 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 snd_pcm_sframes_t result;
2835 unsigned long i;
2836 void __user **bufs;
2837 snd_pcm_uframes_t frames;
2838
2839 pcm_file = file->private_data;
2840 substream = pcm_file->substream;
2841 snd_assert(substream != NULL, return -ENXIO);
2842 runtime = substream->runtime;
2843 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2844 return -EBADFD;
2845 if (count > 1024 || count != runtime->channels)
2846 return -EINVAL;
2847 if (!frame_aligned(runtime, _vector->iov_len))
2848 return -EINVAL;
2849 frames = bytes_to_samples(runtime, _vector->iov_len);
2850 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2851 if (bufs == NULL)
2852 return -ENOMEM;
2853 for (i = 0; i < count; ++i)
2854 bufs[i] = _vector[i].iov_base;
2855 result = snd_pcm_lib_readv(substream, bufs, frames);
2856 if (result > 0)
2857 result = frames_to_bytes(runtime, result);
2858 kfree(bufs);
2859 return result;
2860}
2861
2862static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2863 unsigned long count, loff_t * offset)
2864{
Takashi Iwai877211f2005-11-17 13:59:38 +01002865 struct snd_pcm_file *pcm_file;
2866 struct snd_pcm_substream *substream;
2867 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 snd_pcm_sframes_t result;
2869 unsigned long i;
2870 void __user **bufs;
2871 snd_pcm_uframes_t frames;
2872
2873 pcm_file = file->private_data;
2874 substream = pcm_file->substream;
2875 snd_assert(substream != NULL, result = -ENXIO; goto end);
2876 runtime = substream->runtime;
2877 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2878 result = -EBADFD;
2879 goto end;
2880 }
2881 if (count > 128 || count != runtime->channels ||
2882 !frame_aligned(runtime, _vector->iov_len)) {
2883 result = -EINVAL;
2884 goto end;
2885 }
2886 frames = bytes_to_samples(runtime, _vector->iov_len);
2887 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2888 if (bufs == NULL)
2889 return -ENOMEM;
2890 for (i = 0; i < count; ++i)
2891 bufs[i] = _vector[i].iov_base;
2892 result = snd_pcm_lib_writev(substream, bufs, frames);
2893 if (result > 0)
2894 result = frames_to_bytes(runtime, result);
2895 kfree(bufs);
2896 end:
2897 return result;
2898}
2899
2900static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2901{
Takashi Iwai877211f2005-11-17 13:59:38 +01002902 struct snd_pcm_file *pcm_file;
2903 struct snd_pcm_substream *substream;
2904 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 unsigned int mask;
2906 snd_pcm_uframes_t avail;
2907
2908 pcm_file = file->private_data;
2909
2910 substream = pcm_file->substream;
2911 snd_assert(substream != NULL, return -ENXIO);
2912 runtime = substream->runtime;
2913
2914 poll_wait(file, &runtime->sleep, wait);
2915
2916 snd_pcm_stream_lock_irq(substream);
2917 avail = snd_pcm_playback_avail(runtime);
2918 switch (runtime->status->state) {
2919 case SNDRV_PCM_STATE_RUNNING:
2920 case SNDRV_PCM_STATE_PREPARED:
2921 case SNDRV_PCM_STATE_PAUSED:
2922 if (avail >= runtime->control->avail_min) {
2923 mask = POLLOUT | POLLWRNORM;
2924 break;
2925 }
2926 /* Fall through */
2927 case SNDRV_PCM_STATE_DRAINING:
2928 mask = 0;
2929 break;
2930 default:
2931 mask = POLLOUT | POLLWRNORM | POLLERR;
2932 break;
2933 }
2934 snd_pcm_stream_unlock_irq(substream);
2935 return mask;
2936}
2937
2938static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2939{
Takashi Iwai877211f2005-11-17 13:59:38 +01002940 struct snd_pcm_file *pcm_file;
2941 struct snd_pcm_substream *substream;
2942 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 unsigned int mask;
2944 snd_pcm_uframes_t avail;
2945
2946 pcm_file = file->private_data;
2947
2948 substream = pcm_file->substream;
2949 snd_assert(substream != NULL, return -ENXIO);
2950 runtime = substream->runtime;
2951
2952 poll_wait(file, &runtime->sleep, wait);
2953
2954 snd_pcm_stream_lock_irq(substream);
2955 avail = snd_pcm_capture_avail(runtime);
2956 switch (runtime->status->state) {
2957 case SNDRV_PCM_STATE_RUNNING:
2958 case SNDRV_PCM_STATE_PREPARED:
2959 case SNDRV_PCM_STATE_PAUSED:
2960 if (avail >= runtime->control->avail_min) {
2961 mask = POLLIN | POLLRDNORM;
2962 break;
2963 }
2964 mask = 0;
2965 break;
2966 case SNDRV_PCM_STATE_DRAINING:
2967 if (avail > 0) {
2968 mask = POLLIN | POLLRDNORM;
2969 break;
2970 }
2971 /* Fall through */
2972 default:
2973 mask = POLLIN | POLLRDNORM | POLLERR;
2974 break;
2975 }
2976 snd_pcm_stream_unlock_irq(substream);
2977 return mask;
2978}
2979
2980/*
2981 * mmap support
2982 */
2983
2984/*
2985 * Only on coherent architectures, we can mmap the status and the control records
2986 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2987 */
2988#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2989/*
2990 * mmap status record
2991 */
Takashi Iwai877211f2005-11-17 13:59:38 +01002992static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2993 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
Takashi Iwai877211f2005-11-17 13:59:38 +01002995 struct snd_pcm_substream *substream = area->vm_private_data;
2996 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 struct page * page;
2998
2999 if (substream == NULL)
3000 return NOPAGE_OOM;
3001 runtime = substream->runtime;
3002 page = virt_to_page(runtime->status);
Nick Pigginb5810032005-10-29 18:16:12 -07003003 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (type)
3005 *type = VM_FAULT_MINOR;
3006 return page;
3007}
3008
3009static struct vm_operations_struct snd_pcm_vm_ops_status =
3010{
3011 .nopage = snd_pcm_mmap_status_nopage,
3012};
3013
Takashi Iwai877211f2005-11-17 13:59:38 +01003014static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct vm_area_struct *area)
3016{
Takashi Iwai877211f2005-11-17 13:59:38 +01003017 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 long size;
3019 if (!(area->vm_flags & VM_READ))
3020 return -EINVAL;
3021 runtime = substream->runtime;
3022 snd_assert(runtime != NULL, return -EAGAIN);
3023 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003024 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return -EINVAL;
3026 area->vm_ops = &snd_pcm_vm_ops_status;
3027 area->vm_private_data = substream;
3028 area->vm_flags |= VM_RESERVED;
3029 return 0;
3030}
3031
3032/*
3033 * mmap control record
3034 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003035static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3036 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037{
Takashi Iwai877211f2005-11-17 13:59:38 +01003038 struct snd_pcm_substream *substream = area->vm_private_data;
3039 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 struct page * page;
3041
3042 if (substream == NULL)
3043 return NOPAGE_OOM;
3044 runtime = substream->runtime;
3045 page = virt_to_page(runtime->control);
Nick Pigginb5810032005-10-29 18:16:12 -07003046 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (type)
3048 *type = VM_FAULT_MINOR;
3049 return page;
3050}
3051
3052static struct vm_operations_struct snd_pcm_vm_ops_control =
3053{
3054 .nopage = snd_pcm_mmap_control_nopage,
3055};
3056
Takashi Iwai877211f2005-11-17 13:59:38 +01003057static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct vm_area_struct *area)
3059{
Takashi Iwai877211f2005-11-17 13:59:38 +01003060 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 long size;
3062 if (!(area->vm_flags & VM_READ))
3063 return -EINVAL;
3064 runtime = substream->runtime;
3065 snd_assert(runtime != NULL, return -EAGAIN);
3066 size = area->vm_end - area->vm_start;
Takashi Iwai877211f2005-11-17 13:59:38 +01003067 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return -EINVAL;
3069 area->vm_ops = &snd_pcm_vm_ops_control;
3070 area->vm_private_data = substream;
3071 area->vm_flags |= VM_RESERVED;
3072 return 0;
3073}
3074#else /* ! coherent mmap */
3075/*
3076 * don't support mmap for status and control records.
3077 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003078static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct vm_area_struct *area)
3080{
3081 return -ENXIO;
3082}
Takashi Iwai877211f2005-11-17 13:59:38 +01003083static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct vm_area_struct *area)
3085{
3086 return -ENXIO;
3087}
3088#endif /* coherent mmap */
3089
3090/*
3091 * nopage callback for mmapping a RAM page
3092 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003093static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3094 unsigned long address, int *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Takashi Iwai877211f2005-11-17 13:59:38 +01003096 struct snd_pcm_substream *substream = area->vm_private_data;
3097 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 unsigned long offset;
3099 struct page * page;
3100 void *vaddr;
3101 size_t dma_bytes;
3102
3103 if (substream == NULL)
3104 return NOPAGE_OOM;
3105 runtime = substream->runtime;
3106 offset = area->vm_pgoff << PAGE_SHIFT;
3107 offset += address - area->vm_start;
3108 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3109 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3110 if (offset > dma_bytes - PAGE_SIZE)
3111 return NOPAGE_SIGBUS;
3112 if (substream->ops->page) {
3113 page = substream->ops->page(substream, offset);
3114 if (! page)
3115 return NOPAGE_OOM;
3116 } else {
3117 vaddr = runtime->dma_area + offset;
3118 page = virt_to_page(vaddr);
3119 }
Nick Pigginb5810032005-10-29 18:16:12 -07003120 get_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (type)
3122 *type = VM_FAULT_MINOR;
3123 return page;
3124}
3125
3126static struct vm_operations_struct snd_pcm_vm_ops_data =
3127{
3128 .open = snd_pcm_mmap_data_open,
3129 .close = snd_pcm_mmap_data_close,
3130 .nopage = snd_pcm_mmap_data_nopage,
3131};
3132
3133/*
3134 * mmap the DMA buffer on RAM
3135 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003136static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3137 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
3139 area->vm_ops = &snd_pcm_vm_ops_data;
3140 area->vm_private_data = substream;
3141 area->vm_flags |= VM_RESERVED;
3142 atomic_inc(&substream->runtime->mmap_count);
3143 return 0;
3144}
3145
3146/*
3147 * mmap the DMA buffer on I/O memory area
3148 */
3149#if SNDRV_PCM_INFO_MMAP_IOMEM
3150static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3151{
3152 .open = snd_pcm_mmap_data_open,
3153 .close = snd_pcm_mmap_data_close,
3154};
3155
Takashi Iwai877211f2005-11-17 13:59:38 +01003156int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3157 struct vm_area_struct *area)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 long size;
3160 unsigned long offset;
3161
3162#ifdef pgprot_noncached
3163 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3164#endif
3165 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3166 area->vm_private_data = substream;
3167 area->vm_flags |= VM_IO;
3168 size = area->vm_end - area->vm_start;
3169 offset = area->vm_pgoff << PAGE_SHIFT;
3170 if (io_remap_pfn_range(area, area->vm_start,
3171 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3172 size, area->vm_page_prot))
3173 return -EAGAIN;
3174 atomic_inc(&substream->runtime->mmap_count);
3175 return 0;
3176}
3177#endif /* SNDRV_PCM_INFO_MMAP */
3178
3179/*
3180 * mmap DMA buffer
3181 */
Takashi Iwai877211f2005-11-17 13:59:38 +01003182int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 struct vm_area_struct *area)
3184{
Takashi Iwai877211f2005-11-17 13:59:38 +01003185 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 long size;
3187 unsigned long offset;
3188 size_t dma_bytes;
3189
3190 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3191 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3192 return -EINVAL;
3193 } else {
3194 if (!(area->vm_flags & VM_READ))
3195 return -EINVAL;
3196 }
3197 runtime = substream->runtime;
3198 snd_assert(runtime != NULL, return -EAGAIN);
3199 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3200 return -EBADFD;
3201 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3202 return -ENXIO;
3203 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3204 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3205 return -EINVAL;
3206 size = area->vm_end - area->vm_start;
3207 offset = area->vm_pgoff << PAGE_SHIFT;
3208 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3209 if ((size_t)size > dma_bytes)
3210 return -EINVAL;
3211 if (offset > dma_bytes - size)
3212 return -EINVAL;
3213
3214 if (substream->ops->mmap)
3215 return substream->ops->mmap(substream, area);
3216 else
3217 return snd_pcm_default_mmap(substream, area);
3218}
3219
3220static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3221{
Takashi Iwai877211f2005-11-17 13:59:38 +01003222 struct snd_pcm_file * pcm_file;
3223 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 unsigned long offset;
3225
3226 pcm_file = file->private_data;
3227 substream = pcm_file->substream;
3228 snd_assert(substream != NULL, return -ENXIO);
3229
3230 offset = area->vm_pgoff << PAGE_SHIFT;
3231 switch (offset) {
3232 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3233 if (substream->no_mmap_ctrl)
3234 return -ENXIO;
3235 return snd_pcm_mmap_status(substream, file, area);
3236 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3237 if (substream->no_mmap_ctrl)
3238 return -ENXIO;
3239 return snd_pcm_mmap_control(substream, file, area);
3240 default:
3241 return snd_pcm_mmap_data(substream, file, area);
3242 }
3243 return 0;
3244}
3245
3246static int snd_pcm_fasync(int fd, struct file * file, int on)
3247{
Takashi Iwai877211f2005-11-17 13:59:38 +01003248 struct snd_pcm_file * pcm_file;
3249 struct snd_pcm_substream *substream;
3250 struct snd_pcm_runtime *runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 int err;
3252
3253 pcm_file = file->private_data;
3254 substream = pcm_file->substream;
3255 snd_assert(substream != NULL, return -ENXIO);
3256 runtime = substream->runtime;
3257
3258 err = fasync_helper(fd, file, on, &runtime->fasync);
3259 if (err < 0)
3260 return err;
3261 return 0;
3262}
3263
3264/*
3265 * ioctl32 compat
3266 */
3267#ifdef CONFIG_COMPAT
3268#include "pcm_compat.c"
3269#else
3270#define snd_pcm_ioctl_compat NULL
3271#endif
3272
3273/*
3274 * To be removed helpers to keep binary compatibility
3275 */
3276
Takashi Iwai59d48582005-12-01 10:51:58 +01003277#ifdef CONFIG_SND_SUPPORT_OLD_API
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3279#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3280
Takashi Iwai877211f2005-11-17 13:59:38 +01003281static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3282 struct snd_pcm_hw_params_old *oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
3284 unsigned int i;
3285
3286 memset(params, 0, sizeof(*params));
3287 params->flags = oparams->flags;
3288 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3289 params->masks[i].bits[0] = oparams->masks[i];
3290 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3291 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3292 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3293 params->info = oparams->info;
3294 params->msbits = oparams->msbits;
3295 params->rate_num = oparams->rate_num;
3296 params->rate_den = oparams->rate_den;
3297 params->fifo_size = oparams->fifo_size;
3298}
3299
Takashi Iwai877211f2005-11-17 13:59:38 +01003300static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3301 struct snd_pcm_hw_params *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
3303 unsigned int i;
3304
3305 memset(oparams, 0, sizeof(*oparams));
3306 oparams->flags = params->flags;
3307 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3308 oparams->masks[i] = params->masks[i].bits[0];
3309 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3310 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3311 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3312 oparams->info = params->info;
3313 oparams->msbits = params->msbits;
3314 oparams->rate_num = params->rate_num;
3315 oparams->rate_den = params->rate_den;
3316 oparams->fifo_size = params->fifo_size;
3317}
3318
Takashi Iwai877211f2005-11-17 13:59:38 +01003319static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3320 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Takashi Iwai877211f2005-11-17 13:59:38 +01003322 struct snd_pcm_hw_params *params;
3323 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 int err;
3325
3326 params = kmalloc(sizeof(*params), GFP_KERNEL);
3327 if (!params) {
3328 err = -ENOMEM;
3329 goto out;
3330 }
3331 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3332 if (!oparams) {
3333 err = -ENOMEM;
3334 goto out;
3335 }
3336
3337 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3338 err = -EFAULT;
3339 goto out;
3340 }
3341 snd_pcm_hw_convert_from_old_params(params, oparams);
3342 err = snd_pcm_hw_refine(substream, params);
3343 snd_pcm_hw_convert_to_old_params(oparams, params);
3344 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3345 if (!err)
3346 err = -EFAULT;
3347 }
3348out:
3349 kfree(params);
3350 kfree(oparams);
3351 return err;
3352}
3353
Takashi Iwai877211f2005-11-17 13:59:38 +01003354static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3355 struct snd_pcm_hw_params_old __user * _oparams)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356{
Takashi Iwai877211f2005-11-17 13:59:38 +01003357 struct snd_pcm_hw_params *params;
3358 struct snd_pcm_hw_params_old *oparams = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int err;
3360
3361 params = kmalloc(sizeof(*params), GFP_KERNEL);
3362 if (!params) {
3363 err = -ENOMEM;
3364 goto out;
3365 }
3366 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3367 if (!oparams) {
3368 err = -ENOMEM;
3369 goto out;
3370 }
3371 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3372 err = -EFAULT;
3373 goto out;
3374 }
3375 snd_pcm_hw_convert_from_old_params(params, oparams);
3376 err = snd_pcm_hw_params(substream, params);
3377 snd_pcm_hw_convert_to_old_params(oparams, params);
3378 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3379 if (!err)
3380 err = -EFAULT;
3381 }
3382out:
3383 kfree(params);
3384 kfree(oparams);
3385 return err;
3386}
Takashi Iwai59d48582005-12-01 10:51:58 +01003387#endif /* CONFIG_SND_SUPPORT_OLD_API */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389/*
3390 * Register section
3391 */
3392
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003393struct file_operations snd_pcm_f_ops[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003395 .owner = THIS_MODULE,
3396 .write = snd_pcm_write,
3397 .writev = snd_pcm_writev,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003398 .open = snd_pcm_playback_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003399 .release = snd_pcm_release,
3400 .poll = snd_pcm_playback_poll,
3401 .unlocked_ioctl = snd_pcm_playback_ioctl,
3402 .compat_ioctl = snd_pcm_ioctl_compat,
3403 .mmap = snd_pcm_mmap,
3404 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 },
3406 {
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003407 .owner = THIS_MODULE,
3408 .read = snd_pcm_read,
3409 .readv = snd_pcm_readv,
Clemens Ladischf87135f2005-11-20 14:06:59 +01003410 .open = snd_pcm_capture_open,
Clemens Ladisch2af677f2005-11-20 14:03:48 +01003411 .release = snd_pcm_release,
3412 .poll = snd_pcm_capture_poll,
3413 .unlocked_ioctl = snd_pcm_capture_ioctl,
3414 .compat_ioctl = snd_pcm_ioctl_compat,
3415 .mmap = snd_pcm_mmap,
3416 .fasync = snd_pcm_fasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
3418};