blob: d3226892ad6b44953fd4d980d65874ac40535768 [file] [log] [blame]
Clemens Ladisch31ef9132011-03-15 07:53:21 +01001/*
2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
4 *
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/firewire.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <sound/pcm.h>
15#include "amdtp.h"
16
17#define TICKS_PER_CYCLE 3072
18#define CYCLES_PER_SECOND 8000
19#define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
20
21#define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
22
23#define TAG_CIP 1
24
25#define CIP_EOH (1u << 31)
26#define CIP_FMT_AM (0x10 << 24)
27#define AMDTP_FDF_AM824 (0 << 19)
28#define AMDTP_FDF_SFC_SHIFT 16
29
30/* TODO: make these configurable */
31#define INTERRUPT_INTERVAL 16
32#define QUEUE_LENGTH 48
33
Clemens Ladisch76fb8782012-05-13 22:03:09 +020034static void pcm_period_tasklet(unsigned long data);
35
Clemens Ladisch31ef9132011-03-15 07:53:21 +010036/**
37 * amdtp_out_stream_init - initialize an AMDTP output stream structure
38 * @s: the AMDTP output stream to initialize
39 * @unit: the target of the stream
40 * @flags: the packet transmission method to use
41 */
42int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
43 enum cip_out_flags flags)
44{
Clemens Ladisch31ef9132011-03-15 07:53:21 +010045 s->unit = fw_unit_get(unit);
46 s->flags = flags;
47 s->context = ERR_PTR(-1);
48 mutex_init(&s->mutex);
Clemens Ladisch76fb8782012-05-13 22:03:09 +020049 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
Clemens Ladischec00f5e2011-03-15 07:57:24 +010050 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +010051
52 return 0;
53}
54EXPORT_SYMBOL(amdtp_out_stream_init);
55
56/**
57 * amdtp_out_stream_destroy - free stream resources
58 * @s: the AMDTP output stream to destroy
59 */
60void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
61{
Clemens Ladisch20b65dd2011-09-04 22:15:44 +020062 WARN_ON(amdtp_out_stream_running(s));
Clemens Ladisch31ef9132011-03-15 07:53:21 +010063 mutex_destroy(&s->mutex);
64 fw_unit_put(s->unit);
65}
66EXPORT_SYMBOL(amdtp_out_stream_destroy);
67
Clemens Ladischc5280e92011-10-16 21:39:00 +020068const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
Clemens Ladischa7304e32011-09-04 22:16:10 +020069 [CIP_SFC_32000] = 8,
70 [CIP_SFC_44100] = 8,
71 [CIP_SFC_48000] = 8,
72 [CIP_SFC_88200] = 16,
73 [CIP_SFC_96000] = 16,
74 [CIP_SFC_176400] = 32,
75 [CIP_SFC_192000] = 32,
76};
77EXPORT_SYMBOL(amdtp_syt_intervals);
78
Clemens Ladisch31ef9132011-03-15 07:53:21 +010079/**
Clemens Ladischa7304e32011-09-04 22:16:10 +020080 * amdtp_out_stream_set_parameters - set stream parameters
Clemens Ladisch31ef9132011-03-15 07:53:21 +010081 * @s: the AMDTP output stream to configure
82 * @rate: the sample rate
Clemens Ladischa7304e32011-09-04 22:16:10 +020083 * @pcm_channels: the number of PCM samples in each data block, to be encoded
84 * as AM824 multi-bit linear audio
85 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010086 *
Clemens Ladischa7304e32011-09-04 22:16:10 +020087 * The parameters must be set before the stream is started, and must not be
Clemens Ladisch31ef9132011-03-15 07:53:21 +010088 * changed while the stream is running.
89 */
Clemens Ladischa7304e32011-09-04 22:16:10 +020090void amdtp_out_stream_set_parameters(struct amdtp_out_stream *s,
91 unsigned int rate,
92 unsigned int pcm_channels,
93 unsigned int midi_ports)
Clemens Ladisch31ef9132011-03-15 07:53:21 +010094{
Clemens Ladischa7304e32011-09-04 22:16:10 +020095 static const unsigned int rates[] = {
96 [CIP_SFC_32000] = 32000,
97 [CIP_SFC_44100] = 44100,
98 [CIP_SFC_48000] = 48000,
99 [CIP_SFC_88200] = 88200,
100 [CIP_SFC_96000] = 96000,
101 [CIP_SFC_176400] = 176400,
102 [CIP_SFC_192000] = 192000,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100103 };
104 unsigned int sfc;
105
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200106 if (WARN_ON(amdtp_out_stream_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100107 return;
108
Clemens Ladischa7304e32011-09-04 22:16:10 +0200109 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc)
110 if (rates[sfc] == rate)
Clemens Ladische84d15f2011-09-04 22:12:48 +0200111 goto sfc_found;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100112 WARN_ON(1);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200113 return;
114
115sfc_found:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200116 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
117 if (s->dual_wire) {
118 sfc -= 2;
119 rate /= 2;
120 pcm_channels *= 2;
121 }
Clemens Ladische84d15f2011-09-04 22:12:48 +0200122 s->sfc = sfc;
Clemens Ladischa7304e32011-09-04 22:16:10 +0200123 s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8);
124 s->pcm_channels = pcm_channels;
125 s->midi_ports = midi_ports;
126
127 s->syt_interval = amdtp_syt_intervals[sfc];
Clemens Ladische84d15f2011-09-04 22:12:48 +0200128
129 /* default buffering in the device */
130 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
131 if (s->flags & CIP_BLOCKING)
132 /* additional buffering needed to adjust for no-data packets */
133 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100134}
Clemens Ladischa7304e32011-09-04 22:16:10 +0200135EXPORT_SYMBOL(amdtp_out_stream_set_parameters);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100136
137/**
138 * amdtp_out_stream_get_max_payload - get the stream's packet size
139 * @s: the AMDTP output stream
140 *
141 * This function must not be called before the stream has been configured
Clemens Ladischa7304e32011-09-04 22:16:10 +0200142 * with amdtp_out_stream_set_parameters().
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100143 */
144unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
145{
Clemens Ladische84d15f2011-09-04 22:12:48 +0200146 return 8 + s->syt_interval * s->data_block_quadlets * 4;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100147}
148EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
149
150static void amdtp_write_s16(struct amdtp_out_stream *s,
151 struct snd_pcm_substream *pcm,
152 __be32 *buffer, unsigned int frames);
153static void amdtp_write_s32(struct amdtp_out_stream *s,
154 struct snd_pcm_substream *pcm,
155 __be32 *buffer, unsigned int frames);
Clemens Ladischa7304e32011-09-04 22:16:10 +0200156static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s,
157 struct snd_pcm_substream *pcm,
158 __be32 *buffer, unsigned int frames);
159static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s,
160 struct snd_pcm_substream *pcm,
161 __be32 *buffer, unsigned int frames);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100162
163/**
164 * amdtp_out_stream_set_pcm_format - set the PCM format
165 * @s: the AMDTP output stream to configure
166 * @format: the format of the ALSA PCM device
167 *
Clemens Ladischa7304e32011-09-04 22:16:10 +0200168 * The sample format must be set after the other paramters (rate/PCM channels/
169 * MIDI) and before the stream is started, and must not be changed while the
170 * stream is running.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100171 */
172void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
173 snd_pcm_format_t format)
174{
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200175 if (WARN_ON(amdtp_out_stream_running(s)))
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100176 return;
177
178 switch (format) {
179 default:
180 WARN_ON(1);
181 /* fall through */
182 case SNDRV_PCM_FORMAT_S16:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200183 if (s->dual_wire)
184 s->transfer_samples = amdtp_write_s16_dualwire;
185 else
186 s->transfer_samples = amdtp_write_s16;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100187 break;
188 case SNDRV_PCM_FORMAT_S32:
Clemens Ladischa7304e32011-09-04 22:16:10 +0200189 if (s->dual_wire)
190 s->transfer_samples = amdtp_write_s32_dualwire;
191 else
192 s->transfer_samples = amdtp_write_s32;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100193 break;
194 }
195}
196EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
197
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200198/**
199 * amdtp_out_stream_pcm_prepare - prepare PCM device for running
200 * @s: the AMDTP output stream
201 *
202 * This function should be called from the PCM device's .prepare callback.
203 */
204void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
205{
206 tasklet_kill(&s->period_tasklet);
207 s->pcm_buffer_pointer = 0;
208 s->pcm_period_pointer = 0;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200209 s->pointer_flush = true;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200210}
211EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
212
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100213static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
214{
215 unsigned int phase, data_blocks;
216
217 if (!cip_sfc_is_base_44100(s->sfc)) {
218 /* Sample_rate / 8000 is an integer, and precomputed. */
219 data_blocks = s->data_block_state;
220 } else {
221 phase = s->data_block_state;
222
223 /*
224 * This calculates the number of data blocks per packet so that
225 * 1) the overall rate is correct and exactly synchronized to
226 * the bus clock, and
227 * 2) packets with a rounded-up number of blocks occur as early
228 * as possible in the sequence (to prevent underruns of the
229 * device's buffer).
230 */
231 if (s->sfc == CIP_SFC_44100)
232 /* 6 6 5 6 5 6 5 ... */
233 data_blocks = 5 + ((phase & 1) ^
234 (phase == 0 || phase >= 40));
235 else
236 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
237 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
238 if (++phase >= (80 >> (s->sfc >> 1)))
239 phase = 0;
240 s->data_block_state = phase;
241 }
242
243 return data_blocks;
244}
245
246static unsigned int calculate_syt(struct amdtp_out_stream *s,
247 unsigned int cycle)
248{
249 unsigned int syt_offset, phase, index, syt;
250
251 if (s->last_syt_offset < TICKS_PER_CYCLE) {
252 if (!cip_sfc_is_base_44100(s->sfc))
253 syt_offset = s->last_syt_offset + s->syt_offset_state;
254 else {
255 /*
256 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
257 * n * SYT_INTERVAL * 24576000 / sample_rate
258 * Modulo TICKS_PER_CYCLE, the difference between successive
259 * elements is about 1386.23. Rounding the results of this
260 * formula to the SYT precision results in a sequence of
261 * differences that begins with:
262 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
263 * This code generates _exactly_ the same sequence.
264 */
265 phase = s->syt_offset_state;
266 index = phase % 13;
267 syt_offset = s->last_syt_offset;
268 syt_offset += 1386 + ((index && !(index & 3)) ||
269 phase == 146);
270 if (++phase >= 147)
271 phase = 0;
272 s->syt_offset_state = phase;
273 }
274 } else
275 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
276 s->last_syt_offset = syt_offset;
277
Clemens Ladischbe454362011-03-15 07:55:02 +0100278 if (syt_offset < TICKS_PER_CYCLE) {
Clemens Ladische84d15f2011-09-04 22:12:48 +0200279 syt_offset += s->transfer_delay;
Clemens Ladischbe454362011-03-15 07:55:02 +0100280 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
281 syt += syt_offset % TICKS_PER_CYCLE;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100282
Clemens Ladischbe454362011-03-15 07:55:02 +0100283 return syt & 0xffff;
284 } else {
285 return 0xffff; /* no info */
286 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100287}
288
289static void amdtp_write_s32(struct amdtp_out_stream *s,
290 struct snd_pcm_substream *pcm,
291 __be32 *buffer, unsigned int frames)
292{
293 struct snd_pcm_runtime *runtime = pcm->runtime;
294 unsigned int channels, remaining_frames, frame_step, i, c;
295 const u32 *src;
296
297 channels = s->pcm_channels;
298 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900299 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100300 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
301 frame_step = s->data_block_quadlets - channels;
302
303 for (i = 0; i < frames; ++i) {
304 for (c = 0; c < channels; ++c) {
305 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
306 src++;
307 buffer++;
308 }
309 buffer += frame_step;
310 if (--remaining_frames == 0)
311 src = (void *)runtime->dma_area;
312 }
313}
314
315static void amdtp_write_s16(struct amdtp_out_stream *s,
316 struct snd_pcm_substream *pcm,
317 __be32 *buffer, unsigned int frames)
318{
319 struct snd_pcm_runtime *runtime = pcm->runtime;
320 unsigned int channels, remaining_frames, frame_step, i, c;
321 const u16 *src;
322
323 channels = s->pcm_channels;
324 src = (void *)runtime->dma_area +
Takashi Sakamotoe84841f2013-09-14 00:35:47 +0900325 frames_to_bytes(runtime, s->pcm_buffer_pointer);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100326 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
327 frame_step = s->data_block_quadlets - channels;
328
329 for (i = 0; i < frames; ++i) {
330 for (c = 0; c < channels; ++c) {
331 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
332 src++;
333 buffer++;
334 }
335 buffer += frame_step;
336 if (--remaining_frames == 0)
337 src = (void *)runtime->dma_area;
338 }
339}
340
Clemens Ladischa7304e32011-09-04 22:16:10 +0200341static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s,
342 struct snd_pcm_substream *pcm,
343 __be32 *buffer, unsigned int frames)
344{
345 struct snd_pcm_runtime *runtime = pcm->runtime;
346 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
347 const u32 *src;
348
349 channels = s->pcm_channels;
350 src = (void *)runtime->dma_area +
351 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
352 frame_adjust_1 = channels - 1;
353 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
354
355 channels /= 2;
356 for (i = 0; i < frames; ++i) {
357 for (c = 0; c < channels; ++c) {
358 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
359 src++;
360 buffer += 2;
361 }
362 buffer -= frame_adjust_1;
363 for (c = 0; c < channels; ++c) {
364 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
365 src++;
366 buffer += 2;
367 }
368 buffer -= frame_adjust_2;
369 }
370}
371
372static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s,
373 struct snd_pcm_substream *pcm,
374 __be32 *buffer, unsigned int frames)
375{
376 struct snd_pcm_runtime *runtime = pcm->runtime;
377 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
378 const u16 *src;
379
380 channels = s->pcm_channels;
381 src = (void *)runtime->dma_area +
382 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
383 frame_adjust_1 = channels - 1;
384 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
385
386 channels /= 2;
387 for (i = 0; i < frames; ++i) {
388 for (c = 0; c < channels; ++c) {
389 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
390 src++;
391 buffer += 2;
392 }
393 buffer -= frame_adjust_1;
394 for (c = 0; c < channels; ++c) {
395 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
396 src++;
397 buffer += 2;
398 }
399 buffer -= frame_adjust_2;
400 }
401}
402
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100403static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
404 __be32 *buffer, unsigned int frames)
405{
406 unsigned int i, c;
407
408 for (i = 0; i < frames; ++i) {
409 for (c = 0; c < s->pcm_channels; ++c)
410 buffer[c] = cpu_to_be32(0x40000000);
411 buffer += s->data_block_quadlets;
412 }
413}
414
415static void amdtp_fill_midi(struct amdtp_out_stream *s,
416 __be32 *buffer, unsigned int frames)
417{
418 unsigned int i;
419
420 for (i = 0; i < frames; ++i)
421 buffer[s->pcm_channels + i * s->data_block_quadlets] =
422 cpu_to_be32(0x80000000);
423}
424
425static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
426{
427 __be32 *buffer;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100428 unsigned int index, data_blocks, syt, ptr;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100429 struct snd_pcm_substream *pcm;
430 struct fw_iso_packet packet;
431 int err;
432
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100433 if (s->packet_index < 0)
434 return;
435 index = s->packet_index;
436
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100437 syt = calculate_syt(s, cycle);
Clemens Ladische84d15f2011-09-04 22:12:48 +0200438 if (!(s->flags & CIP_BLOCKING)) {
439 data_blocks = calculate_data_blocks(s);
440 } else {
441 if (syt != 0xffff) {
442 data_blocks = s->syt_interval;
443 } else {
444 data_blocks = 0;
445 syt = 0xffffff;
446 }
447 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100448
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100449 buffer = s->buffer.packets[index].buffer;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100450 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
451 (s->data_block_quadlets << 16) |
452 s->data_block_counter);
453 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
454 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
455 buffer += 2;
456
457 pcm = ACCESS_ONCE(s->pcm);
458 if (pcm)
459 s->transfer_samples(s, pcm, buffer, data_blocks);
460 else
461 amdtp_fill_pcm_silence(s, buffer, data_blocks);
462 if (s->midi_ports)
463 amdtp_fill_midi(s, buffer, data_blocks);
464
465 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
466
467 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100468 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100469 packet.skip = 0;
470 packet.tag = TAG_CIP;
471 packet.sy = 0;
472 packet.header_length = 0;
473
474 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100475 s->buffer.packets[index].offset);
476 if (err < 0) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100477 dev_err(&s->unit->device, "queueing error: %d\n", err);
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100478 s->packet_index = -1;
479 amdtp_out_stream_pcm_abort(s);
480 return;
481 }
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100482
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100483 if (++index >= QUEUE_LENGTH)
484 index = 0;
485 s->packet_index = index;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100486
487 if (pcm) {
Clemens Ladischa7304e32011-09-04 22:16:10 +0200488 if (s->dual_wire)
489 data_blocks *= 2;
490
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100491 ptr = s->pcm_buffer_pointer + data_blocks;
492 if (ptr >= pcm->runtime->buffer_size)
493 ptr -= pcm->runtime->buffer_size;
494 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
495
496 s->pcm_period_pointer += data_blocks;
497 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
498 s->pcm_period_pointer -= pcm->runtime->period_size;
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200499 s->pointer_flush = false;
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200500 tasklet_hi_schedule(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100501 }
502 }
503}
504
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200505static void pcm_period_tasklet(unsigned long data)
506{
507 struct amdtp_out_stream *s = (void *)data;
508 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
509
510 if (pcm)
511 snd_pcm_period_elapsed(pcm);
512}
513
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100514static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
515 size_t header_length, void *header, void *data)
516{
517 struct amdtp_out_stream *s = data;
518 unsigned int i, packets = header_length / 4;
519
520 /*
521 * Compute the cycle of the last queued packet.
522 * (We need only the four lowest bits for the SYT, so we can ignore
523 * that bits 0-11 must wrap around at 3072.)
524 */
525 cycle += QUEUE_LENGTH - packets;
526
527 for (i = 0; i < packets; ++i)
528 queue_out_packet(s, ++cycle);
Clemens Ladisch13882a82011-05-02 09:33:56 +0200529 fw_iso_context_queue_flush(s->context);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100530}
531
532static int queue_initial_skip_packets(struct amdtp_out_stream *s)
533{
534 struct fw_iso_packet skip_packet = {
535 .skip = 1,
536 };
537 unsigned int i;
538 int err;
539
540 for (i = 0; i < QUEUE_LENGTH; ++i) {
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100541 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100542 INTERRUPT_INTERVAL);
543 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
544 if (err < 0)
545 return err;
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100546 if (++s->packet_index >= QUEUE_LENGTH)
547 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100548 }
549
550 return 0;
551}
552
553/**
554 * amdtp_out_stream_start - start sending packets
555 * @s: the AMDTP output stream to start
556 * @channel: the isochronous channel on the bus
557 * @speed: firewire speed code
558 *
559 * The stream cannot be started until it has been configured with
Clemens Ladischa7304e32011-09-04 22:16:10 +0200560 * amdtp_out_stream_set_parameters() and amdtp_out_stream_set_pcm_format(),
Clemens Ladische84d15f2011-09-04 22:12:48 +0200561 * and it must be started before any PCM or MIDI device can be started.
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100562 */
563int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
564{
565 static const struct {
566 unsigned int data_block;
567 unsigned int syt_offset;
568 } initial_state[] = {
569 [CIP_SFC_32000] = { 4, 3072 },
570 [CIP_SFC_48000] = { 6, 1024 },
571 [CIP_SFC_96000] = { 12, 1024 },
572 [CIP_SFC_192000] = { 24, 1024 },
573 [CIP_SFC_44100] = { 0, 67 },
574 [CIP_SFC_88200] = { 0, 67 },
575 [CIP_SFC_176400] = { 0, 67 },
576 };
577 int err;
578
579 mutex_lock(&s->mutex);
580
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200581 if (WARN_ON(amdtp_out_stream_running(s) ||
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100582 (!s->pcm_channels && !s->midi_ports))) {
583 err = -EBADFD;
584 goto err_unlock;
585 }
586
587 s->data_block_state = initial_state[s->sfc].data_block;
588 s->syt_offset_state = initial_state[s->sfc].syt_offset;
589 s->last_syt_offset = TICKS_PER_CYCLE;
590
591 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
592 amdtp_out_stream_get_max_payload(s),
593 DMA_TO_DEVICE);
594 if (err < 0)
595 goto err_unlock;
596
597 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
598 FW_ISO_CONTEXT_TRANSMIT,
599 channel, speed, 0,
600 out_packet_callback, s);
601 if (IS_ERR(s->context)) {
602 err = PTR_ERR(s->context);
603 if (err == -EBUSY)
604 dev_err(&s->unit->device,
605 "no free output stream on this controller\n");
606 goto err_buffer;
607 }
608
609 amdtp_out_stream_update(s);
610
Clemens Ladischec00f5e2011-03-15 07:57:24 +0100611 s->packet_index = 0;
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100612 s->data_block_counter = 0;
613 err = queue_initial_skip_packets(s);
614 if (err < 0)
615 goto err_context;
616
617 err = fw_iso_context_start(s->context, -1, 0, 0);
618 if (err < 0)
619 goto err_context;
620
621 mutex_unlock(&s->mutex);
622
623 return 0;
624
625err_context:
626 fw_iso_context_destroy(s->context);
627 s->context = ERR_PTR(-1);
628err_buffer:
629 iso_packets_buffer_destroy(&s->buffer, s->unit);
630err_unlock:
631 mutex_unlock(&s->mutex);
632
633 return err;
634}
635EXPORT_SYMBOL(amdtp_out_stream_start);
636
637/**
Clemens Ladische9148dd2012-05-13 18:49:14 +0200638 * amdtp_out_stream_pcm_pointer - get the PCM buffer position
639 * @s: the AMDTP output stream that transports the PCM data
640 *
641 * Returns the current buffer position, in frames.
642 */
643unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
644{
Clemens Ladisch92b862c2012-05-13 19:07:22 +0200645 /* this optimization is allowed to be racy */
646 if (s->pointer_flush)
647 fw_iso_context_flush_completions(s->context);
648 else
649 s->pointer_flush = true;
Clemens Ladische9148dd2012-05-13 18:49:14 +0200650
651 return ACCESS_ONCE(s->pcm_buffer_pointer);
652}
653EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);
654
655/**
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100656 * amdtp_out_stream_update - update the stream after a bus reset
657 * @s: the AMDTP output stream
658 */
659void amdtp_out_stream_update(struct amdtp_out_stream *s)
660{
661 ACCESS_ONCE(s->source_node_id_field) =
662 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
663}
664EXPORT_SYMBOL(amdtp_out_stream_update);
665
666/**
667 * amdtp_out_stream_stop - stop sending packets
668 * @s: the AMDTP output stream to stop
669 *
670 * All PCM and MIDI devices of the stream must be stopped before the stream
671 * itself can be stopped.
672 */
673void amdtp_out_stream_stop(struct amdtp_out_stream *s)
674{
675 mutex_lock(&s->mutex);
676
Clemens Ladisch20b65dd2011-09-04 22:15:44 +0200677 if (!amdtp_out_stream_running(s)) {
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100678 mutex_unlock(&s->mutex);
679 return;
680 }
681
Clemens Ladisch76fb8782012-05-13 22:03:09 +0200682 tasklet_kill(&s->period_tasklet);
Clemens Ladisch31ef9132011-03-15 07:53:21 +0100683 fw_iso_context_stop(s->context);
684 fw_iso_context_destroy(s->context);
685 s->context = ERR_PTR(-1);
686 iso_packets_buffer_destroy(&s->buffer, s->unit);
687
688 mutex_unlock(&s->mutex);
689}
690EXPORT_SYMBOL(amdtp_out_stream_stop);
691
692/**
693 * amdtp_out_stream_pcm_abort - abort the running PCM device
694 * @s: the AMDTP stream about to be stopped
695 *
696 * If the isochronous stream needs to be stopped asynchronously, call this
697 * function first to stop the PCM device.
698 */
699void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
700{
701 struct snd_pcm_substream *pcm;
702
703 pcm = ACCESS_ONCE(s->pcm);
704 if (pcm) {
705 snd_pcm_stream_lock_irq(pcm);
706 if (snd_pcm_running(pcm))
707 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
708 snd_pcm_stream_unlock_irq(pcm);
709 }
710}
711EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);