1 // SPDX-License-Identifier: GPL-2.0
2 // ff-protocol-latter - a part of driver for RME Fireface series
3 //
4 // Copyright (c) 2019 Takashi Sakamoto
5 //
6 // Licensed under the terms of the GNU General Public License, version 2.
7 
8 #include <linux/delay.h>
9 
10 #include "ff.h"
11 
12 #define LATTER_STF		0xffff00000004
13 #define LATTER_ISOC_CHANNELS	0xffff00000008
14 #define LATTER_ISOC_START	0xffff0000000c
15 #define LATTER_FETCH_MODE	0xffff00000010
16 #define LATTER_SYNC_STATUS	0x0000801c0000
17 
18 static int parse_clock_bits(u32 data, unsigned int *rate,
19 			    enum snd_ff_clock_src *src)
20 {
21 	static const struct {
22 		unsigned int rate;
23 		u32 flag;
24 	} *rate_entry, rate_entries[] = {
25 		{ 32000,	0x00000000, },
26 		{ 44100,	0x01000000, },
27 		{ 48000,	0x02000000, },
28 		{ 64000,	0x04000000, },
29 		{ 88200,	0x05000000, },
30 		{ 96000,	0x06000000, },
31 		{ 128000,	0x08000000, },
32 		{ 176400,	0x09000000, },
33 		{ 192000,	0x0a000000, },
34 	};
35 	static const struct {
36 		enum snd_ff_clock_src src;
37 		u32 flag;
38 	} *clk_entry, clk_entries[] = {
39 		{ SND_FF_CLOCK_SRC_SPDIF,	0x00000200, },
40 		{ SND_FF_CLOCK_SRC_ADAT1,	0x00000400, },
41 		{ SND_FF_CLOCK_SRC_WORD,	0x00000600, },
42 		{ SND_FF_CLOCK_SRC_INTERNAL,	0x00000e00, },
43 	};
44 	int i;
45 
46 	for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
47 		rate_entry = rate_entries + i;
48 		if ((data & 0x0f000000) == rate_entry->flag) {
49 			*rate = rate_entry->rate;
50 			break;
51 		}
52 	}
53 	if (i == ARRAY_SIZE(rate_entries))
54 		return -EIO;
55 
56 	for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
57 		clk_entry = clk_entries + i;
58 		if ((data & 0x000e00) == clk_entry->flag) {
59 			*src = clk_entry->src;
60 			break;
61 		}
62 	}
63 	if (i == ARRAY_SIZE(clk_entries))
64 		return -EIO;
65 
66 	return 0;
67 }
68 
69 static int latter_get_clock(struct snd_ff *ff, unsigned int *rate,
70 			   enum snd_ff_clock_src *src)
71 {
72 	__le32 reg;
73 	u32 data;
74 	int err;
75 
76 	err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
77 				 LATTER_SYNC_STATUS, &reg, sizeof(reg), 0);
78 	if (err < 0)
79 		return err;
80 	data = le32_to_cpu(reg);
81 
82 	return parse_clock_bits(data, rate, src);
83 }
84 
85 static int latter_switch_fetching_mode(struct snd_ff *ff, bool enable)
86 {
87 	u32 data;
88 	__le32 reg;
89 
90 	if (enable)
91 		data = 0x00000000;
92 	else
93 		data = 0xffffffff;
94 	reg = cpu_to_le32(data);
95 
96 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
97 				  LATTER_FETCH_MODE, &reg, sizeof(reg), 0);
98 }
99 
100 static int keep_resources(struct snd_ff *ff, unsigned int rate)
101 {
102 	enum snd_ff_stream_mode mode;
103 	int i;
104 	int err;
105 
106 	// Check whether the given value is supported or not.
107 	for (i = 0; i < CIP_SFC_COUNT; i++) {
108 		if (amdtp_rate_table[i] == rate)
109 			break;
110 	}
111 	if (i >= CIP_SFC_COUNT)
112 		return -EINVAL;
113 
114 	err = snd_ff_stream_get_multiplier_mode(i, &mode);
115 	if (err < 0)
116 		return err;
117 
118 	/* Keep resources for in-stream. */
119 	ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
120 	err = fw_iso_resources_allocate(&ff->tx_resources,
121 			amdtp_stream_get_max_payload(&ff->tx_stream),
122 			fw_parent_device(ff->unit)->max_speed);
123 	if (err < 0)
124 		return err;
125 
126 	/* Keep resources for out-stream. */
127 	ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
128 	err = fw_iso_resources_allocate(&ff->rx_resources,
129 			amdtp_stream_get_max_payload(&ff->rx_stream),
130 			fw_parent_device(ff->unit)->max_speed);
131 	if (err < 0)
132 		fw_iso_resources_free(&ff->tx_resources);
133 
134 	return err;
135 }
136 
137 static int latter_begin_session(struct snd_ff *ff, unsigned int rate)
138 {
139 	static const struct {
140 		unsigned int stf;
141 		unsigned int code;
142 		unsigned int flag;
143 	} *entry, rate_table[] = {
144 		{ 32000,  0x00, 0x92, },
145 		{ 44100,  0x02, 0x92, },
146 		{ 48000,  0x04, 0x92, },
147 		{ 64000,  0x08, 0x8e, },
148 		{ 88200,  0x0a, 0x8e, },
149 		{ 96000,  0x0c, 0x8e, },
150 		{ 128000, 0x10, 0x8c, },
151 		{ 176400, 0x12, 0x8c, },
152 		{ 192000, 0x14, 0x8c, },
153 	};
154 	u32 data;
155 	__le32 reg;
156 	unsigned int count;
157 	int i;
158 	int err;
159 
160 	for (i = 0; i < ARRAY_SIZE(rate_table); ++i) {
161 		entry = rate_table + i;
162 		if (entry->stf == rate)
163 			break;
164 	}
165 	if (i == ARRAY_SIZE(rate_table))
166 		return -EINVAL;
167 
168 	reg = cpu_to_le32(entry->code);
169 	err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
170 				 LATTER_STF, &reg, sizeof(reg), 0);
171 	if (err < 0)
172 		return err;
173 
174 	// Confirm to shift transmission clock.
175 	count = 0;
176 	while (count++ < 10) {
177 		unsigned int curr_rate;
178 		enum snd_ff_clock_src src;
179 
180 		err = latter_get_clock(ff, &curr_rate, &src);
181 		if (err < 0)
182 			return err;
183 
184 		if (curr_rate == rate)
185 			break;
186 	}
187 	if (count == 10)
188 		return -ETIMEDOUT;
189 
190 	err = keep_resources(ff, rate);
191 	if (err < 0)
192 		return err;
193 
194 	data = (ff->tx_resources.channel << 8) | ff->rx_resources.channel;
195 	reg = cpu_to_le32(data);
196 	err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
197 				 LATTER_ISOC_CHANNELS, &reg, sizeof(reg), 0);
198 	if (err < 0)
199 		return err;
200 
201 	// Always use the maximum number of data channels in data block of
202 	// packet.
203 	reg = cpu_to_le32(entry->flag);
204 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
205 				  LATTER_ISOC_START, &reg, sizeof(reg), 0);
206 }
207 
208 static void latter_finish_session(struct snd_ff *ff)
209 {
210 	__le32 reg;
211 
212 	reg = cpu_to_le32(0x00000000);
213 	snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
214 			   LATTER_ISOC_START, &reg, sizeof(reg), 0);
215 }
216 
217 static void latter_dump_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
218 {
219 	static const struct {
220 		char *const label;
221 		u32 locked_mask;
222 		u32 synced_mask;
223 	} *clk_entry, clk_entries[] = {
224 		{ "S/PDIF",	0x00000001, 0x00000010, },
225 		{ "ADAT",	0x00000002, 0x00000020, },
226 		{ "WDClk",	0x00000004, 0x00000040, },
227 	};
228 	__le32 reg;
229 	u32 data;
230 	unsigned int rate;
231 	enum snd_ff_clock_src src;
232 	const char *label;
233 	int i;
234 	int err;
235 
236 	err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
237 				 LATTER_SYNC_STATUS, &reg, sizeof(reg), 0);
238 	if (err < 0)
239 		return;
240 	data = le32_to_cpu(reg);
241 
242 	snd_iprintf(buffer, "External source detection:\n");
243 
244 	for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
245 		clk_entry = clk_entries + i;
246 		snd_iprintf(buffer, "%s: ", clk_entry->label);
247 		if (data & clk_entry->locked_mask) {
248 			if (data & clk_entry->synced_mask)
249 				snd_iprintf(buffer, "sync\n");
250 			else
251 				snd_iprintf(buffer, "lock\n");
252 		} else {
253 			snd_iprintf(buffer, "none\n");
254 		}
255 	}
256 
257 	err = parse_clock_bits(data, &rate, &src);
258 	if (err < 0)
259 		return;
260 	label = snd_ff_proc_get_clk_label(src);
261 	if (!label)
262 		return;
263 
264 	snd_iprintf(buffer, "Referred clock: %s %d\n", label, rate);
265 }
266 
267 // NOTE: transactions are transferred within 0x00-0x7f in allocated range of
268 // address. This seems to be for check of discontinuity in receiver side.
269 //
270 // Like Fireface 400, drivers can select one of 4 options for lower 4 bytes of
271 // destination address by bit flags in quadlet register (little endian) at
272 // 0x'ffff'0000'0014:
273 //
274 // bit flags: offset of destination address
275 // - 0x00002000: 0x'....'....'0000'0000
276 // - 0x00004000: 0x'....'....'0000'0080
277 // - 0x00008000: 0x'....'....'0000'0100
278 // - 0x00010000: 0x'....'....'0000'0180
279 //
280 // Drivers can suppress the device to transfer asynchronous transactions by
281 // clear these bit flags.
282 //
283 // Actually, the register is write-only and includes the other settings such as
284 // input attenuation. This driver allocates for the first option
285 // (0x'....'....'0000'0000) and expects userspace application to configure the
286 // register for it.
287 static void latter_handle_midi_msg(struct snd_ff *ff, unsigned int offset,
288 				   __le32 *buf, size_t length)
289 {
290 	u32 data = le32_to_cpu(*buf);
291 	unsigned int index = (data & 0x000000f0) >> 4;
292 	u8 byte[3];
293 	struct snd_rawmidi_substream *substream;
294 	unsigned int len;
295 
296 	if (index >= ff->spec->midi_in_ports)
297 		return;
298 
299 	switch (data & 0x0000000f) {
300 	case 0x00000008:
301 	case 0x00000009:
302 	case 0x0000000a:
303 	case 0x0000000b:
304 	case 0x0000000e:
305 		len = 3;
306 		break;
307 	case 0x0000000c:
308 	case 0x0000000d:
309 		len = 2;
310 		break;
311 	default:
312 		len = data & 0x00000003;
313 		if (len == 0)
314 			len = 3;
315 		break;
316 	}
317 
318 	byte[0] = (data & 0x0000ff00) >> 8;
319 	byte[1] = (data & 0x00ff0000) >> 16;
320 	byte[2] = (data & 0xff000000) >> 24;
321 
322 	substream = READ_ONCE(ff->tx_midi_substreams[index]);
323 	if (substream)
324 		snd_rawmidi_receive(substream, byte, len);
325 }
326 
327 /*
328  * When return minus value, given argument is not MIDI status.
329  * When return 0, given argument is a beginning of system exclusive.
330  * When return the others, given argument is MIDI data.
331  */
332 static inline int calculate_message_bytes(u8 status)
333 {
334 	switch (status) {
335 	case 0xf6:	/* Tune request. */
336 	case 0xf8:	/* Timing clock. */
337 	case 0xfa:	/* Start. */
338 	case 0xfb:	/* Continue. */
339 	case 0xfc:	/* Stop. */
340 	case 0xfe:	/* Active sensing. */
341 	case 0xff:	/* System reset. */
342 		return 1;
343 	case 0xf1:	/* MIDI time code quarter frame. */
344 	case 0xf3:	/* Song select. */
345 		return 2;
346 	case 0xf2:	/* Song position pointer. */
347 		return 3;
348 	case 0xf0:	/* Exclusive. */
349 		return 0;
350 	case 0xf7:	/* End of exclusive. */
351 		break;
352 	case 0xf4:	/* Undefined. */
353 	case 0xf5:	/* Undefined. */
354 	case 0xf9:	/* Undefined. */
355 	case 0xfd:	/* Undefined. */
356 		break;
357 	default:
358 		switch (status & 0xf0) {
359 		case 0x80:	/* Note on. */
360 		case 0x90:	/* Note off. */
361 		case 0xa0:	/* Polyphonic key pressure. */
362 		case 0xb0:	/* Control change and Mode change. */
363 		case 0xe0:	/* Pitch bend change. */
364 			return 3;
365 		case 0xc0:	/* Program change. */
366 		case 0xd0:	/* Channel pressure. */
367 			return 2;
368 		default:
369 		break;
370 		}
371 	break;
372 	}
373 
374 	return -EINVAL;
375 }
376 
377 static int latter_fill_midi_msg(struct snd_ff *ff,
378 				struct snd_rawmidi_substream *substream,
379 				unsigned int port)
380 {
381 	u32 data = {0};
382 	u8 *buf = (u8 *)&data;
383 	int consumed;
384 
385 	buf[0] = port << 4;
386 	consumed = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
387 	if (consumed <= 0)
388 		return consumed;
389 
390 	if (!ff->on_sysex[port]) {
391 		if (buf[1] != 0xf0) {
392 			if (consumed < calculate_message_bytes(buf[1]))
393 				return 0;
394 		} else {
395 			// The beginning of exclusives.
396 			ff->on_sysex[port] = true;
397 		}
398 
399 		buf[0] |= consumed;
400 	} else {
401 		if (buf[1] != 0xf7) {
402 			if (buf[2] == 0xf7 || buf[3] == 0xf7) {
403 				// Transfer end code at next time.
404 				consumed -= 1;
405 			}
406 
407 			buf[0] |= consumed;
408 		} else {
409 			// The end of exclusives.
410 			ff->on_sysex[port] = false;
411 			consumed = 1;
412 			buf[0] |= 0x0f;
413 		}
414 	}
415 
416 	ff->msg_buf[port][0] = cpu_to_le32(data);
417 	ff->rx_bytes[port] = consumed;
418 
419 	return 1;
420 }
421 
422 const struct snd_ff_protocol snd_ff_protocol_latter = {
423 	.handle_midi_msg	= latter_handle_midi_msg,
424 	.fill_midi_msg		= latter_fill_midi_msg,
425 	.get_clock		= latter_get_clock,
426 	.switch_fetching_mode	= latter_switch_fetching_mode,
427 	.begin_session		= latter_begin_session,
428 	.finish_session		= latter_finish_session,
429 	.dump_status		= latter_dump_status,
430 };
431