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