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 * This function should be called before starting the stream or after stopping
108 destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
110 struct cmp_connection *conn;
112 if (stream == &efw->tx_stream)
113 conn = &efw->out_conn;
115 conn = &efw->in_conn;
117 amdtp_stream_destroy(stream);
118 cmp_connection_destroy(&efw->out_conn);
122 get_sync_mode(struct snd_efw *efw, enum cip_flags *sync_mode)
124 enum snd_efw_clock_source clock_source;
127 err = snd_efw_command_get_clock_source(efw, &clock_source);
131 if (clock_source == SND_EFW_CLOCK_SOURCE_SYTMATCH)
134 *sync_mode = CIP_SYNC_TO_DEVICE;
139 check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
141 struct cmp_connection *conn;
145 if (s == &efw->tx_stream)
146 conn = &efw->out_conn;
148 conn = &efw->in_conn;
150 err = cmp_connection_check_used(conn, &used);
151 if ((err >= 0) && used && !amdtp_stream_running(s)) {
152 dev_err(&efw->unit->device,
153 "Connection established by others: %cPCR[%d]\n",
154 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
162 int snd_efw_stream_init_duplex(struct snd_efw *efw)
166 err = init_stream(efw, &efw->tx_stream);
169 /* Fireworks transmits NODATA packets with TAG0. */
170 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
171 /* Fireworks has its own meaning for dbc. */
172 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
173 /* Fireworks reset dbc at bus reset. */
174 efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
175 /* AudioFire2 starts packets with non-zero dbc. */
177 efw->tx_stream.flags |= CIP_SKIP_INIT_DBC_CHECK;
178 /* AudioFire9 always reports wrong dbs. */
180 efw->tx_stream.flags |= CIP_WRONG_DBS;
181 /* Firmware version 5.5 reports fixed interval for dbc. */
182 if (efw->firmware_version == 0x5050000)
183 efw->tx_stream.tx_dbc_interval = 8;
185 err = init_stream(efw, &efw->rx_stream);
187 destroy_stream(efw, &efw->tx_stream);
191 /* set IEC61883 compliant mode (actually not fully compliant...) */
192 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
194 destroy_stream(efw, &efw->tx_stream);
195 destroy_stream(efw, &efw->rx_stream);
201 int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
203 struct amdtp_stream *master, *slave;
204 atomic_t *slave_substreams;
205 enum cip_flags sync_mode;
206 unsigned int curr_rate;
209 mutex_lock(&efw->mutex);
211 /* Need no substreams */
212 if ((atomic_read(&efw->playback_substreams) == 0) &&
213 (atomic_read(&efw->capture_substreams) == 0))
216 err = get_sync_mode(efw, &sync_mode);
219 if (sync_mode == CIP_SYNC_TO_DEVICE) {
220 master = &efw->tx_stream;
221 slave = &efw->rx_stream;
222 slave_substreams = &efw->playback_substreams;
224 master = &efw->rx_stream;
225 slave = &efw->tx_stream;
226 slave_substreams = &efw->capture_substreams;
230 * Considering JACK/FFADO streaming:
231 * TODO: This can be removed hwdep functionality becomes popular.
233 err = check_connection_used_by_others(efw, master);
237 /* packet queueing error */
238 if (amdtp_streaming_error(slave))
239 stop_stream(efw, slave);
240 if (amdtp_streaming_error(master))
241 stop_stream(efw, master);
243 /* stop streams if rate is different */
244 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
249 if (rate != curr_rate) {
250 stop_stream(efw, slave);
251 stop_stream(efw, master);
254 /* master should be always running */
255 if (!amdtp_stream_running(master)) {
256 amdtp_stream_set_sync(sync_mode, master, slave);
257 efw->master = master;
259 err = snd_efw_command_set_sampling_rate(efw, rate);
263 err = start_stream(efw, master, rate);
265 dev_err(&efw->unit->device,
266 "fail to start AMDTP master stream:%d\n", err);
271 /* start slave if needed */
272 if (atomic_read(slave_substreams) > 0 && !amdtp_stream_running(slave)) {
273 err = start_stream(efw, slave, rate);
275 dev_err(&efw->unit->device,
276 "fail to start AMDTP slave stream:%d\n", err);
277 stop_stream(efw, master);
281 mutex_unlock(&efw->mutex);
285 void snd_efw_stream_stop_duplex(struct snd_efw *efw)
287 struct amdtp_stream *master, *slave;
288 atomic_t *master_substreams, *slave_substreams;
290 if (efw->master == &efw->rx_stream) {
291 slave = &efw->tx_stream;
292 master = &efw->rx_stream;
293 slave_substreams = &efw->capture_substreams;
294 master_substreams = &efw->playback_substreams;
296 slave = &efw->rx_stream;
297 master = &efw->tx_stream;
298 slave_substreams = &efw->playback_substreams;
299 master_substreams = &efw->capture_substreams;
302 mutex_lock(&efw->mutex);
304 if (atomic_read(slave_substreams) == 0) {
305 stop_stream(efw, slave);
307 if (atomic_read(master_substreams) == 0)
308 stop_stream(efw, master);
311 mutex_unlock(&efw->mutex);
314 void snd_efw_stream_update_duplex(struct snd_efw *efw)
316 if ((cmp_connection_update(&efw->out_conn) < 0) ||
317 (cmp_connection_update(&efw->in_conn) < 0)) {
318 mutex_lock(&efw->mutex);
319 stop_stream(efw, &efw->rx_stream);
320 stop_stream(efw, &efw->tx_stream);
321 mutex_unlock(&efw->mutex);
323 amdtp_stream_update(&efw->rx_stream);
324 amdtp_stream_update(&efw->tx_stream);
328 void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
330 destroy_stream(efw, &efw->rx_stream);
331 destroy_stream(efw, &efw->tx_stream);
334 void snd_efw_stream_lock_changed(struct snd_efw *efw)
336 efw->dev_lock_changed = true;
337 wake_up(&efw->hwdep_wait);
340 int snd_efw_stream_lock_try(struct snd_efw *efw)
344 spin_lock_irq(&efw->lock);
346 /* user land lock this */
347 if (efw->dev_lock_count < 0) {
352 /* this is the first time */
353 if (efw->dev_lock_count++ == 0)
354 snd_efw_stream_lock_changed(efw);
357 spin_unlock_irq(&efw->lock);
361 void snd_efw_stream_lock_release(struct snd_efw *efw)
363 spin_lock_irq(&efw->lock);
365 if (WARN_ON(efw->dev_lock_count <= 0))
367 if (--efw->dev_lock_count == 0)
368 snd_efw_stream_lock_changed(efw);
370 spin_unlock_irq(&efw->lock);