ALSA: firewire-lib: use variable size of queue for isoc packets instead of fixed...
[linux-2.6-block.git] / sound / firewire / fireface / ff-stream.c
CommitLineData
da607e19 1// SPDX-License-Identifier: GPL-2.0-only
75d6d898
TS
2/*
3 * ff-stream.c - a part of driver for RME Fireface series
4 *
5 * Copyright (c) 2015-2017 Takashi Sakamoto
75d6d898
TS
6 */
7
8#include "ff.h"
9
10#define CALLBACK_TIMEOUT_MS 200
11
76ea4688
TS
12int snd_ff_stream_get_multiplier_mode(enum cip_sfc sfc,
13 enum snd_ff_stream_mode *mode)
75d6d898 14{
76ea4688
TS
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,
23 };
24
25 if (sfc >= CIP_SFC_COUNT)
75d6d898
TS
26 return -EINVAL;
27
76ea4688 28 *mode = modes[sfc];
75d6d898
TS
29
30 return 0;
31}
32
75d6d898
TS
33static inline void finish_session(struct snd_ff *ff)
34{
35 ff->spec->protocol->finish_session(ff);
ae3053c2 36 ff->spec->protocol->switch_fetching_mode(ff, false);
75d6d898
TS
37}
38
42355abb 39static int init_stream(struct snd_ff *ff, struct amdtp_stream *s)
75d6d898 40{
75d6d898 41 struct fw_iso_resources *resources;
42355abb
TS
42 enum amdtp_stream_direction dir;
43 int err;
75d6d898 44
42355abb 45 if (s == &ff->tx_stream) {
75d6d898 46 resources = &ff->tx_resources;
42355abb 47 dir = AMDTP_IN_STREAM;
75d6d898
TS
48 } else {
49 resources = &ff->rx_resources;
42355abb 50 dir = AMDTP_OUT_STREAM;
75d6d898
TS
51 }
52
53 err = fw_iso_resources_init(resources, ff->unit);
54 if (err < 0)
55 return err;
56
42355abb 57 err = amdtp_ff_init(s, ff->unit, dir);
75d6d898
TS
58 if (err < 0)
59 fw_iso_resources_destroy(resources);
60
61 return err;
62}
63
42355abb 64static void destroy_stream(struct snd_ff *ff, struct amdtp_stream *s)
75d6d898 65{
42355abb
TS
66 amdtp_stream_destroy(s);
67
68 if (s == &ff->tx_stream)
75d6d898 69 fw_iso_resources_destroy(&ff->tx_resources);
42355abb 70 else
75d6d898 71 fw_iso_resources_destroy(&ff->rx_resources);
75d6d898
TS
72}
73
74int snd_ff_stream_init_duplex(struct snd_ff *ff)
75{
76 int err;
77
42355abb 78 err = init_stream(ff, &ff->rx_stream);
75d6d898 79 if (err < 0)
42355abb 80 return err;
75d6d898 81
42355abb 82 err = init_stream(ff, &ff->tx_stream);
c9a9ce89 83 if (err < 0) {
42355abb 84 destroy_stream(ff, &ff->rx_stream);
c9a9ce89
TS
85 return err;
86 }
87
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);
92 }
42355abb 93
75d6d898
TS
94 return err;
95}
96
97/*
98 * This function should be called before starting streams or after stopping
99 * streams.
100 */
101void snd_ff_stream_destroy_duplex(struct snd_ff *ff)
102{
c9a9ce89
TS
103 amdtp_domain_destroy(&ff->domain);
104
42355abb
TS
105 destroy_stream(ff, &ff->rx_stream);
106 destroy_stream(ff, &ff->tx_stream);
75d6d898
TS
107}
108
9d9ff58c
TS
109int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
110 unsigned int frames_per_period)
75d6d898
TS
111{
112 unsigned int curr_rate;
113 enum snd_ff_clock_src src;
114 int err;
115
b1d0cb0a 116 err = ff->spec->protocol->get_clock(ff, &curr_rate, &src);
75d6d898
TS
117 if (err < 0)
118 return err;
55162d2b
TS
119
120 if (ff->substreams_counter == 0 || curr_rate != rate) {
121 enum snd_ff_stream_mode mode;
122 int i;
75d6d898 123
c9a9ce89 124 amdtp_domain_stop(&ff->domain);
55162d2b 125 finish_session(ff);
75d6d898 126
55162d2b
TS
127 fw_iso_resources_free(&ff->tx_resources);
128 fw_iso_resources_free(&ff->rx_resources);
365c00d0
TS
129
130 for (i = 0; i < CIP_SFC_COUNT; ++i) {
131 if (amdtp_rate_table[i] == rate)
132 break;
133 }
134 if (i >= CIP_SFC_COUNT)
135 return -EINVAL;
136
137 err = snd_ff_stream_get_multiplier_mode(i, &mode);
75d6d898 138 if (err < 0)
365c00d0
TS
139 return err;
140
141 err = amdtp_ff_set_parameters(&ff->tx_stream, rate,
142 ff->spec->pcm_capture_channels[mode]);
143 if (err < 0)
144 return err;
145
146 err = amdtp_ff_set_parameters(&ff->rx_stream, rate,
147 ff->spec->pcm_playback_channels[mode]);
148 if (err < 0)
149 return err;
75d6d898 150
47b87c8e
TS
151 err = ff->spec->protocol->allocate_resources(ff, rate);
152 if (err < 0)
55162d2b 153 return err;
9d9ff58c
TS
154
155 err = amdtp_domain_set_events_per_period(&ff->domain,
a0e02331 156 frames_per_period, 0);
9d9ff58c
TS
157 if (err < 0) {
158 fw_iso_resources_free(&ff->tx_resources);
159 fw_iso_resources_free(&ff->rx_resources);
160 return err;
161 }
55162d2b
TS
162 }
163
164 return 0;
165}
166
55162d2b
TS
167int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
168{
169 int err;
170
171 if (ff->substreams_counter == 0)
172 return 0;
47b87c8e 173
55162d2b 174 if (amdtp_streaming_error(&ff->tx_stream) ||
c9a9ce89
TS
175 amdtp_streaming_error(&ff->rx_stream)) {
176 amdtp_domain_stop(&ff->domain);
55162d2b 177 finish_session(ff);
c9a9ce89 178 }
55162d2b
TS
179
180 /*
181 * Regardless of current source of clock signal, drivers transfer some
182 * packets. Then, the device transfers packets.
183 */
184 if (!amdtp_stream_running(&ff->rx_stream)) {
c9a9ce89
TS
185 int spd = fw_parent_device(ff->unit)->max_speed;
186
75d6d898
TS
187 err = ff->spec->protocol->begin_session(ff, rate);
188 if (err < 0)
189 goto error;
190
c9a9ce89
TS
191 err = amdtp_domain_add_stream(&ff->domain, &ff->rx_stream,
192 ff->rx_resources.channel, spd);
75d6d898
TS
193 if (err < 0)
194 goto error;
195
c9a9ce89
TS
196 err = amdtp_domain_add_stream(&ff->domain, &ff->tx_stream,
197 ff->tx_resources.channel, spd);
75d6d898
TS
198 if (err < 0)
199 goto error;
75d6d898 200
c9a9ce89 201 err = amdtp_domain_start(&ff->domain);
75d6d898
TS
202 if (err < 0)
203 goto error;
204
c9a9ce89
TS
205 if (!amdtp_stream_wait_callback(&ff->rx_stream,
206 CALLBACK_TIMEOUT_MS) ||
207 !amdtp_stream_wait_callback(&ff->tx_stream,
75d6d898
TS
208 CALLBACK_TIMEOUT_MS)) {
209 err = -ETIMEDOUT;
210 goto error;
211 }
c9a9ce89
TS
212
213 err = ff->spec->protocol->switch_fetching_mode(ff, true);
214 if (err < 0)
215 goto error;
75d6d898
TS
216 }
217
218 return 0;
219error:
c9a9ce89 220 amdtp_domain_stop(&ff->domain);
75d6d898 221 finish_session(ff);
75d6d898
TS
222
223 return err;
224}
225
226void snd_ff_stream_stop_duplex(struct snd_ff *ff)
227{
af26bacc 228 if (ff->substreams_counter == 0) {
c9a9ce89 229 amdtp_domain_stop(&ff->domain);
f55e2a89 230 finish_session(ff);
af26bacc
TS
231
232 fw_iso_resources_free(&ff->tx_resources);
233 fw_iso_resources_free(&ff->rx_resources);
234 }
75d6d898
TS
235}
236
237void snd_ff_stream_update_duplex(struct snd_ff *ff)
238{
c9a9ce89
TS
239 amdtp_domain_stop(&ff->domain);
240
b88f4d7c 241 // The device discontinue to transfer packets.
75d6d898 242 amdtp_stream_pcm_abort(&ff->tx_stream);
75d6d898 243 amdtp_stream_pcm_abort(&ff->rx_stream);
75d6d898 244}
f656edd5
TS
245
246void snd_ff_stream_lock_changed(struct snd_ff *ff)
247{
248 ff->dev_lock_changed = true;
249 wake_up(&ff->hwdep_wait);
250}
251
252int snd_ff_stream_lock_try(struct snd_ff *ff)
253{
254 int err;
255
256 spin_lock_irq(&ff->lock);
257
258 /* user land lock this */
259 if (ff->dev_lock_count < 0) {
260 err = -EBUSY;
261 goto end;
262 }
263
264 /* this is the first time */
265 if (ff->dev_lock_count++ == 0)
266 snd_ff_stream_lock_changed(ff);
267 err = 0;
268end:
269 spin_unlock_irq(&ff->lock);
270 return err;
271}
272
273void snd_ff_stream_lock_release(struct snd_ff *ff)
274{
275 spin_lock_irq(&ff->lock);
276
277 if (WARN_ON(ff->dev_lock_count <= 0))
278 goto end;
279 if (--ff->dev_lock_count == 0)
280 snd_ff_stream_lock_changed(ff);
281end:
282 spin_unlock_irq(&ff->lock);
283}