amdtp-dot.c 11 KB

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