blob: ec62aa65cfae696bd39c0248041fe5bb7137d852 [file] [log] [blame]
Takashi Sakamoto315fd412014-04-25 22:45:02 +09001/*
2 * fireworks_stream.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2013-2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8#include "./fireworks.h"
9
10#define CALLBACK_TIMEOUT 100
11
12static unsigned int freq_table[] = {
13 /* multiplier mode 0 */
14 [0] = 32000,
15 [1] = 44100,
16 [2] = 48000,
17 /* multiplier mode 1 */
18 [3] = 88200,
19 [4] = 96000,
20 /* multiplier mode 2 */
21 [5] = 176400,
22 [6] = 192000,
23};
24
25static inline unsigned int
26get_multiplier_mode_with_index(unsigned int index)
27{
28 return ((int)index - 1) / 2;
29}
30
31int snd_efw_get_multiplier_mode(unsigned int sampling_rate, unsigned int *mode)
32{
33 unsigned int i;
34
35 for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
36 if (freq_table[i] == sampling_rate) {
37 *mode = get_multiplier_mode_with_index(i);
38 return 0;
39 }
40 }
41
42 return -EINVAL;
43}
44
45static int
46init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
47{
48 struct cmp_connection *conn;
49 enum cmp_direction c_dir;
50 enum amdtp_stream_direction s_dir;
51 int err;
52
53 if (stream == &efw->tx_stream) {
54 conn = &efw->out_conn;
55 c_dir = CMP_OUTPUT;
56 s_dir = AMDTP_IN_STREAM;
57 } else {
58 conn = &efw->in_conn;
59 c_dir = CMP_INPUT;
60 s_dir = AMDTP_OUT_STREAM;
61 }
62
63 err = cmp_connection_init(conn, efw->unit, c_dir, 0);
64 if (err < 0)
65 goto end;
66
67 err = amdtp_stream_init(stream, efw->unit, s_dir, CIP_BLOCKING);
68 if (err < 0) {
69 amdtp_stream_destroy(stream);
70 cmp_connection_destroy(conn);
71 }
72end:
73 return err;
74}
75
76static void
77stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
78{
79 amdtp_stream_pcm_abort(stream);
80 amdtp_stream_stop(stream);
81
82 if (stream == &efw->tx_stream)
83 cmp_connection_break(&efw->out_conn);
84 else
85 cmp_connection_break(&efw->in_conn);
86}
87
88static int
89start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
90 unsigned int sampling_rate)
91{
92 struct cmp_connection *conn;
93 unsigned int mode, pcm_channels, midi_ports;
94 int err;
95
96 err = snd_efw_get_multiplier_mode(sampling_rate, &mode);
97 if (err < 0)
98 goto end;
99 if (stream == &efw->tx_stream) {
100 conn = &efw->out_conn;
101 pcm_channels = efw->pcm_capture_channels[mode];
102 midi_ports = efw->midi_out_ports;
103 } else {
104 conn = &efw->in_conn;
105 pcm_channels = efw->pcm_playback_channels[mode];
106 midi_ports = efw->midi_in_ports;
107 }
108
109 amdtp_stream_set_parameters(stream, sampling_rate,
110 pcm_channels, midi_ports);
111
112 /* establish connection via CMP */
113 err = cmp_connection_establish(conn,
114 amdtp_stream_get_max_payload(stream));
115 if (err < 0)
116 goto end;
117
118 /* start amdtp stream */
119 err = amdtp_stream_start(stream,
120 conn->resources.channel,
121 conn->speed);
122 if (err < 0) {
123 stop_stream(efw, stream);
124 goto end;
125 }
126
127 /* wait first callback */
128 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
129 stop_stream(efw, stream);
130 err = -ETIMEDOUT;
131 }
132end:
133 return err;
134}
135
136static void
137destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
138{
139 stop_stream(efw, stream);
140
141 amdtp_stream_destroy(stream);
142
143 if (stream == &efw->tx_stream)
144 cmp_connection_destroy(&efw->out_conn);
145 else
146 cmp_connection_destroy(&efw->in_conn);
147}
148
149static int
150get_sync_mode(struct snd_efw *efw, enum cip_flags *sync_mode)
151{
152 enum snd_efw_clock_source clock_source;
153 int err;
154
155 err = snd_efw_command_get_clock_source(efw, &clock_source);
156 if (err < 0)
157 return err;
158
159 if (clock_source == SND_EFW_CLOCK_SOURCE_SYTMATCH)
160 return -ENOSYS;
161
162 *sync_mode = CIP_SYNC_TO_DEVICE;
163 return 0;
164}
165
166static int
167check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
168{
169 struct cmp_connection *conn;
170 bool used;
171 int err;
172
173 if (s == &efw->tx_stream)
174 conn = &efw->out_conn;
175 else
176 conn = &efw->in_conn;
177
178 err = cmp_connection_check_used(conn, &used);
179 if ((err >= 0) && used && !amdtp_stream_running(s)) {
180 dev_err(&efw->unit->device,
181 "Connection established by others: %cPCR[%d]\n",
182 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
183 conn->pcr_index);
184 err = -EBUSY;
185 }
186
187 return err;
188}
189
190int snd_efw_stream_init_duplex(struct snd_efw *efw)
191{
192 int err;
193
194 err = init_stream(efw, &efw->tx_stream);
195 if (err < 0)
196 goto end;
197
198 err = init_stream(efw, &efw->rx_stream);
199 if (err < 0) {
200 destroy_stream(efw, &efw->tx_stream);
201 goto end;
202 }
203
204 /* set IEC61883 compliant mode (actually not fully compliant...) */
205 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
206 if (err < 0) {
207 destroy_stream(efw, &efw->tx_stream);
208 destroy_stream(efw, &efw->rx_stream);
209 }
210end:
211 return err;
212}
213
214int snd_efw_stream_start_duplex(struct snd_efw *efw, int rate)
215{
216 struct amdtp_stream *master, *slave;
217 atomic_t *slave_substreams;
218 enum cip_flags sync_mode;
219 unsigned int curr_rate;
220 int err = 0;
221
222 mutex_lock(&efw->mutex);
223
224 /* Need no substreams */
225 if ((atomic_read(&efw->playback_substreams) == 0) &&
226 (atomic_read(&efw->capture_substreams) == 0))
227 goto end;
228
229 err = get_sync_mode(efw, &sync_mode);
230 if (err < 0)
231 goto end;
232 if (sync_mode == CIP_SYNC_TO_DEVICE) {
233 master = &efw->tx_stream;
234 slave = &efw->rx_stream;
235 slave_substreams = &efw->playback_substreams;
236 } else {
237 master = &efw->rx_stream;
238 slave = &efw->tx_stream;
239 slave_substreams = &efw->capture_substreams;
240 }
241
242 /*
243 * Considering JACK/FFADO streaming:
244 * TODO: This can be removed hwdep functionality becomes popular.
245 */
246 err = check_connection_used_by_others(efw, master);
247 if (err < 0)
248 goto end;
249
250 /* packet queueing error */
251 if (amdtp_streaming_error(slave))
252 stop_stream(efw, slave);
253 if (amdtp_streaming_error(master))
254 stop_stream(efw, master);
255
256 /* stop streams if rate is different */
257 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
258 if (err < 0)
259 goto end;
260 if (rate == 0)
261 rate = curr_rate;
262 if (rate != curr_rate) {
263 stop_stream(efw, slave);
264 stop_stream(efw, master);
265 }
266
267 /* master should be always running */
268 if (!amdtp_stream_running(master)) {
269 amdtp_stream_set_sync(sync_mode, master, slave);
270 efw->master = master;
271
272 err = snd_efw_command_set_sampling_rate(efw, rate);
273 if (err < 0)
274 goto end;
275
276 err = start_stream(efw, master, rate);
277 if (err < 0) {
278 dev_err(&efw->unit->device,
279 "fail to start AMDTP master stream:%d\n", err);
280 goto end;
281 }
282 }
283
284 /* start slave if needed */
285 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
286 err = start_stream(efw, slave, rate);
287 if (err < 0) {
288 dev_err(&efw->unit->device,
289 "fail to start AMDTP slave stream:%d\n", err);
290 stop_stream(efw, master);
291 }
292 }
293end:
294 mutex_unlock(&efw->mutex);
295 return err;
296}
297
298void snd_efw_stream_stop_duplex(struct snd_efw *efw)
299{
300 struct amdtp_stream *master, *slave;
301 atomic_t *master_substreams, *slave_substreams;
302
303 mutex_lock(&efw->mutex);
304
305 if (efw->master == &efw->rx_stream) {
306 slave = &efw->tx_stream;
307 master = &efw->rx_stream;
308 slave_substreams = &efw->capture_substreams;
309 master_substreams = &efw->playback_substreams;
310 } else {
311 slave = &efw->rx_stream;
312 master = &efw->tx_stream;
313 slave_substreams = &efw->playback_substreams;
314 master_substreams = &efw->capture_substreams;
315 }
316
317 if (atomic_read(slave_substreams) == 0) {
318 stop_stream(efw, slave);
319
320 if (atomic_read(master_substreams) == 0)
321 stop_stream(efw, master);
322 }
323
324 mutex_unlock(&efw->mutex);
325}
326
327void snd_efw_stream_update_duplex(struct snd_efw *efw)
328{
329 if ((cmp_connection_update(&efw->out_conn) < 0) ||
330 (cmp_connection_update(&efw->in_conn) < 0)) {
331 mutex_lock(&efw->mutex);
332 stop_stream(efw, &efw->rx_stream);
333 stop_stream(efw, &efw->tx_stream);
334 mutex_unlock(&efw->mutex);
335 } else {
336 amdtp_stream_update(&efw->rx_stream);
337 amdtp_stream_update(&efw->tx_stream);
338 }
339}
340
341void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
342{
343 mutex_lock(&efw->mutex);
344
345 destroy_stream(efw, &efw->rx_stream);
346 destroy_stream(efw, &efw->tx_stream);
347
348 mutex_unlock(&efw->mutex);
349}