amdtp-am824.c 12 KB

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