amdtp-am824.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * AM824 format in Audio and Music Data Transmission Protocol (IEC 61883-6)
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Copyright (c) 2015 Takashi Sakamoto <o-takashi@sakamocchi.jp>
  6. *
  7. * Licensed under the terms of the GNU General Public License, version 2.
  8. */
  9. #include <linux/slab.h>
  10. #include "amdtp-am824.h"
  11. #define CIP_FMT_AM 0x10
  12. /* "Clock-based rate control mode" is just supported. */
  13. #define AMDTP_FDF_AM824 0x00
  14. /*
  15. * Nominally 3125 bytes/second, but the MIDI port's clock might be
  16. * 1% too slow, and the bus clock 100 ppm too fast.
  17. */
  18. #define MIDI_BYTES_PER_SECOND 3093
  19. /*
  20. * Several devices look only at the first eight data blocks.
  21. * In any case, this is more than enough for the MIDI data rate.
  22. */
  23. #define MAX_MIDI_RX_BLOCKS 8
  24. struct amdtp_am824 {
  25. struct snd_rawmidi_substream *midi[AM824_MAX_CHANNELS_FOR_MIDI * 8];
  26. int midi_fifo_limit;
  27. int midi_fifo_used[AM824_MAX_CHANNELS_FOR_MIDI * 8];
  28. unsigned int pcm_channels;
  29. unsigned int midi_ports;
  30. u8 pcm_positions[AM824_MAX_CHANNELS_FOR_PCM];
  31. u8 midi_position;
  32. unsigned int frame_multiplier;
  33. };
  34. /**
  35. * amdtp_am824_set_parameters - set stream parameters
  36. * @s: the AMDTP stream to configure
  37. * @rate: the sample rate
  38. * @pcm_channels: the number of PCM samples in each data block, to be encoded
  39. * as AM824 multi-bit linear audio
  40. * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
  41. * @double_pcm_frames: one data block transfers two PCM frames
  42. *
  43. * The parameters must be set before the stream is started, and must not be
  44. * changed while the stream is running.
  45. */
  46. int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate,
  47. unsigned int pcm_channels,
  48. unsigned int midi_ports,
  49. bool double_pcm_frames)
  50. {
  51. struct amdtp_am824 *p = s->protocol;
  52. unsigned int midi_channels;
  53. unsigned int i;
  54. int err;
  55. if (amdtp_stream_running(s))
  56. return -EINVAL;
  57. if (pcm_channels > AM824_MAX_CHANNELS_FOR_PCM)
  58. return -EINVAL;
  59. midi_channels = DIV_ROUND_UP(midi_ports, 8);
  60. if (midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)
  61. return -EINVAL;
  62. if (WARN_ON(amdtp_stream_running(s)) ||
  63. WARN_ON(pcm_channels > AM824_MAX_CHANNELS_FOR_PCM) ||
  64. WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI))
  65. return -EINVAL;
  66. err = amdtp_stream_set_parameters(s, rate,
  67. pcm_channels + midi_channels);
  68. if (err < 0)
  69. return err;
  70. s->fdf = AMDTP_FDF_AM824 | s->sfc;
  71. p->pcm_channels = pcm_channels;
  72. p->midi_ports = midi_ports;
  73. /*
  74. * In IEC 61883-6, one data block represents one event. In ALSA, one
  75. * event equals to one PCM frame. But Dice has a quirk at higher
  76. * sampling rate to transfer two PCM frames in one data block.
  77. */
  78. if (double_pcm_frames)
  79. p->frame_multiplier = 2;
  80. else
  81. p->frame_multiplier = 1;
  82. /* init the position map for PCM and MIDI channels */
  83. for (i = 0; i < pcm_channels; i++)
  84. p->pcm_positions[i] = i;
  85. p->midi_position = p->pcm_channels;
  86. /*
  87. * We do not know the actual MIDI FIFO size of most devices. Just
  88. * assume two bytes, i.e., one byte can be received over the bus while
  89. * the previous one is transmitted over MIDI.
  90. * (The value here is adjusted for midi_ratelimit_per_packet().)
  91. */
  92. p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
  93. return 0;
  94. }
  95. EXPORT_SYMBOL_GPL(amdtp_am824_set_parameters);
  96. /**
  97. * amdtp_am824_set_pcm_position - set an index of data channel for a channel
  98. * of PCM frame
  99. * @s: the AMDTP stream
  100. * @index: the index of data channel in an data block
  101. * @position: the channel of PCM frame
  102. */
  103. void amdtp_am824_set_pcm_position(struct amdtp_stream *s, unsigned int index,
  104. unsigned int position)
  105. {
  106. struct amdtp_am824 *p = s->protocol;
  107. if (index < p->pcm_channels)
  108. p->pcm_positions[index] = position;
  109. }
  110. EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_position);
  111. /**
  112. * amdtp_am824_set_midi_position - set a index of data channel for MIDI
  113. * conformant data channel
  114. * @s: the AMDTP stream
  115. * @position: the index of data channel in an data block
  116. */
  117. void amdtp_am824_set_midi_position(struct amdtp_stream *s,
  118. unsigned int position)
  119. {
  120. struct amdtp_am824 *p = s->protocol;
  121. p->midi_position = position;
  122. }
  123. EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
  124. static void write_pcm_s32(struct amdtp_stream *s,
  125. struct snd_pcm_substream *pcm,
  126. __be32 *buffer, unsigned int frames)
  127. {
  128. struct amdtp_am824 *p = s->protocol;
  129. struct snd_pcm_runtime *runtime = pcm->runtime;
  130. unsigned int channels, remaining_frames, i, c;
  131. const u32 *src;
  132. channels = p->pcm_channels;
  133. src = (void *)runtime->dma_area +
  134. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  135. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  136. for (i = 0; i < frames; ++i) {
  137. for (c = 0; c < channels; ++c) {
  138. buffer[p->pcm_positions[c]] =
  139. cpu_to_be32((*src >> 8) | 0x40000000);
  140. src++;
  141. }
  142. buffer += s->data_block_quadlets;
  143. if (--remaining_frames == 0)
  144. src = (void *)runtime->dma_area;
  145. }
  146. }
  147. static void read_pcm_s32(struct amdtp_stream *s,
  148. struct snd_pcm_substream *pcm,
  149. __be32 *buffer, unsigned int frames)
  150. {
  151. struct amdtp_am824 *p = s->protocol;
  152. struct snd_pcm_runtime *runtime = pcm->runtime;
  153. unsigned int channels, remaining_frames, i, c;
  154. u32 *dst;
  155. channels = p->pcm_channels;
  156. dst = (void *)runtime->dma_area +
  157. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  158. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  159. for (i = 0; i < frames; ++i) {
  160. for (c = 0; c < channels; ++c) {
  161. *dst = be32_to_cpu(buffer[p->pcm_positions[c]]) << 8;
  162. dst++;
  163. }
  164. buffer += s->data_block_quadlets;
  165. if (--remaining_frames == 0)
  166. dst = (void *)runtime->dma_area;
  167. }
  168. }
  169. static void write_pcm_silence(struct amdtp_stream *s,
  170. __be32 *buffer, unsigned int frames)
  171. {
  172. struct amdtp_am824 *p = s->protocol;
  173. unsigned int i, c, channels = p->pcm_channels;
  174. for (i = 0; i < frames; ++i) {
  175. for (c = 0; c < channels; ++c)
  176. buffer[p->pcm_positions[c]] = cpu_to_be32(0x40000000);
  177. buffer += s->data_block_quadlets;
  178. }
  179. }
  180. /**
  181. * amdtp_am824_add_pcm_hw_constraints - add hw constraints for PCM substream
  182. * @s: the AMDTP stream for AM824 data block, must be initialized.
  183. * @runtime: the PCM substream runtime
  184. *
  185. */
  186. int amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream *s,
  187. struct snd_pcm_runtime *runtime)
  188. {
  189. int err;
  190. err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
  191. if (err < 0)
  192. return err;
  193. /* AM824 in IEC 61883-6 can deliver 24bit data. */
  194. return snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  195. }
  196. EXPORT_SYMBOL_GPL(amdtp_am824_add_pcm_hw_constraints);
  197. /**
  198. * amdtp_am824_midi_trigger - start/stop playback/capture with a MIDI device
  199. * @s: the AMDTP stream
  200. * @port: index of MIDI port
  201. * @midi: the MIDI device to be started, or %NULL to stop the current device
  202. *
  203. * Call this function on a running isochronous stream to enable the actual
  204. * transmission of MIDI data. This function should be called from the MIDI
  205. * device's .trigger callback.
  206. */
  207. void amdtp_am824_midi_trigger(struct amdtp_stream *s, unsigned int port,
  208. struct snd_rawmidi_substream *midi)
  209. {
  210. struct amdtp_am824 *p = s->protocol;
  211. if (port < p->midi_ports)
  212. ACCESS_ONCE(p->midi[port]) = midi;
  213. }
  214. EXPORT_SYMBOL_GPL(amdtp_am824_midi_trigger);
  215. /*
  216. * To avoid sending MIDI bytes at too high a rate, assume that the receiving
  217. * device has a FIFO, and track how much it is filled. This values increases
  218. * by one whenever we send one byte in a packet, but the FIFO empties at
  219. * a constant rate independent of our packet rate. One packet has syt_interval
  220. * samples, so the number of bytes that empty out of the FIFO, per packet(!),
  221. * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
  222. * fractional values, the values in midi_fifo_used[] are measured in bytes
  223. * multiplied by the sample rate.
  224. */
  225. static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
  226. {
  227. struct amdtp_am824 *p = s->protocol;
  228. int used;
  229. used = p->midi_fifo_used[port];
  230. if (used == 0) /* common shortcut */
  231. return true;
  232. used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
  233. used = max(used, 0);
  234. p->midi_fifo_used[port] = used;
  235. return used < p->midi_fifo_limit;
  236. }
  237. static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
  238. {
  239. struct amdtp_am824 *p = s->protocol;
  240. p->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
  241. }
  242. static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  243. unsigned int frames)
  244. {
  245. struct amdtp_am824 *p = s->protocol;
  246. unsigned int f, port;
  247. u8 *b;
  248. for (f = 0; f < frames; f++) {
  249. b = (u8 *)&buffer[p->midi_position];
  250. port = (s->data_block_counter + f) % 8;
  251. if (f < MAX_MIDI_RX_BLOCKS &&
  252. midi_ratelimit_per_packet(s, port) &&
  253. p->midi[port] != NULL &&
  254. snd_rawmidi_transmit(p->midi[port], &b[1], 1) == 1) {
  255. midi_rate_use_one_byte(s, port);
  256. b[0] = 0x81;
  257. } else {
  258. b[0] = 0x80;
  259. b[1] = 0;
  260. }
  261. b[2] = 0;
  262. b[3] = 0;
  263. buffer += s->data_block_quadlets;
  264. }
  265. }
  266. static void read_midi_messages(struct amdtp_stream *s,
  267. __be32 *buffer, unsigned int frames)
  268. {
  269. struct amdtp_am824 *p = s->protocol;
  270. unsigned int f, port;
  271. int len;
  272. u8 *b;
  273. for (f = 0; f < frames; f++) {
  274. port = (8 - s->tx_first_dbc + s->data_block_counter + f) % 8;
  275. b = (u8 *)&buffer[p->midi_position];
  276. len = b[0] - 0x80;
  277. if ((1 <= len) && (len <= 3) && (p->midi[port]))
  278. snd_rawmidi_receive(p->midi[port], b + 1, len);
  279. buffer += s->data_block_quadlets;
  280. }
  281. }
  282. static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
  283. unsigned int data_blocks, unsigned int *syt)
  284. {
  285. struct amdtp_am824 *p = s->protocol;
  286. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  287. unsigned int pcm_frames;
  288. if (pcm) {
  289. write_pcm_s32(s, pcm, buffer, data_blocks);
  290. pcm_frames = data_blocks * p->frame_multiplier;
  291. } else {
  292. write_pcm_silence(s, buffer, data_blocks);
  293. pcm_frames = 0;
  294. }
  295. if (p->midi_ports)
  296. write_midi_messages(s, buffer, data_blocks);
  297. return pcm_frames;
  298. }
  299. static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
  300. unsigned int data_blocks, unsigned int *syt)
  301. {
  302. struct amdtp_am824 *p = s->protocol;
  303. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  304. unsigned int pcm_frames;
  305. if (pcm) {
  306. read_pcm_s32(s, pcm, buffer, data_blocks);
  307. pcm_frames = data_blocks * p->frame_multiplier;
  308. } else {
  309. pcm_frames = 0;
  310. }
  311. if (p->midi_ports)
  312. read_midi_messages(s, buffer, data_blocks);
  313. return pcm_frames;
  314. }
  315. /**
  316. * amdtp_am824_init - initialize an AMDTP stream structure to handle AM824
  317. * data block
  318. * @s: the AMDTP stream to initialize
  319. * @unit: the target of the stream
  320. * @dir: the direction of stream
  321. * @flags: the packet transmission method to use
  322. */
  323. int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
  324. enum amdtp_stream_direction dir, enum cip_flags flags)
  325. {
  326. amdtp_stream_process_data_blocks_t process_data_blocks;
  327. if (dir == AMDTP_IN_STREAM)
  328. process_data_blocks = process_tx_data_blocks;
  329. else
  330. process_data_blocks = process_rx_data_blocks;
  331. return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
  332. process_data_blocks,
  333. sizeof(struct amdtp_am824));
  334. }
  335. EXPORT_SYMBOL_GPL(amdtp_am824_init);