amdtp-motu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * amdtp-motu.c - a part of driver for MOTU FireWire series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <linux/slab.h>
  9. #include <sound/pcm.h>
  10. #include "motu.h"
  11. #define CREATE_TRACE_POINTS
  12. #include "amdtp-motu-trace.h"
  13. #define CIP_FMT_MOTU 0x02
  14. #define CIP_FMT_MOTU_TX_V3 0x22
  15. #define MOTU_FDF_AM824 0x22
  16. /*
  17. * Nominally 3125 bytes/second, but the MIDI port's clock might be
  18. * 1% too slow, and the bus clock 100 ppm too fast.
  19. */
  20. #define MIDI_BYTES_PER_SECOND 3093
  21. struct amdtp_motu {
  22. /* For timestamp processing. */
  23. unsigned int quotient_ticks_per_event;
  24. unsigned int remainder_ticks_per_event;
  25. unsigned int next_ticks;
  26. unsigned int next_accumulated;
  27. unsigned int next_cycles;
  28. unsigned int next_seconds;
  29. unsigned int pcm_chunks;
  30. unsigned int pcm_byte_offset;
  31. struct snd_rawmidi_substream *midi;
  32. unsigned int midi_ports;
  33. unsigned int midi_flag_offset;
  34. unsigned int midi_byte_offset;
  35. int midi_db_count;
  36. unsigned int midi_db_interval;
  37. };
  38. int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate,
  39. unsigned int midi_ports,
  40. struct snd_motu_packet_format *formats)
  41. {
  42. static const struct {
  43. unsigned int quotient_ticks_per_event;
  44. unsigned int remainder_ticks_per_event;
  45. } params[] = {
  46. [CIP_SFC_44100] = { 557, 123 },
  47. [CIP_SFC_48000] = { 512, 0 },
  48. [CIP_SFC_88200] = { 278, 282 },
  49. [CIP_SFC_96000] = { 256, 0 },
  50. [CIP_SFC_176400] = { 139, 141 },
  51. [CIP_SFC_192000] = { 128, 0 },
  52. };
  53. struct amdtp_motu *p = s->protocol;
  54. unsigned int pcm_chunks, data_chunks, data_block_quadlets;
  55. unsigned int delay;
  56. unsigned int mode;
  57. int i, err;
  58. if (amdtp_stream_running(s))
  59. return -EBUSY;
  60. for (i = 0; i < ARRAY_SIZE(snd_motu_clock_rates); ++i) {
  61. if (snd_motu_clock_rates[i] == rate) {
  62. mode = i >> 1;
  63. break;
  64. }
  65. }
  66. if (i == ARRAY_SIZE(snd_motu_clock_rates))
  67. return -EINVAL;
  68. pcm_chunks = formats->fixed_part_pcm_chunks[mode] +
  69. formats->differed_part_pcm_chunks[mode];
  70. data_chunks = formats->msg_chunks + pcm_chunks;
  71. /*
  72. * Each data block includes SPH in its head. Data chunks follow with
  73. * 3 byte alignment. Padding follows with zero to conform to quadlet
  74. * alignment.
  75. */
  76. data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
  77. err = amdtp_stream_set_parameters(s, rate, data_block_quadlets);
  78. if (err < 0)
  79. return err;
  80. p->pcm_chunks = pcm_chunks;
  81. p->pcm_byte_offset = formats->pcm_byte_offset;
  82. p->midi_ports = midi_ports;
  83. p->midi_flag_offset = formats->midi_flag_offset;
  84. p->midi_byte_offset = formats->midi_byte_offset;
  85. p->midi_db_count = 0;
  86. p->midi_db_interval = rate / MIDI_BYTES_PER_SECOND;
  87. /* IEEE 1394 bus requires. */
  88. delay = 0x2e00;
  89. /* For no-data or empty packets to adjust PCM sampling frequency. */
  90. delay += 8000 * 3072 * s->syt_interval / rate;
  91. p->next_seconds = 0;
  92. p->next_cycles = delay / 3072;
  93. p->quotient_ticks_per_event = params[s->sfc].quotient_ticks_per_event;
  94. p->remainder_ticks_per_event = params[s->sfc].remainder_ticks_per_event;
  95. p->next_ticks = delay % 3072;
  96. p->next_accumulated = 0;
  97. return 0;
  98. }
  99. static void read_pcm_s32(struct amdtp_stream *s,
  100. struct snd_pcm_runtime *runtime,
  101. __be32 *buffer, unsigned int data_blocks)
  102. {
  103. struct amdtp_motu *p = s->protocol;
  104. unsigned int channels, remaining_frames, i, c;
  105. u8 *byte;
  106. u32 *dst;
  107. channels = p->pcm_chunks;
  108. dst = (void *)runtime->dma_area +
  109. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  110. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  111. for (i = 0; i < data_blocks; ++i) {
  112. byte = (u8 *)buffer + p->pcm_byte_offset;
  113. for (c = 0; c < channels; ++c) {
  114. *dst = (byte[0] << 24) |
  115. (byte[1] << 16) |
  116. (byte[2] << 8);
  117. byte += 3;
  118. dst++;
  119. }
  120. buffer += s->data_block_quadlets;
  121. if (--remaining_frames == 0)
  122. dst = (void *)runtime->dma_area;
  123. }
  124. }
  125. static void write_pcm_s32(struct amdtp_stream *s,
  126. struct snd_pcm_runtime *runtime,
  127. __be32 *buffer, unsigned int data_blocks)
  128. {
  129. struct amdtp_motu *p = s->protocol;
  130. unsigned int channels, remaining_frames, i, c;
  131. u8 *byte;
  132. const u32 *src;
  133. channels = p->pcm_chunks;
  134. src = (void *)runtime->dma_area +
  135. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  136. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  137. for (i = 0; i < data_blocks; ++i) {
  138. byte = (u8 *)buffer + p->pcm_byte_offset;
  139. for (c = 0; c < channels; ++c) {
  140. byte[0] = (*src >> 24) & 0xff;
  141. byte[1] = (*src >> 16) & 0xff;
  142. byte[2] = (*src >> 8) & 0xff;
  143. byte += 3;
  144. src++;
  145. }
  146. buffer += s->data_block_quadlets;
  147. if (--remaining_frames == 0)
  148. src = (void *)runtime->dma_area;
  149. }
  150. }
  151. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  152. unsigned int data_blocks)
  153. {
  154. struct amdtp_motu *p = s->protocol;
  155. unsigned int channels, i, c;
  156. u8 *byte;
  157. channels = p->pcm_chunks;
  158. for (i = 0; i < data_blocks; ++i) {
  159. byte = (u8 *)buffer + p->pcm_byte_offset;
  160. for (c = 0; c < channels; ++c) {
  161. byte[0] = 0;
  162. byte[1] = 0;
  163. byte[2] = 0;
  164. byte += 3;
  165. }
  166. buffer += s->data_block_quadlets;
  167. }
  168. }
  169. int amdtp_motu_add_pcm_hw_constraints(struct amdtp_stream *s,
  170. struct snd_pcm_runtime *runtime)
  171. {
  172. int err;
  173. /* TODO: how to set an constraint for exactly 24bit PCM sample? */
  174. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  175. if (err < 0)
  176. return err;
  177. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  178. }
  179. void amdtp_motu_midi_trigger(struct amdtp_stream *s, unsigned int port,
  180. struct snd_rawmidi_substream *midi)
  181. {
  182. struct amdtp_motu *p = s->protocol;
  183. if (port < p->midi_ports)
  184. WRITE_ONCE(p->midi, midi);
  185. }
  186. static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  187. unsigned int data_blocks)
  188. {
  189. struct amdtp_motu *p = s->protocol;
  190. struct snd_rawmidi_substream *midi = READ_ONCE(p->midi);
  191. u8 *b;
  192. int i;
  193. for (i = 0; i < data_blocks; i++) {
  194. b = (u8 *)buffer;
  195. if (midi && p->midi_db_count == 0 &&
  196. snd_rawmidi_transmit(midi, b + p->midi_byte_offset, 1) == 1) {
  197. b[p->midi_flag_offset] = 0x01;
  198. } else {
  199. b[p->midi_byte_offset] = 0x00;
  200. b[p->midi_flag_offset] = 0x00;
  201. }
  202. buffer += s->data_block_quadlets;
  203. if (--p->midi_db_count < 0)
  204. p->midi_db_count = p->midi_db_interval;
  205. }
  206. }
  207. static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  208. unsigned int data_blocks)
  209. {
  210. struct amdtp_motu *p = s->protocol;
  211. struct snd_rawmidi_substream *midi;
  212. u8 *b;
  213. int i;
  214. for (i = 0; i < data_blocks; i++) {
  215. b = (u8 *)buffer;
  216. midi = READ_ONCE(p->midi);
  217. if (midi && (b[p->midi_flag_offset] & 0x01))
  218. snd_rawmidi_receive(midi, b + p->midi_byte_offset, 1);
  219. buffer += s->data_block_quadlets;
  220. }
  221. }
  222. /* For tracepoints. */
  223. static void __maybe_unused copy_sph(u32 *frames, __be32 *buffer,
  224. unsigned int data_blocks,
  225. unsigned int data_block_quadlets)
  226. {
  227. unsigned int i;
  228. for (i = 0; i < data_blocks; ++i) {
  229. *frames = be32_to_cpu(*buffer);
  230. buffer += data_block_quadlets;
  231. frames++;
  232. }
  233. }
  234. /* For tracepoints. */
  235. static void __maybe_unused copy_message(u64 *frames, __be32 *buffer,
  236. unsigned int data_blocks,
  237. unsigned int data_block_quadlets)
  238. {
  239. unsigned int i;
  240. /* This is just for v2/v3 protocol. */
  241. for (i = 0; i < data_blocks; ++i) {
  242. *frames = (be32_to_cpu(buffer[1]) << 16) |
  243. (be32_to_cpu(buffer[2]) >> 16);
  244. buffer += data_block_quadlets;
  245. frames++;
  246. }
  247. }
  248. static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
  249. __be32 *buffer, unsigned int data_blocks,
  250. unsigned int *syt)
  251. {
  252. struct amdtp_motu *p = s->protocol;
  253. struct snd_pcm_substream *pcm;
  254. trace_in_data_block_sph(s, data_blocks, buffer);
  255. trace_in_data_block_message(s, data_blocks, buffer);
  256. if (p->midi_ports)
  257. read_midi_messages(s, buffer, data_blocks);
  258. pcm = ACCESS_ONCE(s->pcm);
  259. if (data_blocks > 0 && pcm)
  260. read_pcm_s32(s, pcm->runtime, buffer, data_blocks);
  261. return data_blocks;
  262. }
  263. static inline void compute_next_elapse_from_start(struct amdtp_motu *p)
  264. {
  265. p->next_accumulated += p->remainder_ticks_per_event;
  266. if (p->next_accumulated >= 441) {
  267. p->next_accumulated -= 441;
  268. p->next_ticks++;
  269. }
  270. p->next_ticks += p->quotient_ticks_per_event;
  271. if (p->next_ticks >= 3072) {
  272. p->next_ticks -= 3072;
  273. p->next_cycles++;
  274. }
  275. if (p->next_cycles >= 8000) {
  276. p->next_cycles -= 8000;
  277. p->next_seconds++;
  278. }
  279. if (p->next_seconds >= 128)
  280. p->next_seconds -= 128;
  281. }
  282. static void write_sph(struct amdtp_stream *s, __be32 *buffer,
  283. unsigned int data_blocks)
  284. {
  285. struct amdtp_motu *p = s->protocol;
  286. unsigned int next_cycles;
  287. unsigned int i;
  288. u32 sph;
  289. for (i = 0; i < data_blocks; i++) {
  290. next_cycles = (s->start_cycle + p->next_cycles) % 8000;
  291. sph = ((next_cycles << 12) | p->next_ticks) & 0x01ffffff;
  292. *buffer = cpu_to_be32(sph);
  293. compute_next_elapse_from_start(p);
  294. buffer += s->data_block_quadlets;
  295. }
  296. }
  297. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  298. __be32 *buffer, unsigned int data_blocks,
  299. unsigned int *syt)
  300. {
  301. struct amdtp_motu *p = (struct amdtp_motu *)s->protocol;
  302. struct snd_pcm_substream *pcm;
  303. /* Not used. */
  304. *syt = 0xffff;
  305. /* TODO: how to interact control messages between userspace? */
  306. if (p->midi_ports)
  307. write_midi_messages(s, buffer, data_blocks);
  308. pcm = ACCESS_ONCE(s->pcm);
  309. if (pcm)
  310. write_pcm_s32(s, pcm->runtime, buffer, data_blocks);
  311. else
  312. write_pcm_silence(s, buffer, data_blocks);
  313. write_sph(s, buffer, data_blocks);
  314. trace_out_data_block_sph(s, data_blocks, buffer);
  315. trace_out_data_block_message(s, data_blocks, buffer);
  316. return data_blocks;
  317. }
  318. int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit,
  319. enum amdtp_stream_direction dir,
  320. const struct snd_motu_protocol *const protocol)
  321. {
  322. amdtp_stream_process_data_blocks_t process_data_blocks;
  323. int fmt = CIP_FMT_MOTU;
  324. int flags = CIP_BLOCKING;
  325. int err;
  326. if (dir == AMDTP_IN_STREAM) {
  327. process_data_blocks = process_tx_data_blocks;
  328. /*
  329. * Units of version 3 transmits packets with invalid CIP header
  330. * against IEC 61883-1.
  331. */
  332. if (protocol == &snd_motu_protocol_v3) {
  333. flags |= CIP_WRONG_DBS |
  334. CIP_SKIP_DBC_ZERO_CHECK |
  335. CIP_HEADER_WITHOUT_EOH;
  336. fmt = CIP_FMT_MOTU_TX_V3;
  337. }
  338. } else {
  339. process_data_blocks = process_rx_data_blocks;
  340. flags |= CIP_DBC_IS_END_EVENT;
  341. }
  342. err = amdtp_stream_init(s, unit, dir, flags, fmt, process_data_blocks,
  343. sizeof(struct amdtp_motu));
  344. if (err < 0)
  345. return err;
  346. s->sph = 1;
  347. s->fdf = MOTU_FDF_AM824;
  348. return 0;
  349. }