1 /*
2 * Digital Audio (PCM) abstract layer
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #include <linux/slab.h>
24 #include <linux/time.h>
25 #include <linux/math64.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
32
33 /*
34 * fill ring buffer with silence
35 * runtime->silence_start: starting pointer to silence area
36 * runtime->silence_filled: size filled with silence
37 * runtime->silence_threshold: threshold from application
38 * runtime->silence_size: maximal size from application
39 *
40 * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41 */
42 void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr)
43 {
44 struct snd_pcm_runtime *runtime = substream->runtime;
45 snd_pcm_uframes_t frames, ofs, transfer;
46
47 if (runtime->silence_size < runtime->boundary) {
48 snd_pcm_sframes_t noise_dist, n;
49 if (runtime->silence_start != runtime->control->appl_ptr) {
50 n = runtime->control->appl_ptr - runtime->silence_start;
51 if (n < 0)
52 n += runtime->boundary;
53 if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 runtime->silence_filled -= n;
55 else
56 runtime->silence_filled = 0;
57 runtime->silence_start = runtime->control->appl_ptr;
58 }
59 if (runtime->silence_filled >= runtime->buffer_size)
60 return;
61 noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
62 if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
63 return;
64 frames = runtime->silence_threshold - noise_dist;
65 if (frames > runtime->silence_size)
66 frames = runtime->silence_size;
67 } else {
68 if (new_hw_ptr == ULONG_MAX) { /* initialization */
69 snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
70 runtime->silence_filled = avail > 0 ? avail : 0;
71 runtime->silence_start = (runtime->status->hw_ptr +
72 runtime->silence_filled) %
73 runtime->boundary;
74 } else {
75 ofs = runtime->status->hw_ptr;
76 frames = new_hw_ptr - ofs;
77 if ((snd_pcm_sframes_t)frames < 0)
78 frames += runtime->boundary;
79 runtime->silence_filled -= frames;
80 if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
81 runtime->silence_filled = 0;
82 runtime->silence_start = new_hw_ptr;
83 } else {
84 runtime->silence_start = ofs;
85 }
86 }
87 frames = runtime->buffer_size - runtime->silence_filled;
88 }
89 if (snd_BUG_ON(frames > runtime->buffer_size))
90 return;
91 if (frames == 0)
92 return;
93 ofs = runtime->silence_start % runtime->buffer_size;
94 while (frames > 0) {
95 transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
96 if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
97 runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
98 if (substream->ops->silence) {
99 int err;
100 err = substream->ops->silence(substream, -1, ofs, transfer);
101 snd_BUG_ON(err < 0);
102 } else {
103 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
104 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
105 }
106 } else {
107 unsigned int c;
108 unsigned int channels = runtime->channels;
109 if (substream->ops->silence) {
110 for (c = 0; c < channels; ++c) {
111 int err;
112 err = substream->ops->silence(substream, c, ofs, transfer);
113 snd_BUG_ON(err < 0);
114 }
115 } else {
116 size_t dma_csize = runtime->dma_bytes / channels;
117 for (c = 0; c < channels; ++c) {
118 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
119 snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
120 }
121 }
122 }
123 runtime->silence_filled += transfer;
124 frames -= transfer;
125 ofs = 0;
126 }
127 }
128
129 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
130 #define xrun_debug(substream, mask) ((substream)->pstr->xrun_debug & (mask))
131 #else
132 #define xrun_debug(substream, mask) 0
133 #endif
134
135 #define dump_stack_on_xrun(substream) do { \
136 if (xrun_debug(substream, 2)) \
137 dump_stack(); \
138 } while (0)
139
140 static void pcm_debug_name(struct snd_pcm_substream *substream,
141 char *name, size_t len)
142 {
143 snprintf(name, len, "pcmC%dD%d%c:%d",
144 substream->pcm->card->number,
145 substream->pcm->device,
146 substream->stream ? 'c' : 'p',
147 substream->number);
148 }
149
150 static void xrun(struct snd_pcm_substream *substream)
151 {
152 struct snd_pcm_runtime *runtime = substream->runtime;
153
154 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
155 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
156 snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
157 if (xrun_debug(substream, 1)) {
158 char name[16];
159 pcm_debug_name(substream, name, sizeof(name));
160 snd_printd(KERN_DEBUG "XRUN: %s\n", name);
161 dump_stack_on_xrun(substream);
162 }
163 }
164
165 static snd_pcm_uframes_t
166 snd_pcm_update_hw_ptr_pos(struct snd_pcm_substream *substream,
167 struct snd_pcm_runtime *runtime)
168 {
169 snd_pcm_uframes_t pos;
170
171 pos = substream->ops->pointer(substream);
172 if (pos == SNDRV_PCM_POS_XRUN)
173 return pos; /* XRUN */
174 if (pos >= runtime->buffer_size) {
175 if (printk_ratelimit()) {
176 char name[16];
177 pcm_debug_name(substream, name, sizeof(name));
178 snd_printd(KERN_ERR "BUG: %s, pos = 0x%lx, "
179 "buffer size = 0x%lx, period size = 0x%lx\n",
180 name, pos, runtime->buffer_size,
181 runtime->period_size);
182 }
183 pos = 0;
184 }
185 pos -= pos % runtime->min_align;
186 return pos;
187 }
188
189 static int snd_pcm_update_hw_ptr_post(struct snd_pcm_substream *substream,
190 struct snd_pcm_runtime *runtime)
191 {
192 snd_pcm_uframes_t avail;
193
194 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
195 avail = snd_pcm_playback_avail(runtime);
196 else
197 avail = snd_pcm_capture_avail(runtime);
198 if (avail > runtime->avail_max)
199 runtime->avail_max = avail;
200 if (avail >= runtime->stop_threshold) {
201 if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
202 snd_pcm_drain_done(substream);
203 else
204 xrun(substream);
205 return -EPIPE;
206 }
207 if (avail >= runtime->control->avail_min)
208 wake_up(&runtime->sleep);
209 return 0;
210 }
211
212 #define hw_ptr_error(substream, fmt, args...) \
213 do { \
214 if (xrun_debug(substream, 1)) { \
215 if (printk_ratelimit()) { \
216 snd_printd("PCM: " fmt, ##args); \
217 } \
218 dump_stack_on_xrun(substream); \
219 } \
220 } while (0)
221
222 static int snd_pcm_update_hw_ptr_interrupt(struct snd_pcm_substream *substream)
223 {
224 struct snd_pcm_runtime *runtime = substream->runtime;
225 snd_pcm_uframes_t pos;
226 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_ptr_interrupt, hw_base;
227 snd_pcm_sframes_t hdelta, delta;
228 unsigned long jdelta;
229
230 old_hw_ptr = runtime->status->hw_ptr;
231 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
232 if (pos == SNDRV_PCM_POS_XRUN) {
233 xrun(substream);
234 return -EPIPE;
235 }
236 if (xrun_debug(substream, 8)) {
237 char name[16];
238 pcm_debug_name(substream, name, sizeof(name));
239 snd_printd("period_update: %s: pos=0x%x/0x%x/0x%x, "
240 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
241 name, (unsigned int)pos,
242 (unsigned int)runtime->period_size,
243 (unsigned int)runtime->buffer_size,
244 (unsigned long)old_hw_ptr,
245 (unsigned long)runtime->hw_ptr_base,
246 (unsigned long)runtime->hw_ptr_interrupt);
247 }
248 hw_base = runtime->hw_ptr_base;
249 new_hw_ptr = hw_base + pos;
250 hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
251 delta = new_hw_ptr - hw_ptr_interrupt;
252 if (hw_ptr_interrupt >= runtime->boundary) {
253 hw_ptr_interrupt -= runtime->boundary;
254 if (hw_base < runtime->boundary / 2)
255 /* hw_base was already lapped; recalc delta */
256 delta = new_hw_ptr - hw_ptr_interrupt;
257 }
258 if (delta < 0) {
259 if (runtime->periods == 1 || new_hw_ptr < old_hw_ptr)
260 delta += runtime->buffer_size;
261 if (delta < 0) {
262 hw_ptr_error(substream,
263 "Unexpected hw_pointer value "
264 "(stream=%i, pos=%ld, intr_ptr=%ld)\n",
265 substream->stream, (long)pos,
266 (long)hw_ptr_interrupt);
267 #if 1
268 /* simply skipping the hwptr update seems more
269 * robust in some cases, e.g. on VMware with
270 * inaccurate timer source
271 */
272 return 0; /* skip this update */
273 #else
274 /* rebase to interrupt position */
275 hw_base = new_hw_ptr = hw_ptr_interrupt;
276 /* align hw_base to buffer_size */
277 hw_base -= hw_base % runtime->buffer_size;
278 delta = 0;
279 #endif
280 } else {
281 hw_base += runtime->buffer_size;
282 if (hw_base >= runtime->boundary)
283 hw_base = 0;
284 new_hw_ptr = hw_base + pos;
285 }
286 }
287
288 /* Do jiffies check only in xrun_debug mode */
289 if (!xrun_debug(substream, 4))
290 goto no_jiffies_check;
291
292 /* Skip the jiffies check for hardwares with BATCH flag.
293 * Such hardware usually just increases the position at each IRQ,
294 * thus it can't give any strange position.
295 */
296 if (runtime->hw.info & SNDRV_PCM_INFO_BATCH)
297 goto no_jiffies_check;
298 hdelta = new_hw_ptr - old_hw_ptr;
299 if (hdelta < runtime->delay)
300 goto no_jiffies_check;
301 hdelta -= runtime->delay;
302 jdelta = jiffies - runtime->hw_ptr_jiffies;
303 if (((hdelta * HZ) / runtime->rate) > jdelta + HZ/100) {
304 delta = jdelta /
305 (((runtime->period_size * HZ) / runtime->rate)
306 + HZ/100);
307 hw_ptr_error(substream,
308 "hw_ptr skipping! [Q] "
309 "(pos=%ld, delta=%ld, period=%ld, "
310 "jdelta=%lu/%lu/%lu)\n",
311 (long)pos, (long)hdelta,
312 (long)runtime->period_size, jdelta,
313 ((hdelta * HZ) / runtime->rate), delta);
314 hw_ptr_interrupt = runtime->hw_ptr_interrupt +
315 runtime->period_size * delta;
316 if (hw_ptr_interrupt >= runtime->boundary)
317 hw_ptr_interrupt -= runtime->boundary;
318 /* rebase to interrupt position */
319 hw_base = new_hw_ptr = hw_ptr_interrupt;
320 /* align hw_base to buffer_size */
321 hw_base -= hw_base % runtime->buffer_size;
322 delta = 0;
323 }
324 no_jiffies_check:
325 if (delta > runtime->period_size + runtime->period_size / 2) {
326 hw_ptr_error(substream,
327 "Lost interrupts? "
328 "(stream=%i, delta=%ld, intr_ptr=%ld)\n",
329 substream->stream, (long)delta,
330 (long)hw_ptr_interrupt);
331 /* rebase hw_ptr_interrupt */
332 hw_ptr_interrupt =
333 new_hw_ptr - new_hw_ptr % runtime->period_size;
334 }
335 runtime->hw_ptr_interrupt = hw_ptr_interrupt;
336
337 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
338 runtime->silence_size > 0)
339 snd_pcm_playback_silence(substream, new_hw_ptr);
340
341 if (runtime->status->hw_ptr == new_hw_ptr)
342 return 0;
343
344 runtime->hw_ptr_base = hw_base;
345 runtime->status->hw_ptr = new_hw_ptr;
346 runtime->hw_ptr_jiffies = jiffies;
347 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
348 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
349
350 return snd_pcm_update_hw_ptr_post(substream, runtime);
351 }
352
353 /* CAUTION: call it with irq disabled */
354 int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream)
355 {
356 struct snd_pcm_runtime *runtime = substream->runtime;
357 snd_pcm_uframes_t pos;
358 snd_pcm_uframes_t old_hw_ptr, new_hw_ptr, hw_base;
359 snd_pcm_sframes_t delta;
360 unsigned long jdelta;
361
362 old_hw_ptr = runtime->status->hw_ptr;
363 pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
364 if (pos == SNDRV_PCM_POS_XRUN) {
365 xrun(substream);
366 return -EPIPE;
367 }
368 if (xrun_debug(substream, 16)) {
369 char name[16];
370 pcm_debug_name(substream, name, sizeof(name));
371 snd_printd("hw_update: %s: pos=0x%x/0x%x/0x%x, "
372 "hwptr=0x%lx, hw_base=0x%lx, hw_intr=0x%lx\n",
373 name, (unsigned int)pos,
374 (unsigned int)runtime->period_size,
375 (unsigned int)runtime->buffer_size,
376 (unsigned long)old_hw_ptr,
377 (unsigned long)runtime->hw_ptr_base,
378 (unsigned long)runtime->hw_ptr_interrupt);
379 }
380
381 hw_base = runtime->hw_ptr_base;
382 new_hw_ptr = hw_base + pos;
383
384 delta = new_hw_ptr - old_hw_ptr;
385 jdelta = jiffies - runtime->hw_ptr_jiffies;
386 if (delta < 0) {
387 delta += runtime->buffer_size;
388 if (delta < 0) {
389 hw_ptr_error(substream,
390 "Unexpected hw_pointer value [2] "
391 "(stream=%i, pos=%ld, old_ptr=%ld, jdelta=%li)\n",
392 substream->stream, (long)pos,
393 (long)old_hw_ptr, jdelta);
394 return 0;
395 }
396 hw_base += runtime->buffer_size;
397 if (hw_base >= runtime->boundary)
398 hw_base = 0;
399 new_hw_ptr = hw_base + pos;
400 }
401 /* Do jiffies check only in xrun_debug mode */
402 if (!xrun_debug(substream, 4))
403 goto no_jiffies_check;
404 if (delta < runtime->delay)
405 goto no_jiffies_check;
406 delta -= runtime->delay;
407 if (((delta * HZ) / runtime->rate) > jdelta + HZ/100) {
408 hw_ptr_error(substream,
409 "hw_ptr skipping! "
410 "(pos=%ld, delta=%ld, period=%ld, jdelta=%lu/%lu)\n",
411 (long)pos, (long)delta,
412 (long)runtime->period_size, jdelta,
413 ((delta * HZ) / runtime->rate));
414 return 0;
415 }
416 no_jiffies_check:
417 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
418 runtime->silence_size > 0)
419 snd_pcm_playback_silence(substream, new_hw_ptr);
420
421 if (runtime->status->hw_ptr == new_hw_ptr)
422 return 0;
423
424 runtime->hw_ptr_base = hw_base;
425 runtime->status->hw_ptr = new_hw_ptr;
426 runtime->hw_ptr_jiffies = jiffies;
427 if (runtime->tstamp_mode == SNDRV_PCM_TSTAMP_ENABLE)
428 snd_pcm_gettime(runtime, (struct timespec *)&runtime->status->tstamp);
429
430 return snd_pcm_update_hw_ptr_post(substream, runtime);
431 }
432
433 /**
434 * snd_pcm_set_ops - set the PCM operators
435 * @pcm: the pcm instance
436 * @direction: stream direction, SNDRV_PCM_STREAM_XXX
437 * @ops: the operator table
438 *
439 * Sets the given PCM operators to the pcm instance.
440 */
441 void snd_pcm_set_ops(struct snd_pcm *pcm, int direction, struct snd_pcm_ops *ops)
442 {
443 struct snd_pcm_str *stream = &pcm->streams[direction];
444 struct snd_pcm_substream *substream;
445
446 for (substream = stream->substream; substream != NULL; substream = substream->next)
447 substream->ops = ops;
448 }
449
450 EXPORT_SYMBOL(snd_pcm_set_ops);
451
452 /**
453 * snd_pcm_sync - set the PCM sync id
454 * @substream: the pcm substream
455 *
456 * Sets the PCM sync identifier for the card.
457 */
458 void snd_pcm_set_sync(struct snd_pcm_substream *substream)
459 {
460 struct snd_pcm_runtime *runtime = substream->runtime;
461
462 runtime->sync.id32[0] = substream->pcm->card->number;
463 runtime->sync.id32[1] = -1;
464 runtime->sync.id32[2] = -1;
465 runtime->sync.id32[3] = -1;
466 }
467
468 EXPORT_SYMBOL(snd_pcm_set_sync);
469
470 /*
471 * Standard ioctl routine
472 */
473
474 static inline unsigned int div32(unsigned int a, unsigned int b,
475 unsigned int *r)
476 {
477 if (b == 0) {
478 *r = 0;
479 return UINT_MAX;
480 }
481 *r = a % b;
482 return a / b;
483 }
484
485 static inline unsigned int div_down(unsigned int a, unsigned int b)
486 {
487 if (b == 0)
488 return UINT_MAX;
489 return a / b;
490 }
491
492 static inline unsigned int div_up(unsigned int a, unsigned int b)
493 {
494 unsigned int r;
495 unsigned int q;
496 if (b == 0)
497 return UINT_MAX;
498 q = div32(a, b, &r);
499 if (r)
500 ++q;
501 return q;
502 }
503
504 static inline unsigned int mul(unsigned int a, unsigned int b)
505 {
506 if (a == 0)
507 return 0;
508 if (div_down(UINT_MAX, a) < b)
509 return UINT_MAX;
510 return a * b;
511 }
512
513 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
514 unsigned int c, unsigned int *r)
515 {
516 u_int64_t n = (u_int64_t) a * b;
517 if (c == 0) {
518 snd_BUG_ON(!n);
519 *r = 0;
520 return UINT_MAX;
521 }
522 n = div_u64_rem(n, c, r);
523 if (n >= UINT_MAX) {
524 *r = 0;
525 return UINT_MAX;
526 }
527 return n;
528 }
529
530 /**
531 * snd_interval_refine - refine the interval value of configurator
532 * @i: the interval value to refine
533 * @v: the interval value to refer to
534 *
535 * Refines the interval value with the reference value.
536 * The interval is changed to the range satisfying both intervals.
537 * The interval status (min, max, integer, etc.) are evaluated.
538 *
539 * Returns non-zero if the value is changed, zero if not changed.
540 */
541 int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v)
542 {
543 int changed = 0;
544 if (snd_BUG_ON(snd_interval_empty(i)))
545 return -EINVAL;
546 if (i->min < v->min) {
547 i->min = v->min;
548 i->openmin = v->openmin;
549 changed = 1;
550 } else if (i->min == v->min && !i->openmin && v->openmin) {
551 i->openmin = 1;
552 changed = 1;
553 }
554 if (i->max > v->max) {
555 i->max = v->max;
556 i->openmax = v->openmax;
557 changed = 1;
558 } else if (i->max == v->max && !i->openmax && v->openmax) {
559 i->openmax = 1;
560 changed = 1;
561 }
562 if (!i->integer && v->integer) {
563 i->integer = 1;
564 changed = 1;
565 }
566 if (i->integer) {
567 if (i->openmin) {
568 i->min++;
569 i->openmin = 0;
570 }
571 if (i->openmax) {
572 i->max--;
573 i->openmax = 0;
574 }
575 } else if (!i->openmin && !i->openmax && i->min == i->max)
576 i->integer = 1;
577 if (snd_interval_checkempty(i)) {
578 snd_interval_none(i);
579 return -EINVAL;
580 }
581 return changed;
582 }
583
584 EXPORT_SYMBOL(snd_interval_refine);
585
586 static int snd_interval_refine_first(struct snd_interval *i)
587 {
588 if (snd_BUG_ON(snd_interval_empty(i)))
589 return -EINVAL;
590 if (snd_interval_single(i))
591 return 0;
592 i->max = i->min;
593 i->openmax = i->openmin;
594 if (i->openmax)
595 i->max++;
596 return 1;
597 }
598
599 static int snd_interval_refine_last(struct snd_interval *i)
600 {
601 if (snd_BUG_ON(snd_interval_empty(i)))
602 return -EINVAL;
603 if (snd_interval_single(i))
604 return 0;
605 i->min = i->max;
606 i->openmin = i->openmax;
607 if (i->openmin)
608 i->min--;
609 return 1;
610 }
611
612 void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
613 {
614 if (a->empty || b->empty) {
615 snd_interval_none(c);
616 return;
617 }
618 c->empty = 0;
619 c->min = mul(a->min, b->min);
620 c->openmin = (a->openmin || b->openmin);
621 c->max = mul(a->max, b->max);
622 c->openmax = (a->openmax || b->openmax);
623 c->integer = (a->integer && b->integer);
624 }
625
626 /**
627 * snd_interval_div - refine the interval value with division
628 * @a: dividend
629 * @b: divisor
630 * @c: quotient
631 *
632 * c = a / b
633 *
634 * Returns non-zero if the value is changed, zero if not changed.
635 */
636 void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c)
637 {
638 unsigned int r;
639 if (a->empty || b->empty) {
640 snd_interval_none(c);
641 return;
642 }
643 c->empty = 0;
644 c->min = div32(a->min, b->max, &r);
645 c->openmin = (r || a->openmin || b->openmax);
646 if (b->min > 0) {
647 c->max = div32(a->max, b->min, &r);
648 if (r) {
649 c->max++;
650 c->openmax = 1;
651 } else
652 c->openmax = (a->openmax || b->openmin);
653 } else {
654 c->max = UINT_MAX;
655 c->openmax = 0;
656 }
657 c->integer = 0;
658 }
659
660 /**
661 * snd_interval_muldivk - refine the interval value
662 * @a: dividend 1
663 * @b: dividend 2
664 * @k: divisor (as integer)
665 * @c: result
666 *
667 * c = a * b / k
668 *
669 * Returns non-zero if the value is changed, zero if not changed.
670 */
671 void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
672 unsigned int k, struct snd_interval *c)
673 {
674 unsigned int r;
675 if (a->empty || b->empty) {
676 snd_interval_none(c);
677 return;
678 }
679 c->empty = 0;
680 c->min = muldiv32(a->min, b->min, k, &r);
681 c->openmin = (r || a->openmin || b->openmin);
682 c->max = muldiv32(a->max, b->max, k, &r);
683 if (r) {
684 c->max++;
685 c->openmax = 1;
686 } else
687 c->openmax = (a->openmax || b->openmax);
688 c->integer = 0;
689 }
690
691 /**
692 * snd_interval_mulkdiv - refine the interval value
693 * @a: dividend 1
694 * @k: dividend 2 (as integer)
695 * @b: divisor
696 * @c: result
697 *
698 * c = a * k / b
699 *
700 * Returns non-zero if the value is changed, zero if not changed.
701 */
702 void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
703 const struct snd_interval *b, struct snd_interval *c)
704 {
705 unsigned int r;
706 if (a->empty || b->empty) {
707 snd_interval_none(c);
708 return;
709 }
710 c->empty = 0;
711 c->min = muldiv32(a->min, k, b->max, &r);
712 c->openmin = (r || a->openmin || b->openmax);
713 if (b->min > 0) {
714 c->max = muldiv32(a->max, k, b->min, &r);
715 if (r) {
716 c->max++;
717 c->openmax = 1;
718 } else
719 c->openmax = (a->openmax || b->openmin);
720 } else {
721 c->max = UINT_MAX;
722 c->openmax = 0;
723 }
724 c->integer = 0;
725 }
726
727 /* ---- */
728
729
730 /**
731 * snd_interval_ratnum - refine the interval value
732 * @i: interval to refine
733 * @rats_count: number of ratnum_t
734 * @rats: ratnum_t array
735 * @nump: pointer to store the resultant numerator
736 * @denp: pointer to store the resultant denominator
737 *
738 * Returns non-zero if the value is changed, zero if not changed.
739 */
740 int snd_interval_ratnum(struct snd_interval *i,
741 unsigned int rats_count, struct snd_ratnum *rats,
742 unsigned int *nump, unsigned int *denp)
743 {
744 unsigned int best_num, best_diff, best_den;
745 unsigned int k;
746 struct snd_interval t;
747 int err;
748
749 best_num = best_den = best_diff = 0;
750 for (k = 0; k < rats_count; ++k) {
751 unsigned int num = rats[k].num;
752 unsigned int den;
753 unsigned int q = i->min;
754 int diff;
755 if (q == 0)
756 q = 1;
757 den = div_down(num, q);
758 if (den < rats[k].den_min)
759 continue;
760 if (den > rats[k].den_max)
761 den = rats[k].den_max;
762 else {
763 unsigned int r;
764 r = (den - rats[k].den_min) % rats[k].den_step;
765 if (r != 0)
766 den -= r;
767 }
768 diff = num - q * den;
769 if (best_num == 0 ||
770 diff * best_den < best_diff * den) {
771 best_diff = diff;
772 best_den = den;
773 best_num = num;
774 }
775 }
776 if (best_den == 0) {
777 i->empty = 1;
778 return -EINVAL;
779 }
780 t.min = div_down(best_num, best_den);
781 t.openmin = !!(best_num % best_den);
782
783 best_num = best_den = best_diff = 0;
784 for (k = 0; k < rats_count; ++k) {
785 unsigned int num = rats[k].num;
786 unsigned int den;
787 unsigned int q = i->max;
788 int diff;
789 if (q == 0) {
790 i->empty = 1;
791 return -EINVAL;
792 }
793 den = div_up(num, q);
794 if (den > rats[k].den_max)
795 continue;
796 if (den < rats[k].den_min)
797 den = rats[k].den_min;
798 else {
799 unsigned int r;
800 r = (den - rats[k].den_min) % rats[k].den_step;
801 if (r != 0)
802 den += rats[k].den_step - r;
803 }
804 diff = q * den - num;
805 if (best_num == 0 ||
806 diff * best_den < best_diff * den) {
807 best_diff = diff;
808 best_den = den;
809 best_num = num;
810 }
811 }
812 if (best_den == 0) {
813 i->empty = 1;
814 return -EINVAL;
815 }
816 t.max = div_up(best_num, best_den);
817 t.openmax = !!(best_num % best_den);
818 t.integer = 0;
819 err = snd_interval_refine(i, &t);
820 if (err < 0)
821 return err;
822
823 if (snd_interval_single(i)) {
824 if (nump)
825 *nump = best_num;
826 if (denp)
827 *denp = best_den;
828 }
829 return err;
830 }
831
832 EXPORT_SYMBOL(snd_interval_ratnum);
833
834 /**
835 * snd_interval_ratden - refine the interval value
836 * @i: interval to refine
837 * @rats_count: number of struct ratden
838 * @rats: struct ratden array
839 * @nump: pointer to store the resultant numerator
840 * @denp: pointer to store the resultant denominator
841 *
842 * Returns non-zero if the value is changed, zero if not changed.
843 */
844 static int snd_interval_ratden(struct snd_interval *i,
845 unsigned int rats_count, struct snd_ratden *rats,
846 unsigned int *nump, unsigned int *denp)
847 {
848 unsigned int best_num, best_diff, best_den;
849 unsigned int k;
850 struct snd_interval t;
851 int err;
852
853 best_num = best_den = best_diff = 0;
854 for (k = 0; k < rats_count; ++k) {
855 unsigned int num;
856 unsigned int den = rats[k].den;
857 unsigned int q = i->min;
858 int diff;
859 num = mul(q, den);
860 if (num > rats[k].num_max)
861 continue;
862 if (num < rats[k].num_min)
863 num = rats[k].num_max;
864 else {
865 unsigned int r;
866 r = (num - rats[k].num_min) % rats[k].num_step;
867 if (r != 0)
868 num += rats[k].num_step - r;
869 }
870 diff = num - q * den;
871 if (best_num == 0 ||
872 diff * best_den < best_diff * den) {
873 best_diff = diff;
874 best_den = den;
875 best_num = num;
876 }
877 }
878 if (best_den == 0) {
879 i->empty = 1;
880 return -EINVAL;
881 }
882 t.min = div_down(best_num, best_den);
883 t.openmin = !!(best_num % best_den);
884
885 best_num = best_den = best_diff = 0;
886 for (k = 0; k < rats_count; ++k) {
887 unsigned int num;
888 unsigned int den = rats[k].den;
889 unsigned int q = i->max;
890 int diff;
891 num = mul(q, den);
892 if (num < rats[k].num_min)
893 continue;
894 if (num > rats[k].num_max)
895 num = rats[k].num_max;
896 else {
897 unsigned int r;
898 r = (num - rats[k].num_min) % rats[k].num_step;
899 if (r != 0)
900 num -= r;
901 }
902 diff = q * den - num;
903 if (best_num == 0 ||
904 diff * best_den < best_diff * den) {
905 best_diff = diff;
906 best_den = den;
907 best_num = num;
908 }
909 }
910 if (best_den == 0) {
911 i->empty = 1;
912 return -EINVAL;
913 }
914 t.max = div_up(best_num, best_den);
915 t.openmax = !!(best_num % best_den);
916 t.integer = 0;
917 err = snd_interval_refine(i, &t);
918 if (err < 0)
919 return err;
920
921 if (snd_interval_single(i)) {
922 if (nump)
923 *nump = best_num;
924 if (denp)
925 *denp = best_den;
926 }
927 return err;
928 }
929
930 /**
931 * snd_interval_list - refine the interval value from the list
932 * @i: the interval value to refine
933 * @count: the number of elements in the list
934 * @list: the value list
935 * @mask: the bit-mask to evaluate
936 *
937 * Refines the interval value from the list.
938 * When mask is non-zero, only the elements corresponding to bit 1 are
939 * evaluated.
940 *
941 * Returns non-zero if the value is changed, zero if not changed.
942 */
943 int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask)
944 {
945 unsigned int k;
946 struct snd_interval list_range;
947
948 if (!count) {
949 i->empty = 1;
950 return -EINVAL;
951 }
952 snd_interval_any(&list_range);
953 list_range.min = UINT_MAX;
954 list_range.max = 0;
955 for (k = 0; k < count; k++) {
956 if (mask && !(mask & (1 << k)))
957 continue;
958 if (!snd_interval_test(i, list[k]))
959 continue;
960 list_range.min = min(list_range.min, list[k]);
961 list_range.max = max(list_range.max, list[k]);
962 }
963 return snd_interval_refine(i, &list_range);
964 }
965
966 EXPORT_SYMBOL(snd_interval_list);
967
968 static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
969 {
970 unsigned int n;
971 int changed = 0;
972 n = (i->min - min) % step;
973 if (n != 0 || i->openmin) {
974 i->min += step - n;
975 changed = 1;
976 }
977 n = (i->max - min) % step;
978 if (n != 0 || i->openmax) {
979 i->max -= n;
980 changed = 1;
981 }
982 if (snd_interval_checkempty(i)) {
983 i->empty = 1;
984 return -EINVAL;
985 }
986 return changed;
987 }
988
989 /* Info constraints helpers */
990
991 /**
992 * snd_pcm_hw_rule_add - add the hw-constraint rule
993 * @runtime: the pcm runtime instance
994 * @cond: condition bits
995 * @var: the variable to evaluate
996 * @func: the evaluation function
997 * @private: the private data pointer passed to function
998 * @dep: the dependent variables
999 *
1000 * Returns zero if successful, or a negative error code on failure.
1001 */
1002 int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
1003 int var,
1004 snd_pcm_hw_rule_func_t func, void *private,
1005 int dep, ...)
1006 {
1007 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1008 struct snd_pcm_hw_rule *c;
1009 unsigned int k;
1010 va_list args;
1011 va_start(args, dep);
1012 if (constrs->rules_num >= constrs->rules_all) {
1013 struct snd_pcm_hw_rule *new;
1014 unsigned int new_rules = constrs->rules_all + 16;
1015 new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1016 if (!new)
1017 return -ENOMEM;
1018 if (constrs->rules) {
1019 memcpy(new, constrs->rules,
1020 constrs->rules_num * sizeof(*c));
1021 kfree(constrs->rules);
1022 }
1023 constrs->rules = new;
1024 constrs->rules_all = new_rules;
1025 }
1026 c = &constrs->rules[constrs->rules_num];
1027 c->cond = cond;
1028 c->func = func;
1029 c->var = var;
1030 c->private = private;
1031 k = 0;
1032 while (1) {
1033 if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps)))
1034 return -EINVAL;
1035 c->deps[k++] = dep;
1036 if (dep < 0)
1037 break;
1038 dep = va_arg(args, int);
1039 }
1040 constrs->rules_num++;
1041 va_end(args);
1042 return 0;
1043 }
1044
1045 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
1046
1047 /**
1048 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
1049 * @runtime: PCM runtime instance
1050 * @var: hw_params variable to apply the mask
1051 * @mask: the bitmap mask
1052 *
1053 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
1054 */
1055 int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1056 u_int32_t mask)
1057 {
1058 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1059 struct snd_mask *maskp = constrs_mask(constrs, var);
1060 *maskp->bits &= mask;
1061 memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
1062 if (*maskp->bits == 0)
1063 return -EINVAL;
1064 return 0;
1065 }
1066
1067 /**
1068 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
1069 * @runtime: PCM runtime instance
1070 * @var: hw_params variable to apply the mask
1071 * @mask: the 64bit bitmap mask
1072 *
1073 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
1074 */
1075 int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1076 u_int64_t mask)
1077 {
1078 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1079 struct snd_mask *maskp = constrs_mask(constrs, var);
1080 maskp->bits[0] &= (u_int32_t)mask;
1081 maskp->bits[1] &= (u_int32_t)(mask >> 32);
1082 memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
1083 if (! maskp->bits[0] && ! maskp->bits[1])
1084 return -EINVAL;
1085 return 0;
1086 }
1087
1088 /**
1089 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
1090 * @runtime: PCM runtime instance
1091 * @var: hw_params variable to apply the integer constraint
1092 *
1093 * Apply the constraint of integer to an interval parameter.
1094 */
1095 int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
1096 {
1097 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1098 return snd_interval_setinteger(constrs_interval(constrs, var));
1099 }
1100
1101 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
1102
1103 /**
1104 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
1105 * @runtime: PCM runtime instance
1106 * @var: hw_params variable to apply the range
1107 * @min: the minimal value
1108 * @max: the maximal value
1109 *
1110 * Apply the min/max range constraint to an interval parameter.
1111 */
1112 int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
1113 unsigned int min, unsigned int max)
1114 {
1115 struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
1116 struct snd_interval t;
1117 t.min = min;
1118 t.max = max;
1119 t.openmin = t.openmax = 0;
1120 t.integer = 0;
1121 return snd_interval_refine(constrs_interval(constrs, var), &t);
1122 }
1123
1124 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
1125
1126 static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
1127 struct snd_pcm_hw_rule *rule)
1128 {
1129 struct snd_pcm_hw_constraint_list *list = rule->private;
1130 return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
1131 }
1132
1133
1134 /**
1135 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
1136 * @runtime: PCM runtime instance
1137 * @cond: condition bits
1138 * @var: hw_params variable to apply the list constraint
1139 * @l: list
1140 *
1141 * Apply the list of constraints to an interval parameter.
1142 */
1143 int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
1144 unsigned int cond,
1145 snd_pcm_hw_param_t var,
1146 struct snd_pcm_hw_constraint_list *l)
1147 {
1148 return snd_pcm_hw_rule_add(runtime, cond, var,
1149 snd_pcm_hw_rule_list, l,
1150 var, -1);
1151 }
1152
1153 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
1154
1155 static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
1156 struct snd_pcm_hw_rule *rule)
1157 {
1158 struct snd_pcm_hw_constraint_ratnums *r = rule->private;
1159 unsigned int num = 0, den = 0;
1160 int err;
1161 err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1162 r->nrats, r->rats, &num, &den);
1163 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1164 params->rate_num = num;
1165 params->rate_den = den;
1166 }
1167 return err;
1168 }
1169
1170 /**
1171 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
1172 * @runtime: PCM runtime instance
1173 * @cond: condition bits
1174 * @var: hw_params variable to apply the ratnums constraint
1175 * @r: struct snd_ratnums constriants
1176 */
1177 int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
1178 unsigned int cond,
1179 snd_pcm_hw_param_t var,
1180 struct snd_pcm_hw_constraint_ratnums *r)
1181 {
1182 return snd_pcm_hw_rule_add(runtime, cond, var,
1183 snd_pcm_hw_rule_ratnums, r,
1184 var, -1);
1185 }
1186
1187 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
1188
1189 static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
1190 struct snd_pcm_hw_rule *rule)
1191 {
1192 struct snd_pcm_hw_constraint_ratdens *r = rule->private;
1193 unsigned int num = 0, den = 0;
1194 int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1195 r->nrats, r->rats, &num, &den);
1196 if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1197 params->rate_num = num;
1198 params->rate_den = den;
1199 }
1200 return err;
1201 }
1202
1203 /**
1204 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
1205 * @runtime: PCM runtime instance
1206 * @cond: condition bits
1207 * @var: hw_params variable to apply the ratdens constraint
1208 * @r: struct snd_ratdens constriants
1209 */
1210 int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
1211 unsigned int cond,
1212 snd_pcm_hw_param_t var,
1213 struct snd_pcm_hw_constraint_ratdens *r)
1214 {
1215 return snd_pcm_hw_rule_add(runtime, cond, var,
1216 snd_pcm_hw_rule_ratdens, r,
1217 var, -1);
1218 }
1219
1220 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
1221
1222 static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
1223 struct snd_pcm_hw_rule *rule)
1224 {
1225 unsigned int l = (unsigned long) rule->private;
1226 int width = l & 0xffff;
1227 unsigned int msbits = l >> 16;
1228 struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1229 if (snd_interval_single(i) && snd_interval_value(i) == width)
1230 params->msbits = msbits;
1231 return 0;
1232 }
1233
1234 /**
1235 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
1236 * @runtime: PCM runtime instance
1237 * @cond: condition bits
1238 * @width: sample bits width
1239 * @msbits: msbits width
1240 */
1241 int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
1242 unsigned int cond,
1243 unsigned int width,
1244 unsigned int msbits)
1245 {
1246 unsigned long l = (msbits << 16) | width;
1247 return snd_pcm_hw_rule_add(runtime, cond, -1,
1248 snd_pcm_hw_rule_msbits,
1249 (void*) l,
1250 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1251 }
1252
1253 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
1254
1255 static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
1256 struct snd_pcm_hw_rule *rule)
1257 {
1258 unsigned long step = (unsigned long) rule->private;
1259 return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1260 }
1261
1262 /**
1263 * snd_pcm_hw_constraint_step - add a hw constraint step rule
1264 * @runtime: PCM runtime instance
1265 * @cond: condition bits
1266 * @var: hw_params variable to apply the step constraint
1267 * @step: step size
1268 */
1269 int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
1270 unsigned int cond,
1271 snd_pcm_hw_param_t var,
1272 unsigned long step)
1273 {
1274 return snd_pcm_hw_rule_add(runtime, cond, var,
1275 snd_pcm_hw_rule_step, (void *) step,
1276 var, -1);
1277 }
1278
1279 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
1280
1281 static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
1282 {
1283 static unsigned int pow2_sizes[] = {
1284 1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1285 1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1286 1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1287 1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1288 };
1289 return snd_interval_list(hw_param_interval(params, rule->var),
1290 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1291 }
1292
1293 /**
1294 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
1295 * @runtime: PCM runtime instance
1296 * @cond: condition bits
1297 * @var: hw_params variable to apply the power-of-2 constraint
1298 */
1299 int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
1300 unsigned int cond,
1301 snd_pcm_hw_param_t var)
1302 {
1303 return snd_pcm_hw_rule_add(runtime, cond, var,
1304 snd_pcm_hw_rule_pow2, NULL,
1305 var, -1);
1306 }
1307
1308 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
1309
1310 static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1311 snd_pcm_hw_param_t var)
1312 {
1313 if (hw_is_mask(var)) {
1314 snd_mask_any(hw_param_mask(params, var));
1315 params->cmask |= 1 << var;
1316 params->rmask |= 1 << var;
1317 return;
1318 }
1319 if (hw_is_interval(var)) {
1320 snd_interval_any(hw_param_interval(params, var));
1321 params->cmask |= 1 << var;
1322 params->rmask |= 1 << var;
1323 return;
1324 }
1325 snd_BUG();
1326 }
1327
1328 void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
1329 {
1330 unsigned int k;
1331 memset(params, 0, sizeof(*params));
1332 for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1333 _snd_pcm_hw_param_any(params, k);
1334 for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1335 _snd_pcm_hw_param_any(params, k);
1336 params->info = ~0U;
1337 }
1338
1339 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
1340
1341 /**
1342 * snd_pcm_hw_param_value - return @params field @var value
1343 * @params: the hw_params instance
1344 * @var: parameter to retrieve
1345 * @dir: pointer to the direction (-1,0,1) or %NULL
1346 *
1347 * Return the value for field @var if it's fixed in configuration space
1348 * defined by @params. Return -%EINVAL otherwise.
1349 */
1350 int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
1351 snd_pcm_hw_param_t var, int *dir)
1352 {
1353 if (hw_is_mask(var)) {
1354 const struct snd_mask *mask = hw_param_mask_c(params, var);
1355 if (!snd_mask_single(mask))
1356 return -EINVAL;
1357 if (dir)
1358 *dir = 0;
1359 return snd_mask_value(mask);
1360 }
1361 if (hw_is_interval(var)) {
1362 const struct snd_interval *i = hw_param_interval_c(params, var);
1363 if (!snd_interval_single(i))
1364 return -EINVAL;
1365 if (dir)
1366 *dir = i->openmin;
1367 return snd_interval_value(i);
1368 }
1369 return -EINVAL;
1370 }
1371
1372 EXPORT_SYMBOL(snd_pcm_hw_param_value);
1373
1374 void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
1375 snd_pcm_hw_param_t var)
1376 {
1377 if (hw_is_mask(var)) {
1378 snd_mask_none(hw_param_mask(params, var));
1379 params->cmask |= 1 << var;
1380 params->rmask |= 1 << var;
1381 } else if (hw_is_interval(var)) {
1382 snd_interval_none(hw_param_interval(params, var));
1383 params->cmask |= 1 << var;
1384 params->rmask |= 1 << var;
1385 } else {
1386 snd_BUG();
1387 }
1388 }
1389
1390 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
1391
1392 static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1393 snd_pcm_hw_param_t var)
1394 {
1395 int changed;
1396 if (hw_is_mask(var))
1397 changed = snd_mask_refine_first(hw_param_mask(params, var));
1398 else if (hw_is_interval(var))
1399 changed = snd_interval_refine_first(hw_param_interval(params, var));
1400 else
1401 return -EINVAL;
1402 if (changed) {
1403 params->cmask |= 1 << var;
1404 params->rmask |= 1 << var;
1405 }
1406 return changed;
1407 }
1408
1409
1410 /**
1411 * snd_pcm_hw_param_first - refine config space and return minimum value
1412 * @pcm: PCM instance
1413 * @params: the hw_params instance
1414 * @var: parameter to retrieve
1415 * @dir: pointer to the direction (-1,0,1) or %NULL
1416 *
1417 * Inside configuration space defined by @params remove from @var all
1418 * values > minimum. Reduce configuration space accordingly.
1419 * Return the minimum.
1420 */
1421 int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm,
1422 struct snd_pcm_hw_params *params,
1423 snd_pcm_hw_param_t var, int *dir)
1424 {
1425 int changed = _snd_pcm_hw_param_first(params, var);
1426 if (changed < 0)
1427 return changed;
1428 if (params->rmask) {
1429 int err = snd_pcm_hw_refine(pcm, params);
1430 if (snd_BUG_ON(err < 0))
1431 return err;
1432 }
1433 return snd_pcm_hw_param_value(params, var, dir);
1434 }
1435
1436 EXPORT_SYMBOL(snd_pcm_hw_param_first);
1437
1438 static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1439 snd_pcm_hw_param_t var)
1440 {
1441 int changed;
1442 if (hw_is_mask(var))
1443 changed = snd_mask_refine_last(hw_param_mask(params, var));
1444 else if (hw_is_interval(var))
1445 changed = snd_interval_refine_last(hw_param_interval(params, var));
1446 else
1447 return -EINVAL;
1448 if (changed) {
1449 params->cmask |= 1 << var;
1450 params->rmask |= 1 << var;
1451 }
1452 return changed;
1453 }
1454
1455
1456 /**
1457 * snd_pcm_hw_param_last - refine config space and return maximum value
1458 * @pcm: PCM instance
1459 * @params: the hw_params instance
1460 * @var: parameter to retrieve
1461 * @dir: pointer to the direction (-1,0,1) or %NULL
1462 *
1463 * Inside configuration space defined by @params remove from @var all
1464 * values < maximum. Reduce configuration space accordingly.
1465 * Return the maximum.
1466 */
1467 int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm,
1468 struct snd_pcm_hw_params *params,
1469 snd_pcm_hw_param_t var, int *dir)
1470 {
1471 int changed = _snd_pcm_hw_param_last(params, var);
1472 if (changed < 0)
1473 return changed;
1474 if (params->rmask) {
1475 int err = snd_pcm_hw_refine(pcm, params);
1476 if (snd_BUG_ON(err < 0))
1477 return err;
1478 }
1479 return snd_pcm_hw_param_value(params, var, dir);
1480 }
1481
1482 EXPORT_SYMBOL(snd_pcm_hw_param_last);
1483
1484 /**
1485 * snd_pcm_hw_param_choose - choose a configuration defined by @params
1486 * @pcm: PCM instance
1487 * @params: the hw_params instance
1488 *
1489 * Choose one configuration from configuration space defined by @params.
1490 * The configuration chosen is that obtained fixing in this order:
1491 * first access, first format, first subformat, min channels,
1492 * min rate, min period time, max buffer size, min tick time
1493 */
1494 int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
1495 struct snd_pcm_hw_params *params)
1496 {
1497 static int vars[] = {
1498 SNDRV_PCM_HW_PARAM_ACCESS,
1499 SNDRV_PCM_HW_PARAM_FORMAT,
1500 SNDRV_PCM_HW_PARAM_SUBFORMAT,
1501 SNDRV_PCM_HW_PARAM_CHANNELS,
1502 SNDRV_PCM_HW_PARAM_RATE,
1503 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
1504 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
1505 SNDRV_PCM_HW_PARAM_TICK_TIME,
1506 -1
1507 };
1508 int err, *v;
1509
1510 for (v = vars; *v != -1; v++) {
1511 if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
1512 err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
1513 else
1514 err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1515 if (snd_BUG_ON(err < 0))
1516 return err;
1517 }
1518 return 0;
1519 }
1520
1521 static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
1522 void *arg)
1523 {
1524 struct snd_pcm_runtime *runtime = substream->runtime;
1525 unsigned long flags;
1526 snd_pcm_stream_lock_irqsave(substream, flags);
1527 if (snd_pcm_running(substream) &&
1528 snd_pcm_update_hw_ptr(substream) >= 0)
1529 runtime->status->hw_ptr %= runtime->buffer_size;
1530 else
1531 runtime->status->hw_ptr = 0;
1532 snd_pcm_stream_unlock_irqrestore(substream, flags);
1533 return 0;
1534 }
1535
1536 static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
1537 void *arg)
1538 {
1539 struct snd_pcm_channel_info *info = arg;
1540 struct snd_pcm_runtime *runtime = substream->runtime;
1541 int width;
1542 if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1543 info->offset = -1;
1544 return 0;
1545 }
1546 width = snd_pcm_format_physical_width(runtime->format);
1547 if (width < 0)
1548 return width;
1549 info->offset = 0;
1550 switch (runtime->access) {
1551 case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1552 case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1553 info->first = info->channel * width;
1554 info->step = runtime->channels * width;
1555 break;
1556 case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1557 case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1558 {
1559 size_t size = runtime->dma_bytes / runtime->channels;
1560 info->first = info->channel * size * 8;
1561 info->step = width;
1562 break;
1563 }
1564 default:
1565 snd_BUG();
1566 break;
1567 }
1568 return 0;
1569 }
1570
1571 static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
1572 void *arg)
1573 {
1574 struct snd_pcm_hw_params *params = arg;
1575 snd_pcm_format_t format;
1576 int channels, width;
1577
1578 params->fifo_size = substream->runtime->hw.fifo_size;
1579 if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
1580 format = params_format(params);
1581 channels = params_channels(params);
1582 width = snd_pcm_format_physical_width(format);
1583 params->fifo_size /= width * channels;
1584 }
1585 return 0;
1586 }
1587
1588 /**
1589 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1590 * @substream: the pcm substream instance
1591 * @cmd: ioctl command
1592 * @arg: ioctl argument
1593 *
1594 * Processes the generic ioctl commands for PCM.
1595 * Can be passed as the ioctl callback for PCM ops.
1596 *
1597 * Returns zero if successful, or a negative error code on failure.
1598 */
1599 int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
1600 unsigned int cmd, void *arg)
1601 {
1602 switch (cmd) {
1603 case SNDRV_PCM_IOCTL1_INFO:
1604 return 0;
1605 case SNDRV_PCM_IOCTL1_RESET:
1606 return snd_pcm_lib_ioctl_reset(substream, arg);
1607 case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1608 return snd_pcm_lib_ioctl_channel_info(substream, arg);
1609 case SNDRV_PCM_IOCTL1_FIFO_SIZE:
1610 return snd_pcm_lib_ioctl_fifo_size(substream, arg);
1611 }
1612 return -ENXIO;
1613 }
1614
1615 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
1616
1617 /**
1618 * snd_pcm_period_elapsed - update the pcm status for the next period
1619 * @substream: the pcm substream instance
1620 *
1621 * This function is called from the interrupt handler when the
1622 * PCM has processed the period size. It will update the current
1623 * pointer, wake up sleepers, etc.
1624 *
1625 * Even if more than one periods have elapsed since the last call, you
1626 * have to call this only once.
1627 */
1628 void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
1629 {
1630 struct snd_pcm_runtime *runtime;
1631 unsigned long flags;
1632
1633 if (PCM_RUNTIME_CHECK(substream))
1634 return;
1635 runtime = substream->runtime;
1636
1637 if (runtime->transfer_ack_begin)
1638 runtime->transfer_ack_begin(substream);
1639
1640 snd_pcm_stream_lock_irqsave(substream, flags);
1641 if (!snd_pcm_running(substream) ||
1642 snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1643 goto _end;
1644
1645 if (substream->timer_running)
1646 snd_timer_interrupt(substream->timer, 1);
1647 _end:
1648 snd_pcm_stream_unlock_irqrestore(substream, flags);
1649 if (runtime->transfer_ack_end)
1650 runtime->transfer_ack_end(substream);
1651 kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1652 }
1653
1654 EXPORT_SYMBOL(snd_pcm_period_elapsed);
1655
1656 /*
1657 * Wait until avail_min data becomes available
1658 * Returns a negative error code if any error occurs during operation.
1659 * The available space is stored on availp. When err = 0 and avail = 0
1660 * on the capture stream, it indicates the stream is in DRAINING state.
1661 */
1662 static int wait_for_avail_min(struct snd_pcm_substream *substream,
1663 snd_pcm_uframes_t *availp)
1664 {
1665 struct snd_pcm_runtime *runtime = substream->runtime;
1666 int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
1667 wait_queue_t wait;
1668 int err = 0;
1669 snd_pcm_uframes_t avail = 0;
1670 long tout;
1671
1672 init_waitqueue_entry(&wait, current);
1673 add_wait_queue(&runtime->sleep, &wait);
1674 for (;;) {
1675 if (signal_pending(current)) {
1676 err = -ERESTARTSYS;
1677 break;
1678 }
1679 set_current_state(TASK_INTERRUPTIBLE);
1680 snd_pcm_stream_unlock_irq(substream);
1681 tout = schedule_timeout(msecs_to_jiffies(10000));
1682 snd_pcm_stream_lock_irq(substream);
1683 switch (runtime->status->state) {
1684 case SNDRV_PCM_STATE_SUSPENDED:
1685 err = -ESTRPIPE;
1686 goto _endloop;
1687 case SNDRV_PCM_STATE_XRUN:
1688 err = -EPIPE;
1689 goto _endloop;
1690 case SNDRV_PCM_STATE_DRAINING:
1691 if (is_playback)
1692 err = -EPIPE;
1693 else
1694 avail = 0; /* indicate draining */
1695 goto _endloop;
1696 case SNDRV_PCM_STATE_OPEN:
1697 case SNDRV_PCM_STATE_SETUP:
1698 case SNDRV_PCM_STATE_DISCONNECTED:
1699 err = -EBADFD;
1700 goto _endloop;
1701 }
1702 if (!tout) {
1703 snd_printd("%s write error (DMA or IRQ trouble?)\n",
1704 is_playback ? "playback" : "capture");
1705 err = -EIO;
1706 break;
1707 }
1708 if (is_playback)
1709 avail = snd_pcm_playback_avail(runtime);
1710 else
1711 avail = snd_pcm_capture_avail(runtime);
1712 if (avail >= runtime->control->avail_min)
1713 break;
1714 }
1715 _endloop:
1716 remove_wait_queue(&runtime->sleep, &wait);
1717 *availp = avail;
1718 return err;
1719 }
1720
1721 static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
1722 unsigned int hwoff,
1723 unsigned long data, unsigned int off,
1724 snd_pcm_uframes_t frames)
1725 {
1726 struct snd_pcm_runtime *runtime = substream->runtime;
1727 int err;
1728 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1729 if (substream->ops->copy) {
1730 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1731 return err;
1732 } else {
1733 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1734 if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
1735 return -EFAULT;
1736 }
1737 return 0;
1738 }
1739
1740 typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
1741 unsigned long data, unsigned int off,
1742 snd_pcm_uframes_t size);
1743
1744 static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream,
1745 unsigned long data,
1746 snd_pcm_uframes_t size,
1747 int nonblock,
1748 transfer_f transfer)
1749 {
1750 struct snd_pcm_runtime *runtime = substream->runtime;
1751 snd_pcm_uframes_t xfer = 0;
1752 snd_pcm_uframes_t offset = 0;
1753 int err = 0;
1754
1755 if (size == 0)
1756 return 0;
1757
1758 snd_pcm_stream_lock_irq(substream);
1759 switch (runtime->status->state) {
1760 case SNDRV_PCM_STATE_PREPARED:
1761 case SNDRV_PCM_STATE_RUNNING:
1762 case SNDRV_PCM_STATE_PAUSED:
1763 break;
1764 case SNDRV_PCM_STATE_XRUN:
1765 err = -EPIPE;
1766 goto _end_unlock;
1767 case SNDRV_PCM_STATE_SUSPENDED:
1768 err = -ESTRPIPE;
1769 goto _end_unlock;
1770 default:
1771 err = -EBADFD;
1772 goto _end_unlock;
1773 }
1774
1775 while (size > 0) {
1776 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1777 snd_pcm_uframes_t avail;
1778 snd_pcm_uframes_t cont;
1779 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1780 snd_pcm_update_hw_ptr(substream);
1781 avail = snd_pcm_playback_avail(runtime);
1782 if (!avail) {
1783 if (nonblock) {
1784 err = -EAGAIN;
1785 goto _end_unlock;
1786 }
1787 err = wait_for_avail_min(substream, &avail);
1788 if (err < 0)
1789 goto _end_unlock;
1790 }
1791 frames = size > avail ? avail : size;
1792 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
1793 if (frames > cont)
1794 frames = cont;
1795 if (snd_BUG_ON(!frames)) {
1796 snd_pcm_stream_unlock_irq(substream);
1797 return -EINVAL;
1798 }
1799 appl_ptr = runtime->control->appl_ptr;
1800 appl_ofs = appl_ptr % runtime->buffer_size;
1801 snd_pcm_stream_unlock_irq(substream);
1802 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
1803 goto _end;
1804 snd_pcm_stream_lock_irq(substream);
1805 switch (runtime->status->state) {
1806 case SNDRV_PCM_STATE_XRUN:
1807 err = -EPIPE;
1808 goto _end_unlock;
1809 case SNDRV_PCM_STATE_SUSPENDED:
1810 err = -ESTRPIPE;
1811 goto _end_unlock;
1812 default:
1813 break;
1814 }
1815 appl_ptr += frames;
1816 if (appl_ptr >= runtime->boundary)
1817 appl_ptr -= runtime->boundary;
1818 runtime->control->appl_ptr = appl_ptr;
1819 if (substream->ops->ack)
1820 substream->ops->ack(substream);
1821
1822 offset += frames;
1823 size -= frames;
1824 xfer += frames;
1825 if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
1826 snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
1827 err = snd_pcm_start(substream);
1828 if (err < 0)
1829 goto _end_unlock;
1830 }
1831 }
1832 _end_unlock:
1833 snd_pcm_stream_unlock_irq(substream);
1834 _end:
1835 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
1836 }
1837
1838 /* sanity-check for read/write methods */
1839 static int pcm_sanity_check(struct snd_pcm_substream *substream)
1840 {
1841 struct snd_pcm_runtime *runtime;
1842 if (PCM_RUNTIME_CHECK(substream))
1843 return -ENXIO;
1844 runtime = substream->runtime;
1845 if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
1846 return -EINVAL;
1847 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
1848 return -EBADFD;
1849 return 0;
1850 }
1851
1852 snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
1853 {
1854 struct snd_pcm_runtime *runtime;
1855 int nonblock;
1856 int err;
1857
1858 err = pcm_sanity_check(substream);
1859 if (err < 0)
1860 return err;
1861 runtime = substream->runtime;
1862 nonblock = !!(substream->f_flags & O_NONBLOCK);
1863
1864 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
1865 runtime->channels > 1)
1866 return -EINVAL;
1867 return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
1868 snd_pcm_lib_write_transfer);
1869 }
1870
1871 EXPORT_SYMBOL(snd_pcm_lib_write);
1872
1873 static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
1874 unsigned int hwoff,
1875 unsigned long data, unsigned int off,
1876 snd_pcm_uframes_t frames)
1877 {
1878 struct snd_pcm_runtime *runtime = substream->runtime;
1879 int err;
1880 void __user **bufs = (void __user **)data;
1881 int channels = runtime->channels;
1882 int c;
1883 if (substream->ops->copy) {
1884 if (snd_BUG_ON(!substream->ops->silence))
1885 return -EINVAL;
1886 for (c = 0; c < channels; ++c, ++bufs) {
1887 if (*bufs == NULL) {
1888 if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
1889 return err;
1890 } else {
1891 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1892 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
1893 return err;
1894 }
1895 }
1896 } else {
1897 /* default transfer behaviour */
1898 size_t dma_csize = runtime->dma_bytes / channels;
1899 for (c = 0; c < channels; ++c, ++bufs) {
1900 char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
1901 if (*bufs == NULL) {
1902 snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
1903 } else {
1904 char __user *buf = *bufs + samples_to_bytes(runtime, off);
1905 if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
1906 return -EFAULT;
1907 }
1908 }
1909 }
1910 return 0;
1911 }
1912
1913 snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
1914 void __user **bufs,
1915 snd_pcm_uframes_t frames)
1916 {
1917 struct snd_pcm_runtime *runtime;
1918 int nonblock;
1919 int err;
1920
1921 err = pcm_sanity_check(substream);
1922 if (err < 0)
1923 return err;
1924 runtime = substream->runtime;
1925 nonblock = !!(substream->f_flags & O_NONBLOCK);
1926
1927 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
1928 return -EINVAL;
1929 return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
1930 nonblock, snd_pcm_lib_writev_transfer);
1931 }
1932
1933 EXPORT_SYMBOL(snd_pcm_lib_writev);
1934
1935 static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream,
1936 unsigned int hwoff,
1937 unsigned long data, unsigned int off,
1938 snd_pcm_uframes_t frames)
1939 {
1940 struct snd_pcm_runtime *runtime = substream->runtime;
1941 int err;
1942 char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
1943 if (substream->ops->copy) {
1944 if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
1945 return err;
1946 } else {
1947 char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
1948 if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
1949 return -EFAULT;
1950 }
1951 return 0;
1952 }
1953
1954 static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
1955 unsigned long data,
1956 snd_pcm_uframes_t size,
1957 int nonblock,
1958 transfer_f transfer)
1959 {
1960 struct snd_pcm_runtime *runtime = substream->runtime;
1961 snd_pcm_uframes_t xfer = 0;
1962 snd_pcm_uframes_t offset = 0;
1963 int err = 0;
1964
1965 if (size == 0)
1966 return 0;
1967
1968 snd_pcm_stream_lock_irq(substream);
1969 switch (runtime->status->state) {
1970 case SNDRV_PCM_STATE_PREPARED:
1971 if (size >= runtime->start_threshold) {
1972 err = snd_pcm_start(substream);
1973 if (err < 0)
1974 goto _end_unlock;
1975 }
1976 break;
1977 case SNDRV_PCM_STATE_DRAINING:
1978 case SNDRV_PCM_STATE_RUNNING:
1979 case SNDRV_PCM_STATE_PAUSED:
1980 break;
1981 case SNDRV_PCM_STATE_XRUN:
1982 err = -EPIPE;
1983 goto _end_unlock;
1984 case SNDRV_PCM_STATE_SUSPENDED:
1985 err = -ESTRPIPE;
1986 goto _end_unlock;
1987 default:
1988 err = -EBADFD;
1989 goto _end_unlock;
1990 }
1991
1992 while (size > 0) {
1993 snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
1994 snd_pcm_uframes_t avail;
1995 snd_pcm_uframes_t cont;
1996 if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
1997 snd_pcm_update_hw_ptr(substream);
1998 avail = snd_pcm_capture_avail(runtime);
1999 if (!avail) {
2000 if (runtime->status->state ==
2001 SNDRV_PCM_STATE_DRAINING) {
2002 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
2003 goto _end_unlock;
2004 }
2005 if (nonblock) {
2006 err = -EAGAIN;
2007 goto _end_unlock;
2008 }
2009 err = wait_for_avail_min(substream, &avail);
2010 if (err < 0)
2011 goto _end_unlock;
2012 if (!avail)
2013 continue; /* draining */
2014 }
2015 frames = size > avail ? avail : size;
2016 cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2017 if (frames > cont)
2018 frames = cont;
2019 if (snd_BUG_ON(!frames)) {
2020 snd_pcm_stream_unlock_irq(substream);
2021 return -EINVAL;
2022 }
2023 appl_ptr = runtime->control->appl_ptr;
2024 appl_ofs = appl_ptr % runtime->buffer_size;
2025 snd_pcm_stream_unlock_irq(substream);
2026 if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2027 goto _end;
2028 snd_pcm_stream_lock_irq(substream);
2029 switch (runtime->status->state) {
2030 case SNDRV_PCM_STATE_XRUN:
2031 err = -EPIPE;
2032 goto _end_unlock;
2033 case SNDRV_PCM_STATE_SUSPENDED:
2034 err = -ESTRPIPE;
2035 goto _end_unlock;
2036 default:
2037 break;
2038 }
2039 appl_ptr += frames;
2040 if (appl_ptr >= runtime->boundary)
2041 appl_ptr -= runtime->boundary;
2042 runtime->control->appl_ptr = appl_ptr;
2043 if (substream->ops->ack)
2044 substream->ops->ack(substream);
2045
2046 offset += frames;
2047 size -= frames;
2048 xfer += frames;
2049 }
2050 _end_unlock:
2051 snd_pcm_stream_unlock_irq(substream);
2052 _end:
2053 return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2054 }
2055
2056 snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
2057 {
2058 struct snd_pcm_runtime *runtime;
2059 int nonblock;
2060 int err;
2061
2062 err = pcm_sanity_check(substream);
2063 if (err < 0)
2064 return err;
2065 runtime = substream->runtime;
2066 nonblock = !!(substream->f_flags & O_NONBLOCK);
2067 if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2068 return -EINVAL;
2069 return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2070 }
2071
2072 EXPORT_SYMBOL(snd_pcm_lib_read);
2073
2074 static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
2075 unsigned int hwoff,
2076 unsigned long data, unsigned int off,
2077 snd_pcm_uframes_t frames)
2078 {
2079 struct snd_pcm_runtime *runtime = substream->runtime;
2080 int err;
2081 void __user **bufs = (void __user **)data;
2082 int channels = runtime->channels;
2083 int c;
2084 if (substream->ops->copy) {
2085 for (c = 0; c < channels; ++c, ++bufs) {
2086 char __user *buf;
2087 if (*bufs == NULL)
2088 continue;
2089 buf = *bufs + samples_to_bytes(runtime, off);
2090 if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2091 return err;
2092 }
2093 } else {
2094 snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2095 for (c = 0; c < channels; ++c, ++bufs) {
2096 char *hwbuf;
2097 char __user *buf;
2098 if (*bufs == NULL)
2099 continue;
2100
2101 hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2102 buf = *bufs + samples_to_bytes(runtime, off);
2103 if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2104 return -EFAULT;
2105 }
2106 }
2107 return 0;
2108 }
2109
2110 snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
2111 void __user **bufs,
2112 snd_pcm_uframes_t frames)
2113 {
2114 struct snd_pcm_runtime *runtime;
2115 int nonblock;
2116 int err;
2117
2118 err = pcm_sanity_check(substream);
2119 if (err < 0)
2120 return err;
2121 runtime = substream->runtime;
2122 if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2123 return -EBADFD;
2124
2125 nonblock = !!(substream->f_flags & O_NONBLOCK);
2126 if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2127 return -EINVAL;
2128 return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2129 }
2130
2131 EXPORT_SYMBOL(snd_pcm_lib_readv);
2132
|
This page was automatically generated by the
LXR engine.
|