xref: /linux/sound/core/pcm_lib.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
1 /*
2  *  Digital Audio (PCM) abstract layer
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *                   Abramo Bagnara <abramo@alsa-project.org>
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22 
23 #include <sound/driver.h>
24 #include <linux/slab.h>
25 #include <linux/time.h>
26 #include <sound/core.h>
27 #include <sound/control.h>
28 #include <sound/info.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/timer.h>
32 
33 /*
34  * fill ring buffer with silence
35  * runtime->silence_start: starting pointer to silence area
36  * runtime->silence_filled: size filled with silence
37  * runtime->silence_threshold: threshold from application
38  * runtime->silence_size: maximal size from application
39  *
40  * when runtime->silence_size >= runtime->boundary - fill processed area with silence immediately
41  */
42 void snd_pcm_playback_silence(snd_pcm_substream_t *substream, snd_pcm_uframes_t new_hw_ptr)
43 {
44 	snd_pcm_runtime_t *runtime = substream->runtime;
45 	snd_pcm_uframes_t frames, ofs, transfer;
46 
47 	if (runtime->silence_size < runtime->boundary) {
48 		snd_pcm_sframes_t noise_dist, n;
49 		if (runtime->silence_start != runtime->control->appl_ptr) {
50 			n = runtime->control->appl_ptr - runtime->silence_start;
51 			if (n < 0)
52 				n += runtime->boundary;
53 			if ((snd_pcm_uframes_t)n < runtime->silence_filled)
54 				runtime->silence_filled -= n;
55 			else
56 				runtime->silence_filled = 0;
57 			runtime->silence_start = runtime->control->appl_ptr;
58 		}
59 		if (runtime->silence_filled == runtime->buffer_size)
60 			return;
61 		snd_assert(runtime->silence_filled <= runtime->buffer_size, return);
62 		noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
63 		if (noise_dist >= (snd_pcm_sframes_t) runtime->silence_threshold)
64 			return;
65 		frames = runtime->silence_threshold - noise_dist;
66 		if (frames > runtime->silence_size)
67 			frames = runtime->silence_size;
68 	} else {
69 		if (new_hw_ptr == ULONG_MAX) {	/* initialization */
70 			snd_pcm_sframes_t avail = snd_pcm_playback_hw_avail(runtime);
71 			runtime->silence_filled = avail > 0 ? avail : 0;
72 			runtime->silence_start = (runtime->status->hw_ptr +
73 						  runtime->silence_filled) %
74 						 runtime->boundary;
75 		} else {
76 			ofs = runtime->status->hw_ptr;
77 			frames = new_hw_ptr - ofs;
78 			if ((snd_pcm_sframes_t)frames < 0)
79 				frames += runtime->boundary;
80 			runtime->silence_filled -= frames;
81 			if ((snd_pcm_sframes_t)runtime->silence_filled < 0) {
82 				runtime->silence_filled = 0;
83 				runtime->silence_start = (ofs + frames) - runtime->buffer_size;
84 			} else {
85 				runtime->silence_start = ofs - runtime->silence_filled;
86 			}
87 			if ((snd_pcm_sframes_t)runtime->silence_start < 0)
88 				runtime->silence_start += runtime->boundary;
89 		}
90 		frames = runtime->buffer_size - runtime->silence_filled;
91 	}
92 	snd_assert(frames <= runtime->buffer_size, return);
93 	if (frames == 0)
94 		return;
95 	ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
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);
103 				snd_assert(err >= 0, );
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);
115 					snd_assert(err >= 0, );
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 
131 static void xrun(snd_pcm_substream_t *substream)
132 {
133 	snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
134 #ifdef CONFIG_SND_DEBUG
135 	if (substream->pstr->xrun_debug) {
136 		snd_printd(KERN_DEBUG "XRUN: pcmC%dD%d%c\n",
137 			   substream->pcm->card->number,
138 			   substream->pcm->device,
139 			   substream->stream ? 'c' : 'p');
140 		if (substream->pstr->xrun_debug > 1)
141 			dump_stack();
142 	}
143 #endif
144 }
145 
146 static inline snd_pcm_uframes_t snd_pcm_update_hw_ptr_pos(snd_pcm_substream_t *substream,
147 							  snd_pcm_runtime_t *runtime)
148 {
149 	snd_pcm_uframes_t pos;
150 
151 	pos = substream->ops->pointer(substream);
152 	if (pos == SNDRV_PCM_POS_XRUN)
153 		return pos; /* XRUN */
154 	if (runtime->tstamp_mode & SNDRV_PCM_TSTAMP_MMAP)
155 		snd_timestamp_now((snd_timestamp_t*)&runtime->status->tstamp, runtime->tstamp_timespec);
156 #ifdef CONFIG_SND_DEBUG
157 	if (pos >= runtime->buffer_size) {
158 		snd_printk(KERN_ERR  "BUG: stream = %i, pos = 0x%lx, buffer size = 0x%lx, period size = 0x%lx\n", substream->stream, pos, runtime->buffer_size, runtime->period_size);
159 	} else
160 #endif
161 	snd_runtime_check(pos < runtime->buffer_size, return 0);
162 	pos -= pos % runtime->min_align;
163 	return pos;
164 }
165 
166 static inline int snd_pcm_update_hw_ptr_post(snd_pcm_substream_t *substream,
167 					     snd_pcm_runtime_t *runtime)
168 {
169 	snd_pcm_uframes_t avail;
170 
171 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
172 		avail = snd_pcm_playback_avail(runtime);
173 	else
174 		avail = snd_pcm_capture_avail(runtime);
175 	if (avail > runtime->avail_max)
176 		runtime->avail_max = avail;
177 	if (avail >= runtime->stop_threshold) {
178 		if (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING)
179 			snd_pcm_drain_done(substream);
180 		else
181 			xrun(substream);
182 		return -EPIPE;
183 	}
184 	if (avail >= runtime->control->avail_min)
185 		wake_up(&runtime->sleep);
186 	return 0;
187 }
188 
189 static inline int snd_pcm_update_hw_ptr_interrupt(snd_pcm_substream_t *substream)
190 {
191 	snd_pcm_runtime_t *runtime = substream->runtime;
192 	snd_pcm_uframes_t pos;
193 	snd_pcm_uframes_t new_hw_ptr, hw_ptr_interrupt;
194 	snd_pcm_sframes_t delta;
195 
196 	pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
197 	if (pos == SNDRV_PCM_POS_XRUN) {
198 		xrun(substream);
199 		return -EPIPE;
200 	}
201 	if (runtime->period_size == runtime->buffer_size)
202 		goto __next_buf;
203 	new_hw_ptr = runtime->hw_ptr_base + pos;
204 	hw_ptr_interrupt = runtime->hw_ptr_interrupt + runtime->period_size;
205 
206 	delta = hw_ptr_interrupt - new_hw_ptr;
207 	if (delta > 0) {
208 		if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
209 #ifdef CONFIG_SND_DEBUG
210 			if (runtime->periods > 1 && substream->pstr->xrun_debug) {
211 				snd_printd(KERN_ERR "Unexpected hw_pointer value [1] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
212 				if (substream->pstr->xrun_debug > 1)
213 					dump_stack();
214 			}
215 #endif
216 			return 0;
217 		}
218 	      __next_buf:
219 		runtime->hw_ptr_base += runtime->buffer_size;
220 		if (runtime->hw_ptr_base == runtime->boundary)
221 			runtime->hw_ptr_base = 0;
222 		new_hw_ptr = runtime->hw_ptr_base + pos;
223 	}
224 
225 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
226 	    runtime->silence_size > 0)
227 		snd_pcm_playback_silence(substream, new_hw_ptr);
228 
229 	runtime->status->hw_ptr = new_hw_ptr;
230 	runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
231 
232 	return snd_pcm_update_hw_ptr_post(substream, runtime);
233 }
234 
235 /* CAUTION: call it with irq disabled */
236 int snd_pcm_update_hw_ptr(snd_pcm_substream_t *substream)
237 {
238 	snd_pcm_runtime_t *runtime = substream->runtime;
239 	snd_pcm_uframes_t pos;
240 	snd_pcm_uframes_t old_hw_ptr, new_hw_ptr;
241 	snd_pcm_sframes_t delta;
242 
243 	old_hw_ptr = runtime->status->hw_ptr;
244 	pos = snd_pcm_update_hw_ptr_pos(substream, runtime);
245 	if (pos == SNDRV_PCM_POS_XRUN) {
246 		xrun(substream);
247 		return -EPIPE;
248 	}
249 	new_hw_ptr = runtime->hw_ptr_base + pos;
250 
251 	delta = old_hw_ptr - new_hw_ptr;
252 	if (delta > 0) {
253 		if ((snd_pcm_uframes_t)delta < runtime->buffer_size / 2) {
254 #ifdef CONFIG_SND_DEBUG
255 			if (runtime->periods > 2 && substream->pstr->xrun_debug) {
256 				snd_printd(KERN_ERR "Unexpected hw_pointer value [2] (stream = %i, delta: -%ld, max jitter = %ld): wrong interrupt acknowledge?\n", substream->stream, (long) delta, runtime->buffer_size / 2);
257 				if (substream->pstr->xrun_debug > 1)
258 					dump_stack();
259 			}
260 #endif
261 			return 0;
262 		}
263 		runtime->hw_ptr_base += runtime->buffer_size;
264 		if (runtime->hw_ptr_base == runtime->boundary)
265 			runtime->hw_ptr_base = 0;
266 		new_hw_ptr = runtime->hw_ptr_base + pos;
267 	}
268 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
269 	    runtime->silence_size > 0)
270 		snd_pcm_playback_silence(substream, new_hw_ptr);
271 
272 	runtime->status->hw_ptr = new_hw_ptr;
273 
274 	return snd_pcm_update_hw_ptr_post(substream, runtime);
275 }
276 
277 /**
278  * snd_pcm_set_ops - set the PCM operators
279  * @pcm: the pcm instance
280  * @direction: stream direction, SNDRV_PCM_STREAM_XXX
281  * @ops: the operator table
282  *
283  * Sets the given PCM operators to the pcm instance.
284  */
285 void snd_pcm_set_ops(snd_pcm_t *pcm, int direction, snd_pcm_ops_t *ops)
286 {
287 	snd_pcm_str_t *stream = &pcm->streams[direction];
288 	snd_pcm_substream_t *substream;
289 
290 	for (substream = stream->substream; substream != NULL; substream = substream->next)
291 		substream->ops = ops;
292 }
293 
294 
295 /**
296  * snd_pcm_sync - set the PCM sync id
297  * @substream: the pcm substream
298  *
299  * Sets the PCM sync identifier for the card.
300  */
301 void snd_pcm_set_sync(snd_pcm_substream_t * substream)
302 {
303 	snd_pcm_runtime_t *runtime = substream->runtime;
304 
305 	runtime->sync.id32[0] = substream->pcm->card->number;
306 	runtime->sync.id32[1] = -1;
307 	runtime->sync.id32[2] = -1;
308 	runtime->sync.id32[3] = -1;
309 }
310 
311 /*
312  *  Standard ioctl routine
313  */
314 
315 /* Code taken from alsa-lib */
316 #define assert(a) snd_assert((a), return -EINVAL)
317 
318 static inline unsigned int div32(unsigned int a, unsigned int b,
319 				 unsigned int *r)
320 {
321 	if (b == 0) {
322 		*r = 0;
323 		return UINT_MAX;
324 	}
325 	*r = a % b;
326 	return a / b;
327 }
328 
329 static inline unsigned int div_down(unsigned int a, unsigned int b)
330 {
331 	if (b == 0)
332 		return UINT_MAX;
333 	return a / b;
334 }
335 
336 static inline unsigned int div_up(unsigned int a, unsigned int b)
337 {
338 	unsigned int r;
339 	unsigned int q;
340 	if (b == 0)
341 		return UINT_MAX;
342 	q = div32(a, b, &r);
343 	if (r)
344 		++q;
345 	return q;
346 }
347 
348 static inline unsigned int mul(unsigned int a, unsigned int b)
349 {
350 	if (a == 0)
351 		return 0;
352 	if (div_down(UINT_MAX, a) < b)
353 		return UINT_MAX;
354 	return a * b;
355 }
356 
357 static inline unsigned int muldiv32(unsigned int a, unsigned int b,
358 				    unsigned int c, unsigned int *r)
359 {
360 	u_int64_t n = (u_int64_t) a * b;
361 	if (c == 0) {
362 		snd_assert(n > 0, );
363 		*r = 0;
364 		return UINT_MAX;
365 	}
366 	div64_32(&n, c, r);
367 	if (n >= UINT_MAX) {
368 		*r = 0;
369 		return UINT_MAX;
370 	}
371 	return n;
372 }
373 
374 static int snd_interval_refine_min(snd_interval_t *i, unsigned int min, int openmin)
375 {
376 	int changed = 0;
377 	assert(!snd_interval_empty(i));
378 	if (i->min < min) {
379 		i->min = min;
380 		i->openmin = openmin;
381 		changed = 1;
382 	} else if (i->min == min && !i->openmin && openmin) {
383 		i->openmin = 1;
384 		changed = 1;
385 	}
386 	if (i->integer) {
387 		if (i->openmin) {
388 			i->min++;
389 			i->openmin = 0;
390 		}
391 	}
392 	if (snd_interval_checkempty(i)) {
393 		snd_interval_none(i);
394 		return -EINVAL;
395 	}
396 	return changed;
397 }
398 
399 static int snd_interval_refine_max(snd_interval_t *i, unsigned int max, int openmax)
400 {
401 	int changed = 0;
402 	assert(!snd_interval_empty(i));
403 	if (i->max > max) {
404 		i->max = max;
405 		i->openmax = openmax;
406 		changed = 1;
407 	} else if (i->max == max && !i->openmax && openmax) {
408 		i->openmax = 1;
409 		changed = 1;
410 	}
411 	if (i->integer) {
412 		if (i->openmax) {
413 			i->max--;
414 			i->openmax = 0;
415 		}
416 	}
417 	if (snd_interval_checkempty(i)) {
418 		snd_interval_none(i);
419 		return -EINVAL;
420 	}
421 	return changed;
422 }
423 
424 /**
425  * snd_interval_refine - refine the interval value of configurator
426  * @i: the interval value to refine
427  * @v: the interval value to refer to
428  *
429  * Refines the interval value with the reference value.
430  * The interval is changed to the range satisfying both intervals.
431  * The interval status (min, max, integer, etc.) are evaluated.
432  *
433  * Returns non-zero if the value is changed, zero if not changed.
434  */
435 int snd_interval_refine(snd_interval_t *i, const snd_interval_t *v)
436 {
437 	int changed = 0;
438 	assert(!snd_interval_empty(i));
439 	if (i->min < v->min) {
440 		i->min = v->min;
441 		i->openmin = v->openmin;
442 		changed = 1;
443 	} else if (i->min == v->min && !i->openmin && v->openmin) {
444 		i->openmin = 1;
445 		changed = 1;
446 	}
447 	if (i->max > v->max) {
448 		i->max = v->max;
449 		i->openmax = v->openmax;
450 		changed = 1;
451 	} else if (i->max == v->max && !i->openmax && v->openmax) {
452 		i->openmax = 1;
453 		changed = 1;
454 	}
455 	if (!i->integer && v->integer) {
456 		i->integer = 1;
457 		changed = 1;
458 	}
459 	if (i->integer) {
460 		if (i->openmin) {
461 			i->min++;
462 			i->openmin = 0;
463 		}
464 		if (i->openmax) {
465 			i->max--;
466 			i->openmax = 0;
467 		}
468 	} else if (!i->openmin && !i->openmax && i->min == i->max)
469 		i->integer = 1;
470 	if (snd_interval_checkempty(i)) {
471 		snd_interval_none(i);
472 		return -EINVAL;
473 	}
474 	return changed;
475 }
476 
477 static int snd_interval_refine_first(snd_interval_t *i)
478 {
479 	assert(!snd_interval_empty(i));
480 	if (snd_interval_single(i))
481 		return 0;
482 	i->max = i->min;
483 	i->openmax = i->openmin;
484 	if (i->openmax)
485 		i->max++;
486 	return 1;
487 }
488 
489 static int snd_interval_refine_last(snd_interval_t *i)
490 {
491 	assert(!snd_interval_empty(i));
492 	if (snd_interval_single(i))
493 		return 0;
494 	i->min = i->max;
495 	i->openmin = i->openmax;
496 	if (i->openmin)
497 		i->min--;
498 	return 1;
499 }
500 
501 static int snd_interval_refine_set(snd_interval_t *i, unsigned int val)
502 {
503 	snd_interval_t t;
504 	t.empty = 0;
505 	t.min = t.max = val;
506 	t.openmin = t.openmax = 0;
507 	t.integer = 1;
508 	return snd_interval_refine(i, &t);
509 }
510 
511 void snd_interval_mul(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
512 {
513 	if (a->empty || b->empty) {
514 		snd_interval_none(c);
515 		return;
516 	}
517 	c->empty = 0;
518 	c->min = mul(a->min, b->min);
519 	c->openmin = (a->openmin || b->openmin);
520 	c->max = mul(a->max,  b->max);
521 	c->openmax = (a->openmax || b->openmax);
522 	c->integer = (a->integer && b->integer);
523 }
524 
525 /**
526  * snd_interval_div - refine the interval value with division
527  *
528  * c = a / b
529  *
530  * Returns non-zero if the value is changed, zero if not changed.
531  */
532 void snd_interval_div(const snd_interval_t *a, const snd_interval_t *b, snd_interval_t *c)
533 {
534 	unsigned int r;
535 	if (a->empty || b->empty) {
536 		snd_interval_none(c);
537 		return;
538 	}
539 	c->empty = 0;
540 	c->min = div32(a->min, b->max, &r);
541 	c->openmin = (r || a->openmin || b->openmax);
542 	if (b->min > 0) {
543 		c->max = div32(a->max, b->min, &r);
544 		if (r) {
545 			c->max++;
546 			c->openmax = 1;
547 		} else
548 			c->openmax = (a->openmax || b->openmin);
549 	} else {
550 		c->max = UINT_MAX;
551 		c->openmax = 0;
552 	}
553 	c->integer = 0;
554 }
555 
556 /**
557  * snd_interval_muldivk - refine the interval value
558  *
559  * c = a * b / k
560  *
561  * Returns non-zero if the value is changed, zero if not changed.
562  */
563 void snd_interval_muldivk(const snd_interval_t *a, const snd_interval_t *b,
564 		      unsigned int k, snd_interval_t *c)
565 {
566 	unsigned int r;
567 	if (a->empty || b->empty) {
568 		snd_interval_none(c);
569 		return;
570 	}
571 	c->empty = 0;
572 	c->min = muldiv32(a->min, b->min, k, &r);
573 	c->openmin = (r || a->openmin || b->openmin);
574 	c->max = muldiv32(a->max, b->max, k, &r);
575 	if (r) {
576 		c->max++;
577 		c->openmax = 1;
578 	} else
579 		c->openmax = (a->openmax || b->openmax);
580 	c->integer = 0;
581 }
582 
583 /**
584  * snd_interval_mulkdiv - refine the interval value
585  *
586  * c = a * k / b
587  *
588  * Returns non-zero if the value is changed, zero if not changed.
589  */
590 void snd_interval_mulkdiv(const snd_interval_t *a, unsigned int k,
591 		      const snd_interval_t *b, snd_interval_t *c)
592 {
593 	unsigned int r;
594 	if (a->empty || b->empty) {
595 		snd_interval_none(c);
596 		return;
597 	}
598 	c->empty = 0;
599 	c->min = muldiv32(a->min, k, b->max, &r);
600 	c->openmin = (r || a->openmin || b->openmax);
601 	if (b->min > 0) {
602 		c->max = muldiv32(a->max, k, b->min, &r);
603 		if (r) {
604 			c->max++;
605 			c->openmax = 1;
606 		} else
607 			c->openmax = (a->openmax || b->openmin);
608 	} else {
609 		c->max = UINT_MAX;
610 		c->openmax = 0;
611 	}
612 	c->integer = 0;
613 }
614 
615 #undef assert
616 /* ---- */
617 
618 
619 /**
620  * snd_interval_ratnum - refine the interval value
621  *
622  * Returns non-zero if the value is changed, zero if not changed.
623  */
624 int snd_interval_ratnum(snd_interval_t *i,
625 		    unsigned int rats_count, ratnum_t *rats,
626 		    unsigned int *nump, unsigned int *denp)
627 {
628 	unsigned int best_num, best_diff, best_den;
629 	unsigned int k;
630 	snd_interval_t t;
631 	int err;
632 
633 	best_num = best_den = best_diff = 0;
634 	for (k = 0; k < rats_count; ++k) {
635 		unsigned int num = rats[k].num;
636 		unsigned int den;
637 		unsigned int q = i->min;
638 		int diff;
639 		if (q == 0)
640 			q = 1;
641 		den = div_down(num, q);
642 		if (den < rats[k].den_min)
643 			continue;
644 		if (den > rats[k].den_max)
645 			den = rats[k].den_max;
646 		else {
647 			unsigned int r;
648 			r = (den - rats[k].den_min) % rats[k].den_step;
649 			if (r != 0)
650 				den -= r;
651 		}
652 		diff = num - q * den;
653 		if (best_num == 0 ||
654 		    diff * best_den < best_diff * den) {
655 			best_diff = diff;
656 			best_den = den;
657 			best_num = num;
658 		}
659 	}
660 	if (best_den == 0) {
661 		i->empty = 1;
662 		return -EINVAL;
663 	}
664 	t.min = div_down(best_num, best_den);
665 	t.openmin = !!(best_num % best_den);
666 
667 	best_num = best_den = best_diff = 0;
668 	for (k = 0; k < rats_count; ++k) {
669 		unsigned int num = rats[k].num;
670 		unsigned int den;
671 		unsigned int q = i->max;
672 		int diff;
673 		if (q == 0) {
674 			i->empty = 1;
675 			return -EINVAL;
676 		}
677 		den = div_up(num, q);
678 		if (den > rats[k].den_max)
679 			continue;
680 		if (den < rats[k].den_min)
681 			den = rats[k].den_min;
682 		else {
683 			unsigned int r;
684 			r = (den - rats[k].den_min) % rats[k].den_step;
685 			if (r != 0)
686 				den += rats[k].den_step - r;
687 		}
688 		diff = q * den - num;
689 		if (best_num == 0 ||
690 		    diff * best_den < best_diff * den) {
691 			best_diff = diff;
692 			best_den = den;
693 			best_num = num;
694 		}
695 	}
696 	if (best_den == 0) {
697 		i->empty = 1;
698 		return -EINVAL;
699 	}
700 	t.max = div_up(best_num, best_den);
701 	t.openmax = !!(best_num % best_den);
702 	t.integer = 0;
703 	err = snd_interval_refine(i, &t);
704 	if (err < 0)
705 		return err;
706 
707 	if (snd_interval_single(i)) {
708 		if (nump)
709 			*nump = best_num;
710 		if (denp)
711 			*denp = best_den;
712 	}
713 	return err;
714 }
715 
716 /**
717  * snd_interval_ratden - refine the interval value
718  *
719  * Returns non-zero if the value is changed, zero if not changed.
720  */
721 static int snd_interval_ratden(snd_interval_t *i,
722 			       unsigned int rats_count, ratden_t *rats,
723 			       unsigned int *nump, unsigned int *denp)
724 {
725 	unsigned int best_num, best_diff, best_den;
726 	unsigned int k;
727 	snd_interval_t t;
728 	int err;
729 
730 	best_num = best_den = best_diff = 0;
731 	for (k = 0; k < rats_count; ++k) {
732 		unsigned int num;
733 		unsigned int den = rats[k].den;
734 		unsigned int q = i->min;
735 		int diff;
736 		num = mul(q, den);
737 		if (num > rats[k].num_max)
738 			continue;
739 		if (num < rats[k].num_min)
740 			num = rats[k].num_max;
741 		else {
742 			unsigned int r;
743 			r = (num - rats[k].num_min) % rats[k].num_step;
744 			if (r != 0)
745 				num += rats[k].num_step - r;
746 		}
747 		diff = num - q * den;
748 		if (best_num == 0 ||
749 		    diff * best_den < best_diff * den) {
750 			best_diff = diff;
751 			best_den = den;
752 			best_num = num;
753 		}
754 	}
755 	if (best_den == 0) {
756 		i->empty = 1;
757 		return -EINVAL;
758 	}
759 	t.min = div_down(best_num, best_den);
760 	t.openmin = !!(best_num % best_den);
761 
762 	best_num = best_den = best_diff = 0;
763 	for (k = 0; k < rats_count; ++k) {
764 		unsigned int num;
765 		unsigned int den = rats[k].den;
766 		unsigned int q = i->max;
767 		int diff;
768 		num = mul(q, den);
769 		if (num < rats[k].num_min)
770 			continue;
771 		if (num > rats[k].num_max)
772 			num = rats[k].num_max;
773 		else {
774 			unsigned int r;
775 			r = (num - rats[k].num_min) % rats[k].num_step;
776 			if (r != 0)
777 				num -= r;
778 		}
779 		diff = q * den - num;
780 		if (best_num == 0 ||
781 		    diff * best_den < best_diff * den) {
782 			best_diff = diff;
783 			best_den = den;
784 			best_num = num;
785 		}
786 	}
787 	if (best_den == 0) {
788 		i->empty = 1;
789 		return -EINVAL;
790 	}
791 	t.max = div_up(best_num, best_den);
792 	t.openmax = !!(best_num % best_den);
793 	t.integer = 0;
794 	err = snd_interval_refine(i, &t);
795 	if (err < 0)
796 		return err;
797 
798 	if (snd_interval_single(i)) {
799 		if (nump)
800 			*nump = best_num;
801 		if (denp)
802 			*denp = best_den;
803 	}
804 	return err;
805 }
806 
807 /**
808  * snd_interval_list - refine the interval value from the list
809  * @i: the interval value to refine
810  * @count: the number of elements in the list
811  * @list: the value list
812  * @mask: the bit-mask to evaluate
813  *
814  * Refines the interval value from the list.
815  * When mask is non-zero, only the elements corresponding to bit 1 are
816  * evaluated.
817  *
818  * Returns non-zero if the value is changed, zero if not changed.
819  */
820 int snd_interval_list(snd_interval_t *i, unsigned int count, unsigned int *list, unsigned int mask)
821 {
822         unsigned int k;
823 	int changed = 0;
824         for (k = 0; k < count; k++) {
825 		if (mask && !(mask & (1 << k)))
826 			continue;
827                 if (i->min == list[k] && !i->openmin)
828                         goto _l1;
829                 if (i->min < list[k]) {
830                         i->min = list[k];
831 			i->openmin = 0;
832 			changed = 1;
833                         goto _l1;
834                 }
835         }
836         i->empty = 1;
837         return -EINVAL;
838  _l1:
839         for (k = count; k-- > 0;) {
840 		if (mask && !(mask & (1 << k)))
841 			continue;
842                 if (i->max == list[k] && !i->openmax)
843                         goto _l2;
844                 if (i->max > list[k]) {
845                         i->max = list[k];
846 			i->openmax = 0;
847 			changed = 1;
848                         goto _l2;
849                 }
850         }
851         i->empty = 1;
852         return -EINVAL;
853  _l2:
854 	if (snd_interval_checkempty(i)) {
855 		i->empty = 1;
856 		return -EINVAL;
857 	}
858         return changed;
859 }
860 
861 static int snd_interval_step(snd_interval_t *i, unsigned int min, unsigned int step)
862 {
863 	unsigned int n;
864 	int changed = 0;
865 	n = (i->min - min) % step;
866 	if (n != 0 || i->openmin) {
867 		i->min += step - n;
868 		changed = 1;
869 	}
870 	n = (i->max - min) % step;
871 	if (n != 0 || i->openmax) {
872 		i->max -= n;
873 		changed = 1;
874 	}
875 	if (snd_interval_checkempty(i)) {
876 		i->empty = 1;
877 		return -EINVAL;
878 	}
879 	return changed;
880 }
881 
882 /* Info constraints helpers */
883 
884 /**
885  * snd_pcm_hw_rule_add - add the hw-constraint rule
886  * @runtime: the pcm runtime instance
887  * @cond: condition bits
888  * @var: the variable to evaluate
889  * @func: the evaluation function
890  * @private: the private data pointer passed to function
891  * @dep: the dependent variables
892  *
893  * Returns zero if successful, or a negative error code on failure.
894  */
895 int snd_pcm_hw_rule_add(snd_pcm_runtime_t *runtime, unsigned int cond,
896 			int var,
897 			snd_pcm_hw_rule_func_t func, void *private,
898 			int dep, ...)
899 {
900 	snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
901 	snd_pcm_hw_rule_t *c;
902 	unsigned int k;
903 	va_list args;
904 	va_start(args, dep);
905 	if (constrs->rules_num >= constrs->rules_all) {
906 		snd_pcm_hw_rule_t *new;
907 		unsigned int new_rules = constrs->rules_all + 16;
908 		new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
909 		if (!new)
910 			return -ENOMEM;
911 		if (constrs->rules) {
912 			memcpy(new, constrs->rules,
913 			       constrs->rules_num * sizeof(*c));
914 			kfree(constrs->rules);
915 		}
916 		constrs->rules = new;
917 		constrs->rules_all = new_rules;
918 	}
919 	c = &constrs->rules[constrs->rules_num];
920 	c->cond = cond;
921 	c->func = func;
922 	c->var = var;
923 	c->private = private;
924 	k = 0;
925 	while (1) {
926 		snd_assert(k < ARRAY_SIZE(c->deps), return -EINVAL);
927 		c->deps[k++] = dep;
928 		if (dep < 0)
929 			break;
930 		dep = va_arg(args, int);
931 	}
932 	constrs->rules_num++;
933 	va_end(args);
934 	return 0;
935 }
936 
937 /**
938  * snd_pcm_hw_constraint_mask
939  */
940 int snd_pcm_hw_constraint_mask(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
941 			       u_int32_t mask)
942 {
943 	snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
944 	snd_mask_t *maskp = constrs_mask(constrs, var);
945 	*maskp->bits &= mask;
946 	memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
947 	if (*maskp->bits == 0)
948 		return -EINVAL;
949 	return 0;
950 }
951 
952 /**
953  * snd_pcm_hw_constraint_mask64
954  */
955 int snd_pcm_hw_constraint_mask64(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
956 				 u_int64_t mask)
957 {
958 	snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
959 	snd_mask_t *maskp = constrs_mask(constrs, var);
960 	maskp->bits[0] &= (u_int32_t)mask;
961 	maskp->bits[1] &= (u_int32_t)(mask >> 32);
962 	memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
963 	if (! maskp->bits[0] && ! maskp->bits[1])
964 		return -EINVAL;
965 	return 0;
966 }
967 
968 /**
969  * snd_pcm_hw_constraint_integer
970  */
971 int snd_pcm_hw_constraint_integer(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var)
972 {
973 	snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
974 	return snd_interval_setinteger(constrs_interval(constrs, var));
975 }
976 
977 /**
978  * snd_pcm_hw_constraint_minmax
979  */
980 int snd_pcm_hw_constraint_minmax(snd_pcm_runtime_t *runtime, snd_pcm_hw_param_t var,
981 				 unsigned int min, unsigned int max)
982 {
983 	snd_pcm_hw_constraints_t *constrs = &runtime->hw_constraints;
984 	snd_interval_t t;
985 	t.min = min;
986 	t.max = max;
987 	t.openmin = t.openmax = 0;
988 	t.integer = 0;
989 	return snd_interval_refine(constrs_interval(constrs, var), &t);
990 }
991 
992 static int snd_pcm_hw_rule_list(snd_pcm_hw_params_t *params,
993 				snd_pcm_hw_rule_t *rule)
994 {
995 	snd_pcm_hw_constraint_list_t *list = rule->private;
996 	return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
997 }
998 
999 
1000 /**
1001  * snd_pcm_hw_constraint_list
1002  */
1003 int snd_pcm_hw_constraint_list(snd_pcm_runtime_t *runtime,
1004 			       unsigned int cond,
1005 			       snd_pcm_hw_param_t var,
1006 			       snd_pcm_hw_constraint_list_t *l)
1007 {
1008 	return snd_pcm_hw_rule_add(runtime, cond, var,
1009 				   snd_pcm_hw_rule_list, l,
1010 				   var, -1);
1011 }
1012 
1013 static int snd_pcm_hw_rule_ratnums(snd_pcm_hw_params_t *params,
1014 				   snd_pcm_hw_rule_t *rule)
1015 {
1016 	snd_pcm_hw_constraint_ratnums_t *r = rule->private;
1017 	unsigned int num = 0, den = 0;
1018 	int err;
1019 	err = snd_interval_ratnum(hw_param_interval(params, rule->var),
1020 				  r->nrats, r->rats, &num, &den);
1021 	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1022 		params->rate_num = num;
1023 		params->rate_den = den;
1024 	}
1025 	return err;
1026 }
1027 
1028 /**
1029  * snd_pcm_hw_constraint_ratnums
1030  */
1031 int snd_pcm_hw_constraint_ratnums(snd_pcm_runtime_t *runtime,
1032 				  unsigned int cond,
1033 				  snd_pcm_hw_param_t var,
1034 				  snd_pcm_hw_constraint_ratnums_t *r)
1035 {
1036 	return snd_pcm_hw_rule_add(runtime, cond, var,
1037 				   snd_pcm_hw_rule_ratnums, r,
1038 				   var, -1);
1039 }
1040 
1041 static int snd_pcm_hw_rule_ratdens(snd_pcm_hw_params_t *params,
1042 				   snd_pcm_hw_rule_t *rule)
1043 {
1044 	snd_pcm_hw_constraint_ratdens_t *r = rule->private;
1045 	unsigned int num = 0, den = 0;
1046 	int err = snd_interval_ratden(hw_param_interval(params, rule->var),
1047 				  r->nrats, r->rats, &num, &den);
1048 	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
1049 		params->rate_num = num;
1050 		params->rate_den = den;
1051 	}
1052 	return err;
1053 }
1054 
1055 /**
1056  * snd_pcm_hw_constraint_ratdens
1057  */
1058 int snd_pcm_hw_constraint_ratdens(snd_pcm_runtime_t *runtime,
1059 				  unsigned int cond,
1060 				  snd_pcm_hw_param_t var,
1061 				  snd_pcm_hw_constraint_ratdens_t *r)
1062 {
1063 	return snd_pcm_hw_rule_add(runtime, cond, var,
1064 				   snd_pcm_hw_rule_ratdens, r,
1065 				   var, -1);
1066 }
1067 
1068 static int snd_pcm_hw_rule_msbits(snd_pcm_hw_params_t *params,
1069 				  snd_pcm_hw_rule_t *rule)
1070 {
1071 	unsigned int l = (unsigned long) rule->private;
1072 	int width = l & 0xffff;
1073 	unsigned int msbits = l >> 16;
1074 	snd_interval_t *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
1075 	if (snd_interval_single(i) && snd_interval_value(i) == width)
1076 		params->msbits = msbits;
1077 	return 0;
1078 }
1079 
1080 /**
1081  * snd_pcm_hw_constraint_msbits
1082  */
1083 int snd_pcm_hw_constraint_msbits(snd_pcm_runtime_t *runtime,
1084 				 unsigned int cond,
1085 				 unsigned int width,
1086 				 unsigned int msbits)
1087 {
1088 	unsigned long l = (msbits << 16) | width;
1089 	return snd_pcm_hw_rule_add(runtime, cond, -1,
1090 				    snd_pcm_hw_rule_msbits,
1091 				    (void*) l,
1092 				    SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
1093 }
1094 
1095 static int snd_pcm_hw_rule_step(snd_pcm_hw_params_t *params,
1096 				snd_pcm_hw_rule_t *rule)
1097 {
1098 	unsigned long step = (unsigned long) rule->private;
1099 	return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
1100 }
1101 
1102 /**
1103  * snd_pcm_hw_constraint_step
1104  */
1105 int snd_pcm_hw_constraint_step(snd_pcm_runtime_t *runtime,
1106 			       unsigned int cond,
1107 			       snd_pcm_hw_param_t var,
1108 			       unsigned long step)
1109 {
1110 	return snd_pcm_hw_rule_add(runtime, cond, var,
1111 				   snd_pcm_hw_rule_step, (void *) step,
1112 				   var, -1);
1113 }
1114 
1115 static int snd_pcm_hw_rule_pow2(snd_pcm_hw_params_t *params, snd_pcm_hw_rule_t *rule)
1116 {
1117 	static int pow2_sizes[] = {
1118 		1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
1119 		1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
1120 		1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
1121 		1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
1122 	};
1123 	return snd_interval_list(hw_param_interval(params, rule->var),
1124 				 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
1125 }
1126 
1127 /**
1128  * snd_pcm_hw_constraint_pow2
1129  */
1130 int snd_pcm_hw_constraint_pow2(snd_pcm_runtime_t *runtime,
1131 			       unsigned int cond,
1132 			       snd_pcm_hw_param_t var)
1133 {
1134 	return snd_pcm_hw_rule_add(runtime, cond, var,
1135 				   snd_pcm_hw_rule_pow2, NULL,
1136 				   var, -1);
1137 }
1138 
1139 /* To use the same code we have in alsa-lib */
1140 #define snd_pcm_t snd_pcm_substream_t
1141 #define assert(i) snd_assert((i), return -EINVAL)
1142 #ifndef INT_MIN
1143 #define INT_MIN ((int)((unsigned int)INT_MAX+1))
1144 #endif
1145 
1146 static void _snd_pcm_hw_param_any(snd_pcm_hw_params_t *params,
1147 				  snd_pcm_hw_param_t var)
1148 {
1149 	if (hw_is_mask(var)) {
1150 		snd_mask_any(hw_param_mask(params, var));
1151 		params->cmask |= 1 << var;
1152 		params->rmask |= 1 << var;
1153 		return;
1154 	}
1155 	if (hw_is_interval(var)) {
1156 		snd_interval_any(hw_param_interval(params, var));
1157 		params->cmask |= 1 << var;
1158 		params->rmask |= 1 << var;
1159 		return;
1160 	}
1161 	snd_BUG();
1162 }
1163 
1164 #if 0
1165 /**
1166  * snd_pcm_hw_param_any
1167  */
1168 int snd_pcm_hw_param_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1169 			 snd_pcm_hw_param_t var)
1170 {
1171 	_snd_pcm_hw_param_any(params, var);
1172 	return snd_pcm_hw_refine(pcm, params);
1173 }
1174 #endif  /*  0  */
1175 
1176 void _snd_pcm_hw_params_any(snd_pcm_hw_params_t *params)
1177 {
1178 	unsigned int k;
1179 	memset(params, 0, sizeof(*params));
1180 	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
1181 		_snd_pcm_hw_param_any(params, k);
1182 	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
1183 		_snd_pcm_hw_param_any(params, k);
1184 	params->info = ~0U;
1185 }
1186 
1187 #if 0
1188 /**
1189  * snd_pcm_hw_params_any
1190  *
1191  * Fill PARAMS with full configuration space boundaries
1192  */
1193 int snd_pcm_hw_params_any(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
1194 {
1195 	_snd_pcm_hw_params_any(params);
1196 	return snd_pcm_hw_refine(pcm, params);
1197 }
1198 #endif  /*  0  */
1199 
1200 /**
1201  * snd_pcm_hw_param_value
1202  *
1203  * Return the value for field PAR if it's fixed in configuration space
1204  *  defined by PARAMS. Return -EINVAL otherwise
1205  */
1206 static int snd_pcm_hw_param_value(const snd_pcm_hw_params_t *params,
1207 				  snd_pcm_hw_param_t var, int *dir)
1208 {
1209 	if (hw_is_mask(var)) {
1210 		const snd_mask_t *mask = hw_param_mask_c(params, var);
1211 		if (!snd_mask_single(mask))
1212 			return -EINVAL;
1213 		if (dir)
1214 			*dir = 0;
1215 		return snd_mask_value(mask);
1216 	}
1217 	if (hw_is_interval(var)) {
1218 		const snd_interval_t *i = hw_param_interval_c(params, var);
1219 		if (!snd_interval_single(i))
1220 			return -EINVAL;
1221 		if (dir)
1222 			*dir = i->openmin;
1223 		return snd_interval_value(i);
1224 	}
1225 	assert(0);
1226 	return -EINVAL;
1227 }
1228 
1229 /**
1230  * snd_pcm_hw_param_value_min
1231  *
1232  * Return the minimum value for field PAR.
1233  */
1234 unsigned int snd_pcm_hw_param_value_min(const snd_pcm_hw_params_t *params,
1235 					snd_pcm_hw_param_t var, int *dir)
1236 {
1237 	if (hw_is_mask(var)) {
1238 		if (dir)
1239 			*dir = 0;
1240 		return snd_mask_min(hw_param_mask_c(params, var));
1241 	}
1242 	if (hw_is_interval(var)) {
1243 		const snd_interval_t *i = hw_param_interval_c(params, var);
1244 		if (dir)
1245 			*dir = i->openmin;
1246 		return snd_interval_min(i);
1247 	}
1248 	assert(0);
1249 	return -EINVAL;
1250 }
1251 
1252 /**
1253  * snd_pcm_hw_param_value_max
1254  *
1255  * Return the maximum value for field PAR.
1256  */
1257 unsigned int snd_pcm_hw_param_value_max(const snd_pcm_hw_params_t *params,
1258 					snd_pcm_hw_param_t var, int *dir)
1259 {
1260 	if (hw_is_mask(var)) {
1261 		if (dir)
1262 			*dir = 0;
1263 		return snd_mask_max(hw_param_mask_c(params, var));
1264 	}
1265 	if (hw_is_interval(var)) {
1266 		const snd_interval_t *i = hw_param_interval_c(params, var);
1267 		if (dir)
1268 			*dir = - (int) i->openmax;
1269 		return snd_interval_max(i);
1270 	}
1271 	assert(0);
1272 	return -EINVAL;
1273 }
1274 
1275 void _snd_pcm_hw_param_setempty(snd_pcm_hw_params_t *params,
1276 				snd_pcm_hw_param_t var)
1277 {
1278 	if (hw_is_mask(var)) {
1279 		snd_mask_none(hw_param_mask(params, var));
1280 		params->cmask |= 1 << var;
1281 		params->rmask |= 1 << var;
1282 	} else if (hw_is_interval(var)) {
1283 		snd_interval_none(hw_param_interval(params, var));
1284 		params->cmask |= 1 << var;
1285 		params->rmask |= 1 << var;
1286 	} else {
1287 		snd_BUG();
1288 	}
1289 }
1290 
1291 int _snd_pcm_hw_param_setinteger(snd_pcm_hw_params_t *params,
1292 				 snd_pcm_hw_param_t var)
1293 {
1294 	int changed;
1295 	assert(hw_is_interval(var));
1296 	changed = snd_interval_setinteger(hw_param_interval(params, var));
1297 	if (changed) {
1298 		params->cmask |= 1 << var;
1299 		params->rmask |= 1 << var;
1300 	}
1301 	return changed;
1302 }
1303 
1304 #if 0
1305 /**
1306  * snd_pcm_hw_param_setinteger
1307  *
1308  * Inside configuration space defined by PARAMS remove from PAR all
1309  * non integer values. Reduce configuration space accordingly.
1310  * Return -EINVAL if the configuration space is empty
1311  */
1312 int snd_pcm_hw_param_setinteger(snd_pcm_t *pcm,
1313 				snd_pcm_hw_params_t *params,
1314 				snd_pcm_hw_param_t var)
1315 {
1316 	int changed = _snd_pcm_hw_param_setinteger(params, var);
1317 	if (changed < 0)
1318 		return changed;
1319 	if (params->rmask) {
1320 		int err = snd_pcm_hw_refine(pcm, params);
1321 		if (err < 0)
1322 			return err;
1323 	}
1324 	return 0;
1325 }
1326 #endif  /*  0  */
1327 
1328 static int _snd_pcm_hw_param_first(snd_pcm_hw_params_t *params,
1329 				   snd_pcm_hw_param_t var)
1330 {
1331 	int changed;
1332 	if (hw_is_mask(var))
1333 		changed = snd_mask_refine_first(hw_param_mask(params, var));
1334 	else if (hw_is_interval(var))
1335 		changed = snd_interval_refine_first(hw_param_interval(params, var));
1336 	else {
1337 		assert(0);
1338 		return -EINVAL;
1339 	}
1340 	if (changed) {
1341 		params->cmask |= 1 << var;
1342 		params->rmask |= 1 << var;
1343 	}
1344 	return changed;
1345 }
1346 
1347 
1348 /**
1349  * snd_pcm_hw_param_first
1350  *
1351  * Inside configuration space defined by PARAMS remove from PAR all
1352  * values > minimum. Reduce configuration space accordingly.
1353  * Return the minimum.
1354  */
1355 static int snd_pcm_hw_param_first(snd_pcm_t *pcm,
1356 				  snd_pcm_hw_params_t *params,
1357 				  snd_pcm_hw_param_t var, int *dir)
1358 {
1359 	int changed = _snd_pcm_hw_param_first(params, var);
1360 	if (changed < 0)
1361 		return changed;
1362 	if (params->rmask) {
1363 		int err = snd_pcm_hw_refine(pcm, params);
1364 		assert(err >= 0);
1365 	}
1366 	return snd_pcm_hw_param_value(params, var, dir);
1367 }
1368 
1369 static int _snd_pcm_hw_param_last(snd_pcm_hw_params_t *params,
1370 				  snd_pcm_hw_param_t var)
1371 {
1372 	int changed;
1373 	if (hw_is_mask(var))
1374 		changed = snd_mask_refine_last(hw_param_mask(params, var));
1375 	else if (hw_is_interval(var))
1376 		changed = snd_interval_refine_last(hw_param_interval(params, var));
1377 	else {
1378 		assert(0);
1379 		return -EINVAL;
1380 	}
1381 	if (changed) {
1382 		params->cmask |= 1 << var;
1383 		params->rmask |= 1 << var;
1384 	}
1385 	return changed;
1386 }
1387 
1388 
1389 /**
1390  * snd_pcm_hw_param_last
1391  *
1392  * Inside configuration space defined by PARAMS remove from PAR all
1393  * values < maximum. Reduce configuration space accordingly.
1394  * Return the maximum.
1395  */
1396 static int snd_pcm_hw_param_last(snd_pcm_t *pcm,
1397 				 snd_pcm_hw_params_t *params,
1398 				 snd_pcm_hw_param_t var, int *dir)
1399 {
1400 	int changed = _snd_pcm_hw_param_last(params, var);
1401 	if (changed < 0)
1402 		return changed;
1403 	if (params->rmask) {
1404 		int err = snd_pcm_hw_refine(pcm, params);
1405 		assert(err >= 0);
1406 	}
1407 	return snd_pcm_hw_param_value(params, var, dir);
1408 }
1409 
1410 int _snd_pcm_hw_param_min(snd_pcm_hw_params_t *params,
1411 			  snd_pcm_hw_param_t var, unsigned int val, int dir)
1412 {
1413 	int changed;
1414 	int open = 0;
1415 	if (dir) {
1416 		if (dir > 0) {
1417 			open = 1;
1418 		} else if (dir < 0) {
1419 			if (val > 0) {
1420 				open = 1;
1421 				val--;
1422 			}
1423 		}
1424 	}
1425 	if (hw_is_mask(var))
1426 		changed = snd_mask_refine_min(hw_param_mask(params, var), val + !!open);
1427 	else if (hw_is_interval(var))
1428 		changed = snd_interval_refine_min(hw_param_interval(params, var), val, open);
1429 	else {
1430 		assert(0);
1431 		return -EINVAL;
1432 	}
1433 	if (changed) {
1434 		params->cmask |= 1 << var;
1435 		params->rmask |= 1 << var;
1436 	}
1437 	return changed;
1438 }
1439 
1440 /**
1441  * snd_pcm_hw_param_min
1442  *
1443  * Inside configuration space defined by PARAMS remove from PAR all
1444  * values < VAL. Reduce configuration space accordingly.
1445  * Return new minimum or -EINVAL if the configuration space is empty
1446  */
1447 static int snd_pcm_hw_param_min(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1448 				snd_pcm_hw_param_t var, unsigned int val,
1449 				int *dir)
1450 {
1451 	int changed = _snd_pcm_hw_param_min(params, var, val, dir ? *dir : 0);
1452 	if (changed < 0)
1453 		return changed;
1454 	if (params->rmask) {
1455 		int err = snd_pcm_hw_refine(pcm, params);
1456 		if (err < 0)
1457 			return err;
1458 	}
1459 	return snd_pcm_hw_param_value_min(params, var, dir);
1460 }
1461 
1462 static int _snd_pcm_hw_param_max(snd_pcm_hw_params_t *params,
1463 				 snd_pcm_hw_param_t var, unsigned int val,
1464 				 int dir)
1465 {
1466 	int changed;
1467 	int open = 0;
1468 	if (dir) {
1469 		if (dir < 0) {
1470 			open = 1;
1471 		} else if (dir > 0) {
1472 			open = 1;
1473 			val++;
1474 		}
1475 	}
1476 	if (hw_is_mask(var)) {
1477 		if (val == 0 && open) {
1478 			snd_mask_none(hw_param_mask(params, var));
1479 			changed = -EINVAL;
1480 		} else
1481 			changed = snd_mask_refine_max(hw_param_mask(params, var), val - !!open);
1482 	} else if (hw_is_interval(var))
1483 		changed = snd_interval_refine_max(hw_param_interval(params, var), val, open);
1484 	else {
1485 		assert(0);
1486 		return -EINVAL;
1487 	}
1488 	if (changed) {
1489 		params->cmask |= 1 << var;
1490 		params->rmask |= 1 << var;
1491 	}
1492 	return changed;
1493 }
1494 
1495 /**
1496  * snd_pcm_hw_param_max
1497  *
1498  * Inside configuration space defined by PARAMS remove from PAR all
1499  *  values >= VAL + 1. Reduce configuration space accordingly.
1500  *  Return new maximum or -EINVAL if the configuration space is empty
1501  */
1502 static int snd_pcm_hw_param_max(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1503 				snd_pcm_hw_param_t var, unsigned int val,
1504 				int *dir)
1505 {
1506 	int changed = _snd_pcm_hw_param_max(params, var, val, dir ? *dir : 0);
1507 	if (changed < 0)
1508 		return changed;
1509 	if (params->rmask) {
1510 		int err = snd_pcm_hw_refine(pcm, params);
1511 		if (err < 0)
1512 			return err;
1513 	}
1514 	return snd_pcm_hw_param_value_max(params, var, dir);
1515 }
1516 
1517 int _snd_pcm_hw_param_set(snd_pcm_hw_params_t *params,
1518 			  snd_pcm_hw_param_t var, unsigned int val, int dir)
1519 {
1520 	int changed;
1521 	if (hw_is_mask(var)) {
1522 		snd_mask_t *m = hw_param_mask(params, var);
1523 		if (val == 0 && dir < 0) {
1524 			changed = -EINVAL;
1525 			snd_mask_none(m);
1526 		} else {
1527 			if (dir > 0)
1528 				val++;
1529 			else if (dir < 0)
1530 				val--;
1531 			changed = snd_mask_refine_set(hw_param_mask(params, var), val);
1532 		}
1533 	} else if (hw_is_interval(var)) {
1534 		snd_interval_t *i = hw_param_interval(params, var);
1535 		if (val == 0 && dir < 0) {
1536 			changed = -EINVAL;
1537 			snd_interval_none(i);
1538 		} else if (dir == 0)
1539 			changed = snd_interval_refine_set(i, val);
1540 		else {
1541 			snd_interval_t t;
1542 			t.openmin = 1;
1543 			t.openmax = 1;
1544 			t.empty = 0;
1545 			t.integer = 0;
1546 			if (dir < 0) {
1547 				t.min = val - 1;
1548 				t.max = val;
1549 			} else {
1550 				t.min = val;
1551 				t.max = val+1;
1552 			}
1553 			changed = snd_interval_refine(i, &t);
1554 		}
1555 	} else {
1556 		assert(0);
1557 		return -EINVAL;
1558 	}
1559 	if (changed) {
1560 		params->cmask |= 1 << var;
1561 		params->rmask |= 1 << var;
1562 	}
1563 	return changed;
1564 }
1565 
1566 /**
1567  * snd_pcm_hw_param_set
1568  *
1569  * Inside configuration space defined by PARAMS remove from PAR all
1570  * values != VAL. Reduce configuration space accordingly.
1571  *  Return VAL or -EINVAL if the configuration space is empty
1572  */
1573 int snd_pcm_hw_param_set(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1574 			 snd_pcm_hw_param_t var, unsigned int val, int dir)
1575 {
1576 	int changed = _snd_pcm_hw_param_set(params, var, val, dir);
1577 	if (changed < 0)
1578 		return changed;
1579 	if (params->rmask) {
1580 		int err = snd_pcm_hw_refine(pcm, params);
1581 		if (err < 0)
1582 			return err;
1583 	}
1584 	return snd_pcm_hw_param_value(params, var, NULL);
1585 }
1586 
1587 static int _snd_pcm_hw_param_mask(snd_pcm_hw_params_t *params,
1588 				  snd_pcm_hw_param_t var, const snd_mask_t *val)
1589 {
1590 	int changed;
1591 	assert(hw_is_mask(var));
1592 	changed = snd_mask_refine(hw_param_mask(params, var), val);
1593 	if (changed) {
1594 		params->cmask |= 1 << var;
1595 		params->rmask |= 1 << var;
1596 	}
1597 	return changed;
1598 }
1599 
1600 /**
1601  * snd_pcm_hw_param_mask
1602  *
1603  * Inside configuration space defined by PARAMS remove from PAR all values
1604  * not contained in MASK. Reduce configuration space accordingly.
1605  * This function can be called only for SNDRV_PCM_HW_PARAM_ACCESS,
1606  * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1607  * Return 0 on success or -EINVAL
1608  * if the configuration space is empty
1609  */
1610 int snd_pcm_hw_param_mask(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1611 			  snd_pcm_hw_param_t var, const snd_mask_t *val)
1612 {
1613 	int changed = _snd_pcm_hw_param_mask(params, var, val);
1614 	if (changed < 0)
1615 		return changed;
1616 	if (params->rmask) {
1617 		int err = snd_pcm_hw_refine(pcm, params);
1618 		if (err < 0)
1619 			return err;
1620 	}
1621 	return 0;
1622 }
1623 
1624 static int boundary_sub(int a, int adir,
1625 			int b, int bdir,
1626 			int *c, int *cdir)
1627 {
1628 	adir = adir < 0 ? -1 : (adir > 0 ? 1 : 0);
1629 	bdir = bdir < 0 ? -1 : (bdir > 0 ? 1 : 0);
1630 	*c = a - b;
1631 	*cdir = adir - bdir;
1632 	if (*cdir == -2) {
1633 		assert(*c > INT_MIN);
1634 		(*c)--;
1635 	} else if (*cdir == 2) {
1636 		assert(*c < INT_MAX);
1637 		(*c)++;
1638 	}
1639 	return 0;
1640 }
1641 
1642 static int boundary_lt(unsigned int a, int adir,
1643 		       unsigned int b, int bdir)
1644 {
1645 	assert(a > 0 || adir >= 0);
1646 	assert(b > 0 || bdir >= 0);
1647 	if (adir < 0) {
1648 		a--;
1649 		adir = 1;
1650 	} else if (adir > 0)
1651 		adir = 1;
1652 	if (bdir < 0) {
1653 		b--;
1654 		bdir = 1;
1655 	} else if (bdir > 0)
1656 		bdir = 1;
1657 	return a < b || (a == b && adir < bdir);
1658 }
1659 
1660 /* Return 1 if min is nearer to best than max */
1661 static int boundary_nearer(int min, int mindir,
1662 			   int best, int bestdir,
1663 			   int max, int maxdir)
1664 {
1665 	int dmin, dmindir;
1666 	int dmax, dmaxdir;
1667 	boundary_sub(best, bestdir, min, mindir, &dmin, &dmindir);
1668 	boundary_sub(max, maxdir, best, bestdir, &dmax, &dmaxdir);
1669 	return boundary_lt(dmin, dmindir, dmax, dmaxdir);
1670 }
1671 
1672 /**
1673  * snd_pcm_hw_param_near
1674  *
1675  * Inside configuration space defined by PARAMS set PAR to the available value
1676  * nearest to VAL. Reduce configuration space accordingly.
1677  * This function cannot be called for SNDRV_PCM_HW_PARAM_ACCESS,
1678  * SNDRV_PCM_HW_PARAM_FORMAT, SNDRV_PCM_HW_PARAM_SUBFORMAT.
1679  * Return the value found.
1680   */
1681 int snd_pcm_hw_param_near(snd_pcm_t *pcm, snd_pcm_hw_params_t *params,
1682 			  snd_pcm_hw_param_t var, unsigned int best, int *dir)
1683 {
1684 	snd_pcm_hw_params_t *save = NULL;
1685 	int v;
1686 	unsigned int saved_min;
1687 	int last = 0;
1688 	int min, max;
1689 	int mindir, maxdir;
1690 	int valdir = dir ? *dir : 0;
1691 	/* FIXME */
1692 	if (best > INT_MAX)
1693 		best = INT_MAX;
1694 	min = max = best;
1695 	mindir = maxdir = valdir;
1696 	if (maxdir > 0)
1697 		maxdir = 0;
1698 	else if (maxdir == 0)
1699 		maxdir = -1;
1700 	else {
1701 		maxdir = 1;
1702 		max--;
1703 	}
1704 	save = kmalloc(sizeof(*save), GFP_KERNEL);
1705 	if (save == NULL)
1706 		return -ENOMEM;
1707 	*save = *params;
1708 	saved_min = min;
1709 	min = snd_pcm_hw_param_min(pcm, params, var, min, &mindir);
1710 	if (min >= 0) {
1711 		snd_pcm_hw_params_t *params1;
1712 		if (max < 0)
1713 			goto _end;
1714 		if ((unsigned int)min == saved_min && mindir == valdir)
1715 			goto _end;
1716 		params1 = kmalloc(sizeof(*params1), GFP_KERNEL);
1717 		if (params1 == NULL) {
1718 			kfree(save);
1719 			return -ENOMEM;
1720 		}
1721 		*params1 = *save;
1722 		max = snd_pcm_hw_param_max(pcm, params1, var, max, &maxdir);
1723 		if (max < 0) {
1724 			kfree(params1);
1725 			goto _end;
1726 		}
1727 		if (boundary_nearer(max, maxdir, best, valdir, min, mindir)) {
1728 			*params = *params1;
1729 			last = 1;
1730 		}
1731 		kfree(params1);
1732 	} else {
1733 		*params = *save;
1734 		max = snd_pcm_hw_param_max(pcm, params, var, max, &maxdir);
1735 		assert(max >= 0);
1736 		last = 1;
1737 	}
1738  _end:
1739  	kfree(save);
1740 	if (last)
1741 		v = snd_pcm_hw_param_last(pcm, params, var, dir);
1742 	else
1743 		v = snd_pcm_hw_param_first(pcm, params, var, dir);
1744 	assert(v >= 0);
1745 	return v;
1746 }
1747 
1748 /**
1749  * snd_pcm_hw_param_choose
1750  *
1751  * Choose one configuration from configuration space defined by PARAMS
1752  * The configuration chosen is that obtained fixing in this order:
1753  * first access, first format, first subformat, min channels,
1754  * min rate, min period time, max buffer size, min tick time
1755  */
1756 int snd_pcm_hw_params_choose(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
1757 {
1758 	int err;
1759 
1760 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_ACCESS, NULL);
1761 	assert(err >= 0);
1762 
1763 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_FORMAT, NULL);
1764 	assert(err >= 0);
1765 
1766 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_SUBFORMAT, NULL);
1767 	assert(err >= 0);
1768 
1769 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_CHANNELS, NULL);
1770 	assert(err >= 0);
1771 
1772 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_RATE, NULL);
1773 	assert(err >= 0);
1774 
1775 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_PERIOD_TIME, NULL);
1776 	assert(err >= 0);
1777 
1778 	err = snd_pcm_hw_param_last(pcm, params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL);
1779 	assert(err >= 0);
1780 
1781 	err = snd_pcm_hw_param_first(pcm, params, SNDRV_PCM_HW_PARAM_TICK_TIME, NULL);
1782 	assert(err >= 0);
1783 
1784 	return 0;
1785 }
1786 
1787 #undef snd_pcm_t
1788 #undef assert
1789 
1790 static int snd_pcm_lib_ioctl_reset(snd_pcm_substream_t *substream,
1791 				   void *arg)
1792 {
1793 	snd_pcm_runtime_t *runtime = substream->runtime;
1794 	unsigned long flags;
1795 	snd_pcm_stream_lock_irqsave(substream, flags);
1796 	if (snd_pcm_running(substream) &&
1797 	    snd_pcm_update_hw_ptr(substream) >= 0)
1798 		runtime->status->hw_ptr %= runtime->buffer_size;
1799 	else
1800 		runtime->status->hw_ptr = 0;
1801 	snd_pcm_stream_unlock_irqrestore(substream, flags);
1802 	return 0;
1803 }
1804 
1805 static int snd_pcm_lib_ioctl_channel_info(snd_pcm_substream_t *substream,
1806 					  void *arg)
1807 {
1808 	snd_pcm_channel_info_t *info = arg;
1809 	snd_pcm_runtime_t *runtime = substream->runtime;
1810 	int width;
1811 	if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
1812 		info->offset = -1;
1813 		return 0;
1814 	}
1815 	width = snd_pcm_format_physical_width(runtime->format);
1816 	if (width < 0)
1817 		return width;
1818 	info->offset = 0;
1819 	switch (runtime->access) {
1820 	case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
1821 	case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
1822 		info->first = info->channel * width;
1823 		info->step = runtime->channels * width;
1824 		break;
1825 	case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
1826 	case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
1827 	{
1828 		size_t size = runtime->dma_bytes / runtime->channels;
1829 		info->first = info->channel * size * 8;
1830 		info->step = width;
1831 		break;
1832 	}
1833 	default:
1834 		snd_BUG();
1835 		break;
1836 	}
1837 	return 0;
1838 }
1839 
1840 /**
1841  * snd_pcm_lib_ioctl - a generic PCM ioctl callback
1842  * @substream: the pcm substream instance
1843  * @cmd: ioctl command
1844  * @arg: ioctl argument
1845  *
1846  * Processes the generic ioctl commands for PCM.
1847  * Can be passed as the ioctl callback for PCM ops.
1848  *
1849  * Returns zero if successful, or a negative error code on failure.
1850  */
1851 int snd_pcm_lib_ioctl(snd_pcm_substream_t *substream,
1852 		      unsigned int cmd, void *arg)
1853 {
1854 	switch (cmd) {
1855 	case SNDRV_PCM_IOCTL1_INFO:
1856 		return 0;
1857 	case SNDRV_PCM_IOCTL1_RESET:
1858 		return snd_pcm_lib_ioctl_reset(substream, arg);
1859 	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
1860 		return snd_pcm_lib_ioctl_channel_info(substream, arg);
1861 	}
1862 	return -ENXIO;
1863 }
1864 
1865 /*
1866  *  Conditions
1867  */
1868 
1869 static void snd_pcm_system_tick_set(snd_pcm_substream_t *substream,
1870 				    unsigned long ticks)
1871 {
1872 	snd_pcm_runtime_t *runtime = substream->runtime;
1873 	if (ticks == 0)
1874 		del_timer(&runtime->tick_timer);
1875 	else {
1876 		ticks += (1000000 / HZ) - 1;
1877 		ticks /= (1000000 / HZ);
1878 		mod_timer(&runtime->tick_timer, jiffies + ticks);
1879 	}
1880 }
1881 
1882 /* Temporary alias */
1883 void snd_pcm_tick_set(snd_pcm_substream_t *substream, unsigned long ticks)
1884 {
1885 	snd_pcm_system_tick_set(substream, ticks);
1886 }
1887 
1888 void snd_pcm_tick_prepare(snd_pcm_substream_t *substream)
1889 {
1890 	snd_pcm_runtime_t *runtime = substream->runtime;
1891 	snd_pcm_uframes_t frames = ULONG_MAX;
1892 	snd_pcm_uframes_t avail, dist;
1893 	unsigned int ticks;
1894 	u_int64_t n;
1895 	u_int32_t r;
1896 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1897 		if (runtime->silence_size >= runtime->boundary) {
1898 			frames = 1;
1899 		} else if (runtime->silence_size > 0 &&
1900 			   runtime->silence_filled < runtime->buffer_size) {
1901 			snd_pcm_sframes_t noise_dist;
1902 			noise_dist = snd_pcm_playback_hw_avail(runtime) + runtime->silence_filled;
1903 			snd_assert(noise_dist <= (snd_pcm_sframes_t)runtime->silence_threshold, );
1904 			frames = noise_dist - runtime->silence_threshold;
1905 		}
1906 		avail = snd_pcm_playback_avail(runtime);
1907 	} else {
1908 		avail = snd_pcm_capture_avail(runtime);
1909 	}
1910 	if (avail < runtime->control->avail_min) {
1911 		snd_pcm_sframes_t n = runtime->control->avail_min - avail;
1912 		if (n > 0 && frames > (snd_pcm_uframes_t)n)
1913 			frames = n;
1914 	}
1915 	if (avail < runtime->buffer_size) {
1916 		snd_pcm_sframes_t n = runtime->buffer_size - avail;
1917 		if (n > 0 && frames > (snd_pcm_uframes_t)n)
1918 			frames = n;
1919 	}
1920 	if (frames == ULONG_MAX) {
1921 		snd_pcm_tick_set(substream, 0);
1922 		return;
1923 	}
1924 	dist = runtime->status->hw_ptr - runtime->hw_ptr_base;
1925 	/* Distance to next interrupt */
1926 	dist = runtime->period_size - dist % runtime->period_size;
1927 	if (dist <= frames) {
1928 		snd_pcm_tick_set(substream, 0);
1929 		return;
1930 	}
1931 	/* the base time is us */
1932 	n = frames;
1933 	n *= 1000000;
1934 	div64_32(&n, runtime->tick_time * runtime->rate, &r);
1935 	ticks = n + (r > 0 ? 1 : 0);
1936 	if (ticks < runtime->sleep_min)
1937 		ticks = runtime->sleep_min;
1938 	snd_pcm_tick_set(substream, (unsigned long) ticks);
1939 }
1940 
1941 void snd_pcm_tick_elapsed(snd_pcm_substream_t *substream)
1942 {
1943 	snd_pcm_runtime_t *runtime;
1944 	unsigned long flags;
1945 
1946 	snd_assert(substream != NULL, return);
1947 	runtime = substream->runtime;
1948 	snd_assert(runtime != NULL, return);
1949 
1950 	snd_pcm_stream_lock_irqsave(substream, flags);
1951 	if (!snd_pcm_running(substream) ||
1952 	    snd_pcm_update_hw_ptr(substream) < 0)
1953 		goto _end;
1954 	if (runtime->sleep_min)
1955 		snd_pcm_tick_prepare(substream);
1956  _end:
1957 	snd_pcm_stream_unlock_irqrestore(substream, flags);
1958 }
1959 
1960 /**
1961  * snd_pcm_period_elapsed - update the pcm status for the next period
1962  * @substream: the pcm substream instance
1963  *
1964  * This function is called from the interrupt handler when the
1965  * PCM has processed the period size.  It will update the current
1966  * pointer, set up the tick, wake up sleepers, etc.
1967  *
1968  * Even if more than one periods have elapsed since the last call, you
1969  * have to call this only once.
1970  */
1971 void snd_pcm_period_elapsed(snd_pcm_substream_t *substream)
1972 {
1973 	snd_pcm_runtime_t *runtime;
1974 	unsigned long flags;
1975 
1976 	snd_assert(substream != NULL, return);
1977 	runtime = substream->runtime;
1978 	snd_assert(runtime != NULL, return);
1979 
1980 	if (runtime->transfer_ack_begin)
1981 		runtime->transfer_ack_begin(substream);
1982 
1983 	snd_pcm_stream_lock_irqsave(substream, flags);
1984 	if (!snd_pcm_running(substream) ||
1985 	    snd_pcm_update_hw_ptr_interrupt(substream) < 0)
1986 		goto _end;
1987 
1988 	if (substream->timer_running)
1989 		snd_timer_interrupt(substream->timer, 1);
1990 	if (runtime->sleep_min)
1991 		snd_pcm_tick_prepare(substream);
1992  _end:
1993 	snd_pcm_stream_unlock_irqrestore(substream, flags);
1994 	if (runtime->transfer_ack_end)
1995 		runtime->transfer_ack_end(substream);
1996 	kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
1997 }
1998 
1999 static int snd_pcm_lib_write_transfer(snd_pcm_substream_t *substream,
2000 				      unsigned int hwoff,
2001 				      unsigned long data, unsigned int off,
2002 				      snd_pcm_uframes_t frames)
2003 {
2004 	snd_pcm_runtime_t *runtime = substream->runtime;
2005 	int err;
2006 	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2007 	if (substream->ops->copy) {
2008 		if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2009 			return err;
2010 	} else {
2011 		char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2012 		snd_assert(runtime->dma_area, return -EFAULT);
2013 		if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
2014 			return -EFAULT;
2015 	}
2016 	return 0;
2017 }
2018 
2019 typedef int (*transfer_f)(snd_pcm_substream_t *substream, unsigned int hwoff,
2020 			  unsigned long data, unsigned int off,
2021 			  snd_pcm_uframes_t size);
2022 
2023 static snd_pcm_sframes_t snd_pcm_lib_write1(snd_pcm_substream_t *substream,
2024 					    unsigned long data,
2025 					    snd_pcm_uframes_t size,
2026 					    int nonblock,
2027 					    transfer_f transfer)
2028 {
2029 	snd_pcm_runtime_t *runtime = substream->runtime;
2030 	snd_pcm_uframes_t xfer = 0;
2031 	snd_pcm_uframes_t offset = 0;
2032 	int err = 0;
2033 
2034 	if (size == 0)
2035 		return 0;
2036 	if (size > runtime->xfer_align)
2037 		size -= size % runtime->xfer_align;
2038 
2039 	snd_pcm_stream_lock_irq(substream);
2040 	switch (runtime->status->state) {
2041 	case SNDRV_PCM_STATE_PREPARED:
2042 	case SNDRV_PCM_STATE_RUNNING:
2043 	case SNDRV_PCM_STATE_PAUSED:
2044 		break;
2045 	case SNDRV_PCM_STATE_XRUN:
2046 		err = -EPIPE;
2047 		goto _end_unlock;
2048 	case SNDRV_PCM_STATE_SUSPENDED:
2049 		err = -ESTRPIPE;
2050 		goto _end_unlock;
2051 	default:
2052 		err = -EBADFD;
2053 		goto _end_unlock;
2054 	}
2055 
2056 	while (size > 0) {
2057 		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2058 		snd_pcm_uframes_t avail;
2059 		snd_pcm_uframes_t cont;
2060 		if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2061 			snd_pcm_update_hw_ptr(substream);
2062 		avail = snd_pcm_playback_avail(runtime);
2063 		if (((avail < runtime->control->avail_min && size > avail) ||
2064 		   (size >= runtime->xfer_align && avail < runtime->xfer_align))) {
2065 			wait_queue_t wait;
2066 			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
2067 			long tout;
2068 
2069 			if (nonblock) {
2070 				err = -EAGAIN;
2071 				goto _end_unlock;
2072 			}
2073 
2074 			init_waitqueue_entry(&wait, current);
2075 			add_wait_queue(&runtime->sleep, &wait);
2076 			while (1) {
2077 				if (signal_pending(current)) {
2078 					state = SIGNALED;
2079 					break;
2080 				}
2081 				set_current_state(TASK_INTERRUPTIBLE);
2082 				snd_pcm_stream_unlock_irq(substream);
2083 				tout = schedule_timeout(10 * HZ);
2084 				snd_pcm_stream_lock_irq(substream);
2085 				if (tout == 0) {
2086 					if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2087 					    runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2088 						state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2089 						break;
2090 					}
2091 				}
2092 				switch (runtime->status->state) {
2093 				case SNDRV_PCM_STATE_XRUN:
2094 				case SNDRV_PCM_STATE_DRAINING:
2095 					state = ERROR;
2096 					goto _end_loop;
2097 				case SNDRV_PCM_STATE_SUSPENDED:
2098 					state = SUSPENDED;
2099 					goto _end_loop;
2100 				case SNDRV_PCM_STATE_SETUP:
2101 					state = DROPPED;
2102 					goto _end_loop;
2103 				default:
2104 					break;
2105 				}
2106 				avail = snd_pcm_playback_avail(runtime);
2107 				if (avail >= runtime->control->avail_min) {
2108 					state = READY;
2109 					break;
2110 				}
2111 			}
2112 		       _end_loop:
2113 			remove_wait_queue(&runtime->sleep, &wait);
2114 
2115 			switch (state) {
2116 			case ERROR:
2117 				err = -EPIPE;
2118 				goto _end_unlock;
2119 			case SUSPENDED:
2120 				err = -ESTRPIPE;
2121 				goto _end_unlock;
2122 			case SIGNALED:
2123 				err = -ERESTARTSYS;
2124 				goto _end_unlock;
2125 			case EXPIRED:
2126 				snd_printd("playback write error (DMA or IRQ trouble?)\n");
2127 				err = -EIO;
2128 				goto _end_unlock;
2129 			case DROPPED:
2130 				err = -EBADFD;
2131 				goto _end_unlock;
2132 			default:
2133 				break;
2134 			}
2135 		}
2136 		if (avail > runtime->xfer_align)
2137 			avail -= avail % runtime->xfer_align;
2138 		frames = size > avail ? avail : size;
2139 		cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2140 		if (frames > cont)
2141 			frames = cont;
2142 		snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2143 		appl_ptr = runtime->control->appl_ptr;
2144 		appl_ofs = appl_ptr % runtime->buffer_size;
2145 		snd_pcm_stream_unlock_irq(substream);
2146 		if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2147 			goto _end;
2148 		snd_pcm_stream_lock_irq(substream);
2149 		switch (runtime->status->state) {
2150 		case SNDRV_PCM_STATE_XRUN:
2151 			err = -EPIPE;
2152 			goto _end_unlock;
2153 		case SNDRV_PCM_STATE_SUSPENDED:
2154 			err = -ESTRPIPE;
2155 			goto _end_unlock;
2156 		default:
2157 			break;
2158 		}
2159 		appl_ptr += frames;
2160 		if (appl_ptr >= runtime->boundary)
2161 			appl_ptr -= runtime->boundary;
2162 		runtime->control->appl_ptr = appl_ptr;
2163 		if (substream->ops->ack)
2164 			substream->ops->ack(substream);
2165 
2166 		offset += frames;
2167 		size -= frames;
2168 		xfer += frames;
2169 		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
2170 		    snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
2171 			err = snd_pcm_start(substream);
2172 			if (err < 0)
2173 				goto _end_unlock;
2174 		}
2175 		if (runtime->sleep_min &&
2176 		    runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2177 			snd_pcm_tick_prepare(substream);
2178 	}
2179  _end_unlock:
2180 	snd_pcm_stream_unlock_irq(substream);
2181  _end:
2182 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2183 }
2184 
2185 snd_pcm_sframes_t snd_pcm_lib_write(snd_pcm_substream_t *substream, const void __user *buf, snd_pcm_uframes_t size)
2186 {
2187 	snd_pcm_runtime_t *runtime;
2188 	int nonblock;
2189 
2190 	snd_assert(substream != NULL, return -ENXIO);
2191 	runtime = substream->runtime;
2192 	snd_assert(runtime != NULL, return -ENXIO);
2193 	snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2194 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2195 		return -EBADFD;
2196 
2197 	snd_assert(substream->ffile != NULL, return -ENXIO);
2198 	nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2199 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2200 	if (substream->oss.oss) {
2201 		snd_pcm_oss_setup_t *setup = substream->oss.setup;
2202 		if (setup != NULL) {
2203 			if (setup->nonblock)
2204 				nonblock = 1;
2205 			else if (setup->block)
2206 				nonblock = 0;
2207 		}
2208 	}
2209 #endif
2210 
2211 	if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
2212 	    runtime->channels > 1)
2213 		return -EINVAL;
2214 	return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
2215 				  snd_pcm_lib_write_transfer);
2216 }
2217 
2218 static int snd_pcm_lib_writev_transfer(snd_pcm_substream_t *substream,
2219 				       unsigned int hwoff,
2220 				       unsigned long data, unsigned int off,
2221 				       snd_pcm_uframes_t frames)
2222 {
2223 	snd_pcm_runtime_t *runtime = substream->runtime;
2224 	int err;
2225 	void __user **bufs = (void __user **)data;
2226 	int channels = runtime->channels;
2227 	int c;
2228 	if (substream->ops->copy) {
2229 		snd_assert(substream->ops->silence != NULL, return -EINVAL);
2230 		for (c = 0; c < channels; ++c, ++bufs) {
2231 			if (*bufs == NULL) {
2232 				if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
2233 					return err;
2234 			} else {
2235 				char __user *buf = *bufs + samples_to_bytes(runtime, off);
2236 				if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2237 					return err;
2238 			}
2239 		}
2240 	} else {
2241 		/* default transfer behaviour */
2242 		size_t dma_csize = runtime->dma_bytes / channels;
2243 		snd_assert(runtime->dma_area, return -EFAULT);
2244 		for (c = 0; c < channels; ++c, ++bufs) {
2245 			char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2246 			if (*bufs == NULL) {
2247 				snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
2248 			} else {
2249 				char __user *buf = *bufs + samples_to_bytes(runtime, off);
2250 				if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
2251 					return -EFAULT;
2252 			}
2253 		}
2254 	}
2255 	return 0;
2256 }
2257 
2258 snd_pcm_sframes_t snd_pcm_lib_writev(snd_pcm_substream_t *substream,
2259 				     void __user **bufs,
2260 				     snd_pcm_uframes_t frames)
2261 {
2262 	snd_pcm_runtime_t *runtime;
2263 	int nonblock;
2264 
2265 	snd_assert(substream != NULL, return -ENXIO);
2266 	runtime = substream->runtime;
2267 	snd_assert(runtime != NULL, return -ENXIO);
2268 	snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2269 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2270 		return -EBADFD;
2271 
2272 	snd_assert(substream->ffile != NULL, return -ENXIO);
2273 	nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2274 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2275 	if (substream->oss.oss) {
2276 		snd_pcm_oss_setup_t *setup = substream->oss.setup;
2277 		if (setup != NULL) {
2278 			if (setup->nonblock)
2279 				nonblock = 1;
2280 			else if (setup->block)
2281 				nonblock = 0;
2282 		}
2283 	}
2284 #endif
2285 
2286 	if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2287 		return -EINVAL;
2288 	return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
2289 				  nonblock, snd_pcm_lib_writev_transfer);
2290 }
2291 
2292 static int snd_pcm_lib_read_transfer(snd_pcm_substream_t *substream,
2293 				     unsigned int hwoff,
2294 				     unsigned long data, unsigned int off,
2295 				     snd_pcm_uframes_t frames)
2296 {
2297 	snd_pcm_runtime_t *runtime = substream->runtime;
2298 	int err;
2299 	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
2300 	if (substream->ops->copy) {
2301 		if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
2302 			return err;
2303 	} else {
2304 		char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
2305 		snd_assert(runtime->dma_area, return -EFAULT);
2306 		if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
2307 			return -EFAULT;
2308 	}
2309 	return 0;
2310 }
2311 
2312 static snd_pcm_sframes_t snd_pcm_lib_read1(snd_pcm_substream_t *substream,
2313 					   unsigned long data,
2314 					   snd_pcm_uframes_t size,
2315 					   int nonblock,
2316 					   transfer_f transfer)
2317 {
2318 	snd_pcm_runtime_t *runtime = substream->runtime;
2319 	snd_pcm_uframes_t xfer = 0;
2320 	snd_pcm_uframes_t offset = 0;
2321 	int err = 0;
2322 
2323 	if (size == 0)
2324 		return 0;
2325 	if (size > runtime->xfer_align)
2326 		size -= size % runtime->xfer_align;
2327 
2328 	snd_pcm_stream_lock_irq(substream);
2329 	switch (runtime->status->state) {
2330 	case SNDRV_PCM_STATE_PREPARED:
2331 		if (size >= runtime->start_threshold) {
2332 			err = snd_pcm_start(substream);
2333 			if (err < 0)
2334 				goto _end_unlock;
2335 		}
2336 		break;
2337 	case SNDRV_PCM_STATE_DRAINING:
2338 	case SNDRV_PCM_STATE_RUNNING:
2339 	case SNDRV_PCM_STATE_PAUSED:
2340 		break;
2341 	case SNDRV_PCM_STATE_XRUN:
2342 		err = -EPIPE;
2343 		goto _end_unlock;
2344 	case SNDRV_PCM_STATE_SUSPENDED:
2345 		err = -ESTRPIPE;
2346 		goto _end_unlock;
2347 	default:
2348 		err = -EBADFD;
2349 		goto _end_unlock;
2350 	}
2351 
2352 	while (size > 0) {
2353 		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
2354 		snd_pcm_uframes_t avail;
2355 		snd_pcm_uframes_t cont;
2356 		if (runtime->sleep_min == 0 && runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2357 			snd_pcm_update_hw_ptr(substream);
2358 	      __draining:
2359 		avail = snd_pcm_capture_avail(runtime);
2360 		if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
2361 			if (avail < runtime->xfer_align) {
2362 				err = -EPIPE;
2363 				goto _end_unlock;
2364 			}
2365 		} else if ((avail < runtime->control->avail_min && size > avail) ||
2366 			   (size >= runtime->xfer_align && avail < runtime->xfer_align)) {
2367 			wait_queue_t wait;
2368 			enum { READY, SIGNALED, ERROR, SUSPENDED, EXPIRED, DROPPED } state;
2369 			long tout;
2370 
2371 			if (nonblock) {
2372 				err = -EAGAIN;
2373 				goto _end_unlock;
2374 			}
2375 
2376 			init_waitqueue_entry(&wait, current);
2377 			add_wait_queue(&runtime->sleep, &wait);
2378 			while (1) {
2379 				if (signal_pending(current)) {
2380 					state = SIGNALED;
2381 					break;
2382 				}
2383 				set_current_state(TASK_INTERRUPTIBLE);
2384 				snd_pcm_stream_unlock_irq(substream);
2385 				tout = schedule_timeout(10 * HZ);
2386 				snd_pcm_stream_lock_irq(substream);
2387 				if (tout == 0) {
2388 					if (runtime->status->state != SNDRV_PCM_STATE_PREPARED &&
2389 					    runtime->status->state != SNDRV_PCM_STATE_PAUSED) {
2390 						state = runtime->status->state == SNDRV_PCM_STATE_SUSPENDED ? SUSPENDED : EXPIRED;
2391 						break;
2392 					}
2393 				}
2394 				switch (runtime->status->state) {
2395 				case SNDRV_PCM_STATE_XRUN:
2396 					state = ERROR;
2397 					goto _end_loop;
2398 				case SNDRV_PCM_STATE_SUSPENDED:
2399 					state = SUSPENDED;
2400 					goto _end_loop;
2401 				case SNDRV_PCM_STATE_DRAINING:
2402 					goto __draining;
2403 				case SNDRV_PCM_STATE_SETUP:
2404 					state = DROPPED;
2405 					goto _end_loop;
2406 				default:
2407 					break;
2408 				}
2409 				avail = snd_pcm_capture_avail(runtime);
2410 				if (avail >= runtime->control->avail_min) {
2411 					state = READY;
2412 					break;
2413 				}
2414 			}
2415 		       _end_loop:
2416 			remove_wait_queue(&runtime->sleep, &wait);
2417 
2418 			switch (state) {
2419 			case ERROR:
2420 				err = -EPIPE;
2421 				goto _end_unlock;
2422 			case SUSPENDED:
2423 				err = -ESTRPIPE;
2424 				goto _end_unlock;
2425 			case SIGNALED:
2426 				err = -ERESTARTSYS;
2427 				goto _end_unlock;
2428 			case EXPIRED:
2429 				snd_printd("capture read error (DMA or IRQ trouble?)\n");
2430 				err = -EIO;
2431 				goto _end_unlock;
2432 			case DROPPED:
2433 				err = -EBADFD;
2434 				goto _end_unlock;
2435 			default:
2436 				break;
2437 			}
2438 		}
2439 		if (avail > runtime->xfer_align)
2440 			avail -= avail % runtime->xfer_align;
2441 		frames = size > avail ? avail : size;
2442 		cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
2443 		if (frames > cont)
2444 			frames = cont;
2445 		snd_assert(frames != 0, snd_pcm_stream_unlock_irq(substream); return -EINVAL);
2446 		appl_ptr = runtime->control->appl_ptr;
2447 		appl_ofs = appl_ptr % runtime->buffer_size;
2448 		snd_pcm_stream_unlock_irq(substream);
2449 		if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
2450 			goto _end;
2451 		snd_pcm_stream_lock_irq(substream);
2452 		switch (runtime->status->state) {
2453 		case SNDRV_PCM_STATE_XRUN:
2454 			err = -EPIPE;
2455 			goto _end_unlock;
2456 		case SNDRV_PCM_STATE_SUSPENDED:
2457 			err = -ESTRPIPE;
2458 			goto _end_unlock;
2459 		default:
2460 			break;
2461 		}
2462 		appl_ptr += frames;
2463 		if (appl_ptr >= runtime->boundary)
2464 			appl_ptr -= runtime->boundary;
2465 		runtime->control->appl_ptr = appl_ptr;
2466 		if (substream->ops->ack)
2467 			substream->ops->ack(substream);
2468 
2469 		offset += frames;
2470 		size -= frames;
2471 		xfer += frames;
2472 		if (runtime->sleep_min &&
2473 		    runtime->status->state == SNDRV_PCM_STATE_RUNNING)
2474 			snd_pcm_tick_prepare(substream);
2475 	}
2476  _end_unlock:
2477 	snd_pcm_stream_unlock_irq(substream);
2478  _end:
2479 	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
2480 }
2481 
2482 snd_pcm_sframes_t snd_pcm_lib_read(snd_pcm_substream_t *substream, void __user *buf, snd_pcm_uframes_t size)
2483 {
2484 	snd_pcm_runtime_t *runtime;
2485 	int nonblock;
2486 
2487 	snd_assert(substream != NULL, return -ENXIO);
2488 	runtime = substream->runtime;
2489 	snd_assert(runtime != NULL, return -ENXIO);
2490 	snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2491 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2492 		return -EBADFD;
2493 
2494 	snd_assert(substream->ffile != NULL, return -ENXIO);
2495 	nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2496 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2497 	if (substream->oss.oss) {
2498 		snd_pcm_oss_setup_t *setup = substream->oss.setup;
2499 		if (setup != NULL) {
2500 			if (setup->nonblock)
2501 				nonblock = 1;
2502 			else if (setup->block)
2503 				nonblock = 0;
2504 		}
2505 	}
2506 #endif
2507 	if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
2508 		return -EINVAL;
2509 	return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
2510 }
2511 
2512 static int snd_pcm_lib_readv_transfer(snd_pcm_substream_t *substream,
2513 				      unsigned int hwoff,
2514 				      unsigned long data, unsigned int off,
2515 				      snd_pcm_uframes_t frames)
2516 {
2517 	snd_pcm_runtime_t *runtime = substream->runtime;
2518 	int err;
2519 	void __user **bufs = (void __user **)data;
2520 	int channels = runtime->channels;
2521 	int c;
2522 	if (substream->ops->copy) {
2523 		for (c = 0; c < channels; ++c, ++bufs) {
2524 			char __user *buf;
2525 			if (*bufs == NULL)
2526 				continue;
2527 			buf = *bufs + samples_to_bytes(runtime, off);
2528 			if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
2529 				return err;
2530 		}
2531 	} else {
2532 		snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
2533 		snd_assert(runtime->dma_area, return -EFAULT);
2534 		for (c = 0; c < channels; ++c, ++bufs) {
2535 			char *hwbuf;
2536 			char __user *buf;
2537 			if (*bufs == NULL)
2538 				continue;
2539 
2540 			hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
2541 			buf = *bufs + samples_to_bytes(runtime, off);
2542 			if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
2543 				return -EFAULT;
2544 		}
2545 	}
2546 	return 0;
2547 }
2548 
2549 snd_pcm_sframes_t snd_pcm_lib_readv(snd_pcm_substream_t *substream,
2550 				    void __user **bufs,
2551 				    snd_pcm_uframes_t frames)
2552 {
2553 	snd_pcm_runtime_t *runtime;
2554 	int nonblock;
2555 
2556 	snd_assert(substream != NULL, return -ENXIO);
2557 	runtime = substream->runtime;
2558 	snd_assert(runtime != NULL, return -ENXIO);
2559 	snd_assert(substream->ops->copy != NULL || runtime->dma_area != NULL, return -EINVAL);
2560 	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
2561 		return -EBADFD;
2562 
2563 	snd_assert(substream->ffile != NULL, return -ENXIO);
2564 	nonblock = !!(substream->ffile->f_flags & O_NONBLOCK);
2565 #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2566 	if (substream->oss.oss) {
2567 		snd_pcm_oss_setup_t *setup = substream->oss.setup;
2568 		if (setup != NULL) {
2569 			if (setup->nonblock)
2570 				nonblock = 1;
2571 			else if (setup->block)
2572 				nonblock = 0;
2573 		}
2574 	}
2575 #endif
2576 
2577 	if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
2578 		return -EINVAL;
2579 	return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
2580 }
2581 
2582 /*
2583  *  Exported symbols
2584  */
2585 
2586 EXPORT_SYMBOL(snd_interval_refine);
2587 EXPORT_SYMBOL(snd_interval_list);
2588 EXPORT_SYMBOL(snd_interval_ratnum);
2589 EXPORT_SYMBOL(_snd_pcm_hw_params_any);
2590 EXPORT_SYMBOL(_snd_pcm_hw_param_min);
2591 EXPORT_SYMBOL(_snd_pcm_hw_param_set);
2592 EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
2593 EXPORT_SYMBOL(_snd_pcm_hw_param_setinteger);
2594 EXPORT_SYMBOL(snd_pcm_hw_param_value_min);
2595 EXPORT_SYMBOL(snd_pcm_hw_param_value_max);
2596 EXPORT_SYMBOL(snd_pcm_hw_param_mask);
2597 EXPORT_SYMBOL(snd_pcm_hw_param_first);
2598 EXPORT_SYMBOL(snd_pcm_hw_param_last);
2599 EXPORT_SYMBOL(snd_pcm_hw_param_near);
2600 EXPORT_SYMBOL(snd_pcm_hw_param_set);
2601 EXPORT_SYMBOL(snd_pcm_hw_refine);
2602 EXPORT_SYMBOL(snd_pcm_hw_constraints_init);
2603 EXPORT_SYMBOL(snd_pcm_hw_constraints_complete);
2604 EXPORT_SYMBOL(snd_pcm_hw_constraint_list);
2605 EXPORT_SYMBOL(snd_pcm_hw_constraint_step);
2606 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);
2607 EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);
2608 EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);
2609 EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);
2610 EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);
2611 EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);
2612 EXPORT_SYMBOL(snd_pcm_hw_rule_add);
2613 EXPORT_SYMBOL(snd_pcm_set_ops);
2614 EXPORT_SYMBOL(snd_pcm_set_sync);
2615 EXPORT_SYMBOL(snd_pcm_lib_ioctl);
2616 EXPORT_SYMBOL(snd_pcm_stop);
2617 EXPORT_SYMBOL(snd_pcm_period_elapsed);
2618 EXPORT_SYMBOL(snd_pcm_lib_write);
2619 EXPORT_SYMBOL(snd_pcm_lib_read);
2620 EXPORT_SYMBOL(snd_pcm_lib_writev);
2621 EXPORT_SYMBOL(snd_pcm_lib_readv);
2622 EXPORT_SYMBOL(snd_pcm_lib_buffer_bytes);
2623 EXPORT_SYMBOL(snd_pcm_lib_period_bytes);
2624 /* pcm_memory.c */
2625 EXPORT_SYMBOL(snd_pcm_lib_preallocate_free_for_all);
2626 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages);
2627 EXPORT_SYMBOL(snd_pcm_lib_preallocate_pages_for_all);
2628 EXPORT_SYMBOL(snd_pcm_sgbuf_ops_page);
2629 EXPORT_SYMBOL(snd_pcm_lib_malloc_pages);
2630 EXPORT_SYMBOL(snd_pcm_lib_free_pages);
2631