vmalloc: fix __GFP_HIGHMEM usage for vmalloc_32 on 32b systems
[linux/fpc-iii.git] / sound / firewire / digi00x / amdtp-dot.c
blob4a884a33524809a23ba6a07e8319711f76771081
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
31 /* 3 = MAX(DOT_MIDI_IN_PORTS, DOT_MIDI_OUT_PORTS) + 1. */
32 #define MAX_MIDI_PORTS 3
35 * The double-oh-three algorithm was discovered by Robin Gareus and Damien
36 * Zammit in 2012, with reverse-engineering for Digi 003 Rack.
38 struct dot_state {
39 u8 carry;
40 u8 idx;
41 unsigned int off;
44 struct amdtp_dot {
45 unsigned int pcm_channels;
46 struct dot_state state;
48 struct snd_rawmidi_substream *midi[MAX_MIDI_PORTS];
49 int midi_fifo_used[MAX_MIDI_PORTS];
50 int midi_fifo_limit;
54 * double-oh-three look up table
56 * @param idx index byte (audio-sample data) 0x00..0xff
57 * @param off channel offset shift
58 * @return salt to XOR with given data
60 #define BYTE_PER_SAMPLE (4)
61 #define MAGIC_DOT_BYTE (2)
62 #define MAGIC_BYTE_OFF(x) (((x) * BYTE_PER_SAMPLE) + MAGIC_DOT_BYTE)
63 static u8 dot_scrt(const u8 idx, const unsigned int off)
66 * the length of the added pattern only depends on the lower nibble
67 * of the last non-zero data
69 static const u8 len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14,
70 12, 10, 8, 6, 4, 2, 0};
73 * the lower nibble of the salt. Interleaved sequence.
74 * this is walked backwards according to len[]
76 static const u8 nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4,
77 0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
79 /* circular list for the salt's hi nibble. */
80 static const u8 hir[15] = {0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4,
81 0xc, 0xd, 0xe, 0x1, 0x2, 0xb, 0xa};
84 * start offset for upper nibble mapping.
85 * note: 9 is /special/. In the case where the high nibble == 0x9,
86 * hir[] is not used and - coincidentally - the salt's hi nibble is
87 * 0x09 regardless of the offset.
89 static const u8 hio[16] = {0, 11, 12, 6, 7, 5, 1, 4,
90 3, 0x00, 14, 13, 8, 9, 10, 2};
92 const u8 ln = idx & 0xf;
93 const u8 hn = (idx >> 4) & 0xf;
94 const u8 hr = (hn == 0x9) ? 0x9 : hir[(hio[hn] + off) % 15];
96 if (len[ln] < off)
97 return 0x00;
99 return ((nib[14 + off - len[ln]]) | (hr << 4));
102 static void dot_encode_step(struct dot_state *state, __be32 *const buffer)
104 u8 * const data = (u8 *) buffer;
106 if (data[MAGIC_DOT_BYTE] != 0x00) {
107 state->off = 0;
108 state->idx = data[MAGIC_DOT_BYTE] ^ state->carry;
110 data[MAGIC_DOT_BYTE] ^= state->carry;
111 state->carry = dot_scrt(state->idx, ++(state->off));
114 int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate,
115 unsigned int pcm_channels)
117 struct amdtp_dot *p = s->protocol;
118 int err;
120 if (amdtp_stream_running(s))
121 return -EBUSY;
124 * A first data channel is for MIDI messages, the rest is Multi Bit
125 * Linear Audio data channel.
127 err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1);
128 if (err < 0)
129 return err;
131 s->fdf = AMDTP_FDF_AM824 | s->sfc;
133 p->pcm_channels = pcm_channels;
136 * We do not know the actual MIDI FIFO size of most devices. Just
137 * assume two bytes, i.e., one byte can be received over the bus while
138 * the previous one is transmitted over MIDI.
139 * (The value here is adjusted for midi_ratelimit_per_packet().)
141 p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
143 return 0;
146 static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
147 __be32 *buffer, unsigned int frames)
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 u32 *src;
154 channels = p->pcm_channels;
155 src = (void *)runtime->dma_area +
156 frames_to_bytes(runtime, s->pcm_buffer_pointer);
157 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
159 buffer++;
160 for (i = 0; i < frames; ++i) {
161 for (c = 0; c < channels; ++c) {
162 buffer[c] = cpu_to_be32((*src >> 8) | 0x40000000);
163 dot_encode_step(&p->state, &buffer[c]);
164 src++;
166 buffer += s->data_block_quadlets;
167 if (--remaining_frames == 0)
168 src = (void *)runtime->dma_area;
172 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
173 __be32 *buffer, unsigned int frames)
175 struct amdtp_dot *p = s->protocol;
176 struct snd_pcm_runtime *runtime = pcm->runtime;
177 unsigned int channels, remaining_frames, i, c;
178 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;
185 buffer++;
186 for (i = 0; i < frames; ++i) {
187 for (c = 0; c < channels; ++c) {
188 *dst = be32_to_cpu(buffer[c]) << 8;
189 dst++;
191 buffer += s->data_block_quadlets;
192 if (--remaining_frames == 0)
193 dst = (void *)runtime->dma_area;
197 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
198 unsigned int data_blocks)
200 struct amdtp_dot *p = s->protocol;
201 unsigned int channels, i, c;
203 channels = p->pcm_channels;
205 buffer++;
206 for (i = 0; i < data_blocks; ++i) {
207 for (c = 0; c < channels; ++c)
208 buffer[c] = cpu_to_be32(0x40000000);
209 buffer += s->data_block_quadlets;
213 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
215 struct amdtp_dot *p = s->protocol;
216 int used;
218 used = p->midi_fifo_used[port];
219 if (used == 0)
220 return true;
222 used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
223 used = max(used, 0);
224 p->midi_fifo_used[port] = used;
226 return used < p->midi_fifo_limit;
229 static inline void midi_use_bytes(struct amdtp_stream *s,
230 unsigned int port, unsigned int count)
232 struct amdtp_dot *p = s->protocol;
234 p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
237 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
238 unsigned int data_blocks)
240 struct amdtp_dot *p = s->protocol;
241 unsigned int f, port;
242 int len;
243 u8 *b;
245 for (f = 0; f < data_blocks; f++) {
246 port = (s->data_block_counter + f) % 8;
247 b = (u8 *)&buffer[0];
249 len = 0;
250 if (port < MAX_MIDI_PORTS &&
251 midi_ratelimit_per_packet(s, port) &&
252 p->midi[port] != NULL)
253 len = snd_rawmidi_transmit(p->midi[port], b + 1, 2);
255 if (len > 0) {
257 * Upper 4 bits of LSB represent port number.
258 * - 0000b: physical MIDI port 1.
259 * - 0010b: physical MIDI port 2.
260 * - 1110b: console MIDI port.
262 if (port == 2)
263 b[3] = 0xe0;
264 else if (port == 1)
265 b[3] = 0x20;
266 else
267 b[3] = 0x00;
268 b[3] |= len;
269 midi_use_bytes(s, port, len);
270 } else {
271 b[1] = 0;
272 b[2] = 0;
273 b[3] = 0;
275 b[0] = 0x80;
277 buffer += s->data_block_quadlets;
281 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
282 unsigned int data_blocks)
284 struct amdtp_dot *p = s->protocol;
285 unsigned int f, port, len;
286 u8 *b;
288 for (f = 0; f < data_blocks; f++) {
289 b = (u8 *)&buffer[0];
291 len = b[3] & 0x0f;
292 if (len > 0) {
294 * Upper 4 bits of LSB represent port number.
295 * - 0000b: physical MIDI port 1. Use port 0.
296 * - 1110b: console MIDI port. Use port 2.
298 if (b[3] >> 4 > 0)
299 port = 2;
300 else
301 port = 0;
303 if (port < MAX_MIDI_PORTS && p->midi[port])
304 snd_rawmidi_receive(p->midi[port], b + 1, len);
307 buffer += s->data_block_quadlets;
311 int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
312 struct snd_pcm_runtime *runtime)
314 int err;
316 /* This protocol delivers 24 bit data in 32bit data channel. */
317 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
318 if (err < 0)
319 return err;
321 return amdtp_stream_add_pcm_hw_constraints(s, runtime);
324 void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
325 struct snd_rawmidi_substream *midi)
327 struct amdtp_dot *p = s->protocol;
329 if (port < MAX_MIDI_PORTS)
330 WRITE_ONCE(p->midi[port], midi);
333 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
334 __be32 *buffer,
335 unsigned int data_blocks,
336 unsigned int *syt)
338 struct snd_pcm_substream *pcm;
339 unsigned int pcm_frames;
341 pcm = READ_ONCE(s->pcm);
342 if (pcm) {
343 read_pcm_s32(s, pcm, buffer, data_blocks);
344 pcm_frames = data_blocks;
345 } else {
346 pcm_frames = 0;
349 read_midi_messages(s, buffer, data_blocks);
351 return pcm_frames;
354 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
355 __be32 *buffer,
356 unsigned int data_blocks,
357 unsigned int *syt)
359 struct snd_pcm_substream *pcm;
360 unsigned int pcm_frames;
362 pcm = READ_ONCE(s->pcm);
363 if (pcm) {
364 write_pcm_s32(s, pcm, buffer, data_blocks);
365 pcm_frames = data_blocks;
366 } else {
367 write_pcm_silence(s, buffer, data_blocks);
368 pcm_frames = 0;
371 write_midi_messages(s, buffer, data_blocks);
373 return pcm_frames;
376 int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
377 enum amdtp_stream_direction dir)
379 amdtp_stream_process_data_blocks_t process_data_blocks;
380 enum cip_flags flags;
382 /* Use different mode between incoming/outgoing. */
383 if (dir == AMDTP_IN_STREAM) {
384 flags = CIP_NONBLOCKING;
385 process_data_blocks = process_tx_data_blocks;
386 } else {
387 flags = CIP_BLOCKING;
388 process_data_blocks = process_rx_data_blocks;
391 return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
392 process_data_blocks, sizeof(struct amdtp_dot));
395 void amdtp_dot_reset(struct amdtp_stream *s)
397 struct amdtp_dot *p = s->protocol;
399 p->state.carry = 0x00;
400 p->state.idx = 0x00;
401 p->state.off = 0;