fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / sound / core / oss / io.c
1 /*
2  *  PCM I/O Plug-In Interface
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@suse.cz>
4  *
5  *
6  *   This library is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU Library General Public License as
8  *   published by the Free Software Foundation; either version 2 of
9  *   the License, or (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU Library General Public License for more details.
15  *
16  *   You should have received a copy of the GNU Library General Public
17  *   License along with this library; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21   
22 #include <sound/driver.h>
23
24 #ifdef CONFIG_SND_PCM_OSS_PLUGINS
25
26 #include <linux/time.h>
27 #include <sound/core.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include "pcm_plugin.h"
31
32 #define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1)
33 #define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count,1)
34 #define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1)
35 #define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count,1)
36
37 /*
38  *  Basic io plugin
39  */
40  
41 static snd_pcm_sframes_t io_playback_transfer(struct snd_pcm_plugin *plugin,
42                                     const struct snd_pcm_plugin_channel *src_channels,
43                                     struct snd_pcm_plugin_channel *dst_channels,
44                                     snd_pcm_uframes_t frames)
45 {
46         snd_assert(plugin != NULL, return -ENXIO);
47         snd_assert(src_channels != NULL, return -ENXIO);
48         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
49                 return pcm_write(plugin->plug, src_channels->area.addr, frames);
50         } else {
51                 int channel, channels = plugin->dst_format.channels;
52                 void **bufs = (void**)plugin->extra_data;
53                 snd_assert(bufs != NULL, return -ENXIO);
54                 for (channel = 0; channel < channels; channel++) {
55                         if (src_channels[channel].enabled)
56                                 bufs[channel] = src_channels[channel].area.addr;
57                         else
58                                 bufs[channel] = NULL;
59                 }
60                 return pcm_writev(plugin->plug, bufs, frames);
61         }
62 }
63  
64 static snd_pcm_sframes_t io_capture_transfer(struct snd_pcm_plugin *plugin,
65                                    const struct snd_pcm_plugin_channel *src_channels,
66                                    struct snd_pcm_plugin_channel *dst_channels,
67                                    snd_pcm_uframes_t frames)
68 {
69         snd_assert(plugin != NULL, return -ENXIO);
70         snd_assert(dst_channels != NULL, return -ENXIO);
71         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
72                 return pcm_read(plugin->plug, dst_channels->area.addr, frames);
73         } else {
74                 int channel, channels = plugin->dst_format.channels;
75                 void **bufs = (void**)plugin->extra_data;
76                 snd_assert(bufs != NULL, return -ENXIO);
77                 for (channel = 0; channel < channels; channel++) {
78                         if (dst_channels[channel].enabled)
79                                 bufs[channel] = dst_channels[channel].area.addr;
80                         else
81                                 bufs[channel] = NULL;
82                 }
83                 return pcm_readv(plugin->plug, bufs, frames);
84         }
85         return 0;
86 }
87  
88 static snd_pcm_sframes_t io_src_channels(struct snd_pcm_plugin *plugin,
89                              snd_pcm_uframes_t frames,
90                              struct snd_pcm_plugin_channel **channels)
91 {
92         int err;
93         unsigned int channel;
94         struct snd_pcm_plugin_channel *v;
95         err = snd_pcm_plugin_client_channels(plugin, frames, &v);
96         if (err < 0)
97                 return err;
98         *channels = v;
99         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
100                 for (channel = 0; channel < plugin->src_format.channels; ++channel, ++v)
101                         v->wanted = 1;
102         }
103         return frames;
104 }
105
106 int snd_pcm_plugin_build_io(struct snd_pcm_substream *plug,
107                             struct snd_pcm_hw_params *params,
108                             struct snd_pcm_plugin **r_plugin)
109 {
110         int err;
111         struct snd_pcm_plugin_format format;
112         struct snd_pcm_plugin *plugin;
113
114         snd_assert(r_plugin != NULL, return -ENXIO);
115         *r_plugin = NULL;
116         snd_assert(plug != NULL && params != NULL, return -ENXIO);
117         format.format = params_format(params);
118         format.rate = params_rate(params);
119         format.channels = params_channels(params);
120         err = snd_pcm_plugin_build(plug, "I/O io",
121                                    &format, &format,
122                                    sizeof(void *) * format.channels,
123                                    &plugin);
124         if (err < 0)
125                 return err;
126         plugin->access = params_access(params);
127         if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
128                 plugin->transfer = io_playback_transfer;
129                 if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
130                         plugin->client_channels = io_src_channels;
131         } else {
132                 plugin->transfer = io_capture_transfer;
133         }
134
135         *r_plugin = plugin;
136         return 0;
137 }
138
139 #endif