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