Linux 4.18.10
[linux/fpc-iii.git] / sound / firewire / fireworks / fireworks_stream.c
blob827161bc269cfa1414972692bcdb45d0a42ad9ed
1 /*
2 * fireworks_stream.c - a part of driver for Fireworks based devices
4 * Copyright (c) 2013-2014 Takashi Sakamoto
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8 #include "./fireworks.h"
10 #define CALLBACK_TIMEOUT 100
12 static int
13 init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
15 struct cmp_connection *conn;
16 enum cmp_direction c_dir;
17 enum amdtp_stream_direction s_dir;
18 int err;
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;
30 err = cmp_connection_init(conn, efw->unit, c_dir, 0);
31 if (err < 0)
32 goto end;
34 err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING);
35 if (err < 0) {
36 amdtp_stream_destroy(stream);
37 cmp_connection_destroy(conn);
39 end:
40 return err;
43 static void
44 stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
46 amdtp_stream_pcm_abort(stream);
47 amdtp_stream_stop(stream);
49 if (stream == &efw->tx_stream)
50 cmp_connection_break(&efw->out_conn);
51 else
52 cmp_connection_break(&efw->in_conn);
55 static int
56 start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
57 unsigned int sampling_rate)
59 struct cmp_connection *conn;
60 unsigned int mode, pcm_channels, midi_ports;
61 int err;
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;
76 err = amdtp_am824_set_parameters(stream, sampling_rate,
77 pcm_channels, midi_ports, false);
78 if (err < 0)
79 goto end;
81 /* establish connection via CMP */
82 err = cmp_connection_establish(conn,
83 amdtp_stream_get_max_payload(stream));
84 if (err < 0)
85 goto end;
87 /* start amdtp stream */
88 err = amdtp_stream_start(stream,
89 conn->resources.channel,
90 conn->speed);
91 if (err < 0) {
92 stop_stream(efw, stream);
93 goto end;
96 /* wait first callback */
97 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
98 stop_stream(efw, stream);
99 err = -ETIMEDOUT;
101 end:
102 return err;
106 * This function should be called before starting the stream or after stopping
107 * the streams.
109 static void
110 destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
112 struct cmp_connection *conn;
114 if (stream == &efw->tx_stream)
115 conn = &efw->out_conn;
116 else
117 conn = &efw->in_conn;
119 amdtp_stream_destroy(stream);
120 cmp_connection_destroy(conn);
123 static int
124 check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
126 struct cmp_connection *conn;
127 bool used;
128 int err;
130 if (s == &efw->tx_stream)
131 conn = &efw->out_conn;
132 else
133 conn = &efw->in_conn;
135 err = cmp_connection_check_used(conn, &used);
136 if ((err >= 0) && used && !amdtp_stream_running(s)) {
137 dev_err(&efw->unit->device,
138 "Connection established by others: %cPCR[%d]\n",
139 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
140 conn->pcr_index);
141 err = -EBUSY;
144 return err;
147 int snd_efw_stream_init_duplex(struct snd_efw *efw)
149 int err;
151 err = init_stream(efw, &efw->tx_stream);
152 if (err < 0)
153 goto end;
154 /* Fireworks transmits NODATA packets with TAG0. */
155 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
156 /* Fireworks has its own meaning for dbc. */
157 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
158 /* Fireworks reset dbc at bus reset. */
159 efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
161 * But Recent firmwares starts packets with non-zero dbc.
162 * Driver version 5.7.6 installs firmware version 5.7.3.
164 if (efw->is_fireworks3 &&
165 (efw->firmware_version == 0x5070000 ||
166 efw->firmware_version == 0x5070300 ||
167 efw->firmware_version == 0x5080000))
168 efw->tx_stream.tx_first_dbc = 0x02;
169 /* AudioFire9 always reports wrong dbs. */
170 if (efw->is_af9)
171 efw->tx_stream.flags |= CIP_WRONG_DBS;
172 /* Firmware version 5.5 reports fixed interval for dbc. */
173 if (efw->firmware_version == 0x5050000)
174 efw->tx_stream.tx_dbc_interval = 8;
176 err = init_stream(efw, &efw->rx_stream);
177 if (err < 0) {
178 destroy_stream(efw, &efw->tx_stream);
179 goto end;
182 /* set IEC61883 compliant mode (actually not fully compliant...) */
183 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
184 if (err < 0) {
185 destroy_stream(efw, &efw->tx_stream);
186 destroy_stream(efw, &efw->rx_stream);
188 end:
189 return err;
192 int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
194 unsigned int curr_rate;
195 int err = 0;
197 /* Need no substreams */
198 if (efw->playback_substreams == 0 && efw->capture_substreams == 0)
199 goto end;
202 * Considering JACK/FFADO streaming:
203 * TODO: This can be removed hwdep functionality becomes popular.
205 err = check_connection_used_by_others(efw, &efw->rx_stream);
206 if (err < 0)
207 goto end;
209 /* packet queueing error */
210 if (amdtp_streaming_error(&efw->tx_stream))
211 stop_stream(efw, &efw->tx_stream);
212 if (amdtp_streaming_error(&efw->rx_stream))
213 stop_stream(efw, &efw->rx_stream);
215 /* stop streams if rate is different */
216 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
217 if (err < 0)
218 goto end;
219 if (rate == 0)
220 rate = curr_rate;
221 if (rate != curr_rate) {
222 stop_stream(efw, &efw->tx_stream);
223 stop_stream(efw, &efw->rx_stream);
226 /* master should be always running */
227 if (!amdtp_stream_running(&efw->rx_stream)) {
228 err = snd_efw_command_set_sampling_rate(efw, rate);
229 if (err < 0)
230 goto end;
232 err = start_stream(efw, &efw->rx_stream, rate);
233 if (err < 0) {
234 dev_err(&efw->unit->device,
235 "fail to start AMDTP master stream:%d\n", err);
236 goto end;
240 /* start slave if needed */
241 if (efw->capture_substreams > 0 &&
242 !amdtp_stream_running(&efw->tx_stream)) {
243 err = start_stream(efw, &efw->tx_stream, rate);
244 if (err < 0) {
245 dev_err(&efw->unit->device,
246 "fail to start AMDTP slave stream:%d\n", err);
247 stop_stream(efw, &efw->rx_stream);
250 end:
251 return err;
254 void snd_efw_stream_stop_duplex(struct snd_efw *efw)
256 if (efw->capture_substreams == 0) {
257 stop_stream(efw, &efw->tx_stream);
259 if (efw->playback_substreams == 0)
260 stop_stream(efw, &efw->rx_stream);
264 void snd_efw_stream_update_duplex(struct snd_efw *efw)
266 if (cmp_connection_update(&efw->out_conn) < 0 ||
267 cmp_connection_update(&efw->in_conn) < 0) {
268 stop_stream(efw, &efw->rx_stream);
269 stop_stream(efw, &efw->tx_stream);
270 } else {
271 amdtp_stream_update(&efw->rx_stream);
272 amdtp_stream_update(&efw->tx_stream);
276 void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
278 destroy_stream(efw, &efw->rx_stream);
279 destroy_stream(efw, &efw->tx_stream);
282 void snd_efw_stream_lock_changed(struct snd_efw *efw)
284 efw->dev_lock_changed = true;
285 wake_up(&efw->hwdep_wait);
288 int snd_efw_stream_lock_try(struct snd_efw *efw)
290 int err;
292 spin_lock_irq(&efw->lock);
294 /* user land lock this */
295 if (efw->dev_lock_count < 0) {
296 err = -EBUSY;
297 goto end;
300 /* this is the first time */
301 if (efw->dev_lock_count++ == 0)
302 snd_efw_stream_lock_changed(efw);
303 err = 0;
304 end:
305 spin_unlock_irq(&efw->lock);
306 return err;
309 void snd_efw_stream_lock_release(struct snd_efw *efw)
311 spin_lock_irq(&efw->lock);
313 if (WARN_ON(efw->dev_lock_count <= 0))
314 goto end;
315 if (--efw->dev_lock_count == 0)
316 snd_efw_stream_lock_changed(efw);
317 end:
318 spin_unlock_irq(&efw->lock);