amdtp-ff.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * amdtp-ff.c - a part of driver for RME Fireface series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <sound/pcm.h>
  9. #include "ff.h"
  10. struct amdtp_ff {
  11. unsigned int pcm_channels;
  12. };
  13. int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate,
  14. unsigned int pcm_channels)
  15. {
  16. struct amdtp_ff *p = s->protocol;
  17. unsigned int data_channels;
  18. if (amdtp_stream_running(s))
  19. return -EBUSY;
  20. p->pcm_channels = pcm_channels;
  21. data_channels = pcm_channels;
  22. return amdtp_stream_set_parameters(s, rate, data_channels);
  23. }
  24. static void write_pcm_s32(struct amdtp_stream *s,
  25. struct snd_pcm_substream *pcm,
  26. __le32 *buffer, unsigned int frames)
  27. {
  28. struct amdtp_ff *p = s->protocol;
  29. struct snd_pcm_runtime *runtime = pcm->runtime;
  30. unsigned int channels, remaining_frames, i, c;
  31. const u32 *src;
  32. channels = p->pcm_channels;
  33. src = (void *)runtime->dma_area +
  34. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  35. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  36. for (i = 0; i < frames; ++i) {
  37. for (c = 0; c < channels; ++c) {
  38. buffer[c] = cpu_to_le32(*src);
  39. src++;
  40. }
  41. buffer += s->data_block_quadlets;
  42. if (--remaining_frames == 0)
  43. src = (void *)runtime->dma_area;
  44. }
  45. }
  46. static void read_pcm_s32(struct amdtp_stream *s,
  47. struct snd_pcm_substream *pcm,
  48. __le32 *buffer, unsigned int frames)
  49. {
  50. struct amdtp_ff *p = s->protocol;
  51. struct snd_pcm_runtime *runtime = pcm->runtime;
  52. unsigned int channels, remaining_frames, i, c;
  53. u32 *dst;
  54. channels = p->pcm_channels;
  55. dst = (void *)runtime->dma_area +
  56. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  57. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  58. for (i = 0; i < frames; ++i) {
  59. for (c = 0; c < channels; ++c) {
  60. *dst = le32_to_cpu(buffer[c]) & 0xffffff00;
  61. dst++;
  62. }
  63. buffer += s->data_block_quadlets;
  64. if (--remaining_frames == 0)
  65. dst = (void *)runtime->dma_area;
  66. }
  67. }
  68. static void write_pcm_silence(struct amdtp_stream *s,
  69. __le32 *buffer, unsigned int frames)
  70. {
  71. struct amdtp_ff *p = s->protocol;
  72. unsigned int i, c, channels = p->pcm_channels;
  73. for (i = 0; i < frames; ++i) {
  74. for (c = 0; c < channels; ++c)
  75. buffer[c] = cpu_to_le32(0x00000000);
  76. buffer += s->data_block_quadlets;
  77. }
  78. }
  79. int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s,
  80. struct snd_pcm_runtime *runtime)
  81. {
  82. int err;
  83. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  84. if (err < 0)
  85. return err;
  86. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  87. }
  88. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  89. __be32 *buffer,
  90. unsigned int data_blocks,
  91. unsigned int *syt)
  92. {
  93. struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
  94. unsigned int pcm_frames;
  95. if (pcm) {
  96. write_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
  97. pcm_frames = data_blocks;
  98. } else {
  99. write_pcm_silence(s, (__le32 *)buffer, data_blocks);
  100. pcm_frames = 0;
  101. }
  102. return pcm_frames;
  103. }
  104. static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
  105. __be32 *buffer,
  106. unsigned int data_blocks,
  107. unsigned int *syt)
  108. {
  109. struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
  110. unsigned int pcm_frames;
  111. if (pcm) {
  112. read_pcm_s32(s, pcm, (__le32 *)buffer, data_blocks);
  113. pcm_frames = data_blocks;
  114. } else {
  115. pcm_frames = 0;
  116. }
  117. return pcm_frames;
  118. }
  119. int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit,
  120. enum amdtp_stream_direction dir)
  121. {
  122. amdtp_stream_process_data_blocks_t process_data_blocks;
  123. if (dir == AMDTP_IN_STREAM)
  124. process_data_blocks = process_tx_data_blocks;
  125. else
  126. process_data_blocks = process_rx_data_blocks;
  127. return amdtp_stream_init(s, unit, dir, CIP_NO_HEADER, 0,
  128. process_data_blocks, sizeof(struct amdtp_ff));
  129. }