1 // SPDX-License-Identifier: GPL-2.0
2 // ff-protocol-latter - a part of driver for RME Fireface series
4 // Copyright (c) 2019 Takashi Sakamoto
6 // Licensed under the terms of the GNU General Public License, version 2.
8 #include <linux/delay.h>
12 #define LATTER_STF 0xffff00000004ULL
13 #define LATTER_ISOC_CHANNELS 0xffff00000008ULL
14 #define LATTER_ISOC_START 0xffff0000000cULL
15 #define LATTER_FETCH_MODE 0xffff00000010ULL
16 #define LATTER_SYNC_STATUS 0x0000801c0000ULL
18 static int parse_clock_bits(u32 data, unsigned int *rate,
19 enum snd_ff_clock_src *src)
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, },
36 enum snd_ff_clock_src src;
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, },
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;
53 if (i == ARRAY_SIZE(rate_entries))
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;
63 if (i == ARRAY_SIZE(clk_entries))
69 static int latter_get_clock(struct snd_ff *ff, unsigned int *rate,
70 enum snd_ff_clock_src *src)
76 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
77 LATTER_SYNC_STATUS, ®, sizeof(reg), 0);
80 data = le32_to_cpu(reg);
82 return parse_clock_bits(data, rate, src);
85 static int latter_switch_fetching_mode(struct snd_ff *ff, bool enable)
94 reg = cpu_to_le32(data);
96 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
97 LATTER_FETCH_MODE, ®, sizeof(reg), 0);
100 static int latter_allocate_resources(struct snd_ff *ff, unsigned int rate)
102 enum snd_ff_stream_mode mode;
109 // Set the number of data blocks transferred in a second.
110 if (rate % 32000 == 0)
112 else if (rate % 44100 == 0)
114 else if (rate % 48000 == 0)
119 if (rate >= 64000 && rate < 128000)
121 else if (rate >= 128000 && rate < 192000)
124 reg = cpu_to_le32(code);
125 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
126 LATTER_STF, ®, sizeof(reg), 0);
130 // Confirm to shift transmission clock.
132 while (count++ < 10) {
133 unsigned int curr_rate;
134 enum snd_ff_clock_src src;
136 err = latter_get_clock(ff, &curr_rate, &src);
140 if (curr_rate == rate)
146 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); ++i) {
147 if (rate == amdtp_rate_table[i])
150 if (i == ARRAY_SIZE(amdtp_rate_table))
153 err = snd_ff_stream_get_multiplier_mode(i, &mode);
157 // Keep resources for in-stream.
158 ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
159 err = fw_iso_resources_allocate(&ff->tx_resources,
160 amdtp_stream_get_max_payload(&ff->tx_stream),
161 fw_parent_device(ff->unit)->max_speed);
165 // Keep resources for out-stream.
166 ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
167 err = fw_iso_resources_allocate(&ff->rx_resources,
168 amdtp_stream_get_max_payload(&ff->rx_stream),
169 fw_parent_device(ff->unit)->max_speed);
171 fw_iso_resources_free(&ff->tx_resources);
176 static int latter_begin_session(struct snd_ff *ff, unsigned int rate)
178 unsigned int generation = ff->rx_resources.generation;
184 if (rate >= 32000 && rate <= 48000)
186 else if (rate >= 64000 && rate <= 96000)
188 else if (rate >= 128000 && rate <= 192000)
193 if (generation != fw_parent_device(ff->unit)->card->generation) {
194 err = fw_iso_resources_update(&ff->tx_resources);
198 err = fw_iso_resources_update(&ff->rx_resources);
203 data = (ff->tx_resources.channel << 8) | ff->rx_resources.channel;
204 reg = cpu_to_le32(data);
205 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
206 LATTER_ISOC_CHANNELS, ®, sizeof(reg), 0);
210 // Always use the maximum number of data channels in data block of
212 reg = cpu_to_le32(flag);
213 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
214 LATTER_ISOC_START, ®, sizeof(reg), 0);
217 static void latter_finish_session(struct snd_ff *ff)
221 reg = cpu_to_le32(0x00000000);
222 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
223 LATTER_ISOC_START, ®, sizeof(reg), 0);
226 static void latter_dump_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
228 static const struct {
232 } *clk_entry, clk_entries[] = {
233 { "S/PDIF", 0x00000001, 0x00000010, },
234 { "ADAT", 0x00000002, 0x00000020, },
235 { "WDClk", 0x00000004, 0x00000040, },
240 enum snd_ff_clock_src src;
245 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
246 LATTER_SYNC_STATUS, ®, sizeof(reg), 0);
249 data = le32_to_cpu(reg);
251 snd_iprintf(buffer, "External source detection:\n");
253 for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
254 clk_entry = clk_entries + i;
255 snd_iprintf(buffer, "%s: ", clk_entry->label);
256 if (data & clk_entry->locked_mask) {
257 if (data & clk_entry->synced_mask)
258 snd_iprintf(buffer, "sync\n");
260 snd_iprintf(buffer, "lock\n");
262 snd_iprintf(buffer, "none\n");
266 err = parse_clock_bits(data, &rate, &src);
269 label = snd_ff_proc_get_clk_label(src);
273 snd_iprintf(buffer, "Referred clock: %s %d\n", label, rate);
276 // NOTE: transactions are transferred within 0x00-0x7f in allocated range of
277 // address. This seems to be for check of discontinuity in receiver side.
279 // Like Fireface 400, drivers can select one of 4 options for lower 4 bytes of
280 // destination address by bit flags in quadlet register (little endian) at
281 // 0x'ffff'0000'0014:
283 // bit flags: offset of destination address
284 // - 0x00002000: 0x'....'....'0000'0000
285 // - 0x00004000: 0x'....'....'0000'0080
286 // - 0x00008000: 0x'....'....'0000'0100
287 // - 0x00010000: 0x'....'....'0000'0180
289 // Drivers can suppress the device to transfer asynchronous transactions by
290 // clear these bit flags.
292 // Actually, the register is write-only and includes the other settings such as
293 // input attenuation. This driver allocates for the first option
294 // (0x'....'....'0000'0000) and expects userspace application to configure the
296 static void latter_handle_midi_msg(struct snd_ff *ff, unsigned int offset,
297 __le32 *buf, size_t length)
299 u32 data = le32_to_cpu(*buf);
300 unsigned int index = (data & 0x000000f0) >> 4;
302 struct snd_rawmidi_substream *substream;
305 if (index >= ff->spec->midi_in_ports)
308 switch (data & 0x0000000f) {
321 len = data & 0x00000003;
327 byte[0] = (data & 0x0000ff00) >> 8;
328 byte[1] = (data & 0x00ff0000) >> 16;
329 byte[2] = (data & 0xff000000) >> 24;
331 substream = READ_ONCE(ff->tx_midi_substreams[index]);
333 snd_rawmidi_receive(substream, byte, len);
337 * When return minus value, given argument is not MIDI status.
338 * When return 0, given argument is a beginning of system exclusive.
339 * When return the others, given argument is MIDI data.
341 static inline int calculate_message_bytes(u8 status)
344 case 0xf6: /* Tune request. */
345 case 0xf8: /* Timing clock. */
346 case 0xfa: /* Start. */
347 case 0xfb: /* Continue. */
348 case 0xfc: /* Stop. */
349 case 0xfe: /* Active sensing. */
350 case 0xff: /* System reset. */
352 case 0xf1: /* MIDI time code quarter frame. */
353 case 0xf3: /* Song select. */
355 case 0xf2: /* Song position pointer. */
357 case 0xf0: /* Exclusive. */
359 case 0xf7: /* End of exclusive. */
361 case 0xf4: /* Undefined. */
362 case 0xf5: /* Undefined. */
363 case 0xf9: /* Undefined. */
364 case 0xfd: /* Undefined. */
367 switch (status & 0xf0) {
368 case 0x80: /* Note on. */
369 case 0x90: /* Note off. */
370 case 0xa0: /* Polyphonic key pressure. */
371 case 0xb0: /* Control change and Mode change. */
372 case 0xe0: /* Pitch bend change. */
374 case 0xc0: /* Program change. */
375 case 0xd0: /* Channel pressure. */
386 static int latter_fill_midi_msg(struct snd_ff *ff,
387 struct snd_rawmidi_substream *substream,
391 u8 *buf = (u8 *)&data;
395 consumed = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
399 if (!ff->on_sysex[port]) {
400 if (buf[1] != 0xf0) {
401 if (consumed < calculate_message_bytes(buf[1]))
404 // The beginning of exclusives.
405 ff->on_sysex[port] = true;
410 if (buf[1] != 0xf7) {
411 if (buf[2] == 0xf7 || buf[3] == 0xf7) {
412 // Transfer end code at next time.
418 // The end of exclusives.
419 ff->on_sysex[port] = false;
425 ff->msg_buf[port][0] = cpu_to_le32(data);
426 ff->rx_bytes[port] = consumed;
431 const struct snd_ff_protocol snd_ff_protocol_latter = {
432 .handle_midi_msg = latter_handle_midi_msg,
433 .fill_midi_msg = latter_fill_midi_msg,
434 .get_clock = latter_get_clock,
435 .switch_fetching_mode = latter_switch_fetching_mode,
436 .allocate_resources = latter_allocate_resources,
437 .begin_session = latter_begin_session,
438 .finish_session = latter_finish_session,
439 .dump_status = latter_dump_status,