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.
8 #include "./fireworks.h"
10 #define CALLBACK_TIMEOUT 100
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
;
20 if (stream
== &efw
->tx_stream
) {
21 conn
= &efw
->out_conn
;
23 s_dir
= AMDTP_IN_STREAM
;
27 s_dir
= AMDTP_OUT_STREAM
;
30 err
= cmp_connection_init(conn
, efw
->unit
, c_dir
, 0);
34 err
= amdtp_am824_init(stream
, efw
->unit
, s_dir
, CIP_BLOCKING
);
36 amdtp_stream_destroy(stream
);
37 cmp_connection_destroy(conn
);
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
);
52 cmp_connection_break(&efw
->in_conn
);
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
;
63 err
= snd_efw_get_multiplier_mode(sampling_rate
, &mode
);
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
;
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);
81 /* establish connection via CMP */
82 err
= cmp_connection_establish(conn
,
83 amdtp_stream_get_max_payload(stream
));
87 /* start amdtp stream */
88 err
= amdtp_stream_start(stream
,
89 conn
->resources
.channel
,
92 stop_stream(efw
, stream
);
96 /* wait first callback */
97 if (!amdtp_stream_wait_callback(stream
, CALLBACK_TIMEOUT
)) {
98 stop_stream(efw
, stream
);
106 * This function should be called before starting the stream or after stopping
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
;
117 conn
= &efw
->in_conn
;
119 amdtp_stream_destroy(stream
);
120 cmp_connection_destroy(&efw
->out_conn
);
124 get_sync_mode(struct snd_efw
*efw
, enum cip_flags
*sync_mode
)
126 enum snd_efw_clock_source clock_source
;
129 err
= snd_efw_command_get_clock_source(efw
, &clock_source
);
133 if (clock_source
== SND_EFW_CLOCK_SOURCE_SYTMATCH
)
136 *sync_mode
= CIP_SYNC_TO_DEVICE
;
141 check_connection_used_by_others(struct snd_efw
*efw
, struct amdtp_stream
*s
)
143 struct cmp_connection
*conn
;
147 if (s
== &efw
->tx_stream
)
148 conn
= &efw
->out_conn
;
150 conn
= &efw
->in_conn
;
152 err
= cmp_connection_check_used(conn
, &used
);
153 if ((err
>= 0) && used
&& !amdtp_stream_running(s
)) {
154 dev_err(&efw
->unit
->device
,
155 "Connection established by others: %cPCR[%d]\n",
156 (conn
->direction
== CMP_OUTPUT
) ? 'o' : 'i',
164 int snd_efw_stream_init_duplex(struct snd_efw
*efw
)
168 err
= init_stream(efw
, &efw
->tx_stream
);
171 /* Fireworks transmits NODATA packets with TAG0. */
172 efw
->tx_stream
.flags
|= CIP_EMPTY_WITH_TAG0
;
173 /* Fireworks has its own meaning for dbc. */
174 efw
->tx_stream
.flags
|= CIP_DBC_IS_END_EVENT
;
175 /* Fireworks reset dbc at bus reset. */
176 efw
->tx_stream
.flags
|= CIP_SKIP_DBC_ZERO_CHECK
;
178 * But Recent firmwares starts packets with non-zero dbc.
179 * Driver version 5.7.6 installs firmware version 5.7.3.
181 if (efw
->is_fireworks3
&&
182 (efw
->firmware_version
== 0x5070000 ||
183 efw
->firmware_version
== 0x5070300 ||
184 efw
->firmware_version
== 0x5080000))
185 efw
->tx_stream
.tx_first_dbc
= 0x02;
186 /* AudioFire9 always reports wrong dbs. */
188 efw
->tx_stream
.flags
|= CIP_WRONG_DBS
;
189 /* Firmware version 5.5 reports fixed interval for dbc. */
190 if (efw
->firmware_version
== 0x5050000)
191 efw
->tx_stream
.tx_dbc_interval
= 8;
193 err
= init_stream(efw
, &efw
->rx_stream
);
195 destroy_stream(efw
, &efw
->tx_stream
);
199 /* set IEC61883 compliant mode (actually not fully compliant...) */
200 err
= snd_efw_command_set_tx_mode(efw
, SND_EFW_TRANSPORT_MODE_IEC61883
);
202 destroy_stream(efw
, &efw
->tx_stream
);
203 destroy_stream(efw
, &efw
->rx_stream
);
209 int snd_efw_stream_start_duplex(struct snd_efw
*efw
, unsigned int rate
)
211 struct amdtp_stream
*master
, *slave
;
212 atomic_t
*slave_substreams
;
213 enum cip_flags sync_mode
;
214 unsigned int curr_rate
;
217 mutex_lock(&efw
->mutex
);
219 /* Need no substreams */
220 if ((atomic_read(&efw
->playback_substreams
) == 0) &&
221 (atomic_read(&efw
->capture_substreams
) == 0))
224 err
= get_sync_mode(efw
, &sync_mode
);
227 if (sync_mode
== CIP_SYNC_TO_DEVICE
) {
228 master
= &efw
->tx_stream
;
229 slave
= &efw
->rx_stream
;
230 slave_substreams
= &efw
->playback_substreams
;
232 master
= &efw
->rx_stream
;
233 slave
= &efw
->tx_stream
;
234 slave_substreams
= &efw
->capture_substreams
;
238 * Considering JACK/FFADO streaming:
239 * TODO: This can be removed hwdep functionality becomes popular.
241 err
= check_connection_used_by_others(efw
, master
);
245 /* packet queueing error */
246 if (amdtp_streaming_error(slave
))
247 stop_stream(efw
, slave
);
248 if (amdtp_streaming_error(master
))
249 stop_stream(efw
, master
);
251 /* stop streams if rate is different */
252 err
= snd_efw_command_get_sampling_rate(efw
, &curr_rate
);
257 if (rate
!= curr_rate
) {
258 stop_stream(efw
, slave
);
259 stop_stream(efw
, master
);
262 /* master should be always running */
263 if (!amdtp_stream_running(master
)) {
264 amdtp_stream_set_sync(sync_mode
, master
, slave
);
265 efw
->master
= master
;
267 err
= snd_efw_command_set_sampling_rate(efw
, rate
);
271 err
= start_stream(efw
, master
, rate
);
273 dev_err(&efw
->unit
->device
,
274 "fail to start AMDTP master stream:%d\n", err
);
279 /* start slave if needed */
280 if (atomic_read(slave_substreams
) > 0 && !amdtp_stream_running(slave
)) {
281 err
= start_stream(efw
, slave
, rate
);
283 dev_err(&efw
->unit
->device
,
284 "fail to start AMDTP slave stream:%d\n", err
);
285 stop_stream(efw
, master
);
289 mutex_unlock(&efw
->mutex
);
293 void snd_efw_stream_stop_duplex(struct snd_efw
*efw
)
295 struct amdtp_stream
*master
, *slave
;
296 atomic_t
*master_substreams
, *slave_substreams
;
298 if (efw
->master
== &efw
->rx_stream
) {
299 slave
= &efw
->tx_stream
;
300 master
= &efw
->rx_stream
;
301 slave_substreams
= &efw
->capture_substreams
;
302 master_substreams
= &efw
->playback_substreams
;
304 slave
= &efw
->rx_stream
;
305 master
= &efw
->tx_stream
;
306 slave_substreams
= &efw
->playback_substreams
;
307 master_substreams
= &efw
->capture_substreams
;
310 mutex_lock(&efw
->mutex
);
312 if (atomic_read(slave_substreams
) == 0) {
313 stop_stream(efw
, slave
);
315 if (atomic_read(master_substreams
) == 0)
316 stop_stream(efw
, master
);
319 mutex_unlock(&efw
->mutex
);
322 void snd_efw_stream_update_duplex(struct snd_efw
*efw
)
324 if ((cmp_connection_update(&efw
->out_conn
) < 0) ||
325 (cmp_connection_update(&efw
->in_conn
) < 0)) {
326 mutex_lock(&efw
->mutex
);
327 stop_stream(efw
, &efw
->rx_stream
);
328 stop_stream(efw
, &efw
->tx_stream
);
329 mutex_unlock(&efw
->mutex
);
331 amdtp_stream_update(&efw
->rx_stream
);
332 amdtp_stream_update(&efw
->tx_stream
);
336 void snd_efw_stream_destroy_duplex(struct snd_efw
*efw
)
338 destroy_stream(efw
, &efw
->rx_stream
);
339 destroy_stream(efw
, &efw
->tx_stream
);
342 void snd_efw_stream_lock_changed(struct snd_efw
*efw
)
344 efw
->dev_lock_changed
= true;
345 wake_up(&efw
->hwdep_wait
);
348 int snd_efw_stream_lock_try(struct snd_efw
*efw
)
352 spin_lock_irq(&efw
->lock
);
354 /* user land lock this */
355 if (efw
->dev_lock_count
< 0) {
360 /* this is the first time */
361 if (efw
->dev_lock_count
++ == 0)
362 snd_efw_stream_lock_changed(efw
);
365 spin_unlock_irq(&efw
->lock
);
369 void snd_efw_stream_lock_release(struct snd_efw
*efw
)
371 spin_lock_irq(&efw
->lock
);
373 if (WARN_ON(efw
->dev_lock_count
<= 0))
375 if (--efw
->dev_lock_count
== 0)
376 snd_efw_stream_lock_changed(efw
);
378 spin_unlock_irq(&efw
->lock
);