blob: 455c43e81850412e639b37cb5105759b8594d178 [file] [log] [blame]
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +09001/*
2 * digi00x-stream.c - a part of driver for Digidesign Digi 002/003 family
3 *
4 * Copyright (c) 2014-2015 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include "digi00x.h"
10
11#define CALLBACK_TIMEOUT 500
12
13const unsigned int snd_dg00x_stream_rates[SND_DG00X_RATE_COUNT] = {
14 [SND_DG00X_RATE_44100] = 44100,
15 [SND_DG00X_RATE_48000] = 48000,
16 [SND_DG00X_RATE_88200] = 88200,
17 [SND_DG00X_RATE_96000] = 96000,
18};
19
20/* Multi Bit Linear Audio data channels for each sampling transfer frequency. */
21const unsigned int
22snd_dg00x_stream_pcm_channels[SND_DG00X_RATE_COUNT] = {
23 /* Analog/ADAT/SPDIF */
24 [SND_DG00X_RATE_44100] = (8 + 8 + 2),
25 [SND_DG00X_RATE_48000] = (8 + 8 + 2),
26 /* Analog/SPDIF */
27 [SND_DG00X_RATE_88200] = (8 + 2),
28 [SND_DG00X_RATE_96000] = (8 + 2),
29};
30
31int snd_dg00x_stream_get_local_rate(struct snd_dg00x *dg00x, unsigned int *rate)
32{
33 u32 data;
34 __be32 reg;
35 int err;
36
37 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
38 DG00X_ADDR_BASE + DG00X_OFFSET_LOCAL_RATE,
39 &reg, sizeof(reg), 0);
40 if (err < 0)
41 return err;
42
43 data = be32_to_cpu(reg) & 0x0f;
44 if (data < ARRAY_SIZE(snd_dg00x_stream_rates))
45 *rate = snd_dg00x_stream_rates[data];
46 else
47 err = -EIO;
48
49 return err;
50}
51
52int snd_dg00x_stream_set_local_rate(struct snd_dg00x *dg00x, unsigned int rate)
53{
54 __be32 reg;
55 unsigned int i;
56
57 for (i = 0; i < ARRAY_SIZE(snd_dg00x_stream_rates); i++) {
58 if (rate == snd_dg00x_stream_rates[i])
59 break;
60 }
61 if (i == ARRAY_SIZE(snd_dg00x_stream_rates))
62 return -EINVAL;
63
64 reg = cpu_to_be32(i);
65 return snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
66 DG00X_ADDR_BASE + DG00X_OFFSET_LOCAL_RATE,
67 &reg, sizeof(reg), 0);
68}
69
70int snd_dg00x_stream_get_clock(struct snd_dg00x *dg00x,
71 enum snd_dg00x_clock *clock)
72{
73 __be32 reg;
74 int err;
75
76 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
77 DG00X_ADDR_BASE + DG00X_OFFSET_CLOCK_SOURCE,
78 &reg, sizeof(reg), 0);
79 if (err < 0)
80 return err;
81
82 *clock = be32_to_cpu(reg) & 0x0f;
83 if (*clock >= SND_DG00X_CLOCK_COUNT)
84 err = -EIO;
85
86 return err;
87}
88
89int snd_dg00x_stream_check_external_clock(struct snd_dg00x *dg00x, bool *detect)
90{
91 __be32 reg;
92 int err;
93
94 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
95 DG00X_ADDR_BASE + DG00X_OFFSET_DETECT_EXTERNAL,
96 &reg, sizeof(reg), 0);
97 if (err >= 0)
98 *detect = be32_to_cpu(reg) > 0;
99
100 return err;
101}
102
103int snd_dg00x_stream_get_external_rate(struct snd_dg00x *dg00x,
104 unsigned int *rate)
105{
106 u32 data;
107 __be32 reg;
108 int err;
109
110 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
111 DG00X_ADDR_BASE + DG00X_OFFSET_EXTERNAL_RATE,
112 &reg, sizeof(reg), 0);
113 if (err < 0)
114 return err;
115
116 data = be32_to_cpu(reg) & 0x0f;
117 if (data < ARRAY_SIZE(snd_dg00x_stream_rates))
118 *rate = snd_dg00x_stream_rates[data];
119 /* This means desync. */
120 else
121 err = -EBUSY;
122
123 return err;
124}
125
126static void finish_session(struct snd_dg00x *dg00x)
127{
128 __be32 data = cpu_to_be32(0x00000003);
129
130 snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
131 DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_SET,
132 &data, sizeof(data), 0);
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900133
134 // Unregister isochronous channels for both direction.
135 data = 0;
136 snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
137 DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS,
138 &data, sizeof(data), 0);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900139}
140
141static int begin_session(struct snd_dg00x *dg00x)
142{
143 __be32 data;
144 u32 curr;
145 int err;
146
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900147 // Register isochronous channels for both direction.
148 data = cpu_to_be32((dg00x->tx_resources.channel << 16) |
149 dg00x->rx_resources.channel);
150 err = snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
151 DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS,
152 &data, sizeof(data), 0);
153 if (err < 0)
154 goto error;
155
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900156 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
157 DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_STATE,
158 &data, sizeof(data), 0);
159 if (err < 0)
160 goto error;
161 curr = be32_to_cpu(data);
162
163 if (curr == 0)
164 curr = 2;
165
166 curr--;
167 while (curr > 0) {
168 data = cpu_to_be32(curr);
169 err = snd_fw_transaction(dg00x->unit,
170 TCODE_WRITE_QUADLET_REQUEST,
171 DG00X_ADDR_BASE +
172 DG00X_OFFSET_STREAMING_SET,
173 &data, sizeof(data), 0);
174 if (err < 0)
175 goto error;
176
177 msleep(20);
178 curr--;
179 }
180
181 return 0;
182error:
183 finish_session(dg00x);
184 return err;
185}
186
187static void release_resources(struct snd_dg00x *dg00x)
188{
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900189 /* Release isochronous resources. */
190 fw_iso_resources_free(&dg00x->tx_resources);
191 fw_iso_resources_free(&dg00x->rx_resources);
192}
193
194static int keep_resources(struct snd_dg00x *dg00x, unsigned int rate)
195{
196 unsigned int i;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900197 int err;
198
199 /* Check sampling rate. */
200 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
201 if (snd_dg00x_stream_rates[i] == rate)
202 break;
203 }
204 if (i == SND_DG00X_RATE_COUNT)
205 return -EINVAL;
206
207 /* Keep resources for out-stream. */
208 err = amdtp_dot_set_parameters(&dg00x->rx_stream, rate,
Takashi Sakamoto9dc5d312015-10-11 12:30:15 +0900209 snd_dg00x_stream_pcm_channels[i]);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900210 if (err < 0)
211 return err;
212 err = fw_iso_resources_allocate(&dg00x->rx_resources,
213 amdtp_stream_get_max_payload(&dg00x->rx_stream),
214 fw_parent_device(dg00x->unit)->max_speed);
215 if (err < 0)
216 return err;
217
218 /* Keep resources for in-stream. */
219 err = amdtp_dot_set_parameters(&dg00x->tx_stream, rate,
Takashi Sakamoto9dc5d312015-10-11 12:30:15 +0900220 snd_dg00x_stream_pcm_channels[i]);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900221 if (err < 0)
222 return err;
223 err = fw_iso_resources_allocate(&dg00x->tx_resources,
224 amdtp_stream_get_max_payload(&dg00x->tx_stream),
225 fw_parent_device(dg00x->unit)->max_speed);
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900226 if (err < 0) {
227 fw_iso_resources_free(&dg00x->rx_resources);
228 return err;
229 }
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900230
231 return 0;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900232}
233
234int snd_dg00x_stream_init_duplex(struct snd_dg00x *dg00x)
235{
236 int err;
237
238 /* For out-stream. */
239 err = fw_iso_resources_init(&dg00x->rx_resources, dg00x->unit);
240 if (err < 0)
241 goto error;
242 err = amdtp_dot_init(&dg00x->rx_stream, dg00x->unit, AMDTP_OUT_STREAM);
243 if (err < 0)
244 goto error;
245
246 /* For in-stream. */
247 err = fw_iso_resources_init(&dg00x->tx_resources, dg00x->unit);
248 if (err < 0)
249 goto error;
250 err = amdtp_dot_init(&dg00x->tx_stream, dg00x->unit, AMDTP_IN_STREAM);
251 if (err < 0)
252 goto error;
253
254 return 0;
255error:
256 snd_dg00x_stream_destroy_duplex(dg00x);
257 return err;
258}
259
260/*
261 * This function should be called before starting streams or after stopping
262 * streams.
263 */
264void snd_dg00x_stream_destroy_duplex(struct snd_dg00x *dg00x)
265{
266 amdtp_stream_destroy(&dg00x->rx_stream);
267 fw_iso_resources_destroy(&dg00x->rx_resources);
268
269 amdtp_stream_destroy(&dg00x->tx_stream);
270 fw_iso_resources_destroy(&dg00x->tx_resources);
271}
272
273int snd_dg00x_stream_start_duplex(struct snd_dg00x *dg00x, unsigned int rate)
274{
275 unsigned int curr_rate;
276 int err = 0;
277
278 if (dg00x->substreams_counter == 0)
279 goto end;
280
281 /* Check current sampling rate. */
282 err = snd_dg00x_stream_get_local_rate(dg00x, &curr_rate);
283 if (err < 0)
284 goto error;
Takashi Sakamoto9fbfd382015-10-11 12:30:16 +0900285 if (rate == 0)
286 rate = curr_rate;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900287 if (curr_rate != rate ||
288 amdtp_streaming_error(&dg00x->tx_stream) ||
289 amdtp_streaming_error(&dg00x->rx_stream)) {
290 finish_session(dg00x);
291
292 amdtp_stream_stop(&dg00x->tx_stream);
293 amdtp_stream_stop(&dg00x->rx_stream);
294 release_resources(dg00x);
295 }
296
297 /*
298 * No packets are transmitted without receiving packets, reagardless of
299 * which source of clock is used.
300 */
301 if (!amdtp_stream_running(&dg00x->rx_stream)) {
302 err = snd_dg00x_stream_set_local_rate(dg00x, rate);
303 if (err < 0)
304 goto error;
305
306 err = keep_resources(dg00x, rate);
307 if (err < 0)
308 goto error;
309
310 err = begin_session(dg00x);
311 if (err < 0)
312 goto error;
313
314 err = amdtp_stream_start(&dg00x->rx_stream,
315 dg00x->rx_resources.channel,
316 fw_parent_device(dg00x->unit)->max_speed);
317 if (err < 0)
318 goto error;
319
320 if (!amdtp_stream_wait_callback(&dg00x->rx_stream,
321 CALLBACK_TIMEOUT)) {
322 err = -ETIMEDOUT;
323 goto error;
324 }
325 }
326
327 /*
328 * The value of SYT field in transmitted packets is always 0x0000. Thus,
329 * duplex streams with timestamp synchronization cannot be built.
330 */
331 if (!amdtp_stream_running(&dg00x->tx_stream)) {
332 err = amdtp_stream_start(&dg00x->tx_stream,
333 dg00x->tx_resources.channel,
334 fw_parent_device(dg00x->unit)->max_speed);
335 if (err < 0)
336 goto error;
337
338 if (!amdtp_stream_wait_callback(&dg00x->tx_stream,
339 CALLBACK_TIMEOUT)) {
340 err = -ETIMEDOUT;
341 goto error;
342 }
343 }
344end:
345 return err;
346error:
347 finish_session(dg00x);
348
349 amdtp_stream_stop(&dg00x->tx_stream);
350 amdtp_stream_stop(&dg00x->rx_stream);
351 release_resources(dg00x);
352
353 return err;
354}
355
356void snd_dg00x_stream_stop_duplex(struct snd_dg00x *dg00x)
357{
358 if (dg00x->substreams_counter > 0)
359 return;
360
361 amdtp_stream_stop(&dg00x->tx_stream);
362 amdtp_stream_stop(&dg00x->rx_stream);
363 finish_session(dg00x);
364 release_resources(dg00x);
365
366 /*
367 * Just after finishing the session, the device may lost transmitting
368 * functionality for a short time.
369 */
370 msleep(50);
371}
372
373void snd_dg00x_stream_update_duplex(struct snd_dg00x *dg00x)
374{
375 fw_iso_resources_update(&dg00x->tx_resources);
376 fw_iso_resources_update(&dg00x->rx_resources);
377
378 amdtp_stream_update(&dg00x->tx_stream);
379 amdtp_stream_update(&dg00x->rx_stream);
380}
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900381
382void snd_dg00x_stream_lock_changed(struct snd_dg00x *dg00x)
383{
384 dg00x->dev_lock_changed = true;
385 wake_up(&dg00x->hwdep_wait);
386}
387
388int snd_dg00x_stream_lock_try(struct snd_dg00x *dg00x)
389{
390 int err;
391
392 spin_lock_irq(&dg00x->lock);
393
394 /* user land lock this */
395 if (dg00x->dev_lock_count < 0) {
396 err = -EBUSY;
397 goto end;
398 }
399
400 /* this is the first time */
401 if (dg00x->dev_lock_count++ == 0)
402 snd_dg00x_stream_lock_changed(dg00x);
403 err = 0;
404end:
405 spin_unlock_irq(&dg00x->lock);
406 return err;
407}
408
409void snd_dg00x_stream_lock_release(struct snd_dg00x *dg00x)
410{
411 spin_lock_irq(&dg00x->lock);
412
413 if (WARN_ON(dg00x->dev_lock_count <= 0))
414 goto end;
415 if (--dg00x->dev_lock_count == 0)
416 snd_dg00x_stream_lock_changed(dg00x);
417end:
418 spin_unlock_irq(&dg00x->lock);
419}