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