blob: ee47924aef0df676223975a08df37ad055cc1f51 [file] [log] [blame]
Takashi Sakamoto315fd412014-04-25 22:45:02 +09001/*
2 * fireworks_stream.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2013-2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8#include "./fireworks.h"
9
10#define CALLBACK_TIMEOUT 100
11
Takashi Sakamoto315fd412014-04-25 22:45:02 +090012static int
13init_stream(struct snd_efw *efw, struct amdtp_stream *stream)
14{
15 struct cmp_connection *conn;
16 enum cmp_direction c_dir;
17 enum amdtp_stream_direction s_dir;
18 int err;
19
20 if (stream == &efw->tx_stream) {
21 conn = &efw->out_conn;
22 c_dir = CMP_OUTPUT;
23 s_dir = AMDTP_IN_STREAM;
24 } else {
25 conn = &efw->in_conn;
26 c_dir = CMP_INPUT;
27 s_dir = AMDTP_OUT_STREAM;
28 }
29
30 err = cmp_connection_init(conn, efw->unit, c_dir, 0);
31 if (err < 0)
32 goto end;
33
Takashi Sakamoto59558152015-09-19 11:21:55 +090034 err = amdtp_am824_init(stream, efw->unit, s_dir, CIP_BLOCKING);
Takashi Sakamoto315fd412014-04-25 22:45:02 +090035 if (err < 0) {
36 amdtp_stream_destroy(stream);
37 cmp_connection_destroy(conn);
38 }
39end:
40 return err;
41}
42
43static void
44stop_stream(struct snd_efw *efw, struct amdtp_stream *stream)
45{
46 amdtp_stream_pcm_abort(stream);
47 amdtp_stream_stop(stream);
48
49 if (stream == &efw->tx_stream)
50 cmp_connection_break(&efw->out_conn);
51 else
52 cmp_connection_break(&efw->in_conn);
53}
54
55static int
56start_stream(struct snd_efw *efw, struct amdtp_stream *stream,
57 unsigned int sampling_rate)
58{
59 struct cmp_connection *conn;
60 unsigned int mode, pcm_channels, midi_ports;
61 int err;
62
63 err = snd_efw_get_multiplier_mode(sampling_rate, &mode);
64 if (err < 0)
65 goto end;
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;
70 } else {
71 conn = &efw->in_conn;
72 pcm_channels = efw->pcm_playback_channels[mode];
73 midi_ports = efw->midi_in_ports;
74 }
75
Takashi Sakamoto51c29fd2015-09-19 11:21:56 +090076 err = amdtp_am824_set_parameters(stream, sampling_rate,
77 pcm_channels, midi_ports, false);
Takashi Sakamoto547e6312015-09-19 11:21:49 +090078 if (err < 0)
79 goto end;
Takashi Sakamoto315fd412014-04-25 22:45:02 +090080
81 /* establish connection via CMP */
82 err = cmp_connection_establish(conn,
83 amdtp_stream_get_max_payload(stream));
84 if (err < 0)
85 goto end;
86
87 /* start amdtp stream */
88 err = amdtp_stream_start(stream,
89 conn->resources.channel,
90 conn->speed);
91 if (err < 0) {
92 stop_stream(efw, stream);
93 goto end;
94 }
95
96 /* wait first callback */
97 if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) {
98 stop_stream(efw, stream);
99 err = -ETIMEDOUT;
100 }
101end:
102 return err;
103}
104
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900105/*
106 * This function should be called before starting the stream or after stopping
107 * the streams.
108 */
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900109static void
110destroy_stream(struct snd_efw *efw, struct amdtp_stream *stream)
111{
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900112 struct cmp_connection *conn;
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900113
114 if (stream == &efw->tx_stream)
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900115 conn = &efw->out_conn;
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900116 else
Takashi Sakamotod23c2cc2015-02-21 23:54:59 +0900117 conn = &efw->in_conn;
118
119 amdtp_stream_destroy(stream);
120 cmp_connection_destroy(&efw->out_conn);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900121}
122
123static int
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900124check_connection_used_by_others(struct snd_efw *efw, struct amdtp_stream *s)
125{
126 struct cmp_connection *conn;
127 bool used;
128 int err;
129
130 if (s == &efw->tx_stream)
131 conn = &efw->out_conn;
132 else
133 conn = &efw->in_conn;
134
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',
140 conn->pcr_index);
141 err = -EBUSY;
142 }
143
144 return err;
145}
146
147int snd_efw_stream_init_duplex(struct snd_efw *efw)
148{
149 int err;
150
151 err = init_stream(efw, &efw->tx_stream);
152 if (err < 0)
153 goto end;
Takashi Sakamoto7ab56642014-04-25 22:45:03 +0900154 /* Fireworks transmits NODATA packets with TAG0. */
155 efw->tx_stream.flags |= CIP_EMPTY_WITH_TAG0;
Takashi Sakamotoc8bdf492014-04-25 22:45:04 +0900156 /* Fireworks has its own meaning for dbc. */
157 efw->tx_stream.flags |= CIP_DBC_IS_END_EVENT;
Takashi Sakamotob84b1a22014-04-25 22:45:07 +0900158 /* Fireworks reset dbc at bus reset. */
159 efw->tx_stream.flags |= CIP_SKIP_DBC_ZERO_CHECK;
Takashi Sakamoto18f5ed32015-08-05 09:21:05 +0900160 /*
161 * But Recent firmwares starts packets with non-zero dbc.
162 * Driver version 5.7.6 installs firmware version 5.7.3.
163 */
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;
Takashi Sakamoto69702232014-04-25 22:45:05 +0900169 /* AudioFire9 always reports wrong dbs. */
170 if (efw->is_af9)
171 efw->tx_stream.flags |= CIP_WRONG_DBS;
Takashi Sakamotod9cd0062014-04-25 22:45:06 +0900172 /* Firmware version 5.5 reports fixed interval for dbc. */
173 if (efw->firmware_version == 0x5050000)
174 efw->tx_stream.tx_dbc_interval = 8;
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900175
176 err = init_stream(efw, &efw->rx_stream);
177 if (err < 0) {
178 destroy_stream(efw, &efw->tx_stream);
179 goto end;
180 }
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900181
182 /* set IEC61883 compliant mode (actually not fully compliant...) */
183 err = snd_efw_command_set_tx_mode(efw, SND_EFW_TRANSPORT_MODE_IEC61883);
184 if (err < 0) {
185 destroy_stream(efw, &efw->tx_stream);
186 destroy_stream(efw, &efw->rx_stream);
187 }
188end:
189 return err;
190}
191
Takashi Sakamoto4a286d52014-05-28 00:14:40 +0900192int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate)
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900193{
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900194 unsigned int curr_rate;
195 int err = 0;
196
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900197 /* Need no substreams */
Takashi Sakamoto4d2c50a2015-11-14 16:54:51 +0900198 if (efw->playback_substreams == 0 && efw->capture_substreams == 0)
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900199 goto end;
200
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900201 /*
202 * Considering JACK/FFADO streaming:
203 * TODO: This can be removed hwdep functionality becomes popular.
204 */
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900205 err = check_connection_used_by_others(efw, &efw->rx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900206 if (err < 0)
207 goto end;
208
209 /* packet queueing error */
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900210 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);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900214
215 /* stop streams if rate is different */
216 err = snd_efw_command_get_sampling_rate(efw, &curr_rate);
217 if (err < 0)
218 goto end;
219 if (rate == 0)
220 rate = curr_rate;
221 if (rate != curr_rate) {
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900222 stop_stream(efw, &efw->tx_stream);
223 stop_stream(efw, &efw->rx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900224 }
225
226 /* master should be always running */
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900227 if (!amdtp_stream_running(&efw->rx_stream)) {
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900228 err = snd_efw_command_set_sampling_rate(efw, rate);
229 if (err < 0)
230 goto end;
231
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900232 err = start_stream(efw, &efw->rx_stream, rate);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900233 if (err < 0) {
234 dev_err(&efw->unit->device,
235 "fail to start AMDTP master stream:%d\n", err);
236 goto end;
237 }
238 }
239
240 /* start slave if needed */
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900241 if (efw->capture_substreams > 0 &&
242 !amdtp_stream_running(&efw->tx_stream)) {
243 err = start_stream(efw, &efw->tx_stream, rate);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900244 if (err < 0) {
245 dev_err(&efw->unit->device,
246 "fail to start AMDTP slave stream:%d\n", err);
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900247 stop_stream(efw, &efw->rx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900248 }
249 }
250end:
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900251 return err;
252}
253
254void snd_efw_stream_stop_duplex(struct snd_efw *efw)
255{
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900256 if (efw->capture_substreams == 0) {
257 stop_stream(efw, &efw->tx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900258
Takashi Sakamotoeb4a3782016-05-09 23:15:51 +0900259 if (efw->playback_substreams == 0)
260 stop_stream(efw, &efw->rx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900261 }
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900262}
263
264void snd_efw_stream_update_duplex(struct snd_efw *efw)
265{
Takashi Sakamoto99d73552016-02-20 16:41:01 +0900266 if (cmp_connection_update(&efw->out_conn) < 0 ||
267 cmp_connection_update(&efw->in_conn) < 0) {
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900268 stop_stream(efw, &efw->rx_stream);
269 stop_stream(efw, &efw->tx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900270 } else {
271 amdtp_stream_update(&efw->rx_stream);
272 amdtp_stream_update(&efw->tx_stream);
273 }
274}
275
276void snd_efw_stream_destroy_duplex(struct snd_efw *efw)
277{
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900278 destroy_stream(efw, &efw->rx_stream);
279 destroy_stream(efw, &efw->tx_stream);
Takashi Sakamoto315fd412014-04-25 22:45:02 +0900280}
Takashi Sakamoto594ddce2014-04-25 22:45:12 +0900281
282void snd_efw_stream_lock_changed(struct snd_efw *efw)
283{
284 efw->dev_lock_changed = true;
285 wake_up(&efw->hwdep_wait);
286}
287
288int snd_efw_stream_lock_try(struct snd_efw *efw)
289{
290 int err;
291
292 spin_lock_irq(&efw->lock);
293
294 /* user land lock this */
295 if (efw->dev_lock_count < 0) {
296 err = -EBUSY;
297 goto end;
298 }
299
300 /* this is the first time */
301 if (efw->dev_lock_count++ == 0)
302 snd_efw_stream_lock_changed(efw);
303 err = 0;
304end:
305 spin_unlock_irq(&efw->lock);
306 return err;
307}
308
309void snd_efw_stream_lock_release(struct snd_efw *efw)
310{
311 spin_lock_irq(&efw->lock);
312
313 if (WARN_ON(efw->dev_lock_count <= 0))
314 goto end;
315 if (--efw->dev_lock_count == 0)
316 snd_efw_stream_lock_changed(efw);
317end:
318 spin_unlock_irq(&efw->lock);
319}