1 // SPDX-License-Identifier: GPL-2.0-only
3 * fireworks_stream.c - a part of driver for Fireworks based devices
5 * Copyright (c) 2013-2014 Takashi Sakamoto
7 #include "./fireworks.h"
9 #define CALLBACK_TIMEOUT 100
11 static int init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
13 struct cmp_connection *conn;
14 enum cmp_direction c_dir;
15 enum amdtp_stream_direction s_dir;
18 if (stream == &efw->tx_stream) {
19 conn = &efw->out_conn;
21 s_dir = AMDTP_IN_STREAM;
25 s_dir = AMDTP_OUT_STREAM;
28 err = cmp_connection_init(conn, efw->unit, c_dir, 0);
32 err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING);
34 amdtp_stream_destroy(stream);
35 cmp_connection_destroy(conn);
39 if (stream == &efw->tx_stream) {
40 // Fireworks transmits NODATA packets with TAG0.
41 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
42 // Fireworks has its own meaning for dbc.
43 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
44 // Fireworks reset dbc at bus reset.
45 efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
46 // But Recent firmwares starts packets with non-zero dbc.
47 // Driver version 5.7.6 installs firmware version 5.7.3.
48 if (efw->is_fireworks3 &&
49 (efw->firmware_version == 0x5070000 ||
50 efw->firmware_version == 0x5070300 ||
51 efw->firmware_version == 0x5080000))
52 efw->tx_stream.flags |= CIP_UNALIGHED_DBC;
53 // AudioFire9 always reports wrong dbs.
55 efw->tx_stream.flags |= CIP_WRONG_DBS;
56 // Firmware version 5.5 reports fixed interval for dbc.
57 if (efw->firmware_version == 0x5050000)
58 efw->tx_stream.ctx_data.tx.dbc_interval = 8;
64 static int start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
67 struct cmp_connection *conn;
70 if (stream == &efw->tx_stream)
71 conn = &efw->out_conn;
75 // Establish connection via CMP.
76 err = cmp_connection_establish(conn);
80 // Start amdtp stream.
81 err = amdtp_domain_add_stream(&efw->domain, stream,
82 conn->resources.channel, conn->speed);
84 cmp_connection_break(conn);
91 // This function should be called before starting the stream or after stopping
93 static void destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
95 amdtp_stream_destroy(stream);
97 if (stream == &efw->tx_stream)
98 cmp_connection_destroy(&efw->out_conn);
100 cmp_connection_destroy(&efw->in_conn);
104 check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
106 struct cmp_connection *conn;
110 if (s == &efw->tx_stream)
111 conn = &efw->out_conn;
113 conn = &efw->in_conn;
115 err = cmp_connection_check_used(conn, &used);
116 if ((err >= 0) && used && !amdtp_stream_running(s)) {
117 dev_err(&efw->unit->device,
118 "Connection established by others: %cPCR[%d]\n",
119 (conn->direction == CMP_OUTPUT) ? 'o' : 'i',
127 int snd_efw_stream_init_duplex(struct snd_efw *efw)
131 err = init_stream(efw, &efw->tx_stream);
135 err = init_stream(efw, &efw->rx_stream);
137 destroy_stream(efw, &efw->tx_stream);
141 err = amdtp_domain_init(&efw->domain);
143 destroy_stream(efw, &efw->tx_stream);
144 destroy_stream(efw, &efw->rx_stream);
148 // set IEC61883 compliant mode (actually not fully compliant...).
149 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
151 destroy_stream(efw, &efw->tx_stream);
152 destroy_stream(efw, &efw->rx_stream);
158 static int keep_resources(struct snd_efw *efw, struct amdtp_stream *stream,
159 unsigned int rate, unsigned int mode)
161 unsigned int pcm_channels;
162 unsigned int midi_ports;
163 struct cmp_connection *conn;
166 if (stream == &efw->tx_stream) {
167 pcm_channels = efw->pcm_capture_channels[mode];
168 midi_ports = efw->midi_out_ports;
169 conn = &efw->out_conn;
171 pcm_channels = efw->pcm_playback_channels[mode];
172 midi_ports = efw->midi_in_ports;
173 conn = &efw->in_conn;
176 err = amdtp_am824_set_parameters(stream, rate, pcm_channels,
181 return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream));
184 int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate,
185 unsigned int frames_per_period,
186 unsigned int frames_per_buffer)
188 unsigned int curr_rate;
191 // Considering JACK/FFADO streaming:
192 // TODO: This can be removed hwdep functionality becomes popular.
193 err = check_connection_used_by_others(efw, &efw->rx_stream);
197 // stop streams if rate is different.
198 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
203 if (rate != curr_rate) {
204 amdtp_domain_stop(&efw->domain);
206 cmp_connection_break(&efw->out_conn);
207 cmp_connection_break(&efw->in_conn);
209 cmp_connection_release(&efw->out_conn);
210 cmp_connection_release(&efw->in_conn);
213 if (efw->substreams_counter == 0 || rate != curr_rate) {
216 err = snd_efw_command_set_sampling_rate(efw, rate);
220 err = snd_efw_get_multiplier_mode(rate, &mode);
224 err = keep_resources(efw, &efw->tx_stream, rate, mode);
228 err = keep_resources(efw, &efw->rx_stream, rate, mode);
230 cmp_connection_release(&efw->in_conn);
234 err = amdtp_domain_set_events_per_period(&efw->domain,
235 frames_per_period, frames_per_buffer);
237 cmp_connection_release(&efw->in_conn);
238 cmp_connection_release(&efw->out_conn);
246 int snd_efw_stream_start_duplex(struct snd_efw *efw)
251 // Need no substreams.
252 if (efw->substreams_counter == 0)
255 if (amdtp_streaming_error(&efw->rx_stream) ||
256 amdtp_streaming_error(&efw->tx_stream)) {
257 amdtp_domain_stop(&efw->domain);
258 cmp_connection_break(&efw->out_conn);
259 cmp_connection_break(&efw->in_conn);
262 err = snd_efw_command_get_sampling_rate(efw, &rate);
266 if (!amdtp_stream_running(&efw->rx_stream)) {
267 err = start_stream(efw, &efw->rx_stream, rate);
271 err = start_stream(efw, &efw->tx_stream, rate);
275 err = amdtp_domain_start(&efw->domain, 0);
279 // Wait first callback.
280 if (!amdtp_stream_wait_callback(&efw->rx_stream,
282 !amdtp_stream_wait_callback(&efw->tx_stream,
291 amdtp_domain_stop(&efw->domain);
293 cmp_connection_break(&efw->out_conn);
294 cmp_connection_break(&efw->in_conn);
299 void snd_efw_stream_stop_duplex(struct snd_efw *efw)
301 if (efw->substreams_counter == 0) {
302 amdtp_domain_stop(&efw->domain);
304 cmp_connection_break(&efw->out_conn);
305 cmp_connection_break(&efw->in_conn);
307 cmp_connection_release(&efw->out_conn);
308 cmp_connection_release(&efw->in_conn);
312 void snd_efw_stream_update_duplex(struct snd_efw *efw)
314 amdtp_domain_stop(&efw->domain);
316 cmp_connection_break(&efw->out_conn);
317 cmp_connection_break(&efw->in_conn);
319 amdtp_stream_pcm_abort(&efw->rx_stream);
320 amdtp_stream_pcm_abort(&efw->tx_stream);
323 void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
325 amdtp_domain_destroy(&efw->domain);
327 destroy_stream(efw, &efw->rx_stream);
328 destroy_stream(efw, &efw->tx_stream);
331 void snd_efw_stream_lock_changed(struct snd_efw *efw)
333 efw->dev_lock_changed = true;
334 wake_up(&efw->hwdep_wait);
337 int snd_efw_stream_lock_try(struct snd_efw *efw)
341 spin_lock_irq(&efw->lock);
343 /* user land lock this */
344 if (efw->dev_lock_count < 0) {
349 /* this is the first time */
350 if (efw->dev_lock_count++ == 0)
351 snd_efw_stream_lock_changed(efw);
354 spin_unlock_irq(&efw->lock);
358 void snd_efw_stream_lock_release(struct snd_efw *efw)
360 spin_lock_irq(&efw->lock);
362 if (WARN_ON(efw->dev_lock_count <= 0))
364 if (--efw->dev_lock_count == 0)
365 snd_efw_stream_lock_changed(efw);
367 spin_unlock_irq(&efw->lock);