amdtp-dot.c 10 KB

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