sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / firewire / digi00x / amdtp-dot.c
blobb3cffd01a19f711f3572920a33fa75098f39ffde
1 /*
2 * amdtp-dot.c - a part of driver for Digidesign Digi 002/003 family
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>
8 * Licensed under the terms of the GNU General Public License, version 2.
9 */
11 #include <sound/pcm.h>
12 #include "digi00x.h"
14 #define CIP_FMT_AM 0x10
16 /* 'Clock-based rate control mode' is just supported. */
17 #define AMDTP_FDF_AM824 0x00
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.
23 #define MIDI_BYTES_PER_SECOND 3093
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.
29 #define MAX_MIDI_RX_BLOCKS 8
32 * The double-oh-three algorithm was discovered by Robin Gareus and Damien
33 * Zammit in 2012, with reverse-engineering for Digi 003 Rack.
35 struct dot_state {
36 u8 carry;
37 u8 idx;
38 unsigned int off;
41 struct amdtp_dot {
42 unsigned int pcm_channels;
43 struct dot_state state;
45 unsigned int midi_ports;
46 /* 2 = MAX(DOT_MIDI_IN_PORTS, DOT_MIDI_OUT_PORTS) */
47 struct snd_rawmidi_substream *midi[2];
48 int midi_fifo_used[2];
49 int midi_fifo_limit;
51 void (*transfer_samples)(struct amdtp_stream *s,
52 struct snd_pcm_substream *pcm,
53 __be32 *buffer, unsigned int frames);
57 * double-oh-three look up table
59 * @param idx index byte (audio-sample data) 0x00..0xff
60 * @param off channel offset shift
61 * @return salt to XOR with given data
63 #define BYTE_PER_SAMPLE (4)
64 #define MAGIC_DOT_BYTE (2)
65 #define MAGIC_BYTE_OFF(x) (((x) * BYTE_PER_SAMPLE) + MAGIC_DOT_BYTE)
66 static u8 dot_scrt(const u8 idx, const unsigned int off)
69 * the length of the added pattern only depends on the lower nibble
70 * of the last non-zero data
72 static const u8 len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14,
73 12, 10, 8, 6, 4, 2, 0};
76 * the lower nibble of the salt. Interleaved sequence.
77 * this is walked backwards according to len[]
79 static const u8 nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4,
80 0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
82 /* circular list for the salt's hi nibble. */
83 static const u8 hir[15] = {0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4,
84 0xc, 0xd, 0xe, 0x1, 0x2, 0xb, 0xa};
87 * start offset for upper nibble mapping.
88 * note: 9 is /special/. In the case where the high nibble == 0x9,
89 * hir[] is not used and - coincidentally - the salt's hi nibble is
90 * 0x09 regardless of the offset.
92 static const u8 hio[16] = {0, 11, 12, 6, 7, 5, 1, 4,
93 3, 0x00, 14, 13, 8, 9, 10, 2};
95 const u8 ln = idx & 0xf;
96 const u8 hn = (idx >> 4) & 0xf;
97 const u8 hr = (hn == 0x9) ? 0x9 : hir[(hio[hn] + off) % 15];
99 if (len[ln] < off)
100 return 0x00;
102 return ((nib[14 + off - len[ln]]) | (hr << 4));
105 static void dot_encode_step(struct dot_state *state, __be32 *const buffer)
107 u8 * const data = (u8 *) buffer;
109 if (data[MAGIC_DOT_BYTE] != 0x00) {
110 state->off = 0;
111 state->idx = data[MAGIC_DOT_BYTE] ^ state->carry;
113 data[MAGIC_DOT_BYTE] ^= state->carry;
114 state->carry = dot_scrt(state->idx, ++(state->off));
117 int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate,
118 unsigned int pcm_channels)
120 struct amdtp_dot *p = s->protocol;
121 int err;
123 if (amdtp_stream_running(s))
124 return -EBUSY;
127 * A first data channel is for MIDI conformant data channel, the rest is
128 * Multi Bit Linear Audio data channel.
130 err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1);
131 if (err < 0)
132 return err;
134 s->fdf = AMDTP_FDF_AM824 | s->sfc;
136 p->pcm_channels = pcm_channels;
138 if (s->direction == AMDTP_IN_STREAM)
139 p->midi_ports = DOT_MIDI_IN_PORTS;
140 else
141 p->midi_ports = DOT_MIDI_OUT_PORTS;
144 * We do not know the actual MIDI FIFO size of most devices. Just
145 * assume two bytes, i.e., one byte can be received over the bus while
146 * the previous one is transmitted over MIDI.
147 * (The value here is adjusted for midi_ratelimit_per_packet().)
149 p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
151 return 0;
154 static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
155 __be32 *buffer, unsigned int frames)
157 struct amdtp_dot *p = s->protocol;
158 struct snd_pcm_runtime *runtime = pcm->runtime;
159 unsigned int channels, remaining_frames, i, c;
160 const u32 *src;
162 channels = p->pcm_channels;
163 src = (void *)runtime->dma_area +
164 frames_to_bytes(runtime, s->pcm_buffer_pointer);
165 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
167 buffer++;
168 for (i = 0; i < frames; ++i) {
169 for (c = 0; c < channels; ++c) {
170 buffer[c] = cpu_to_be32((*src >> 8) | 0x40000000);
171 dot_encode_step(&p->state, &buffer[c]);
172 src++;
174 buffer += s->data_block_quadlets;
175 if (--remaining_frames == 0)
176 src = (void *)runtime->dma_area;
180 static void write_pcm_s16(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
181 __be32 *buffer, unsigned int frames)
183 struct amdtp_dot *p = s->protocol;
184 struct snd_pcm_runtime *runtime = pcm->runtime;
185 unsigned int channels, remaining_frames, i, c;
186 const u16 *src;
188 channels = p->pcm_channels;
189 src = (void *)runtime->dma_area +
190 frames_to_bytes(runtime, s->pcm_buffer_pointer);
191 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
193 buffer++;
194 for (i = 0; i < frames; ++i) {
195 for (c = 0; c < channels; ++c) {
196 buffer[c] = cpu_to_be32((*src << 8) | 0x40000000);
197 dot_encode_step(&p->state, &buffer[c]);
198 src++;
200 buffer += s->data_block_quadlets;
201 if (--remaining_frames == 0)
202 src = (void *)runtime->dma_area;
206 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
207 __be32 *buffer, unsigned int frames)
209 struct amdtp_dot *p = s->protocol;
210 struct snd_pcm_runtime *runtime = pcm->runtime;
211 unsigned int channels, remaining_frames, i, c;
212 u32 *dst;
214 channels = p->pcm_channels;
215 dst = (void *)runtime->dma_area +
216 frames_to_bytes(runtime, s->pcm_buffer_pointer);
217 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
219 buffer++;
220 for (i = 0; i < frames; ++i) {
221 for (c = 0; c < channels; ++c) {
222 *dst = be32_to_cpu(buffer[c]) << 8;
223 dst++;
225 buffer += s->data_block_quadlets;
226 if (--remaining_frames == 0)
227 dst = (void *)runtime->dma_area;
231 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
232 unsigned int data_blocks)
234 struct amdtp_dot *p = s->protocol;
235 unsigned int channels, i, c;
237 channels = p->pcm_channels;
239 buffer++;
240 for (i = 0; i < data_blocks; ++i) {
241 for (c = 0; c < channels; ++c)
242 buffer[c] = cpu_to_be32(0x40000000);
243 buffer += s->data_block_quadlets;
247 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
249 struct amdtp_dot *p = s->protocol;
250 int used;
252 used = p->midi_fifo_used[port];
253 if (used == 0)
254 return true;
256 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
257 used = max(used, 0);
258 p->midi_fifo_used[port] = used;
260 return used < p->midi_fifo_limit;
263 static inline void midi_use_bytes(struct amdtp_stream *s,
264 unsigned int port, unsigned int count)
266 struct amdtp_dot *p = s->protocol;
268 p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
271 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
272 unsigned int data_blocks)
274 struct amdtp_dot *p = s->protocol;
275 unsigned int f, port;
276 int len;
277 u8 *b;
279 for (f = 0; f < data_blocks; f++) {
280 port = (s->data_block_counter + f) % 8;
281 b = (u8 *)&buffer[0];
283 len = 0;
284 if (port < p->midi_ports &&
285 midi_ratelimit_per_packet(s, port) &&
286 p->midi[port] != NULL)
287 len = snd_rawmidi_transmit(p->midi[port], b + 1, 2);
289 if (len > 0) {
290 b[3] = (0x10 << port) | len;
291 midi_use_bytes(s, port, len);
292 } else {
293 b[1] = 0;
294 b[2] = 0;
295 b[3] = 0;
297 b[0] = 0x80;
299 buffer += s->data_block_quadlets;
303 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
304 unsigned int data_blocks)
306 struct amdtp_dot *p = s->protocol;
307 unsigned int f, port, len;
308 u8 *b;
310 for (f = 0; f < data_blocks; f++) {
311 b = (u8 *)&buffer[0];
312 port = b[3] >> 4;
313 len = b[3] & 0x0f;
315 if (port < p->midi_ports && p->midi[port] && len > 0)
316 snd_rawmidi_receive(p->midi[port], b + 1, len);
318 buffer += s->data_block_quadlets;
322 int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
323 struct snd_pcm_runtime *runtime)
325 int err;
327 /* This protocol delivers 24 bit data in 32bit data channel. */
328 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
329 if (err < 0)
330 return err;
332 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
335 void amdtp_dot_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
337 struct amdtp_dot *p = s->protocol;
339 if (WARN_ON(amdtp_stream_pcm_running(s)))
340 return;
342 switch (format) {
343 default:
344 WARN_ON(1);
345 /* fall through */
346 case SNDRV_PCM_FORMAT_S16:
347 if (s->direction == AMDTP_OUT_STREAM) {
348 p->transfer_samples = write_pcm_s16;
349 break;
351 WARN_ON(1);
352 /* fall through */
353 case SNDRV_PCM_FORMAT_S32:
354 if (s->direction == AMDTP_OUT_STREAM)
355 p->transfer_samples = write_pcm_s32;
356 else
357 p->transfer_samples = read_pcm_s32;
358 break;
362 void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
363 struct snd_rawmidi_substream *midi)
365 struct amdtp_dot *p = s->protocol;
367 if (port < p->midi_ports)
368 ACCESS_ONCE(p->midi[port]) = midi;
371 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
372 __be32 *buffer,
373 unsigned int data_blocks,
374 unsigned int *syt)
376 struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
377 struct snd_pcm_substream *pcm;
378 unsigned int pcm_frames;
380 pcm = ACCESS_ONCE(s->pcm);
381 if (pcm) {
382 p->transfer_samples(s, pcm, buffer, data_blocks);
383 pcm_frames = data_blocks;
384 } else {
385 pcm_frames = 0;
388 read_midi_messages(s, buffer, data_blocks);
390 return pcm_frames;
393 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
394 __be32 *buffer,
395 unsigned int data_blocks,
396 unsigned int *syt)
398 struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
399 struct snd_pcm_substream *pcm;
400 unsigned int pcm_frames;
402 pcm = ACCESS_ONCE(s->pcm);
403 if (pcm) {
404 p->transfer_samples(s, pcm, buffer, data_blocks);
405 pcm_frames = data_blocks;
406 } else {
407 write_pcm_silence(s, buffer, data_blocks);
408 pcm_frames = 0;
411 write_midi_messages(s, buffer, data_blocks);
413 return pcm_frames;
416 int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
417 enum amdtp_stream_direction dir)
419 amdtp_stream_process_data_blocks_t process_data_blocks;
420 enum cip_flags flags;
422 /* Use different mode between incoming/outgoing. */
423 if (dir == AMDTP_IN_STREAM) {
424 flags = CIP_NONBLOCKING;
425 process_data_blocks = process_tx_data_blocks;
426 } else {
427 flags = CIP_BLOCKING;
428 process_data_blocks = process_rx_data_blocks;
431 return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
432 process_data_blocks, sizeof(struct amdtp_dot));
435 void amdtp_dot_reset(struct amdtp_stream *s)
437 struct amdtp_dot *p = s->protocol;
439 p->state.carry = 0x00;
440 p->state.idx = 0x00;
441 p->state.off = 0;