xref: /openbmc/linux/sound/firewire/fireface/ff-protocol-latter.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1fd1cc9deSTakashi Sakamoto // SPDX-License-Identifier: GPL-2.0
21dd0dd0bSChristophe JAILLET // ff-protocol-latter.c - a part of driver for RME Fireface series
3fd1cc9deSTakashi Sakamoto //
4fd1cc9deSTakashi Sakamoto // Copyright (c) 2019 Takashi Sakamoto
5fd1cc9deSTakashi Sakamoto 
6fd1cc9deSTakashi Sakamoto #include <linux/delay.h>
7fd1cc9deSTakashi Sakamoto 
8fd1cc9deSTakashi Sakamoto #include "ff.h"
9fd1cc9deSTakashi Sakamoto 
106954158aSGeert Uytterhoeven #define LATTER_STF		0xffff00000004ULL
116954158aSGeert Uytterhoeven #define LATTER_ISOC_CHANNELS	0xffff00000008ULL
126954158aSGeert Uytterhoeven #define LATTER_ISOC_START	0xffff0000000cULL
136954158aSGeert Uytterhoeven #define LATTER_FETCH_MODE	0xffff00000010ULL
146954158aSGeert Uytterhoeven #define LATTER_SYNC_STATUS	0x0000801c0000ULL
15fd1cc9deSTakashi Sakamoto 
16c50bfc8aSTakashi Sakamoto // The content of sync status register differs between models.
17c50bfc8aSTakashi Sakamoto //
18c50bfc8aSTakashi Sakamoto // Fireface UCX:
19c50bfc8aSTakashi Sakamoto //  0xf0000000: (unidentified)
20c50bfc8aSTakashi Sakamoto //  0x0f000000: effective rate of sampling clock
21c50bfc8aSTakashi Sakamoto //  0x00f00000: detected rate of word clock on BNC interface
22c50bfc8aSTakashi Sakamoto //  0x000f0000: detected rate of ADAT or S/PDIF on optical interface
23c50bfc8aSTakashi Sakamoto //  0x0000f000: detected rate of S/PDIF on coaxial interface
24c50bfc8aSTakashi Sakamoto //  0x00000e00: effective source of sampling clock
25c50bfc8aSTakashi Sakamoto //    0x00000e00: Internal
26c50bfc8aSTakashi Sakamoto //    0x00000800: (unidentified)
27c50bfc8aSTakashi Sakamoto //    0x00000600: Word clock on BNC interface
28c50bfc8aSTakashi Sakamoto //    0x00000400: ADAT on optical interface
29c50bfc8aSTakashi Sakamoto //    0x00000200: S/PDIF on coaxial or optical interface
30c50bfc8aSTakashi Sakamoto //  0x00000100: Optical interface is used for ADAT signal
31c50bfc8aSTakashi Sakamoto //  0x00000080: (unidentified)
32c50bfc8aSTakashi Sakamoto //  0x00000040: Synchronized to word clock on BNC interface
33c50bfc8aSTakashi Sakamoto //  0x00000020: Synchronized to ADAT or S/PDIF on optical interface
34c50bfc8aSTakashi Sakamoto //  0x00000010: Synchronized to S/PDIF on coaxial interface
35c50bfc8aSTakashi Sakamoto //  0x00000008: (unidentified)
36c50bfc8aSTakashi Sakamoto //  0x00000004: Lock word clock on BNC interface
37c50bfc8aSTakashi Sakamoto //  0x00000002: Lock ADAT or S/PDIF on optical interface
38c50bfc8aSTakashi Sakamoto //  0x00000001: Lock S/PDIF on coaxial interface
39c50bfc8aSTakashi Sakamoto //
40c50bfc8aSTakashi Sakamoto // Fireface 802 (and perhaps UFX):
41c50bfc8aSTakashi Sakamoto //   0xf0000000: effective rate of sampling clock
42c50bfc8aSTakashi Sakamoto //   0x0f000000: detected rate of ADAT-B on 2nd optical interface
43c50bfc8aSTakashi Sakamoto //   0x00f00000: detected rate of ADAT-A on 1st optical interface
44c50bfc8aSTakashi Sakamoto //   0x000f0000: detected rate of AES/EBU on XLR or coaxial interface
45c50bfc8aSTakashi Sakamoto //   0x0000f000: detected rate of word clock on BNC interface
46c50bfc8aSTakashi Sakamoto //   0x00000e00: effective source of sampling clock
47c50bfc8aSTakashi Sakamoto //     0x00000e00: internal
48c50bfc8aSTakashi Sakamoto //     0x00000800: ADAT-B
49c50bfc8aSTakashi Sakamoto //     0x00000600: ADAT-A
50c50bfc8aSTakashi Sakamoto //     0x00000400: AES/EBU
51c50bfc8aSTakashi Sakamoto //     0x00000200: Word clock
52c50bfc8aSTakashi Sakamoto //   0x00000080: Synchronized to ADAT-B on 2nd optical interface
53c50bfc8aSTakashi Sakamoto //   0x00000040: Synchronized to ADAT-A on 1st optical interface
54c50bfc8aSTakashi Sakamoto //   0x00000020: Synchronized to AES/EBU on XLR or 2nd optical interface
55c50bfc8aSTakashi Sakamoto //   0x00000010: Synchronized to word clock on BNC interface
56c50bfc8aSTakashi Sakamoto //   0x00000008: Lock ADAT-B on 2nd optical interface
57c50bfc8aSTakashi Sakamoto //   0x00000004: Lock ADAT-A on 1st optical interface
58c50bfc8aSTakashi Sakamoto //   0x00000002: Lock AES/EBU on XLR or 2nd optical interface
59c50bfc8aSTakashi Sakamoto //   0x00000001: Lock word clock on BNC interface
60c50bfc8aSTakashi Sakamoto //
61c50bfc8aSTakashi Sakamoto // The pattern for rate bits:
62c50bfc8aSTakashi Sakamoto //   0x00: 32.0 kHz
63c50bfc8aSTakashi Sakamoto //   0x01: 44.1 kHz
64c50bfc8aSTakashi Sakamoto //   0x02: 48.0 kHz
65c50bfc8aSTakashi Sakamoto //   0x04: 64.0 kHz
66c50bfc8aSTakashi Sakamoto //   0x05: 88.2 kHz
67c50bfc8aSTakashi Sakamoto //   0x06: 96.0 kHz
68c50bfc8aSTakashi Sakamoto //   0x08: 128.0 kHz
69c50bfc8aSTakashi Sakamoto //   0x09: 176.4 kHz
70c50bfc8aSTakashi Sakamoto //   0x0a: 192.0 kHz
parse_clock_bits(u32 data,unsigned int * rate,enum snd_ff_clock_src * src,enum snd_ff_unit_version unit_version)71fd1cc9deSTakashi Sakamoto static int parse_clock_bits(u32 data, unsigned int *rate,
72062bb452STakashi Sakamoto 			    enum snd_ff_clock_src *src,
73062bb452STakashi Sakamoto 			    enum snd_ff_unit_version unit_version)
74fd1cc9deSTakashi Sakamoto {
75fd1cc9deSTakashi Sakamoto 	static const struct {
76fd1cc9deSTakashi Sakamoto 		unsigned int rate;
77fd1cc9deSTakashi Sakamoto 		u32 flag;
78fd1cc9deSTakashi Sakamoto 	} *rate_entry, rate_entries[] = {
79c50bfc8aSTakashi Sakamoto 		{ 32000,	0x00, },
80c50bfc8aSTakashi Sakamoto 		{ 44100,	0x01, },
81c50bfc8aSTakashi Sakamoto 		{ 48000,	0x02, },
82c50bfc8aSTakashi Sakamoto 		{ 64000,	0x04, },
83c50bfc8aSTakashi Sakamoto 		{ 88200,	0x05, },
84c50bfc8aSTakashi Sakamoto 		{ 96000,	0x06, },
85c50bfc8aSTakashi Sakamoto 		{ 128000,	0x08, },
86c50bfc8aSTakashi Sakamoto 		{ 176400,	0x09, },
87c50bfc8aSTakashi Sakamoto 		{ 192000,	0x0a, },
88fd1cc9deSTakashi Sakamoto 	};
89fd1cc9deSTakashi Sakamoto 	static const struct {
90fd1cc9deSTakashi Sakamoto 		enum snd_ff_clock_src src;
91fd1cc9deSTakashi Sakamoto 		u32 flag;
92c50bfc8aSTakashi Sakamoto 	} *clk_entry, *clk_entries, ucx_clk_entries[] = {
93fd1cc9deSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_SPDIF,	0x00000200, },
94fd1cc9deSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_ADAT1,	0x00000400, },
95fd1cc9deSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_WORD,	0x00000600, },
96fd1cc9deSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_INTERNAL,	0x00000e00, },
97c50bfc8aSTakashi Sakamoto 	}, ufx_ff802_clk_entries[] = {
98c50bfc8aSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_WORD,	0x00000200, },
99c50bfc8aSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_SPDIF,	0x00000400, },
100c50bfc8aSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_ADAT1,	0x00000600, },
101c50bfc8aSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_ADAT2,	0x00000800, },
102c50bfc8aSTakashi Sakamoto 		{ SND_FF_CLOCK_SRC_INTERNAL,	0x00000e00, },
103fd1cc9deSTakashi Sakamoto 	};
104c50bfc8aSTakashi Sakamoto 	u32 rate_bits;
105c50bfc8aSTakashi Sakamoto 	unsigned int clk_entry_count;
106fd1cc9deSTakashi Sakamoto 	int i;
107fd1cc9deSTakashi Sakamoto 
108c50bfc8aSTakashi Sakamoto 	if (unit_version == SND_FF_UNIT_VERSION_UCX) {
109c50bfc8aSTakashi Sakamoto 		rate_bits = (data & 0x0f000000) >> 24;
110c50bfc8aSTakashi Sakamoto 		clk_entries = ucx_clk_entries;
111c50bfc8aSTakashi Sakamoto 		clk_entry_count = ARRAY_SIZE(ucx_clk_entries);
112c50bfc8aSTakashi Sakamoto 	} else {
113c50bfc8aSTakashi Sakamoto 		rate_bits = (data & 0xf0000000) >> 28;
114c50bfc8aSTakashi Sakamoto 		clk_entries = ufx_ff802_clk_entries;
115c50bfc8aSTakashi Sakamoto 		clk_entry_count = ARRAY_SIZE(ufx_ff802_clk_entries);
116062bb452STakashi Sakamoto 	}
117062bb452STakashi Sakamoto 
118fd1cc9deSTakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
119fd1cc9deSTakashi Sakamoto 		rate_entry = rate_entries + i;
120c50bfc8aSTakashi Sakamoto 		if (rate_bits == rate_entry->flag) {
121fd1cc9deSTakashi Sakamoto 			*rate = rate_entry->rate;
122fd1cc9deSTakashi Sakamoto 			break;
123fd1cc9deSTakashi Sakamoto 		}
124fd1cc9deSTakashi Sakamoto 	}
125fd1cc9deSTakashi Sakamoto 	if (i == ARRAY_SIZE(rate_entries))
126fd1cc9deSTakashi Sakamoto 		return -EIO;
127fd1cc9deSTakashi Sakamoto 
128c50bfc8aSTakashi Sakamoto 	for (i = 0; i < clk_entry_count; ++i) {
129fd1cc9deSTakashi Sakamoto 		clk_entry = clk_entries + i;
130fd1cc9deSTakashi Sakamoto 		if ((data & 0x000e00) == clk_entry->flag) {
131fd1cc9deSTakashi Sakamoto 			*src = clk_entry->src;
132fd1cc9deSTakashi Sakamoto 			break;
133fd1cc9deSTakashi Sakamoto 		}
134fd1cc9deSTakashi Sakamoto 	}
135c50bfc8aSTakashi Sakamoto 	if (i == clk_entry_count)
136fd1cc9deSTakashi Sakamoto 		return -EIO;
137fd1cc9deSTakashi Sakamoto 
138fd1cc9deSTakashi Sakamoto 	return 0;
139fd1cc9deSTakashi Sakamoto }
140fd1cc9deSTakashi Sakamoto 
latter_get_clock(struct snd_ff * ff,unsigned int * rate,enum snd_ff_clock_src * src)141fd1cc9deSTakashi Sakamoto static int latter_get_clock(struct snd_ff *ff, unsigned int *rate,
142fd1cc9deSTakashi Sakamoto 			   enum snd_ff_clock_src *src)
143fd1cc9deSTakashi Sakamoto {
144fd1cc9deSTakashi Sakamoto 	__le32 reg;
145fd1cc9deSTakashi Sakamoto 	u32 data;
146fd1cc9deSTakashi Sakamoto 	int err;
147fd1cc9deSTakashi Sakamoto 
148fd1cc9deSTakashi Sakamoto 	err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
149fd1cc9deSTakashi Sakamoto 				 LATTER_SYNC_STATUS, &reg, sizeof(reg), 0);
150fd1cc9deSTakashi Sakamoto 	if (err < 0)
151fd1cc9deSTakashi Sakamoto 		return err;
152fd1cc9deSTakashi Sakamoto 	data = le32_to_cpu(reg);
153fd1cc9deSTakashi Sakamoto 
154062bb452STakashi Sakamoto 	return parse_clock_bits(data, rate, src, ff->unit_version);
155fd1cc9deSTakashi Sakamoto }
156fd1cc9deSTakashi Sakamoto 
latter_switch_fetching_mode(struct snd_ff * ff,bool enable)157fd1cc9deSTakashi Sakamoto static int latter_switch_fetching_mode(struct snd_ff *ff, bool enable)
158fd1cc9deSTakashi Sakamoto {
159fd1cc9deSTakashi Sakamoto 	u32 data;
160fd1cc9deSTakashi Sakamoto 	__le32 reg;
161fd1cc9deSTakashi Sakamoto 
162fd1cc9deSTakashi Sakamoto 	if (enable)
163fd1cc9deSTakashi Sakamoto 		data = 0x00000000;
164fd1cc9deSTakashi Sakamoto 	else
165fd1cc9deSTakashi Sakamoto 		data = 0xffffffff;
166fd1cc9deSTakashi Sakamoto 	reg = cpu_to_le32(data);
167fd1cc9deSTakashi Sakamoto 
168fd1cc9deSTakashi Sakamoto 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
169fd1cc9deSTakashi Sakamoto 				  LATTER_FETCH_MODE, &reg, sizeof(reg), 0);
170fd1cc9deSTakashi Sakamoto }
171fd1cc9deSTakashi Sakamoto 
latter_allocate_resources(struct snd_ff * ff,unsigned int rate)17260aec494STakashi Sakamoto static int latter_allocate_resources(struct snd_ff *ff, unsigned int rate)
173fd1cc9deSTakashi Sakamoto {
174fd1cc9deSTakashi Sakamoto 	enum snd_ff_stream_mode mode;
175fd1cc9deSTakashi Sakamoto 	unsigned int code;
176fd1cc9deSTakashi Sakamoto 	__le32 reg;
177fd1cc9deSTakashi Sakamoto 	unsigned int count;
178fd1cc9deSTakashi Sakamoto 	int i;
179fd1cc9deSTakashi Sakamoto 	int err;
180fd1cc9deSTakashi Sakamoto 
18160aec494STakashi Sakamoto 	// Set the number of data blocks transferred in a second.
182bbd6aac3STakashi Sakamoto 	if (rate % 48000 == 0)
183bbd6aac3STakashi Sakamoto 		code = 0x04;
18460aec494STakashi Sakamoto 	else if (rate % 44100 == 0)
18560aec494STakashi Sakamoto 		code = 0x02;
186bbd6aac3STakashi Sakamoto 	else if (rate % 32000 == 0)
187bbd6aac3STakashi Sakamoto 		code = 0x00;
18860aec494STakashi Sakamoto 	else
189fd1cc9deSTakashi Sakamoto 		return -EINVAL;
190fd1cc9deSTakashi Sakamoto 
19160aec494STakashi Sakamoto 	if (rate >= 64000 && rate < 128000)
19260aec494STakashi Sakamoto 		code |= 0x08;
193bbd6aac3STakashi Sakamoto 	else if (rate >= 128000)
19460aec494STakashi Sakamoto 		code |= 0x10;
19560aec494STakashi Sakamoto 
19660aec494STakashi Sakamoto 	reg = cpu_to_le32(code);
197fd1cc9deSTakashi Sakamoto 	err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
198fd1cc9deSTakashi Sakamoto 				 LATTER_STF, &reg, sizeof(reg), 0);
199fd1cc9deSTakashi Sakamoto 	if (err < 0)
200fd1cc9deSTakashi Sakamoto 		return err;
201fd1cc9deSTakashi Sakamoto 
202fd1cc9deSTakashi Sakamoto 	// Confirm to shift transmission clock.
203fd1cc9deSTakashi Sakamoto 	count = 0;
204fd1cc9deSTakashi Sakamoto 	while (count++ < 10) {
205fd1cc9deSTakashi Sakamoto 		unsigned int curr_rate;
206fd1cc9deSTakashi Sakamoto 		enum snd_ff_clock_src src;
207fd1cc9deSTakashi Sakamoto 
208fd1cc9deSTakashi Sakamoto 		err = latter_get_clock(ff, &curr_rate, &src);
209fd1cc9deSTakashi Sakamoto 		if (err < 0)
210fd1cc9deSTakashi Sakamoto 			return err;
211fd1cc9deSTakashi Sakamoto 
212fd1cc9deSTakashi Sakamoto 		if (curr_rate == rate)
213fd1cc9deSTakashi Sakamoto 			break;
214fd1cc9deSTakashi Sakamoto 	}
215bbd6aac3STakashi Sakamoto 	if (count > 10)
216fd1cc9deSTakashi Sakamoto 		return -ETIMEDOUT;
217fd1cc9deSTakashi Sakamoto 
21860aec494STakashi Sakamoto 	for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); ++i) {
21960aec494STakashi Sakamoto 		if (rate == amdtp_rate_table[i])
22060aec494STakashi Sakamoto 			break;
22160aec494STakashi Sakamoto 	}
22260aec494STakashi Sakamoto 	if (i == ARRAY_SIZE(amdtp_rate_table))
22360aec494STakashi Sakamoto 		return -EINVAL;
22460aec494STakashi Sakamoto 
22560aec494STakashi Sakamoto 	err = snd_ff_stream_get_multiplier_mode(i, &mode);
226fd1cc9deSTakashi Sakamoto 	if (err < 0)
227fd1cc9deSTakashi Sakamoto 		return err;
228fd1cc9deSTakashi Sakamoto 
22960aec494STakashi Sakamoto 	// Keep resources for in-stream.
23060aec494STakashi Sakamoto 	ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
23160aec494STakashi Sakamoto 	err = fw_iso_resources_allocate(&ff->tx_resources,
23260aec494STakashi Sakamoto 			amdtp_stream_get_max_payload(&ff->tx_stream),
23360aec494STakashi Sakamoto 			fw_parent_device(ff->unit)->max_speed);
23460aec494STakashi Sakamoto 	if (err < 0)
23560aec494STakashi Sakamoto 		return err;
23660aec494STakashi Sakamoto 
23760aec494STakashi Sakamoto 	// Keep resources for out-stream.
23860aec494STakashi Sakamoto 	ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
23960aec494STakashi Sakamoto 	err = fw_iso_resources_allocate(&ff->rx_resources,
24060aec494STakashi Sakamoto 			amdtp_stream_get_max_payload(&ff->rx_stream),
24160aec494STakashi Sakamoto 			fw_parent_device(ff->unit)->max_speed);
24260aec494STakashi Sakamoto 	if (err < 0)
24360aec494STakashi Sakamoto 		fw_iso_resources_free(&ff->tx_resources);
24460aec494STakashi Sakamoto 
24560aec494STakashi Sakamoto 	return err;
24660aec494STakashi Sakamoto }
24760aec494STakashi Sakamoto 
latter_begin_session(struct snd_ff * ff,unsigned int rate)24860aec494STakashi Sakamoto static int latter_begin_session(struct snd_ff *ff, unsigned int rate)
24960aec494STakashi Sakamoto {
250b88f4d7cSTakashi Sakamoto 	unsigned int generation = ff->rx_resources.generation;
25160aec494STakashi Sakamoto 	unsigned int flag;
25260aec494STakashi Sakamoto 	u32 data;
25360aec494STakashi Sakamoto 	__le32 reg;
25460aec494STakashi Sakamoto 	int err;
25560aec494STakashi Sakamoto 
256062bb452STakashi Sakamoto 	if (ff->unit_version == SND_FF_UNIT_VERSION_UCX) {
257062bb452STakashi Sakamoto 		// For Fireface UCX. Always use the maximum number of data
258062bb452STakashi Sakamoto 		// channels in data block of packet.
25960aec494STakashi Sakamoto 		if (rate >= 32000 && rate <= 48000)
26060aec494STakashi Sakamoto 			flag = 0x92;
26160aec494STakashi Sakamoto 		else if (rate >= 64000 && rate <= 96000)
26260aec494STakashi Sakamoto 			flag = 0x8e;
26360aec494STakashi Sakamoto 		else if (rate >= 128000 && rate <= 192000)
26460aec494STakashi Sakamoto 			flag = 0x8c;
26560aec494STakashi Sakamoto 		else
26660aec494STakashi Sakamoto 			return -EINVAL;
267062bb452STakashi Sakamoto 	} else {
2681f65e668STakashi Sakamoto 		// For Fireface UFX and 802. Due to bandwidth limitation on
269062bb452STakashi Sakamoto 		// IEEE 1394a (400 Mbps), Analog 1-12 and AES are available
270062bb452STakashi Sakamoto 		// without any ADAT at quadruple speed.
271062bb452STakashi Sakamoto 		if (rate >= 32000 && rate <= 48000)
272062bb452STakashi Sakamoto 			flag = 0x9e;
273062bb452STakashi Sakamoto 		else if (rate >= 64000 && rate <= 96000)
274062bb452STakashi Sakamoto 			flag = 0x96;
275062bb452STakashi Sakamoto 		else if (rate >= 128000 && rate <= 192000)
276062bb452STakashi Sakamoto 			flag = 0x8e;
277062bb452STakashi Sakamoto 		else
278062bb452STakashi Sakamoto 			return -EINVAL;
279062bb452STakashi Sakamoto 	}
28060aec494STakashi Sakamoto 
281b88f4d7cSTakashi Sakamoto 	if (generation != fw_parent_device(ff->unit)->card->generation) {
282b88f4d7cSTakashi Sakamoto 		err = fw_iso_resources_update(&ff->tx_resources);
283b88f4d7cSTakashi Sakamoto 		if (err < 0)
284b88f4d7cSTakashi Sakamoto 			return err;
285b88f4d7cSTakashi Sakamoto 
286b88f4d7cSTakashi Sakamoto 		err = fw_iso_resources_update(&ff->rx_resources);
287b88f4d7cSTakashi Sakamoto 		if (err < 0)
288b88f4d7cSTakashi Sakamoto 			return err;
289b88f4d7cSTakashi Sakamoto 	}
290b88f4d7cSTakashi Sakamoto 
291fd1cc9deSTakashi Sakamoto 	data = (ff->tx_resources.channel << 8) | ff->rx_resources.channel;
292fd1cc9deSTakashi Sakamoto 	reg = cpu_to_le32(data);
293fd1cc9deSTakashi Sakamoto 	err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
294fd1cc9deSTakashi Sakamoto 				 LATTER_ISOC_CHANNELS, &reg, sizeof(reg), 0);
295fd1cc9deSTakashi Sakamoto 	if (err < 0)
296fd1cc9deSTakashi Sakamoto 		return err;
297fd1cc9deSTakashi Sakamoto 
29860aec494STakashi Sakamoto 	reg = cpu_to_le32(flag);
299fd1cc9deSTakashi Sakamoto 	return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
300fd1cc9deSTakashi Sakamoto 				  LATTER_ISOC_START, &reg, sizeof(reg), 0);
301fd1cc9deSTakashi Sakamoto }
302fd1cc9deSTakashi Sakamoto 
latter_finish_session(struct snd_ff * ff)303fd1cc9deSTakashi Sakamoto static void latter_finish_session(struct snd_ff *ff)
304fd1cc9deSTakashi Sakamoto {
305fd1cc9deSTakashi Sakamoto 	__le32 reg;
306fd1cc9deSTakashi Sakamoto 
307fd1cc9deSTakashi Sakamoto 	reg = cpu_to_le32(0x00000000);
308fd1cc9deSTakashi Sakamoto 	snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
309fd1cc9deSTakashi Sakamoto 			   LATTER_ISOC_START, &reg, sizeof(reg), 0);
310fd1cc9deSTakashi Sakamoto }
311fd1cc9deSTakashi Sakamoto 
latter_dump_status(struct snd_ff * ff,struct snd_info_buffer * buffer)312fd1cc9deSTakashi Sakamoto static void latter_dump_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
313fd1cc9deSTakashi Sakamoto {
314fd1cc9deSTakashi Sakamoto 	static const struct {
315fd1cc9deSTakashi Sakamoto 		char *const label;
316fd1cc9deSTakashi Sakamoto 		u32 locked_mask;
317fd1cc9deSTakashi Sakamoto 		u32 synced_mask;
318c50bfc8aSTakashi Sakamoto 	} *clk_entry, *clk_entries, ucx_clk_entries[] = {
319fd1cc9deSTakashi Sakamoto 		{ "S/PDIF",	0x00000001, 0x00000010, },
320fd1cc9deSTakashi Sakamoto 		{ "ADAT",	0x00000002, 0x00000020, },
321fd1cc9deSTakashi Sakamoto 		{ "WDClk",	0x00000004, 0x00000040, },
322c50bfc8aSTakashi Sakamoto 	}, ufx_ff802_clk_entries[] = {
323c50bfc8aSTakashi Sakamoto 		{ "WDClk",	0x00000001, 0x00000010, },
324c50bfc8aSTakashi Sakamoto 		{ "AES/EBU",	0x00000002, 0x00000020, },
325c50bfc8aSTakashi Sakamoto 		{ "ADAT-A",	0x00000004, 0x00000040, },
326c50bfc8aSTakashi Sakamoto 		{ "ADAT-B",	0x00000008, 0x00000080, },
327fd1cc9deSTakashi Sakamoto 	};
328fd1cc9deSTakashi Sakamoto 	__le32 reg;
329fd1cc9deSTakashi Sakamoto 	u32 data;
330fd1cc9deSTakashi Sakamoto 	unsigned int rate;
331fd1cc9deSTakashi Sakamoto 	enum snd_ff_clock_src src;
332fd1cc9deSTakashi Sakamoto 	const char *label;
333c50bfc8aSTakashi Sakamoto 	unsigned int clk_entry_count;
334fd1cc9deSTakashi Sakamoto 	int i;
335fd1cc9deSTakashi Sakamoto 	int err;
336fd1cc9deSTakashi Sakamoto 
337fd1cc9deSTakashi Sakamoto 	err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
338fd1cc9deSTakashi Sakamoto 				 LATTER_SYNC_STATUS, &reg, sizeof(reg), 0);
339fd1cc9deSTakashi Sakamoto 	if (err < 0)
340fd1cc9deSTakashi Sakamoto 		return;
341fd1cc9deSTakashi Sakamoto 	data = le32_to_cpu(reg);
342fd1cc9deSTakashi Sakamoto 
343fd1cc9deSTakashi Sakamoto 	snd_iprintf(buffer, "External source detection:\n");
344fd1cc9deSTakashi Sakamoto 
345c50bfc8aSTakashi Sakamoto 	if (ff->unit_version == SND_FF_UNIT_VERSION_UCX) {
346c50bfc8aSTakashi Sakamoto 		clk_entries = ucx_clk_entries;
347c50bfc8aSTakashi Sakamoto 		clk_entry_count = ARRAY_SIZE(ucx_clk_entries);
348c50bfc8aSTakashi Sakamoto 	} else {
349c50bfc8aSTakashi Sakamoto 		clk_entries = ufx_ff802_clk_entries;
350c50bfc8aSTakashi Sakamoto 		clk_entry_count = ARRAY_SIZE(ufx_ff802_clk_entries);
351c50bfc8aSTakashi Sakamoto 	}
352c50bfc8aSTakashi Sakamoto 
353c50bfc8aSTakashi Sakamoto 	for (i = 0; i < clk_entry_count; ++i) {
354fd1cc9deSTakashi Sakamoto 		clk_entry = clk_entries + i;
355fd1cc9deSTakashi Sakamoto 		snd_iprintf(buffer, "%s: ", clk_entry->label);
356fd1cc9deSTakashi Sakamoto 		if (data & clk_entry->locked_mask) {
357fd1cc9deSTakashi Sakamoto 			if (data & clk_entry->synced_mask)
358fd1cc9deSTakashi Sakamoto 				snd_iprintf(buffer, "sync\n");
359fd1cc9deSTakashi Sakamoto 			else
360fd1cc9deSTakashi Sakamoto 				snd_iprintf(buffer, "lock\n");
361fd1cc9deSTakashi Sakamoto 		} else {
362fd1cc9deSTakashi Sakamoto 			snd_iprintf(buffer, "none\n");
363fd1cc9deSTakashi Sakamoto 		}
364fd1cc9deSTakashi Sakamoto 	}
365fd1cc9deSTakashi Sakamoto 
366062bb452STakashi Sakamoto 	err = parse_clock_bits(data, &rate, &src, ff->unit_version);
367fd1cc9deSTakashi Sakamoto 	if (err < 0)
368fd1cc9deSTakashi Sakamoto 		return;
369fd1cc9deSTakashi Sakamoto 	label = snd_ff_proc_get_clk_label(src);
370fd1cc9deSTakashi Sakamoto 	if (!label)
371fd1cc9deSTakashi Sakamoto 		return;
372fd1cc9deSTakashi Sakamoto 
373fd1cc9deSTakashi Sakamoto 	snd_iprintf(buffer, "Referred clock: %s %d\n", label, rate);
374fd1cc9deSTakashi Sakamoto }
375fd1cc9deSTakashi Sakamoto 
37673f5537fSTakashi Sakamoto // NOTE: transactions are transferred within 0x00-0x7f in allocated range of
37773f5537fSTakashi Sakamoto // address. This seems to be for check of discontinuity in receiver side.
378d8002539STakashi Sakamoto //
379d8002539STakashi Sakamoto // Like Fireface 400, drivers can select one of 4 options for lower 4 bytes of
380d8002539STakashi Sakamoto // destination address by bit flags in quadlet register (little endian) at
381d8002539STakashi Sakamoto // 0x'ffff'0000'0014:
382d8002539STakashi Sakamoto //
383d8002539STakashi Sakamoto // bit flags: offset of destination address
384d8002539STakashi Sakamoto // - 0x00002000: 0x'....'....'0000'0000
385d8002539STakashi Sakamoto // - 0x00004000: 0x'....'....'0000'0080
386d8002539STakashi Sakamoto // - 0x00008000: 0x'....'....'0000'0100
387d8002539STakashi Sakamoto // - 0x00010000: 0x'....'....'0000'0180
388d8002539STakashi Sakamoto //
389d8002539STakashi Sakamoto // Drivers can suppress the device to transfer asynchronous transactions by
390d8002539STakashi Sakamoto // clear these bit flags.
391d8002539STakashi Sakamoto //
392d8002539STakashi Sakamoto // Actually, the register is write-only and includes the other settings such as
393d8002539STakashi Sakamoto // input attenuation. This driver allocates for the first option
394d8002539STakashi Sakamoto // (0x'....'....'0000'0000) and expects userspace application to configure the
395d8002539STakashi Sakamoto // register for it.
latter_handle_midi_msg(struct snd_ff * ff,unsigned int offset,const __le32 * buf,size_t length,u32 tstamp)39650c597c7STakashi Sakamoto static void latter_handle_midi_msg(struct snd_ff *ff, unsigned int offset, const __le32 *buf,
397*4bdcb8d2STakashi Sakamoto 				   size_t length, u32 tstamp)
39873f5537fSTakashi Sakamoto {
39973f5537fSTakashi Sakamoto 	u32 data = le32_to_cpu(*buf);
40073f5537fSTakashi Sakamoto 	unsigned int index = (data & 0x000000f0) >> 4;
40173f5537fSTakashi Sakamoto 	u8 byte[3];
40273f5537fSTakashi Sakamoto 	struct snd_rawmidi_substream *substream;
40373f5537fSTakashi Sakamoto 	unsigned int len;
40473f5537fSTakashi Sakamoto 
4052dee43ecSDan Carpenter 	if (index >= ff->spec->midi_in_ports)
40673f5537fSTakashi Sakamoto 		return;
40773f5537fSTakashi Sakamoto 
40873f5537fSTakashi Sakamoto 	switch (data & 0x0000000f) {
40973f5537fSTakashi Sakamoto 	case 0x00000008:
41073f5537fSTakashi Sakamoto 	case 0x00000009:
41173f5537fSTakashi Sakamoto 	case 0x0000000a:
41273f5537fSTakashi Sakamoto 	case 0x0000000b:
41373f5537fSTakashi Sakamoto 	case 0x0000000e:
41473f5537fSTakashi Sakamoto 		len = 3;
41573f5537fSTakashi Sakamoto 		break;
41673f5537fSTakashi Sakamoto 	case 0x0000000c:
41773f5537fSTakashi Sakamoto 	case 0x0000000d:
41873f5537fSTakashi Sakamoto 		len = 2;
41973f5537fSTakashi Sakamoto 		break;
42073f5537fSTakashi Sakamoto 	default:
42173f5537fSTakashi Sakamoto 		len = data & 0x00000003;
42273f5537fSTakashi Sakamoto 		if (len == 0)
42373f5537fSTakashi Sakamoto 			len = 3;
42473f5537fSTakashi Sakamoto 		break;
42573f5537fSTakashi Sakamoto 	}
42673f5537fSTakashi Sakamoto 
42773f5537fSTakashi Sakamoto 	byte[0] = (data & 0x0000ff00) >> 8;
42873f5537fSTakashi Sakamoto 	byte[1] = (data & 0x00ff0000) >> 16;
42973f5537fSTakashi Sakamoto 	byte[2] = (data & 0xff000000) >> 24;
43073f5537fSTakashi Sakamoto 
43173f5537fSTakashi Sakamoto 	substream = READ_ONCE(ff->tx_midi_substreams[index]);
43273f5537fSTakashi Sakamoto 	if (substream)
43373f5537fSTakashi Sakamoto 		snd_rawmidi_receive(substream, byte, len);
43473f5537fSTakashi Sakamoto }
43573f5537fSTakashi Sakamoto 
436f0f9f497STakashi Sakamoto /*
437f0f9f497STakashi Sakamoto  * When return minus value, given argument is not MIDI status.
438f0f9f497STakashi Sakamoto  * When return 0, given argument is a beginning of system exclusive.
439f0f9f497STakashi Sakamoto  * When return the others, given argument is MIDI data.
440f0f9f497STakashi Sakamoto  */
calculate_message_bytes(u8 status)441f0f9f497STakashi Sakamoto static inline int calculate_message_bytes(u8 status)
442f0f9f497STakashi Sakamoto {
443f0f9f497STakashi Sakamoto 	switch (status) {
444f0f9f497STakashi Sakamoto 	case 0xf6:	/* Tune request. */
445f0f9f497STakashi Sakamoto 	case 0xf8:	/* Timing clock. */
446f0f9f497STakashi Sakamoto 	case 0xfa:	/* Start. */
447f0f9f497STakashi Sakamoto 	case 0xfb:	/* Continue. */
448f0f9f497STakashi Sakamoto 	case 0xfc:	/* Stop. */
449f0f9f497STakashi Sakamoto 	case 0xfe:	/* Active sensing. */
450f0f9f497STakashi Sakamoto 	case 0xff:	/* System reset. */
451f0f9f497STakashi Sakamoto 		return 1;
452f0f9f497STakashi Sakamoto 	case 0xf1:	/* MIDI time code quarter frame. */
453f0f9f497STakashi Sakamoto 	case 0xf3:	/* Song select. */
454f0f9f497STakashi Sakamoto 		return 2;
455f0f9f497STakashi Sakamoto 	case 0xf2:	/* Song position pointer. */
456f0f9f497STakashi Sakamoto 		return 3;
457f0f9f497STakashi Sakamoto 	case 0xf0:	/* Exclusive. */
458f0f9f497STakashi Sakamoto 		return 0;
459f0f9f497STakashi Sakamoto 	case 0xf7:	/* End of exclusive. */
460f0f9f497STakashi Sakamoto 		break;
461f0f9f497STakashi Sakamoto 	case 0xf4:	/* Undefined. */
462f0f9f497STakashi Sakamoto 	case 0xf5:	/* Undefined. */
463f0f9f497STakashi Sakamoto 	case 0xf9:	/* Undefined. */
464f0f9f497STakashi Sakamoto 	case 0xfd:	/* Undefined. */
465f0f9f497STakashi Sakamoto 		break;
466f0f9f497STakashi Sakamoto 	default:
467f0f9f497STakashi Sakamoto 		switch (status & 0xf0) {
468f0f9f497STakashi Sakamoto 		case 0x80:	/* Note on. */
469f0f9f497STakashi Sakamoto 		case 0x90:	/* Note off. */
470f0f9f497STakashi Sakamoto 		case 0xa0:	/* Polyphonic key pressure. */
471f0f9f497STakashi Sakamoto 		case 0xb0:	/* Control change and Mode change. */
472f0f9f497STakashi Sakamoto 		case 0xe0:	/* Pitch bend change. */
473f0f9f497STakashi Sakamoto 			return 3;
474f0f9f497STakashi Sakamoto 		case 0xc0:	/* Program change. */
475f0f9f497STakashi Sakamoto 		case 0xd0:	/* Channel pressure. */
476f0f9f497STakashi Sakamoto 			return 2;
477f0f9f497STakashi Sakamoto 		default:
478f0f9f497STakashi Sakamoto 		break;
479f0f9f497STakashi Sakamoto 		}
480f0f9f497STakashi Sakamoto 	break;
481f0f9f497STakashi Sakamoto 	}
482f0f9f497STakashi Sakamoto 
483f0f9f497STakashi Sakamoto 	return -EINVAL;
484f0f9f497STakashi Sakamoto }
485f0f9f497STakashi Sakamoto 
latter_fill_midi_msg(struct snd_ff * ff,struct snd_rawmidi_substream * substream,unsigned int port)486f0f9f497STakashi Sakamoto static int latter_fill_midi_msg(struct snd_ff *ff,
487f0f9f497STakashi Sakamoto 				struct snd_rawmidi_substream *substream,
488f0f9f497STakashi Sakamoto 				unsigned int port)
489f0f9f497STakashi Sakamoto {
490f0f9f497STakashi Sakamoto 	u32 data = {0};
491f0f9f497STakashi Sakamoto 	u8 *buf = (u8 *)&data;
492f0f9f497STakashi Sakamoto 	int consumed;
493f0f9f497STakashi Sakamoto 
494f0f9f497STakashi Sakamoto 	buf[0] = port << 4;
495f0f9f497STakashi Sakamoto 	consumed = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
496f0f9f497STakashi Sakamoto 	if (consumed <= 0)
497f0f9f497STakashi Sakamoto 		return consumed;
498f0f9f497STakashi Sakamoto 
499f0f9f497STakashi Sakamoto 	if (!ff->on_sysex[port]) {
500f0f9f497STakashi Sakamoto 		if (buf[1] != 0xf0) {
501f0f9f497STakashi Sakamoto 			if (consumed < calculate_message_bytes(buf[1]))
502f0f9f497STakashi Sakamoto 				return 0;
503f0f9f497STakashi Sakamoto 		} else {
504f0f9f497STakashi Sakamoto 			// The beginning of exclusives.
505f0f9f497STakashi Sakamoto 			ff->on_sysex[port] = true;
506f0f9f497STakashi Sakamoto 		}
507f0f9f497STakashi Sakamoto 
508f0f9f497STakashi Sakamoto 		buf[0] |= consumed;
509f0f9f497STakashi Sakamoto 	} else {
510f0f9f497STakashi Sakamoto 		if (buf[1] != 0xf7) {
511f0f9f497STakashi Sakamoto 			if (buf[2] == 0xf7 || buf[3] == 0xf7) {
512f0f9f497STakashi Sakamoto 				// Transfer end code at next time.
513f0f9f497STakashi Sakamoto 				consumed -= 1;
514f0f9f497STakashi Sakamoto 			}
515f0f9f497STakashi Sakamoto 
516f0f9f497STakashi Sakamoto 			buf[0] |= consumed;
517f0f9f497STakashi Sakamoto 		} else {
518f0f9f497STakashi Sakamoto 			// The end of exclusives.
519f0f9f497STakashi Sakamoto 			ff->on_sysex[port] = false;
520f0f9f497STakashi Sakamoto 			consumed = 1;
521f0f9f497STakashi Sakamoto 			buf[0] |= 0x0f;
522f0f9f497STakashi Sakamoto 		}
523f0f9f497STakashi Sakamoto 	}
524f0f9f497STakashi Sakamoto 
525f0f9f497STakashi Sakamoto 	ff->msg_buf[port][0] = cpu_to_le32(data);
526f0f9f497STakashi Sakamoto 	ff->rx_bytes[port] = consumed;
527f0f9f497STakashi Sakamoto 
528f0f9f497STakashi Sakamoto 	return 1;
529f0f9f497STakashi Sakamoto }
530f0f9f497STakashi Sakamoto 
531fd1cc9deSTakashi Sakamoto const struct snd_ff_protocol snd_ff_protocol_latter = {
53250c597c7STakashi Sakamoto 	.handle_msg		= latter_handle_midi_msg,
533f0f9f497STakashi Sakamoto 	.fill_midi_msg		= latter_fill_midi_msg,
534fd1cc9deSTakashi Sakamoto 	.get_clock		= latter_get_clock,
535fd1cc9deSTakashi Sakamoto 	.switch_fetching_mode	= latter_switch_fetching_mode,
53660aec494STakashi Sakamoto 	.allocate_resources	= latter_allocate_resources,
537fd1cc9deSTakashi Sakamoto 	.begin_session		= latter_begin_session,
538fd1cc9deSTakashi Sakamoto 	.finish_session		= latter_finish_session,
539fd1cc9deSTakashi Sakamoto 	.dump_status		= latter_dump_status,
540fd1cc9deSTakashi Sakamoto };
541