1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun // ff-protocol-former.c - a part of driver for RME Fireface series
3*4882a593Smuzhiyun //
4*4882a593Smuzhiyun // Copyright (c) 2019 Takashi Sakamoto
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Licensed under the terms of the GNU General Public License, version 2.
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include "ff.h"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #define FORMER_REG_SYNC_STATUS 0x0000801c0000ull
13*4882a593Smuzhiyun /* For block write request. */
14*4882a593Smuzhiyun #define FORMER_REG_FETCH_PCM_FRAMES 0x0000801c0000ull
15*4882a593Smuzhiyun #define FORMER_REG_CLOCK_CONFIG 0x0000801c0004ull
16*4882a593Smuzhiyun
parse_clock_bits(u32 data,unsigned int * rate,enum snd_ff_clock_src * src)17*4882a593Smuzhiyun static int parse_clock_bits(u32 data, unsigned int *rate,
18*4882a593Smuzhiyun enum snd_ff_clock_src *src)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun static const struct {
21*4882a593Smuzhiyun unsigned int rate;
22*4882a593Smuzhiyun u32 mask;
23*4882a593Smuzhiyun } *rate_entry, rate_entries[] = {
24*4882a593Smuzhiyun { 32000, 0x00000002, },
25*4882a593Smuzhiyun { 44100, 0x00000000, },
26*4882a593Smuzhiyun { 48000, 0x00000006, },
27*4882a593Smuzhiyun { 64000, 0x0000000a, },
28*4882a593Smuzhiyun { 88200, 0x00000008, },
29*4882a593Smuzhiyun { 96000, 0x0000000e, },
30*4882a593Smuzhiyun { 128000, 0x00000012, },
31*4882a593Smuzhiyun { 176400, 0x00000010, },
32*4882a593Smuzhiyun { 192000, 0x00000016, },
33*4882a593Smuzhiyun };
34*4882a593Smuzhiyun static const struct {
35*4882a593Smuzhiyun enum snd_ff_clock_src src;
36*4882a593Smuzhiyun u32 mask;
37*4882a593Smuzhiyun } *clk_entry, clk_entries[] = {
38*4882a593Smuzhiyun { SND_FF_CLOCK_SRC_ADAT1, 0x00000000, },
39*4882a593Smuzhiyun { SND_FF_CLOCK_SRC_ADAT2, 0x00000400, },
40*4882a593Smuzhiyun { SND_FF_CLOCK_SRC_SPDIF, 0x00000c00, },
41*4882a593Smuzhiyun { SND_FF_CLOCK_SRC_WORD, 0x00001000, },
42*4882a593Smuzhiyun { SND_FF_CLOCK_SRC_LTC, 0x00001800, },
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun int i;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
47*4882a593Smuzhiyun rate_entry = rate_entries + i;
48*4882a593Smuzhiyun if ((data & 0x0000001e) == rate_entry->mask) {
49*4882a593Smuzhiyun *rate = rate_entry->rate;
50*4882a593Smuzhiyun break;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun if (i == ARRAY_SIZE(rate_entries))
54*4882a593Smuzhiyun return -EIO;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun if (data & 0x00000001) {
57*4882a593Smuzhiyun *src = SND_FF_CLOCK_SRC_INTERNAL;
58*4882a593Smuzhiyun } else {
59*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
60*4882a593Smuzhiyun clk_entry = clk_entries + i;
61*4882a593Smuzhiyun if ((data & 0x00001c00) == clk_entry->mask) {
62*4882a593Smuzhiyun *src = clk_entry->src;
63*4882a593Smuzhiyun break;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun if (i == ARRAY_SIZE(clk_entries))
67*4882a593Smuzhiyun return -EIO;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
former_get_clock(struct snd_ff * ff,unsigned int * rate,enum snd_ff_clock_src * src)73*4882a593Smuzhiyun static int former_get_clock(struct snd_ff *ff, unsigned int *rate,
74*4882a593Smuzhiyun enum snd_ff_clock_src *src)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun __le32 reg;
77*4882a593Smuzhiyun u32 data;
78*4882a593Smuzhiyun int err;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
81*4882a593Smuzhiyun FORMER_REG_CLOCK_CONFIG, ®, sizeof(reg), 0);
82*4882a593Smuzhiyun if (err < 0)
83*4882a593Smuzhiyun return err;
84*4882a593Smuzhiyun data = le32_to_cpu(reg);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun return parse_clock_bits(data, rate, src);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
former_switch_fetching_mode(struct snd_ff * ff,bool enable)89*4882a593Smuzhiyun static int former_switch_fetching_mode(struct snd_ff *ff, bool enable)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun unsigned int count;
92*4882a593Smuzhiyun __le32 *reg;
93*4882a593Smuzhiyun int i;
94*4882a593Smuzhiyun int err;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun count = 0;
97*4882a593Smuzhiyun for (i = 0; i < SND_FF_STREAM_MODE_COUNT; ++i)
98*4882a593Smuzhiyun count = max(count, ff->spec->pcm_playback_channels[i]);
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun reg = kcalloc(count, sizeof(__le32), GFP_KERNEL);
101*4882a593Smuzhiyun if (!reg)
102*4882a593Smuzhiyun return -ENOMEM;
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun if (!enable) {
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * Each quadlet is corresponding to data channels in a data
107*4882a593Smuzhiyun * blocks in reverse order. Precisely, quadlets for available
108*4882a593Smuzhiyun * data channels should be enabled. Here, I take second best
109*4882a593Smuzhiyun * to fetch PCM frames from all of data channels regardless of
110*4882a593Smuzhiyun * stf.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun for (i = 0; i < count; ++i)
113*4882a593Smuzhiyun reg[i] = cpu_to_le32(0x00000001);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_BLOCK_REQUEST,
117*4882a593Smuzhiyun FORMER_REG_FETCH_PCM_FRAMES, reg,
118*4882a593Smuzhiyun sizeof(__le32) * count, 0);
119*4882a593Smuzhiyun kfree(reg);
120*4882a593Smuzhiyun return err;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
dump_clock_config(struct snd_ff * ff,struct snd_info_buffer * buffer)123*4882a593Smuzhiyun static void dump_clock_config(struct snd_ff *ff, struct snd_info_buffer *buffer)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun __le32 reg;
126*4882a593Smuzhiyun u32 data;
127*4882a593Smuzhiyun unsigned int rate;
128*4882a593Smuzhiyun enum snd_ff_clock_src src;
129*4882a593Smuzhiyun const char *label;
130*4882a593Smuzhiyun int err;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
133*4882a593Smuzhiyun FORMER_REG_CLOCK_CONFIG, ®, sizeof(reg), 0);
134*4882a593Smuzhiyun if (err < 0)
135*4882a593Smuzhiyun return;
136*4882a593Smuzhiyun data = le32_to_cpu(reg);
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun snd_iprintf(buffer, "Output S/PDIF format: %s (Emphasis: %s)\n",
139*4882a593Smuzhiyun (data & 0x00000020) ? "Professional" : "Consumer",
140*4882a593Smuzhiyun (data & 0x00000040) ? "on" : "off");
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun snd_iprintf(buffer, "Optical output interface format: %s\n",
143*4882a593Smuzhiyun (data & 0x00000100) ? "S/PDIF" : "ADAT");
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun snd_iprintf(buffer, "Word output single speed: %s\n",
146*4882a593Smuzhiyun (data & 0x00002000) ? "on" : "off");
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun snd_iprintf(buffer, "S/PDIF input interface: %s\n",
149*4882a593Smuzhiyun (data & 0x00000200) ? "Optical" : "Coaxial");
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun err = parse_clock_bits(data, &rate, &src);
152*4882a593Smuzhiyun if (err < 0)
153*4882a593Smuzhiyun return;
154*4882a593Smuzhiyun label = snd_ff_proc_get_clk_label(src);
155*4882a593Smuzhiyun if (!label)
156*4882a593Smuzhiyun return;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun snd_iprintf(buffer, "Clock configuration: %d %s\n", rate, label);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
dump_sync_status(struct snd_ff * ff,struct snd_info_buffer * buffer)161*4882a593Smuzhiyun static void dump_sync_status(struct snd_ff *ff, struct snd_info_buffer *buffer)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun static const struct {
164*4882a593Smuzhiyun char *const label;
165*4882a593Smuzhiyun u32 locked_mask;
166*4882a593Smuzhiyun u32 synced_mask;
167*4882a593Smuzhiyun } *clk_entry, clk_entries[] = {
168*4882a593Smuzhiyun { "WDClk", 0x40000000, 0x20000000, },
169*4882a593Smuzhiyun { "S/PDIF", 0x00080000, 0x00040000, },
170*4882a593Smuzhiyun { "ADAT1", 0x00000400, 0x00001000, },
171*4882a593Smuzhiyun { "ADAT2", 0x00000800, 0x00002000, },
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun static const struct {
174*4882a593Smuzhiyun char *const label;
175*4882a593Smuzhiyun u32 mask;
176*4882a593Smuzhiyun } *referred_entry, referred_entries[] = {
177*4882a593Smuzhiyun { "ADAT1", 0x00000000, },
178*4882a593Smuzhiyun { "ADAT2", 0x00400000, },
179*4882a593Smuzhiyun { "S/PDIF", 0x00c00000, },
180*4882a593Smuzhiyun { "WDclk", 0x01000000, },
181*4882a593Smuzhiyun { "TCO", 0x01400000, },
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun static const struct {
184*4882a593Smuzhiyun unsigned int rate;
185*4882a593Smuzhiyun u32 mask;
186*4882a593Smuzhiyun } *rate_entry, rate_entries[] = {
187*4882a593Smuzhiyun { 32000, 0x02000000, },
188*4882a593Smuzhiyun { 44100, 0x04000000, },
189*4882a593Smuzhiyun { 48000, 0x06000000, },
190*4882a593Smuzhiyun { 64000, 0x08000000, },
191*4882a593Smuzhiyun { 88200, 0x0a000000, },
192*4882a593Smuzhiyun { 96000, 0x0c000000, },
193*4882a593Smuzhiyun { 128000, 0x0e000000, },
194*4882a593Smuzhiyun { 176400, 0x10000000, },
195*4882a593Smuzhiyun { 192000, 0x12000000, },
196*4882a593Smuzhiyun };
197*4882a593Smuzhiyun __le32 reg[2];
198*4882a593Smuzhiyun u32 data[2];
199*4882a593Smuzhiyun int i;
200*4882a593Smuzhiyun int err;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_READ_BLOCK_REQUEST,
203*4882a593Smuzhiyun FORMER_REG_SYNC_STATUS, reg, sizeof(reg), 0);
204*4882a593Smuzhiyun if (err < 0)
205*4882a593Smuzhiyun return;
206*4882a593Smuzhiyun data[0] = le32_to_cpu(reg[0]);
207*4882a593Smuzhiyun data[1] = le32_to_cpu(reg[1]);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun snd_iprintf(buffer, "External source detection:\n");
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(clk_entries); ++i) {
212*4882a593Smuzhiyun const char *state;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun clk_entry = clk_entries + i;
215*4882a593Smuzhiyun if (data[0] & clk_entry->locked_mask) {
216*4882a593Smuzhiyun if (data[0] & clk_entry->synced_mask)
217*4882a593Smuzhiyun state = "sync";
218*4882a593Smuzhiyun else
219*4882a593Smuzhiyun state = "lock";
220*4882a593Smuzhiyun } else {
221*4882a593Smuzhiyun state = "none";
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun snd_iprintf(buffer, "%s: %s\n", clk_entry->label, state);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun snd_iprintf(buffer, "Referred clock:\n");
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun if (data[1] & 0x00000001) {
230*4882a593Smuzhiyun snd_iprintf(buffer, "Internal\n");
231*4882a593Smuzhiyun } else {
232*4882a593Smuzhiyun unsigned int rate;
233*4882a593Smuzhiyun const char *label;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(referred_entries); ++i) {
236*4882a593Smuzhiyun referred_entry = referred_entries + i;
237*4882a593Smuzhiyun if ((data[0] & 0x1e0000) == referred_entry->mask) {
238*4882a593Smuzhiyun label = referred_entry->label;
239*4882a593Smuzhiyun break;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun if (i == ARRAY_SIZE(referred_entries))
243*4882a593Smuzhiyun label = "none";
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(rate_entries); ++i) {
246*4882a593Smuzhiyun rate_entry = rate_entries + i;
247*4882a593Smuzhiyun if ((data[0] & 0x1e000000) == rate_entry->mask) {
248*4882a593Smuzhiyun rate = rate_entry->rate;
249*4882a593Smuzhiyun break;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun if (i == ARRAY_SIZE(rate_entries))
253*4882a593Smuzhiyun rate = 0;
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun snd_iprintf(buffer, "%s %d\n", label, rate);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun
former_dump_status(struct snd_ff * ff,struct snd_info_buffer * buffer)259*4882a593Smuzhiyun static void former_dump_status(struct snd_ff *ff,
260*4882a593Smuzhiyun struct snd_info_buffer *buffer)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun dump_clock_config(ff, buffer);
263*4882a593Smuzhiyun dump_sync_status(ff, buffer);
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
former_fill_midi_msg(struct snd_ff * ff,struct snd_rawmidi_substream * substream,unsigned int port)266*4882a593Smuzhiyun static int former_fill_midi_msg(struct snd_ff *ff,
267*4882a593Smuzhiyun struct snd_rawmidi_substream *substream,
268*4882a593Smuzhiyun unsigned int port)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun u8 *buf = (u8 *)ff->msg_buf[port];
271*4882a593Smuzhiyun int len;
272*4882a593Smuzhiyun int i;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun len = snd_rawmidi_transmit_peek(substream, buf,
275*4882a593Smuzhiyun SND_FF_MAXIMIM_MIDI_QUADS);
276*4882a593Smuzhiyun if (len <= 0)
277*4882a593Smuzhiyun return len;
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun // One quadlet includes one byte.
280*4882a593Smuzhiyun for (i = len - 1; i >= 0; --i)
281*4882a593Smuzhiyun ff->msg_buf[port][i] = cpu_to_le32(buf[i]);
282*4882a593Smuzhiyun ff->rx_bytes[port] = len;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun return len;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun #define FF800_STF 0x0000fc88f000
288*4882a593Smuzhiyun #define FF800_RX_PACKET_FORMAT 0x0000fc88f004
289*4882a593Smuzhiyun #define FF800_ALLOC_TX_STREAM 0x0000fc88f008
290*4882a593Smuzhiyun #define FF800_ISOC_COMM_START 0x0000fc88f00c
291*4882a593Smuzhiyun #define FF800_TX_S800_FLAG 0x00000800
292*4882a593Smuzhiyun #define FF800_ISOC_COMM_STOP 0x0000fc88f010
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun #define FF800_TX_PACKET_ISOC_CH 0x0000801c0008
295*4882a593Smuzhiyun
allocate_tx_resources(struct snd_ff * ff)296*4882a593Smuzhiyun static int allocate_tx_resources(struct snd_ff *ff)
297*4882a593Smuzhiyun {
298*4882a593Smuzhiyun __le32 reg;
299*4882a593Smuzhiyun unsigned int count;
300*4882a593Smuzhiyun unsigned int tx_isoc_channel;
301*4882a593Smuzhiyun int err;
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun reg = cpu_to_le32(ff->tx_stream.data_block_quadlets);
304*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
305*4882a593Smuzhiyun FF800_ALLOC_TX_STREAM, ®, sizeof(reg), 0);
306*4882a593Smuzhiyun if (err < 0)
307*4882a593Smuzhiyun return err;
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun // Wait till the format of tx packet is available.
310*4882a593Smuzhiyun count = 0;
311*4882a593Smuzhiyun while (count++ < 10) {
312*4882a593Smuzhiyun u32 data;
313*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_READ_QUADLET_REQUEST,
314*4882a593Smuzhiyun FF800_TX_PACKET_ISOC_CH, ®, sizeof(reg), 0);
315*4882a593Smuzhiyun if (err < 0)
316*4882a593Smuzhiyun return err;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun data = le32_to_cpu(reg);
319*4882a593Smuzhiyun if (data != 0xffffffff) {
320*4882a593Smuzhiyun tx_isoc_channel = data;
321*4882a593Smuzhiyun break;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun msleep(50);
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun if (count >= 10)
327*4882a593Smuzhiyun return -ETIMEDOUT;
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun // NOTE: this is a makeshift to start OHCI 1394 IR context in the
330*4882a593Smuzhiyun // channel. On the other hand, 'struct fw_iso_resources.allocated' is
331*4882a593Smuzhiyun // not true and it's not deallocated at stop.
332*4882a593Smuzhiyun ff->tx_resources.channel = tx_isoc_channel;
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun return 0;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
ff800_allocate_resources(struct snd_ff * ff,unsigned int rate)337*4882a593Smuzhiyun static int ff800_allocate_resources(struct snd_ff *ff, unsigned int rate)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun u32 data;
340*4882a593Smuzhiyun __le32 reg;
341*4882a593Smuzhiyun int err;
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun reg = cpu_to_le32(rate);
344*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
345*4882a593Smuzhiyun FF800_STF, ®, sizeof(reg), 0);
346*4882a593Smuzhiyun if (err < 0)
347*4882a593Smuzhiyun return err;
348*4882a593Smuzhiyun
349*4882a593Smuzhiyun // If starting isochronous communication immediately, change of STF has
350*4882a593Smuzhiyun // no effect. In this case, the communication runs based on former STF.
351*4882a593Smuzhiyun // Let's sleep for a bit.
352*4882a593Smuzhiyun msleep(100);
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun // Controllers should allocate isochronous resources for rx stream.
355*4882a593Smuzhiyun err = fw_iso_resources_allocate(&ff->rx_resources,
356*4882a593Smuzhiyun amdtp_stream_get_max_payload(&ff->rx_stream),
357*4882a593Smuzhiyun fw_parent_device(ff->unit)->max_speed);
358*4882a593Smuzhiyun if (err < 0)
359*4882a593Smuzhiyun return err;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun // Set isochronous channel and the number of quadlets of rx packets.
362*4882a593Smuzhiyun // This should be done before the allocation of tx resources to avoid
363*4882a593Smuzhiyun // periodical noise.
364*4882a593Smuzhiyun data = ff->rx_stream.data_block_quadlets << 3;
365*4882a593Smuzhiyun data = (data << 8) | ff->rx_resources.channel;
366*4882a593Smuzhiyun reg = cpu_to_le32(data);
367*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
368*4882a593Smuzhiyun FF800_RX_PACKET_FORMAT, ®, sizeof(reg), 0);
369*4882a593Smuzhiyun if (err < 0)
370*4882a593Smuzhiyun return err;
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun return allocate_tx_resources(ff);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
ff800_begin_session(struct snd_ff * ff,unsigned int rate)375*4882a593Smuzhiyun static int ff800_begin_session(struct snd_ff *ff, unsigned int rate)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun unsigned int generation = ff->rx_resources.generation;
378*4882a593Smuzhiyun __le32 reg;
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun if (generation != fw_parent_device(ff->unit)->card->generation) {
381*4882a593Smuzhiyun int err = fw_iso_resources_update(&ff->rx_resources);
382*4882a593Smuzhiyun if (err < 0)
383*4882a593Smuzhiyun return err;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun reg = cpu_to_le32(0x80000000);
387*4882a593Smuzhiyun reg |= cpu_to_le32(ff->tx_stream.data_block_quadlets);
388*4882a593Smuzhiyun if (fw_parent_device(ff->unit)->max_speed == SCODE_800)
389*4882a593Smuzhiyun reg |= cpu_to_le32(FF800_TX_S800_FLAG);
390*4882a593Smuzhiyun return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
391*4882a593Smuzhiyun FF800_ISOC_COMM_START, ®, sizeof(reg), 0);
392*4882a593Smuzhiyun }
393*4882a593Smuzhiyun
ff800_finish_session(struct snd_ff * ff)394*4882a593Smuzhiyun static void ff800_finish_session(struct snd_ff *ff)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun __le32 reg;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun reg = cpu_to_le32(0x80000000);
399*4882a593Smuzhiyun snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
400*4882a593Smuzhiyun FF800_ISOC_COMM_STOP, ®, sizeof(reg), 0);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun // Fireface 800 doesn't allow drivers to register lower 4 bytes of destination
404*4882a593Smuzhiyun // address.
405*4882a593Smuzhiyun // A write transaction to clear registered higher 4 bytes of destination address
406*4882a593Smuzhiyun // has an effect to suppress asynchronous transaction from device.
ff800_handle_midi_msg(struct snd_ff * ff,unsigned int offset,__le32 * buf,size_t length)407*4882a593Smuzhiyun static void ff800_handle_midi_msg(struct snd_ff *ff, unsigned int offset,
408*4882a593Smuzhiyun __le32 *buf, size_t length)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun int i;
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun for (i = 0; i < length / 4; i++) {
413*4882a593Smuzhiyun u8 byte = le32_to_cpu(buf[i]) & 0xff;
414*4882a593Smuzhiyun struct snd_rawmidi_substream *substream;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun substream = READ_ONCE(ff->tx_midi_substreams[0]);
417*4882a593Smuzhiyun if (substream)
418*4882a593Smuzhiyun snd_rawmidi_receive(substream, &byte, 1);
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun const struct snd_ff_protocol snd_ff_protocol_ff800 = {
423*4882a593Smuzhiyun .handle_midi_msg = ff800_handle_midi_msg,
424*4882a593Smuzhiyun .fill_midi_msg = former_fill_midi_msg,
425*4882a593Smuzhiyun .get_clock = former_get_clock,
426*4882a593Smuzhiyun .switch_fetching_mode = former_switch_fetching_mode,
427*4882a593Smuzhiyun .allocate_resources = ff800_allocate_resources,
428*4882a593Smuzhiyun .begin_session = ff800_begin_session,
429*4882a593Smuzhiyun .finish_session = ff800_finish_session,
430*4882a593Smuzhiyun .dump_status = former_dump_status,
431*4882a593Smuzhiyun };
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun #define FF400_STF 0x000080100500ull
434*4882a593Smuzhiyun #define FF400_RX_PACKET_FORMAT 0x000080100504ull
435*4882a593Smuzhiyun #define FF400_ISOC_COMM_START 0x000080100508ull
436*4882a593Smuzhiyun #define FF400_TX_PACKET_FORMAT 0x00008010050cull
437*4882a593Smuzhiyun #define FF400_ISOC_COMM_STOP 0x000080100510ull
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun // Fireface 400 manages isochronous channel number in 3 bit field. Therefore,
440*4882a593Smuzhiyun // we can allocate between 0 and 7 channel.
ff400_allocate_resources(struct snd_ff * ff,unsigned int rate)441*4882a593Smuzhiyun static int ff400_allocate_resources(struct snd_ff *ff, unsigned int rate)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun __le32 reg;
444*4882a593Smuzhiyun enum snd_ff_stream_mode mode;
445*4882a593Smuzhiyun int i;
446*4882a593Smuzhiyun int err;
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun // Check whether the given value is supported or not.
449*4882a593Smuzhiyun for (i = 0; i < CIP_SFC_COUNT; i++) {
450*4882a593Smuzhiyun if (amdtp_rate_table[i] == rate)
451*4882a593Smuzhiyun break;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun if (i >= CIP_SFC_COUNT)
454*4882a593Smuzhiyun return -EINVAL;
455*4882a593Smuzhiyun
456*4882a593Smuzhiyun // Set the number of data blocks transferred in a second.
457*4882a593Smuzhiyun reg = cpu_to_le32(rate);
458*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
459*4882a593Smuzhiyun FF400_STF, ®, sizeof(reg), 0);
460*4882a593Smuzhiyun if (err < 0)
461*4882a593Smuzhiyun return err;
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun msleep(100);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun err = snd_ff_stream_get_multiplier_mode(i, &mode);
466*4882a593Smuzhiyun if (err < 0)
467*4882a593Smuzhiyun return err;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun // Keep resources for in-stream.
470*4882a593Smuzhiyun ff->tx_resources.channels_mask = 0x00000000000000ffuLL;
471*4882a593Smuzhiyun err = fw_iso_resources_allocate(&ff->tx_resources,
472*4882a593Smuzhiyun amdtp_stream_get_max_payload(&ff->tx_stream),
473*4882a593Smuzhiyun fw_parent_device(ff->unit)->max_speed);
474*4882a593Smuzhiyun if (err < 0)
475*4882a593Smuzhiyun return err;
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun // Keep resources for out-stream.
478*4882a593Smuzhiyun ff->rx_resources.channels_mask = 0x00000000000000ffuLL;
479*4882a593Smuzhiyun err = fw_iso_resources_allocate(&ff->rx_resources,
480*4882a593Smuzhiyun amdtp_stream_get_max_payload(&ff->rx_stream),
481*4882a593Smuzhiyun fw_parent_device(ff->unit)->max_speed);
482*4882a593Smuzhiyun if (err < 0)
483*4882a593Smuzhiyun fw_iso_resources_free(&ff->tx_resources);
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun return err;
486*4882a593Smuzhiyun }
487*4882a593Smuzhiyun
ff400_begin_session(struct snd_ff * ff,unsigned int rate)488*4882a593Smuzhiyun static int ff400_begin_session(struct snd_ff *ff, unsigned int rate)
489*4882a593Smuzhiyun {
490*4882a593Smuzhiyun unsigned int generation = ff->rx_resources.generation;
491*4882a593Smuzhiyun __le32 reg;
492*4882a593Smuzhiyun int err;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun if (generation != fw_parent_device(ff->unit)->card->generation) {
495*4882a593Smuzhiyun err = fw_iso_resources_update(&ff->tx_resources);
496*4882a593Smuzhiyun if (err < 0)
497*4882a593Smuzhiyun return err;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun err = fw_iso_resources_update(&ff->rx_resources);
500*4882a593Smuzhiyun if (err < 0)
501*4882a593Smuzhiyun return err;
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun // Set isochronous channel and the number of quadlets of received
505*4882a593Smuzhiyun // packets.
506*4882a593Smuzhiyun reg = cpu_to_le32(((ff->rx_stream.data_block_quadlets << 3) << 8) |
507*4882a593Smuzhiyun ff->rx_resources.channel);
508*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
509*4882a593Smuzhiyun FF400_RX_PACKET_FORMAT, ®, sizeof(reg), 0);
510*4882a593Smuzhiyun if (err < 0)
511*4882a593Smuzhiyun return err;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun // Set isochronous channel and the number of quadlets of transmitted
514*4882a593Smuzhiyun // packet.
515*4882a593Smuzhiyun // TODO: investigate the purpose of this 0x80.
516*4882a593Smuzhiyun reg = cpu_to_le32((0x80 << 24) |
517*4882a593Smuzhiyun (ff->tx_resources.channel << 5) |
518*4882a593Smuzhiyun (ff->tx_stream.data_block_quadlets));
519*4882a593Smuzhiyun err = snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
520*4882a593Smuzhiyun FF400_TX_PACKET_FORMAT, ®, sizeof(reg), 0);
521*4882a593Smuzhiyun if (err < 0)
522*4882a593Smuzhiyun return err;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun // Allow to transmit packets.
525*4882a593Smuzhiyun reg = cpu_to_le32(0x00000001);
526*4882a593Smuzhiyun return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
527*4882a593Smuzhiyun FF400_ISOC_COMM_START, ®, sizeof(reg), 0);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
ff400_finish_session(struct snd_ff * ff)530*4882a593Smuzhiyun static void ff400_finish_session(struct snd_ff *ff)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun __le32 reg;
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun reg = cpu_to_le32(0x80000000);
535*4882a593Smuzhiyun snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
536*4882a593Smuzhiyun FF400_ISOC_COMM_STOP, ®, sizeof(reg), 0);
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun // For Fireface 400, lower 4 bytes of destination address is configured by bit
540*4882a593Smuzhiyun // flag in quadlet register (little endian) at 0x'0000'801'0051c. Drivers can
541*4882a593Smuzhiyun // select one of 4 options:
542*4882a593Smuzhiyun //
543*4882a593Smuzhiyun // bit flags: offset of destination address
544*4882a593Smuzhiyun // - 0x04000000: 0x'....'....'0000'0000
545*4882a593Smuzhiyun // - 0x08000000: 0x'....'....'0000'0080
546*4882a593Smuzhiyun // - 0x10000000: 0x'....'....'0000'0100
547*4882a593Smuzhiyun // - 0x20000000: 0x'....'....'0000'0180
548*4882a593Smuzhiyun //
549*4882a593Smuzhiyun // Drivers can suppress the device to transfer asynchronous transactions by
550*4882a593Smuzhiyun // using below 2 bits.
551*4882a593Smuzhiyun // - 0x01000000: suppress transmission
552*4882a593Smuzhiyun // - 0x02000000: suppress transmission
553*4882a593Smuzhiyun //
554*4882a593Smuzhiyun // Actually, the register is write-only and includes the other options such as
555*4882a593Smuzhiyun // input attenuation. This driver allocates destination address with '0000'0000
556*4882a593Smuzhiyun // in its lower offset and expects userspace application to configure the
557*4882a593Smuzhiyun // register for it.
ff400_handle_midi_msg(struct snd_ff * ff,unsigned int offset,__le32 * buf,size_t length)558*4882a593Smuzhiyun static void ff400_handle_midi_msg(struct snd_ff *ff, unsigned int offset,
559*4882a593Smuzhiyun __le32 *buf, size_t length)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun int i;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun for (i = 0; i < length / 4; i++) {
564*4882a593Smuzhiyun u32 quad = le32_to_cpu(buf[i]);
565*4882a593Smuzhiyun u8 byte;
566*4882a593Smuzhiyun unsigned int index;
567*4882a593Smuzhiyun struct snd_rawmidi_substream *substream;
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* Message in first port. */
570*4882a593Smuzhiyun /*
571*4882a593Smuzhiyun * This value may represent the index of this unit when the same
572*4882a593Smuzhiyun * units are on the same IEEE 1394 bus. This driver doesn't use
573*4882a593Smuzhiyun * it.
574*4882a593Smuzhiyun */
575*4882a593Smuzhiyun index = (quad >> 8) & 0xff;
576*4882a593Smuzhiyun if (index > 0) {
577*4882a593Smuzhiyun substream = READ_ONCE(ff->tx_midi_substreams[0]);
578*4882a593Smuzhiyun if (substream != NULL) {
579*4882a593Smuzhiyun byte = quad & 0xff;
580*4882a593Smuzhiyun snd_rawmidi_receive(substream, &byte, 1);
581*4882a593Smuzhiyun }
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /* Message in second port. */
585*4882a593Smuzhiyun index = (quad >> 24) & 0xff;
586*4882a593Smuzhiyun if (index > 0) {
587*4882a593Smuzhiyun substream = READ_ONCE(ff->tx_midi_substreams[1]);
588*4882a593Smuzhiyun if (substream != NULL) {
589*4882a593Smuzhiyun byte = (quad >> 16) & 0xff;
590*4882a593Smuzhiyun snd_rawmidi_receive(substream, &byte, 1);
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun const struct snd_ff_protocol snd_ff_protocol_ff400 = {
597*4882a593Smuzhiyun .handle_midi_msg = ff400_handle_midi_msg,
598*4882a593Smuzhiyun .fill_midi_msg = former_fill_midi_msg,
599*4882a593Smuzhiyun .get_clock = former_get_clock,
600*4882a593Smuzhiyun .switch_fetching_mode = former_switch_fetching_mode,
601*4882a593Smuzhiyun .allocate_resources = ff400_allocate_resources,
602*4882a593Smuzhiyun .begin_session = ff400_begin_session,
603*4882a593Smuzhiyun .finish_session = ff400_finish_session,
604*4882a593Smuzhiyun .dump_status = former_dump_status,
605*4882a593Smuzhiyun };
606