2 * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
3 * with Common Isochronous Packet (IEC 61883-1) headers
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
6 * Licensed under the terms of the GNU General Public License, version 2.
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>
17 #define TICKS_PER_CYCLE 3072
18 #define CYCLES_PER_SECOND 8000
19 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
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
30 /* TODO: make these configurable */
31 #define INTERRUPT_INTERVAL 16
32 #define QUEUE_LENGTH 48
34 static void pcm_period_tasklet(unsigned long data);
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
42 int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
43 enum cip_out_flags flags)
45 if (flags != CIP_NONBLOCKING)
48 s->unit = fw_unit_get(unit);
50 s->context = ERR_PTR(-1);
51 mutex_init(&s->mutex);
52 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
57 EXPORT_SYMBOL(amdtp_out_stream_init);
60 * amdtp_out_stream_destroy - free stream resources
61 * @s: the AMDTP output stream to destroy
63 void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
65 WARN_ON(!IS_ERR(s->context));
66 mutex_destroy(&s->mutex);
69 EXPORT_SYMBOL(amdtp_out_stream_destroy);
72 * amdtp_out_stream_set_rate - set the sample rate
73 * @s: the AMDTP output stream to configure
74 * @rate: the sample rate
76 * The sample rate must be set before the stream is started, and must not be
77 * changed while the stream is running.
79 void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
83 unsigned int syt_interval;
85 [CIP_SFC_32000] = { 32000, 8, },
86 [CIP_SFC_44100] = { 44100, 8, },
87 [CIP_SFC_48000] = { 48000, 8, },
88 [CIP_SFC_88200] = { 88200, 16, },
89 [CIP_SFC_96000] = { 96000, 16, },
90 [CIP_SFC_176400] = { 176400, 32, },
91 [CIP_SFC_192000] = { 192000, 32, },
95 if (WARN_ON(!IS_ERR(s->context)))
98 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
99 if (rate_info[sfc].rate == rate) {
101 s->syt_interval = rate_info[sfc].syt_interval;
106 EXPORT_SYMBOL(amdtp_out_stream_set_rate);
109 * amdtp_out_stream_get_max_payload - get the stream's packet size
110 * @s: the AMDTP output stream
112 * This function must not be called before the stream has been configured
113 * with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
114 * amdtp_out_stream_set_midi().
116 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
118 static const unsigned int max_data_blocks[] = {
122 [CIP_SFC_88200] = 12,
123 [CIP_SFC_96000] = 12,
124 [CIP_SFC_176400] = 23,
125 [CIP_SFC_192000] = 24,
128 s->data_block_quadlets = s->pcm_channels;
129 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
131 return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
133 EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
135 static void amdtp_write_s16(struct amdtp_out_stream *s,
136 struct snd_pcm_substream *pcm,
137 __be32 *buffer, unsigned int frames);
138 static void amdtp_write_s32(struct amdtp_out_stream *s,
139 struct snd_pcm_substream *pcm,
140 __be32 *buffer, unsigned int frames);
143 * amdtp_out_stream_set_pcm_format - set the PCM format
144 * @s: the AMDTP output stream to configure
145 * @format: the format of the ALSA PCM device
147 * The sample format must be set before the stream is started, and must not be
148 * changed while the stream is running.
150 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
151 snd_pcm_format_t format)
153 if (WARN_ON(!IS_ERR(s->context)))
160 case SNDRV_PCM_FORMAT_S16:
161 s->transfer_samples = amdtp_write_s16;
163 case SNDRV_PCM_FORMAT_S32:
164 s->transfer_samples = amdtp_write_s32;
168 EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
171 * amdtp_out_stream_pcm_prepare - prepare PCM device for running
172 * @s: the AMDTP output stream
174 * This function should be called from the PCM device's .prepare callback.
176 void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
178 tasklet_kill(&s->period_tasklet);
179 s->pcm_buffer_pointer = 0;
180 s->pcm_period_pointer = 0;
182 EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
184 static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
186 unsigned int phase, data_blocks;
188 if (!cip_sfc_is_base_44100(s->sfc)) {
189 /* Sample_rate / 8000 is an integer, and precomputed. */
190 data_blocks = s->data_block_state;
192 phase = s->data_block_state;
195 * This calculates the number of data blocks per packet so that
196 * 1) the overall rate is correct and exactly synchronized to
198 * 2) packets with a rounded-up number of blocks occur as early
199 * as possible in the sequence (to prevent underruns of the
202 if (s->sfc == CIP_SFC_44100)
203 /* 6 6 5 6 5 6 5 ... */
204 data_blocks = 5 + ((phase & 1) ^
205 (phase == 0 || phase >= 40));
207 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
208 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
209 if (++phase >= (80 >> (s->sfc >> 1)))
211 s->data_block_state = phase;
217 static unsigned int calculate_syt(struct amdtp_out_stream *s,
220 unsigned int syt_offset, phase, index, syt;
222 if (s->last_syt_offset < TICKS_PER_CYCLE) {
223 if (!cip_sfc_is_base_44100(s->sfc))
224 syt_offset = s->last_syt_offset + s->syt_offset_state;
227 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
228 * n * SYT_INTERVAL * 24576000 / sample_rate
229 * Modulo TICKS_PER_CYCLE, the difference between successive
230 * elements is about 1386.23. Rounding the results of this
231 * formula to the SYT precision results in a sequence of
232 * differences that begins with:
233 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
234 * This code generates _exactly_ the same sequence.
236 phase = s->syt_offset_state;
238 syt_offset = s->last_syt_offset;
239 syt_offset += 1386 + ((index && !(index & 3)) ||
243 s->syt_offset_state = phase;
246 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
247 s->last_syt_offset = syt_offset;
249 if (syt_offset < TICKS_PER_CYCLE) {
250 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
251 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
252 syt += syt_offset % TICKS_PER_CYCLE;
256 return 0xffff; /* no info */
260 static void amdtp_write_s32(struct amdtp_out_stream *s,
261 struct snd_pcm_substream *pcm,
262 __be32 *buffer, unsigned int frames)
264 struct snd_pcm_runtime *runtime = pcm->runtime;
265 unsigned int channels, remaining_frames, frame_step, i, c;
268 channels = s->pcm_channels;
269 src = (void *)runtime->dma_area +
270 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
271 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
272 frame_step = s->data_block_quadlets - channels;
274 for (i = 0; i < frames; ++i) {
275 for (c = 0; c < channels; ++c) {
276 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
280 buffer += frame_step;
281 if (--remaining_frames == 0)
282 src = (void *)runtime->dma_area;
286 static void amdtp_write_s16(struct amdtp_out_stream *s,
287 struct snd_pcm_substream *pcm,
288 __be32 *buffer, unsigned int frames)
290 struct snd_pcm_runtime *runtime = pcm->runtime;
291 unsigned int channels, remaining_frames, frame_step, i, c;
294 channels = s->pcm_channels;
295 src = (void *)runtime->dma_area +
296 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
297 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
298 frame_step = s->data_block_quadlets - channels;
300 for (i = 0; i < frames; ++i) {
301 for (c = 0; c < channels; ++c) {
302 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
306 buffer += frame_step;
307 if (--remaining_frames == 0)
308 src = (void *)runtime->dma_area;
312 static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
313 __be32 *buffer, unsigned int frames)
317 for (i = 0; i < frames; ++i) {
318 for (c = 0; c < s->pcm_channels; ++c)
319 buffer[c] = cpu_to_be32(0x40000000);
320 buffer += s->data_block_quadlets;
324 static void amdtp_fill_midi(struct amdtp_out_stream *s,
325 __be32 *buffer, unsigned int frames)
329 for (i = 0; i < frames; ++i)
330 buffer[s->pcm_channels + i * s->data_block_quadlets] =
331 cpu_to_be32(0x80000000);
334 static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
337 unsigned int index, data_blocks, syt, ptr;
338 struct snd_pcm_substream *pcm;
339 struct fw_iso_packet packet;
342 if (s->packet_index < 0)
344 index = s->packet_index;
346 data_blocks = calculate_data_blocks(s);
347 syt = calculate_syt(s, cycle);
349 buffer = s->buffer.packets[index].buffer;
350 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
351 (s->data_block_quadlets << 16) |
352 s->data_block_counter);
353 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
354 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
357 pcm = ACCESS_ONCE(s->pcm);
359 s->transfer_samples(s, pcm, buffer, data_blocks);
361 amdtp_fill_pcm_silence(s, buffer, data_blocks);
363 amdtp_fill_midi(s, buffer, data_blocks);
365 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
367 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
368 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
370 packet.tag = TAG_CIP;
372 packet.header_length = 0;
374 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
375 s->buffer.packets[index].offset);
377 dev_err(&s->unit->device, "queueing error: %d\n", err);
378 s->packet_index = -1;
379 amdtp_out_stream_pcm_abort(s);
383 if (++index >= QUEUE_LENGTH)
385 s->packet_index = index;
388 ptr = s->pcm_buffer_pointer + data_blocks;
389 if (ptr >= pcm->runtime->buffer_size)
390 ptr -= pcm->runtime->buffer_size;
391 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
393 s->pcm_period_pointer += data_blocks;
394 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
395 s->pcm_period_pointer -= pcm->runtime->period_size;
396 tasklet_hi_schedule(&s->period_tasklet);
401 static void pcm_period_tasklet(unsigned long data)
403 struct amdtp_out_stream *s = (void *)data;
404 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
407 snd_pcm_period_elapsed(pcm);
410 static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
411 size_t header_length, void *header, void *data)
413 struct amdtp_out_stream *s = data;
414 unsigned int i, packets = header_length / 4;
417 * Compute the cycle of the last queued packet.
418 * (We need only the four lowest bits for the SYT, so we can ignore
419 * that bits 0-11 must wrap around at 3072.)
421 cycle += QUEUE_LENGTH - packets;
423 for (i = 0; i < packets; ++i)
424 queue_out_packet(s, ++cycle);
425 fw_iso_context_queue_flush(s->context);
428 static int queue_initial_skip_packets(struct amdtp_out_stream *s)
430 struct fw_iso_packet skip_packet = {
436 for (i = 0; i < QUEUE_LENGTH; ++i) {
437 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
439 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
442 if (++s->packet_index >= QUEUE_LENGTH)
450 * amdtp_out_stream_start - start sending packets
451 * @s: the AMDTP output stream to start
452 * @channel: the isochronous channel on the bus
453 * @speed: firewire speed code
455 * The stream cannot be started until it has been configured with
456 * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
457 * amdtp_out_stream_set_midi(); and it must be started before any
458 * PCM or MIDI device can be started.
460 int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
462 static const struct {
463 unsigned int data_block;
464 unsigned int syt_offset;
465 } initial_state[] = {
466 [CIP_SFC_32000] = { 4, 3072 },
467 [CIP_SFC_48000] = { 6, 1024 },
468 [CIP_SFC_96000] = { 12, 1024 },
469 [CIP_SFC_192000] = { 24, 1024 },
470 [CIP_SFC_44100] = { 0, 67 },
471 [CIP_SFC_88200] = { 0, 67 },
472 [CIP_SFC_176400] = { 0, 67 },
476 mutex_lock(&s->mutex);
478 if (WARN_ON(!IS_ERR(s->context) ||
479 (!s->pcm_channels && !s->midi_ports))) {
484 s->data_block_state = initial_state[s->sfc].data_block;
485 s->syt_offset_state = initial_state[s->sfc].syt_offset;
486 s->last_syt_offset = TICKS_PER_CYCLE;
488 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
489 amdtp_out_stream_get_max_payload(s),
494 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
495 FW_ISO_CONTEXT_TRANSMIT,
497 out_packet_callback, s);
498 if (IS_ERR(s->context)) {
499 err = PTR_ERR(s->context);
501 dev_err(&s->unit->device,
502 "no free output stream on this controller\n");
506 amdtp_out_stream_update(s);
509 s->data_block_counter = 0;
510 err = queue_initial_skip_packets(s);
514 err = fw_iso_context_start(s->context, -1, 0, 0);
518 mutex_unlock(&s->mutex);
523 fw_iso_context_destroy(s->context);
524 s->context = ERR_PTR(-1);
526 iso_packets_buffer_destroy(&s->buffer, s->unit);
528 mutex_unlock(&s->mutex);
532 EXPORT_SYMBOL(amdtp_out_stream_start);
535 * amdtp_out_stream_update - update the stream after a bus reset
536 * @s: the AMDTP output stream
538 void amdtp_out_stream_update(struct amdtp_out_stream *s)
540 ACCESS_ONCE(s->source_node_id_field) =
541 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
543 EXPORT_SYMBOL(amdtp_out_stream_update);
546 * amdtp_out_stream_stop - stop sending packets
547 * @s: the AMDTP output stream to stop
549 * All PCM and MIDI devices of the stream must be stopped before the stream
550 * itself can be stopped.
552 void amdtp_out_stream_stop(struct amdtp_out_stream *s)
554 mutex_lock(&s->mutex);
556 if (IS_ERR(s->context)) {
557 mutex_unlock(&s->mutex);
561 tasklet_kill(&s->period_tasklet);
562 fw_iso_context_stop(s->context);
563 fw_iso_context_destroy(s->context);
564 s->context = ERR_PTR(-1);
565 iso_packets_buffer_destroy(&s->buffer, s->unit);
567 mutex_unlock(&s->mutex);
569 EXPORT_SYMBOL(amdtp_out_stream_stop);
572 * amdtp_out_stream_pcm_abort - abort the running PCM device
573 * @s: the AMDTP stream about to be stopped
575 * If the isochronous stream needs to be stopped asynchronously, call this
576 * function first to stop the PCM device.
578 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
580 struct snd_pcm_substream *pcm;
582 pcm = ACCESS_ONCE(s->pcm);
584 snd_pcm_stream_lock_irq(pcm);
585 if (snd_pcm_running(pcm))
586 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
587 snd_pcm_stream_unlock_irq(pcm);
590 EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);