amdtp-am824.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. void (*transfer_samples)(struct amdtp_stream *s,
  33. struct snd_pcm_substream *pcm,
  34. __be32 *buffer, unsigned int frames);
  35. unsigned int frame_multiplier;
  36. };
  37. /**
  38. * amdtp_am824_set_parameters - set stream parameters
  39. * @s: the AMDTP stream to configure
  40. * @rate: the sample rate
  41. * @pcm_channels: the number of PCM samples in each data block, to be encoded
  42. * as AM824 multi-bit linear audio
  43. * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
  44. * @double_pcm_frames: one data block transfers two PCM frames
  45. *
  46. * The parameters must be set before the stream is started, and must not be
  47. * changed while the stream is running.
  48. */
  49. int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate,
  50. unsigned int pcm_channels,
  51. unsigned int midi_ports,
  52. bool double_pcm_frames)
  53. {
  54. struct amdtp_am824 *p = s->protocol;
  55. unsigned int midi_channels;
  56. unsigned int i;
  57. int err;
  58. if (amdtp_stream_running(s))
  59. return -EINVAL;
  60. if (pcm_channels > AM824_MAX_CHANNELS_FOR_PCM)
  61. return -EINVAL;
  62. midi_channels = DIV_ROUND_UP(midi_ports, 8);
  63. if (midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)
  64. return -EINVAL;
  65. if (WARN_ON(amdtp_stream_running(s)) ||
  66. WARN_ON(pcm_channels > AM824_MAX_CHANNELS_FOR_PCM) ||
  67. WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI))
  68. return -EINVAL;
  69. err = amdtp_stream_set_parameters(s, rate,
  70. pcm_channels + midi_channels);
  71. if (err < 0)
  72. return err;
  73. s->fdf = AMDTP_FDF_AM824 | s->sfc;
  74. p->pcm_channels = pcm_channels;
  75. p->midi_ports = midi_ports;
  76. /*
  77. * In IEC 61883-6, one data block represents one event. In ALSA, one
  78. * event equals to one PCM frame. But Dice has a quirk at higher
  79. * sampling rate to transfer two PCM frames in one data block.
  80. */
  81. if (double_pcm_frames)
  82. p->frame_multiplier = 2;
  83. else
  84. p->frame_multiplier = 1;
  85. /* init the position map for PCM and MIDI channels */
  86. for (i = 0; i < pcm_channels; i++)
  87. p->pcm_positions[i] = i;
  88. p->midi_position = p->pcm_channels;
  89. /*
  90. * We do not know the actual MIDI FIFO size of most devices. Just
  91. * assume two bytes, i.e., one byte can be received over the bus while
  92. * the previous one is transmitted over MIDI.
  93. * (The value here is adjusted for midi_ratelimit_per_packet().)
  94. */
  95. p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
  96. return 0;
  97. }
  98. EXPORT_SYMBOL_GPL(amdtp_am824_set_parameters);
  99. /**
  100. * amdtp_am824_set_pcm_position - set an index of data channel for a channel
  101. * of PCM frame
  102. * @s: the AMDTP stream
  103. * @index: the index of data channel in an data block
  104. * @position: the channel of PCM frame
  105. */
  106. void amdtp_am824_set_pcm_position(struct amdtp_stream *s, unsigned int index,
  107. unsigned int position)
  108. {
  109. struct amdtp_am824 *p = s->protocol;
  110. if (index < p->pcm_channels)
  111. p->pcm_positions[index] = position;
  112. }
  113. EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_position);
  114. /**
  115. * amdtp_am824_set_midi_position - set a index of data channel for MIDI
  116. * conformant data channel
  117. * @s: the AMDTP stream
  118. * @position: the index of data channel in an data block
  119. */
  120. void amdtp_am824_set_midi_position(struct amdtp_stream *s,
  121. unsigned int position)
  122. {
  123. struct amdtp_am824 *p = s->protocol;
  124. p->midi_position = position;
  125. }
  126. EXPORT_SYMBOL_GPL(amdtp_am824_set_midi_position);
  127. static void write_pcm_s32(struct amdtp_stream *s,
  128. struct snd_pcm_substream *pcm,
  129. __be32 *buffer, unsigned int frames)
  130. {
  131. struct amdtp_am824 *p = s->protocol;
  132. struct snd_pcm_runtime *runtime = pcm->runtime;
  133. unsigned int channels, remaining_frames, i, c;
  134. const u32 *src;
  135. channels = p->pcm_channels;
  136. src = (void *)runtime->dma_area +
  137. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  138. remaining_frames = runtime->buffer_size - s->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 write_pcm_s16(struct amdtp_stream *s,
  151. struct snd_pcm_substream *pcm,
  152. __be32 *buffer, unsigned int frames)
  153. {
  154. struct amdtp_am824 *p = s->protocol;
  155. struct snd_pcm_runtime *runtime = pcm->runtime;
  156. unsigned int channels, remaining_frames, i, c;
  157. const u16 *src;
  158. channels = p->pcm_channels;
  159. src = (void *)runtime->dma_area +
  160. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  161. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  162. for (i = 0; i < frames; ++i) {
  163. for (c = 0; c < channels; ++c) {
  164. buffer[p->pcm_positions[c]] =
  165. cpu_to_be32((*src << 8) | 0x42000000);
  166. src++;
  167. }
  168. buffer += s->data_block_quadlets;
  169. if (--remaining_frames == 0)
  170. src = (void *)runtime->dma_area;
  171. }
  172. }
  173. static void read_pcm_s32(struct amdtp_stream *s,
  174. struct snd_pcm_substream *pcm,
  175. __be32 *buffer, unsigned int frames)
  176. {
  177. struct amdtp_am824 *p = s->protocol;
  178. struct snd_pcm_runtime *runtime = pcm->runtime;
  179. unsigned int channels, remaining_frames, i, c;
  180. u32 *dst;
  181. channels = p->pcm_channels;
  182. dst = (void *)runtime->dma_area +
  183. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  184. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  185. for (i = 0; i < frames; ++i) {
  186. for (c = 0; c < channels; ++c) {
  187. *dst = be32_to_cpu(buffer[p->pcm_positions[c]]) << 8;
  188. dst++;
  189. }
  190. buffer += s->data_block_quadlets;
  191. if (--remaining_frames == 0)
  192. dst = (void *)runtime->dma_area;
  193. }
  194. }
  195. static void write_pcm_silence(struct amdtp_stream *s,
  196. __be32 *buffer, unsigned int frames)
  197. {
  198. struct amdtp_am824 *p = s->protocol;
  199. unsigned int i, c, channels = p->pcm_channels;
  200. for (i = 0; i < frames; ++i) {
  201. for (c = 0; c < channels; ++c)
  202. buffer[p->pcm_positions[c]] = cpu_to_be32(0x40000000);
  203. buffer += s->data_block_quadlets;
  204. }
  205. }
  206. /**
  207. * amdtp_am824_set_pcm_format - set the PCM format
  208. * @s: the AMDTP stream to configure
  209. * @format: the format of the ALSA PCM device
  210. *
  211. * The sample format must be set after the other parameters (rate/PCM channels/
  212. * MIDI) and before the stream is started, and must not be changed while the
  213. * stream is running.
  214. */
  215. void amdtp_am824_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
  216. {
  217. struct amdtp_am824 *p = s->protocol;
  218. if (WARN_ON(amdtp_stream_pcm_running(s)))
  219. return;
  220. switch (format) {
  221. default:
  222. WARN_ON(1);
  223. /* fall through */
  224. case SNDRV_PCM_FORMAT_S16:
  225. if (s->direction == AMDTP_OUT_STREAM) {
  226. p->transfer_samples = write_pcm_s16;
  227. break;
  228. }
  229. WARN_ON(1);
  230. /* fall through */
  231. case SNDRV_PCM_FORMAT_S32:
  232. if (s->direction == AMDTP_OUT_STREAM)
  233. p->transfer_samples = write_pcm_s32;
  234. else
  235. p->transfer_samples = read_pcm_s32;
  236. break;
  237. }
  238. }
  239. EXPORT_SYMBOL_GPL(amdtp_am824_set_pcm_format);
  240. /**
  241. * amdtp_am824_add_pcm_hw_constraints - add hw constraints for PCM substream
  242. * @s: the AMDTP stream for AM824 data block, must be initialized.
  243. * @runtime: the PCM substream runtime
  244. *
  245. */
  246. int amdtp_am824_add_pcm_hw_constraints(struct amdtp_stream *s,
  247. struct snd_pcm_runtime *runtime)
  248. {
  249. int err;
  250. err = amdtp_stream_add_pcm_hw_constraints(s, runtime);
  251. if (err < 0)
  252. return err;
  253. /* AM824 in IEC 61883-6 can deliver 24bit data. */
  254. return snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  255. }
  256. EXPORT_SYMBOL_GPL(amdtp_am824_add_pcm_hw_constraints);
  257. /**
  258. * amdtp_am824_midi_trigger - start/stop playback/capture with a MIDI device
  259. * @s: the AMDTP stream
  260. * @port: index of MIDI port
  261. * @midi: the MIDI device to be started, or %NULL to stop the current device
  262. *
  263. * Call this function on a running isochronous stream to enable the actual
  264. * transmission of MIDI data. This function should be called from the MIDI
  265. * device's .trigger callback.
  266. */
  267. void amdtp_am824_midi_trigger(struct amdtp_stream *s, unsigned int port,
  268. struct snd_rawmidi_substream *midi)
  269. {
  270. struct amdtp_am824 *p = s->protocol;
  271. if (port < p->midi_ports)
  272. ACCESS_ONCE(p->midi[port]) = midi;
  273. }
  274. EXPORT_SYMBOL_GPL(amdtp_am824_midi_trigger);
  275. /*
  276. * To avoid sending MIDI bytes at too high a rate, assume that the receiving
  277. * device has a FIFO, and track how much it is filled. This values increases
  278. * by one whenever we send one byte in a packet, but the FIFO empties at
  279. * a constant rate independent of our packet rate. One packet has syt_interval
  280. * samples, so the number of bytes that empty out of the FIFO, per packet(!),
  281. * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
  282. * fractional values, the values in midi_fifo_used[] are measured in bytes
  283. * multiplied by the sample rate.
  284. */
  285. static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
  286. {
  287. struct amdtp_am824 *p = s->protocol;
  288. int used;
  289. used = p->midi_fifo_used[port];
  290. if (used == 0) /* common shortcut */
  291. return true;
  292. used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
  293. used = max(used, 0);
  294. p->midi_fifo_used[port] = used;
  295. return used < p->midi_fifo_limit;
  296. }
  297. static void midi_rate_use_one_byte(struct amdtp_stream *s, unsigned int port)
  298. {
  299. struct amdtp_am824 *p = s->protocol;
  300. p->midi_fifo_used[port] += amdtp_rate_table[s->sfc];
  301. }
  302. static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  303. unsigned int frames)
  304. {
  305. struct amdtp_am824 *p = s->protocol;
  306. unsigned int f, port;
  307. u8 *b;
  308. for (f = 0; f < frames; f++) {
  309. b = (u8 *)&buffer[p->midi_position];
  310. port = (s->data_block_counter + f) % 8;
  311. if (f < MAX_MIDI_RX_BLOCKS &&
  312. midi_ratelimit_per_packet(s, port) &&
  313. p->midi[port] != NULL &&
  314. snd_rawmidi_transmit(p->midi[port], &b[1], 1) == 1) {
  315. midi_rate_use_one_byte(s, port);
  316. b[0] = 0x81;
  317. } else {
  318. b[0] = 0x80;
  319. b[1] = 0;
  320. }
  321. b[2] = 0;
  322. b[3] = 0;
  323. buffer += s->data_block_quadlets;
  324. }
  325. }
  326. static void read_midi_messages(struct amdtp_stream *s,
  327. __be32 *buffer, unsigned int frames)
  328. {
  329. struct amdtp_am824 *p = s->protocol;
  330. unsigned int f, port;
  331. int len;
  332. u8 *b;
  333. for (f = 0; f < frames; f++) {
  334. port = (s->data_block_counter + f) % 8;
  335. b = (u8 *)&buffer[p->midi_position];
  336. len = b[0] - 0x80;
  337. if ((1 <= len) && (len <= 3) && (p->midi[port]))
  338. snd_rawmidi_receive(p->midi[port], b + 1, len);
  339. buffer += s->data_block_quadlets;
  340. }
  341. }
  342. static unsigned int process_rx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
  343. unsigned int data_blocks, unsigned int *syt)
  344. {
  345. struct amdtp_am824 *p = s->protocol;
  346. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  347. unsigned int pcm_frames;
  348. if (pcm) {
  349. p->transfer_samples(s, pcm, buffer, data_blocks);
  350. pcm_frames = data_blocks * p->frame_multiplier;
  351. } else {
  352. write_pcm_silence(s, buffer, data_blocks);
  353. pcm_frames = 0;
  354. }
  355. if (p->midi_ports)
  356. write_midi_messages(s, buffer, data_blocks);
  357. return pcm_frames;
  358. }
  359. static unsigned int process_tx_data_blocks(struct amdtp_stream *s, __be32 *buffer,
  360. unsigned int data_blocks, unsigned int *syt)
  361. {
  362. struct amdtp_am824 *p = s->protocol;
  363. struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
  364. unsigned int pcm_frames;
  365. if (pcm) {
  366. p->transfer_samples(s, pcm, buffer, data_blocks);
  367. pcm_frames = data_blocks * p->frame_multiplier;
  368. } else {
  369. pcm_frames = 0;
  370. }
  371. if (p->midi_ports)
  372. read_midi_messages(s, buffer, data_blocks);
  373. return pcm_frames;
  374. }
  375. /**
  376. * amdtp_am824_init - initialize an AMDTP stream structure to handle AM824
  377. * data block
  378. * @s: the AMDTP stream to initialize
  379. * @unit: the target of the stream
  380. * @dir: the direction of stream
  381. * @flags: the packet transmission method to use
  382. */
  383. int amdtp_am824_init(struct amdtp_stream *s, struct fw_unit *unit,
  384. enum amdtp_stream_direction dir, enum cip_flags flags)
  385. {
  386. amdtp_stream_process_data_blocks_t process_data_blocks;
  387. if (dir == AMDTP_IN_STREAM)
  388. process_data_blocks = process_tx_data_blocks;
  389. else
  390. process_data_blocks = process_rx_data_blocks;
  391. return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
  392. process_data_blocks,
  393. sizeof(struct amdtp_am824));
  394. }
  395. EXPORT_SYMBOL_GPL(amdtp_am824_init);