blob: d687b047446bc8edb44d43fdd3b15dc804ab2b0f [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;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900197 /* Fireworks transmits NODATA packets with TAG0. */
198 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900199 /* Fireworks has its own meaning for dbc. */
200 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900201
202 err = init_stream(efw, &efw->rx_stream);
203 if (err < 0) {
204 destroy_stream(efw, &efw->tx_stream);
205 goto end;
206 }
207
208 /* set IEC61883 compliant mode (actually not fully compliant...) */
209 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
210 if (err < 0) {
211 destroy_stream(efw, &efw->tx_stream);
212 destroy_stream(efw, &efw->rx_stream);
213 }
214end:
215 return err;
216}
217
218int snd_efw_stream_start_duplex(struct snd_efw *efw, int rate)
219{
220 struct amdtp_stream *master, *slave;
221 atomic_t *slave_substreams;
222 enum cip_flags sync_mode;
223 unsigned int curr_rate;
224 int err = 0;
225
226 mutex_lock(&efw->mutex);
227
228 /* Need no substreams */
229 if ((atomic_read(&efw->playback_substreams) == 0) &&
230 (atomic_read(&efw->capture_substreams) == 0))
231 goto end;
232
233 err = get_sync_mode(efw, &sync_mode);
234 if (err < 0)
235 goto end;
236 if (sync_mode == CIP_SYNC_TO_DEVICE) {
237 master = &efw->tx_stream;
238 slave = &efw->rx_stream;
239 slave_substreams = &efw->playback_substreams;
240 } else {
241 master = &efw->rx_stream;
242 slave = &efw->tx_stream;
243 slave_substreams = &efw->capture_substreams;
244 }
245
246 /*
247 * Considering JACK/FFADO streaming:
248 * TODO: This can be removed hwdep functionality becomes popular.
249 */
250 err = check_connection_used_by_others(efw, master);
251 if (err < 0)
252 goto end;
253
254 /* packet queueing error */
255 if (amdtp_streaming_error(slave))
256 stop_stream(efw, slave);
257 if (amdtp_streaming_error(master))
258 stop_stream(efw, master);
259
260 /* stop streams if rate is different */
261 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
262 if (err < 0)
263 goto end;
264 if (rate == 0)
265 rate = curr_rate;
266 if (rate != curr_rate) {
267 stop_stream(efw, slave);
268 stop_stream(efw, master);
269 }
270
271 /* master should be always running */
272 if (!amdtp_stream_running(master)) {
273 amdtp_stream_set_sync(sync_mode, master, slave);
274 efw->master = master;
275
276 err = snd_efw_command_set_sampling_rate(efw, rate);
277 if (err < 0)
278 goto end;
279
280 err = start_stream(efw, master, rate);
281 if (err < 0) {
282 dev_err(&efw->unit->device,
283 "fail to start AMDTP master stream:%d\n", err);
284 goto end;
285 }
286 }
287
288 /* start slave if needed */
289 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
290 err = start_stream(efw, slave, rate);
291 if (err < 0) {
292 dev_err(&efw->unit->device,
293 "fail to start AMDTP slave stream:%d\n", err);
294 stop_stream(efw, master);
295 }
296 }
297end:
298 mutex_unlock(&efw->mutex);
299 return err;
300}
301
302void snd_efw_stream_stop_duplex(struct snd_efw *efw)
303{
304 struct amdtp_stream *master, *slave;
305 atomic_t *master_substreams, *slave_substreams;
306
307 mutex_lock(&efw->mutex);
308
309 if (efw->master == &efw->rx_stream) {
310 slave = &efw->tx_stream;
311 master = &efw->rx_stream;
312 slave_substreams = &efw->capture_substreams;
313 master_substreams = &efw->playback_substreams;
314 } else {
315 slave = &efw->rx_stream;
316 master = &efw->tx_stream;
317 slave_substreams = &efw->playback_substreams;
318 master_substreams = &efw->capture_substreams;
319 }
320
321 if (atomic_read(slave_substreams) == 0) {
322 stop_stream(efw, slave);
323
324 if (atomic_read(master_substreams) == 0)
325 stop_stream(efw, master);
326 }
327
328 mutex_unlock(&efw->mutex);
329}
330
331void snd_efw_stream_update_duplex(struct snd_efw *efw)
332{
333 if ((cmp_connection_update(&efw->out_conn) < 0) ||
334 (cmp_connection_update(&efw->in_conn) < 0)) {
335 mutex_lock(&efw->mutex);
336 stop_stream(efw, &efw->rx_stream);
337 stop_stream(efw, &efw->tx_stream);
338 mutex_unlock(&efw->mutex);
339 } else {
340 amdtp_stream_update(&efw->rx_stream);
341 amdtp_stream_update(&efw->tx_stream);
342 }
343}
344
345void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
346{
347 mutex_lock(&efw->mutex);
348
349 destroy_stream(efw, &efw->rx_stream);
350 destroy_stream(efw, &efw->tx_stream);
351
352 mutex_unlock(&efw->mutex);
353}