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