e60b84d7a0f6bcab6fbfbbdd720747c97345d656
[deliverable/linux.git] / sound / firewire / dice / dice-stream.c
1 /*
2 * dice_stream.c - a part of driver for DICE based devices
3 *
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Copyright (c) 2014 Takashi Sakamoto <o-takashi@sakamocchi.jp>
6 *
7 * Licensed under the terms of the GNU General Public License, version 2.
8 */
9
10 #include "dice.h"
11
12 #define CALLBACK_TIMEOUT 200
13
14 const unsigned int snd_dice_rates[SND_DICE_RATES_COUNT] = {
15 /* mode 0 */
16 [0] = 32000,
17 [1] = 44100,
18 [2] = 48000,
19 /* mode 1 */
20 [3] = 88200,
21 [4] = 96000,
22 /* mode 2 */
23 [5] = 176400,
24 [6] = 192000,
25 };
26
27 int snd_dice_stream_get_rate_mode(struct snd_dice *dice, unsigned int rate,
28 unsigned int *mode)
29 {
30 int i;
31
32 for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
33 if (!(dice->clock_caps & BIT(i)))
34 continue;
35 if (snd_dice_rates[i] != rate)
36 continue;
37
38 *mode = (i - 1) / 2;
39 return 0;
40 }
41 return -EINVAL;
42 }
43
44 static void release_resources(struct snd_dice *dice,
45 struct fw_iso_resources *resources)
46 {
47 unsigned int channel;
48
49 /* Reset channel number */
50 channel = cpu_to_be32((u32)-1);
51 if (resources == &dice->tx_resources)
52 snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
53 &channel, 4);
54 else
55 snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
56 &channel, 4);
57
58 fw_iso_resources_free(resources);
59 }
60
61 static int keep_resources(struct snd_dice *dice,
62 struct fw_iso_resources *resources,
63 unsigned int max_payload_bytes)
64 {
65 unsigned int channel;
66 int err;
67
68 err = fw_iso_resources_allocate(resources, max_payload_bytes,
69 fw_parent_device(dice->unit)->max_speed);
70 if (err < 0)
71 goto end;
72
73 /* Set channel number */
74 channel = cpu_to_be32(resources->channel);
75 if (resources == &dice->tx_resources)
76 err = snd_dice_transaction_write_tx(dice, TX_ISOCHRONOUS,
77 &channel, 4);
78 else
79 err = snd_dice_transaction_write_rx(dice, RX_ISOCHRONOUS,
80 &channel, 4);
81 if (err < 0)
82 release_resources(dice, resources);
83 end:
84 return err;
85 }
86
87 static void stop_stream(struct snd_dice *dice, struct amdtp_stream *stream)
88 {
89 amdtp_stream_pcm_abort(stream);
90 amdtp_stream_stop(stream);
91
92 if (stream == &dice->tx_stream)
93 release_resources(dice, &dice->tx_resources);
94 else
95 release_resources(dice, &dice->rx_resources);
96 }
97
98 static int start_stream(struct snd_dice *dice, struct amdtp_stream *stream,
99 unsigned int rate)
100 {
101 struct fw_iso_resources *resources;
102 unsigned int i, mode, pcm_chs, midi_ports;
103 int err;
104
105 err = snd_dice_stream_get_rate_mode(dice, rate, &mode);
106 if (err < 0)
107 goto end;
108 if (stream == &dice->tx_stream) {
109 resources = &dice->tx_resources;
110 pcm_chs = dice->tx_channels[mode];
111 midi_ports = dice->tx_midi_ports[mode];
112 } else {
113 resources = &dice->rx_resources;
114 pcm_chs = dice->rx_channels[mode];
115 midi_ports = dice->rx_midi_ports[mode];
116 }
117
118 /*
119 * At 176.4/192.0 kHz, Dice has a quirk to transfer two PCM frames in
120 * one data block of AMDTP packet. Thus sampling transfer frequency is
121 * a half of PCM sampling frequency, i.e. PCM frames at 192.0 kHz are
122 * transferred on AMDTP packets at 96 kHz. Two successive samples of a
123 * channel are stored consecutively in the packet. This quirk is called
124 * as 'Dual Wire'.
125 * For this quirk, blocking mode is required and PCM buffer size should
126 * be aligned to SYT_INTERVAL.
127 */
128 if (mode > 1) {
129 rate /= 2;
130 pcm_chs *= 2;
131 stream->double_pcm_frames = true;
132 } else {
133 stream->double_pcm_frames = false;
134 }
135
136 amdtp_stream_set_parameters(stream, rate, pcm_chs, midi_ports);
137 if (mode > 1) {
138 pcm_chs /= 2;
139
140 for (i = 0; i < pcm_chs; i++) {
141 stream->pcm_positions[i] = i * 2;
142 stream->pcm_positions[i + pcm_chs] = i * 2 + 1;
143 }
144 }
145
146 err = keep_resources(dice, resources,
147 amdtp_stream_get_max_payload(stream));
148 if (err < 0) {
149 dev_err(&dice->unit->device,
150 "fail to keep isochronous resources\n");
151 goto end;
152 }
153
154 err = amdtp_stream_start(stream, resources->channel,
155 fw_parent_device(dice->unit)->max_speed);
156 if (err < 0)
157 release_resources(dice, resources);
158 end:
159 return err;
160 }
161
162 static int get_sync_mode(struct snd_dice *dice, enum cip_flags *sync_mode)
163 {
164 /* Currently, clock source is fixed at SYT-Match mode. */
165 *sync_mode = 0;
166 return 0;
167 }
168
169 int snd_dice_stream_start_duplex(struct snd_dice *dice, unsigned int rate)
170 {
171 struct amdtp_stream *master, *slave;
172 unsigned int curr_rate;
173 enum cip_flags sync_mode;
174 int err = 0;
175
176 if (dice->substreams_counter == 0)
177 goto end;
178
179 err = get_sync_mode(dice, &sync_mode);
180 if (err < 0)
181 goto end;
182 if (sync_mode == CIP_SYNC_TO_DEVICE) {
183 master = &dice->tx_stream;
184 slave = &dice->rx_stream;
185 } else {
186 master = &dice->rx_stream;
187 slave = &dice->tx_stream;
188 }
189
190 /* Some packet queueing errors. */
191 if (amdtp_streaming_error(master) || amdtp_streaming_error(slave))
192 stop_stream(dice, master);
193
194 /* Stop stream if rate is different. */
195 err = snd_dice_transaction_get_rate(dice, &curr_rate);
196 if (err < 0) {
197 dev_err(&dice->unit->device,
198 "fail to get sampling rate\n");
199 goto end;
200 }
201 if (rate != curr_rate)
202 stop_stream(dice, master);
203
204 if (!amdtp_stream_running(master)) {
205 stop_stream(dice, slave);
206 snd_dice_transaction_clear_enable(dice);
207
208 amdtp_stream_set_sync(sync_mode, master, slave);
209
210 err = snd_dice_transaction_set_rate(dice, rate);
211 if (err < 0) {
212 dev_err(&dice->unit->device,
213 "fail to set sampling rate\n");
214 goto end;
215 }
216
217 /* Start both streams. */
218 err = start_stream(dice, master, rate);
219 if (err < 0) {
220 dev_err(&dice->unit->device,
221 "fail to start AMDTP master stream\n");
222 goto end;
223 }
224 err = start_stream(dice, slave, rate);
225 if (err < 0) {
226 dev_err(&dice->unit->device,
227 "fail to start AMDTP slave stream\n");
228 stop_stream(dice, master);
229 goto end;
230 }
231 err = snd_dice_transaction_set_enable(dice);
232 if (err < 0) {
233 dev_err(&dice->unit->device,
234 "fail to enable interface\n");
235 stop_stream(dice, master);
236 stop_stream(dice, slave);
237 goto end;
238 }
239
240 /* Wait first callbacks */
241 if (!amdtp_stream_wait_callback(master, CALLBACK_TIMEOUT) ||
242 !amdtp_stream_wait_callback(slave, CALLBACK_TIMEOUT)) {
243 snd_dice_transaction_clear_enable(dice);
244 stop_stream(dice, master);
245 stop_stream(dice, slave);
246 err = -ETIMEDOUT;
247 }
248 }
249 end:
250 return err;
251 }
252
253 void snd_dice_stream_stop_duplex(struct snd_dice *dice)
254 {
255 if (dice->substreams_counter > 0)
256 return;
257
258 snd_dice_transaction_clear_enable(dice);
259
260 stop_stream(dice, &dice->tx_stream);
261 stop_stream(dice, &dice->rx_stream);
262 }
263
264 static int init_stream(struct snd_dice *dice, struct amdtp_stream *stream)
265 {
266 int err;
267 struct fw_iso_resources *resources;
268 enum amdtp_stream_direction dir;
269
270 if (stream == &dice->tx_stream) {
271 resources = &dice->tx_resources;
272 dir = AMDTP_IN_STREAM;
273 } else {
274 resources = &dice->rx_resources;
275 dir = AMDTP_OUT_STREAM;
276 }
277
278 err = fw_iso_resources_init(resources, dice->unit);
279 if (err < 0)
280 goto end;
281 resources->channels_mask = 0x00000000ffffffffuLL;
282
283 err = amdtp_stream_init(stream, dice->unit, dir, CIP_BLOCKING);
284 if (err < 0) {
285 amdtp_stream_destroy(stream);
286 fw_iso_resources_destroy(resources);
287 }
288 end:
289 return err;
290 }
291
292 static void destroy_stream(struct snd_dice *dice, struct amdtp_stream *stream)
293 {
294 amdtp_stream_destroy(stream);
295
296 if (stream == &dice->tx_stream)
297 fw_iso_resources_destroy(&dice->tx_resources);
298 else
299 fw_iso_resources_destroy(&dice->rx_resources);
300 }
301
302 int snd_dice_stream_init_duplex(struct snd_dice *dice)
303 {
304 int err;
305
306 dice->substreams_counter = 0;
307
308 err = init_stream(dice, &dice->tx_stream);
309 if (err < 0)
310 goto end;
311
312 err = init_stream(dice, &dice->rx_stream);
313 if (err < 0)
314 goto end;
315
316 /* Currently, clock source is fixed at SYT-Match mode. */
317 err = snd_dice_transaction_set_clock_source(dice, CLOCK_SOURCE_ARX1);
318 if (err < 0) {
319 destroy_stream(dice, &dice->rx_stream);
320 destroy_stream(dice, &dice->tx_stream);
321 }
322 end:
323 return err;
324 }
325
326 void snd_dice_stream_destroy_duplex(struct snd_dice *dice)
327 {
328 snd_dice_transaction_clear_enable(dice);
329
330 stop_stream(dice, &dice->tx_stream);
331 destroy_stream(dice, &dice->tx_stream);
332
333 stop_stream(dice, &dice->rx_stream);
334 destroy_stream(dice, &dice->rx_stream);
335
336 dice->substreams_counter = 0;
337 }
338
339 void snd_dice_stream_update_duplex(struct snd_dice *dice)
340 {
341 /*
342 * On a bus reset, the DICE firmware disables streaming and then goes
343 * off contemplating its own navel for hundreds of milliseconds before
344 * it can react to any of our attempts to reenable streaming. This
345 * means that we lose synchronization anyway, so we force our streams
346 * to stop so that the application can restart them in an orderly
347 * manner.
348 */
349 dice->global_enabled = false;
350
351 stop_stream(dice, &dice->rx_stream);
352 stop_stream(dice, &dice->tx_stream);
353
354 fw_iso_resources_update(&dice->rx_resources);
355 fw_iso_resources_update(&dice->tx_resources);
356 }
357
358 static void dice_lock_changed(struct snd_dice *dice)
359 {
360 dice->dev_lock_changed = true;
361 wake_up(&dice->hwdep_wait);
362 }
363
364 int snd_dice_stream_lock_try(struct snd_dice *dice)
365 {
366 int err;
367
368 spin_lock_irq(&dice->lock);
369
370 if (dice->dev_lock_count < 0) {
371 err = -EBUSY;
372 goto out;
373 }
374
375 if (dice->dev_lock_count++ == 0)
376 dice_lock_changed(dice);
377 err = 0;
378 out:
379 spin_unlock_irq(&dice->lock);
380 return err;
381 }
382
383 void snd_dice_stream_lock_release(struct snd_dice *dice)
384 {
385 spin_lock_irq(&dice->lock);
386
387 if (WARN_ON(dice->dev_lock_count <= 0))
388 goto out;
389
390 if (--dice->dev_lock_count == 0)
391 dice_lock_changed(dice);
392 out:
393 spin_unlock_irq(&dice->lock);
394 }
This page took 0.042936 seconds and 4 git commands to generate.