blob: 8104af94aed5aacb9d44a92c0b964b8726c5b99f [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{
Takashi Sakamotod18b0a62019-06-11 22:21:08 +0900128 __be32 data;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900129
Takashi Sakamotod18b0a62019-06-11 22:21:08 +0900130 amdtp_stream_stop(&dg00x->tx_stream);
131 amdtp_stream_stop(&dg00x->rx_stream);
132
133 data = cpu_to_be32(0x00000003);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900134 snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
135 DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_SET,
136 &data, sizeof(data), 0);
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900137
138 // Unregister isochronous channels for both direction.
139 data = 0;
140 snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
141 DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS,
142 &data, sizeof(data), 0);
Takashi Sakamotod18b0a62019-06-11 22:21:08 +0900143
144 // Just after finishing the session, the device may lost transmitting
145 // functionality for a short time.
146 msleep(50);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900147}
148
149static int begin_session(struct snd_dg00x *dg00x)
150{
151 __be32 data;
152 u32 curr;
153 int err;
154
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900155 // Register isochronous channels for both direction.
156 data = cpu_to_be32((dg00x->tx_resources.channel << 16) |
157 dg00x->rx_resources.channel);
158 err = snd_fw_transaction(dg00x->unit, TCODE_WRITE_QUADLET_REQUEST,
159 DG00X_ADDR_BASE + DG00X_OFFSET_ISOC_CHANNELS,
160 &data, sizeof(data), 0);
161 if (err < 0)
Takashi Sakamoto638e19f2019-06-11 22:21:09 +0900162 return err;
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900163
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900164 err = snd_fw_transaction(dg00x->unit, TCODE_READ_QUADLET_REQUEST,
165 DG00X_ADDR_BASE + DG00X_OFFSET_STREAMING_STATE,
166 &data, sizeof(data), 0);
167 if (err < 0)
Takashi Sakamoto638e19f2019-06-11 22:21:09 +0900168 return err;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900169 curr = be32_to_cpu(data);
170
171 if (curr == 0)
172 curr = 2;
173
174 curr--;
175 while (curr > 0) {
176 data = cpu_to_be32(curr);
177 err = snd_fw_transaction(dg00x->unit,
178 TCODE_WRITE_QUADLET_REQUEST,
179 DG00X_ADDR_BASE +
180 DG00X_OFFSET_STREAMING_SET,
181 &data, sizeof(data), 0);
182 if (err < 0)
Takashi Sakamoto638e19f2019-06-11 22:21:09 +0900183 break;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900184
185 msleep(20);
186 curr--;
187 }
188
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900189 return err;
190}
191
192static void release_resources(struct snd_dg00x *dg00x)
193{
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900194 /* Release isochronous resources. */
195 fw_iso_resources_free(&dg00x->tx_resources);
196 fw_iso_resources_free(&dg00x->rx_resources);
197}
198
199static int keep_resources(struct snd_dg00x *dg00x, unsigned int rate)
200{
201 unsigned int i;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900202 int err;
203
204 /* Check sampling rate. */
205 for (i = 0; i < SND_DG00X_RATE_COUNT; i++) {
206 if (snd_dg00x_stream_rates[i] == rate)
207 break;
208 }
209 if (i == SND_DG00X_RATE_COUNT)
210 return -EINVAL;
211
212 /* Keep resources for out-stream. */
213 err = amdtp_dot_set_parameters(&dg00x->rx_stream, rate,
Takashi Sakamoto9dc5d312015-10-11 12:30:15 +0900214 snd_dg00x_stream_pcm_channels[i]);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900215 if (err < 0)
216 return err;
217 err = fw_iso_resources_allocate(&dg00x->rx_resources,
218 amdtp_stream_get_max_payload(&dg00x->rx_stream),
219 fw_parent_device(dg00x->unit)->max_speed);
220 if (err < 0)
221 return err;
222
223 /* Keep resources for in-stream. */
224 err = amdtp_dot_set_parameters(&dg00x->tx_stream, rate,
Takashi Sakamoto9dc5d312015-10-11 12:30:15 +0900225 snd_dg00x_stream_pcm_channels[i]);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900226 if (err < 0)
227 return err;
228 err = fw_iso_resources_allocate(&dg00x->tx_resources,
229 amdtp_stream_get_max_payload(&dg00x->tx_stream),
230 fw_parent_device(dg00x->unit)->max_speed);
Takashi Sakamoto6bc93222019-06-11 22:21:07 +0900231 if (err < 0) {
232 fw_iso_resources_free(&dg00x->rx_resources);
233 return err;
234 }
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900235
236 return 0;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900237}
238
239int snd_dg00x_stream_init_duplex(struct snd_dg00x *dg00x)
240{
241 int err;
242
243 /* For out-stream. */
244 err = fw_iso_resources_init(&dg00x->rx_resources, dg00x->unit);
245 if (err < 0)
246 goto error;
247 err = amdtp_dot_init(&dg00x->rx_stream, dg00x->unit, AMDTP_OUT_STREAM);
248 if (err < 0)
249 goto error;
250
251 /* For in-stream. */
252 err = fw_iso_resources_init(&dg00x->tx_resources, dg00x->unit);
253 if (err < 0)
254 goto error;
255 err = amdtp_dot_init(&dg00x->tx_stream, dg00x->unit, AMDTP_IN_STREAM);
256 if (err < 0)
257 goto error;
258
259 return 0;
260error:
261 snd_dg00x_stream_destroy_duplex(dg00x);
262 return err;
263}
264
265/*
266 * This function should be called before starting streams or after stopping
267 * streams.
268 */
269void snd_dg00x_stream_destroy_duplex(struct snd_dg00x *dg00x)
270{
271 amdtp_stream_destroy(&dg00x->rx_stream);
272 fw_iso_resources_destroy(&dg00x->rx_resources);
273
274 amdtp_stream_destroy(&dg00x->tx_stream);
275 fw_iso_resources_destroy(&dg00x->tx_resources);
276}
277
278int snd_dg00x_stream_start_duplex(struct snd_dg00x *dg00x, unsigned int rate)
279{
280 unsigned int curr_rate;
281 int err = 0;
282
283 if (dg00x->substreams_counter == 0)
284 goto end;
285
286 /* Check current sampling rate. */
287 err = snd_dg00x_stream_get_local_rate(dg00x, &curr_rate);
288 if (err < 0)
289 goto error;
Takashi Sakamoto9fbfd382015-10-11 12:30:16 +0900290 if (rate == 0)
291 rate = curr_rate;
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900292 if (curr_rate != rate ||
293 amdtp_streaming_error(&dg00x->tx_stream) ||
294 amdtp_streaming_error(&dg00x->rx_stream)) {
295 finish_session(dg00x);
296
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900297 release_resources(dg00x);
298 }
299
300 /*
301 * No packets are transmitted without receiving packets, reagardless of
302 * which source of clock is used.
303 */
304 if (!amdtp_stream_running(&dg00x->rx_stream)) {
305 err = snd_dg00x_stream_set_local_rate(dg00x, rate);
306 if (err < 0)
307 goto error;
308
309 err = keep_resources(dg00x, rate);
310 if (err < 0)
311 goto error;
312
313 err = begin_session(dg00x);
314 if (err < 0)
315 goto error;
316
317 err = amdtp_stream_start(&dg00x->rx_stream,
318 dg00x->rx_resources.channel,
319 fw_parent_device(dg00x->unit)->max_speed);
320 if (err < 0)
321 goto error;
322
323 if (!amdtp_stream_wait_callback(&dg00x->rx_stream,
324 CALLBACK_TIMEOUT)) {
325 err = -ETIMEDOUT;
326 goto error;
327 }
328 }
329
330 /*
331 * The value of SYT field in transmitted packets is always 0x0000. Thus,
332 * duplex streams with timestamp synchronization cannot be built.
333 */
334 if (!amdtp_stream_running(&dg00x->tx_stream)) {
335 err = amdtp_stream_start(&dg00x->tx_stream,
336 dg00x->tx_resources.channel,
337 fw_parent_device(dg00x->unit)->max_speed);
338 if (err < 0)
339 goto error;
340
341 if (!amdtp_stream_wait_callback(&dg00x->tx_stream,
342 CALLBACK_TIMEOUT)) {
343 err = -ETIMEDOUT;
344 goto error;
345 }
346 }
347end:
348 return err;
349error:
350 finish_session(dg00x);
351
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900352 release_resources(dg00x);
353
354 return err;
355}
356
357void snd_dg00x_stream_stop_duplex(struct snd_dg00x *dg00x)
358{
359 if (dg00x->substreams_counter > 0)
360 return;
361
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900362 finish_session(dg00x);
363 release_resources(dg00x);
Takashi Sakamoto3a2a1792015-09-30 09:39:18 +0900364}
365
366void snd_dg00x_stream_update_duplex(struct snd_dg00x *dg00x)
367{
368 fw_iso_resources_update(&dg00x->tx_resources);
369 fw_iso_resources_update(&dg00x->rx_resources);
370
371 amdtp_stream_update(&dg00x->tx_stream);
372 amdtp_stream_update(&dg00x->rx_stream);
373}
Takashi Sakamoto660dd3d2015-09-30 09:39:21 +0900374
375void snd_dg00x_stream_lock_changed(struct snd_dg00x *dg00x)
376{
377 dg00x->dev_lock_changed = true;
378 wake_up(&dg00x->hwdep_wait);
379}
380
381int snd_dg00x_stream_lock_try(struct snd_dg00x *dg00x)
382{
383 int err;
384
385 spin_lock_irq(&dg00x->lock);
386
387 /* user land lock this */
388 if (dg00x->dev_lock_count < 0) {
389 err = -EBUSY;
390 goto end;
391 }
392
393 /* this is the first time */
394 if (dg00x->dev_lock_count++ == 0)
395 snd_dg00x_stream_lock_changed(dg00x);
396 err = 0;
397end:
398 spin_unlock_irq(&dg00x->lock);
399 return err;
400}
401
402void snd_dg00x_stream_lock_release(struct snd_dg00x *dg00x)
403{
404 spin_lock_irq(&dg00x->lock);
405
406 if (WARN_ON(dg00x->dev_lock_count <= 0))
407 goto end;
408 if (--dg00x->dev_lock_count == 0)
409 snd_dg00x_stream_lock_changed(dg00x);
410end:
411 spin_unlock_irq(&dg00x->lock);
412}