]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/core/pcm_native.c
[ALSA] Decentralize PM control
[net-next-2.6.git] / sound / core / pcm_native.c
CommitLineData
1da177e4
LT
1/*
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
877211f5 42struct snd_pcm_hw_params_old {
1da177e4
LT
43 unsigned int flags;
44 unsigned int masks[SNDRV_PCM_HW_PARAM_SUBFORMAT -
45 SNDRV_PCM_HW_PARAM_ACCESS + 1];
877211f5 46 struct snd_interval intervals[SNDRV_PCM_HW_PARAM_TICK_TIME -
1da177e4
LT
47 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;
877211f5 54 snd_pcm_uframes_t fifo_size;
1da177e4
LT
55 unsigned char reserved[64];
56};
57
877211f5
TI
58#define SNDRV_PCM_IOCTL_HW_REFINE_OLD _IOWR('A', 0x10, struct snd_pcm_hw_params_old)
59#define SNDRV_PCM_IOCTL_HW_PARAMS_OLD _IOWR('A', 0x11, struct snd_pcm_hw_params_old)
1da177e4 60
877211f5
TI
61static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
62 struct snd_pcm_hw_params_old __user * _oparams);
63static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params_old __user * _oparams);
1da177e4
LT
65
66/*
67 *
68 */
69
70DEFINE_RWLOCK(snd_pcm_link_rwlock);
71static DECLARE_RWSEM(snd_pcm_link_rwsem);
72
73
74static inline mm_segment_t snd_enter_user(void)
75{
76 mm_segment_t fs = get_fs();
77 set_fs(get_ds());
78 return fs;
79}
80
81static inline void snd_leave_user(mm_segment_t fs)
82{
83 set_fs(fs);
84}
85
86
87
877211f5 88int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info)
1da177e4 89{
877211f5
TI
90 struct snd_pcm_runtime *runtime;
91 struct snd_pcm *pcm = substream->pcm;
92 struct snd_pcm_str *pstr = substream->pstr;
1da177e4
LT
93
94 snd_assert(substream != NULL, return -ENXIO);
95 memset(info, 0, sizeof(*info));
96 info->card = pcm->card->number;
97 info->device = pcm->device;
98 info->stream = substream->stream;
99 info->subdevice = substream->number;
100 strlcpy(info->id, pcm->id, sizeof(info->id));
101 strlcpy(info->name, pcm->name, sizeof(info->name));
102 info->dev_class = pcm->dev_class;
103 info->dev_subclass = pcm->dev_subclass;
104 info->subdevices_count = pstr->substream_count;
105 info->subdevices_avail = pstr->substream_count - pstr->substream_opened;
106 strlcpy(info->subname, substream->name, sizeof(info->subname));
107 runtime = substream->runtime;
108 /* AB: FIXME!!! This is definitely nonsense */
109 if (runtime) {
110 info->sync = runtime->sync;
111 substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_INFO, info);
112 }
113 return 0;
114}
115
877211f5
TI
116int snd_pcm_info_user(struct snd_pcm_substream *substream,
117 struct snd_pcm_info __user * _info)
1da177e4 118{
877211f5 119 struct snd_pcm_info *info;
1da177e4
LT
120 int err;
121
122 info = kmalloc(sizeof(*info), GFP_KERNEL);
123 if (! info)
124 return -ENOMEM;
125 err = snd_pcm_info(substream, info);
126 if (err >= 0) {
127 if (copy_to_user(_info, info, sizeof(*info)))
128 err = -EFAULT;
129 }
130 kfree(info);
131 return err;
132}
133
134#undef RULES_DEBUG
135
136#ifdef RULES_DEBUG
137#define HW_PARAM(v) [SNDRV_PCM_HW_PARAM_##v] = #v
138char *snd_pcm_hw_param_names[] = {
139 HW_PARAM(ACCESS),
140 HW_PARAM(FORMAT),
141 HW_PARAM(SUBFORMAT),
142 HW_PARAM(SAMPLE_BITS),
143 HW_PARAM(FRAME_BITS),
144 HW_PARAM(CHANNELS),
145 HW_PARAM(RATE),
146 HW_PARAM(PERIOD_TIME),
147 HW_PARAM(PERIOD_SIZE),
148 HW_PARAM(PERIOD_BYTES),
149 HW_PARAM(PERIODS),
150 HW_PARAM(BUFFER_TIME),
151 HW_PARAM(BUFFER_SIZE),
152 HW_PARAM(BUFFER_BYTES),
153 HW_PARAM(TICK_TIME),
154};
155#endif
156
877211f5
TI
157int snd_pcm_hw_refine(struct snd_pcm_substream *substream,
158 struct snd_pcm_hw_params *params)
1da177e4
LT
159{
160 unsigned int k;
877211f5
TI
161 struct snd_pcm_hardware *hw;
162 struct snd_interval *i = NULL;
163 struct snd_mask *m = NULL;
164 struct snd_pcm_hw_constraints *constrs = &substream->runtime->hw_constraints;
1da177e4
LT
165 unsigned int rstamps[constrs->rules_num];
166 unsigned int vstamps[SNDRV_PCM_HW_PARAM_LAST_INTERVAL + 1];
167 unsigned int stamp = 2;
168 int changed, again;
169
170 params->info = 0;
171 params->fifo_size = 0;
172 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_SAMPLE_BITS))
173 params->msbits = 0;
174 if (params->rmask & (1 << SNDRV_PCM_HW_PARAM_RATE)) {
175 params->rate_num = 0;
176 params->rate_den = 0;
177 }
178
179 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
180 m = hw_param_mask(params, k);
181 if (snd_mask_empty(m))
182 return -EINVAL;
183 if (!(params->rmask & (1 << k)))
184 continue;
185#ifdef RULES_DEBUG
186 printk("%s = ", snd_pcm_hw_param_names[k]);
187 printk("%04x%04x%04x%04x -> ", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
188#endif
189 changed = snd_mask_refine(m, constrs_mask(constrs, k));
190#ifdef RULES_DEBUG
191 printk("%04x%04x%04x%04x\n", m->bits[3], m->bits[2], m->bits[1], m->bits[0]);
192#endif
193 if (changed)
194 params->cmask |= 1 << k;
195 if (changed < 0)
196 return changed;
197 }
198
199 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
200 i = hw_param_interval(params, k);
201 if (snd_interval_empty(i))
202 return -EINVAL;
203 if (!(params->rmask & (1 << k)))
204 continue;
205#ifdef RULES_DEBUG
206 printk("%s = ", snd_pcm_hw_param_names[k]);
207 if (i->empty)
208 printk("empty");
209 else
210 printk("%c%u %u%c",
211 i->openmin ? '(' : '[', i->min,
212 i->max, i->openmax ? ')' : ']');
213 printk(" -> ");
214#endif
215 changed = snd_interval_refine(i, constrs_interval(constrs, k));
216#ifdef RULES_DEBUG
217 if (i->empty)
218 printk("empty\n");
219 else
220 printk("%c%u %u%c\n",
221 i->openmin ? '(' : '[', i->min,
222 i->max, i->openmax ? ')' : ']');
223#endif
224 if (changed)
225 params->cmask |= 1 << k;
226 if (changed < 0)
227 return changed;
228 }
229
230 for (k = 0; k < constrs->rules_num; k++)
231 rstamps[k] = 0;
232 for (k = 0; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
233 vstamps[k] = (params->rmask & (1 << k)) ? 1 : 0;
234 do {
235 again = 0;
236 for (k = 0; k < constrs->rules_num; k++) {
877211f5 237 struct snd_pcm_hw_rule *r = &constrs->rules[k];
1da177e4
LT
238 unsigned int d;
239 int doit = 0;
240 if (r->cond && !(r->cond & params->flags))
241 continue;
242 for (d = 0; r->deps[d] >= 0; d++) {
243 if (vstamps[r->deps[d]] > rstamps[k]) {
244 doit = 1;
245 break;
246 }
247 }
248 if (!doit)
249 continue;
250#ifdef RULES_DEBUG
251 printk("Rule %d [%p]: ", k, r->func);
252 if (r->var >= 0) {
253 printk("%s = ", snd_pcm_hw_param_names[r->var]);
254 if (hw_is_mask(r->var)) {
255 m = hw_param_mask(params, r->var);
256 printk("%x", *m->bits);
257 } else {
258 i = hw_param_interval(params, r->var);
259 if (i->empty)
260 printk("empty");
261 else
262 printk("%c%u %u%c",
263 i->openmin ? '(' : '[', i->min,
264 i->max, i->openmax ? ')' : ']');
265 }
266 }
267#endif
268 changed = r->func(params, r);
269#ifdef RULES_DEBUG
270 if (r->var >= 0) {
271 printk(" -> ");
272 if (hw_is_mask(r->var))
273 printk("%x", *m->bits);
274 else {
275 if (i->empty)
276 printk("empty");
277 else
278 printk("%c%u %u%c",
279 i->openmin ? '(' : '[', i->min,
280 i->max, i->openmax ? ')' : ']');
281 }
282 }
283 printk("\n");
284#endif
285 rstamps[k] = stamp;
286 if (changed && r->var >= 0) {
287 params->cmask |= (1 << r->var);
288 vstamps[r->var] = stamp;
289 again = 1;
290 }
291 if (changed < 0)
292 return changed;
293 stamp++;
294 }
295 } while (again);
296 if (!params->msbits) {
297 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
298 if (snd_interval_single(i))
299 params->msbits = snd_interval_value(i);
300 }
301
302 if (!params->rate_den) {
303 i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
304 if (snd_interval_single(i)) {
305 params->rate_num = snd_interval_value(i);
306 params->rate_den = 1;
307 }
308 }
309
310 hw = &substream->runtime->hw;
311 if (!params->info)
312 params->info = hw->info;
313 if (!params->fifo_size)
314 params->fifo_size = hw->fifo_size;
315 params->rmask = 0;
316 return 0;
317}
318
877211f5
TI
319static int snd_pcm_hw_refine_user(struct snd_pcm_substream *substream,
320 struct snd_pcm_hw_params __user * _params)
1da177e4 321{
877211f5 322 struct snd_pcm_hw_params *params;
1da177e4
LT
323 int err;
324
325 params = kmalloc(sizeof(*params), GFP_KERNEL);
326 if (!params) {
327 err = -ENOMEM;
328 goto out;
329 }
330 if (copy_from_user(params, _params, sizeof(*params))) {
331 err = -EFAULT;
332 goto out;
333 }
334 err = snd_pcm_hw_refine(substream, params);
335 if (copy_to_user(_params, params, sizeof(*params))) {
336 if (!err)
337 err = -EFAULT;
338 }
339out:
340 kfree(params);
341 return err;
342}
343
877211f5
TI
344static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
345 struct snd_pcm_hw_params *params)
1da177e4 346{
877211f5 347 struct snd_pcm_runtime *runtime;
1da177e4
LT
348 int err;
349 unsigned int bits;
350 snd_pcm_uframes_t frames;
351
352 snd_assert(substream != NULL, return -ENXIO);
353 runtime = substream->runtime;
354 snd_assert(runtime != NULL, return -ENXIO);
355 snd_pcm_stream_lock_irq(substream);
356 switch (runtime->status->state) {
357 case SNDRV_PCM_STATE_OPEN:
358 case SNDRV_PCM_STATE_SETUP:
359 case SNDRV_PCM_STATE_PREPARED:
360 break;
361 default:
362 snd_pcm_stream_unlock_irq(substream);
363 return -EBADFD;
364 }
365 snd_pcm_stream_unlock_irq(substream);
366#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
367 if (!substream->oss.oss)
368#endif
369 if (atomic_read(&runtime->mmap_count))
370 return -EBADFD;
371
372 params->rmask = ~0U;
373 err = snd_pcm_hw_refine(substream, params);
374 if (err < 0)
375 goto _error;
376
377 err = snd_pcm_hw_params_choose(substream, params);
378 if (err < 0)
379 goto _error;
380
381 if (substream->ops->hw_params != NULL) {
382 err = substream->ops->hw_params(substream, params);
383 if (err < 0)
384 goto _error;
385 }
386
387 runtime->access = params_access(params);
388 runtime->format = params_format(params);
389 runtime->subformat = params_subformat(params);
390 runtime->channels = params_channels(params);
391 runtime->rate = params_rate(params);
392 runtime->period_size = params_period_size(params);
393 runtime->periods = params_periods(params);
394 runtime->buffer_size = params_buffer_size(params);
395 runtime->tick_time = params_tick_time(params);
396 runtime->info = params->info;
397 runtime->rate_num = params->rate_num;
398 runtime->rate_den = params->rate_den;
399
400 bits = snd_pcm_format_physical_width(runtime->format);
401 runtime->sample_bits = bits;
402 bits *= runtime->channels;
403 runtime->frame_bits = bits;
404 frames = 1;
405 while (bits % 8 != 0) {
406 bits *= 2;
407 frames *= 2;
408 }
409 runtime->byte_align = bits / 8;
410 runtime->min_align = frames;
411
412 /* Default sw params */
413 runtime->tstamp_mode = SNDRV_PCM_TSTAMP_NONE;
414 runtime->period_step = 1;
415 runtime->sleep_min = 0;
416 runtime->control->avail_min = runtime->period_size;
417 runtime->xfer_align = runtime->period_size;
418 runtime->start_threshold = 1;
419 runtime->stop_threshold = runtime->buffer_size;
420 runtime->silence_threshold = 0;
421 runtime->silence_size = 0;
422 runtime->boundary = runtime->buffer_size;
423 while (runtime->boundary * 2 <= LONG_MAX - runtime->buffer_size)
424 runtime->boundary *= 2;
425
426 snd_pcm_timer_resolution_change(substream);
427 runtime->status->state = SNDRV_PCM_STATE_SETUP;
428 return 0;
429 _error:
430 /* hardware might be unuseable from this time,
431 so we force application to retry to set
432 the correct hardware parameter settings */
433 runtime->status->state = SNDRV_PCM_STATE_OPEN;
434 if (substream->ops->hw_free != NULL)
435 substream->ops->hw_free(substream);
436 return err;
437}
438
877211f5
TI
439static int snd_pcm_hw_params_user(struct snd_pcm_substream *substream,
440 struct snd_pcm_hw_params __user * _params)
1da177e4 441{
877211f5 442 struct snd_pcm_hw_params *params;
1da177e4
LT
443 int err;
444
445 params = kmalloc(sizeof(*params), GFP_KERNEL);
446 if (!params) {
447 err = -ENOMEM;
448 goto out;
449 }
450 if (copy_from_user(params, _params, sizeof(*params))) {
451 err = -EFAULT;
452 goto out;
453 }
454 err = snd_pcm_hw_params(substream, params);
455 if (copy_to_user(_params, params, sizeof(*params))) {
456 if (!err)
457 err = -EFAULT;
458 }
459out:
460 kfree(params);
461 return err;
462}
463
877211f5 464static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4 465{
877211f5 466 struct snd_pcm_runtime *runtime;
1da177e4
LT
467 int result = 0;
468
469 snd_assert(substream != NULL, return -ENXIO);
470 runtime = substream->runtime;
471 snd_assert(runtime != NULL, return -ENXIO);
472 snd_pcm_stream_lock_irq(substream);
473 switch (runtime->status->state) {
474 case SNDRV_PCM_STATE_SETUP:
475 case SNDRV_PCM_STATE_PREPARED:
476 break;
477 default:
478 snd_pcm_stream_unlock_irq(substream);
479 return -EBADFD;
480 }
481 snd_pcm_stream_unlock_irq(substream);
482 if (atomic_read(&runtime->mmap_count))
483 return -EBADFD;
484 if (substream->ops->hw_free)
485 result = substream->ops->hw_free(substream);
486 runtime->status->state = SNDRV_PCM_STATE_OPEN;
487 return result;
488}
489
877211f5
TI
490static int snd_pcm_sw_params(struct snd_pcm_substream *substream,
491 struct snd_pcm_sw_params *params)
1da177e4 492{
877211f5 493 struct snd_pcm_runtime *runtime;
1da177e4
LT
494
495 snd_assert(substream != NULL, return -ENXIO);
496 runtime = substream->runtime;
497 snd_assert(runtime != NULL, return -ENXIO);
498 snd_pcm_stream_lock_irq(substream);
499 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
500 snd_pcm_stream_unlock_irq(substream);
501 return -EBADFD;
502 }
503 snd_pcm_stream_unlock_irq(substream);
504
505 if (params->tstamp_mode > SNDRV_PCM_TSTAMP_LAST)
506 return -EINVAL;
507 if (params->avail_min == 0)
508 return -EINVAL;
509 if (params->xfer_align == 0 ||
510 params->xfer_align % runtime->min_align != 0)
511 return -EINVAL;
512 if (params->silence_size >= runtime->boundary) {
513 if (params->silence_threshold != 0)
514 return -EINVAL;
515 } else {
516 if (params->silence_size > params->silence_threshold)
517 return -EINVAL;
518 if (params->silence_threshold > runtime->buffer_size)
519 return -EINVAL;
520 }
521 snd_pcm_stream_lock_irq(substream);
522 runtime->tstamp_mode = params->tstamp_mode;
523 runtime->sleep_min = params->sleep_min;
524 runtime->period_step = params->period_step;
525 runtime->control->avail_min = params->avail_min;
526 runtime->start_threshold = params->start_threshold;
527 runtime->stop_threshold = params->stop_threshold;
528 runtime->silence_threshold = params->silence_threshold;
529 runtime->silence_size = params->silence_size;
530 runtime->xfer_align = params->xfer_align;
531 params->boundary = runtime->boundary;
532 if (snd_pcm_running(substream)) {
533 if (runtime->sleep_min)
534 snd_pcm_tick_prepare(substream);
535 else
536 snd_pcm_tick_set(substream, 0);
537 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
538 runtime->silence_size > 0)
539 snd_pcm_playback_silence(substream, ULONG_MAX);
540 wake_up(&runtime->sleep);
541 }
542 snd_pcm_stream_unlock_irq(substream);
543 return 0;
544}
545
877211f5
TI
546static int snd_pcm_sw_params_user(struct snd_pcm_substream *substream,
547 struct snd_pcm_sw_params __user * _params)
1da177e4 548{
877211f5 549 struct snd_pcm_sw_params params;
1da177e4
LT
550 int err;
551 if (copy_from_user(&params, _params, sizeof(params)))
552 return -EFAULT;
553 err = snd_pcm_sw_params(substream, &params);
554 if (copy_to_user(_params, &params, sizeof(params)))
555 return -EFAULT;
556 return err;
557}
558
877211f5
TI
559int snd_pcm_status(struct snd_pcm_substream *substream,
560 struct snd_pcm_status *status)
1da177e4 561{
877211f5 562 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
563
564 snd_pcm_stream_lock_irq(substream);
565 status->state = runtime->status->state;
566 status->suspended_state = runtime->status->suspended_state;
567 if (status->state == SNDRV_PCM_STATE_OPEN)
568 goto _end;
569 status->trigger_tstamp = runtime->trigger_tstamp;
570 if (snd_pcm_running(substream)) {
571 snd_pcm_update_hw_ptr(substream);
572 if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
573 status->tstamp = runtime->status->tstamp;
574 else
07799e75 575 getnstimeofday(&status->tstamp);
1da177e4 576 } else
07799e75 577 getnstimeofday(&status->tstamp);
1da177e4
LT
578 status->appl_ptr = runtime->control->appl_ptr;
579 status->hw_ptr = runtime->status->hw_ptr;
580 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
581 status->avail = snd_pcm_playback_avail(runtime);
582 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
583 runtime->status->state == SNDRV_PCM_STATE_DRAINING)
584 status->delay = runtime->buffer_size - status->avail;
585 else
586 status->delay = 0;
587 } else {
588 status->avail = snd_pcm_capture_avail(runtime);
589 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
590 status->delay = status->avail;
591 else
592 status->delay = 0;
593 }
594 status->avail_max = runtime->avail_max;
595 status->overrange = runtime->overrange;
596 runtime->avail_max = 0;
597 runtime->overrange = 0;
598 _end:
599 snd_pcm_stream_unlock_irq(substream);
600 return 0;
601}
602
877211f5
TI
603static int snd_pcm_status_user(struct snd_pcm_substream *substream,
604 struct snd_pcm_status __user * _status)
1da177e4 605{
877211f5
TI
606 struct snd_pcm_status status;
607 struct snd_pcm_runtime *runtime;
1da177e4
LT
608 int res;
609
610 snd_assert(substream != NULL, return -ENXIO);
611 runtime = substream->runtime;
612 memset(&status, 0, sizeof(status));
613 res = snd_pcm_status(substream, &status);
614 if (res < 0)
615 return res;
616 if (copy_to_user(_status, &status, sizeof(status)))
617 return -EFAULT;
618 return 0;
619}
620
877211f5
TI
621static int snd_pcm_channel_info(struct snd_pcm_substream *substream,
622 struct snd_pcm_channel_info * info)
1da177e4 623{
877211f5 624 struct snd_pcm_runtime *runtime;
1da177e4
LT
625 unsigned int channel;
626
627 snd_assert(substream != NULL, return -ENXIO);
628 channel = info->channel;
629 runtime = substream->runtime;
630 snd_pcm_stream_lock_irq(substream);
631 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
632 snd_pcm_stream_unlock_irq(substream);
633 return -EBADFD;
634 }
635 snd_pcm_stream_unlock_irq(substream);
636 if (channel >= runtime->channels)
637 return -EINVAL;
638 memset(info, 0, sizeof(*info));
639 info->channel = channel;
640 return substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_CHANNEL_INFO, info);
641}
642
877211f5
TI
643static int snd_pcm_channel_info_user(struct snd_pcm_substream *substream,
644 struct snd_pcm_channel_info __user * _info)
1da177e4 645{
877211f5 646 struct snd_pcm_channel_info info;
1da177e4
LT
647 int res;
648
649 if (copy_from_user(&info, _info, sizeof(info)))
650 return -EFAULT;
651 res = snd_pcm_channel_info(substream, &info);
652 if (res < 0)
653 return res;
654 if (copy_to_user(_info, &info, sizeof(info)))
655 return -EFAULT;
656 return 0;
657}
658
877211f5 659static void snd_pcm_trigger_tstamp(struct snd_pcm_substream *substream)
1da177e4 660{
877211f5 661 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
662 if (runtime->trigger_master == NULL)
663 return;
664 if (runtime->trigger_master == substream) {
07799e75 665 getnstimeofday(&runtime->trigger_tstamp);
1da177e4
LT
666 } else {
667 snd_pcm_trigger_tstamp(runtime->trigger_master);
668 runtime->trigger_tstamp = runtime->trigger_master->runtime->trigger_tstamp;
669 }
670 runtime->trigger_master = NULL;
671}
672
673struct action_ops {
877211f5
TI
674 int (*pre_action)(struct snd_pcm_substream *substream, int state);
675 int (*do_action)(struct snd_pcm_substream *substream, int state);
676 void (*undo_action)(struct snd_pcm_substream *substream, int state);
677 void (*post_action)(struct snd_pcm_substream *substream, int state);
1da177e4
LT
678};
679
680/*
681 * this functions is core for handling of linked stream
682 * Note: the stream state might be changed also on failure
683 * Note2: call with calling stream lock + link lock
684 */
685static int snd_pcm_action_group(struct action_ops *ops,
877211f5 686 struct snd_pcm_substream *substream,
1da177e4
LT
687 int state, int do_lock)
688{
689 struct list_head *pos;
877211f5
TI
690 struct snd_pcm_substream *s = NULL;
691 struct snd_pcm_substream *s1;
1da177e4
LT
692 int res = 0;
693
694 snd_pcm_group_for_each(pos, substream) {
695 s = snd_pcm_group_substream_entry(pos);
696 if (do_lock && s != substream)
697 spin_lock(&s->self_group.lock);
698 res = ops->pre_action(s, state);
699 if (res < 0)
700 goto _unlock;
701 }
702 snd_pcm_group_for_each(pos, substream) {
703 s = snd_pcm_group_substream_entry(pos);
704 res = ops->do_action(s, state);
705 if (res < 0) {
706 if (ops->undo_action) {
707 snd_pcm_group_for_each(pos, substream) {
708 s1 = snd_pcm_group_substream_entry(pos);
709 if (s1 == s) /* failed stream */
710 break;
711 ops->undo_action(s1, state);
712 }
713 }
714 s = NULL; /* unlock all */
715 goto _unlock;
716 }
717 }
718 snd_pcm_group_for_each(pos, substream) {
719 s = snd_pcm_group_substream_entry(pos);
720 ops->post_action(s, state);
721 }
722 _unlock:
723 if (do_lock) {
724 /* unlock streams */
725 snd_pcm_group_for_each(pos, substream) {
726 s1 = snd_pcm_group_substream_entry(pos);
727 if (s1 != substream)
728 spin_unlock(&s1->self_group.lock);
729 if (s1 == s) /* end */
730 break;
731 }
732 }
733 return res;
734}
735
736/*
737 * Note: call with stream lock
738 */
739static int snd_pcm_action_single(struct action_ops *ops,
877211f5 740 struct snd_pcm_substream *substream,
1da177e4
LT
741 int state)
742{
743 int res;
744
745 res = ops->pre_action(substream, state);
746 if (res < 0)
747 return res;
748 res = ops->do_action(substream, state);
749 if (res == 0)
750 ops->post_action(substream, state);
751 else if (ops->undo_action)
752 ops->undo_action(substream, state);
753 return res;
754}
755
756/*
757 * Note: call with stream lock
758 */
759static int snd_pcm_action(struct action_ops *ops,
877211f5 760 struct snd_pcm_substream *substream,
1da177e4
LT
761 int state)
762{
763 int res;
764
765 if (snd_pcm_stream_linked(substream)) {
766 if (!spin_trylock(&substream->group->lock)) {
767 spin_unlock(&substream->self_group.lock);
768 spin_lock(&substream->group->lock);
769 spin_lock(&substream->self_group.lock);
770 }
771 res = snd_pcm_action_group(ops, substream, state, 1);
772 spin_unlock(&substream->group->lock);
773 } else {
774 res = snd_pcm_action_single(ops, substream, state);
775 }
776 return res;
777}
778
779/*
780 * Note: don't use any locks before
781 */
782static int snd_pcm_action_lock_irq(struct action_ops *ops,
877211f5 783 struct snd_pcm_substream *substream,
1da177e4
LT
784 int state)
785{
786 int res;
787
788 read_lock_irq(&snd_pcm_link_rwlock);
789 if (snd_pcm_stream_linked(substream)) {
790 spin_lock(&substream->group->lock);
791 spin_lock(&substream->self_group.lock);
792 res = snd_pcm_action_group(ops, substream, state, 1);
793 spin_unlock(&substream->self_group.lock);
794 spin_unlock(&substream->group->lock);
795 } else {
796 spin_lock(&substream->self_group.lock);
797 res = snd_pcm_action_single(ops, substream, state);
798 spin_unlock(&substream->self_group.lock);
799 }
800 read_unlock_irq(&snd_pcm_link_rwlock);
801 return res;
802}
803
804/*
805 */
806static int snd_pcm_action_nonatomic(struct action_ops *ops,
877211f5 807 struct snd_pcm_substream *substream,
1da177e4
LT
808 int state)
809{
810 int res;
811
812 down_read(&snd_pcm_link_rwsem);
813 if (snd_pcm_stream_linked(substream))
814 res = snd_pcm_action_group(ops, substream, state, 0);
815 else
816 res = snd_pcm_action_single(ops, substream, state);
817 up_read(&snd_pcm_link_rwsem);
818 return res;
819}
820
821/*
822 * start callbacks
823 */
877211f5 824static int snd_pcm_pre_start(struct snd_pcm_substream *substream, int state)
1da177e4 825{
877211f5 826 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
827 if (runtime->status->state != SNDRV_PCM_STATE_PREPARED)
828 return -EBADFD;
829 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
830 !snd_pcm_playback_data(substream))
831 return -EPIPE;
832 runtime->trigger_master = substream;
833 return 0;
834}
835
877211f5 836static int snd_pcm_do_start(struct snd_pcm_substream *substream, int state)
1da177e4
LT
837{
838 if (substream->runtime->trigger_master != substream)
839 return 0;
840 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_START);
841}
842
877211f5 843static void snd_pcm_undo_start(struct snd_pcm_substream *substream, int state)
1da177e4
LT
844{
845 if (substream->runtime->trigger_master == substream)
846 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
847}
848
877211f5 849static void snd_pcm_post_start(struct snd_pcm_substream *substream, int state)
1da177e4 850{
877211f5 851 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
852 snd_pcm_trigger_tstamp(substream);
853 runtime->status->state = state;
854 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
855 runtime->silence_size > 0)
856 snd_pcm_playback_silence(substream, ULONG_MAX);
857 if (runtime->sleep_min)
858 snd_pcm_tick_prepare(substream);
859 if (substream->timer)
877211f5
TI
860 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTART,
861 &runtime->trigger_tstamp);
1da177e4
LT
862}
863
864static struct action_ops snd_pcm_action_start = {
865 .pre_action = snd_pcm_pre_start,
866 .do_action = snd_pcm_do_start,
867 .undo_action = snd_pcm_undo_start,
868 .post_action = snd_pcm_post_start
869};
870
871/**
872 * snd_pcm_start
df8db936 873 * @substream: the PCM substream instance
1da177e4
LT
874 *
875 * Start all linked streams.
876 */
877211f5 877int snd_pcm_start(struct snd_pcm_substream *substream)
1da177e4 878{
877211f5
TI
879 return snd_pcm_action(&snd_pcm_action_start, substream,
880 SNDRV_PCM_STATE_RUNNING);
1da177e4
LT
881}
882
883/*
884 * stop callbacks
885 */
877211f5 886static int snd_pcm_pre_stop(struct snd_pcm_substream *substream, int state)
1da177e4 887{
877211f5 888 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
889 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
890 return -EBADFD;
891 runtime->trigger_master = substream;
892 return 0;
893}
894
877211f5 895static int snd_pcm_do_stop(struct snd_pcm_substream *substream, int state)
1da177e4
LT
896{
897 if (substream->runtime->trigger_master == substream &&
898 snd_pcm_running(substream))
899 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_STOP);
900 return 0; /* unconditonally stop all substreams */
901}
902
877211f5 903static void snd_pcm_post_stop(struct snd_pcm_substream *substream, int state)
1da177e4 904{
877211f5 905 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
906 if (runtime->status->state != state) {
907 snd_pcm_trigger_tstamp(substream);
908 if (substream->timer)
877211f5
TI
909 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSTOP,
910 &runtime->trigger_tstamp);
1da177e4
LT
911 runtime->status->state = state;
912 snd_pcm_tick_set(substream, 0);
913 }
914 wake_up(&runtime->sleep);
915}
916
917static struct action_ops snd_pcm_action_stop = {
918 .pre_action = snd_pcm_pre_stop,
919 .do_action = snd_pcm_do_stop,
920 .post_action = snd_pcm_post_stop
921};
922
923/**
924 * snd_pcm_stop
df8db936
TI
925 * @substream: the PCM substream instance
926 * @state: PCM state after stopping the stream
1da177e4
LT
927 *
928 * Try to stop all running streams in the substream group.
929 * The state of each stream is changed to the given value after that unconditionally.
930 */
877211f5 931int snd_pcm_stop(struct snd_pcm_substream *substream, int state)
1da177e4
LT
932{
933 return snd_pcm_action(&snd_pcm_action_stop, substream, state);
934}
935
936/**
937 * snd_pcm_drain_done
df8db936 938 * @substream: the PCM substream
1da177e4
LT
939 *
940 * Stop the DMA only when the given stream is playback.
941 * The state is changed to SETUP.
942 * Unlike snd_pcm_stop(), this affects only the given stream.
943 */
877211f5 944int snd_pcm_drain_done(struct snd_pcm_substream *substream)
1da177e4 945{
877211f5
TI
946 return snd_pcm_action_single(&snd_pcm_action_stop, substream,
947 SNDRV_PCM_STATE_SETUP);
1da177e4
LT
948}
949
950/*
951 * pause callbacks
952 */
877211f5 953static int snd_pcm_pre_pause(struct snd_pcm_substream *substream, int push)
1da177e4 954{
877211f5 955 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
956 if (!(runtime->info & SNDRV_PCM_INFO_PAUSE))
957 return -ENOSYS;
958 if (push) {
959 if (runtime->status->state != SNDRV_PCM_STATE_RUNNING)
960 return -EBADFD;
961 } else if (runtime->status->state != SNDRV_PCM_STATE_PAUSED)
962 return -EBADFD;
963 runtime->trigger_master = substream;
964 return 0;
965}
966
877211f5 967static int snd_pcm_do_pause(struct snd_pcm_substream *substream, int push)
1da177e4
LT
968{
969 if (substream->runtime->trigger_master != substream)
970 return 0;
971 return substream->ops->trigger(substream,
972 push ? SNDRV_PCM_TRIGGER_PAUSE_PUSH :
973 SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
974}
975
877211f5 976static void snd_pcm_undo_pause(struct snd_pcm_substream *substream, int push)
1da177e4
LT
977{
978 if (substream->runtime->trigger_master == substream)
979 substream->ops->trigger(substream,
980 push ? SNDRV_PCM_TRIGGER_PAUSE_RELEASE :
981 SNDRV_PCM_TRIGGER_PAUSE_PUSH);
982}
983
877211f5 984static void snd_pcm_post_pause(struct snd_pcm_substream *substream, int push)
1da177e4 985{
877211f5 986 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
987 snd_pcm_trigger_tstamp(substream);
988 if (push) {
989 runtime->status->state = SNDRV_PCM_STATE_PAUSED;
990 if (substream->timer)
877211f5
TI
991 snd_timer_notify(substream->timer,
992 SNDRV_TIMER_EVENT_MPAUSE,
993 &runtime->trigger_tstamp);
1da177e4
LT
994 snd_pcm_tick_set(substream, 0);
995 wake_up(&runtime->sleep);
996 } else {
997 runtime->status->state = SNDRV_PCM_STATE_RUNNING;
998 if (runtime->sleep_min)
999 snd_pcm_tick_prepare(substream);
1000 if (substream->timer)
877211f5
TI
1001 snd_timer_notify(substream->timer,
1002 SNDRV_TIMER_EVENT_MCONTINUE,
1003 &runtime->trigger_tstamp);
1da177e4
LT
1004 }
1005}
1006
1007static struct action_ops snd_pcm_action_pause = {
1008 .pre_action = snd_pcm_pre_pause,
1009 .do_action = snd_pcm_do_pause,
1010 .undo_action = snd_pcm_undo_pause,
1011 .post_action = snd_pcm_post_pause
1012};
1013
1014/*
1015 * Push/release the pause for all linked streams.
1016 */
877211f5 1017static int snd_pcm_pause(struct snd_pcm_substream *substream, int push)
1da177e4
LT
1018{
1019 return snd_pcm_action(&snd_pcm_action_pause, substream, push);
1020}
1021
1022#ifdef CONFIG_PM
1023/* suspend */
1024
877211f5 1025static int snd_pcm_pre_suspend(struct snd_pcm_substream *substream, int state)
1da177e4 1026{
877211f5 1027 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1028 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1029 return -EBUSY;
1030 runtime->trigger_master = substream;
1031 return 0;
1032}
1033
877211f5 1034static int snd_pcm_do_suspend(struct snd_pcm_substream *substream, int state)
1da177e4 1035{
877211f5 1036 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1037 if (runtime->trigger_master != substream)
1038 return 0;
1039 if (! snd_pcm_running(substream))
1040 return 0;
1041 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1042 return 0; /* suspend unconditionally */
1043}
1044
877211f5 1045static void snd_pcm_post_suspend(struct snd_pcm_substream *substream, int state)
1da177e4 1046{
877211f5 1047 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1048 snd_pcm_trigger_tstamp(substream);
1049 if (substream->timer)
877211f5
TI
1050 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MSUSPEND,
1051 &runtime->trigger_tstamp);
1da177e4
LT
1052 runtime->status->suspended_state = runtime->status->state;
1053 runtime->status->state = SNDRV_PCM_STATE_SUSPENDED;
1054 snd_pcm_tick_set(substream, 0);
1055 wake_up(&runtime->sleep);
1056}
1057
1058static struct action_ops snd_pcm_action_suspend = {
1059 .pre_action = snd_pcm_pre_suspend,
1060 .do_action = snd_pcm_do_suspend,
1061 .post_action = snd_pcm_post_suspend
1062};
1063
1064/**
1065 * snd_pcm_suspend
df8db936 1066 * @substream: the PCM substream
1da177e4
LT
1067 *
1068 * Trigger SUSPEND to all linked streams.
1069 * After this call, all streams are changed to SUSPENDED state.
1070 */
877211f5 1071int snd_pcm_suspend(struct snd_pcm_substream *substream)
1da177e4
LT
1072{
1073 int err;
1074 unsigned long flags;
1075
1076 snd_pcm_stream_lock_irqsave(substream, flags);
1077 err = snd_pcm_action(&snd_pcm_action_suspend, substream, 0);
1078 snd_pcm_stream_unlock_irqrestore(substream, flags);
1079 return err;
1080}
1081
1082/**
1083 * snd_pcm_suspend_all
df8db936 1084 * @pcm: the PCM instance
1da177e4
LT
1085 *
1086 * Trigger SUSPEND to all substreams in the given pcm.
1087 * After this call, all streams are changed to SUSPENDED state.
1088 */
877211f5 1089int snd_pcm_suspend_all(struct snd_pcm *pcm)
1da177e4 1090{
877211f5 1091 struct snd_pcm_substream *substream;
1da177e4
LT
1092 int stream, err = 0;
1093
1094 for (stream = 0; stream < 2; stream++) {
877211f5
TI
1095 for (substream = pcm->streams[stream].substream;
1096 substream; substream = substream->next) {
1da177e4
LT
1097 /* FIXME: the open/close code should lock this as well */
1098 if (substream->runtime == NULL)
1099 continue;
1100 err = snd_pcm_suspend(substream);
1101 if (err < 0 && err != -EBUSY)
1102 return err;
1103 }
1104 }
1105 return 0;
1106}
1107
1108/* resume */
1109
877211f5 1110static int snd_pcm_pre_resume(struct snd_pcm_substream *substream, int state)
1da177e4 1111{
877211f5 1112 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1113 if (!(runtime->info & SNDRV_PCM_INFO_RESUME))
1114 return -ENOSYS;
1115 runtime->trigger_master = substream;
1116 return 0;
1117}
1118
877211f5 1119static int snd_pcm_do_resume(struct snd_pcm_substream *substream, int state)
1da177e4 1120{
877211f5 1121 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1122 if (runtime->trigger_master != substream)
1123 return 0;
1124 /* DMA not running previously? */
1125 if (runtime->status->suspended_state != SNDRV_PCM_STATE_RUNNING &&
1126 (runtime->status->suspended_state != SNDRV_PCM_STATE_DRAINING ||
1127 substream->stream != SNDRV_PCM_STREAM_PLAYBACK))
1128 return 0;
1129 return substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_RESUME);
1130}
1131
877211f5 1132static void snd_pcm_undo_resume(struct snd_pcm_substream *substream, int state)
1da177e4
LT
1133{
1134 if (substream->runtime->trigger_master == substream &&
1135 snd_pcm_running(substream))
1136 substream->ops->trigger(substream, SNDRV_PCM_TRIGGER_SUSPEND);
1137}
1138
877211f5 1139static void snd_pcm_post_resume(struct snd_pcm_substream *substream, int state)
1da177e4 1140{
877211f5 1141 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1142 snd_pcm_trigger_tstamp(substream);
1143 if (substream->timer)
877211f5
TI
1144 snd_timer_notify(substream->timer, SNDRV_TIMER_EVENT_MRESUME,
1145 &runtime->trigger_tstamp);
1da177e4
LT
1146 runtime->status->state = runtime->status->suspended_state;
1147 if (runtime->sleep_min)
1148 snd_pcm_tick_prepare(substream);
1149}
1150
1151static struct action_ops snd_pcm_action_resume = {
1152 .pre_action = snd_pcm_pre_resume,
1153 .do_action = snd_pcm_do_resume,
1154 .undo_action = snd_pcm_undo_resume,
1155 .post_action = snd_pcm_post_resume
1156};
1157
877211f5 1158static int snd_pcm_resume(struct snd_pcm_substream *substream)
1da177e4 1159{
877211f5 1160 struct snd_card *card = substream->pcm->card;
1da177e4
LT
1161 int res;
1162
1163 snd_power_lock(card);
1164 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1165 res = snd_pcm_action_lock_irq(&snd_pcm_action_resume, substream, 0);
1166 snd_power_unlock(card);
1167 return res;
1168}
1169
1170#else
1171
877211f5 1172static int snd_pcm_resume(struct snd_pcm_substream *substream)
1da177e4
LT
1173{
1174 return -ENOSYS;
1175}
1176
1177#endif /* CONFIG_PM */
1178
1179/*
1180 * xrun ioctl
1181 *
1182 * Change the RUNNING stream(s) to XRUN state.
1183 */
877211f5 1184static int snd_pcm_xrun(struct snd_pcm_substream *substream)
1da177e4 1185{
877211f5
TI
1186 struct snd_card *card = substream->pcm->card;
1187 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1188 int result;
1189
1190 snd_power_lock(card);
1191 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1192 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1193 if (result < 0)
1194 goto _unlock;
1195 }
1196
1197 snd_pcm_stream_lock_irq(substream);
1198 switch (runtime->status->state) {
1199 case SNDRV_PCM_STATE_XRUN:
1200 result = 0; /* already there */
1201 break;
1202 case SNDRV_PCM_STATE_RUNNING:
1203 result = snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
1204 break;
1205 default:
1206 result = -EBADFD;
1207 }
1208 snd_pcm_stream_unlock_irq(substream);
1209 _unlock:
1210 snd_power_unlock(card);
1211 return result;
1212}
1213
1214/*
1215 * reset ioctl
1216 */
877211f5 1217static int snd_pcm_pre_reset(struct snd_pcm_substream *substream, int state)
1da177e4 1218{
877211f5 1219 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1220 switch (runtime->status->state) {
1221 case SNDRV_PCM_STATE_RUNNING:
1222 case SNDRV_PCM_STATE_PREPARED:
1223 case SNDRV_PCM_STATE_PAUSED:
1224 case SNDRV_PCM_STATE_SUSPENDED:
1225 return 0;
1226 default:
1227 return -EBADFD;
1228 }
1229}
1230
877211f5 1231static int snd_pcm_do_reset(struct snd_pcm_substream *substream, int state)
1da177e4 1232{
877211f5 1233 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1234 int err = substream->ops->ioctl(substream, SNDRV_PCM_IOCTL1_RESET, NULL);
1235 if (err < 0)
1236 return err;
1237 // snd_assert(runtime->status->hw_ptr < runtime->buffer_size, );
1238 runtime->hw_ptr_base = 0;
877211f5
TI
1239 runtime->hw_ptr_interrupt = runtime->status->hw_ptr -
1240 runtime->status->hw_ptr % runtime->period_size;
1da177e4
LT
1241 runtime->silence_start = runtime->status->hw_ptr;
1242 runtime->silence_filled = 0;
1243 return 0;
1244}
1245
877211f5 1246static void snd_pcm_post_reset(struct snd_pcm_substream *substream, int state)
1da177e4 1247{
877211f5 1248 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1249 runtime->control->appl_ptr = runtime->status->hw_ptr;
1250 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
1251 runtime->silence_size > 0)
1252 snd_pcm_playback_silence(substream, ULONG_MAX);
1253}
1254
1255static struct action_ops snd_pcm_action_reset = {
1256 .pre_action = snd_pcm_pre_reset,
1257 .do_action = snd_pcm_do_reset,
1258 .post_action = snd_pcm_post_reset
1259};
1260
877211f5 1261static int snd_pcm_reset(struct snd_pcm_substream *substream)
1da177e4
LT
1262{
1263 return snd_pcm_action_nonatomic(&snd_pcm_action_reset, substream, 0);
1264}
1265
1266/*
1267 * prepare ioctl
1268 */
877211f5 1269static int snd_pcm_pre_prepare(struct snd_pcm_substream *substream, int state)
1da177e4 1270{
877211f5 1271 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1272 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1273 return -EBADFD;
1274 if (snd_pcm_running(substream))
1275 return -EBUSY;
1276 return 0;
1277}
1278
877211f5 1279static int snd_pcm_do_prepare(struct snd_pcm_substream *substream, int state)
1da177e4
LT
1280{
1281 int err;
1282 err = substream->ops->prepare(substream);
1283 if (err < 0)
1284 return err;
1285 return snd_pcm_do_reset(substream, 0);
1286}
1287
877211f5 1288static void snd_pcm_post_prepare(struct snd_pcm_substream *substream, int state)
1da177e4 1289{
877211f5 1290 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1291 runtime->control->appl_ptr = runtime->status->hw_ptr;
1292 runtime->status->state = SNDRV_PCM_STATE_PREPARED;
1293}
1294
1295static struct action_ops snd_pcm_action_prepare = {
1296 .pre_action = snd_pcm_pre_prepare,
1297 .do_action = snd_pcm_do_prepare,
1298 .post_action = snd_pcm_post_prepare
1299};
1300
1301/**
1302 * snd_pcm_prepare
df8db936
TI
1303 * @substream: the PCM substream instance
1304 *
1305 * Prepare the PCM substream to be triggerable.
1da177e4 1306 */
877211f5 1307int snd_pcm_prepare(struct snd_pcm_substream *substream)
1da177e4
LT
1308{
1309 int res;
877211f5 1310 struct snd_card *card = substream->pcm->card;
1da177e4
LT
1311
1312 snd_power_lock(card);
1313 if ((res = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile)) >= 0)
1314 res = snd_pcm_action_nonatomic(&snd_pcm_action_prepare, substream, 0);
1315 snd_power_unlock(card);
1316 return res;
1317}
1318
1319/*
1320 * drain ioctl
1321 */
1322
877211f5 1323static int snd_pcm_pre_drain_init(struct snd_pcm_substream *substream, int state)
1da177e4
LT
1324{
1325 if (substream->ffile->f_flags & O_NONBLOCK)
1326 return -EAGAIN;
1327 substream->runtime->trigger_master = substream;
1328 return 0;
1329}
1330
877211f5 1331static int snd_pcm_do_drain_init(struct snd_pcm_substream *substream, int state)
1da177e4 1332{
877211f5 1333 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
1334 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1335 switch (runtime->status->state) {
1336 case SNDRV_PCM_STATE_PREPARED:
1337 /* start playback stream if possible */
1338 if (! snd_pcm_playback_empty(substream)) {
1339 snd_pcm_do_start(substream, SNDRV_PCM_STATE_DRAINING);
1340 snd_pcm_post_start(substream, SNDRV_PCM_STATE_DRAINING);
1341 }
1342 break;
1343 case SNDRV_PCM_STATE_RUNNING:
1344 runtime->status->state = SNDRV_PCM_STATE_DRAINING;
1345 break;
1346 default:
1347 break;
1348 }
1349 } else {
1350 /* stop running stream */
1351 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING) {
1352 int state = snd_pcm_capture_avail(runtime) > 0 ?
1353 SNDRV_PCM_STATE_DRAINING : SNDRV_PCM_STATE_SETUP;
1354 snd_pcm_do_stop(substream, state);
1355 snd_pcm_post_stop(substream, state);
1356 }
1357 }
1358 return 0;
1359}
1360
877211f5 1361static void snd_pcm_post_drain_init(struct snd_pcm_substream *substream, int state)
1da177e4
LT
1362{
1363}
1364
1365static struct action_ops snd_pcm_action_drain_init = {
1366 .pre_action = snd_pcm_pre_drain_init,
1367 .do_action = snd_pcm_do_drain_init,
1368 .post_action = snd_pcm_post_drain_init
1369};
1370
1371struct drain_rec {
877211f5 1372 struct snd_pcm_substream *substream;
1da177e4
LT
1373 wait_queue_t wait;
1374 snd_pcm_uframes_t stop_threshold;
1375};
1376
877211f5 1377static int snd_pcm_drop(struct snd_pcm_substream *substream);
1da177e4
LT
1378
1379/*
1380 * Drain the stream(s).
1381 * When the substream is linked, sync until the draining of all playback streams
1382 * is finished.
1383 * After this call, all streams are supposed to be either SETUP or DRAINING
1384 * (capture only) state.
1385 */
877211f5 1386static int snd_pcm_drain(struct snd_pcm_substream *substream)
1da177e4 1387{
877211f5
TI
1388 struct snd_card *card;
1389 struct snd_pcm_runtime *runtime;
1da177e4
LT
1390 struct list_head *pos;
1391 int result = 0;
1392 int i, num_drecs;
1393 struct drain_rec *drec, drec_tmp, *d;
1394
1395 snd_assert(substream != NULL, return -ENXIO);
1396 card = substream->pcm->card;
1397 runtime = substream->runtime;
1398
1399 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1400 return -EBADFD;
1401
1da177e4
LT
1402 snd_power_lock(card);
1403 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1404 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
21cb2a2e
TI
1405 if (result < 0) {
1406 snd_power_unlock(card);
1407 return result;
1408 }
1da177e4
LT
1409 }
1410
1411 /* allocate temporary record for drain sync */
21cb2a2e 1412 down_read(&snd_pcm_link_rwsem);
1da177e4
LT
1413 if (snd_pcm_stream_linked(substream)) {
1414 drec = kmalloc(substream->group->count * sizeof(*drec), GFP_KERNEL);
1415 if (! drec) {
21cb2a2e
TI
1416 up_read(&snd_pcm_link_rwsem);
1417 snd_power_unlock(card);
1418 return -ENOMEM;
1da177e4
LT
1419 }
1420 } else
1421 drec = &drec_tmp;
1422
21cb2a2e 1423 /* count only playback streams */
1da177e4
LT
1424 num_drecs = 0;
1425 snd_pcm_group_for_each(pos, substream) {
877211f5 1426 struct snd_pcm_substream *s = snd_pcm_group_substream_entry(pos);
1da177e4 1427 runtime = s->runtime;
1da177e4
LT
1428 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1429 d = &drec[num_drecs++];
1430 d->substream = s;
1431 init_waitqueue_entry(&d->wait, current);
1432 add_wait_queue(&runtime->sleep, &d->wait);
1433 /* stop_threshold fixup to avoid endless loop when
1434 * stop_threshold > buffer_size
1435 */
1436 d->stop_threshold = runtime->stop_threshold;
1437 if (runtime->stop_threshold > runtime->buffer_size)
1438 runtime->stop_threshold = runtime->buffer_size;
1439 }
1440 }
21cb2a2e 1441 up_read(&snd_pcm_link_rwsem);
1da177e4 1442 if (! num_drecs)
21cb2a2e
TI
1443 goto _error;
1444
1445 snd_pcm_stream_lock_irq(substream);
1446 /* resume pause */
1447 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1448 snd_pcm_pause(substream, 0);
1449
1450 /* pre-start/stop - all running streams are changed to DRAINING state */
1451 result = snd_pcm_action(&snd_pcm_action_drain_init, substream, 0);
1452 if (result < 0) {
1453 snd_pcm_stream_unlock_irq(substream);
1454 goto _error;
1455 }
1da177e4
LT
1456
1457 for (;;) {
1458 long tout;
1459 if (signal_pending(current)) {
1460 result = -ERESTARTSYS;
1461 break;
1462 }
21cb2a2e
TI
1463 /* all finished? */
1464 for (i = 0; i < num_drecs; i++) {
1465 runtime = drec[i].substream->runtime;
1466 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
1467 break;
1468 }
1469 if (i == num_drecs)
1470 break; /* yes, all drained */
1471
1da177e4
LT
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 snd_pcm_stream_unlock_irq(substream);
1474 snd_power_unlock(card);
1475 tout = schedule_timeout(10 * HZ);
1476 snd_power_lock(card);
1477 snd_pcm_stream_lock_irq(substream);
1478 if (tout == 0) {
1479 if (substream->runtime->status->state == SNDRV_PCM_STATE_SUSPENDED)
1480 result = -ESTRPIPE;
1481 else {
1482 snd_printd("playback drain error (DMA or IRQ trouble?)\n");
1483 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1484 result = -EIO;
1485 }
1486 break;
1487 }
1da177e4 1488 }
21cb2a2e
TI
1489
1490 snd_pcm_stream_unlock_irq(substream);
1491
1492 _error:
1da177e4
LT
1493 for (i = 0; i < num_drecs; i++) {
1494 d = &drec[i];
1495 runtime = d->substream->runtime;
1496 remove_wait_queue(&runtime->sleep, &d->wait);
1497 runtime->stop_threshold = d->stop_threshold;
1498 }
1499
1da177e4
LT
1500 if (drec != &drec_tmp)
1501 kfree(drec);
1da177e4 1502 snd_power_unlock(card);
1da177e4
LT
1503
1504 return result;
1505}
1506
1507/*
1508 * drop ioctl
1509 *
1510 * Immediately put all linked substreams into SETUP state.
1511 */
877211f5 1512static int snd_pcm_drop(struct snd_pcm_substream *substream)
1da177e4 1513{
877211f5
TI
1514 struct snd_pcm_runtime *runtime;
1515 struct snd_card *card;
1da177e4
LT
1516 int result = 0;
1517
1518 snd_assert(substream != NULL, return -ENXIO);
1519 runtime = substream->runtime;
1520 card = substream->pcm->card;
1521
1522 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1523 return -EBADFD;
1524
1525 snd_power_lock(card);
1526 if (runtime->status->state == SNDRV_PCM_STATE_SUSPENDED) {
1527 result = snd_power_wait(card, SNDRV_CTL_POWER_D0, substream->ffile);
1528 if (result < 0)
1529 goto _unlock;
1530 }
1531
1532 snd_pcm_stream_lock_irq(substream);
1533 /* resume pause */
1534 if (runtime->status->state == SNDRV_PCM_STATE_PAUSED)
1535 snd_pcm_pause(substream, 0);
1536
1537 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1538 /* runtime->control->appl_ptr = runtime->status->hw_ptr; */
1539 snd_pcm_stream_unlock_irq(substream);
1540 _unlock:
1541 snd_power_unlock(card);
1542 return result;
1543}
1544
1545
1546/* WARNING: Don't forget to fput back the file */
1da177e4
LT
1547static struct file *snd_pcm_file_fd(int fd)
1548{
1549 struct file *file;
1550 struct inode *inode;
1551 unsigned short minor;
1552 file = fget(fd);
1553 if (!file)
1554 return NULL;
1555 inode = file->f_dentry->d_inode;
1556 if (!S_ISCHR(inode->i_mode) ||
1557 imajor(inode) != snd_major) {
1558 fput(file);
1559 return NULL;
1560 }
1561 minor = iminor(inode);
1562 if (minor >= 256 ||
1563 minor % SNDRV_MINOR_DEVICES < SNDRV_MINOR_PCM_PLAYBACK) {
1564 fput(file);
1565 return NULL;
1566 }
1567 return file;
1568}
1569
1570/*
1571 * PCM link handling
1572 */
877211f5 1573static int snd_pcm_link(struct snd_pcm_substream *substream, int fd)
1da177e4
LT
1574{
1575 int res = 0;
1576 struct file *file;
877211f5
TI
1577 struct snd_pcm_file *pcm_file;
1578 struct snd_pcm_substream *substream1;
1da177e4
LT
1579
1580 file = snd_pcm_file_fd(fd);
1581 if (!file)
1582 return -EBADFD;
1583 pcm_file = file->private_data;
1584 substream1 = pcm_file->substream;
1585 down_write(&snd_pcm_link_rwsem);
1586 write_lock_irq(&snd_pcm_link_rwlock);
1587 if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN ||
1588 substream->runtime->status->state != substream1->runtime->status->state) {
1589 res = -EBADFD;
1590 goto _end;
1591 }
1592 if (snd_pcm_stream_linked(substream1)) {
1593 res = -EALREADY;
1594 goto _end;
1595 }
1596 if (!snd_pcm_stream_linked(substream)) {
877211f5 1597 substream->group = kmalloc(sizeof(struct snd_pcm_group), GFP_ATOMIC);
1da177e4
LT
1598 if (substream->group == NULL) {
1599 res = -ENOMEM;
1600 goto _end;
1601 }
1602 spin_lock_init(&substream->group->lock);
1603 INIT_LIST_HEAD(&substream->group->substreams);
1604 list_add_tail(&substream->link_list, &substream->group->substreams);
1605 substream->group->count = 1;
1606 }
1607 list_add_tail(&substream1->link_list, &substream->group->substreams);
1608 substream->group->count++;
1609 substream1->group = substream->group;
1610 _end:
1611 write_unlock_irq(&snd_pcm_link_rwlock);
1612 up_write(&snd_pcm_link_rwsem);
1613 fput(file);
1614 return res;
1615}
1616
877211f5 1617static void relink_to_local(struct snd_pcm_substream *substream)
1da177e4
LT
1618{
1619 substream->group = &substream->self_group;
1620 INIT_LIST_HEAD(&substream->self_group.substreams);
1621 list_add_tail(&substream->link_list, &substream->self_group.substreams);
1622}
1623
877211f5 1624static int snd_pcm_unlink(struct snd_pcm_substream *substream)
1da177e4
LT
1625{
1626 struct list_head *pos;
1627 int res = 0;
1628
1629 down_write(&snd_pcm_link_rwsem);
1630 write_lock_irq(&snd_pcm_link_rwlock);
1631 if (!snd_pcm_stream_linked(substream)) {
1632 res = -EALREADY;
1633 goto _end;
1634 }
1635 list_del(&substream->link_list);
1636 substream->group->count--;
1637 if (substream->group->count == 1) { /* detach the last stream, too */
1638 snd_pcm_group_for_each(pos, substream) {
1639 relink_to_local(snd_pcm_group_substream_entry(pos));
1640 break;
1641 }
1642 kfree(substream->group);
1643 }
1644 relink_to_local(substream);
1645 _end:
1646 write_unlock_irq(&snd_pcm_link_rwlock);
1647 up_write(&snd_pcm_link_rwsem);
1648 return res;
1649}
1650
1651/*
1652 * hw configurator
1653 */
877211f5
TI
1654static int snd_pcm_hw_rule_mul(struct snd_pcm_hw_params *params,
1655 struct snd_pcm_hw_rule *rule)
1da177e4 1656{
877211f5 1657 struct snd_interval t;
1da177e4
LT
1658 snd_interval_mul(hw_param_interval_c(params, rule->deps[0]),
1659 hw_param_interval_c(params, rule->deps[1]), &t);
1660 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1661}
1662
877211f5
TI
1663static int snd_pcm_hw_rule_div(struct snd_pcm_hw_params *params,
1664 struct snd_pcm_hw_rule *rule)
1da177e4 1665{
877211f5 1666 struct snd_interval t;
1da177e4
LT
1667 snd_interval_div(hw_param_interval_c(params, rule->deps[0]),
1668 hw_param_interval_c(params, rule->deps[1]), &t);
1669 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1670}
1671
877211f5
TI
1672static int snd_pcm_hw_rule_muldivk(struct snd_pcm_hw_params *params,
1673 struct snd_pcm_hw_rule *rule)
1da177e4 1674{
877211f5 1675 struct snd_interval t;
1da177e4
LT
1676 snd_interval_muldivk(hw_param_interval_c(params, rule->deps[0]),
1677 hw_param_interval_c(params, rule->deps[1]),
1678 (unsigned long) rule->private, &t);
1679 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1680}
1681
877211f5
TI
1682static int snd_pcm_hw_rule_mulkdiv(struct snd_pcm_hw_params *params,
1683 struct snd_pcm_hw_rule *rule)
1da177e4 1684{
877211f5 1685 struct snd_interval t;
1da177e4
LT
1686 snd_interval_mulkdiv(hw_param_interval_c(params, rule->deps[0]),
1687 (unsigned long) rule->private,
1688 hw_param_interval_c(params, rule->deps[1]), &t);
1689 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1690}
1691
877211f5
TI
1692static int snd_pcm_hw_rule_format(struct snd_pcm_hw_params *params,
1693 struct snd_pcm_hw_rule *rule)
1da177e4
LT
1694{
1695 unsigned int k;
877211f5
TI
1696 struct snd_interval *i = hw_param_interval(params, rule->deps[0]);
1697 struct snd_mask m;
1698 struct snd_mask *mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
1da177e4
LT
1699 snd_mask_any(&m);
1700 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1701 int bits;
1702 if (! snd_mask_test(mask, k))
1703 continue;
1704 bits = snd_pcm_format_physical_width(k);
1705 if (bits <= 0)
1706 continue; /* ignore invalid formats */
1707 if ((unsigned)bits < i->min || (unsigned)bits > i->max)
1708 snd_mask_reset(&m, k);
1709 }
1710 return snd_mask_refine(mask, &m);
1711}
1712
877211f5
TI
1713static int snd_pcm_hw_rule_sample_bits(struct snd_pcm_hw_params *params,
1714 struct snd_pcm_hw_rule *rule)
1da177e4 1715{
877211f5 1716 struct snd_interval t;
1da177e4
LT
1717 unsigned int k;
1718 t.min = UINT_MAX;
1719 t.max = 0;
1720 t.openmin = 0;
1721 t.openmax = 0;
1722 for (k = 0; k <= SNDRV_PCM_FORMAT_LAST; ++k) {
1723 int bits;
1724 if (! snd_mask_test(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), k))
1725 continue;
1726 bits = snd_pcm_format_physical_width(k);
1727 if (bits <= 0)
1728 continue; /* ignore invalid formats */
1729 if (t.min > (unsigned)bits)
1730 t.min = bits;
1731 if (t.max < (unsigned)bits)
1732 t.max = bits;
1733 }
1734 t.integer = 1;
1735 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1736}
1737
1738#if SNDRV_PCM_RATE_5512 != 1 << 0 || SNDRV_PCM_RATE_192000 != 1 << 12
1739#error "Change this table"
1740#endif
1741
1742static unsigned int rates[] = { 5512, 8000, 11025, 16000, 22050, 32000, 44100,
1743 48000, 64000, 88200, 96000, 176400, 192000 };
1744
877211f5
TI
1745static int snd_pcm_hw_rule_rate(struct snd_pcm_hw_params *params,
1746 struct snd_pcm_hw_rule *rule)
1da177e4 1747{
877211f5 1748 struct snd_pcm_hardware *hw = rule->private;
1da177e4
LT
1749 return snd_interval_list(hw_param_interval(params, rule->var),
1750 ARRAY_SIZE(rates), rates, hw->rates);
1751}
1752
877211f5
TI
1753static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params,
1754 struct snd_pcm_hw_rule *rule)
1da177e4 1755{
877211f5
TI
1756 struct snd_interval t;
1757 struct snd_pcm_substream *substream = rule->private;
1da177e4
LT
1758 t.min = 0;
1759 t.max = substream->buffer_bytes_max;
1760 t.openmin = 0;
1761 t.openmax = 0;
1762 t.integer = 1;
1763 return snd_interval_refine(hw_param_interval(params, rule->var), &t);
1764}
1765
877211f5 1766int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream)
1da177e4 1767{
877211f5
TI
1768 struct snd_pcm_runtime *runtime = substream->runtime;
1769 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1da177e4
LT
1770 int k, err;
1771
1772 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++) {
1773 snd_mask_any(constrs_mask(constrs, k));
1774 }
1775
1776 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++) {
1777 snd_interval_any(constrs_interval(constrs, k));
1778 }
1779
1780 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_CHANNELS));
1781 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_SIZE));
1782 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_BUFFER_BYTES));
1783 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_SAMPLE_BITS));
1784 snd_interval_setinteger(constrs_interval(constrs, SNDRV_PCM_HW_PARAM_FRAME_BITS));
1785
1786 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
1787 snd_pcm_hw_rule_format, NULL,
1788 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1789 if (err < 0)
1790 return err;
1791 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1792 snd_pcm_hw_rule_sample_bits, NULL,
1793 SNDRV_PCM_HW_PARAM_FORMAT,
1794 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1795 if (err < 0)
1796 return err;
1797 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1798 snd_pcm_hw_rule_div, NULL,
1799 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1800 if (err < 0)
1801 return err;
1802 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1803 snd_pcm_hw_rule_mul, NULL,
1804 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, SNDRV_PCM_HW_PARAM_CHANNELS, -1);
1805 if (err < 0)
1806 return err;
1807 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FRAME_BITS,
1808 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1809 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -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_mulkdiv, (void*) 8,
1814 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, -1);
1815 if (err < 0)
1816 return err;
1817 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1818 snd_pcm_hw_rule_div, NULL,
1819 SNDRV_PCM_HW_PARAM_FRAME_BITS, SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1820 if (err < 0)
1821 return err;
1822 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1823 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1824 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_TIME, -1);
1825 if (err < 0)
1826 return err;
1827 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1828 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1829 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_BUFFER_TIME, -1);
1830 if (err < 0)
1831 return err;
1832 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS,
1833 snd_pcm_hw_rule_div, NULL,
1834 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, -1);
1835 if (err < 0)
1836 return err;
1837 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1838 snd_pcm_hw_rule_div, NULL,
1839 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1840 if (err < 0)
1841 return err;
1842 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1843 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1844 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -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_muldivk, (void*) 1000000,
1849 SNDRV_PCM_HW_PARAM_PERIOD_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1850 if (err < 0)
1851 return err;
1852 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1853 snd_pcm_hw_rule_mul, NULL,
1854 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_PERIODS, -1);
1855 if (err < 0)
1856 return err;
1857 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1858 snd_pcm_hw_rule_mulkdiv, (void*) 8,
1859 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, SNDRV_PCM_HW_PARAM_FRAME_BITS, -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_muldivk, (void*) 1000000,
1864 SNDRV_PCM_HW_PARAM_BUFFER_TIME, SNDRV_PCM_HW_PARAM_RATE, -1);
1865 if (err < 0)
1866 return err;
1867 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1868 snd_pcm_hw_rule_muldivk, (void*) 8,
1869 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 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_BYTES,
1873 snd_pcm_hw_rule_muldivk, (void*) 8,
1874 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_FRAME_BITS, -1);
1875 if (err < 0)
1876 return err;
1877 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1878 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1879 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1880 if (err < 0)
1881 return err;
1882 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
1883 snd_pcm_hw_rule_mulkdiv, (void*) 1000000,
1884 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, SNDRV_PCM_HW_PARAM_RATE, -1);
1885 if (err < 0)
1886 return err;
1887 return 0;
1888}
1889
877211f5 1890int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream)
1da177e4 1891{
877211f5
TI
1892 struct snd_pcm_runtime *runtime = substream->runtime;
1893 struct snd_pcm_hardware *hw = &runtime->hw;
1da177e4
LT
1894 int err;
1895 unsigned int mask = 0;
1896
1897 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1898 mask |= 1 << SNDRV_PCM_ACCESS_RW_INTERLEAVED;
1899 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1900 mask |= 1 << SNDRV_PCM_ACCESS_RW_NONINTERLEAVED;
1901 if (hw->info & SNDRV_PCM_INFO_MMAP) {
1902 if (hw->info & SNDRV_PCM_INFO_INTERLEAVED)
1903 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_INTERLEAVED;
1904 if (hw->info & SNDRV_PCM_INFO_NONINTERLEAVED)
1905 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED;
1906 if (hw->info & SNDRV_PCM_INFO_COMPLEX)
1907 mask |= 1 << SNDRV_PCM_ACCESS_MMAP_COMPLEX;
1908 }
1909 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_ACCESS, mask);
1910 snd_assert(err >= 0, return -EINVAL);
1911
1912 err = snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, hw->formats);
1913 snd_assert(err >= 0, return -EINVAL);
1914
1915 err = snd_pcm_hw_constraint_mask(runtime, SNDRV_PCM_HW_PARAM_SUBFORMAT, 1 << SNDRV_PCM_SUBFORMAT_STD);
1916 snd_assert(err >= 0, return -EINVAL);
1917
1918 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
1919 hw->channels_min, hw->channels_max);
1920 snd_assert(err >= 0, return -EINVAL);
1921
1922 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
1923 hw->rate_min, hw->rate_max);
1924 snd_assert(err >= 0, return -EINVAL);
1925
1926 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1927 hw->period_bytes_min, hw->period_bytes_max);
1928 snd_assert(err >= 0, return -EINVAL);
1929
1930 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIODS,
1931 hw->periods_min, hw->periods_max);
1932 snd_assert(err >= 0, return -EINVAL);
1933
1934 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1935 hw->period_bytes_min, hw->buffer_bytes_max);
1936 snd_assert(err >= 0, return -EINVAL);
1937
1938 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1939 snd_pcm_hw_rule_buffer_bytes_max, substream,
1940 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, -1);
1941 if (err < 0)
1942 return err;
1943
1944 /* FIXME: remove */
1945 if (runtime->dma_bytes) {
1946 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, runtime->dma_bytes);
1947 snd_assert(err >= 0, return -EINVAL);
1948 }
1949
1950 if (!(hw->rates & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))) {
1951 err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1952 snd_pcm_hw_rule_rate, hw,
1953 SNDRV_PCM_HW_PARAM_RATE, -1);
1954 if (err < 0)
1955 return err;
1956 }
1957
1958 /* FIXME: this belong to lowlevel */
1959 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_TICK_TIME,
1960 1000000 / HZ, 1000000 / HZ);
1961 snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
1962
1963 return 0;
1964}
1965
877211f5
TI
1966static void snd_pcm_add_file(struct snd_pcm_str *str,
1967 struct snd_pcm_file *pcm_file)
1da177e4
LT
1968{
1969 pcm_file->next = str->files;
1970 str->files = pcm_file;
1971}
1972
877211f5
TI
1973static void snd_pcm_remove_file(struct snd_pcm_str *str,
1974 struct snd_pcm_file *pcm_file)
1da177e4 1975{
877211f5 1976 struct snd_pcm_file * pcm_file1;
1da177e4
LT
1977 if (str->files == pcm_file) {
1978 str->files = pcm_file->next;
1979 } else {
1980 pcm_file1 = str->files;
1981 while (pcm_file1 && pcm_file1->next != pcm_file)
1982 pcm_file1 = pcm_file1->next;
1983 if (pcm_file1 != NULL)
1984 pcm_file1->next = pcm_file->next;
1985 }
1986}
1987
877211f5 1988static int snd_pcm_release_file(struct snd_pcm_file * pcm_file)
1da177e4 1989{
877211f5
TI
1990 struct snd_pcm_substream *substream;
1991 struct snd_pcm_runtime *runtime;
1992 struct snd_pcm_str * str;
1da177e4
LT
1993
1994 snd_assert(pcm_file != NULL, return -ENXIO);
1995 substream = pcm_file->substream;
1996 snd_assert(substream != NULL, return -ENXIO);
1997 runtime = substream->runtime;
1998 str = substream->pstr;
1999 snd_pcm_unlink(substream);
443feb88 2000 if (substream->ffile != NULL) {
1da177e4
LT
2001 if (substream->ops->hw_free != NULL)
2002 substream->ops->hw_free(substream);
2003 substream->ops->close(substream);
443feb88 2004 substream->ffile = NULL;
1da177e4 2005 }
1da177e4
LT
2006 snd_pcm_remove_file(str, pcm_file);
2007 snd_pcm_release_substream(substream);
2008 kfree(pcm_file);
2009 return 0;
2010}
2011
2012static int snd_pcm_open_file(struct file *file,
877211f5 2013 struct snd_pcm *pcm,
1da177e4 2014 int stream,
877211f5 2015 struct snd_pcm_file **rpcm_file)
1da177e4
LT
2016{
2017 int err = 0;
877211f5
TI
2018 struct snd_pcm_file *pcm_file;
2019 struct snd_pcm_substream *substream;
2020 struct snd_pcm_str *str;
1da177e4
LT
2021
2022 snd_assert(rpcm_file != NULL, return -EINVAL);
2023 *rpcm_file = NULL;
2024
ca2c0966 2025 pcm_file = kzalloc(sizeof(*pcm_file), GFP_KERNEL);
1da177e4
LT
2026 if (pcm_file == NULL) {
2027 return -ENOMEM;
2028 }
2029
2030 if ((err = snd_pcm_open_substream(pcm, stream, &substream)) < 0) {
2031 kfree(pcm_file);
2032 return err;
2033 }
2034
2035 str = substream->pstr;
2036 substream->file = pcm_file;
2037 substream->no_mmap_ctrl = 0;
2038
2039 pcm_file->substream = substream;
2040
2041 snd_pcm_add_file(str, pcm_file);
2042
2043 err = snd_pcm_hw_constraints_init(substream);
2044 if (err < 0) {
2045 snd_printd("snd_pcm_hw_constraints_init failed\n");
2046 snd_pcm_release_file(pcm_file);
2047 return err;
2048 }
2049
2050 if ((err = substream->ops->open(substream)) < 0) {
2051 snd_pcm_release_file(pcm_file);
2052 return err;
2053 }
443feb88 2054 substream->ffile = file;
1da177e4
LT
2055
2056 err = snd_pcm_hw_constraints_complete(substream);
2057 if (err < 0) {
2058 snd_printd("snd_pcm_hw_constraints_complete failed\n");
1da177e4
LT
2059 snd_pcm_release_file(pcm_file);
2060 return err;
2061 }
2062
1da177e4
LT
2063 file->private_data = pcm_file;
2064 *rpcm_file = pcm_file;
2065 return 0;
2066}
2067
2068static int snd_pcm_open(struct inode *inode, struct file *file)
2069{
2070 int cardnum = SNDRV_MINOR_CARD(iminor(inode));
2071 int device = SNDRV_MINOR_DEVICE(iminor(inode));
2072 int err;
877211f5
TI
2073 struct snd_pcm *pcm;
2074 struct snd_pcm_file *pcm_file;
1da177e4
LT
2075 wait_queue_t wait;
2076
7c22f1aa
TI
2077 if (device < SNDRV_MINOR_PCM_PLAYBACK || device >= SNDRV_MINOR_DEVICES)
2078 return -ENXIO;
1da177e4
LT
2079 pcm = snd_pcm_devices[(cardnum * SNDRV_PCM_DEVICES) + (device % SNDRV_MINOR_PCMS)];
2080 if (pcm == NULL) {
2081 err = -ENODEV;
2082 goto __error1;
2083 }
2084 err = snd_card_file_add(pcm->card, file);
2085 if (err < 0)
2086 goto __error1;
2087 if (!try_module_get(pcm->card->module)) {
2088 err = -EFAULT;
2089 goto __error2;
2090 }
2091 init_waitqueue_entry(&wait, current);
2092 add_wait_queue(&pcm->open_wait, &wait);
2093 down(&pcm->open_mutex);
2094 while (1) {
2095 err = snd_pcm_open_file(file, pcm, device >= SNDRV_MINOR_PCM_CAPTURE ? SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK, &pcm_file);
2096 if (err >= 0)
2097 break;
2098 if (err == -EAGAIN) {
2099 if (file->f_flags & O_NONBLOCK) {
2100 err = -EBUSY;
2101 break;
2102 }
2103 } else
2104 break;
2105 set_current_state(TASK_INTERRUPTIBLE);
2106 up(&pcm->open_mutex);
2107 schedule();
2108 down(&pcm->open_mutex);
2109 if (signal_pending(current)) {
2110 err = -ERESTARTSYS;
2111 break;
2112 }
2113 }
2114 remove_wait_queue(&pcm->open_wait, &wait);
2115 up(&pcm->open_mutex);
2116 if (err < 0)
2117 goto __error;
2118 return err;
2119
2120 __error:
2121 module_put(pcm->card->module);
2122 __error2:
2123 snd_card_file_remove(pcm->card, file);
2124 __error1:
2125 return err;
2126}
2127
2128static int snd_pcm_release(struct inode *inode, struct file *file)
2129{
877211f5
TI
2130 struct snd_pcm *pcm;
2131 struct snd_pcm_substream *substream;
2132 struct snd_pcm_file *pcm_file;
1da177e4
LT
2133
2134 pcm_file = file->private_data;
2135 substream = pcm_file->substream;
2136 snd_assert(substream != NULL, return -ENXIO);
2137 snd_assert(!atomic_read(&substream->runtime->mmap_count), );
2138 pcm = substream->pcm;
2139 snd_pcm_drop(substream);
2140 fasync_helper(-1, file, 0, &substream->runtime->fasync);
2141 down(&pcm->open_mutex);
2142 snd_pcm_release_file(pcm_file);
2143 up(&pcm->open_mutex);
2144 wake_up(&pcm->open_wait);
2145 module_put(pcm->card->module);
2146 snd_card_file_remove(pcm->card, file);
2147 return 0;
2148}
2149
877211f5
TI
2150static snd_pcm_sframes_t snd_pcm_playback_rewind(struct snd_pcm_substream *substream,
2151 snd_pcm_uframes_t frames)
1da177e4 2152{
877211f5 2153 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2154 snd_pcm_sframes_t appl_ptr;
2155 snd_pcm_sframes_t ret;
2156 snd_pcm_sframes_t hw_avail;
2157
2158 if (frames == 0)
2159 return 0;
2160
2161 snd_pcm_stream_lock_irq(substream);
2162 switch (runtime->status->state) {
2163 case SNDRV_PCM_STATE_PREPARED:
2164 break;
2165 case SNDRV_PCM_STATE_DRAINING:
2166 case SNDRV_PCM_STATE_RUNNING:
2167 if (snd_pcm_update_hw_ptr(substream) >= 0)
2168 break;
2169 /* Fall through */
2170 case SNDRV_PCM_STATE_XRUN:
2171 ret = -EPIPE;
2172 goto __end;
2173 default:
2174 ret = -EBADFD;
2175 goto __end;
2176 }
2177
2178 hw_avail = snd_pcm_playback_hw_avail(runtime);
2179 if (hw_avail <= 0) {
2180 ret = 0;
2181 goto __end;
2182 }
2183 if (frames > (snd_pcm_uframes_t)hw_avail)
2184 frames = hw_avail;
2185 else
2186 frames -= frames % runtime->xfer_align;
2187 appl_ptr = runtime->control->appl_ptr - frames;
2188 if (appl_ptr < 0)
2189 appl_ptr += runtime->boundary;
2190 runtime->control->appl_ptr = appl_ptr;
2191 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2192 runtime->sleep_min)
2193 snd_pcm_tick_prepare(substream);
2194 ret = frames;
2195 __end:
2196 snd_pcm_stream_unlock_irq(substream);
2197 return ret;
2198}
2199
877211f5
TI
2200static snd_pcm_sframes_t snd_pcm_capture_rewind(struct snd_pcm_substream *substream,
2201 snd_pcm_uframes_t frames)
1da177e4 2202{
877211f5 2203 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2204 snd_pcm_sframes_t appl_ptr;
2205 snd_pcm_sframes_t ret;
2206 snd_pcm_sframes_t hw_avail;
2207
2208 if (frames == 0)
2209 return 0;
2210
2211 snd_pcm_stream_lock_irq(substream);
2212 switch (runtime->status->state) {
2213 case SNDRV_PCM_STATE_PREPARED:
2214 case SNDRV_PCM_STATE_DRAINING:
2215 break;
2216 case SNDRV_PCM_STATE_RUNNING:
2217 if (snd_pcm_update_hw_ptr(substream) >= 0)
2218 break;
2219 /* Fall through */
2220 case SNDRV_PCM_STATE_XRUN:
2221 ret = -EPIPE;
2222 goto __end;
2223 default:
2224 ret = -EBADFD;
2225 goto __end;
2226 }
2227
2228 hw_avail = snd_pcm_capture_hw_avail(runtime);
2229 if (hw_avail <= 0) {
2230 ret = 0;
2231 goto __end;
2232 }
2233 if (frames > (snd_pcm_uframes_t)hw_avail)
2234 frames = hw_avail;
2235 else
2236 frames -= frames % runtime->xfer_align;
2237 appl_ptr = runtime->control->appl_ptr - frames;
2238 if (appl_ptr < 0)
2239 appl_ptr += runtime->boundary;
2240 runtime->control->appl_ptr = appl_ptr;
2241 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2242 runtime->sleep_min)
2243 snd_pcm_tick_prepare(substream);
2244 ret = frames;
2245 __end:
2246 snd_pcm_stream_unlock_irq(substream);
2247 return ret;
2248}
2249
877211f5
TI
2250static snd_pcm_sframes_t snd_pcm_playback_forward(struct snd_pcm_substream *substream,
2251 snd_pcm_uframes_t frames)
1da177e4 2252{
877211f5 2253 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2254 snd_pcm_sframes_t appl_ptr;
2255 snd_pcm_sframes_t ret;
2256 snd_pcm_sframes_t avail;
2257
2258 if (frames == 0)
2259 return 0;
2260
2261 snd_pcm_stream_lock_irq(substream);
2262 switch (runtime->status->state) {
2263 case SNDRV_PCM_STATE_PREPARED:
2264 case SNDRV_PCM_STATE_PAUSED:
2265 break;
2266 case SNDRV_PCM_STATE_DRAINING:
2267 case SNDRV_PCM_STATE_RUNNING:
2268 if (snd_pcm_update_hw_ptr(substream) >= 0)
2269 break;
2270 /* Fall through */
2271 case SNDRV_PCM_STATE_XRUN:
2272 ret = -EPIPE;
2273 goto __end;
2274 default:
2275 ret = -EBADFD;
2276 goto __end;
2277 }
2278
2279 avail = snd_pcm_playback_avail(runtime);
2280 if (avail <= 0) {
2281 ret = 0;
2282 goto __end;
2283 }
2284 if (frames > (snd_pcm_uframes_t)avail)
2285 frames = avail;
2286 else
2287 frames -= frames % runtime->xfer_align;
2288 appl_ptr = runtime->control->appl_ptr + frames;
2289 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2290 appl_ptr -= runtime->boundary;
2291 runtime->control->appl_ptr = appl_ptr;
2292 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2293 runtime->sleep_min)
2294 snd_pcm_tick_prepare(substream);
2295 ret = frames;
2296 __end:
2297 snd_pcm_stream_unlock_irq(substream);
2298 return ret;
2299}
2300
877211f5
TI
2301static snd_pcm_sframes_t snd_pcm_capture_forward(struct snd_pcm_substream *substream,
2302 snd_pcm_uframes_t frames)
1da177e4 2303{
877211f5 2304 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2305 snd_pcm_sframes_t appl_ptr;
2306 snd_pcm_sframes_t ret;
2307 snd_pcm_sframes_t avail;
2308
2309 if (frames == 0)
2310 return 0;
2311
2312 snd_pcm_stream_lock_irq(substream);
2313 switch (runtime->status->state) {
2314 case SNDRV_PCM_STATE_PREPARED:
2315 case SNDRV_PCM_STATE_DRAINING:
2316 case SNDRV_PCM_STATE_PAUSED:
2317 break;
2318 case SNDRV_PCM_STATE_RUNNING:
2319 if (snd_pcm_update_hw_ptr(substream) >= 0)
2320 break;
2321 /* Fall through */
2322 case SNDRV_PCM_STATE_XRUN:
2323 ret = -EPIPE;
2324 goto __end;
2325 default:
2326 ret = -EBADFD;
2327 goto __end;
2328 }
2329
2330 avail = snd_pcm_capture_avail(runtime);
2331 if (avail <= 0) {
2332 ret = 0;
2333 goto __end;
2334 }
2335 if (frames > (snd_pcm_uframes_t)avail)
2336 frames = avail;
2337 else
2338 frames -= frames % runtime->xfer_align;
2339 appl_ptr = runtime->control->appl_ptr + frames;
2340 if (appl_ptr >= (snd_pcm_sframes_t)runtime->boundary)
2341 appl_ptr -= runtime->boundary;
2342 runtime->control->appl_ptr = appl_ptr;
2343 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING &&
2344 runtime->sleep_min)
2345 snd_pcm_tick_prepare(substream);
2346 ret = frames;
2347 __end:
2348 snd_pcm_stream_unlock_irq(substream);
2349 return ret;
2350}
2351
877211f5 2352static int snd_pcm_hwsync(struct snd_pcm_substream *substream)
1da177e4 2353{
877211f5 2354 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2355 int err;
2356
2357 snd_pcm_stream_lock_irq(substream);
2358 switch (runtime->status->state) {
2359 case SNDRV_PCM_STATE_DRAINING:
2360 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2361 goto __badfd;
2362 case SNDRV_PCM_STATE_RUNNING:
2363 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2364 break;
2365 /* Fall through */
2366 case SNDRV_PCM_STATE_PREPARED:
2367 case SNDRV_PCM_STATE_SUSPENDED:
2368 err = 0;
2369 break;
2370 case SNDRV_PCM_STATE_XRUN:
2371 err = -EPIPE;
2372 break;
2373 default:
2374 __badfd:
2375 err = -EBADFD;
2376 break;
2377 }
2378 snd_pcm_stream_unlock_irq(substream);
2379 return err;
2380}
2381
877211f5
TI
2382static int snd_pcm_delay(struct snd_pcm_substream *substream,
2383 snd_pcm_sframes_t __user *res)
1da177e4 2384{
877211f5 2385 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2386 int err;
2387 snd_pcm_sframes_t n = 0;
2388
2389 snd_pcm_stream_lock_irq(substream);
2390 switch (runtime->status->state) {
2391 case SNDRV_PCM_STATE_DRAINING:
2392 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
2393 goto __badfd;
2394 case SNDRV_PCM_STATE_RUNNING:
2395 if ((err = snd_pcm_update_hw_ptr(substream)) < 0)
2396 break;
2397 /* Fall through */
2398 case SNDRV_PCM_STATE_PREPARED:
2399 case SNDRV_PCM_STATE_SUSPENDED:
2400 err = 0;
2401 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
2402 n = snd_pcm_playback_hw_avail(runtime);
2403 else
2404 n = snd_pcm_capture_avail(runtime);
2405 break;
2406 case SNDRV_PCM_STATE_XRUN:
2407 err = -EPIPE;
2408 break;
2409 default:
2410 __badfd:
2411 err = -EBADFD;
2412 break;
2413 }
2414 snd_pcm_stream_unlock_irq(substream);
2415 if (!err)
2416 if (put_user(n, res))
2417 err = -EFAULT;
2418 return err;
2419}
2420
877211f5
TI
2421static int snd_pcm_sync_ptr(struct snd_pcm_substream *substream,
2422 struct snd_pcm_sync_ptr __user *_sync_ptr)
1da177e4 2423{
877211f5
TI
2424 struct snd_pcm_runtime *runtime = substream->runtime;
2425 struct snd_pcm_sync_ptr sync_ptr;
2426 volatile struct snd_pcm_mmap_status *status;
2427 volatile struct snd_pcm_mmap_control *control;
1da177e4
LT
2428 int err;
2429
2430 memset(&sync_ptr, 0, sizeof(sync_ptr));
2431 if (get_user(sync_ptr.flags, (unsigned __user *)&(_sync_ptr->flags)))
2432 return -EFAULT;
877211f5 2433 if (copy_from_user(&sync_ptr.c.control, &(_sync_ptr->c.control), sizeof(struct snd_pcm_mmap_control)))
1da177e4
LT
2434 return -EFAULT;
2435 status = runtime->status;
2436 control = runtime->control;
2437 if (sync_ptr.flags & SNDRV_PCM_SYNC_PTR_HWSYNC) {
2438 err = snd_pcm_hwsync(substream);
2439 if (err < 0)
2440 return err;
2441 }
2442 snd_pcm_stream_lock_irq(substream);
2443 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_APPL))
2444 control->appl_ptr = sync_ptr.c.control.appl_ptr;
2445 else
2446 sync_ptr.c.control.appl_ptr = control->appl_ptr;
2447 if (!(sync_ptr.flags & SNDRV_PCM_SYNC_PTR_AVAIL_MIN))
2448 control->avail_min = sync_ptr.c.control.avail_min;
2449 else
2450 sync_ptr.c.control.avail_min = control->avail_min;
2451 sync_ptr.s.status.state = status->state;
2452 sync_ptr.s.status.hw_ptr = status->hw_ptr;
2453 sync_ptr.s.status.tstamp = status->tstamp;
2454 sync_ptr.s.status.suspended_state = status->suspended_state;
2455 snd_pcm_stream_unlock_irq(substream);
2456 if (copy_to_user(_sync_ptr, &sync_ptr, sizeof(sync_ptr)))
2457 return -EFAULT;
2458 return 0;
2459}
2460
877211f5 2461static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
1da177e4 2462 unsigned int cmd, void __user *arg);
877211f5 2463static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
1da177e4
LT
2464 unsigned int cmd, void __user *arg);
2465
877211f5 2466static int snd_pcm_common_ioctl1(struct snd_pcm_substream *substream,
1da177e4
LT
2467 unsigned int cmd, void __user *arg)
2468{
2469 snd_assert(substream != NULL, return -ENXIO);
2470
2471 switch (cmd) {
2472 case SNDRV_PCM_IOCTL_PVERSION:
2473 return put_user(SNDRV_PCM_VERSION, (int __user *)arg) ? -EFAULT : 0;
2474 case SNDRV_PCM_IOCTL_INFO:
2475 return snd_pcm_info_user(substream, arg);
07799e75 2476 case SNDRV_PCM_IOCTL_TSTAMP: /* just for compatibility */
1da177e4 2477 return 0;
1da177e4
LT
2478 case SNDRV_PCM_IOCTL_HW_REFINE:
2479 return snd_pcm_hw_refine_user(substream, arg);
2480 case SNDRV_PCM_IOCTL_HW_PARAMS:
2481 return snd_pcm_hw_params_user(substream, arg);
2482 case SNDRV_PCM_IOCTL_HW_FREE:
2483 return snd_pcm_hw_free(substream);
2484 case SNDRV_PCM_IOCTL_SW_PARAMS:
2485 return snd_pcm_sw_params_user(substream, arg);
2486 case SNDRV_PCM_IOCTL_STATUS:
2487 return snd_pcm_status_user(substream, arg);
2488 case SNDRV_PCM_IOCTL_CHANNEL_INFO:
2489 return snd_pcm_channel_info_user(substream, arg);
2490 case SNDRV_PCM_IOCTL_PREPARE:
2491 return snd_pcm_prepare(substream);
2492 case SNDRV_PCM_IOCTL_RESET:
2493 return snd_pcm_reset(substream);
2494 case SNDRV_PCM_IOCTL_START:
2495 return snd_pcm_action_lock_irq(&snd_pcm_action_start, substream, SNDRV_PCM_STATE_RUNNING);
2496 case SNDRV_PCM_IOCTL_LINK:
2497 return snd_pcm_link(substream, (int)(unsigned long) arg);
2498 case SNDRV_PCM_IOCTL_UNLINK:
2499 return snd_pcm_unlink(substream);
2500 case SNDRV_PCM_IOCTL_RESUME:
2501 return snd_pcm_resume(substream);
2502 case SNDRV_PCM_IOCTL_XRUN:
2503 return snd_pcm_xrun(substream);
2504 case SNDRV_PCM_IOCTL_HWSYNC:
2505 return snd_pcm_hwsync(substream);
2506 case SNDRV_PCM_IOCTL_DELAY:
2507 return snd_pcm_delay(substream, arg);
2508 case SNDRV_PCM_IOCTL_SYNC_PTR:
2509 return snd_pcm_sync_ptr(substream, arg);
2510 case SNDRV_PCM_IOCTL_HW_REFINE_OLD:
2511 return snd_pcm_hw_refine_old_user(substream, arg);
2512 case SNDRV_PCM_IOCTL_HW_PARAMS_OLD:
2513 return snd_pcm_hw_params_old_user(substream, arg);
2514 case SNDRV_PCM_IOCTL_DRAIN:
2515 return snd_pcm_drain(substream);
2516 case SNDRV_PCM_IOCTL_DROP:
2517 return snd_pcm_drop(substream);
2518 }
2519 snd_printd("unknown ioctl = 0x%x\n", cmd);
2520 return -ENOTTY;
2521}
2522
877211f5 2523static int snd_pcm_playback_ioctl1(struct snd_pcm_substream *substream,
1da177e4
LT
2524 unsigned int cmd, void __user *arg)
2525{
2526 snd_assert(substream != NULL, return -ENXIO);
2527 snd_assert(substream->stream == SNDRV_PCM_STREAM_PLAYBACK, return -EINVAL);
2528 switch (cmd) {
2529 case SNDRV_PCM_IOCTL_WRITEI_FRAMES:
2530 {
877211f5
TI
2531 struct snd_xferi xferi;
2532 struct snd_xferi __user *_xferi = arg;
2533 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2534 snd_pcm_sframes_t result;
2535 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2536 return -EBADFD;
2537 if (put_user(0, &_xferi->result))
2538 return -EFAULT;
2539 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2540 return -EFAULT;
2541 result = snd_pcm_lib_write(substream, xferi.buf, xferi.frames);
2542 __put_user(result, &_xferi->result);
2543 return result < 0 ? result : 0;
2544 }
2545 case SNDRV_PCM_IOCTL_WRITEN_FRAMES:
2546 {
877211f5
TI
2547 struct snd_xfern xfern;
2548 struct snd_xfern __user *_xfern = arg;
2549 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2550 void __user **bufs;
2551 snd_pcm_sframes_t result;
2552 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2553 return -EBADFD;
2554 if (runtime->channels > 128)
2555 return -EINVAL;
2556 if (put_user(0, &_xfern->result))
2557 return -EFAULT;
2558 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2559 return -EFAULT;
2560 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2561 if (bufs == NULL)
2562 return -ENOMEM;
2563 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2564 kfree(bufs);
2565 return -EFAULT;
2566 }
2567 result = snd_pcm_lib_writev(substream, bufs, xfern.frames);
2568 kfree(bufs);
2569 __put_user(result, &_xfern->result);
2570 return result < 0 ? result : 0;
2571 }
2572 case SNDRV_PCM_IOCTL_REWIND:
2573 {
2574 snd_pcm_uframes_t frames;
2575 snd_pcm_uframes_t __user *_frames = arg;
2576 snd_pcm_sframes_t result;
2577 if (get_user(frames, _frames))
2578 return -EFAULT;
2579 if (put_user(0, _frames))
2580 return -EFAULT;
2581 result = snd_pcm_playback_rewind(substream, frames);
2582 __put_user(result, _frames);
2583 return result < 0 ? result : 0;
2584 }
2585 case SNDRV_PCM_IOCTL_FORWARD:
2586 {
2587 snd_pcm_uframes_t frames;
2588 snd_pcm_uframes_t __user *_frames = arg;
2589 snd_pcm_sframes_t result;
2590 if (get_user(frames, _frames))
2591 return -EFAULT;
2592 if (put_user(0, _frames))
2593 return -EFAULT;
2594 result = snd_pcm_playback_forward(substream, frames);
2595 __put_user(result, _frames);
2596 return result < 0 ? result : 0;
2597 }
2598 case SNDRV_PCM_IOCTL_PAUSE:
2599 {
2600 int res;
2601 snd_pcm_stream_lock_irq(substream);
2602 res = snd_pcm_pause(substream, (int)(unsigned long)arg);
2603 snd_pcm_stream_unlock_irq(substream);
2604 return res;
2605 }
2606 }
2607 return snd_pcm_common_ioctl1(substream, cmd, arg);
2608}
2609
877211f5 2610static int snd_pcm_capture_ioctl1(struct snd_pcm_substream *substream,
1da177e4
LT
2611 unsigned int cmd, void __user *arg)
2612{
2613 snd_assert(substream != NULL, return -ENXIO);
2614 snd_assert(substream->stream == SNDRV_PCM_STREAM_CAPTURE, return -EINVAL);
2615 switch (cmd) {
2616 case SNDRV_PCM_IOCTL_READI_FRAMES:
2617 {
877211f5
TI
2618 struct snd_xferi xferi;
2619 struct snd_xferi __user *_xferi = arg;
2620 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2621 snd_pcm_sframes_t result;
2622 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2623 return -EBADFD;
2624 if (put_user(0, &_xferi->result))
2625 return -EFAULT;
2626 if (copy_from_user(&xferi, _xferi, sizeof(xferi)))
2627 return -EFAULT;
2628 result = snd_pcm_lib_read(substream, xferi.buf, xferi.frames);
2629 __put_user(result, &_xferi->result);
2630 return result < 0 ? result : 0;
2631 }
2632 case SNDRV_PCM_IOCTL_READN_FRAMES:
2633 {
877211f5
TI
2634 struct snd_xfern xfern;
2635 struct snd_xfern __user *_xfern = arg;
2636 struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4
LT
2637 void *bufs;
2638 snd_pcm_sframes_t result;
2639 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2640 return -EBADFD;
2641 if (runtime->channels > 128)
2642 return -EINVAL;
2643 if (put_user(0, &_xfern->result))
2644 return -EFAULT;
2645 if (copy_from_user(&xfern, _xfern, sizeof(xfern)))
2646 return -EFAULT;
2647 bufs = kmalloc(sizeof(void *) * runtime->channels, GFP_KERNEL);
2648 if (bufs == NULL)
2649 return -ENOMEM;
2650 if (copy_from_user(bufs, xfern.bufs, sizeof(void *) * runtime->channels)) {
2651 kfree(bufs);
2652 return -EFAULT;
2653 }
2654 result = snd_pcm_lib_readv(substream, bufs, xfern.frames);
2655 kfree(bufs);
2656 __put_user(result, &_xfern->result);
2657 return result < 0 ? result : 0;
2658 }
2659 case SNDRV_PCM_IOCTL_REWIND:
2660 {
2661 snd_pcm_uframes_t frames;
2662 snd_pcm_uframes_t __user *_frames = arg;
2663 snd_pcm_sframes_t result;
2664 if (get_user(frames, _frames))
2665 return -EFAULT;
2666 if (put_user(0, _frames))
2667 return -EFAULT;
2668 result = snd_pcm_capture_rewind(substream, frames);
2669 __put_user(result, _frames);
2670 return result < 0 ? result : 0;
2671 }
2672 case SNDRV_PCM_IOCTL_FORWARD:
2673 {
2674 snd_pcm_uframes_t frames;
2675 snd_pcm_uframes_t __user *_frames = arg;
2676 snd_pcm_sframes_t result;
2677 if (get_user(frames, _frames))
2678 return -EFAULT;
2679 if (put_user(0, _frames))
2680 return -EFAULT;
2681 result = snd_pcm_capture_forward(substream, frames);
2682 __put_user(result, _frames);
2683 return result < 0 ? result : 0;
2684 }
2685 }
2686 return snd_pcm_common_ioctl1(substream, cmd, arg);
2687}
2688
877211f5
TI
2689static long snd_pcm_playback_ioctl(struct file *file, unsigned int cmd,
2690 unsigned long arg)
1da177e4 2691{
877211f5 2692 struct snd_pcm_file *pcm_file;
1da177e4
LT
2693
2694 pcm_file = file->private_data;
2695
2696 if (((cmd >> 8) & 0xff) != 'A')
2697 return -ENOTTY;
2698
2699 return snd_pcm_playback_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2700}
2701
877211f5
TI
2702static long snd_pcm_capture_ioctl(struct file *file, unsigned int cmd,
2703 unsigned long arg)
1da177e4 2704{
877211f5 2705 struct snd_pcm_file *pcm_file;
1da177e4
LT
2706
2707 pcm_file = file->private_data;
2708
2709 if (((cmd >> 8) & 0xff) != 'A')
2710 return -ENOTTY;
2711
2712 return snd_pcm_capture_ioctl1(pcm_file->substream, cmd, (void __user *)arg);
2713}
2714
877211f5 2715int snd_pcm_kernel_playback_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
2716 unsigned int cmd, void *arg)
2717{
2718 mm_segment_t fs;
2719 int result;
2720
2721 fs = snd_enter_user();
2722 result = snd_pcm_playback_ioctl1(substream, cmd, (void __user *)arg);
2723 snd_leave_user(fs);
2724 return result;
2725}
2726
877211f5 2727int snd_pcm_kernel_capture_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
2728 unsigned int cmd, void *arg)
2729{
2730 mm_segment_t fs;
2731 int result;
2732
2733 fs = snd_enter_user();
2734 result = snd_pcm_capture_ioctl1(substream, cmd, (void __user *)arg);
2735 snd_leave_user(fs);
2736 return result;
2737}
2738
877211f5 2739int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream,
1da177e4
LT
2740 unsigned int cmd, void *arg)
2741{
2742 switch (substream->stream) {
2743 case SNDRV_PCM_STREAM_PLAYBACK:
2744 return snd_pcm_kernel_playback_ioctl(substream, cmd, arg);
2745 case SNDRV_PCM_STREAM_CAPTURE:
2746 return snd_pcm_kernel_capture_ioctl(substream, cmd, arg);
2747 default:
2748 return -EINVAL;
2749 }
2750}
2751
877211f5
TI
2752static ssize_t snd_pcm_read(struct file *file, char __user *buf, size_t count,
2753 loff_t * offset)
1da177e4 2754{
877211f5
TI
2755 struct snd_pcm_file *pcm_file;
2756 struct snd_pcm_substream *substream;
2757 struct snd_pcm_runtime *runtime;
1da177e4
LT
2758 snd_pcm_sframes_t result;
2759
2760 pcm_file = file->private_data;
2761 substream = pcm_file->substream;
2762 snd_assert(substream != NULL, return -ENXIO);
2763 runtime = substream->runtime;
2764 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2765 return -EBADFD;
2766 if (!frame_aligned(runtime, count))
2767 return -EINVAL;
2768 count = bytes_to_frames(runtime, count);
2769 result = snd_pcm_lib_read(substream, buf, count);
2770 if (result > 0)
2771 result = frames_to_bytes(runtime, result);
2772 return result;
2773}
2774
877211f5
TI
2775static ssize_t snd_pcm_write(struct file *file, const char __user *buf,
2776 size_t count, loff_t * offset)
1da177e4 2777{
877211f5
TI
2778 struct snd_pcm_file *pcm_file;
2779 struct snd_pcm_substream *substream;
2780 struct snd_pcm_runtime *runtime;
1da177e4
LT
2781 snd_pcm_sframes_t result;
2782
2783 pcm_file = file->private_data;
2784 substream = pcm_file->substream;
2785 snd_assert(substream != NULL, result = -ENXIO; goto end);
2786 runtime = substream->runtime;
2787 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2788 result = -EBADFD;
2789 goto end;
2790 }
2791 if (!frame_aligned(runtime, count)) {
2792 result = -EINVAL;
2793 goto end;
2794 }
2795 count = bytes_to_frames(runtime, count);
2796 result = snd_pcm_lib_write(substream, buf, count);
2797 if (result > 0)
2798 result = frames_to_bytes(runtime, result);
2799 end:
2800 return result;
2801}
2802
2803static ssize_t snd_pcm_readv(struct file *file, const struct iovec *_vector,
2804 unsigned long count, loff_t * offset)
2805
2806{
877211f5
TI
2807 struct snd_pcm_file *pcm_file;
2808 struct snd_pcm_substream *substream;
2809 struct snd_pcm_runtime *runtime;
1da177e4
LT
2810 snd_pcm_sframes_t result;
2811 unsigned long i;
2812 void __user **bufs;
2813 snd_pcm_uframes_t frames;
2814
2815 pcm_file = file->private_data;
2816 substream = pcm_file->substream;
2817 snd_assert(substream != NULL, return -ENXIO);
2818 runtime = substream->runtime;
2819 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2820 return -EBADFD;
2821 if (count > 1024 || count != runtime->channels)
2822 return -EINVAL;
2823 if (!frame_aligned(runtime, _vector->iov_len))
2824 return -EINVAL;
2825 frames = bytes_to_samples(runtime, _vector->iov_len);
2826 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2827 if (bufs == NULL)
2828 return -ENOMEM;
2829 for (i = 0; i < count; ++i)
2830 bufs[i] = _vector[i].iov_base;
2831 result = snd_pcm_lib_readv(substream, bufs, frames);
2832 if (result > 0)
2833 result = frames_to_bytes(runtime, result);
2834 kfree(bufs);
2835 return result;
2836}
2837
2838static ssize_t snd_pcm_writev(struct file *file, const struct iovec *_vector,
2839 unsigned long count, loff_t * offset)
2840{
877211f5
TI
2841 struct snd_pcm_file *pcm_file;
2842 struct snd_pcm_substream *substream;
2843 struct snd_pcm_runtime *runtime;
1da177e4
LT
2844 snd_pcm_sframes_t result;
2845 unsigned long i;
2846 void __user **bufs;
2847 snd_pcm_uframes_t frames;
2848
2849 pcm_file = file->private_data;
2850 substream = pcm_file->substream;
2851 snd_assert(substream != NULL, result = -ENXIO; goto end);
2852 runtime = substream->runtime;
2853 if (runtime->status->state == SNDRV_PCM_STATE_OPEN) {
2854 result = -EBADFD;
2855 goto end;
2856 }
2857 if (count > 128 || count != runtime->channels ||
2858 !frame_aligned(runtime, _vector->iov_len)) {
2859 result = -EINVAL;
2860 goto end;
2861 }
2862 frames = bytes_to_samples(runtime, _vector->iov_len);
2863 bufs = kmalloc(sizeof(void *) * count, GFP_KERNEL);
2864 if (bufs == NULL)
2865 return -ENOMEM;
2866 for (i = 0; i < count; ++i)
2867 bufs[i] = _vector[i].iov_base;
2868 result = snd_pcm_lib_writev(substream, bufs, frames);
2869 if (result > 0)
2870 result = frames_to_bytes(runtime, result);
2871 kfree(bufs);
2872 end:
2873 return result;
2874}
2875
2876static unsigned int snd_pcm_playback_poll(struct file *file, poll_table * wait)
2877{
877211f5
TI
2878 struct snd_pcm_file *pcm_file;
2879 struct snd_pcm_substream *substream;
2880 struct snd_pcm_runtime *runtime;
1da177e4
LT
2881 unsigned int mask;
2882 snd_pcm_uframes_t avail;
2883
2884 pcm_file = file->private_data;
2885
2886 substream = pcm_file->substream;
2887 snd_assert(substream != NULL, return -ENXIO);
2888 runtime = substream->runtime;
2889
2890 poll_wait(file, &runtime->sleep, wait);
2891
2892 snd_pcm_stream_lock_irq(substream);
2893 avail = snd_pcm_playback_avail(runtime);
2894 switch (runtime->status->state) {
2895 case SNDRV_PCM_STATE_RUNNING:
2896 case SNDRV_PCM_STATE_PREPARED:
2897 case SNDRV_PCM_STATE_PAUSED:
2898 if (avail >= runtime->control->avail_min) {
2899 mask = POLLOUT | POLLWRNORM;
2900 break;
2901 }
2902 /* Fall through */
2903 case SNDRV_PCM_STATE_DRAINING:
2904 mask = 0;
2905 break;
2906 default:
2907 mask = POLLOUT | POLLWRNORM | POLLERR;
2908 break;
2909 }
2910 snd_pcm_stream_unlock_irq(substream);
2911 return mask;
2912}
2913
2914static unsigned int snd_pcm_capture_poll(struct file *file, poll_table * wait)
2915{
877211f5
TI
2916 struct snd_pcm_file *pcm_file;
2917 struct snd_pcm_substream *substream;
2918 struct snd_pcm_runtime *runtime;
1da177e4
LT
2919 unsigned int mask;
2920 snd_pcm_uframes_t avail;
2921
2922 pcm_file = file->private_data;
2923
2924 substream = pcm_file->substream;
2925 snd_assert(substream != NULL, return -ENXIO);
2926 runtime = substream->runtime;
2927
2928 poll_wait(file, &runtime->sleep, wait);
2929
2930 snd_pcm_stream_lock_irq(substream);
2931 avail = snd_pcm_capture_avail(runtime);
2932 switch (runtime->status->state) {
2933 case SNDRV_PCM_STATE_RUNNING:
2934 case SNDRV_PCM_STATE_PREPARED:
2935 case SNDRV_PCM_STATE_PAUSED:
2936 if (avail >= runtime->control->avail_min) {
2937 mask = POLLIN | POLLRDNORM;
2938 break;
2939 }
2940 mask = 0;
2941 break;
2942 case SNDRV_PCM_STATE_DRAINING:
2943 if (avail > 0) {
2944 mask = POLLIN | POLLRDNORM;
2945 break;
2946 }
2947 /* Fall through */
2948 default:
2949 mask = POLLIN | POLLRDNORM | POLLERR;
2950 break;
2951 }
2952 snd_pcm_stream_unlock_irq(substream);
2953 return mask;
2954}
2955
2956/*
2957 * mmap support
2958 */
2959
2960/*
2961 * Only on coherent architectures, we can mmap the status and the control records
2962 * for effcient data transfer. On others, we have to use HWSYNC ioctl...
2963 */
2964#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
2965/*
2966 * mmap status record
2967 */
877211f5
TI
2968static struct page * snd_pcm_mmap_status_nopage(struct vm_area_struct *area,
2969 unsigned long address, int *type)
1da177e4 2970{
877211f5
TI
2971 struct snd_pcm_substream *substream = area->vm_private_data;
2972 struct snd_pcm_runtime *runtime;
1da177e4
LT
2973 struct page * page;
2974
2975 if (substream == NULL)
2976 return NOPAGE_OOM;
2977 runtime = substream->runtime;
2978 page = virt_to_page(runtime->status);
b5810039 2979 get_page(page);
1da177e4
LT
2980 if (type)
2981 *type = VM_FAULT_MINOR;
2982 return page;
2983}
2984
2985static struct vm_operations_struct snd_pcm_vm_ops_status =
2986{
2987 .nopage = snd_pcm_mmap_status_nopage,
2988};
2989
877211f5 2990static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
1da177e4
LT
2991 struct vm_area_struct *area)
2992{
877211f5 2993 struct snd_pcm_runtime *runtime;
1da177e4
LT
2994 long size;
2995 if (!(area->vm_flags & VM_READ))
2996 return -EINVAL;
2997 runtime = substream->runtime;
2998 snd_assert(runtime != NULL, return -EAGAIN);
2999 size = area->vm_end - area->vm_start;
877211f5 3000 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_status)))
1da177e4
LT
3001 return -EINVAL;
3002 area->vm_ops = &snd_pcm_vm_ops_status;
3003 area->vm_private_data = substream;
3004 area->vm_flags |= VM_RESERVED;
3005 return 0;
3006}
3007
3008/*
3009 * mmap control record
3010 */
877211f5
TI
3011static struct page * snd_pcm_mmap_control_nopage(struct vm_area_struct *area,
3012 unsigned long address, int *type)
1da177e4 3013{
877211f5
TI
3014 struct snd_pcm_substream *substream = area->vm_private_data;
3015 struct snd_pcm_runtime *runtime;
1da177e4
LT
3016 struct page * page;
3017
3018 if (substream == NULL)
3019 return NOPAGE_OOM;
3020 runtime = substream->runtime;
3021 page = virt_to_page(runtime->control);
b5810039 3022 get_page(page);
1da177e4
LT
3023 if (type)
3024 *type = VM_FAULT_MINOR;
3025 return page;
3026}
3027
3028static struct vm_operations_struct snd_pcm_vm_ops_control =
3029{
3030 .nopage = snd_pcm_mmap_control_nopage,
3031};
3032
877211f5 3033static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
1da177e4
LT
3034 struct vm_area_struct *area)
3035{
877211f5 3036 struct snd_pcm_runtime *runtime;
1da177e4
LT
3037 long size;
3038 if (!(area->vm_flags & VM_READ))
3039 return -EINVAL;
3040 runtime = substream->runtime;
3041 snd_assert(runtime != NULL, return -EAGAIN);
3042 size = area->vm_end - area->vm_start;
877211f5 3043 if (size != PAGE_ALIGN(sizeof(struct snd_pcm_mmap_control)))
1da177e4
LT
3044 return -EINVAL;
3045 area->vm_ops = &snd_pcm_vm_ops_control;
3046 area->vm_private_data = substream;
3047 area->vm_flags |= VM_RESERVED;
3048 return 0;
3049}
3050#else /* ! coherent mmap */
3051/*
3052 * don't support mmap for status and control records.
3053 */
877211f5 3054static int snd_pcm_mmap_status(struct snd_pcm_substream *substream, struct file *file,
1da177e4
LT
3055 struct vm_area_struct *area)
3056{
3057 return -ENXIO;
3058}
877211f5 3059static int snd_pcm_mmap_control(struct snd_pcm_substream *substream, struct file *file,
1da177e4
LT
3060 struct vm_area_struct *area)
3061{
3062 return -ENXIO;
3063}
3064#endif /* coherent mmap */
3065
3066/*
3067 * nopage callback for mmapping a RAM page
3068 */
877211f5
TI
3069static struct page *snd_pcm_mmap_data_nopage(struct vm_area_struct *area,
3070 unsigned long address, int *type)
1da177e4 3071{
877211f5
TI
3072 struct snd_pcm_substream *substream = area->vm_private_data;
3073 struct snd_pcm_runtime *runtime;
1da177e4
LT
3074 unsigned long offset;
3075 struct page * page;
3076 void *vaddr;
3077 size_t dma_bytes;
3078
3079 if (substream == NULL)
3080 return NOPAGE_OOM;
3081 runtime = substream->runtime;
3082 offset = area->vm_pgoff << PAGE_SHIFT;
3083 offset += address - area->vm_start;
3084 snd_assert((offset % PAGE_SIZE) == 0, return NOPAGE_OOM);
3085 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3086 if (offset > dma_bytes - PAGE_SIZE)
3087 return NOPAGE_SIGBUS;
3088 if (substream->ops->page) {
3089 page = substream->ops->page(substream, offset);
3090 if (! page)
3091 return NOPAGE_OOM;
3092 } else {
3093 vaddr = runtime->dma_area + offset;
3094 page = virt_to_page(vaddr);
3095 }
b5810039 3096 get_page(page);
1da177e4
LT
3097 if (type)
3098 *type = VM_FAULT_MINOR;
3099 return page;
3100}
3101
3102static struct vm_operations_struct snd_pcm_vm_ops_data =
3103{
3104 .open = snd_pcm_mmap_data_open,
3105 .close = snd_pcm_mmap_data_close,
3106 .nopage = snd_pcm_mmap_data_nopage,
3107};
3108
3109/*
3110 * mmap the DMA buffer on RAM
3111 */
877211f5
TI
3112static int snd_pcm_default_mmap(struct snd_pcm_substream *substream,
3113 struct vm_area_struct *area)
1da177e4
LT
3114{
3115 area->vm_ops = &snd_pcm_vm_ops_data;
3116 area->vm_private_data = substream;
3117 area->vm_flags |= VM_RESERVED;
3118 atomic_inc(&substream->runtime->mmap_count);
3119 return 0;
3120}
3121
3122/*
3123 * mmap the DMA buffer on I/O memory area
3124 */
3125#if SNDRV_PCM_INFO_MMAP_IOMEM
3126static struct vm_operations_struct snd_pcm_vm_ops_data_mmio =
3127{
3128 .open = snd_pcm_mmap_data_open,
3129 .close = snd_pcm_mmap_data_close,
3130};
3131
877211f5
TI
3132int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream,
3133 struct vm_area_struct *area)
1da177e4
LT
3134{
3135 long size;
3136 unsigned long offset;
3137
3138#ifdef pgprot_noncached
3139 area->vm_page_prot = pgprot_noncached(area->vm_page_prot);
3140#endif
3141 area->vm_ops = &snd_pcm_vm_ops_data_mmio;
3142 area->vm_private_data = substream;
3143 area->vm_flags |= VM_IO;
3144 size = area->vm_end - area->vm_start;
3145 offset = area->vm_pgoff << PAGE_SHIFT;
3146 if (io_remap_pfn_range(area, area->vm_start,
3147 (substream->runtime->dma_addr + offset) >> PAGE_SHIFT,
3148 size, area->vm_page_prot))
3149 return -EAGAIN;
3150 atomic_inc(&substream->runtime->mmap_count);
3151 return 0;
3152}
3153#endif /* SNDRV_PCM_INFO_MMAP */
3154
3155/*
3156 * mmap DMA buffer
3157 */
877211f5 3158int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file,
1da177e4
LT
3159 struct vm_area_struct *area)
3160{
877211f5 3161 struct snd_pcm_runtime *runtime;
1da177e4
LT
3162 long size;
3163 unsigned long offset;
3164 size_t dma_bytes;
3165
3166 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
3167 if (!(area->vm_flags & (VM_WRITE|VM_READ)))
3168 return -EINVAL;
3169 } else {
3170 if (!(area->vm_flags & VM_READ))
3171 return -EINVAL;
3172 }
3173 runtime = substream->runtime;
3174 snd_assert(runtime != NULL, return -EAGAIN);
3175 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
3176 return -EBADFD;
3177 if (!(runtime->info & SNDRV_PCM_INFO_MMAP))
3178 return -ENXIO;
3179 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
3180 runtime->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
3181 return -EINVAL;
3182 size = area->vm_end - area->vm_start;
3183 offset = area->vm_pgoff << PAGE_SHIFT;
3184 dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
3185 if ((size_t)size > dma_bytes)
3186 return -EINVAL;
3187 if (offset > dma_bytes - size)
3188 return -EINVAL;
3189
3190 if (substream->ops->mmap)
3191 return substream->ops->mmap(substream, area);
3192 else
3193 return snd_pcm_default_mmap(substream, area);
3194}
3195
3196static int snd_pcm_mmap(struct file *file, struct vm_area_struct *area)
3197{
877211f5
TI
3198 struct snd_pcm_file * pcm_file;
3199 struct snd_pcm_substream *substream;
1da177e4
LT
3200 unsigned long offset;
3201
3202 pcm_file = file->private_data;
3203 substream = pcm_file->substream;
3204 snd_assert(substream != NULL, return -ENXIO);
3205
3206 offset = area->vm_pgoff << PAGE_SHIFT;
3207 switch (offset) {
3208 case SNDRV_PCM_MMAP_OFFSET_STATUS:
3209 if (substream->no_mmap_ctrl)
3210 return -ENXIO;
3211 return snd_pcm_mmap_status(substream, file, area);
3212 case SNDRV_PCM_MMAP_OFFSET_CONTROL:
3213 if (substream->no_mmap_ctrl)
3214 return -ENXIO;
3215 return snd_pcm_mmap_control(substream, file, area);
3216 default:
3217 return snd_pcm_mmap_data(substream, file, area);
3218 }
3219 return 0;
3220}
3221
3222static int snd_pcm_fasync(int fd, struct file * file, int on)
3223{
877211f5
TI
3224 struct snd_pcm_file * pcm_file;
3225 struct snd_pcm_substream *substream;
3226 struct snd_pcm_runtime *runtime;
1da177e4
LT
3227 int err;
3228
3229 pcm_file = file->private_data;
3230 substream = pcm_file->substream;
3231 snd_assert(substream != NULL, return -ENXIO);
3232 runtime = substream->runtime;
3233
3234 err = fasync_helper(fd, file, on, &runtime->fasync);
3235 if (err < 0)
3236 return err;
3237 return 0;
3238}
3239
3240/*
3241 * ioctl32 compat
3242 */
3243#ifdef CONFIG_COMPAT
3244#include "pcm_compat.c"
3245#else
3246#define snd_pcm_ioctl_compat NULL
3247#endif
3248
3249/*
3250 * To be removed helpers to keep binary compatibility
3251 */
3252
3253#define __OLD_TO_NEW_MASK(x) ((x&7)|((x&0x07fffff8)<<5))
3254#define __NEW_TO_OLD_MASK(x) ((x&7)|((x&0xffffff00)>>5))
3255
877211f5
TI
3256static void snd_pcm_hw_convert_from_old_params(struct snd_pcm_hw_params *params,
3257 struct snd_pcm_hw_params_old *oparams)
1da177e4
LT
3258{
3259 unsigned int i;
3260
3261 memset(params, 0, sizeof(*params));
3262 params->flags = oparams->flags;
3263 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3264 params->masks[i].bits[0] = oparams->masks[i];
3265 memcpy(params->intervals, oparams->intervals, sizeof(oparams->intervals));
3266 params->rmask = __OLD_TO_NEW_MASK(oparams->rmask);
3267 params->cmask = __OLD_TO_NEW_MASK(oparams->cmask);
3268 params->info = oparams->info;
3269 params->msbits = oparams->msbits;
3270 params->rate_num = oparams->rate_num;
3271 params->rate_den = oparams->rate_den;
3272 params->fifo_size = oparams->fifo_size;
3273}
3274
877211f5
TI
3275static void snd_pcm_hw_convert_to_old_params(struct snd_pcm_hw_params_old *oparams,
3276 struct snd_pcm_hw_params *params)
1da177e4
LT
3277{
3278 unsigned int i;
3279
3280 memset(oparams, 0, sizeof(*oparams));
3281 oparams->flags = params->flags;
3282 for (i = 0; i < ARRAY_SIZE(oparams->masks); i++)
3283 oparams->masks[i] = params->masks[i].bits[0];
3284 memcpy(oparams->intervals, params->intervals, sizeof(oparams->intervals));
3285 oparams->rmask = __NEW_TO_OLD_MASK(params->rmask);
3286 oparams->cmask = __NEW_TO_OLD_MASK(params->cmask);
3287 oparams->info = params->info;
3288 oparams->msbits = params->msbits;
3289 oparams->rate_num = params->rate_num;
3290 oparams->rate_den = params->rate_den;
3291 oparams->fifo_size = params->fifo_size;
3292}
3293
877211f5
TI
3294static int snd_pcm_hw_refine_old_user(struct snd_pcm_substream *substream,
3295 struct snd_pcm_hw_params_old __user * _oparams)
1da177e4 3296{
877211f5
TI
3297 struct snd_pcm_hw_params *params;
3298 struct snd_pcm_hw_params_old *oparams = NULL;
1da177e4
LT
3299 int err;
3300
3301 params = kmalloc(sizeof(*params), GFP_KERNEL);
3302 if (!params) {
3303 err = -ENOMEM;
3304 goto out;
3305 }
3306 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3307 if (!oparams) {
3308 err = -ENOMEM;
3309 goto out;
3310 }
3311
3312 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3313 err = -EFAULT;
3314 goto out;
3315 }
3316 snd_pcm_hw_convert_from_old_params(params, oparams);
3317 err = snd_pcm_hw_refine(substream, params);
3318 snd_pcm_hw_convert_to_old_params(oparams, params);
3319 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3320 if (!err)
3321 err = -EFAULT;
3322 }
3323out:
3324 kfree(params);
3325 kfree(oparams);
3326 return err;
3327}
3328
877211f5
TI
3329static int snd_pcm_hw_params_old_user(struct snd_pcm_substream *substream,
3330 struct snd_pcm_hw_params_old __user * _oparams)
1da177e4 3331{
877211f5
TI
3332 struct snd_pcm_hw_params *params;
3333 struct snd_pcm_hw_params_old *oparams = NULL;
1da177e4
LT
3334 int err;
3335
3336 params = kmalloc(sizeof(*params), GFP_KERNEL);
3337 if (!params) {
3338 err = -ENOMEM;
3339 goto out;
3340 }
3341 oparams = kmalloc(sizeof(*oparams), GFP_KERNEL);
3342 if (!oparams) {
3343 err = -ENOMEM;
3344 goto out;
3345 }
3346 if (copy_from_user(oparams, _oparams, sizeof(*oparams))) {
3347 err = -EFAULT;
3348 goto out;
3349 }
3350 snd_pcm_hw_convert_from_old_params(params, oparams);
3351 err = snd_pcm_hw_params(substream, params);
3352 snd_pcm_hw_convert_to_old_params(oparams, params);
3353 if (copy_to_user(_oparams, oparams, sizeof(*oparams))) {
3354 if (!err)
3355 err = -EFAULT;
3356 }
3357out:
3358 kfree(params);
3359 kfree(oparams);
3360 return err;
3361}
3362
3363/*
3364 * Register section
3365 */
3366
3367static struct file_operations snd_pcm_f_ops_playback = {
3368 .owner = THIS_MODULE,
3369 .write = snd_pcm_write,
3370 .writev = snd_pcm_writev,
3371 .open = snd_pcm_open,
3372 .release = snd_pcm_release,
3373 .poll = snd_pcm_playback_poll,
3374 .unlocked_ioctl = snd_pcm_playback_ioctl,
3375 .compat_ioctl = snd_pcm_ioctl_compat,
3376 .mmap = snd_pcm_mmap,
3377 .fasync = snd_pcm_fasync,
3378};
3379
3380static struct file_operations snd_pcm_f_ops_capture = {
3381 .owner = THIS_MODULE,
3382 .read = snd_pcm_read,
3383 .readv = snd_pcm_readv,
3384 .open = snd_pcm_open,
3385 .release = snd_pcm_release,
3386 .poll = snd_pcm_capture_poll,
3387 .unlocked_ioctl = snd_pcm_capture_ioctl,
3388 .compat_ioctl = snd_pcm_ioctl_compat,
3389 .mmap = snd_pcm_mmap,
3390 .fasync = snd_pcm_fasync,
3391};
3392
877211f5 3393struct snd_minor snd_pcm_reg[2] =
1da177e4
LT
3394{
3395 {
3396 .comment = "digital audio playback",
3397 .f_ops = &snd_pcm_f_ops_playback,
3398 },
3399 {
3400 .comment = "digital audio capture",
3401 .f_ops = &snd_pcm_f_ops_capture,
3402 }
3403};