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_stream_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 amdtp_stream_set_parameters(stream
, sampling_rate
,
77 pcm_channels
, midi_ports
);
79 /* establish connection via CMP */
80 err
= cmp_connection_establish(conn
,
81 amdtp_stream_get_max_payload(stream
));
85 /* start amdtp stream */
86 err
= amdtp_stream_start(stream
,
87 conn
->resources
.channel
,
90 stop_stream(efw
, stream
);
94 /* wait first callback */
95 if (!amdtp_stream_wait_callback(stream
, CALLBACK_TIMEOUT
)) {
96 stop_stream(efw
, stream
);
104 destroy_stream(struct snd_efw
*efw
, struct amdtp_stream
*stream
)
106 stop_stream(efw
, stream
);
108 amdtp_stream_destroy(stream
);
110 if (stream
== &efw
->tx_stream
)
111 cmp_connection_destroy(&efw
->out_conn
);
113 cmp_connection_destroy(&efw
->in_conn
);
117 get_sync_mode(struct snd_efw
*efw
, enum cip_flags
*sync_mode
)
119 enum snd_efw_clock_source clock_source
;
122 err
= snd_efw_command_get_clock_source(efw
, &clock_source
);
126 if (clock_source
== SND_EFW_CLOCK_SOURCE_SYTMATCH
)
129 *sync_mode
= CIP_SYNC_TO_DEVICE
;
134 check_connection_used_by_others(struct snd_efw
*efw
, struct amdtp_stream
*s
)
136 struct cmp_connection
*conn
;
140 if (s
== &efw
->tx_stream
)
141 conn
= &efw
->out_conn
;
143 conn
= &efw
->in_conn
;
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',
157 int snd_efw_stream_init_duplex(struct snd_efw
*efw
)
161 err
= init_stream(efw
, &efw
->tx_stream
);
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
;
171 * But Recent firmwares starts packets with non-zero dbc.
172 * Driver version 5.7.6 installs firmware version 5.7.3.
174 if (efw
->is_fireworks3
&&
175 (efw
->firmware_version
== 0x5070000 ||
176 efw
->firmware_version
== 0x5070300 ||
177 efw
->firmware_version
== 0x5080000))
178 efw
->tx_stream
.tx_first_dbc
= 0x02;
179 /* AudioFire9 always reports wrong dbs. */
181 efw
->tx_stream
.flags
|= CIP_WRONG_DBS
;
182 /* Firmware version 5.5 reports fixed interval for dbc. */
183 if (efw
->firmware_version
== 0x5050000)
184 efw
->tx_stream
.tx_dbc_interval
= 8;
186 err
= init_stream(efw
, &efw
->rx_stream
);
188 destroy_stream(efw
, &efw
->tx_stream
);
192 * Fireworks ignores MIDI messages in more than first 8 data
193 * blocks of an received AMDTP packet.
195 efw
->rx_stream
.rx_blocks_for_midi
= 8;
197 /* set IEC61883 compliant mode (actually not fully compliant...) */
198 err
= snd_efw_command_set_tx_mode(efw
, SND_EFW_TRANSPORT_MODE_IEC61883
);
200 destroy_stream(efw
, &efw
->tx_stream
);
201 destroy_stream(efw
, &efw
->rx_stream
);
207 int snd_efw_stream_start_duplex(struct snd_efw
*efw
, unsigned int rate
)
209 struct amdtp_stream
*master
, *slave
;
210 atomic_t
*slave_substreams
;
211 enum cip_flags sync_mode
;
212 unsigned int curr_rate
;
215 mutex_lock(&efw
->mutex
);
217 /* Need no substreams */
218 if ((atomic_read(&efw
->playback_substreams
) == 0) &&
219 (atomic_read(&efw
->capture_substreams
) == 0))
222 err
= get_sync_mode(efw
, &sync_mode
);
225 if (sync_mode
== CIP_SYNC_TO_DEVICE
) {
226 master
= &efw
->tx_stream
;
227 slave
= &efw
->rx_stream
;
228 slave_substreams
= &efw
->playback_substreams
;
230 master
= &efw
->rx_stream
;
231 slave
= &efw
->tx_stream
;
232 slave_substreams
= &efw
->capture_substreams
;
236 * Considering JACK/FFADO streaming:
237 * TODO: This can be removed hwdep functionality becomes popular.
239 err
= check_connection_used_by_others(efw
, master
);
243 /* packet queueing error */
244 if (amdtp_streaming_error(slave
))
245 stop_stream(efw
, slave
);
246 if (amdtp_streaming_error(master
))
247 stop_stream(efw
, master
);
249 /* stop streams if rate is different */
250 err
= snd_efw_command_get_sampling_rate(efw
, &curr_rate
);
255 if (rate
!= curr_rate
) {
256 stop_stream(efw
, slave
);
257 stop_stream(efw
, master
);
260 /* master should be always running */
261 if (!amdtp_stream_running(master
)) {
262 amdtp_stream_set_sync(sync_mode
, master
, slave
);
263 efw
->master
= master
;
265 err
= snd_efw_command_set_sampling_rate(efw
, rate
);
269 err
= start_stream(efw
, master
, rate
);
271 dev_err(&efw
->unit
->device
,
272 "fail to start AMDTP master stream:%d\n", err
);
277 /* start slave if needed */
278 if (atomic_read(slave_substreams
) > 0 && !amdtp_stream_running(slave
)) {
279 err
= start_stream(efw
, slave
, rate
);
281 dev_err(&efw
->unit
->device
,
282 "fail to start AMDTP slave stream:%d\n", err
);
283 stop_stream(efw
, master
);
287 mutex_unlock(&efw
->mutex
);
291 void snd_efw_stream_stop_duplex(struct snd_efw
*efw
)
293 struct amdtp_stream
*master
, *slave
;
294 atomic_t
*master_substreams
, *slave_substreams
;
296 if (efw
->master
== &efw
->rx_stream
) {
297 slave
= &efw
->tx_stream
;
298 master
= &efw
->rx_stream
;
299 slave_substreams
= &efw
->capture_substreams
;
300 master_substreams
= &efw
->playback_substreams
;
302 slave
= &efw
->rx_stream
;
303 master
= &efw
->tx_stream
;
304 slave_substreams
= &efw
->playback_substreams
;
305 master_substreams
= &efw
->capture_substreams
;
308 mutex_lock(&efw
->mutex
);
310 if (atomic_read(slave_substreams
) == 0) {
311 stop_stream(efw
, slave
);
313 if (atomic_read(master_substreams
) == 0)
314 stop_stream(efw
, master
);
317 mutex_unlock(&efw
->mutex
);
320 void snd_efw_stream_update_duplex(struct snd_efw
*efw
)
322 if ((cmp_connection_update(&efw
->out_conn
) < 0) ||
323 (cmp_connection_update(&efw
->in_conn
) < 0)) {
324 mutex_lock(&efw
->mutex
);
325 stop_stream(efw
, &efw
->rx_stream
);
326 stop_stream(efw
, &efw
->tx_stream
);
327 mutex_unlock(&efw
->mutex
);
329 amdtp_stream_update(&efw
->rx_stream
);
330 amdtp_stream_update(&efw
->tx_stream
);
334 void snd_efw_stream_destroy_duplex(struct snd_efw
*efw
)
336 mutex_lock(&efw
->mutex
);
338 destroy_stream(efw
, &efw
->rx_stream
);
339 destroy_stream(efw
, &efw
->tx_stream
);
341 mutex_unlock(&efw
->mutex
);
344 void snd_efw_stream_lock_changed(struct snd_efw
*efw
)
346 efw
->dev_lock_changed
= true;
347 wake_up(&efw
->hwdep_wait
);
350 int snd_efw_stream_lock_try(struct snd_efw
*efw
)
354 spin_lock_irq(&efw
->lock
);
356 /* user land lock this */
357 if (efw
->dev_lock_count
< 0) {
362 /* this is the first time */
363 if (efw
->dev_lock_count
++ == 0)
364 snd_efw_stream_lock_changed(efw
);
367 spin_unlock_irq(&efw
->lock
);
371 void snd_efw_stream_lock_release(struct snd_efw
*efw
)
373 spin_lock_irq(&efw
->lock
);
375 if (WARN_ON(efw
->dev_lock_count
<= 0))
377 if (--efw
->dev_lock_count
== 0)
378 snd_efw_stream_lock_changed(efw
);
380 spin_unlock_irq(&efw
->lock
);