Merge branches 'release' and 'ppc-workaround' into release
[linux-2.6] / sound / core / oss / io.c
1 /*
2  *  PCM I/O Plug-In Interface
3  *  Copyright (c) 1999 by Jaroslav Kysela <perex@perex.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 <linux/time.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include "pcm_plugin.h"
27
28 #define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1)
29 #define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count,1)
30 #define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1)
31 #define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count,1)
32
33 /*
34  *  Basic io plugin
35  */
36  
37 static snd_pcm_sframes_t io_playback_transfer(struct snd_pcm_plugin *plugin,
38                                     const struct snd_pcm_plugin_channel *src_channels,
39                                     struct snd_pcm_plugin_channel *dst_channels,
40                                     snd_pcm_uframes_t frames)
41 {
42         snd_assert(plugin != NULL, return -ENXIO);
43         snd_assert(src_channels != NULL, return -ENXIO);
44         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
45                 return pcm_write(plugin->plug, src_channels->area.addr, frames);
46         } else {
47                 int channel, channels = plugin->dst_format.channels;
48                 void **bufs = (void**)plugin->extra_data;
49                 snd_assert(bufs != NULL, return -ENXIO);
50                 for (channel = 0; channel < channels; channel++) {
51                         if (src_channels[channel].enabled)
52                                 bufs[channel] = src_channels[channel].area.addr;
53                         else
54                                 bufs[channel] = NULL;
55                 }
56                 return pcm_writev(plugin->plug, bufs, frames);
57         }
58 }
59  
60 static snd_pcm_sframes_t io_capture_transfer(struct snd_pcm_plugin *plugin,
61                                    const struct snd_pcm_plugin_channel *src_channels,
62                                    struct snd_pcm_plugin_channel *dst_channels,
63                                    snd_pcm_uframes_t frames)
64 {
65         snd_assert(plugin != NULL, return -ENXIO);
66         snd_assert(dst_channels != NULL, return -ENXIO);
67         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
68                 return pcm_read(plugin->plug, dst_channels->area.addr, frames);
69         } else {
70                 int channel, channels = plugin->dst_format.channels;
71                 void **bufs = (void**)plugin->extra_data;
72                 snd_assert(bufs != NULL, return -ENXIO);
73                 for (channel = 0; channel < channels; channel++) {
74                         if (dst_channels[channel].enabled)
75                                 bufs[channel] = dst_channels[channel].area.addr;
76                         else
77                                 bufs[channel] = NULL;
78                 }
79                 return pcm_readv(plugin->plug, bufs, frames);
80         }
81         return 0;
82 }
83  
84 static snd_pcm_sframes_t io_src_channels(struct snd_pcm_plugin *plugin,
85                              snd_pcm_uframes_t frames,
86                              struct snd_pcm_plugin_channel **channels)
87 {
88         int err;
89         unsigned int channel;
90         struct snd_pcm_plugin_channel *v;
91         err = snd_pcm_plugin_client_channels(plugin, frames, &v);
92         if (err < 0)
93                 return err;
94         *channels = v;
95         if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
96                 for (channel = 0; channel < plugin->src_format.channels; ++channel, ++v)
97                         v->wanted = 1;
98         }
99         return frames;
100 }
101
102 int snd_pcm_plugin_build_io(struct snd_pcm_substream *plug,
103                             struct snd_pcm_hw_params *params,
104                             struct snd_pcm_plugin **r_plugin)
105 {
106         int err;
107         struct snd_pcm_plugin_format format;
108         struct snd_pcm_plugin *plugin;
109
110         snd_assert(r_plugin != NULL, return -ENXIO);
111         *r_plugin = NULL;
112         snd_assert(plug != NULL && params != NULL, return -ENXIO);
113         format.format = params_format(params);
114         format.rate = params_rate(params);
115         format.channels = params_channels(params);
116         err = snd_pcm_plugin_build(plug, "I/O io",
117                                    &format, &format,
118                                    sizeof(void *) * format.channels,
119                                    &plugin);
120         if (err < 0)
121                 return err;
122         plugin->access = params_access(params);
123         if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
124                 plugin->transfer = io_playback_transfer;
125                 if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
126                         plugin->client_channels = io_src_channels;
127         } else {
128                 plugin->transfer = io_capture_transfer;
129         }
130
131         *r_plugin = plugin;
132         return 0;
133 }