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 check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
126 struct cmp_connection *conn;
130 if (s == &efw->tx_stream)
131 conn = &efw->out_conn;
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',
147 int snd_efw_stream_init_duplex(struct snd_efw *efw)
151 err = init_stream(efw, &efw->tx_stream);
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. */
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);
178 destroy_stream(efw, &efw->tx_stream);
182 /* set IEC61883 compliant mode (actually not fully compliant...) */
183 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
185 destroy_stream(efw, &efw->tx_stream);
186 destroy_stream(efw, &efw->rx_stream);
192 int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
194 unsigned int curr_rate;
197 /* Need no substreams */
198 if (efw->playback_substreams == 0 && efw->capture_substreams == 0)
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);
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);
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);
232 err = start_stream(efw, &efw->rx_stream, rate);
234 dev_err(&efw->unit->device,
235 "fail to start AMDTP master stream:%d\n", err);
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);
245 dev_err(&efw->unit->device,
246 "fail to start AMDTP slave stream:%d\n", err);
247 stop_stream(efw, &efw->rx_stream);
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);
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)
292 spin_lock_irq(&efw->lock);
294 /* user land lock this */
295 if (efw->dev_lock_count < 0) {
300 /* this is the first time */
301 if (efw->dev_lock_count++ == 0)
302 snd_efw_stream_lock_changed(efw);
305 spin_unlock_irq(&efw->lock);
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))
315 if (--efw->dev_lock_count == 0)
316 snd_efw_stream_lock_changed(efw);
318 spin_unlock_irq(&efw->lock);