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
35 * amdtp_out_stream_init - initialize an AMDTP output stream structure
36 * @s: the AMDTP output stream to initialize
37 * @unit: the target of the stream
38 * @flags: the packet transmission method to use
40 int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
41 enum cip_out_flags flags)
43 if (flags != CIP_NONBLOCKING)
46 s->unit = fw_unit_get(unit);
48 s->context = ERR_PTR(-1);
49 mutex_init(&s->mutex);
53 EXPORT_SYMBOL(amdtp_out_stream_init);
56 * amdtp_out_stream_destroy - free stream resources
57 * @s: the AMDTP output stream to destroy
59 void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
61 WARN_ON(!IS_ERR(s->context));
62 mutex_destroy(&s->mutex);
65 EXPORT_SYMBOL(amdtp_out_stream_destroy);
68 * amdtp_out_stream_set_rate - set the sample rate
69 * @s: the AMDTP output stream to configure
70 * @rate: the sample rate
72 * The sample rate must be set before the stream is started, and must not be
73 * changed while the stream is running.
75 void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
79 unsigned int syt_interval;
81 [CIP_SFC_32000] = { 32000, 8, },
82 [CIP_SFC_44100] = { 44100, 8, },
83 [CIP_SFC_48000] = { 48000, 8, },
84 [CIP_SFC_88200] = { 88200, 16, },
85 [CIP_SFC_96000] = { 96000, 16, },
86 [CIP_SFC_176400] = { 176400, 32, },
87 [CIP_SFC_192000] = { 192000, 32, },
91 if (WARN_ON(!IS_ERR(s->context)))
94 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
95 if (rate_info[sfc].rate == rate) {
97 s->syt_interval = rate_info[sfc].syt_interval;
102 EXPORT_SYMBOL(amdtp_out_stream_set_rate);
105 * amdtp_out_stream_get_max_payload - get the stream's packet size
106 * @s: the AMDTP output stream
108 * This function must not be called before the stream has been configured
109 * with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
110 * amdtp_out_stream_set_midi().
112 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
114 static const unsigned int max_data_blocks[] = {
118 [CIP_SFC_88200] = 12,
119 [CIP_SFC_96000] = 12,
120 [CIP_SFC_176400] = 23,
121 [CIP_SFC_192000] = 24,
124 s->data_block_quadlets = s->pcm_channels;
125 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
127 return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
129 EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
131 static void amdtp_write_s16(struct amdtp_out_stream *s,
132 struct snd_pcm_substream *pcm,
133 __be32 *buffer, unsigned int frames);
134 static void amdtp_write_s32(struct amdtp_out_stream *s,
135 struct snd_pcm_substream *pcm,
136 __be32 *buffer, unsigned int frames);
139 * amdtp_out_stream_set_pcm_format - set the PCM format
140 * @s: the AMDTP output stream to configure
141 * @format: the format of the ALSA PCM device
143 * The sample format must be set before the stream is started, and must not be
144 * changed while the stream is running.
146 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
147 snd_pcm_format_t format)
149 if (WARN_ON(!IS_ERR(s->context)))
156 case SNDRV_PCM_FORMAT_S16:
157 s->transfer_samples = amdtp_write_s16;
159 case SNDRV_PCM_FORMAT_S32:
160 s->transfer_samples = amdtp_write_s32;
164 EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
166 static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
168 unsigned int phase, data_blocks;
170 if (!cip_sfc_is_base_44100(s->sfc)) {
171 /* Sample_rate / 8000 is an integer, and precomputed. */
172 data_blocks = s->data_block_state;
174 phase = s->data_block_state;
177 * This calculates the number of data blocks per packet so that
178 * 1) the overall rate is correct and exactly synchronized to
180 * 2) packets with a rounded-up number of blocks occur as early
181 * as possible in the sequence (to prevent underruns of the
184 if (s->sfc == CIP_SFC_44100)
185 /* 6 6 5 6 5 6 5 ... */
186 data_blocks = 5 + ((phase & 1) ^
187 (phase == 0 || phase >= 40));
189 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
190 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
191 if (++phase >= (80 >> (s->sfc >> 1)))
193 s->data_block_state = phase;
199 static unsigned int calculate_syt(struct amdtp_out_stream *s,
202 unsigned int syt_offset, phase, index, syt;
204 if (s->last_syt_offset < TICKS_PER_CYCLE) {
205 if (!cip_sfc_is_base_44100(s->sfc))
206 syt_offset = s->last_syt_offset + s->syt_offset_state;
209 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
210 * n * SYT_INTERVAL * 24576000 / sample_rate
211 * Modulo TICKS_PER_CYCLE, the difference between successive
212 * elements is about 1386.23. Rounding the results of this
213 * formula to the SYT precision results in a sequence of
214 * differences that begins with:
215 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
216 * This code generates _exactly_ the same sequence.
218 phase = s->syt_offset_state;
220 syt_offset = s->last_syt_offset;
221 syt_offset += 1386 + ((index && !(index & 3)) ||
225 s->syt_offset_state = phase;
228 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
229 s->last_syt_offset = syt_offset;
231 if (syt_offset < TICKS_PER_CYCLE) {
232 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
233 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
234 syt += syt_offset % TICKS_PER_CYCLE;
238 return 0xffff; /* no info */
242 static void amdtp_write_s32(struct amdtp_out_stream *s,
243 struct snd_pcm_substream *pcm,
244 __be32 *buffer, unsigned int frames)
246 struct snd_pcm_runtime *runtime = pcm->runtime;
247 unsigned int channels, remaining_frames, frame_step, i, c;
250 channels = s->pcm_channels;
251 src = (void *)runtime->dma_area +
252 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
253 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
254 frame_step = s->data_block_quadlets - channels;
256 for (i = 0; i < frames; ++i) {
257 for (c = 0; c < channels; ++c) {
258 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
262 buffer += frame_step;
263 if (--remaining_frames == 0)
264 src = (void *)runtime->dma_area;
268 static void amdtp_write_s16(struct amdtp_out_stream *s,
269 struct snd_pcm_substream *pcm,
270 __be32 *buffer, unsigned int frames)
272 struct snd_pcm_runtime *runtime = pcm->runtime;
273 unsigned int channels, remaining_frames, frame_step, i, c;
276 channels = s->pcm_channels;
277 src = (void *)runtime->dma_area +
278 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
279 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
280 frame_step = s->data_block_quadlets - channels;
282 for (i = 0; i < frames; ++i) {
283 for (c = 0; c < channels; ++c) {
284 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
288 buffer += frame_step;
289 if (--remaining_frames == 0)
290 src = (void *)runtime->dma_area;
294 static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
295 __be32 *buffer, unsigned int frames)
299 for (i = 0; i < frames; ++i) {
300 for (c = 0; c < s->pcm_channels; ++c)
301 buffer[c] = cpu_to_be32(0x40000000);
302 buffer += s->data_block_quadlets;
306 static void amdtp_fill_midi(struct amdtp_out_stream *s,
307 __be32 *buffer, unsigned int frames)
311 for (i = 0; i < frames; ++i)
312 buffer[s->pcm_channels + i * s->data_block_quadlets] =
313 cpu_to_be32(0x80000000);
316 static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
319 unsigned int data_blocks, syt, ptr;
320 struct snd_pcm_substream *pcm;
321 struct fw_iso_packet packet;
324 data_blocks = calculate_data_blocks(s);
325 syt = calculate_syt(s, cycle);
327 buffer = s->buffer.packets[s->packet_counter].buffer;
328 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
329 (s->data_block_quadlets << 16) |
330 s->data_block_counter);
331 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
332 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
335 pcm = ACCESS_ONCE(s->pcm);
337 s->transfer_samples(s, pcm, buffer, data_blocks);
339 amdtp_fill_pcm_silence(s, buffer, data_blocks);
341 amdtp_fill_midi(s, buffer, data_blocks);
343 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
345 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
346 packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
349 packet.tag = TAG_CIP;
351 packet.header_length = 0;
353 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
354 s->buffer.packets[s->packet_counter].offset);
356 dev_err(&s->unit->device, "queueing error: %d\n", err);
358 if (++s->packet_counter >= QUEUE_LENGTH)
359 s->packet_counter = 0;
362 ptr = s->pcm_buffer_pointer + data_blocks;
363 if (ptr >= pcm->runtime->buffer_size)
364 ptr -= pcm->runtime->buffer_size;
365 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
367 s->pcm_period_pointer += data_blocks;
368 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
369 s->pcm_period_pointer -= pcm->runtime->period_size;
370 snd_pcm_period_elapsed(pcm);
375 static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
376 size_t header_length, void *header, void *data)
378 struct amdtp_out_stream *s = data;
379 unsigned int i, packets = header_length / 4;
382 * Compute the cycle of the last queued packet.
383 * (We need only the four lowest bits for the SYT, so we can ignore
384 * that bits 0-11 must wrap around at 3072.)
386 cycle += QUEUE_LENGTH - packets;
388 for (i = 0; i < packets; ++i)
389 queue_out_packet(s, ++cycle);
392 static int queue_initial_skip_packets(struct amdtp_out_stream *s)
394 struct fw_iso_packet skip_packet = {
400 for (i = 0; i < QUEUE_LENGTH; ++i) {
401 skip_packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
403 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
406 if (++s->packet_counter >= QUEUE_LENGTH)
407 s->packet_counter = 0;
414 * amdtp_out_stream_start - start sending packets
415 * @s: the AMDTP output stream to start
416 * @channel: the isochronous channel on the bus
417 * @speed: firewire speed code
419 * The stream cannot be started until it has been configured with
420 * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
421 * amdtp_out_stream_set_midi(); and it must be started before any
422 * PCM or MIDI device can be started.
424 int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
426 static const struct {
427 unsigned int data_block;
428 unsigned int syt_offset;
429 } initial_state[] = {
430 [CIP_SFC_32000] = { 4, 3072 },
431 [CIP_SFC_48000] = { 6, 1024 },
432 [CIP_SFC_96000] = { 12, 1024 },
433 [CIP_SFC_192000] = { 24, 1024 },
434 [CIP_SFC_44100] = { 0, 67 },
435 [CIP_SFC_88200] = { 0, 67 },
436 [CIP_SFC_176400] = { 0, 67 },
440 mutex_lock(&s->mutex);
442 if (WARN_ON(!IS_ERR(s->context) ||
443 (!s->pcm_channels && !s->midi_ports))) {
448 s->data_block_state = initial_state[s->sfc].data_block;
449 s->syt_offset_state = initial_state[s->sfc].syt_offset;
450 s->last_syt_offset = TICKS_PER_CYCLE;
452 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
453 amdtp_out_stream_get_max_payload(s),
458 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
459 FW_ISO_CONTEXT_TRANSMIT,
461 out_packet_callback, s);
462 if (IS_ERR(s->context)) {
463 err = PTR_ERR(s->context);
465 dev_err(&s->unit->device,
466 "no free output stream on this controller\n");
470 amdtp_out_stream_update(s);
472 s->packet_counter = 0;
473 s->data_block_counter = 0;
474 err = queue_initial_skip_packets(s);
478 err = fw_iso_context_start(s->context, -1, 0, 0);
482 mutex_unlock(&s->mutex);
487 fw_iso_context_destroy(s->context);
488 s->context = ERR_PTR(-1);
490 iso_packets_buffer_destroy(&s->buffer, s->unit);
492 mutex_unlock(&s->mutex);
496 EXPORT_SYMBOL(amdtp_out_stream_start);
499 * amdtp_out_stream_update - update the stream after a bus reset
500 * @s: the AMDTP output stream
502 void amdtp_out_stream_update(struct amdtp_out_stream *s)
504 ACCESS_ONCE(s->source_node_id_field) =
505 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
507 EXPORT_SYMBOL(amdtp_out_stream_update);
510 * amdtp_out_stream_stop - stop sending packets
511 * @s: the AMDTP output stream to stop
513 * All PCM and MIDI devices of the stream must be stopped before the stream
514 * itself can be stopped.
516 void amdtp_out_stream_stop(struct amdtp_out_stream *s)
518 mutex_lock(&s->mutex);
520 if (IS_ERR(s->context)) {
521 mutex_unlock(&s->mutex);
525 fw_iso_context_stop(s->context);
526 fw_iso_context_destroy(s->context);
527 s->context = ERR_PTR(-1);
528 iso_packets_buffer_destroy(&s->buffer, s->unit);
530 mutex_unlock(&s->mutex);
532 EXPORT_SYMBOL(amdtp_out_stream_stop);
535 * amdtp_out_stream_pcm_abort - abort the running PCM device
536 * @s: the AMDTP stream about to be stopped
538 * If the isochronous stream needs to be stopped asynchronously, call this
539 * function first to stop the PCM device.
541 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
543 struct snd_pcm_substream *pcm;
545 pcm = ACCESS_ONCE(s->pcm);
547 snd_pcm_stream_lock_irq(pcm);
548 if (snd_pcm_running(pcm))
549 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
550 snd_pcm_stream_unlock_irq(pcm);
553 EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);