amdtp-tascam.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * amdtp-tascam.c - a part of driver for TASCAM FireWire series
  3. *
  4. * Copyright (c) 2015 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <sound/pcm.h>
  9. #include "tascam.h"
  10. #define AMDTP_FMT_TSCM_TX 0x1e
  11. #define AMDTP_FMT_TSCM_RX 0x3e
  12. struct amdtp_tscm {
  13. unsigned int pcm_channels;
  14. void (*transfer_samples)(struct amdtp_stream *s,
  15. struct snd_pcm_substream *pcm,
  16. __be32 *buffer, unsigned int frames);
  17. };
  18. int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate)
  19. {
  20. struct amdtp_tscm *p = s->protocol;
  21. unsigned int data_channels;
  22. if (amdtp_stream_running(s))
  23. return -EBUSY;
  24. data_channels = p->pcm_channels;
  25. /* Packets in in-stream have extra 2 data channels. */
  26. if (s->direction == AMDTP_IN_STREAM)
  27. data_channels += 2;
  28. return amdtp_stream_set_parameters(s, rate, data_channels);
  29. }
  30. static void write_pcm_s32(struct amdtp_stream *s,
  31. struct snd_pcm_substream *pcm,
  32. __be32 *buffer, unsigned int frames)
  33. {
  34. struct amdtp_tscm *p = s->protocol;
  35. struct snd_pcm_runtime *runtime = pcm->runtime;
  36. unsigned int channels, remaining_frames, i, c;
  37. const u32 *src;
  38. channels = p->pcm_channels;
  39. src = (void *)runtime->dma_area +
  40. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  41. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  42. for (i = 0; i < frames; ++i) {
  43. for (c = 0; c < channels; ++c) {
  44. buffer[c] = cpu_to_be32(*src);
  45. src++;
  46. }
  47. buffer += s->data_block_quadlets;
  48. if (--remaining_frames == 0)
  49. src = (void *)runtime->dma_area;
  50. }
  51. }
  52. static void write_pcm_s16(struct amdtp_stream *s,
  53. struct snd_pcm_substream *pcm,
  54. __be32 *buffer, unsigned int frames)
  55. {
  56. struct amdtp_tscm *p = s->protocol;
  57. struct snd_pcm_runtime *runtime = pcm->runtime;
  58. unsigned int channels, remaining_frames, i, c;
  59. const u16 *src;
  60. channels = p->pcm_channels;
  61. src = (void *)runtime->dma_area +
  62. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  63. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  64. for (i = 0; i < frames; ++i) {
  65. for (c = 0; c < channels; ++c) {
  66. buffer[c] = cpu_to_be32(*src << 16);
  67. src++;
  68. }
  69. buffer += s->data_block_quadlets;
  70. if (--remaining_frames == 0)
  71. src = (void *)runtime->dma_area;
  72. }
  73. }
  74. static void read_pcm_s32(struct amdtp_stream *s,
  75. struct snd_pcm_substream *pcm,
  76. __be32 *buffer, unsigned int frames)
  77. {
  78. struct amdtp_tscm *p = s->protocol;
  79. struct snd_pcm_runtime *runtime = pcm->runtime;
  80. unsigned int channels, remaining_frames, i, c;
  81. u32 *dst;
  82. channels = p->pcm_channels;
  83. dst = (void *)runtime->dma_area +
  84. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  85. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  86. /* The first data channel is for event counter. */
  87. buffer += 1;
  88. for (i = 0; i < frames; ++i) {
  89. for (c = 0; c < channels; ++c) {
  90. *dst = be32_to_cpu(buffer[c]);
  91. dst++;
  92. }
  93. buffer += s->data_block_quadlets;
  94. if (--remaining_frames == 0)
  95. dst = (void *)runtime->dma_area;
  96. }
  97. }
  98. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  99. unsigned int data_blocks)
  100. {
  101. struct amdtp_tscm *p = s->protocol;
  102. unsigned int channels, i, c;
  103. channels = p->pcm_channels;
  104. for (i = 0; i < data_blocks; ++i) {
  105. for (c = 0; c < channels; ++c)
  106. buffer[c] = 0x00000000;
  107. buffer += s->data_block_quadlets;
  108. }
  109. }
  110. int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
  111. struct snd_pcm_runtime *runtime)
  112. {
  113. int err;
  114. /*
  115. * Our implementation allows this protocol to deliver 24 bit sample in
  116. * 32bit data channel.
  117. */
  118. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  119. if (err < 0)
  120. return err;
  121. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  122. }
  123. void amdtp_tscm_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
  124. {
  125. struct amdtp_tscm *p = s->protocol;
  126. if (WARN_ON(amdtp_stream_pcm_running(s)))
  127. return;
  128. switch (format) {
  129. default:
  130. WARN_ON(1);
  131. /* fall through */
  132. case SNDRV_PCM_FORMAT_S16:
  133. if (s->direction == AMDTP_OUT_STREAM) {
  134. p->transfer_samples = write_pcm_s16;
  135. break;
  136. }
  137. WARN_ON(1);
  138. /* fall through */
  139. case SNDRV_PCM_FORMAT_S32:
  140. if (s->direction == AMDTP_OUT_STREAM)
  141. p->transfer_samples = write_pcm_s32;
  142. else
  143. p->transfer_samples = read_pcm_s32;
  144. break;
  145. }
  146. }
  147. static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
  148. __be32 *buffer,
  149. unsigned int data_blocks,
  150. unsigned int *syt)
  151. {
  152. struct amdtp_tscm *p = (struct amdtp_tscm *)s->protocol;
  153. struct snd_pcm_substream *pcm;
  154. pcm = ACCESS_ONCE(s->pcm);
  155. if (data_blocks > 0 && pcm)
  156. p->transfer_samples(s, pcm, buffer, data_blocks);
  157. /* A place holder for control messages. */
  158. return data_blocks;
  159. }
  160. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  161. __be32 *buffer,
  162. unsigned int data_blocks,
  163. unsigned int *syt)
  164. {
  165. struct amdtp_tscm *p = (struct amdtp_tscm *)s->protocol;
  166. struct snd_pcm_substream *pcm;
  167. /* This field is not used. */
  168. *syt = 0x0000;
  169. pcm = ACCESS_ONCE(s->pcm);
  170. if (pcm)
  171. p->transfer_samples(s, pcm, buffer, data_blocks);
  172. else
  173. write_pcm_silence(s, buffer, data_blocks);
  174. return data_blocks;
  175. }
  176. int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
  177. enum amdtp_stream_direction dir, unsigned int pcm_channels)
  178. {
  179. amdtp_stream_process_data_blocks_t process_data_blocks;
  180. struct amdtp_tscm *p;
  181. unsigned int fmt;
  182. int err;
  183. if (dir == AMDTP_IN_STREAM) {
  184. fmt = AMDTP_FMT_TSCM_TX;
  185. process_data_blocks = process_tx_data_blocks;
  186. } else {
  187. fmt = AMDTP_FMT_TSCM_RX;
  188. process_data_blocks = process_rx_data_blocks;
  189. }
  190. err = amdtp_stream_init(s, unit, dir,
  191. CIP_NONBLOCKING | CIP_SKIP_DBC_ZERO_CHECK, fmt,
  192. process_data_blocks, sizeof(struct amdtp_tscm));
  193. if (err < 0)
  194. return 0;
  195. /* Use fixed value for FDF field. */
  196. s->fdf = 0x00;
  197. /* This protocol uses fixed number of data channels for PCM samples. */
  198. p = s->protocol;
  199. p->pcm_channels = pcm_channels;
  200. return 0;
  201. }