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
,
20 enum snd_ff_unit_version unit_version
)
25 } *rate_entry
, rate_entries
[] = {
26 { 32000, 0x00000000, },
27 { 44100, 0x01000000, },
28 { 48000, 0x02000000, },
29 { 64000, 0x04000000, },
30 { 88200, 0x05000000, },
31 { 96000, 0x06000000, },
32 { 128000, 0x08000000, },
33 { 176400, 0x09000000, },
34 { 192000, 0x0a000000, },
37 enum snd_ff_clock_src src
;
39 } *clk_entry
, clk_entries
[] = {
40 { SND_FF_CLOCK_SRC_SPDIF
, 0x00000200, },
41 { SND_FF_CLOCK_SRC_ADAT1
, 0x00000400, },
42 { SND_FF_CLOCK_SRC_WORD
, 0x00000600, },
43 { SND_FF_CLOCK_SRC_INTERNAL
, 0x00000e00, },
47 if (unit_version
!= SND_FF_UNIT_VERSION_UCX
) {
48 // e.g. 0x00fe0f20 but expected 0x00eff002.
49 data
= ((data
& 0xf0f0f0f0) >> 4) | ((data
& 0x0f0f0f0f) << 4);
52 for (i
= 0; i
< ARRAY_SIZE(rate_entries
); ++i
) {
53 rate_entry
= rate_entries
+ i
;
54 if ((data
& 0x0f000000) == rate_entry
->flag
) {
55 *rate
= rate_entry
->rate
;
59 if (i
== ARRAY_SIZE(rate_entries
))
62 for (i
= 0; i
< ARRAY_SIZE(clk_entries
); ++i
) {
63 clk_entry
= clk_entries
+ i
;
64 if ((data
& 0x000e00) == clk_entry
->flag
) {
65 *src
= clk_entry
->src
;
69 if (i
== ARRAY_SIZE(clk_entries
))
75 static int latter_get_clock(struct snd_ff
*ff
, unsigned int *rate
,
76 enum snd_ff_clock_src
*src
)
82 err
= snd_fw_transaction(ff
->unit
, TCODE_READ_QUADLET_REQUEST
,
83 LATTER_SYNC_STATUS
, ®
, sizeof(reg
), 0);
86 data
= le32_to_cpu(reg
);
88 return parse_clock_bits(data
, rate
, src
, ff
->unit_version
);
91 static int latter_switch_fetching_mode(struct snd_ff
*ff
, bool enable
)
100 reg
= cpu_to_le32(data
);
102 return snd_fw_transaction(ff
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
103 LATTER_FETCH_MODE
, ®
, sizeof(reg
), 0);
106 static int latter_allocate_resources(struct snd_ff
*ff
, unsigned int rate
)
108 enum snd_ff_stream_mode mode
;
115 // Set the number of data blocks transferred in a second.
116 if (rate
% 48000 == 0)
118 else if (rate
% 44100 == 0)
120 else if (rate
% 32000 == 0)
125 if (rate
>= 64000 && rate
< 128000)
127 else if (rate
>= 128000)
130 reg
= cpu_to_le32(code
);
131 err
= snd_fw_transaction(ff
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
132 LATTER_STF
, ®
, sizeof(reg
), 0);
136 // Confirm to shift transmission clock.
138 while (count
++ < 10) {
139 unsigned int curr_rate
;
140 enum snd_ff_clock_src src
;
142 err
= latter_get_clock(ff
, &curr_rate
, &src
);
146 if (curr_rate
== rate
)
152 for (i
= 0; i
< ARRAY_SIZE(amdtp_rate_table
); ++i
) {
153 if (rate
== amdtp_rate_table
[i
])
156 if (i
== ARRAY_SIZE(amdtp_rate_table
))
159 err
= snd_ff_stream_get_multiplier_mode(i
, &mode
);
163 // Keep resources for in-stream.
164 ff
->tx_resources
.channels_mask
= 0x00000000000000ffuLL
;
165 err
= fw_iso_resources_allocate(&ff
->tx_resources
,
166 amdtp_stream_get_max_payload(&ff
->tx_stream
),
167 fw_parent_device(ff
->unit
)->max_speed
);
171 // Keep resources for out-stream.
172 ff
->rx_resources
.channels_mask
= 0x00000000000000ffuLL
;
173 err
= fw_iso_resources_allocate(&ff
->rx_resources
,
174 amdtp_stream_get_max_payload(&ff
->rx_stream
),
175 fw_parent_device(ff
->unit
)->max_speed
);
177 fw_iso_resources_free(&ff
->tx_resources
);
182 static int latter_begin_session(struct snd_ff
*ff
, unsigned int rate
)
184 unsigned int generation
= ff
->rx_resources
.generation
;
190 if (ff
->unit_version
== SND_FF_UNIT_VERSION_UCX
) {
191 // For Fireface UCX. Always use the maximum number of data
192 // channels in data block of packet.
193 if (rate
>= 32000 && rate
<= 48000)
195 else if (rate
>= 64000 && rate
<= 96000)
197 else if (rate
>= 128000 && rate
<= 192000)
202 // For Fireface UFX and 802. Due to bandwidth limitation on
203 // IEEE 1394a (400 Mbps), Analog 1-12 and AES are available
204 // without any ADAT at quadruple speed.
205 if (rate
>= 32000 && rate
<= 48000)
207 else if (rate
>= 64000 && rate
<= 96000)
209 else if (rate
>= 128000 && rate
<= 192000)
215 if (generation
!= fw_parent_device(ff
->unit
)->card
->generation
) {
216 err
= fw_iso_resources_update(&ff
->tx_resources
);
220 err
= fw_iso_resources_update(&ff
->rx_resources
);
225 data
= (ff
->tx_resources
.channel
<< 8) | ff
->rx_resources
.channel
;
226 reg
= cpu_to_le32(data
);
227 err
= snd_fw_transaction(ff
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
228 LATTER_ISOC_CHANNELS
, ®
, sizeof(reg
), 0);
232 reg
= cpu_to_le32(flag
);
233 return snd_fw_transaction(ff
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
234 LATTER_ISOC_START
, ®
, sizeof(reg
), 0);
237 static void latter_finish_session(struct snd_ff
*ff
)
241 reg
= cpu_to_le32(0x00000000);
242 snd_fw_transaction(ff
->unit
, TCODE_WRITE_QUADLET_REQUEST
,
243 LATTER_ISOC_START
, ®
, sizeof(reg
), 0);
246 static void latter_dump_status(struct snd_ff
*ff
, struct snd_info_buffer
*buffer
)
248 static const struct {
252 } *clk_entry
, clk_entries
[] = {
253 { "S/PDIF", 0x00000001, 0x00000010, },
254 { "ADAT", 0x00000002, 0x00000020, },
255 { "WDClk", 0x00000004, 0x00000040, },
260 enum snd_ff_clock_src src
;
265 err
= snd_fw_transaction(ff
->unit
, TCODE_READ_QUADLET_REQUEST
,
266 LATTER_SYNC_STATUS
, ®
, sizeof(reg
), 0);
269 data
= le32_to_cpu(reg
);
271 snd_iprintf(buffer
, "External source detection:\n");
273 for (i
= 0; i
< ARRAY_SIZE(clk_entries
); ++i
) {
274 clk_entry
= clk_entries
+ i
;
275 snd_iprintf(buffer
, "%s: ", clk_entry
->label
);
276 if (data
& clk_entry
->locked_mask
) {
277 if (data
& clk_entry
->synced_mask
)
278 snd_iprintf(buffer
, "sync\n");
280 snd_iprintf(buffer
, "lock\n");
282 snd_iprintf(buffer
, "none\n");
286 err
= parse_clock_bits(data
, &rate
, &src
, ff
->unit_version
);
289 label
= snd_ff_proc_get_clk_label(src
);
293 snd_iprintf(buffer
, "Referred clock: %s %d\n", label
, rate
);
296 // NOTE: transactions are transferred within 0x00-0x7f in allocated range of
297 // address. This seems to be for check of discontinuity in receiver side.
299 // Like Fireface 400, drivers can select one of 4 options for lower 4 bytes of
300 // destination address by bit flags in quadlet register (little endian) at
301 // 0x'ffff'0000'0014:
303 // bit flags: offset of destination address
304 // - 0x00002000: 0x'....'....'0000'0000
305 // - 0x00004000: 0x'....'....'0000'0080
306 // - 0x00008000: 0x'....'....'0000'0100
307 // - 0x00010000: 0x'....'....'0000'0180
309 // Drivers can suppress the device to transfer asynchronous transactions by
310 // clear these bit flags.
312 // Actually, the register is write-only and includes the other settings such as
313 // input attenuation. This driver allocates for the first option
314 // (0x'....'....'0000'0000) and expects userspace application to configure the
316 static void latter_handle_midi_msg(struct snd_ff
*ff
, unsigned int offset
,
317 __le32
*buf
, size_t length
)
319 u32 data
= le32_to_cpu(*buf
);
320 unsigned int index
= (data
& 0x000000f0) >> 4;
322 struct snd_rawmidi_substream
*substream
;
325 if (index
>= ff
->spec
->midi_in_ports
)
328 switch (data
& 0x0000000f) {
341 len
= data
& 0x00000003;
347 byte
[0] = (data
& 0x0000ff00) >> 8;
348 byte
[1] = (data
& 0x00ff0000) >> 16;
349 byte
[2] = (data
& 0xff000000) >> 24;
351 substream
= READ_ONCE(ff
->tx_midi_substreams
[index
]);
353 snd_rawmidi_receive(substream
, byte
, len
);
357 * When return minus value, given argument is not MIDI status.
358 * When return 0, given argument is a beginning of system exclusive.
359 * When return the others, given argument is MIDI data.
361 static inline int calculate_message_bytes(u8 status
)
364 case 0xf6: /* Tune request. */
365 case 0xf8: /* Timing clock. */
366 case 0xfa: /* Start. */
367 case 0xfb: /* Continue. */
368 case 0xfc: /* Stop. */
369 case 0xfe: /* Active sensing. */
370 case 0xff: /* System reset. */
372 case 0xf1: /* MIDI time code quarter frame. */
373 case 0xf3: /* Song select. */
375 case 0xf2: /* Song position pointer. */
377 case 0xf0: /* Exclusive. */
379 case 0xf7: /* End of exclusive. */
381 case 0xf4: /* Undefined. */
382 case 0xf5: /* Undefined. */
383 case 0xf9: /* Undefined. */
384 case 0xfd: /* Undefined. */
387 switch (status
& 0xf0) {
388 case 0x80: /* Note on. */
389 case 0x90: /* Note off. */
390 case 0xa0: /* Polyphonic key pressure. */
391 case 0xb0: /* Control change and Mode change. */
392 case 0xe0: /* Pitch bend change. */
394 case 0xc0: /* Program change. */
395 case 0xd0: /* Channel pressure. */
406 static int latter_fill_midi_msg(struct snd_ff
*ff
,
407 struct snd_rawmidi_substream
*substream
,
411 u8
*buf
= (u8
*)&data
;
415 consumed
= snd_rawmidi_transmit_peek(substream
, buf
+ 1, 3);
419 if (!ff
->on_sysex
[port
]) {
420 if (buf
[1] != 0xf0) {
421 if (consumed
< calculate_message_bytes(buf
[1]))
424 // The beginning of exclusives.
425 ff
->on_sysex
[port
] = true;
430 if (buf
[1] != 0xf7) {
431 if (buf
[2] == 0xf7 || buf
[3] == 0xf7) {
432 // Transfer end code at next time.
438 // The end of exclusives.
439 ff
->on_sysex
[port
] = false;
445 ff
->msg_buf
[port
][0] = cpu_to_le32(data
);
446 ff
->rx_bytes
[port
] = consumed
;
451 const struct snd_ff_protocol snd_ff_protocol_latter
= {
452 .handle_midi_msg
= latter_handle_midi_msg
,
453 .fill_midi_msg
= latter_fill_midi_msg
,
454 .get_clock
= latter_get_clock
,
455 .switch_fetching_mode
= latter_switch_fetching_mode
,
456 .allocate_resources
= latter_allocate_resources
,
457 .begin_session
= latter_begin_session
,
458 .finish_session
= latter_finish_session
,
459 .dump_status
= latter_dump_status
,