WIP FPC-III support
[linux/fpc-iii.git] / sound / firewire / fireface / ff-stream.c
blob5452115c0ef93e52a3e0da00f65c26e9fca73648
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ff-stream.c - a part of driver for RME Fireface series
5 * Copyright (c) 2015-2017 Takashi Sakamoto
6 */
8 #include "ff.h"
10 #define CALLBACK_TIMEOUT_MS 200
12 int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
13 enum snd_ff_stream_mode *mode)
15 static const enum snd_ff_stream_mode modes[] = {
16 [CIP_SFC_32000] = SND_FF_STREAM_MODE_LOW,
17 [CIP_SFC_44100] = SND_FF_STREAM_MODE_LOW,
18 [CIP_SFC_48000] = SND_FF_STREAM_MODE_LOW,
19 [CIP_SFC_88200] = SND_FF_STREAM_MODE_MID,
20 [CIP_SFC_96000] = SND_FF_STREAM_MODE_MID,
21 [CIP_SFC_176400] = SND_FF_STREAM_MODE_HIGH,
22 [CIP_SFC_192000] = SND_FF_STREAM_MODE_HIGH,
25 if (sfc >= CIP_SFC_COUNT)
26 return -EINVAL;
28 *mode = modes[sfc];
30 return 0;
33 static inline void finish_session(struct snd_ff *ff)
35 ff->spec->protocol->finish_session(ff);
36 ff->spec->protocol->switch_fetching_mode(ff, false);
39 static int init_stream(struct snd_ff *ff, struct amdtp_stream *s)
41 struct fw_iso_resources *resources;
42 enum amdtp_stream_direction dir;
43 int err;
45 if (s == &ff->tx_stream) {
46 resources = &ff->tx_resources;
47 dir = AMDTP_IN_STREAM;
48 } else {
49 resources = &ff->rx_resources;
50 dir = AMDTP_OUT_STREAM;
53 err = fw_iso_resources_init(resources, ff->unit);
54 if (err < 0)
55 return err;
57 err = amdtp_ff_init(s, ff->unit, dir);
58 if (err < 0)
59 fw_iso_resources_destroy(resources);
61 return err;
64 static void destroy_stream(struct snd_ff *ff, struct amdtp_stream *s)
66 amdtp_stream_destroy(s);
68 if (s == &ff->tx_stream)
69 fw_iso_resources_destroy(&ff->tx_resources);
70 else
71 fw_iso_resources_destroy(&ff->rx_resources);
74 int snd_ff_stream_init_duplex(struct snd_ff *ff)
76 int err;
78 err = init_stream(ff, &ff->rx_stream);
79 if (err < 0)
80 return err;
82 err = init_stream(ff, &ff->tx_stream);
83 if (err < 0) {
84 destroy_stream(ff, &ff->rx_stream);
85 return err;
88 err = amdtp_domain_init(&ff->domain);
89 if (err < 0) {
90 destroy_stream(ff, &ff->rx_stream);
91 destroy_stream(ff, &ff->tx_stream);
94 return err;
98 * This function should be called before starting streams or after stopping
99 * streams.
101 void snd_ff_stream_destroy_duplex(struct snd_ff *ff)
103 amdtp_domain_destroy(&ff->domain);
105 destroy_stream(ff, &ff->rx_stream);
106 destroy_stream(ff, &ff->tx_stream);
109 int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
110 unsigned int frames_per_period,
111 unsigned int frames_per_buffer)
113 unsigned int curr_rate;
114 enum snd_ff_clock_src src;
115 int err;
117 err = ff->spec->protocol->get_clock(ff, &curr_rate, &src);
118 if (err < 0)
119 return err;
121 if (ff->substreams_counter == 0 || curr_rate != rate) {
122 enum snd_ff_stream_mode mode;
123 int i;
125 amdtp_domain_stop(&ff->domain);
126 finish_session(ff);
128 fw_iso_resources_free(&ff->tx_resources);
129 fw_iso_resources_free(&ff->rx_resources);
131 for (i = 0; i < CIP_SFC_COUNT; ++i) {
132 if (amdtp_rate_table[i] == rate)
133 break;
135 if (i >= CIP_SFC_COUNT)
136 return -EINVAL;
138 err = snd_ff_stream_get_multiplier_mode(i, &mode);
139 if (err < 0)
140 return err;
142 err = amdtp_ff_set_parameters(&ff->tx_stream, rate,
143 ff->spec->pcm_capture_channels[mode]);
144 if (err < 0)
145 return err;
147 err = amdtp_ff_set_parameters(&ff->rx_stream, rate,
148 ff->spec->pcm_playback_channels[mode]);
149 if (err < 0)
150 return err;
152 err = ff->spec->protocol->allocate_resources(ff, rate);
153 if (err < 0)
154 return err;
156 err = amdtp_domain_set_events_per_period(&ff->domain,
157 frames_per_period, frames_per_buffer);
158 if (err < 0) {
159 fw_iso_resources_free(&ff->tx_resources);
160 fw_iso_resources_free(&ff->rx_resources);
161 return err;
165 return 0;
168 int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
170 int err;
172 if (ff->substreams_counter == 0)
173 return 0;
175 if (amdtp_streaming_error(&ff->tx_stream) ||
176 amdtp_streaming_error(&ff->rx_stream)) {
177 amdtp_domain_stop(&ff->domain);
178 finish_session(ff);
182 * Regardless of current source of clock signal, drivers transfer some
183 * packets. Then, the device transfers packets.
185 if (!amdtp_stream_running(&ff->rx_stream)) {
186 int spd = fw_parent_device(ff->unit)->max_speed;
188 err = ff->spec->protocol->begin_session(ff, rate);
189 if (err < 0)
190 goto error;
192 err = amdtp_domain_add_stream(&ff->domain, &ff->rx_stream,
193 ff->rx_resources.channel, spd);
194 if (err < 0)
195 goto error;
197 err = amdtp_domain_add_stream(&ff->domain, &ff->tx_stream,
198 ff->tx_resources.channel, spd);
199 if (err < 0)
200 goto error;
202 err = amdtp_domain_start(&ff->domain, 0);
203 if (err < 0)
204 goto error;
206 if (!amdtp_stream_wait_callback(&ff->rx_stream,
207 CALLBACK_TIMEOUT_MS) ||
208 !amdtp_stream_wait_callback(&ff->tx_stream,
209 CALLBACK_TIMEOUT_MS)) {
210 err = -ETIMEDOUT;
211 goto error;
214 err = ff->spec->protocol->switch_fetching_mode(ff, true);
215 if (err < 0)
216 goto error;
219 return 0;
220 error:
221 amdtp_domain_stop(&ff->domain);
222 finish_session(ff);
224 return err;
227 void snd_ff_stream_stop_duplex(struct snd_ff *ff)
229 if (ff->substreams_counter == 0) {
230 amdtp_domain_stop(&ff->domain);
231 finish_session(ff);
233 fw_iso_resources_free(&ff->tx_resources);
234 fw_iso_resources_free(&ff->rx_resources);
238 void snd_ff_stream_update_duplex(struct snd_ff *ff)
240 amdtp_domain_stop(&ff->domain);
242 // The device discontinue to transfer packets.
243 amdtp_stream_pcm_abort(&ff->tx_stream);
244 amdtp_stream_pcm_abort(&ff->rx_stream);
247 void snd_ff_stream_lock_changed(struct snd_ff *ff)
249 ff->dev_lock_changed = true;
250 wake_up(&ff->hwdep_wait);
253 int snd_ff_stream_lock_try(struct snd_ff *ff)
255 int err;
257 spin_lock_irq(&ff->lock);
259 /* user land lock this */
260 if (ff->dev_lock_count < 0) {
261 err = -EBUSY;
262 goto end;
265 /* this is the first time */
266 if (ff->dev_lock_count++ == 0)
267 snd_ff_stream_lock_changed(ff);
268 err = 0;
269 end:
270 spin_unlock_irq(&ff->lock);
271 return err;
274 void snd_ff_stream_lock_release(struct snd_ff *ff)
276 spin_lock_irq(&ff->lock);
278 if (WARN_ON(ff->dev_lock_count <= 0))
279 goto end;
280 if (--ff->dev_lock_count == 0)
281 snd_ff_stream_lock_changed(ff);
282 end:
283 spin_unlock_irq(&ff->lock);