GNU Linux-libre 4.9.309-gnu1
[releases.git] / sound / firewire / digi00x / amdtp-dot.c
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
11 #include <sound/pcm.h>
12 #include "digi00x.h"
13
14 #define CIP_FMT_AM              0x10
15
16 /* 'Clock-based rate control mode' is just supported. */
17 #define AMDTP_FDF_AM824         0x00
18
19 /*
20  * Nominally 3125 bytes/second, but the MIDI port's clock might be
21  * 1% too slow, and the bus clock 100 ppm too fast.
22  */
23 #define MIDI_BYTES_PER_SECOND   3093
24
25 /*
26  * Several devices look only at the first eight data blocks.
27  * In any case, this is more than enough for the MIDI data rate.
28  */
29 #define MAX_MIDI_RX_BLOCKS      8
30
31 /* 3 = MAX(DOT_MIDI_IN_PORTS, DOT_MIDI_OUT_PORTS) + 1. */
32 #define MAX_MIDI_PORTS          3
33
34 /*
35  * The double-oh-three algorithm was discovered by Robin Gareus and Damien
36  * Zammit in 2012, with reverse-engineering for Digi 003 Rack.
37  */
38 struct dot_state {
39         u8 carry;
40         u8 idx;
41         unsigned int off;
42 };
43
44 struct amdtp_dot {
45         unsigned int pcm_channels;
46         struct dot_state state;
47
48         struct snd_rawmidi_substream *midi[MAX_MIDI_PORTS];
49         int midi_fifo_used[MAX_MIDI_PORTS];
50         int midi_fifo_limit;
51
52         void (*transfer_samples)(struct amdtp_stream *s,
53                                  struct snd_pcm_substream *pcm,
54                                  __be32 *buffer, unsigned int frames);
55 };
56
57 /*
58  * double-oh-three look up table
59  *
60  * @param idx index byte (audio-sample data) 0x00..0xff
61  * @param off channel offset shift
62  * @return salt to XOR with given data
63  */
64 #define BYTE_PER_SAMPLE (4)
65 #define MAGIC_DOT_BYTE (2)
66 #define MAGIC_BYTE_OFF(x) (((x) * BYTE_PER_SAMPLE) + MAGIC_DOT_BYTE)
67 static u8 dot_scrt(const u8 idx, const unsigned int off)
68 {
69         /*
70          * the length of the added pattern only depends on the lower nibble
71          * of the last non-zero data
72          */
73         static const u8 len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14,
74                                    12, 10, 8, 6, 4, 2, 0};
75
76         /*
77          * the lower nibble of the salt. Interleaved sequence.
78          * this is walked backwards according to len[]
79          */
80         static const u8 nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4,
81                                    0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
82
83         /* circular list for the salt's hi nibble. */
84         static const u8 hir[15] = {0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4,
85                                    0xc, 0xd, 0xe, 0x1, 0x2, 0xb, 0xa};
86
87         /*
88          * start offset for upper nibble mapping.
89          * note: 9 is /special/. In the case where the high nibble == 0x9,
90          * hir[] is not used and - coincidentally - the salt's hi nibble is
91          * 0x09 regardless of the offset.
92          */
93         static const u8 hio[16] = {0, 11, 12, 6, 7, 5, 1, 4,
94                                    3, 0x00, 14, 13, 8, 9, 10, 2};
95
96         const u8 ln = idx & 0xf;
97         const u8 hn = (idx >> 4) & 0xf;
98         const u8 hr = (hn == 0x9) ? 0x9 : hir[(hio[hn] + off) % 15];
99
100         if (len[ln] < off)
101                 return 0x00;
102
103         return ((nib[14 + off - len[ln]]) | (hr << 4));
104 }
105
106 static void dot_encode_step(struct dot_state *state, __be32 *const buffer)
107 {
108         u8 * const data = (u8 *) buffer;
109
110         if (data[MAGIC_DOT_BYTE] != 0x00) {
111                 state->off = 0;
112                 state->idx = data[MAGIC_DOT_BYTE] ^ state->carry;
113         }
114         data[MAGIC_DOT_BYTE] ^= state->carry;
115         state->carry = dot_scrt(state->idx, ++(state->off));
116 }
117
118 int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate,
119                              unsigned int pcm_channels)
120 {
121         struct amdtp_dot *p = s->protocol;
122         int err;
123
124         if (amdtp_stream_running(s))
125                 return -EBUSY;
126
127         /*
128          * A first data channel is for MIDI messages, the rest is Multi Bit
129          * Linear Audio data channel.
130          */
131         err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1);
132         if (err < 0)
133                 return err;
134
135         s->fdf = AMDTP_FDF_AM824 | s->sfc;
136
137         p->pcm_channels = pcm_channels;
138
139         /*
140          * We do not know the actual MIDI FIFO size of most devices.  Just
141          * assume two bytes, i.e., one byte can be received over the bus while
142          * the previous one is transmitted over MIDI.
143          * (The value here is adjusted for midi_ratelimit_per_packet().)
144          */
145         p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
146
147         return 0;
148 }
149
150 static void write_pcm_s32(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 u32 *src;
157
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
163         buffer++;
164         for (i = 0; i < frames; ++i) {
165                 for (c = 0; c < channels; ++c) {
166                         buffer[c] = cpu_to_be32((*src >> 8) | 0x40000000);
167                         dot_encode_step(&p->state, &buffer[c]);
168                         src++;
169                 }
170                 buffer += s->data_block_quadlets;
171                 if (--remaining_frames == 0)
172                         src = (void *)runtime->dma_area;
173         }
174 }
175
176 static void write_pcm_s16(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
177                           __be32 *buffer, unsigned int frames)
178 {
179         struct amdtp_dot *p = s->protocol;
180         struct snd_pcm_runtime *runtime = pcm->runtime;
181         unsigned int channels, remaining_frames, i, c;
182         const u16 *src;
183
184         channels = p->pcm_channels;
185         src = (void *)runtime->dma_area +
186                         frames_to_bytes(runtime, s->pcm_buffer_pointer);
187         remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
188
189         buffer++;
190         for (i = 0; i < frames; ++i) {
191                 for (c = 0; c < channels; ++c) {
192                         buffer[c] = cpu_to_be32((*src << 8) | 0x40000000);
193                         dot_encode_step(&p->state, &buffer[c]);
194                         src++;
195                 }
196                 buffer += s->data_block_quadlets;
197                 if (--remaining_frames == 0)
198                         src = (void *)runtime->dma_area;
199         }
200 }
201
202 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
203                          __be32 *buffer, unsigned int frames)
204 {
205         struct amdtp_dot *p = s->protocol;
206         struct snd_pcm_runtime *runtime = pcm->runtime;
207         unsigned int channels, remaining_frames, i, c;
208         u32 *dst;
209
210         channels = p->pcm_channels;
211         dst  = (void *)runtime->dma_area +
212                         frames_to_bytes(runtime, s->pcm_buffer_pointer);
213         remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
214
215         buffer++;
216         for (i = 0; i < frames; ++i) {
217                 for (c = 0; c < channels; ++c) {
218                         *dst = be32_to_cpu(buffer[c]) << 8;
219                         dst++;
220                 }
221                 buffer += s->data_block_quadlets;
222                 if (--remaining_frames == 0)
223                         dst = (void *)runtime->dma_area;
224         }
225 }
226
227 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
228                               unsigned int data_blocks)
229 {
230         struct amdtp_dot *p = s->protocol;
231         unsigned int channels, i, c;
232
233         channels = p->pcm_channels;
234
235         buffer++;
236         for (i = 0; i < data_blocks; ++i) {
237                 for (c = 0; c < channels; ++c)
238                         buffer[c] = cpu_to_be32(0x40000000);
239                 buffer += s->data_block_quadlets;
240         }
241 }
242
243 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
244 {
245         struct amdtp_dot *p = s->protocol;
246         int used;
247
248         used = p->midi_fifo_used[port];
249         if (used == 0)
250                 return true;
251
252         used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
253         used = max(used, 0);
254         p->midi_fifo_used[port] = used;
255
256         return used < p->midi_fifo_limit;
257 }
258
259 static inline void midi_use_bytes(struct amdtp_stream *s,
260                                   unsigned int port, unsigned int count)
261 {
262         struct amdtp_dot *p = s->protocol;
263
264         p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
265 }
266
267 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
268                                 unsigned int data_blocks)
269 {
270         struct amdtp_dot *p = s->protocol;
271         unsigned int f, port;
272         int len;
273         u8 *b;
274
275         for (f = 0; f < data_blocks; f++) {
276                 port = (s->data_block_counter + f) % 8;
277                 b = (u8 *)&buffer[0];
278
279                 len = 0;
280                 if (port < MAX_MIDI_PORTS &&
281                     midi_ratelimit_per_packet(s, port) &&
282                     p->midi[port] != NULL)
283                         len = snd_rawmidi_transmit(p->midi[port], b + 1, 2);
284
285                 if (len > 0) {
286                         /*
287                          * Upper 4 bits of LSB represent port number.
288                          * - 0000b: physical MIDI port 1.
289                          * - 0010b: physical MIDI port 2.
290                          * - 1110b: console MIDI port.
291                          */
292                         if (port == 2)
293                                 b[3] = 0xe0;
294                         else if (port == 1)
295                                 b[3] = 0x20;
296                         else
297                                 b[3] = 0x00;
298                         b[3] |= len;
299                         midi_use_bytes(s, port, len);
300                 } else {
301                         b[1] = 0;
302                         b[2] = 0;
303                         b[3] = 0;
304                 }
305                 b[0] = 0x80;
306
307                 buffer += s->data_block_quadlets;
308         }
309 }
310
311 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
312                                unsigned int data_blocks)
313 {
314         struct amdtp_dot *p = s->protocol;
315         unsigned int f, port, len;
316         u8 *b;
317
318         for (f = 0; f < data_blocks; f++) {
319                 b = (u8 *)&buffer[0];
320
321                 len = b[3] & 0x0f;
322                 if (len > 0) {
323                         /*
324                          * Upper 4 bits of LSB represent port number.
325                          * - 0000b: physical MIDI port 1. Use port 0.
326                          * - 1110b: console MIDI port. Use port 2.
327                          */
328                         if (b[3] >> 4 > 0)
329                                 port = 2;
330                         else
331                                 port = 0;
332
333                         if (port < MAX_MIDI_PORTS && p->midi[port])
334                                 snd_rawmidi_receive(p->midi[port], b + 1, len);
335                 }
336
337                 buffer += s->data_block_quadlets;
338         }
339 }
340
341 int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
342                                      struct snd_pcm_runtime *runtime)
343 {
344         int err;
345
346         /* This protocol delivers 24 bit data in 32bit data channel. */
347         err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
348         if (err < 0)
349                 return err;
350
351         return amdtp_stream_add_pcm_hw_constraints(s, runtime);
352 }
353
354 void amdtp_dot_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
355 {
356         struct amdtp_dot *p = s->protocol;
357
358         if (WARN_ON(amdtp_stream_pcm_running(s)))
359                 return;
360
361         switch (format) {
362         default:
363                 WARN_ON(1);
364                 /* fall through */
365         case SNDRV_PCM_FORMAT_S16:
366                 if (s->direction == AMDTP_OUT_STREAM) {
367                         p->transfer_samples = write_pcm_s16;
368                         break;
369                 }
370                 WARN_ON(1);
371                 /* fall through */
372         case SNDRV_PCM_FORMAT_S32:
373                 if (s->direction == AMDTP_OUT_STREAM)
374                         p->transfer_samples = write_pcm_s32;
375                 else
376                         p->transfer_samples = read_pcm_s32;
377                 break;
378         }
379 }
380
381 void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
382                           struct snd_rawmidi_substream *midi)
383 {
384         struct amdtp_dot *p = s->protocol;
385
386         if (port < MAX_MIDI_PORTS)
387                 ACCESS_ONCE(p->midi[port]) = midi;
388 }
389
390 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
391                                            __be32 *buffer,
392                                            unsigned int data_blocks,
393                                            unsigned int *syt)
394 {
395         struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
396         struct snd_pcm_substream *pcm;
397         unsigned int pcm_frames;
398
399         pcm = ACCESS_ONCE(s->pcm);
400         if (pcm) {
401                 p->transfer_samples(s, pcm, buffer, data_blocks);
402                 pcm_frames = data_blocks;
403         } else {
404                 pcm_frames = 0;
405         }
406
407         read_midi_messages(s, buffer, data_blocks);
408
409         return pcm_frames;
410 }
411
412 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
413                                            __be32 *buffer,
414                                            unsigned int data_blocks,
415                                            unsigned int *syt)
416 {
417         struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
418         struct snd_pcm_substream *pcm;
419         unsigned int pcm_frames;
420
421         pcm = ACCESS_ONCE(s->pcm);
422         if (pcm) {
423                 p->transfer_samples(s, pcm, buffer, data_blocks);
424                 pcm_frames = data_blocks;
425         } else {
426                 write_pcm_silence(s, buffer, data_blocks);
427                 pcm_frames = 0;
428         }
429
430         write_midi_messages(s, buffer, data_blocks);
431
432         return pcm_frames;
433 }
434
435 int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
436                  enum amdtp_stream_direction dir)
437 {
438         amdtp_stream_process_data_blocks_t process_data_blocks;
439         enum cip_flags flags;
440
441         /* Use different mode between incoming/outgoing. */
442         if (dir == AMDTP_IN_STREAM) {
443                 flags = CIP_NONBLOCKING;
444                 process_data_blocks = process_tx_data_blocks;
445         } else {
446                 flags = CIP_BLOCKING;
447                 process_data_blocks = process_rx_data_blocks;
448         }
449
450         return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
451                                  process_data_blocks, sizeof(struct amdtp_dot));
452 }
453
454 void amdtp_dot_reset(struct amdtp_stream *s)
455 {
456         struct amdtp_dot *p = s->protocol;
457
458         p->state.carry = 0x00;
459         p->state.idx = 0x00;
460         p->state.off = 0;
461 }