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 // The content of sync status register differs between models.
21 // 0xf0000000: (unidentified)
22 // 0x0f000000: effective rate of sampling clock
23 // 0x00f00000: detected rate of word clock on BNC interface
24 // 0x000f0000: detected rate of ADAT or S/PDIF on optical interface
25 // 0x0000f000: detected rate of S/PDIF on coaxial interface
26 // 0x00000e00: effective source of sampling clock
27 // 0x00000e00: Internal
28 // 0x00000800: (unidentified)
29 // 0x00000600: Word clock on BNC interface
30 // 0x00000400: ADAT on optical interface
31 // 0x00000200: S/PDIF on coaxial or optical interface
32 // 0x00000100: Optical interface is used for ADAT signal
33 // 0x00000080: (unidentified)
34 // 0x00000040: Synchronized to word clock on BNC interface
35 // 0x00000020: Synchronized to ADAT or S/PDIF on optical interface
36 // 0x00000010: Synchronized to S/PDIF on coaxial interface
37 // 0x00000008: (unidentified)
38 // 0x00000004: Lock word clock on BNC interface
39 // 0x00000002: Lock ADAT or S/PDIF on optical interface
40 // 0x00000001: Lock S/PDIF on coaxial interface
42 // Fireface 802 (and perhaps UFX):
43 // 0xf0000000: effective rate of sampling clock
44 // 0x0f000000: detected rate of ADAT-B on 2nd optical interface
45 // 0x00f00000: detected rate of ADAT-A on 1st optical interface
46 // 0x000f0000: detected rate of AES/EBU on XLR or coaxial interface
47 // 0x0000f000: detected rate of word clock on BNC interface
48 // 0x00000e00: effective source of sampling clock
49 // 0x00000e00: internal
52 // 0x00000400: AES/EBU
53 // 0x00000200: Word clock
54 // 0x00000080: Synchronized to ADAT-B on 2nd optical interface
55 // 0x00000040: Synchronized to ADAT-A on 1st optical interface
56 // 0x00000020: Synchronized to AES/EBU on XLR or 2nd optical interface
57 // 0x00000010: Synchronized to word clock on BNC interface
58 // 0x00000008: Lock ADAT-B on 2nd optical interface
59 // 0x00000004: Lock ADAT-A on 1st optical interface
60 // 0x00000002: Lock AES/EBU on XLR or 2nd optical interface
61 // 0x00000001: Lock word clock on BNC interface
63 // The pattern for rate bits:
73 static int parse_clock_bits(u32 data, unsigned int *rate,
74 enum snd_ff_clock_src *src,
75 enum snd_ff_unit_version unit_version)
80 } *rate_entry, rate_entries[] = {
92 enum snd_ff_clock_src src;
94 } *clk_entry, *clk_entries, ucx_clk_entries[] = {
95 { SND_FF_CLOCK_SRC_SPDIF, 0x00000200, },
96 { SND_FF_CLOCK_SRC_ADAT1, 0x00000400, },
97 { SND_FF_CLOCK_SRC_WORD, 0x00000600, },
98 { SND_FF_CLOCK_SRC_INTERNAL, 0x00000e00, },
99 }, ufx_ff802_clk_entries[] = {
100 { SND_FF_CLOCK_SRC_WORD, 0x00000200, },
101 { SND_FF_CLOCK_SRC_SPDIF, 0x00000400, },
102 { SND_FF_CLOCK_SRC_ADAT1, 0x00000600, },
103 { SND_FF_CLOCK_SRC_ADAT2, 0x00000800, },
104 { SND_FF_CLOCK_SRC_INTERNAL, 0x00000e00, },
107 unsigned int clk_entry_count;
110 if (unit_version == SND_FF_UNIT_VERSION_UCX) {
111 rate_bits = (data & 0x0f000000) >> 24;
112 clk_entries = ucx_clk_entries;
113 clk_entry_count = ARRAY_SIZE(ucx_clk_entries);
115 rate_bits = (data & 0xf0000000) >> 28;
116 clk_entries = ufx_ff802_clk_entries;
117 clk_entry_count = ARRAY_SIZE(ufx_ff802_clk_entries);
120 for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
121 rate_entry = rate_entries + i;
122 if (rate_bits == rate_entry->flag) {
123 *rate = rate_entry->rate;
127 if (i == ARRAY_SIZE(rate_entries))
130 for (i = 0; i < clk_entry_count; ++i) {
131 clk_entry = clk_entries + i;
132 if ((data & 0x000e00) == clk_entry->flag) {
133 *src = clk_entry->src;
137 if (i == clk_entry_count)
143 static int latter_get_clock(struct snd_ff *ff, unsigned int *rate,
144 enum snd_ff_clock_src *src)
150 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
151 LATTER_SYNC_STATUS, ®, sizeof(reg), 0);
154 data = le32_to_cpu(reg);
156 return parse_clock_bits(data, rate, src, ff->unit_version);
159 static int latter_switch_fetching_mode(struct snd_ff *ff, bool enable)
168 reg = cpu_to_le32(data);
170 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
171 LATTER_FETCH_MODE, ®, sizeof(reg), 0);
174 static int latter_allocate_resources(struct snd_ff *ff, unsigned int rate)
176 enum snd_ff_stream_mode mode;
183 // Set the number of data blocks transferred in a second.
184 if (rate % 48000 == 0)
186 else if (rate % 44100 == 0)
188 else if (rate % 32000 == 0)
193 if (rate >= 64000 && rate < 128000)
195 else if (rate >= 128000)
198 reg = cpu_to_le32(code);
199 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
200 LATTER_STF, ®, sizeof(reg), 0);
204 // Confirm to shift transmission clock.
206 while (count++ < 10) {
207 unsigned int curr_rate;
208 enum snd_ff_clock_src src;
210 err = latter_get_clock(ff, &curr_rate, &src);
214 if (curr_rate == rate)
220 for (i = 0; i < ARRAY_SIZE(amdtp_rate_table); ++i) {
221 if (rate == amdtp_rate_table[i])
224 if (i == ARRAY_SIZE(amdtp_rate_table))
227 err = snd_ff_stream_get_multiplier_mode(i, &mode);
231 // Keep resources for in-stream.
232 ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
233 err = fw_iso_resources_allocate(&ff->tx_resources,
234 amdtp_stream_get_max_payload(&ff->tx_stream),
235 fw_parent_device(ff->unit)->max_speed);
239 // Keep resources for out-stream.
240 ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
241 err = fw_iso_resources_allocate(&ff->rx_resources,
242 amdtp_stream_get_max_payload(&ff->rx_stream),
243 fw_parent_device(ff->unit)->max_speed);
245 fw_iso_resources_free(&ff->tx_resources);
250 static int latter_begin_session(struct snd_ff *ff, unsigned int rate)
252 unsigned int generation = ff->rx_resources.generation;
258 if (ff->unit_version == SND_FF_UNIT_VERSION_UCX) {
259 // For Fireface UCX. Always use the maximum number of data
260 // channels in data block of packet.
261 if (rate >= 32000 && rate <= 48000)
263 else if (rate >= 64000 && rate <= 96000)
265 else if (rate >= 128000 && rate <= 192000)
270 // For Fireface UFX and 802. Due to bandwidth limitation on
271 // IEEE 1394a (400 Mbps), Analog 1-12 and AES are available
272 // without any ADAT at quadruple speed.
273 if (rate >= 32000 && rate <= 48000)
275 else if (rate >= 64000 && rate <= 96000)
277 else if (rate >= 128000 && rate <= 192000)
283 if (generation != fw_parent_device(ff->unit)->card->generation) {
284 err = fw_iso_resources_update(&ff->tx_resources);
288 err = fw_iso_resources_update(&ff->rx_resources);
293 data = (ff->tx_resources.channel << 8) | ff->rx_resources.channel;
294 reg = cpu_to_le32(data);
295 err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
296 LATTER_ISOC_CHANNELS, ®, sizeof(reg), 0);
300 reg = cpu_to_le32(flag);
301 return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
302 LATTER_ISOC_START, ®, sizeof(reg), 0);
305 static void latter_finish_session(struct snd_ff *ff)
309 reg = cpu_to_le32(0x00000000);
310 snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
311 LATTER_ISOC_START, ®, sizeof(reg), 0);
314 static void latter_dump_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
316 static const struct {
320 } *clk_entry, *clk_entries, ucx_clk_entries[] = {
321 { "S/PDIF", 0x00000001, 0x00000010, },
322 { "ADAT", 0x00000002, 0x00000020, },
323 { "WDClk", 0x00000004, 0x00000040, },
324 }, ufx_ff802_clk_entries[] = {
325 { "WDClk", 0x00000001, 0x00000010, },
326 { "AES/EBU", 0x00000002, 0x00000020, },
327 { "ADAT-A", 0x00000004, 0x00000040, },
328 { "ADAT-B", 0x00000008, 0x00000080, },
333 enum snd_ff_clock_src src;
335 unsigned int clk_entry_count;
339 err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
340 LATTER_SYNC_STATUS, ®, sizeof(reg), 0);
343 data = le32_to_cpu(reg);
345 snd_iprintf(buffer, "External source detection:\n");
347 if (ff->unit_version == SND_FF_UNIT_VERSION_UCX) {
348 clk_entries = ucx_clk_entries;
349 clk_entry_count = ARRAY_SIZE(ucx_clk_entries);
351 clk_entries = ufx_ff802_clk_entries;
352 clk_entry_count = ARRAY_SIZE(ufx_ff802_clk_entries);
355 for (i = 0; i < clk_entry_count; ++i) {
356 clk_entry = clk_entries + i;
357 snd_iprintf(buffer, "%s: ", clk_entry->label);
358 if (data & clk_entry->locked_mask) {
359 if (data & clk_entry->synced_mask)
360 snd_iprintf(buffer, "sync\n");
362 snd_iprintf(buffer, "lock\n");
364 snd_iprintf(buffer, "none\n");
368 err = parse_clock_bits(data, &rate, &src, ff->unit_version);
371 label = snd_ff_proc_get_clk_label(src);
375 snd_iprintf(buffer, "Referred clock: %s %d\n", label, rate);
378 // NOTE: transactions are transferred within 0x00-0x7f in allocated range of
379 // address. This seems to be for check of discontinuity in receiver side.
381 // Like Fireface 400, drivers can select one of 4 options for lower 4 bytes of
382 // destination address by bit flags in quadlet register (little endian) at
383 // 0x'ffff'0000'0014:
385 // bit flags: offset of destination address
386 // - 0x00002000: 0x'....'....'0000'0000
387 // - 0x00004000: 0x'....'....'0000'0080
388 // - 0x00008000: 0x'....'....'0000'0100
389 // - 0x00010000: 0x'....'....'0000'0180
391 // Drivers can suppress the device to transfer asynchronous transactions by
392 // clear these bit flags.
394 // Actually, the register is write-only and includes the other settings such as
395 // input attenuation. This driver allocates for the first option
396 // (0x'....'....'0000'0000) and expects userspace application to configure the
398 static void latter_handle_midi_msg(struct snd_ff *ff, unsigned int offset,
399 __le32 *buf, size_t length)
401 u32 data = le32_to_cpu(*buf);
402 unsigned int index = (data & 0x000000f0) >> 4;
404 struct snd_rawmidi_substream *substream;
407 if (index >= ff->spec->midi_in_ports)
410 switch (data & 0x0000000f) {
423 len = data & 0x00000003;
429 byte[0] = (data & 0x0000ff00) >> 8;
430 byte[1] = (data & 0x00ff0000) >> 16;
431 byte[2] = (data & 0xff000000) >> 24;
433 substream = READ_ONCE(ff->tx_midi_substreams[index]);
435 snd_rawmidi_receive(substream, byte, len);
439 * When return minus value, given argument is not MIDI status.
440 * When return 0, given argument is a beginning of system exclusive.
441 * When return the others, given argument is MIDI data.
443 static inline int calculate_message_bytes(u8 status)
446 case 0xf6: /* Tune request. */
447 case 0xf8: /* Timing clock. */
448 case 0xfa: /* Start. */
449 case 0xfb: /* Continue. */
450 case 0xfc: /* Stop. */
451 case 0xfe: /* Active sensing. */
452 case 0xff: /* System reset. */
454 case 0xf1: /* MIDI time code quarter frame. */
455 case 0xf3: /* Song select. */
457 case 0xf2: /* Song position pointer. */
459 case 0xf0: /* Exclusive. */
461 case 0xf7: /* End of exclusive. */
463 case 0xf4: /* Undefined. */
464 case 0xf5: /* Undefined. */
465 case 0xf9: /* Undefined. */
466 case 0xfd: /* Undefined. */
469 switch (status & 0xf0) {
470 case 0x80: /* Note on. */
471 case 0x90: /* Note off. */
472 case 0xa0: /* Polyphonic key pressure. */
473 case 0xb0: /* Control change and Mode change. */
474 case 0xe0: /* Pitch bend change. */
476 case 0xc0: /* Program change. */
477 case 0xd0: /* Channel pressure. */
488 static int latter_fill_midi_msg(struct snd_ff *ff,
489 struct snd_rawmidi_substream *substream,
493 u8 *buf = (u8 *)&data;
497 consumed = snd_rawmidi_transmit_peek(substream, buf + 1, 3);
501 if (!ff->on_sysex[port]) {
502 if (buf[1] != 0xf0) {
503 if (consumed < calculate_message_bytes(buf[1]))
506 // The beginning of exclusives.
507 ff->on_sysex[port] = true;
512 if (buf[1] != 0xf7) {
513 if (buf[2] == 0xf7 || buf[3] == 0xf7) {
514 // Transfer end code at next time.
520 // The end of exclusives.
521 ff->on_sysex[port] = false;
527 ff->msg_buf[port][0] = cpu_to_le32(data);
528 ff->rx_bytes[port] = consumed;
533 const struct snd_ff_protocol snd_ff_protocol_latter = {
534 .handle_midi_msg = latter_handle_midi_msg,
535 .fill_midi_msg = latter_fill_midi_msg,
536 .get_clock = latter_get_clock,
537 .switch_fetching_mode = latter_switch_fetching_mode,
538 .allocate_resources = latter_allocate_resources,
539 .begin_session = latter_begin_session,
540 .finish_session = latter_finish_session,
541 .dump_status = latter_dump_status,