18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  PCM I/O Plug-In Interface
38c2ecf20Sopenharmony_ci *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.cz>
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *   This library is free software; you can redistribute it and/or modify
78c2ecf20Sopenharmony_ci *   it under the terms of the GNU Library General Public License as
88c2ecf20Sopenharmony_ci *   published by the Free Software Foundation; either version 2 of
98c2ecf20Sopenharmony_ci *   the License, or (at your option) any later version.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   This program is distributed in the hope that it will be useful,
128c2ecf20Sopenharmony_ci *   but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2ecf20Sopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148c2ecf20Sopenharmony_ci *   GNU Library General Public License for more details.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *   You should have received a copy of the GNU Library General Public
178c2ecf20Sopenharmony_ci *   License along with this library; if not, write to the Free Software
188c2ecf20Sopenharmony_ci *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include <linux/time.h>
238c2ecf20Sopenharmony_ci#include <sound/core.h>
248c2ecf20Sopenharmony_ci#include <sound/pcm.h>
258c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
268c2ecf20Sopenharmony_ci#include "pcm_plugin.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1)
298c2ecf20Sopenharmony_ci#define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count)
308c2ecf20Sopenharmony_ci#define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1)
318c2ecf20Sopenharmony_ci#define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci *  Basic io plugin
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic snd_pcm_sframes_t io_playback_transfer(struct snd_pcm_plugin *plugin,
388c2ecf20Sopenharmony_ci				    const struct snd_pcm_plugin_channel *src_channels,
398c2ecf20Sopenharmony_ci				    struct snd_pcm_plugin_channel *dst_channels,
408c2ecf20Sopenharmony_ci				    snd_pcm_uframes_t frames)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!plugin))
438c2ecf20Sopenharmony_ci		return -ENXIO;
448c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!src_channels))
458c2ecf20Sopenharmony_ci		return -ENXIO;
468c2ecf20Sopenharmony_ci	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
478c2ecf20Sopenharmony_ci		return pcm_write(plugin->plug, src_channels->area.addr, frames);
488c2ecf20Sopenharmony_ci	} else {
498c2ecf20Sopenharmony_ci		int channel, channels = plugin->dst_format.channels;
508c2ecf20Sopenharmony_ci		void **bufs = (void**)plugin->extra_data;
518c2ecf20Sopenharmony_ci		if (snd_BUG_ON(!bufs))
528c2ecf20Sopenharmony_ci			return -ENXIO;
538c2ecf20Sopenharmony_ci		for (channel = 0; channel < channels; channel++) {
548c2ecf20Sopenharmony_ci			if (src_channels[channel].enabled)
558c2ecf20Sopenharmony_ci				bufs[channel] = src_channels[channel].area.addr;
568c2ecf20Sopenharmony_ci			else
578c2ecf20Sopenharmony_ci				bufs[channel] = NULL;
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci		return pcm_writev(plugin->plug, bufs, frames);
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic snd_pcm_sframes_t io_capture_transfer(struct snd_pcm_plugin *plugin,
648c2ecf20Sopenharmony_ci				   const struct snd_pcm_plugin_channel *src_channels,
658c2ecf20Sopenharmony_ci				   struct snd_pcm_plugin_channel *dst_channels,
668c2ecf20Sopenharmony_ci				   snd_pcm_uframes_t frames)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!plugin))
698c2ecf20Sopenharmony_ci		return -ENXIO;
708c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!dst_channels))
718c2ecf20Sopenharmony_ci		return -ENXIO;
728c2ecf20Sopenharmony_ci	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
738c2ecf20Sopenharmony_ci		return pcm_read(plugin->plug, dst_channels->area.addr, frames);
748c2ecf20Sopenharmony_ci	} else {
758c2ecf20Sopenharmony_ci		int channel, channels = plugin->dst_format.channels;
768c2ecf20Sopenharmony_ci		void **bufs = (void**)plugin->extra_data;
778c2ecf20Sopenharmony_ci		if (snd_BUG_ON(!bufs))
788c2ecf20Sopenharmony_ci			return -ENXIO;
798c2ecf20Sopenharmony_ci		for (channel = 0; channel < channels; channel++) {
808c2ecf20Sopenharmony_ci			if (dst_channels[channel].enabled)
818c2ecf20Sopenharmony_ci				bufs[channel] = dst_channels[channel].area.addr;
828c2ecf20Sopenharmony_ci			else
838c2ecf20Sopenharmony_ci				bufs[channel] = NULL;
848c2ecf20Sopenharmony_ci		}
858c2ecf20Sopenharmony_ci		return pcm_readv(plugin->plug, bufs, frames);
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic snd_pcm_sframes_t io_src_channels(struct snd_pcm_plugin *plugin,
918c2ecf20Sopenharmony_ci			     snd_pcm_uframes_t frames,
928c2ecf20Sopenharmony_ci			     struct snd_pcm_plugin_channel **channels)
938c2ecf20Sopenharmony_ci{
948c2ecf20Sopenharmony_ci	int err;
958c2ecf20Sopenharmony_ci	unsigned int channel;
968c2ecf20Sopenharmony_ci	struct snd_pcm_plugin_channel *v;
978c2ecf20Sopenharmony_ci	err = snd_pcm_plugin_client_channels(plugin, frames, &v);
988c2ecf20Sopenharmony_ci	if (err < 0)
998c2ecf20Sopenharmony_ci		return err;
1008c2ecf20Sopenharmony_ci	*channels = v;
1018c2ecf20Sopenharmony_ci	if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
1028c2ecf20Sopenharmony_ci		for (channel = 0; channel < plugin->src_format.channels; ++channel, ++v)
1038c2ecf20Sopenharmony_ci			v->wanted = 1;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci	return frames;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ciint snd_pcm_plugin_build_io(struct snd_pcm_substream *plug,
1098c2ecf20Sopenharmony_ci			    struct snd_pcm_hw_params *params,
1108c2ecf20Sopenharmony_ci			    struct snd_pcm_plugin **r_plugin)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	int err;
1138c2ecf20Sopenharmony_ci	struct snd_pcm_plugin_format format;
1148c2ecf20Sopenharmony_ci	struct snd_pcm_plugin *plugin;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!r_plugin))
1178c2ecf20Sopenharmony_ci		return -ENXIO;
1188c2ecf20Sopenharmony_ci	*r_plugin = NULL;
1198c2ecf20Sopenharmony_ci	if (snd_BUG_ON(!plug || !params))
1208c2ecf20Sopenharmony_ci		return -ENXIO;
1218c2ecf20Sopenharmony_ci	format.format = params_format(params);
1228c2ecf20Sopenharmony_ci	format.rate = params_rate(params);
1238c2ecf20Sopenharmony_ci	format.channels = params_channels(params);
1248c2ecf20Sopenharmony_ci	err = snd_pcm_plugin_build(plug, "I/O io",
1258c2ecf20Sopenharmony_ci				   &format, &format,
1268c2ecf20Sopenharmony_ci				   sizeof(void *) * format.channels,
1278c2ecf20Sopenharmony_ci				   &plugin);
1288c2ecf20Sopenharmony_ci	if (err < 0)
1298c2ecf20Sopenharmony_ci		return err;
1308c2ecf20Sopenharmony_ci	plugin->access = params_access(params);
1318c2ecf20Sopenharmony_ci	if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
1328c2ecf20Sopenharmony_ci		plugin->transfer = io_playback_transfer;
1338c2ecf20Sopenharmony_ci		if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
1348c2ecf20Sopenharmony_ci			plugin->client_channels = io_src_channels;
1358c2ecf20Sopenharmony_ci	} else {
1368c2ecf20Sopenharmony_ci		plugin->transfer = io_capture_transfer;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	*r_plugin = plugin;
1408c2ecf20Sopenharmony_ci	return 0;
1418c2ecf20Sopenharmony_ci}
142