amdtp-tascam.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * amdtp-tascam.c - a part of driver for TASCAM FireWire series
  4. *
  5. * Copyright (c) 2015 Takashi Sakamoto
  6. */
  7. #include <sound/pcm.h>
  8. #include "tascam.h"
  9. #define AMDTP_FMT_TSCM_TX 0x1e
  10. #define AMDTP_FMT_TSCM_RX 0x3e
  11. struct amdtp_tscm {
  12. unsigned int pcm_channels;
  13. };
  14. int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate)
  15. {
  16. struct amdtp_tscm *p = s->protocol;
  17. unsigned int data_channels;
  18. if (amdtp_stream_running(s))
  19. return -EBUSY;
  20. data_channels = p->pcm_channels;
  21. /* Packets in in-stream have extra 2 data channels. */
  22. if (s->direction == AMDTP_IN_STREAM)
  23. data_channels += 2;
  24. return amdtp_stream_set_parameters(s, rate, data_channels, 1);
  25. }
  26. static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  27. __be32 *buffer, unsigned int frames,
  28. unsigned int pcm_frames)
  29. {
  30. struct amdtp_tscm *p = s->protocol;
  31. unsigned int channels = p->pcm_channels;
  32. struct snd_pcm_runtime *runtime = pcm->runtime;
  33. unsigned int pcm_buffer_pointer;
  34. int remaining_frames;
  35. const u32 *src;
  36. int i, c;
  37. pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
  38. pcm_buffer_pointer %= runtime->buffer_size;
  39. src = (void *)runtime->dma_area +
  40. frames_to_bytes(runtime, pcm_buffer_pointer);
  41. remaining_frames = runtime->buffer_size - 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 read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  53. __be32 *buffer, unsigned int frames,
  54. unsigned int pcm_frames)
  55. {
  56. struct amdtp_tscm *p = s->protocol;
  57. unsigned int channels = p->pcm_channels;
  58. struct snd_pcm_runtime *runtime = pcm->runtime;
  59. unsigned int pcm_buffer_pointer;
  60. int remaining_frames;
  61. u32 *dst;
  62. int i, c;
  63. pcm_buffer_pointer = s->pcm_buffer_pointer + pcm_frames;
  64. pcm_buffer_pointer %= runtime->buffer_size;
  65. dst = (void *)runtime->dma_area +
  66. frames_to_bytes(runtime, pcm_buffer_pointer);
  67. remaining_frames = runtime->buffer_size - pcm_buffer_pointer;
  68. /* The first data channel is for event counter. */
  69. buffer += 1;
  70. for (i = 0; i < frames; ++i) {
  71. for (c = 0; c < channels; ++c) {
  72. *dst = be32_to_cpu(buffer[c]);
  73. dst++;
  74. }
  75. buffer += s->data_block_quadlets;
  76. if (--remaining_frames == 0)
  77. dst = (void *)runtime->dma_area;
  78. }
  79. }
  80. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  81. unsigned int data_blocks)
  82. {
  83. struct amdtp_tscm *p = s->protocol;
  84. unsigned int channels, i, c;
  85. channels = p->pcm_channels;
  86. for (i = 0; i < data_blocks; ++i) {
  87. for (c = 0; c < channels; ++c)
  88. buffer[c] = 0x00000000;
  89. buffer += s->data_block_quadlets;
  90. }
  91. }
  92. int amdtp_tscm_add_pcm_hw_constraints(struct amdtp_stream *s,
  93. struct snd_pcm_runtime *runtime)
  94. {
  95. int err;
  96. /*
  97. * Our implementation allows this protocol to deliver 24 bit sample in
  98. * 32bit data channel.
  99. */
  100. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  101. if (err < 0)
  102. return err;
  103. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  104. }
  105. static void read_status_messages(struct amdtp_stream *s,
  106. __be32 *buffer, unsigned int data_blocks)
  107. {
  108. struct snd_tscm *tscm = container_of(s, struct snd_tscm, tx_stream);
  109. bool used = READ_ONCE(tscm->hwdep->used);
  110. int i;
  111. for (i = 0; i < data_blocks; i++) {
  112. unsigned int index;
  113. __be32 before;
  114. __be32 after;
  115. index = be32_to_cpu(buffer[0]) % SNDRV_FIREWIRE_TASCAM_STATE_COUNT;
  116. before = tscm->state[index];
  117. after = buffer[s->data_block_quadlets - 1];
  118. if (used && index > 4 && index < 16) {
  119. __be32 mask;
  120. if (index == 5)
  121. mask = cpu_to_be32(~0x0000ffff);
  122. else if (index == 6)
  123. mask = cpu_to_be32(~0x0000ffff);
  124. else if (index == 8)
  125. mask = cpu_to_be32(~0x000f0f00);
  126. else
  127. mask = cpu_to_be32(~0x00000000);
  128. if ((before ^ after) & mask) {
  129. struct snd_firewire_tascam_change *entry =
  130. &tscm->queue[tscm->push_pos];
  131. unsigned long flag;
  132. spin_lock_irqsave(&tscm->lock, flag);
  133. entry->index = index;
  134. entry->before = before;
  135. entry->after = after;
  136. if (++tscm->push_pos >= SND_TSCM_QUEUE_COUNT)
  137. tscm->push_pos = 0;
  138. spin_unlock_irqrestore(&tscm->lock, flag);
  139. wake_up(&tscm->hwdep_wait);
  140. }
  141. }
  142. tscm->state[index] = after;
  143. buffer += s->data_block_quadlets;
  144. }
  145. }
  146. static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc,
  147. unsigned int count, struct snd_pcm_substream *pcm)
  148. {
  149. unsigned int pcm_frames = 0;
  150. int i;
  151. for (i = 0; i < count; ++i) {
  152. __be32 *buf = desc->ctx_payload;
  153. unsigned int data_blocks = desc->data_blocks;
  154. if (pcm) {
  155. read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
  156. pcm_frames += data_blocks;
  157. }
  158. read_status_messages(s, buf, data_blocks);
  159. desc = amdtp_stream_next_packet_desc(s, desc);
  160. }
  161. }
  162. static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc,
  163. unsigned int count, struct snd_pcm_substream *pcm)
  164. {
  165. unsigned int pcm_frames = 0;
  166. int i;
  167. for (i = 0; i < count; ++i) {
  168. __be32 *buf = desc->ctx_payload;
  169. unsigned int data_blocks = desc->data_blocks;
  170. if (pcm) {
  171. write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames);
  172. pcm_frames += data_blocks;
  173. } else {
  174. write_pcm_silence(s, buf, data_blocks);
  175. }
  176. desc = amdtp_stream_next_packet_desc(s, desc);
  177. }
  178. }
  179. int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
  180. enum amdtp_stream_direction dir, unsigned int pcm_channels)
  181. {
  182. amdtp_stream_process_ctx_payloads_t process_ctx_payloads;
  183. unsigned int flags = CIP_NONBLOCKING | CIP_SKIP_DBC_ZERO_CHECK | CIP_UNAWARE_SYT;
  184. struct amdtp_tscm *p;
  185. unsigned int fmt;
  186. int err;
  187. if (dir == AMDTP_IN_STREAM) {
  188. fmt = AMDTP_FMT_TSCM_TX;
  189. process_ctx_payloads = process_ir_ctx_payloads;
  190. } else {
  191. fmt = AMDTP_FMT_TSCM_RX;
  192. process_ctx_payloads = process_it_ctx_payloads;
  193. }
  194. err = amdtp_stream_init(s, unit, dir, flags, fmt,
  195. process_ctx_payloads, sizeof(struct amdtp_tscm));
  196. if (err < 0)
  197. return err;
  198. if (dir == AMDTP_OUT_STREAM) {
  199. // Use fixed value for FDF field.
  200. s->ctx_data.rx.fdf = 0x00;
  201. }
  202. /* This protocol uses fixed number of data channels for PCM samples. */
  203. p = s->protocol;
  204. p->pcm_channels = pcm_channels;
  205. return 0;
  206. }