MAINTAINERS: Add phy-miphy28lp.c and phy-miphy365x.c to ARCH/STI architecture
[deliverable/linux.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 int
13 init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
14 {
15 struct cmp_connection *conn;
16 enum cmp_direction c_dir;
17 enum amdtp_stream_direction s_dir;
18 int err;
19
20 if (stream == &efw->tx_stream) {
21 conn = &efw->out_conn;
22 c_dir = CMP_OUTPUT;
23 s_dir = AMDTP_IN_STREAM;
24 } else {
25 conn = &efw->in_conn;
26 c_dir = CMP_INPUT;
27 s_dir = AMDTP_OUT_STREAM;
28 }
29
30 err = cmp_connection_init(conn, efw->unit, c_dir, 0);
31 if (err < 0)
32 goto end;
33
34 err = amdtp_stream_init(stream, efw->unit, s_dir, CIP_BLOCKING);
35 if (err < 0) {
36 amdtp_stream_destroy(stream);
37 cmp_connection_destroy(conn);
38 }
39 end:
40 return err;
41 }
42
43 static void
44 stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
45 {
46 amdtp_stream_pcm_abort(stream);
47 amdtp_stream_stop(stream);
48
49 if (stream == &efw->tx_stream)
50 cmp_connection_break(&efw->out_conn);
51 else
52 cmp_connection_break(&efw->in_conn);
53 }
54
55 static int
56 start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
57 unsigned int sampling_rate)
58 {
59 struct cmp_connection *conn;
60 unsigned int mode, pcm_channels, midi_ports;
61 int err;
62
63 err = snd_efw_get_multiplier_mode(sampling_rate, &mode);
64 if (err < 0)
65 goto end;
66 if (stream == &efw->tx_stream) {
67 conn = &efw->out_conn;
68 pcm_channels = efw->pcm_capture_channels[mode];
69 midi_ports = efw->midi_out_ports;
70 } else {
71 conn = &efw->in_conn;
72 pcm_channels = efw->pcm_playback_channels[mode];
73 midi_ports = efw->midi_in_ports;
74 }
75
76 amdtp_stream_set_parameters(stream, sampling_rate,
77 pcm_channels, midi_ports);
78
79 /* establish connection via CMP */
80 err = cmp_connection_establish(conn,
81 amdtp_stream_get_max_payload(stream));
82 if (err < 0)
83 goto end;
84
85 /* start amdtp stream */
86 err = amdtp_stream_start(stream,
87 conn->resources.channel,
88 conn->speed);
89 if (err < 0) {
90 stop_stream(efw, stream);
91 goto end;
92 }
93
94 /* wait first callback */
95 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
96 stop_stream(efw, stream);
97 err = -ETIMEDOUT;
98 }
99 end:
100 return err;
101 }
102
103 static void
104 destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
105 {
106 stop_stream(efw, stream);
107
108 amdtp_stream_destroy(stream);
109
110 if (stream == &efw->tx_stream)
111 cmp_connection_destroy(&efw->out_conn);
112 else
113 cmp_connection_destroy(&efw->in_conn);
114 }
115
116 static int
117 get_sync_mode(struct snd_efw *efw, enum cip_flags *sync_mode)
118 {
119 enum snd_efw_clock_source clock_source;
120 int err;
121
122 err = snd_efw_command_get_clock_source(efw, &clock_source);
123 if (err < 0)
124 return err;
125
126 if (clock_source == SND_EFW_CLOCK_SOURCE_SYTMATCH)
127 return -ENOSYS;
128
129 *sync_mode = CIP_SYNC_TO_DEVICE;
130 return 0;
131 }
132
133 static int
134 check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
135 {
136 struct cmp_connection *conn;
137 bool used;
138 int err;
139
140 if (s == &efw->tx_stream)
141 conn = &efw->out_conn;
142 else
143 conn = &efw->in_conn;
144
145 err = cmp_connection_check_used(conn, &used);
146 if ((err >= 0) && used && !amdtp_stream_running(s)) {
147 dev_err(&efw->unit->device,
148 "Connection established by others: %cPCR[%d]\n",
149 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
150 conn->pcr_index);
151 err = -EBUSY;
152 }
153
154 return err;
155 }
156
157 int snd_efw_stream_init_duplex(struct snd_efw *efw)
158 {
159 int err;
160
161 err = init_stream(efw, &efw->tx_stream);
162 if (err < 0)
163 goto end;
164 /* Fireworks transmits NODATA packets with TAG0. */
165 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
166 /* Fireworks has its own meaning for dbc. */
167 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
168 /* Fireworks reset dbc at bus reset. */
169 efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
170 /* AudioFire9 always reports wrong dbs. */
171 if (efw->is_af9)
172 efw->tx_stream.flags |= CIP_WRONG_DBS;
173 /* Firmware version 5.5 reports fixed interval for dbc. */
174 if (efw->firmware_version == 0x5050000)
175 efw->tx_stream.tx_dbc_interval = 8;
176
177 err = init_stream(efw, &efw->rx_stream);
178 if (err < 0) {
179 destroy_stream(efw, &efw->tx_stream);
180 goto end;
181 }
182
183 /* set IEC61883 compliant mode (actually not fully compliant...) */
184 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
185 if (err < 0) {
186 destroy_stream(efw, &efw->tx_stream);
187 destroy_stream(efw, &efw->rx_stream);
188 }
189 end:
190 return err;
191 }
192
193 int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
194 {
195 struct amdtp_stream *master, *slave;
196 atomic_t *slave_substreams;
197 enum cip_flags sync_mode;
198 unsigned int curr_rate;
199 int err = 0;
200
201 mutex_lock(&efw->mutex);
202
203 /* Need no substreams */
204 if ((atomic_read(&efw->playback_substreams) == 0) &&
205 (atomic_read(&efw->capture_substreams) == 0))
206 goto end;
207
208 err = get_sync_mode(efw, &sync_mode);
209 if (err < 0)
210 goto end;
211 if (sync_mode == CIP_SYNC_TO_DEVICE) {
212 master = &efw->tx_stream;
213 slave = &efw->rx_stream;
214 slave_substreams = &efw->playback_substreams;
215 } else {
216 master = &efw->rx_stream;
217 slave = &efw->tx_stream;
218 slave_substreams = &efw->capture_substreams;
219 }
220
221 /*
222 * Considering JACK/FFADO streaming:
223 * TODO: This can be removed hwdep functionality becomes popular.
224 */
225 err = check_connection_used_by_others(efw, master);
226 if (err < 0)
227 goto end;
228
229 /* packet queueing error */
230 if (amdtp_streaming_error(slave))
231 stop_stream(efw, slave);
232 if (amdtp_streaming_error(master))
233 stop_stream(efw, master);
234
235 /* stop streams if rate is different */
236 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
237 if (err < 0)
238 goto end;
239 if (rate == 0)
240 rate = curr_rate;
241 if (rate != curr_rate) {
242 stop_stream(efw, slave);
243 stop_stream(efw, master);
244 }
245
246 /* master should be always running */
247 if (!amdtp_stream_running(master)) {
248 amdtp_stream_set_sync(sync_mode, master, slave);
249 efw->master = master;
250
251 err = snd_efw_command_set_sampling_rate(efw, rate);
252 if (err < 0)
253 goto end;
254
255 err = start_stream(efw, master, rate);
256 if (err < 0) {
257 dev_err(&efw->unit->device,
258 "fail to start AMDTP master stream:%d\n", err);
259 goto end;
260 }
261 }
262
263 /* start slave if needed */
264 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
265 err = start_stream(efw, slave, rate);
266 if (err < 0) {
267 dev_err(&efw->unit->device,
268 "fail to start AMDTP slave stream:%d\n", err);
269 stop_stream(efw, master);
270 }
271 }
272 end:
273 mutex_unlock(&efw->mutex);
274 return err;
275 }
276
277 void snd_efw_stream_stop_duplex(struct snd_efw *efw)
278 {
279 struct amdtp_stream *master, *slave;
280 atomic_t *master_substreams, *slave_substreams;
281
282 if (efw->master == &efw->rx_stream) {
283 slave = &efw->tx_stream;
284 master = &efw->rx_stream;
285 slave_substreams = &efw->capture_substreams;
286 master_substreams = &efw->playback_substreams;
287 } else {
288 slave = &efw->rx_stream;
289 master = &efw->tx_stream;
290 slave_substreams = &efw->playback_substreams;
291 master_substreams = &efw->capture_substreams;
292 }
293
294 mutex_lock(&efw->mutex);
295
296 if (atomic_read(slave_substreams) == 0) {
297 stop_stream(efw, slave);
298
299 if (atomic_read(master_substreams) == 0)
300 stop_stream(efw, master);
301 }
302
303 mutex_unlock(&efw->mutex);
304 }
305
306 void snd_efw_stream_update_duplex(struct snd_efw *efw)
307 {
308 if ((cmp_connection_update(&efw->out_conn) < 0) ||
309 (cmp_connection_update(&efw->in_conn) < 0)) {
310 mutex_lock(&efw->mutex);
311 stop_stream(efw, &efw->rx_stream);
312 stop_stream(efw, &efw->tx_stream);
313 mutex_unlock(&efw->mutex);
314 } else {
315 amdtp_stream_update(&efw->rx_stream);
316 amdtp_stream_update(&efw->tx_stream);
317 }
318 }
319
320 void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
321 {
322 mutex_lock(&efw->mutex);
323
324 destroy_stream(efw, &efw->rx_stream);
325 destroy_stream(efw, &efw->tx_stream);
326
327 mutex_unlock(&efw->mutex);
328 }
329
330 void snd_efw_stream_lock_changed(struct snd_efw *efw)
331 {
332 efw->dev_lock_changed = true;
333 wake_up(&efw->hwdep_wait);
334 }
335
336 int snd_efw_stream_lock_try(struct snd_efw *efw)
337 {
338 int err;
339
340 spin_lock_irq(&efw->lock);
341
342 /* user land lock this */
343 if (efw->dev_lock_count < 0) {
344 err = -EBUSY;
345 goto end;
346 }
347
348 /* this is the first time */
349 if (efw->dev_lock_count++ == 0)
350 snd_efw_stream_lock_changed(efw);
351 err = 0;
352 end:
353 spin_unlock_irq(&efw->lock);
354 return err;
355 }
356
357 void snd_efw_stream_lock_release(struct snd_efw *efw)
358 {
359 spin_lock_irq(&efw->lock);
360
361 if (WARN_ON(efw->dev_lock_count <= 0))
362 goto end;
363 if (--efw->dev_lock_count == 0)
364 snd_efw_stream_lock_changed(efw);
365 end:
366 spin_unlock_irq(&efw->lock);
367 }
This page took 0.077368 seconds and 5 git commands to generate.