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 s->unit = fw_unit_get(unit);
47 s->context = ERR_PTR(-1);
48 mutex_init(&s->mutex);
49 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
54 EXPORT_SYMBOL(amdtp_out_stream_init);
57 * amdtp_out_stream_destroy - free stream resources
58 * @s: the AMDTP output stream to destroy
60 void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
62 WARN_ON(amdtp_out_stream_running(s));
63 mutex_destroy(&s->mutex);
66 EXPORT_SYMBOL(amdtp_out_stream_destroy);
69 * amdtp_out_stream_set_rate - set the sample rate
70 * @s: the AMDTP output stream to configure
71 * @rate: the sample rate
73 * The sample rate must be set before the stream is started, and must not be
74 * changed while the stream is running.
76 void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
80 unsigned int syt_interval;
82 [CIP_SFC_32000] = { 32000, 8, },
83 [CIP_SFC_44100] = { 44100, 8, },
84 [CIP_SFC_48000] = { 48000, 8, },
85 [CIP_SFC_88200] = { 88200, 16, },
86 [CIP_SFC_96000] = { 96000, 16, },
87 [CIP_SFC_176400] = { 176400, 32, },
88 [CIP_SFC_192000] = { 192000, 32, },
92 if (WARN_ON(amdtp_out_stream_running(s)))
95 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
96 if (rate_info[sfc].rate == rate)
103 s->syt_interval = rate_info[sfc].syt_interval;
105 /* default buffering in the device */
106 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
107 if (s->flags & CIP_BLOCKING)
108 /* additional buffering needed to adjust for no-data packets */
109 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
111 EXPORT_SYMBOL(amdtp_out_stream_set_rate);
114 * amdtp_out_stream_get_max_payload - get the stream's packet size
115 * @s: the AMDTP output stream
117 * This function must not be called before the stream has been configured
118 * with amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(), and
119 * amdtp_out_stream_set_midi().
121 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
123 s->data_block_quadlets = s->pcm_channels;
124 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
126 return 8 + s->syt_interval * s->data_block_quadlets * 4;
128 EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
130 static void amdtp_write_s16(struct amdtp_out_stream *s,
131 struct snd_pcm_substream *pcm,
132 __be32 *buffer, unsigned int frames);
133 static void amdtp_write_s32(struct amdtp_out_stream *s,
134 struct snd_pcm_substream *pcm,
135 __be32 *buffer, unsigned int frames);
138 * amdtp_out_stream_set_pcm_format - set the PCM format
139 * @s: the AMDTP output stream to configure
140 * @format: the format of the ALSA PCM device
142 * The sample format must be set before the stream is started, and must not be
143 * changed while the stream is running.
145 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
146 snd_pcm_format_t format)
148 if (WARN_ON(amdtp_out_stream_running(s)))
155 case SNDRV_PCM_FORMAT_S16:
156 s->transfer_samples = amdtp_write_s16;
158 case SNDRV_PCM_FORMAT_S32:
159 s->transfer_samples = amdtp_write_s32;
163 EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
166 * amdtp_out_stream_pcm_prepare - prepare PCM device for running
167 * @s: the AMDTP output stream
169 * This function should be called from the PCM device's .prepare callback.
171 void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
173 tasklet_kill(&s->period_tasklet);
174 s->pcm_buffer_pointer = 0;
175 s->pcm_period_pointer = 0;
176 s->pointer_flush = true;
178 EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
180 static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
182 unsigned int phase, data_blocks;
184 if (!cip_sfc_is_base_44100(s->sfc)) {
185 /* Sample_rate / 8000 is an integer, and precomputed. */
186 data_blocks = s->data_block_state;
188 phase = s->data_block_state;
191 * This calculates the number of data blocks per packet so that
192 * 1) the overall rate is correct and exactly synchronized to
194 * 2) packets with a rounded-up number of blocks occur as early
195 * as possible in the sequence (to prevent underruns of the
198 if (s->sfc == CIP_SFC_44100)
199 /* 6 6 5 6 5 6 5 ... */
200 data_blocks = 5 + ((phase & 1) ^
201 (phase == 0 || phase >= 40));
203 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
204 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
205 if (++phase >= (80 >> (s->sfc >> 1)))
207 s->data_block_state = phase;
213 static unsigned int calculate_syt(struct amdtp_out_stream *s,
216 unsigned int syt_offset, phase, index, syt;
218 if (s->last_syt_offset < TICKS_PER_CYCLE) {
219 if (!cip_sfc_is_base_44100(s->sfc))
220 syt_offset = s->last_syt_offset + s->syt_offset_state;
223 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
224 * n * SYT_INTERVAL * 24576000 / sample_rate
225 * Modulo TICKS_PER_CYCLE, the difference between successive
226 * elements is about 1386.23. Rounding the results of this
227 * formula to the SYT precision results in a sequence of
228 * differences that begins with:
229 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
230 * This code generates _exactly_ the same sequence.
232 phase = s->syt_offset_state;
234 syt_offset = s->last_syt_offset;
235 syt_offset += 1386 + ((index && !(index & 3)) ||
239 s->syt_offset_state = phase;
242 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
243 s->last_syt_offset = syt_offset;
245 if (syt_offset < TICKS_PER_CYCLE) {
246 syt_offset += s->transfer_delay;
247 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
248 syt += syt_offset % TICKS_PER_CYCLE;
252 return 0xffff; /* no info */
256 static void amdtp_write_s32(struct amdtp_out_stream *s,
257 struct snd_pcm_substream *pcm,
258 __be32 *buffer, unsigned int frames)
260 struct snd_pcm_runtime *runtime = pcm->runtime;
261 unsigned int channels, remaining_frames, frame_step, i, c;
264 channels = s->pcm_channels;
265 src = (void *)runtime->dma_area +
266 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
267 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
268 frame_step = s->data_block_quadlets - channels;
270 for (i = 0; i < frames; ++i) {
271 for (c = 0; c < channels; ++c) {
272 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
276 buffer += frame_step;
277 if (--remaining_frames == 0)
278 src = (void *)runtime->dma_area;
282 static void amdtp_write_s16(struct amdtp_out_stream *s,
283 struct snd_pcm_substream *pcm,
284 __be32 *buffer, unsigned int frames)
286 struct snd_pcm_runtime *runtime = pcm->runtime;
287 unsigned int channels, remaining_frames, frame_step, i, c;
290 channels = s->pcm_channels;
291 src = (void *)runtime->dma_area +
292 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
293 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
294 frame_step = s->data_block_quadlets - channels;
296 for (i = 0; i < frames; ++i) {
297 for (c = 0; c < channels; ++c) {
298 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
302 buffer += frame_step;
303 if (--remaining_frames == 0)
304 src = (void *)runtime->dma_area;
308 static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
309 __be32 *buffer, unsigned int frames)
313 for (i = 0; i < frames; ++i) {
314 for (c = 0; c < s->pcm_channels; ++c)
315 buffer[c] = cpu_to_be32(0x40000000);
316 buffer += s->data_block_quadlets;
320 static void amdtp_fill_midi(struct amdtp_out_stream *s,
321 __be32 *buffer, unsigned int frames)
325 for (i = 0; i < frames; ++i)
326 buffer[s->pcm_channels + i * s->data_block_quadlets] =
327 cpu_to_be32(0x80000000);
330 static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
333 unsigned int index, data_blocks, syt, ptr;
334 struct snd_pcm_substream *pcm;
335 struct fw_iso_packet packet;
338 if (s->packet_index < 0)
340 index = s->packet_index;
342 syt = calculate_syt(s, cycle);
343 if (!(s->flags & CIP_BLOCKING)) {
344 data_blocks = calculate_data_blocks(s);
347 data_blocks = s->syt_interval;
354 buffer = s->buffer.packets[index].buffer;
355 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
356 (s->data_block_quadlets << 16) |
357 s->data_block_counter);
358 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
359 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
362 pcm = ACCESS_ONCE(s->pcm);
364 s->transfer_samples(s, pcm, buffer, data_blocks);
366 amdtp_fill_pcm_silence(s, buffer, data_blocks);
368 amdtp_fill_midi(s, buffer, data_blocks);
370 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
372 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
373 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
375 packet.tag = TAG_CIP;
377 packet.header_length = 0;
379 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
380 s->buffer.packets[index].offset);
382 dev_err(&s->unit->device, "queueing error: %d\n", err);
383 s->packet_index = -1;
384 amdtp_out_stream_pcm_abort(s);
388 if (++index >= QUEUE_LENGTH)
390 s->packet_index = index;
393 ptr = s->pcm_buffer_pointer + data_blocks;
394 if (ptr >= pcm->runtime->buffer_size)
395 ptr -= pcm->runtime->buffer_size;
396 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
398 s->pcm_period_pointer += data_blocks;
399 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
400 s->pcm_period_pointer -= pcm->runtime->period_size;
401 s->pointer_flush = false;
402 tasklet_hi_schedule(&s->period_tasklet);
407 static void pcm_period_tasklet(unsigned long data)
409 struct amdtp_out_stream *s = (void *)data;
410 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
413 snd_pcm_period_elapsed(pcm);
416 static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
417 size_t header_length, void *header, void *data)
419 struct amdtp_out_stream *s = data;
420 unsigned int i, packets = header_length / 4;
423 * Compute the cycle of the last queued packet.
424 * (We need only the four lowest bits for the SYT, so we can ignore
425 * that bits 0-11 must wrap around at 3072.)
427 cycle += QUEUE_LENGTH - packets;
429 for (i = 0; i < packets; ++i)
430 queue_out_packet(s, ++cycle);
431 fw_iso_context_queue_flush(s->context);
434 static int queue_initial_skip_packets(struct amdtp_out_stream *s)
436 struct fw_iso_packet skip_packet = {
442 for (i = 0; i < QUEUE_LENGTH; ++i) {
443 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
445 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
448 if (++s->packet_index >= QUEUE_LENGTH)
456 * amdtp_out_stream_start - start sending packets
457 * @s: the AMDTP output stream to start
458 * @channel: the isochronous channel on the bus
459 * @speed: firewire speed code
461 * The stream cannot be started until it has been configured with
462 * amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(),
463 * amdtp_out_stream_set_midi(), and amdtp_out_stream_set_format();
464 * and it must be started before any PCM or MIDI device can be started.
466 int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
468 static const struct {
469 unsigned int data_block;
470 unsigned int syt_offset;
471 } initial_state[] = {
472 [CIP_SFC_32000] = { 4, 3072 },
473 [CIP_SFC_48000] = { 6, 1024 },
474 [CIP_SFC_96000] = { 12, 1024 },
475 [CIP_SFC_192000] = { 24, 1024 },
476 [CIP_SFC_44100] = { 0, 67 },
477 [CIP_SFC_88200] = { 0, 67 },
478 [CIP_SFC_176400] = { 0, 67 },
482 mutex_lock(&s->mutex);
484 if (WARN_ON(amdtp_out_stream_running(s) ||
485 (!s->pcm_channels && !s->midi_ports))) {
490 s->data_block_state = initial_state[s->sfc].data_block;
491 s->syt_offset_state = initial_state[s->sfc].syt_offset;
492 s->last_syt_offset = TICKS_PER_CYCLE;
494 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
495 amdtp_out_stream_get_max_payload(s),
500 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
501 FW_ISO_CONTEXT_TRANSMIT,
503 out_packet_callback, s);
504 if (IS_ERR(s->context)) {
505 err = PTR_ERR(s->context);
507 dev_err(&s->unit->device,
508 "no free output stream on this controller\n");
512 amdtp_out_stream_update(s);
515 s->data_block_counter = 0;
516 err = queue_initial_skip_packets(s);
520 err = fw_iso_context_start(s->context, -1, 0, 0);
524 mutex_unlock(&s->mutex);
529 fw_iso_context_destroy(s->context);
530 s->context = ERR_PTR(-1);
532 iso_packets_buffer_destroy(&s->buffer, s->unit);
534 mutex_unlock(&s->mutex);
538 EXPORT_SYMBOL(amdtp_out_stream_start);
541 * amdtp_out_stream_pcm_pointer - get the PCM buffer position
542 * @s: the AMDTP output stream that transports the PCM data
544 * Returns the current buffer position, in frames.
546 unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
548 /* this optimization is allowed to be racy */
549 if (s->pointer_flush)
550 fw_iso_context_flush_completions(s->context);
552 s->pointer_flush = true;
554 return ACCESS_ONCE(s->pcm_buffer_pointer);
556 EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);
559 * amdtp_out_stream_update - update the stream after a bus reset
560 * @s: the AMDTP output stream
562 void amdtp_out_stream_update(struct amdtp_out_stream *s)
564 ACCESS_ONCE(s->source_node_id_field) =
565 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
567 EXPORT_SYMBOL(amdtp_out_stream_update);
570 * amdtp_out_stream_stop - stop sending packets
571 * @s: the AMDTP output stream to stop
573 * All PCM and MIDI devices of the stream must be stopped before the stream
574 * itself can be stopped.
576 void amdtp_out_stream_stop(struct amdtp_out_stream *s)
578 mutex_lock(&s->mutex);
580 if (!amdtp_out_stream_running(s)) {
581 mutex_unlock(&s->mutex);
585 tasklet_kill(&s->period_tasklet);
586 fw_iso_context_stop(s->context);
587 fw_iso_context_destroy(s->context);
588 s->context = ERR_PTR(-1);
589 iso_packets_buffer_destroy(&s->buffer, s->unit);
591 mutex_unlock(&s->mutex);
593 EXPORT_SYMBOL(amdtp_out_stream_stop);
596 * amdtp_out_stream_pcm_abort - abort the running PCM device
597 * @s: the AMDTP stream about to be stopped
599 * If the isochronous stream needs to be stopped asynchronously, call this
600 * function first to stop the PCM device.
602 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
604 struct snd_pcm_substream *pcm;
606 pcm = ACCESS_ONCE(s->pcm);
608 snd_pcm_stream_lock_irq(pcm);
609 if (snd_pcm_running(pcm))
610 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
611 snd_pcm_stream_unlock_irq(pcm);
614 EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);