io.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <linux/time.h>
  22. #include <sound/core.h>
  23. #include <sound/pcm.h>
  24. #include <sound/pcm_params.h>
  25. #include "pcm_plugin.h"
  26. #define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1)
  27. #define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count,1)
  28. #define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1)
  29. #define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count,1)
  30. /*
  31. * Basic io plugin
  32. */
  33. static snd_pcm_sframes_t io_playback_transfer(struct snd_pcm_plugin *plugin,
  34. const struct snd_pcm_plugin_channel *src_channels,
  35. struct snd_pcm_plugin_channel *dst_channels,
  36. snd_pcm_uframes_t frames)
  37. {
  38. if (snd_BUG_ON(!plugin))
  39. return -ENXIO;
  40. if (snd_BUG_ON(!src_channels))
  41. return -ENXIO;
  42. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  43. return pcm_write(plugin->plug, src_channels->area.addr, frames);
  44. } else {
  45. int channel, channels = plugin->dst_format.channels;
  46. void **bufs = (void**)plugin->extra_data;
  47. if (snd_BUG_ON(!bufs))
  48. return -ENXIO;
  49. for (channel = 0; channel < channels; channel++) {
  50. if (src_channels[channel].enabled)
  51. bufs[channel] = src_channels[channel].area.addr;
  52. else
  53. bufs[channel] = NULL;
  54. }
  55. return pcm_writev(plugin->plug, bufs, frames);
  56. }
  57. }
  58. static snd_pcm_sframes_t io_capture_transfer(struct snd_pcm_plugin *plugin,
  59. const struct snd_pcm_plugin_channel *src_channels,
  60. struct snd_pcm_plugin_channel *dst_channels,
  61. snd_pcm_uframes_t frames)
  62. {
  63. if (snd_BUG_ON(!plugin))
  64. return -ENXIO;
  65. if (snd_BUG_ON(!dst_channels))
  66. 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. if (snd_BUG_ON(!bufs))
  73. return -ENXIO;
  74. for (channel = 0; channel < channels; channel++) {
  75. if (dst_channels[channel].enabled)
  76. bufs[channel] = dst_channels[channel].area.addr;
  77. else
  78. bufs[channel] = NULL;
  79. }
  80. return pcm_readv(plugin->plug, bufs, frames);
  81. }
  82. return 0;
  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. int snd_pcm_plugin_build_io(struct snd_pcm_substream *plug,
  102. struct snd_pcm_hw_params *params,
  103. struct snd_pcm_plugin **r_plugin)
  104. {
  105. int err;
  106. struct snd_pcm_plugin_format format;
  107. struct snd_pcm_plugin *plugin;
  108. if (snd_BUG_ON(!r_plugin))
  109. return -ENXIO;
  110. *r_plugin = NULL;
  111. if (snd_BUG_ON(!plug || !params))
  112. 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. *r_plugin = plugin;
  131. return 0;
  132. }