amdtp-dot.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * amdtp-dot.c - a part of driver for Digidesign Digi 002/003 family
  3. *
  4. * Copyright (c) 2014-2015 Takashi Sakamoto
  5. * Copyright (C) 2012 Robin Gareus <robin@gareus.org>
  6. * Copyright (C) 2012 Damien Zammit <damien@zamaudio.com>
  7. *
  8. * Licensed under the terms of the GNU General Public License, version 2.
  9. */
  10. #include <sound/pcm.h>
  11. #include "digi00x.h"
  12. #define CIP_FMT_AM 0x10
  13. /* 'Clock-based rate control mode' is just supported. */
  14. #define AMDTP_FDF_AM824 0x00
  15. /*
  16. * Nominally 3125 bytes/second, but the MIDI port's clock might be
  17. * 1% too slow, and the bus clock 100 ppm too fast.
  18. */
  19. #define MIDI_BYTES_PER_SECOND 3093
  20. /*
  21. * Several devices look only at the first eight data blocks.
  22. * In any case, this is more than enough for the MIDI data rate.
  23. */
  24. #define MAX_MIDI_RX_BLOCKS 8
  25. /* 3 = MAX(DOT_MIDI_IN_PORTS, DOT_MIDI_OUT_PORTS) + 1. */
  26. #define MAX_MIDI_PORTS 3
  27. /*
  28. * The double-oh-three algorithm was discovered by Robin Gareus and Damien
  29. * Zammit in 2012, with reverse-engineering for Digi 003 Rack.
  30. */
  31. struct dot_state {
  32. u8 carry;
  33. u8 idx;
  34. unsigned int off;
  35. };
  36. struct amdtp_dot {
  37. unsigned int pcm_channels;
  38. struct dot_state state;
  39. struct snd_rawmidi_substream *midi[MAX_MIDI_PORTS];
  40. int midi_fifo_used[MAX_MIDI_PORTS];
  41. int midi_fifo_limit;
  42. };
  43. /*
  44. * double-oh-three look up table
  45. *
  46. * @param idx index byte (audio-sample data) 0x00..0xff
  47. * @param off channel offset shift
  48. * @return salt to XOR with given data
  49. */
  50. #define BYTE_PER_SAMPLE (4)
  51. #define MAGIC_DOT_BYTE (2)
  52. #define MAGIC_BYTE_OFF(x) (((x) * BYTE_PER_SAMPLE) + MAGIC_DOT_BYTE)
  53. static u8 dot_scrt(const u8 idx, const unsigned int off)
  54. {
  55. /*
  56. * the length of the added pattern only depends on the lower nibble
  57. * of the last non-zero data
  58. */
  59. static const u8 len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14,
  60. 12, 10, 8, 6, 4, 2, 0};
  61. /*
  62. * the lower nibble of the salt. Interleaved sequence.
  63. * this is walked backwards according to len[]
  64. */
  65. static const u8 nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4,
  66. 0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
  67. /* circular list for the salt's hi nibble. */
  68. static const u8 hir[15] = {0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4,
  69. 0xc, 0xd, 0xe, 0x1, 0x2, 0xb, 0xa};
  70. /*
  71. * start offset for upper nibble mapping.
  72. * note: 9 is /special/. In the case where the high nibble == 0x9,
  73. * hir[] is not used and - coincidentally - the salt's hi nibble is
  74. * 0x09 regardless of the offset.
  75. */
  76. static const u8 hio[16] = {0, 11, 12, 6, 7, 5, 1, 4,
  77. 3, 0x00, 14, 13, 8, 9, 10, 2};
  78. const u8 ln = idx & 0xf;
  79. const u8 hn = (idx >> 4) & 0xf;
  80. const u8 hr = (hn == 0x9) ? 0x9 : hir[(hio[hn] + off) % 15];
  81. if (len[ln] < off)
  82. return 0x00;
  83. return ((nib[14 + off - len[ln]]) | (hr << 4));
  84. }
  85. static void dot_encode_step(struct dot_state *state, __be32 *const buffer)
  86. {
  87. u8 * const data = (u8 *) buffer;
  88. if (data[MAGIC_DOT_BYTE] != 0x00) {
  89. state->off = 0;
  90. state->idx = data[MAGIC_DOT_BYTE] ^ state->carry;
  91. }
  92. data[MAGIC_DOT_BYTE] ^= state->carry;
  93. state->carry = dot_scrt(state->idx, ++(state->off));
  94. }
  95. int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate,
  96. unsigned int pcm_channels)
  97. {
  98. struct amdtp_dot *p = s->protocol;
  99. int err;
  100. if (amdtp_stream_running(s))
  101. return -EBUSY;
  102. /*
  103. * A first data channel is for MIDI messages, the rest is Multi Bit
  104. * Linear Audio data channel.
  105. */
  106. err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1);
  107. if (err < 0)
  108. return err;
  109. s->fdf = AMDTP_FDF_AM824 | s->sfc;
  110. p->pcm_channels = pcm_channels;
  111. /*
  112. * We do not know the actual MIDI FIFO size of most devices. Just
  113. * assume two bytes, i.e., one byte can be received over the bus while
  114. * the previous one is transmitted over MIDI.
  115. * (The value here is adjusted for midi_ratelimit_per_packet().)
  116. */
  117. p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
  118. return 0;
  119. }
  120. static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  121. __be32 *buffer, unsigned int frames)
  122. {
  123. struct amdtp_dot *p = s->protocol;
  124. struct snd_pcm_runtime *runtime = pcm->runtime;
  125. unsigned int channels, remaining_frames, i, c;
  126. const u32 *src;
  127. channels = p->pcm_channels;
  128. src = (void *)runtime->dma_area +
  129. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  130. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  131. buffer++;
  132. for (i = 0; i < frames; ++i) {
  133. for (c = 0; c < channels; ++c) {
  134. buffer[c] = cpu_to_be32((*src >> 8) | 0x40000000);
  135. dot_encode_step(&p->state, &buffer[c]);
  136. src++;
  137. }
  138. buffer += s->data_block_quadlets;
  139. if (--remaining_frames == 0)
  140. src = (void *)runtime->dma_area;
  141. }
  142. }
  143. static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  144. __be32 *buffer, unsigned int frames)
  145. {
  146. struct amdtp_dot *p = s->protocol;
  147. struct snd_pcm_runtime *runtime = pcm->runtime;
  148. unsigned int channels, remaining_frames, i, c;
  149. u32 *dst;
  150. channels = p->pcm_channels;
  151. dst = (void *)runtime->dma_area +
  152. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  153. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  154. buffer++;
  155. for (i = 0; i < frames; ++i) {
  156. for (c = 0; c < channels; ++c) {
  157. *dst = be32_to_cpu(buffer[c]) << 8;
  158. dst++;
  159. }
  160. buffer += s->data_block_quadlets;
  161. if (--remaining_frames == 0)
  162. dst = (void *)runtime->dma_area;
  163. }
  164. }
  165. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  166. unsigned int data_blocks)
  167. {
  168. struct amdtp_dot *p = s->protocol;
  169. unsigned int channels, i, c;
  170. channels = p->pcm_channels;
  171. buffer++;
  172. for (i = 0; i < data_blocks; ++i) {
  173. for (c = 0; c < channels; ++c)
  174. buffer[c] = cpu_to_be32(0x40000000);
  175. buffer += s->data_block_quadlets;
  176. }
  177. }
  178. static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
  179. {
  180. struct amdtp_dot *p = s->protocol;
  181. int used;
  182. used = p->midi_fifo_used[port];
  183. if (used == 0)
  184. return true;
  185. used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
  186. used = max(used, 0);
  187. p->midi_fifo_used[port] = used;
  188. return used < p->midi_fifo_limit;
  189. }
  190. static inline void midi_use_bytes(struct amdtp_stream *s,
  191. unsigned int port, unsigned int count)
  192. {
  193. struct amdtp_dot *p = s->protocol;
  194. p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
  195. }
  196. static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  197. unsigned int data_blocks)
  198. {
  199. struct amdtp_dot *p = s->protocol;
  200. unsigned int f, port;
  201. int len;
  202. u8 *b;
  203. for (f = 0; f < data_blocks; f++) {
  204. port = (s->data_block_counter + f) % 8;
  205. b = (u8 *)&buffer[0];
  206. len = 0;
  207. if (port < MAX_MIDI_PORTS &&
  208. midi_ratelimit_per_packet(s, port) &&
  209. p->midi[port] != NULL)
  210. len = snd_rawmidi_transmit(p->midi[port], b + 1, 2);
  211. if (len > 0) {
  212. /*
  213. * Upper 4 bits of LSB represent port number.
  214. * - 0000b: physical MIDI port 1.
  215. * - 0010b: physical MIDI port 2.
  216. * - 1110b: console MIDI port.
  217. */
  218. if (port == 2)
  219. b[3] = 0xe0;
  220. else if (port == 1)
  221. b[3] = 0x20;
  222. else
  223. b[3] = 0x00;
  224. b[3] |= len;
  225. midi_use_bytes(s, port, len);
  226. } else {
  227. b[1] = 0;
  228. b[2] = 0;
  229. b[3] = 0;
  230. }
  231. b[0] = 0x80;
  232. buffer += s->data_block_quadlets;
  233. }
  234. }
  235. static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  236. unsigned int data_blocks)
  237. {
  238. struct amdtp_dot *p = s->protocol;
  239. unsigned int f, port, len;
  240. u8 *b;
  241. for (f = 0; f < data_blocks; f++) {
  242. b = (u8 *)&buffer[0];
  243. len = b[3] & 0x0f;
  244. if (len > 0) {
  245. /*
  246. * Upper 4 bits of LSB represent port number.
  247. * - 0000b: physical MIDI port 1. Use port 0.
  248. * - 1110b: console MIDI port. Use port 2.
  249. */
  250. if (b[3] >> 4 > 0)
  251. port = 2;
  252. else
  253. port = 0;
  254. if (port < MAX_MIDI_PORTS && p->midi[port])
  255. snd_rawmidi_receive(p->midi[port], b + 1, len);
  256. }
  257. buffer += s->data_block_quadlets;
  258. }
  259. }
  260. int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
  261. struct snd_pcm_runtime *runtime)
  262. {
  263. int err;
  264. /* This protocol delivers 24 bit data in 32bit data channel. */
  265. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  266. if (err < 0)
  267. return err;
  268. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  269. }
  270. void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
  271. struct snd_rawmidi_substream *midi)
  272. {
  273. struct amdtp_dot *p = s->protocol;
  274. if (port < MAX_MIDI_PORTS)
  275. ACCESS_ONCE(p->midi[port]) = midi;
  276. }
  277. static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
  278. __be32 *buffer,
  279. unsigned int data_blocks,
  280. unsigned int *syt)
  281. {
  282. struct snd_pcm_substream *pcm;
  283. unsigned int pcm_frames;
  284. pcm = ACCESS_ONCE(s->pcm);
  285. if (pcm) {
  286. read_pcm_s32(s, pcm, buffer, data_blocks);
  287. pcm_frames = data_blocks;
  288. } else {
  289. pcm_frames = 0;
  290. }
  291. read_midi_messages(s, buffer, data_blocks);
  292. return pcm_frames;
  293. }
  294. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  295. __be32 *buffer,
  296. unsigned int data_blocks,
  297. unsigned int *syt)
  298. {
  299. struct snd_pcm_substream *pcm;
  300. unsigned int pcm_frames;
  301. pcm = ACCESS_ONCE(s->pcm);
  302. if (pcm) {
  303. write_pcm_s32(s, pcm, buffer, data_blocks);
  304. pcm_frames = data_blocks;
  305. } else {
  306. write_pcm_silence(s, buffer, data_blocks);
  307. pcm_frames = 0;
  308. }
  309. write_midi_messages(s, buffer, data_blocks);
  310. return pcm_frames;
  311. }
  312. int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
  313. enum amdtp_stream_direction dir)
  314. {
  315. amdtp_stream_process_data_blocks_t process_data_blocks;
  316. enum cip_flags flags;
  317. /* Use different mode between incoming/outgoing. */
  318. if (dir == AMDTP_IN_STREAM) {
  319. flags = CIP_NONBLOCKING;
  320. process_data_blocks = process_tx_data_blocks;
  321. } else {
  322. flags = CIP_BLOCKING;
  323. process_data_blocks = process_rx_data_blocks;
  324. }
  325. return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
  326. process_data_blocks, sizeof(struct amdtp_dot));
  327. }
  328. void amdtp_dot_reset(struct amdtp_stream *s)
  329. {
  330. struct amdtp_dot *p = s->protocol;
  331. p->state.carry = 0x00;
  332. p->state.idx = 0x00;
  333. p->state.off = 0;
  334. }