xref: /openbmc/linux/sound/core/oss/pcm_plugin.c (revision 3381df09)
1 /*
2  *  PCM Plug-In shared (kernel/library) code
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
4  *  Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
5  *
6  *
7  *   This library is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU Library General Public License as
9  *   published by the Free Software Foundation; either version 2 of
10  *   the License, or (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 Library General Public License for more details.
16  *
17  *   You should have received a copy of the GNU Library General Public
18  *   License along with this library; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  *
21  */
22 
23 #if 0
24 #define PLUGIN_DEBUG
25 #endif
26 
27 #include <linux/slab.h>
28 #include <linux/time.h>
29 #include <linux/vmalloc.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include "pcm_plugin.h"
34 
35 #define snd_pcm_plug_first(plug) ((plug)->runtime->oss.plugin_first)
36 #define snd_pcm_plug_last(plug) ((plug)->runtime->oss.plugin_last)
37 
38 /*
39  *  because some cards might have rates "very close", we ignore
40  *  all "resampling" requests within +-5%
41  */
42 static int rate_match(unsigned int src_rate, unsigned int dst_rate)
43 {
44 	unsigned int low = (src_rate * 95) / 100;
45 	unsigned int high = (src_rate * 105) / 100;
46 	return dst_rate >= low && dst_rate <= high;
47 }
48 
49 static int snd_pcm_plugin_alloc(struct snd_pcm_plugin *plugin, snd_pcm_uframes_t frames)
50 {
51 	struct snd_pcm_plugin_format *format;
52 	ssize_t width;
53 	size_t size;
54 	unsigned int channel;
55 	struct snd_pcm_plugin_channel *c;
56 
57 	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK) {
58 		format = &plugin->src_format;
59 	} else {
60 		format = &plugin->dst_format;
61 	}
62 	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
63 		return width;
64 	size = frames * format->channels * width;
65 	if (snd_BUG_ON(size % 8))
66 		return -ENXIO;
67 	size /= 8;
68 	if (plugin->buf_frames < frames) {
69 		kvfree(plugin->buf);
70 		plugin->buf = kvzalloc(size, GFP_KERNEL);
71 		plugin->buf_frames = frames;
72 	}
73 	if (!plugin->buf) {
74 		plugin->buf_frames = 0;
75 		return -ENOMEM;
76 	}
77 	c = plugin->buf_channels;
78 	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
79 		for (channel = 0; channel < format->channels; channel++, c++) {
80 			c->frames = frames;
81 			c->enabled = 1;
82 			c->wanted = 0;
83 			c->area.addr = plugin->buf;
84 			c->area.first = channel * width;
85 			c->area.step = format->channels * width;
86 		}
87 	} else if (plugin->access == SNDRV_PCM_ACCESS_RW_NONINTERLEAVED) {
88 		if (snd_BUG_ON(size % format->channels))
89 			return -EINVAL;
90 		size /= format->channels;
91 		for (channel = 0; channel < format->channels; channel++, c++) {
92 			c->frames = frames;
93 			c->enabled = 1;
94 			c->wanted = 0;
95 			c->area.addr = plugin->buf + (channel * size);
96 			c->area.first = 0;
97 			c->area.step = width;
98 		}
99 	} else
100 		return -EINVAL;
101 	return 0;
102 }
103 
104 int snd_pcm_plug_alloc(struct snd_pcm_substream *plug, snd_pcm_uframes_t frames)
105 {
106 	int err;
107 	if (snd_BUG_ON(!snd_pcm_plug_first(plug)))
108 		return -ENXIO;
109 	if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
110 		struct snd_pcm_plugin *plugin = snd_pcm_plug_first(plug);
111 		while (plugin->next) {
112 			if (plugin->dst_frames)
113 				frames = plugin->dst_frames(plugin, frames);
114 			if ((snd_pcm_sframes_t)frames <= 0)
115 				return -ENXIO;
116 			plugin = plugin->next;
117 			err = snd_pcm_plugin_alloc(plugin, frames);
118 			if (err < 0)
119 				return err;
120 		}
121 	} else {
122 		struct snd_pcm_plugin *plugin = snd_pcm_plug_last(plug);
123 		while (plugin->prev) {
124 			if (plugin->src_frames)
125 				frames = plugin->src_frames(plugin, frames);
126 			if ((snd_pcm_sframes_t)frames <= 0)
127 				return -ENXIO;
128 			plugin = plugin->prev;
129 			err = snd_pcm_plugin_alloc(plugin, frames);
130 			if (err < 0)
131 				return err;
132 		}
133 	}
134 	return 0;
135 }
136 
137 
138 snd_pcm_sframes_t snd_pcm_plugin_client_channels(struct snd_pcm_plugin *plugin,
139 				       snd_pcm_uframes_t frames,
140 				       struct snd_pcm_plugin_channel **channels)
141 {
142 	*channels = plugin->buf_channels;
143 	return frames;
144 }
145 
146 int snd_pcm_plugin_build(struct snd_pcm_substream *plug,
147 			 const char *name,
148 			 struct snd_pcm_plugin_format *src_format,
149 			 struct snd_pcm_plugin_format *dst_format,
150 			 size_t extra,
151 			 struct snd_pcm_plugin **ret)
152 {
153 	struct snd_pcm_plugin *plugin;
154 	unsigned int channels;
155 
156 	if (snd_BUG_ON(!plug))
157 		return -ENXIO;
158 	if (snd_BUG_ON(!src_format || !dst_format))
159 		return -ENXIO;
160 	plugin = kzalloc(sizeof(*plugin) + extra, GFP_KERNEL);
161 	if (plugin == NULL)
162 		return -ENOMEM;
163 	plugin->name = name;
164 	plugin->plug = plug;
165 	plugin->stream = snd_pcm_plug_stream(plug);
166 	plugin->access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
167 	plugin->src_format = *src_format;
168 	plugin->src_width = snd_pcm_format_physical_width(src_format->format);
169 	snd_BUG_ON(plugin->src_width <= 0);
170 	plugin->dst_format = *dst_format;
171 	plugin->dst_width = snd_pcm_format_physical_width(dst_format->format);
172 	snd_BUG_ON(plugin->dst_width <= 0);
173 	if (plugin->stream == SNDRV_PCM_STREAM_PLAYBACK)
174 		channels = src_format->channels;
175 	else
176 		channels = dst_format->channels;
177 	plugin->buf_channels = kcalloc(channels, sizeof(*plugin->buf_channels), GFP_KERNEL);
178 	if (plugin->buf_channels == NULL) {
179 		snd_pcm_plugin_free(plugin);
180 		return -ENOMEM;
181 	}
182 	plugin->client_channels = snd_pcm_plugin_client_channels;
183 	*ret = plugin;
184 	return 0;
185 }
186 
187 int snd_pcm_plugin_free(struct snd_pcm_plugin *plugin)
188 {
189 	if (! plugin)
190 		return 0;
191 	if (plugin->private_free)
192 		plugin->private_free(plugin);
193 	kfree(plugin->buf_channels);
194 	kvfree(plugin->buf);
195 	kfree(plugin);
196 	return 0;
197 }
198 
199 static snd_pcm_sframes_t calc_dst_frames(struct snd_pcm_substream *plug,
200 					 snd_pcm_sframes_t frames,
201 					 bool check_size)
202 {
203 	struct snd_pcm_plugin *plugin, *plugin_next;
204 
205 	plugin = snd_pcm_plug_first(plug);
206 	while (plugin && frames > 0) {
207 		plugin_next = plugin->next;
208 		if (plugin->dst_frames) {
209 			frames = plugin->dst_frames(plugin, frames);
210 			if (frames < 0)
211 				return frames;
212 		}
213 		if (check_size && frames > plugin->buf_frames)
214 			frames = plugin->buf_frames;
215 		plugin = plugin_next;
216 	}
217 	return frames;
218 }
219 
220 static snd_pcm_sframes_t calc_src_frames(struct snd_pcm_substream *plug,
221 					 snd_pcm_sframes_t frames,
222 					 bool check_size)
223 {
224 	struct snd_pcm_plugin *plugin, *plugin_prev;
225 
226 	plugin = snd_pcm_plug_last(plug);
227 	while (plugin && frames > 0) {
228 		if (check_size && frames > plugin->buf_frames)
229 			frames = plugin->buf_frames;
230 		plugin_prev = plugin->prev;
231 		if (plugin->src_frames) {
232 			frames = plugin->src_frames(plugin, frames);
233 			if (frames < 0)
234 				return frames;
235 		}
236 		plugin = plugin_prev;
237 	}
238 	return frames;
239 }
240 
241 snd_pcm_sframes_t snd_pcm_plug_client_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t drv_frames)
242 {
243 	if (snd_BUG_ON(!plug))
244 		return -ENXIO;
245 	switch (snd_pcm_plug_stream(plug)) {
246 	case SNDRV_PCM_STREAM_PLAYBACK:
247 		return calc_src_frames(plug, drv_frames, false);
248 	case SNDRV_PCM_STREAM_CAPTURE:
249 		return calc_dst_frames(plug, drv_frames, false);
250 	default:
251 		snd_BUG();
252 		return -EINVAL;
253 	}
254 }
255 
256 snd_pcm_sframes_t snd_pcm_plug_slave_size(struct snd_pcm_substream *plug, snd_pcm_uframes_t clt_frames)
257 {
258 	if (snd_BUG_ON(!plug))
259 		return -ENXIO;
260 	switch (snd_pcm_plug_stream(plug)) {
261 	case SNDRV_PCM_STREAM_PLAYBACK:
262 		return calc_dst_frames(plug, clt_frames, false);
263 	case SNDRV_PCM_STREAM_CAPTURE:
264 		return calc_src_frames(plug, clt_frames, false);
265 	default:
266 		snd_BUG();
267 		return -EINVAL;
268 	}
269 }
270 
271 static int snd_pcm_plug_formats(const struct snd_mask *mask,
272 				snd_pcm_format_t format)
273 {
274 	struct snd_mask formats = *mask;
275 	u64 linfmts = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
276 		       SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_S16_LE |
277 		       SNDRV_PCM_FMTBIT_U16_BE | SNDRV_PCM_FMTBIT_S16_BE |
278 		       SNDRV_PCM_FMTBIT_U24_LE | SNDRV_PCM_FMTBIT_S24_LE |
279 		       SNDRV_PCM_FMTBIT_U24_BE | SNDRV_PCM_FMTBIT_S24_BE |
280 		       SNDRV_PCM_FMTBIT_U24_3LE | SNDRV_PCM_FMTBIT_S24_3LE |
281 		       SNDRV_PCM_FMTBIT_U24_3BE | SNDRV_PCM_FMTBIT_S24_3BE |
282 		       SNDRV_PCM_FMTBIT_U32_LE | SNDRV_PCM_FMTBIT_S32_LE |
283 		       SNDRV_PCM_FMTBIT_U32_BE | SNDRV_PCM_FMTBIT_S32_BE);
284 	snd_mask_set(&formats, (__force int)SNDRV_PCM_FORMAT_MU_LAW);
285 
286 	if (formats.bits[0] & lower_32_bits(linfmts))
287 		formats.bits[0] |= lower_32_bits(linfmts);
288 	if (formats.bits[1] & upper_32_bits(linfmts))
289 		formats.bits[1] |= upper_32_bits(linfmts);
290 	return snd_mask_test(&formats, (__force int)format);
291 }
292 
293 static const snd_pcm_format_t preferred_formats[] = {
294 	SNDRV_PCM_FORMAT_S16_LE,
295 	SNDRV_PCM_FORMAT_S16_BE,
296 	SNDRV_PCM_FORMAT_U16_LE,
297 	SNDRV_PCM_FORMAT_U16_BE,
298 	SNDRV_PCM_FORMAT_S24_3LE,
299 	SNDRV_PCM_FORMAT_S24_3BE,
300 	SNDRV_PCM_FORMAT_U24_3LE,
301 	SNDRV_PCM_FORMAT_U24_3BE,
302 	SNDRV_PCM_FORMAT_S24_LE,
303 	SNDRV_PCM_FORMAT_S24_BE,
304 	SNDRV_PCM_FORMAT_U24_LE,
305 	SNDRV_PCM_FORMAT_U24_BE,
306 	SNDRV_PCM_FORMAT_S32_LE,
307 	SNDRV_PCM_FORMAT_S32_BE,
308 	SNDRV_PCM_FORMAT_U32_LE,
309 	SNDRV_PCM_FORMAT_U32_BE,
310 	SNDRV_PCM_FORMAT_S8,
311 	SNDRV_PCM_FORMAT_U8
312 };
313 
314 snd_pcm_format_t snd_pcm_plug_slave_format(snd_pcm_format_t format,
315 					   const struct snd_mask *format_mask)
316 {
317 	int i;
318 
319 	if (snd_mask_test(format_mask, (__force int)format))
320 		return format;
321 	if (!snd_pcm_plug_formats(format_mask, format))
322 		return (__force snd_pcm_format_t)-EINVAL;
323 	if (snd_pcm_format_linear(format)) {
324 		unsigned int width = snd_pcm_format_width(format);
325 		int unsignd = snd_pcm_format_unsigned(format) > 0;
326 		int big = snd_pcm_format_big_endian(format) > 0;
327 		unsigned int badness, best = -1;
328 		snd_pcm_format_t best_format = (__force snd_pcm_format_t)-1;
329 		for (i = 0; i < ARRAY_SIZE(preferred_formats); i++) {
330 			snd_pcm_format_t f = preferred_formats[i];
331 			unsigned int w;
332 			if (!snd_mask_test(format_mask, (__force int)f))
333 				continue;
334 			w = snd_pcm_format_width(f);
335 			if (w >= width)
336 				badness = w - width;
337 			else
338 				badness = width - w + 32;
339 			badness += snd_pcm_format_unsigned(f) != unsignd;
340 			badness += snd_pcm_format_big_endian(f) != big;
341 			if (badness < best) {
342 				best_format = f;
343 				best = badness;
344 			}
345 		}
346 		if ((__force int)best_format >= 0)
347 			return best_format;
348 		else
349 			return (__force snd_pcm_format_t)-EINVAL;
350 	} else {
351 		switch (format) {
352 		case SNDRV_PCM_FORMAT_MU_LAW:
353 			for (i = 0; i < ARRAY_SIZE(preferred_formats); ++i) {
354 				snd_pcm_format_t format1 = preferred_formats[i];
355 				if (snd_mask_test(format_mask, (__force int)format1))
356 					return format1;
357 			}
358 			/* fall through */
359 		default:
360 			return (__force snd_pcm_format_t)-EINVAL;
361 		}
362 	}
363 }
364 
365 int snd_pcm_plug_format_plugins(struct snd_pcm_substream *plug,
366 				struct snd_pcm_hw_params *params,
367 				struct snd_pcm_hw_params *slave_params)
368 {
369 	struct snd_pcm_plugin_format tmpformat;
370 	struct snd_pcm_plugin_format dstformat;
371 	struct snd_pcm_plugin_format srcformat;
372 	snd_pcm_access_t src_access, dst_access;
373 	struct snd_pcm_plugin *plugin = NULL;
374 	int err;
375 	int stream = snd_pcm_plug_stream(plug);
376 	int slave_interleaved = (params_channels(slave_params) == 1 ||
377 				 params_access(slave_params) == SNDRV_PCM_ACCESS_RW_INTERLEAVED);
378 
379 	switch (stream) {
380 	case SNDRV_PCM_STREAM_PLAYBACK:
381 		dstformat.format = params_format(slave_params);
382 		dstformat.rate = params_rate(slave_params);
383 		dstformat.channels = params_channels(slave_params);
384 		srcformat.format = params_format(params);
385 		srcformat.rate = params_rate(params);
386 		srcformat.channels = params_channels(params);
387 		src_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
388 		dst_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
389 						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
390 		break;
391 	case SNDRV_PCM_STREAM_CAPTURE:
392 		dstformat.format = params_format(params);
393 		dstformat.rate = params_rate(params);
394 		dstformat.channels = params_channels(params);
395 		srcformat.format = params_format(slave_params);
396 		srcformat.rate = params_rate(slave_params);
397 		srcformat.channels = params_channels(slave_params);
398 		src_access = (slave_interleaved ? SNDRV_PCM_ACCESS_RW_INTERLEAVED :
399 						  SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);
400 		dst_access = SNDRV_PCM_ACCESS_RW_INTERLEAVED;
401 		break;
402 	default:
403 		snd_BUG();
404 		return -EINVAL;
405 	}
406 	tmpformat = srcformat;
407 
408 	pdprintf("srcformat: format=%i, rate=%i, channels=%i\n",
409 		 srcformat.format,
410 		 srcformat.rate,
411 		 srcformat.channels);
412 	pdprintf("dstformat: format=%i, rate=%i, channels=%i\n",
413 		 dstformat.format,
414 		 dstformat.rate,
415 		 dstformat.channels);
416 
417 	/* Format change (linearization) */
418 	if (! rate_match(srcformat.rate, dstformat.rate) &&
419 	    ! snd_pcm_format_linear(srcformat.format)) {
420 		if (srcformat.format != SNDRV_PCM_FORMAT_MU_LAW)
421 			return -EINVAL;
422 		tmpformat.format = SNDRV_PCM_FORMAT_S16;
423 		err = snd_pcm_plugin_build_mulaw(plug,
424 						 &srcformat, &tmpformat,
425 						 &plugin);
426 		if (err < 0)
427 			return err;
428 		err = snd_pcm_plugin_append(plugin);
429 		if (err < 0) {
430 			snd_pcm_plugin_free(plugin);
431 			return err;
432 		}
433 		srcformat = tmpformat;
434 		src_access = dst_access;
435 	}
436 
437 	/* channels reduction */
438 	if (srcformat.channels > dstformat.channels) {
439 		tmpformat.channels = dstformat.channels;
440 		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
441 		pdprintf("channels reduction: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
442 		if (err < 0)
443 			return err;
444 		err = snd_pcm_plugin_append(plugin);
445 		if (err < 0) {
446 			snd_pcm_plugin_free(plugin);
447 			return err;
448 		}
449 		srcformat = tmpformat;
450 		src_access = dst_access;
451 	}
452 
453 	/* rate resampling */
454 	if (!rate_match(srcformat.rate, dstformat.rate)) {
455 		if (srcformat.format != SNDRV_PCM_FORMAT_S16) {
456 			/* convert to S16 for resampling */
457 			tmpformat.format = SNDRV_PCM_FORMAT_S16;
458 			err = snd_pcm_plugin_build_linear(plug,
459 							  &srcformat, &tmpformat,
460 							  &plugin);
461 			if (err < 0)
462 				return err;
463 			err = snd_pcm_plugin_append(plugin);
464 			if (err < 0) {
465 				snd_pcm_plugin_free(plugin);
466 				return err;
467 			}
468 			srcformat = tmpformat;
469 			src_access = dst_access;
470 		}
471 		tmpformat.rate = dstformat.rate;
472         	err = snd_pcm_plugin_build_rate(plug,
473         					&srcformat, &tmpformat,
474 						&plugin);
475 		pdprintf("rate down resampling: src=%i, dst=%i returns %i\n", srcformat.rate, tmpformat.rate, err);
476 		if (err < 0)
477 			return err;
478 		err = snd_pcm_plugin_append(plugin);
479 		if (err < 0) {
480 			snd_pcm_plugin_free(plugin);
481 			return err;
482 		}
483 		srcformat = tmpformat;
484 		src_access = dst_access;
485         }
486 
487 	/* format change */
488 	if (srcformat.format != dstformat.format) {
489 		tmpformat.format = dstformat.format;
490 		if (srcformat.format == SNDRV_PCM_FORMAT_MU_LAW ||
491 		    tmpformat.format == SNDRV_PCM_FORMAT_MU_LAW) {
492 			err = snd_pcm_plugin_build_mulaw(plug,
493 							 &srcformat, &tmpformat,
494 							 &plugin);
495 		}
496 		else if (snd_pcm_format_linear(srcformat.format) &&
497 			 snd_pcm_format_linear(tmpformat.format)) {
498 			err = snd_pcm_plugin_build_linear(plug,
499 							  &srcformat, &tmpformat,
500 							  &plugin);
501 		}
502 		else
503 			return -EINVAL;
504 		pdprintf("format change: src=%i, dst=%i returns %i\n", srcformat.format, tmpformat.format, err);
505 		if (err < 0)
506 			return err;
507 		err = snd_pcm_plugin_append(plugin);
508 		if (err < 0) {
509 			snd_pcm_plugin_free(plugin);
510 			return err;
511 		}
512 		srcformat = tmpformat;
513 		src_access = dst_access;
514 	}
515 
516 	/* channels extension */
517 	if (srcformat.channels < dstformat.channels) {
518 		tmpformat.channels = dstformat.channels;
519 		err = snd_pcm_plugin_build_route(plug, &srcformat, &tmpformat, &plugin);
520 		pdprintf("channels extension: src=%i, dst=%i returns %i\n", srcformat.channels, tmpformat.channels, err);
521 		if (err < 0)
522 			return err;
523 		err = snd_pcm_plugin_append(plugin);
524 		if (err < 0) {
525 			snd_pcm_plugin_free(plugin);
526 			return err;
527 		}
528 		srcformat = tmpformat;
529 		src_access = dst_access;
530 	}
531 
532 	/* de-interleave */
533 	if (src_access != dst_access) {
534 		err = snd_pcm_plugin_build_copy(plug,
535 						&srcformat,
536 						&tmpformat,
537 						&plugin);
538 		pdprintf("interleave change (copy: returns %i)\n", err);
539 		if (err < 0)
540 			return err;
541 		err = snd_pcm_plugin_append(plugin);
542 		if (err < 0) {
543 			snd_pcm_plugin_free(plugin);
544 			return err;
545 		}
546 	}
547 
548 	return 0;
549 }
550 
551 snd_pcm_sframes_t snd_pcm_plug_client_channels_buf(struct snd_pcm_substream *plug,
552 					 char *buf,
553 					 snd_pcm_uframes_t count,
554 					 struct snd_pcm_plugin_channel **channels)
555 {
556 	struct snd_pcm_plugin *plugin;
557 	struct snd_pcm_plugin_channel *v;
558 	struct snd_pcm_plugin_format *format;
559 	int width, nchannels, channel;
560 	int stream = snd_pcm_plug_stream(plug);
561 
562 	if (snd_BUG_ON(!buf))
563 		return -ENXIO;
564 	if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
565 		plugin = snd_pcm_plug_first(plug);
566 		format = &plugin->src_format;
567 	} else {
568 		plugin = snd_pcm_plug_last(plug);
569 		format = &plugin->dst_format;
570 	}
571 	v = plugin->buf_channels;
572 	*channels = v;
573 	if ((width = snd_pcm_format_physical_width(format->format)) < 0)
574 		return width;
575 	nchannels = format->channels;
576 	if (snd_BUG_ON(plugin->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
577 		       format->channels > 1))
578 		return -ENXIO;
579 	for (channel = 0; channel < nchannels; channel++, v++) {
580 		v->frames = count;
581 		v->enabled = 1;
582 		v->wanted = (stream == SNDRV_PCM_STREAM_CAPTURE);
583 		v->area.addr = buf;
584 		v->area.first = channel * width;
585 		v->area.step = nchannels * width;
586 	}
587 	return count;
588 }
589 
590 snd_pcm_sframes_t snd_pcm_plug_write_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *src_channels, snd_pcm_uframes_t size)
591 {
592 	struct snd_pcm_plugin *plugin, *next;
593 	struct snd_pcm_plugin_channel *dst_channels;
594 	int err;
595 	snd_pcm_sframes_t frames = size;
596 
597 	plugin = snd_pcm_plug_first(plug);
598 	while (plugin) {
599 		if (frames <= 0)
600 			return frames;
601 		if ((next = plugin->next) != NULL) {
602 			snd_pcm_sframes_t frames1 = frames;
603 			if (plugin->dst_frames) {
604 				frames1 = plugin->dst_frames(plugin, frames);
605 				if (frames1 <= 0)
606 					return frames1;
607 			}
608 			if ((err = next->client_channels(next, frames1, &dst_channels)) < 0) {
609 				return err;
610 			}
611 			if (err != frames1) {
612 				frames = err;
613 				if (plugin->src_frames) {
614 					frames = plugin->src_frames(plugin, frames1);
615 					if (frames <= 0)
616 						return frames;
617 				}
618 			}
619 		} else
620 			dst_channels = NULL;
621 		pdprintf("write plugin: %s, %li\n", plugin->name, frames);
622 		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
623 			return frames;
624 		src_channels = dst_channels;
625 		plugin = next;
626 	}
627 	return calc_src_frames(plug, frames, true);
628 }
629 
630 snd_pcm_sframes_t snd_pcm_plug_read_transfer(struct snd_pcm_substream *plug, struct snd_pcm_plugin_channel *dst_channels_final, snd_pcm_uframes_t size)
631 {
632 	struct snd_pcm_plugin *plugin, *next;
633 	struct snd_pcm_plugin_channel *src_channels, *dst_channels;
634 	snd_pcm_sframes_t frames = size;
635 	int err;
636 
637 	frames = calc_src_frames(plug, frames, true);
638 	if (frames < 0)
639 		return frames;
640 
641 	src_channels = NULL;
642 	plugin = snd_pcm_plug_first(plug);
643 	while (plugin && frames > 0) {
644 		if ((next = plugin->next) != NULL) {
645 			if ((err = plugin->client_channels(plugin, frames, &dst_channels)) < 0) {
646 				return err;
647 			}
648 			frames = err;
649 		} else {
650 			dst_channels = dst_channels_final;
651 		}
652 		pdprintf("read plugin: %s, %li\n", plugin->name, frames);
653 		if ((frames = plugin->transfer(plugin, src_channels, dst_channels, frames)) < 0)
654 			return frames;
655 		plugin = next;
656 		src_channels = dst_channels;
657 	}
658 	return frames;
659 }
660 
661 int snd_pcm_area_silence(const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
662 			 size_t samples, snd_pcm_format_t format)
663 {
664 	/* FIXME: sub byte resolution and odd dst_offset */
665 	unsigned char *dst;
666 	unsigned int dst_step;
667 	int width;
668 	const unsigned char *silence;
669 	if (!dst_area->addr)
670 		return 0;
671 	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
672 	width = snd_pcm_format_physical_width(format);
673 	if (width <= 0)
674 		return -EINVAL;
675 	if (dst_area->step == (unsigned int) width && width >= 8)
676 		return snd_pcm_format_set_silence(format, dst, samples);
677 	silence = snd_pcm_format_silence_64(format);
678 	if (! silence)
679 		return -EINVAL;
680 	dst_step = dst_area->step / 8;
681 	if (width == 4) {
682 		/* Ima ADPCM */
683 		int dstbit = dst_area->first % 8;
684 		int dstbit_step = dst_area->step % 8;
685 		while (samples-- > 0) {
686 			if (dstbit)
687 				*dst &= 0xf0;
688 			else
689 				*dst &= 0x0f;
690 			dst += dst_step;
691 			dstbit += dstbit_step;
692 			if (dstbit == 8) {
693 				dst++;
694 				dstbit = 0;
695 			}
696 		}
697 	} else {
698 		width /= 8;
699 		while (samples-- > 0) {
700 			memcpy(dst, silence, width);
701 			dst += dst_step;
702 		}
703 	}
704 	return 0;
705 }
706 
707 int snd_pcm_area_copy(const struct snd_pcm_channel_area *src_area, size_t src_offset,
708 		      const struct snd_pcm_channel_area *dst_area, size_t dst_offset,
709 		      size_t samples, snd_pcm_format_t format)
710 {
711 	/* FIXME: sub byte resolution and odd dst_offset */
712 	char *src, *dst;
713 	int width;
714 	int src_step, dst_step;
715 	src = src_area->addr + (src_area->first + src_area->step * src_offset) / 8;
716 	if (!src_area->addr)
717 		return snd_pcm_area_silence(dst_area, dst_offset, samples, format);
718 	dst = dst_area->addr + (dst_area->first + dst_area->step * dst_offset) / 8;
719 	if (!dst_area->addr)
720 		return 0;
721 	width = snd_pcm_format_physical_width(format);
722 	if (width <= 0)
723 		return -EINVAL;
724 	if (src_area->step == (unsigned int) width &&
725 	    dst_area->step == (unsigned int) width && width >= 8) {
726 		size_t bytes = samples * width / 8;
727 		memcpy(dst, src, bytes);
728 		return 0;
729 	}
730 	src_step = src_area->step / 8;
731 	dst_step = dst_area->step / 8;
732 	if (width == 4) {
733 		/* Ima ADPCM */
734 		int srcbit = src_area->first % 8;
735 		int srcbit_step = src_area->step % 8;
736 		int dstbit = dst_area->first % 8;
737 		int dstbit_step = dst_area->step % 8;
738 		while (samples-- > 0) {
739 			unsigned char srcval;
740 			if (srcbit)
741 				srcval = *src & 0x0f;
742 			else
743 				srcval = (*src & 0xf0) >> 4;
744 			if (dstbit)
745 				*dst = (*dst & 0xf0) | srcval;
746 			else
747 				*dst = (*dst & 0x0f) | (srcval << 4);
748 			src += src_step;
749 			srcbit += srcbit_step;
750 			if (srcbit == 8) {
751 				src++;
752 				srcbit = 0;
753 			}
754 			dst += dst_step;
755 			dstbit += dstbit_step;
756 			if (dstbit == 8) {
757 				dst++;
758 				dstbit = 0;
759 			}
760 		}
761 	} else {
762 		width /= 8;
763 		while (samples-- > 0) {
764 			memcpy(dst, src, width);
765 			src += src_step;
766 			dst += dst_step;
767 		}
768 	}
769 	return 0;
770 }
771