io.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <sound/driver.h>
  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. #define pcm_write(plug,buf,count) snd_pcm_oss_write3(plug,buf,count,1)
  28. #define pcm_writev(plug,vec,count) snd_pcm_oss_writev3(plug,vec,count,1)
  29. #define pcm_read(plug,buf,count) snd_pcm_oss_read3(plug,buf,count,1)
  30. #define pcm_readv(plug,vec,count) snd_pcm_oss_readv3(plug,vec,count,1)
  31. /*
  32. * Basic io plugin
  33. */
  34. static snd_pcm_sframes_t io_playback_transfer(snd_pcm_plugin_t *plugin,
  35. const snd_pcm_plugin_channel_t *src_channels,
  36. snd_pcm_plugin_channel_t *dst_channels ATTRIBUTE_UNUSED,
  37. snd_pcm_uframes_t frames)
  38. {
  39. snd_assert(plugin != NULL, return -ENXIO);
  40. snd_assert(src_channels != NULL, return -ENXIO);
  41. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  42. return pcm_write(plugin->plug, src_channels->area.addr, frames);
  43. } else {
  44. int channel, channels = plugin->dst_format.channels;
  45. void **bufs = (void**)plugin->extra_data;
  46. snd_assert(bufs != NULL, return -ENXIO);
  47. for (channel = 0; channel < channels; channel++) {
  48. if (src_channels[channel].enabled)
  49. bufs[channel] = src_channels[channel].area.addr;
  50. else
  51. bufs[channel] = NULL;
  52. }
  53. return pcm_writev(plugin->plug, bufs, frames);
  54. }
  55. }
  56. static snd_pcm_sframes_t io_capture_transfer(snd_pcm_plugin_t *plugin,
  57. const snd_pcm_plugin_channel_t *src_channels ATTRIBUTE_UNUSED,
  58. snd_pcm_plugin_channel_t *dst_channels,
  59. snd_pcm_uframes_t frames)
  60. {
  61. snd_assert(plugin != NULL, return -ENXIO);
  62. snd_assert(dst_channels != NULL, return -ENXIO);
  63. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  64. return pcm_read(plugin->plug, dst_channels->area.addr, frames);
  65. } else {
  66. int channel, channels = plugin->dst_format.channels;
  67. void **bufs = (void**)plugin->extra_data;
  68. snd_assert(bufs != NULL, return -ENXIO);
  69. for (channel = 0; channel < channels; channel++) {
  70. if (dst_channels[channel].enabled)
  71. bufs[channel] = dst_channels[channel].area.addr;
  72. else
  73. bufs[channel] = NULL;
  74. }
  75. return pcm_readv(plugin->plug, bufs, frames);
  76. }
  77. return 0;
  78. }
  79. static snd_pcm_sframes_t io_src_channels(snd_pcm_plugin_t *plugin,
  80. snd_pcm_uframes_t frames,
  81. snd_pcm_plugin_channel_t **channels)
  82. {
  83. int err;
  84. unsigned int channel;
  85. snd_pcm_plugin_channel_t *v;
  86. err = snd_pcm_plugin_client_channels(plugin, frames, &v);
  87. if (err < 0)
  88. return err;
  89. *channels = v;
  90. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED) {
  91. for (channel = 0; channel < plugin->src_format.channels; ++channel, ++v)
  92. v->wanted = 1;
  93. }
  94. return frames;
  95. }
  96. int snd_pcm_plugin_build_io(snd_pcm_plug_t *plug,
  97. snd_pcm_hw_params_t *params,
  98. snd_pcm_plugin_t **r_plugin)
  99. {
  100. int err;
  101. snd_pcm_plugin_format_t format;
  102. snd_pcm_plugin_t *plugin;
  103. snd_assert(r_plugin != NULL, return -ENXIO);
  104. *r_plugin = NULL;
  105. snd_assert(plug != NULL && params != NULL, return -ENXIO);
  106. format.format = params_format(params);
  107. format.rate = params_rate(params);
  108. format.channels = params_channels(params);
  109. err = snd_pcm_plugin_build(plug, "I/O io",
  110. &format, &format,
  111. sizeof(void *) * format.channels,
  112. &plugin);
  113. if (err < 0)
  114. return err;
  115. plugin->access = params_access(params);
  116. if (snd_pcm_plug_stream(plug) == SNDRV_PCM_STREAM_PLAYBACK) {
  117. plugin->transfer = io_playback_transfer;
  118. if (plugin->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED)
  119. plugin->client_channels = io_src_channels;
  120. } else {
  121. plugin->transfer = io_capture_transfer;
  122. }
  123. *r_plugin = plugin;
  124. return 0;
  125. }