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 /*
32  * The double-oh-three algorithm was discovered by Robin Gareus and Damien
33  * Zammit in 2012, with reverse-engineering for Digi 003 Rack.
34  */
35 struct dot_state {
36 	u8 carry;
37 	u8 idx;
38 	unsigned int off;
39 };
40 
41 struct amdtp_dot {
42 	unsigned int pcm_channels;
43 	struct dot_state state;
44 
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;
50 
51 	void (*transfer_samples)(struct amdtp_stream *s,
52 				 struct snd_pcm_substream *pcm,
53 				 __be32 *buffer, unsigned int frames);
54 };
55 
56 /*
57  * double-oh-three look up table
58  *
59  * @param idx index byte (audio-sample data) 0x00..0xff
60  * @param off channel offset shift
61  * @return salt to XOR with given data
62  */
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 const u8 dot_scrt(const u8 idx, const unsigned int off)
67 {
68 	/*
69 	 * the length of the added pattern only depends on the lower nibble
70 	 * of the last non-zero data
71 	 */
72 	static const u8 len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14,
73 				   12, 10, 8, 6, 4, 2, 0};
74 
75 	/*
76 	 * the lower nibble of the salt. Interleaved sequence.
77 	 * this is walked backwards according to len[]
78 	 */
79 	static const u8 nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4,
80 				   0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
81 
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};
85 
86 	/*
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.
91 	 */
92 	static const u8 hio[16] = {0, 11, 12, 6, 7, 5, 1, 4,
93 				   3, 0x00, 14, 13, 8, 9, 10, 2};
94 
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];
98 
99 	if (len[ln] < off)
100 		return 0x00;
101 
102 	return ((nib[14 + off - len[ln]]) | (hr << 4));
103 }
104 
105 static void dot_encode_step(struct dot_state *state, __be32 *const buffer)
106 {
107 	u8 * const data = (u8 *) buffer;
108 
109 	if (data[MAGIC_DOT_BYTE] != 0x00) {
110 		state->off = 0;
111 		state->idx = data[MAGIC_DOT_BYTE] ^ state->carry;
112 	}
113 	data[MAGIC_DOT_BYTE] ^= state->carry;
114 	state->carry = dot_scrt(state->idx, ++(state->off));
115 }
116 
117 int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate,
118 			     unsigned int pcm_channels)
119 {
120 	struct amdtp_dot *p = s->protocol;
121 	int err;
122 
123 	if (amdtp_stream_running(s))
124 		return -EBUSY;
125 
126 	/*
127 	 * A first data channel is for MIDI conformant data channel, the rest is
128 	 * Multi Bit Linear Audio data channel.
129 	 */
130 	err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1);
131 	if (err < 0)
132 		return err;
133 
134 	s->fdf = AMDTP_FDF_AM824 | s->sfc;
135 
136 	p->pcm_channels = pcm_channels;
137 
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;
142 
143 	/*
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().)
148 	 */
149 	p->midi_fifo_limit = rate - MIDI_BYTES_PER_SECOND * s->syt_interval + 1;
150 
151 	return 0;
152 }
153 
154 static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
155 			  __be32 *buffer, unsigned int frames)
156 {
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;
161 
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;
166 
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++;
173 		}
174 		buffer += s->data_block_quadlets;
175 		if (--remaining_frames == 0)
176 			src = (void *)runtime->dma_area;
177 	}
178 }
179 
180 static void write_pcm_s16(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
181 			  __be32 *buffer, unsigned int frames)
182 {
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;
187 
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;
192 
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++;
199 		}
200 		buffer += s->data_block_quadlets;
201 		if (--remaining_frames == 0)
202 			src = (void *)runtime->dma_area;
203 	}
204 }
205 
206 static void read_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
207 			 __be32 *buffer, unsigned int frames)
208 {
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;
213 
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;
218 
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++;
224 		}
225 		buffer += s->data_block_quadlets;
226 		if (--remaining_frames == 0)
227 			dst = (void *)runtime->dma_area;
228 	}
229 }
230 
231 static void write_pcm_silence(struct amdtp_stream *s, __be32 *buffer,
232 			      unsigned int data_blocks)
233 {
234 	struct amdtp_dot *p = s->protocol;
235 	unsigned int channels, i, c;
236 
237 	channels = p->pcm_channels;
238 
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;
244 	}
245 }
246 
247 static bool midi_ratelimit_per_packet(struct amdtp_stream *s, unsigned int port)
248 {
249 	struct amdtp_dot *p = s->protocol;
250 	int used;
251 
252 	used = p->midi_fifo_used[port];
253 	if (used == 0)
254 		return true;
255 
256 	used -= MIDI_BYTES_PER_SECOND * s->syt_interval;
257 	used = max(used, 0);
258 	p->midi_fifo_used[port] = used;
259 
260 	return used < p->midi_fifo_limit;
261 }
262 
263 static inline void midi_use_bytes(struct amdtp_stream *s,
264 				  unsigned int port, unsigned int count)
265 {
266 	struct amdtp_dot *p = s->protocol;
267 
268 	p->midi_fifo_used[port] += amdtp_rate_table[s->sfc] * count;
269 }
270 
271 static void write_midi_messages(struct amdtp_stream *s, __be32 *buffer,
272 				unsigned int data_blocks)
273 {
274 	struct amdtp_dot *p = s->protocol;
275 	unsigned int f, port;
276 	int len;
277 	u8 *b;
278 
279 	for (f = 0; f < data_blocks; f++) {
280 		port = (s->data_block_counter + f) % 8;
281 		b = (u8 *)&buffer[0];
282 
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);
288 
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;
296 		}
297 		b[0] = 0x80;
298 
299 		buffer += s->data_block_quadlets;
300 	}
301 }
302 
303 static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer,
304 			       unsigned int data_blocks)
305 {
306 	struct amdtp_dot *p = s->protocol;
307 	unsigned int f, port, len;
308 	u8 *b;
309 
310 	for (f = 0; f < data_blocks; f++) {
311 		b = (u8 *)&buffer[0];
312 		port = b[3] >> 4;
313 		len = b[3] & 0x0f;
314 
315 		if (port < p->midi_ports && p->midi[port] && len > 0)
316 			snd_rawmidi_receive(p->midi[port], b + 1, len);
317 
318 		buffer += s->data_block_quadlets;
319 	}
320 }
321 
322 int amdtp_dot_add_pcm_hw_constraints(struct amdtp_stream *s,
323 				     struct snd_pcm_runtime *runtime)
324 {
325 	int err;
326 
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;
331 
332 	return amdtp_stream_add_pcm_hw_constraints(s, runtime);
333 }
334 
335 void amdtp_dot_set_pcm_format(struct amdtp_stream *s, snd_pcm_format_t format)
336 {
337 	struct amdtp_dot *p = s->protocol;
338 
339 	if (WARN_ON(amdtp_stream_pcm_running(s)))
340 		return;
341 
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;
350 		}
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;
359 	}
360 }
361 
362 void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port,
363 			  struct snd_rawmidi_substream *midi)
364 {
365 	struct amdtp_dot *p = s->protocol;
366 
367 	if (port < p->midi_ports)
368 		ACCESS_ONCE(p->midi[port]) = midi;
369 }
370 
371 static unsigned int process_tx_data_blocks(struct amdtp_stream *s,
372 					   __be32 *buffer,
373 					   unsigned int data_blocks,
374 					   unsigned int *syt)
375 {
376 	struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
377 	struct snd_pcm_substream *pcm;
378 	unsigned int pcm_frames;
379 
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;
386 	}
387 
388 	read_midi_messages(s, buffer, data_blocks);
389 
390 	return pcm_frames;
391 }
392 
393 static unsigned int process_rx_data_blocks(struct amdtp_stream *s,
394 					   __be32 *buffer,
395 					   unsigned int data_blocks,
396 					   unsigned int *syt)
397 {
398 	struct amdtp_dot *p = (struct amdtp_dot *)s->protocol;
399 	struct snd_pcm_substream *pcm;
400 	unsigned int pcm_frames;
401 
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;
409 	}
410 
411 	write_midi_messages(s, buffer, data_blocks);
412 
413 	return pcm_frames;
414 }
415 
416 int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit,
417 		 enum amdtp_stream_direction dir)
418 {
419 	amdtp_stream_process_data_blocks_t process_data_blocks;
420 	enum cip_flags flags;
421 
422 	/* Use different mode between incoming/outgoing. */
423 	if (dir == AMDTP_IN_STREAM) {
424 		flags = CIP_NONBLOCKING | CIP_SKIP_INIT_DBC_CHECK;
425 		process_data_blocks = process_tx_data_blocks;
426 	} else {
427 		flags = CIP_BLOCKING;
428 		process_data_blocks = process_rx_data_blocks;
429 	}
430 
431 	return amdtp_stream_init(s, unit, dir, flags, CIP_FMT_AM,
432 				 process_data_blocks, sizeof(struct amdtp_dot));
433 }
434 
435 void amdtp_dot_reset(struct amdtp_stream *s)
436 {
437 	struct amdtp_dot *p = s->protocol;
438 
439 	p->state.carry = 0x00;
440 	p->state.idx = 0x00;
441 	p->state.off = 0;
442 }
443