amdtp-dot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. 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 const 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 messages, the rest is Multi Bit
  107. * 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. /*
  115. * We do not know the actual MIDI FIFO size of most devices. Just
  116. * assume two bytes, i.e., one byte can be received over the bus while
  117. * the previous one is transmitted over MIDI.
  118. * (The value here is adjusted for midi_ratelimit_per_packet().)
  119. */
  120. p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
  121. return 0;
  122. }
  123. static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  124. __be32 *buffer, unsigned int frames)
  125. {
  126. struct amdtp_dot *p = s->protocol;
  127. struct snd_pcm_runtime *runtime = pcm->runtime;
  128. unsigned int channels, remaining_frames, i, c;
  129. const u32 *src;
  130. channels = p->pcm_channels;
  131. src = (void *)runtime->dma_area +
  132. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  133. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  134. buffer++;
  135. for (i = 0; i < frames; ++i) {
  136. for (c = 0; c < channels; ++c) {
  137. buffer[c] = cpu_to_be32((*src >> 8) | 0x40000000);
  138. dot_encode_step(&p->state, &buffer[c]);
  139. src++;
  140. }
  141. buffer += s->data_block_quadlets;
  142. if (--remaining_frames == 0)
  143. src = (void *)runtime->dma_area;
  144. }
  145. }
  146. static void write_pcm_s16(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  147. __be32 *buffer, unsigned int frames)
  148. {
  149. struct amdtp_dot *p = s->protocol;
  150. struct snd_pcm_runtime *runtime = pcm->runtime;
  151. unsigned int channels, remaining_frames, i, c;
  152. const u16 *src;
  153. channels = p->pcm_channels;
  154. src = (void *)runtime->dma_area +
  155. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  156. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  157. buffer++;
  158. for (i = 0; i < frames; ++i) {
  159. for (c = 0; c < channels; ++c) {
  160. buffer[c] = cpu_to_be32((*src << 8) | 0x40000000);
  161. dot_encode_step(&p->state, &buffer[c]);
  162. src++;
  163. }
  164. buffer += s->data_block_quadlets;
  165. if (--remaining_frames == 0)
  166. src = (void *)runtime->dma_area;
  167. }
  168. }
  169. static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
  170. __be32 *buffer, unsigned int frames)
  171. {
  172. struct amdtp_dot *p = s->protocol;
  173. struct snd_pcm_runtime *runtime = pcm->runtime;
  174. unsigned int channels, remaining_frames, i, c;
  175. u32 *dst;
  176. channels = p->pcm_channels;
  177. dst = (void *)runtime->dma_area +
  178. frames_to_bytes(runtime, s->pcm_buffer_pointer);
  179. remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
  180. buffer++;
  181. for (i = 0; i < frames; ++i) {
  182. for (c = 0; c < channels; ++c) {
  183. *dst = be32_to_cpu(buffer[c]) << 8;
  184. dst++;
  185. }
  186. buffer += s->data_block_quadlets;
  187. if (--remaining_frames == 0)
  188. dst = (void *)runtime->dma_area;
  189. }
  190. }
  191. static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
  192. unsigned int data_blocks)
  193. {
  194. struct amdtp_dot *p = s->protocol;
  195. unsigned int channels, i, c;
  196. channels = p->pcm_channels;
  197. buffer++;
  198. for (i = 0; i < data_blocks; ++i) {
  199. for (c = 0; c < channels; ++c)
  200. buffer[c] = cpu_to_be32(0x40000000);
  201. buffer += s->data_block_quadlets;
  202. }
  203. }
  204. static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
  205. {
  206. struct amdtp_dot *p = s->protocol;
  207. int used;
  208. used = p->midi_fifo_used[port];
  209. if (used == 0)
  210. return true;
  211. used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
  212. used = max(used, 0);
  213. p->midi_fifo_used[port] = used;
  214. return used < p->midi_fifo_limit;
  215. }
  216. static inline void midi_use_bytes(struct amdtp_stream *s,
  217. unsigned int port, unsigned int count)
  218. {
  219. struct amdtp_dot *p = s->protocol;
  220. p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
  221. }
  222. static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  223. unsigned int data_blocks)
  224. {
  225. struct amdtp_dot *p = s->protocol;
  226. unsigned int f, port;
  227. int len;
  228. u8 *b;
  229. for (f = 0; f < data_blocks; f++) {
  230. port = (s->data_block_counter + f) % 8;
  231. b = (u8 *)&buffer[0];
  232. len = 0;
  233. if (port < MAX_MIDI_PORTS &&
  234. midi_ratelimit_per_packet(s, port) &&
  235. p->midi[port] != NULL)
  236. len = snd_rawmidi_transmit(p->midi[port], b + 1, 2);
  237. if (len > 0) {
  238. /*
  239. * Upper 4 bits of LSB represent port number.
  240. * - 0000b: physical MIDI port 1.
  241. * - 0010b: physical MIDI port 2.
  242. * - 1110b: console MIDI port.
  243. */
  244. if (port == 2)
  245. b[3] = 0xe0;
  246. else if (port == 1)
  247. b[3] = 0x20;
  248. else
  249. b[3] = 0x00;
  250. b[3] |= len;
  251. midi_use_bytes(s, port, len);
  252. } else {
  253. b[1] = 0;
  254. b[2] = 0;
  255. b[3] = 0;
  256. }
  257. b[0] = 0x80;
  258. buffer += s->data_block_quadlets;
  259. }
  260. }
  261. static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
  262. unsigned int data_blocks)
  263. {
  264. struct amdtp_dot *p = s->protocol;
  265. unsigned int f, port, len;
  266. u8 *b;
  267. for (f = 0; f < data_blocks; f++) {
  268. b = (u8 *)&buffer[0];
  269. len = b[3] & 0x0f;
  270. if (len > 0) {
  271. /*
  272. * Upper 4 bits of LSB represent port number.
  273. * - 0000b: physical MIDI port 1. Use port 0.
  274. * - 1110b: console MIDI port. Use port 2.
  275. */
  276. if (b[3] >> 4 > 0)
  277. port = 2;
  278. else
  279. port = 0;
  280. if (port < MAX_MIDI_PORTS && p->midi[port])
  281. snd_rawmidi_receive(p->midi[port], b + 1, len);
  282. }
  283. buffer += s->data_block_quadlets;
  284. }
  285. }
  286. int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
  287. struct snd_pcm_runtime *runtime)
  288. {
  289. int err;
  290. /* This protocol delivers 24 bit data in 32bit data channel. */
  291. err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  292. if (err < 0)
  293. return err;
  294. return amdtp_stream_add_pcm_hw_constraints(s, runtime);
  295. }
  296. void amdtp_dot_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
  297. {
  298. struct amdtp_dot *p = s->protocol;
  299. if (WARN_ON(amdtp_stream_pcm_running(s)))
  300. return;
  301. switch (format) {
  302. default:
  303. WARN_ON(1);
  304. /* fall through */
  305. case SNDRV_PCM_FORMAT_S16:
  306. if (s->direction == AMDTP_OUT_STREAM) {
  307. p->transfer_samples = write_pcm_s16;
  308. break;
  309. }
  310. WARN_ON(1);
  311. /* fall through */
  312. case SNDRV_PCM_FORMAT_S32:
  313. if (s->direction == AMDTP_OUT_STREAM)
  314. p->transfer_samples = write_pcm_s32;
  315. else
  316. p->transfer_samples = read_pcm_s32;
  317. break;
  318. }
  319. }
  320. void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
  321. struct snd_rawmidi_substream *midi)
  322. {
  323. struct amdtp_dot *p = s->protocol;
  324. if (port < MAX_MIDI_PORTS)
  325. ACCESS_ONCE(p->midi[port]) = midi;
  326. }
  327. static unsigned int process_tx_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. pcm_frames = 0;
  341. }
  342. read_midi_messages(s, buffer, data_blocks);
  343. return pcm_frames;
  344. }
  345. static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
  346. __be32 *buffer,
  347. unsigned int data_blocks,
  348. unsigned int *syt)
  349. {
  350. struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
  351. struct snd_pcm_substream *pcm;
  352. unsigned int pcm_frames;
  353. pcm = ACCESS_ONCE(s->pcm);
  354. if (pcm) {
  355. p->transfer_samples(s, pcm, buffer, data_blocks);
  356. pcm_frames = data_blocks;
  357. } else {
  358. write_pcm_silence(s, buffer, data_blocks);
  359. pcm_frames = 0;
  360. }
  361. write_midi_messages(s, buffer, data_blocks);
  362. return pcm_frames;
  363. }
  364. int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
  365. enum amdtp_stream_direction dir)
  366. {
  367. amdtp_stream_process_data_blocks_t process_data_blocks;
  368. enum cip_flags flags;
  369. /* Use different mode between incoming/outgoing. */
  370. if (dir == AMDTP_IN_STREAM) {
  371. flags = CIP_NONBLOCKING | CIP_SKIP_INIT_DBC_CHECK;
  372. process_data_blocks = process_tx_data_blocks;
  373. } else {
  374. flags = CIP_BLOCKING;
  375. process_data_blocks = process_rx_data_blocks;
  376. }
  377. return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
  378. process_data_blocks, sizeof(struct amdtp_dot));
  379. }
  380. void amdtp_dot_reset(struct amdtp_stream *s)
  381. {
  382. struct amdtp_dot *p = s->protocol;
  383. p->state.carry = 0x00;
  384. p->state.idx = 0x00;
  385. p->state.off = 0;
  386. }