amdtp-tascam.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. };
  15. int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate)
  16. {
  17. struct amdtp_tscm *p = s->protocol;
  18. unsigned int data_channels;
  19. if (amdtp_stream_running(s))
  20. return -EBUSY;
  21. data_channels = p->pcm_channels;
  22. /* Packets in in-stream have extra 2 data channels. */
  23. if (s->direction == AMDTP_IN_STREAM)
  24. data_channels += 2;
  25. return amdtp_stream_set_parameters(s, rate, data_channels);
  26. }
  27. static void write_pcm_s32(struct amdtp_stream *s,
  28. struct snd_pcm_substream *pcm,
  29. __be32 *buffer, unsigned int frames)
  30. {
  31. struct amdtp_tscm *p = s->protocol;
  32. struct snd_pcm_runtime *runtime = pcm->runtime;
  33. unsigned int channels, remaining_frames, i, c;
  34. const u32 *src;
  35. channels = p->pcm_channels;
  36. src = (void *)runtime->dma_area +
  37. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  38. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  39. for (i = 0; i < frames; ++i) {
  40. for (c = 0; c < channels; ++c) {
  41. buffer[c] = cpu_to_be32(*src);
  42. src++;
  43. }
  44. buffer += s->data_block_quadlets;
  45. if (--remaining_frames == 0)
  46. src = (void *)runtime->dma_area;
  47. }
  48. }
  49. static void read_pcm_s32(struct amdtp_stream *s,
  50. struct snd_pcm_substream *pcm,
  51. __be32 *buffer, unsigned int frames)
  52. {
  53. struct amdtp_tscm *p = s->protocol;
  54. struct snd_pcm_runtime *runtime = pcm->runtime;
  55. unsigned int channels, remaining_frames, i, c;
  56. u32 *dst;
  57. channels = p->pcm_channels;
  58. dst = (void *)runtime->dma_area +
  59. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  60. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  61. /* The first data channel is for event counter. */
  62. buffer += 1;
  63. for (i = 0; i < frames; ++i) {
  64. for (c = 0; c < channels; ++c) {
  65. *dst = be32_to_cpu(buffer[c]);
  66. dst++;
  67. }
  68. buffer += s->data_block_quadlets;
  69. if (--remaining_frames == 0)
  70. dst = (void *)runtime->dma_area;
  71. }
  72. }
  73. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  74. unsigned int data_blocks)
  75. {
  76. struct amdtp_tscm *p = s->protocol;
  77. unsigned int channels, i, c;
  78. channels = p->pcm_channels;
  79. for (i = 0; i < data_blocks; ++i) {
  80. for (c = 0; c < channels; ++c)
  81. buffer[c] = 0x00000000;
  82. buffer += s->data_block_quadlets;
  83. }
  84. }
  85. int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
  86. struct snd_pcm_runtime *runtime)
  87. {
  88. int err;
  89. /*
  90. * Our implementation allows this protocol to deliver 24 bit sample in
  91. * 32bit data channel.
  92. */
  93. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  94. if (err < 0)
  95. return err;
  96. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  97. }
  98. static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
  99. __be32 *buffer,
  100. unsigned int data_blocks,
  101. unsigned int *syt)
  102. {
  103. struct snd_pcm_substream *pcm;
  104. pcm = READ_ONCE(s->pcm);
  105. if (data_blocks > 0 && pcm)
  106. read_pcm_s32(s, pcm, buffer, data_blocks);
  107. /* A place holder for control messages. */
  108. return data_blocks;
  109. }
  110. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  111. __be32 *buffer,
  112. unsigned int data_blocks,
  113. unsigned int *syt)
  114. {
  115. struct snd_pcm_substream *pcm;
  116. /* This field is not used. */
  117. *syt = 0x0000;
  118. pcm = READ_ONCE(s->pcm);
  119. if (pcm)
  120. write_pcm_s32(s, pcm, buffer, data_blocks);
  121. else
  122. write_pcm_silence(s, buffer, data_blocks);
  123. return data_blocks;
  124. }
  125. int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
  126. enum amdtp_stream_direction dir, unsigned int pcm_channels)
  127. {
  128. amdtp_stream_process_data_blocks_t process_data_blocks;
  129. struct amdtp_tscm *p;
  130. unsigned int fmt;
  131. int err;
  132. if (dir == AMDTP_IN_STREAM) {
  133. fmt = AMDTP_FMT_TSCM_TX;
  134. process_data_blocks = process_tx_data_blocks;
  135. } else {
  136. fmt = AMDTP_FMT_TSCM_RX;
  137. process_data_blocks = process_rx_data_blocks;
  138. }
  139. err = amdtp_stream_init(s, unit, dir,
  140. CIP_NONBLOCKING | CIP_SKIP_DBC_ZERO_CHECK, fmt,
  141. process_data_blocks, sizeof(struct amdtp_tscm));
  142. if (err < 0)
  143. return 0;
  144. /* Use fixed value for FDF field. */
  145. s->fdf = 0x00;
  146. /* This protocol uses fixed number of data channels for PCM samples. */
  147. p = s->protocol;
  148. p->pcm_channels = pcm_channels;
  149. return 0;
  150. }