ALSA: fireworks/firewire-lib: Add a quirk to reset data block counter at bus reset
[firefly-linux-kernel-4.4.55.git] / sound / firewire / fireworks / fireworks_stream.c
1 /*
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
12 static 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
25 static inline unsigned int
26 get_multiplier_mode_with_index(unsigned int index)
27 {
28         return ((int)index - 1) / 2;
29 }
30
31 int 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
45 static int
46 init_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         }
72 end:
73         return err;
74 }
75
76 static void
77 stop_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
88 static int
89 start_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         }
132 end:
133         return err;
134 }
135
136 static void
137 destroy_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
149 static int
150 get_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
166 static int
167 check_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
190 int 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         /* Fireworks transmits NODATA packets with TAG0. */
198         efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
199         /* Fireworks has its own meaning for dbc. */
200         efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
201         /* Fireworks reset dbc at bus reset. */
202         efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
203         /* AudioFire9 always reports wrong dbs. */
204         if (efw->is_af9)
205                 efw->tx_stream.flags |= CIP_WRONG_DBS;
206         /* Firmware version 5.5 reports fixed interval for dbc. */
207         if (efw->firmware_version == 0x5050000)
208                 efw->tx_stream.tx_dbc_interval = 8;
209
210         err = init_stream(efw, &efw->rx_stream);
211         if (err < 0) {
212                 destroy_stream(efw, &efw->tx_stream);
213                 goto end;
214         }
215
216         /* set IEC61883 compliant mode (actually not fully compliant...) */
217         err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
218         if (err < 0) {
219                 destroy_stream(efw, &efw->tx_stream);
220                 destroy_stream(efw, &efw->rx_stream);
221         }
222 end:
223         return err;
224 }
225
226 int snd_efw_stream_start_duplex(struct snd_efw *efw, int rate)
227 {
228         struct amdtp_stream *master, *slave;
229         atomic_t *slave_substreams;
230         enum cip_flags sync_mode;
231         unsigned int curr_rate;
232         int err = 0;
233
234         mutex_lock(&efw->mutex);
235
236         /* Need no substreams */
237         if ((atomic_read(&efw->playback_substreams) == 0) &&
238             (atomic_read(&efw->capture_substreams)  == 0))
239                 goto end;
240
241         err = get_sync_mode(efw, &sync_mode);
242         if (err < 0)
243                 goto end;
244         if (sync_mode == CIP_SYNC_TO_DEVICE) {
245                 master = &efw->tx_stream;
246                 slave  = &efw->rx_stream;
247                 slave_substreams  = &efw->playback_substreams;
248         } else {
249                 master = &efw->rx_stream;
250                 slave  = &efw->tx_stream;
251                 slave_substreams = &efw->capture_substreams;
252         }
253
254         /*
255          * Considering JACK/FFADO streaming:
256          * TODO: This can be removed hwdep functionality becomes popular.
257          */
258         err = check_connection_used_by_others(efw, master);
259         if (err < 0)
260                 goto end;
261
262         /* packet queueing error */
263         if (amdtp_streaming_error(slave))
264                 stop_stream(efw, slave);
265         if (amdtp_streaming_error(master))
266                 stop_stream(efw, master);
267
268         /* stop streams if rate is different */
269         err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
270         if (err < 0)
271                 goto end;
272         if (rate == 0)
273                 rate = curr_rate;
274         if (rate != curr_rate) {
275                 stop_stream(efw, slave);
276                 stop_stream(efw, master);
277         }
278
279         /* master should be always running */
280         if (!amdtp_stream_running(master)) {
281                 amdtp_stream_set_sync(sync_mode, master, slave);
282                 efw->master = master;
283
284                 err = snd_efw_command_set_sampling_rate(efw, rate);
285                 if (err < 0)
286                         goto end;
287
288                 err = start_stream(efw, master, rate);
289                 if (err < 0) {
290                         dev_err(&efw->unit->device,
291                                 "fail to start AMDTP master stream:%d\n", err);
292                         goto end;
293                 }
294         }
295
296         /* start slave if needed */
297         if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
298                 err = start_stream(efw, slave, rate);
299                 if (err < 0) {
300                         dev_err(&efw->unit->device,
301                                 "fail to start AMDTP slave stream:%d\n", err);
302                         stop_stream(efw, master);
303                 }
304         }
305 end:
306         mutex_unlock(&efw->mutex);
307         return err;
308 }
309
310 void snd_efw_stream_stop_duplex(struct snd_efw *efw)
311 {
312         struct amdtp_stream *master, *slave;
313         atomic_t *master_substreams, *slave_substreams;
314
315         mutex_lock(&efw->mutex);
316
317         if (efw->master == &efw->rx_stream) {
318                 slave  = &efw->tx_stream;
319                 master = &efw->rx_stream;
320                 slave_substreams  = &efw->capture_substreams;
321                 master_substreams = &efw->playback_substreams;
322         } else {
323                 slave  = &efw->rx_stream;
324                 master = &efw->tx_stream;
325                 slave_substreams  = &efw->playback_substreams;
326                 master_substreams = &efw->capture_substreams;
327         }
328
329         if (atomic_read(slave_substreams) == 0) {
330                 stop_stream(efw, slave);
331
332                 if (atomic_read(master_substreams) == 0)
333                         stop_stream(efw, master);
334         }
335
336         mutex_unlock(&efw->mutex);
337 }
338
339 void snd_efw_stream_update_duplex(struct snd_efw *efw)
340 {
341         if ((cmp_connection_update(&efw->out_conn) < 0) ||
342             (cmp_connection_update(&efw->in_conn) < 0)) {
343                 mutex_lock(&efw->mutex);
344                 stop_stream(efw, &efw->rx_stream);
345                 stop_stream(efw, &efw->tx_stream);
346                 mutex_unlock(&efw->mutex);
347         } else {
348                 amdtp_stream_update(&efw->rx_stream);
349                 amdtp_stream_update(&efw->tx_stream);
350         }
351 }
352
353 void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
354 {
355         mutex_lock(&efw->mutex);
356
357         destroy_stream(efw, &efw->rx_stream);
358         destroy_stream(efw, &efw->tx_stream);
359
360         mutex_unlock(&efw->mutex);
361 }