ALSA: dice, firewire-lib: add blocking mode
[firewire-audio.git] / sound / firewire / amdtp.c
blobfb324fd8133c612b022456e9ea63217b32cf8069
1 /*
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.
7 */
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"
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 */
23 #define TAG_CIP 1
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 /**
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 s->unit = fw_unit_get(unit);
44 s->flags = flags;
45 s->context = ERR_PTR(-1);
46 mutex_init(&s->mutex);
47 s->packet_index = 0;
49 return 0;
51 EXPORT_SYMBOL(amdtp_out_stream_init);
53 /**
54 * amdtp_out_stream_destroy - free stream resources
55 * @s: the AMDTP output stream to destroy
57 void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
59 WARN_ON(!IS_ERR(s->context));
60 mutex_destroy(&s->mutex);
61 fw_unit_put(s->unit);
63 EXPORT_SYMBOL(amdtp_out_stream_destroy);
65 /**
66 * amdtp_out_stream_set_rate - set the sample rate
67 * @s: the AMDTP output stream to configure
68 * @rate: the sample rate
70 * The sample rate must be set before the stream is started, and must not be
71 * changed while the stream is running.
73 void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
75 static const struct {
76 unsigned int rate;
77 unsigned int syt_interval;
78 } rate_info[] = {
79 [CIP_SFC_32000] = { 32000, 8, },
80 [CIP_SFC_44100] = { 44100, 8, },
81 [CIP_SFC_48000] = { 48000, 8, },
82 [CIP_SFC_88200] = { 88200, 16, },
83 [CIP_SFC_96000] = { 96000, 16, },
84 [CIP_SFC_176400] = { 176400, 32, },
85 [CIP_SFC_192000] = { 192000, 32, },
87 unsigned int sfc;
89 if (WARN_ON(!IS_ERR(s->context)))
90 return;
92 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
93 if (rate_info[sfc].rate == rate)
94 goto sfc_found;
95 WARN_ON(1);
96 return;
98 sfc_found:
99 s->sfc = sfc;
100 s->syt_interval = rate_info[sfc].syt_interval;
102 /* default buffering in the device */
103 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
104 if (s->flags & CIP_BLOCKING)
105 /* additional buffering needed to adjust for no-data packets */
106 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
108 EXPORT_SYMBOL(amdtp_out_stream_set_rate);
111 * amdtp_out_stream_get_max_payload - get the stream's packet size
112 * @s: the AMDTP output stream
114 * This function must not be called before the stream has been configured
115 * with amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(), and
116 * amdtp_out_stream_set_midi().
118 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
120 s->data_block_quadlets = s->pcm_channels;
121 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
123 return 8 + s->syt_interval * s->data_block_quadlets * 4;
125 EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
127 static void amdtp_write_s16(struct amdtp_out_stream *s,
128 struct snd_pcm_substream *pcm,
129 __be32 *buffer, unsigned int frames);
130 static void amdtp_write_s32(struct amdtp_out_stream *s,
131 struct snd_pcm_substream *pcm,
132 __be32 *buffer, unsigned int frames);
135 * amdtp_out_stream_set_pcm_format - set the PCM format
136 * @s: the AMDTP output stream to configure
137 * @format: the format of the ALSA PCM device
139 * The sample format must be set before the stream is started, and must not be
140 * changed while the stream is running.
142 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
143 snd_pcm_format_t format)
145 if (WARN_ON(!IS_ERR(s->context)))
146 return;
148 switch (format) {
149 default:
150 WARN_ON(1);
151 /* fall through */
152 case SNDRV_PCM_FORMAT_S16:
153 s->transfer_samples = amdtp_write_s16;
154 break;
155 case SNDRV_PCM_FORMAT_S32:
156 s->transfer_samples = amdtp_write_s32;
157 break;
160 EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
162 static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
164 unsigned int phase, data_blocks;
166 if (!cip_sfc_is_base_44100(s->sfc)) {
167 /* Sample_rate / 8000 is an integer, and precomputed. */
168 data_blocks = s->data_block_state;
169 } else {
170 phase = s->data_block_state;
173 * This calculates the number of data blocks per packet so that
174 * 1) the overall rate is correct and exactly synchronized to
175 * the bus clock, and
176 * 2) packets with a rounded-up number of blocks occur as early
177 * as possible in the sequence (to prevent underruns of the
178 * device's buffer).
180 if (s->sfc == CIP_SFC_44100)
181 /* 6 6 5 6 5 6 5 ... */
182 data_blocks = 5 + ((phase & 1) ^
183 (phase == 0 || phase >= 40));
184 else
185 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
186 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
187 if (++phase >= (80 >> (s->sfc >> 1)))
188 phase = 0;
189 s->data_block_state = phase;
192 return data_blocks;
195 static unsigned int calculate_syt(struct amdtp_out_stream *s,
196 unsigned int cycle)
198 unsigned int syt_offset, phase, index, syt;
200 if (s->last_syt_offset < TICKS_PER_CYCLE) {
201 if (!cip_sfc_is_base_44100(s->sfc))
202 syt_offset = s->last_syt_offset + s->syt_offset_state;
203 else {
205 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
206 * n * SYT_INTERVAL * 24576000 / sample_rate
207 * Modulo TICKS_PER_CYCLE, the difference between successive
208 * elements is about 1386.23. Rounding the results of this
209 * formula to the SYT precision results in a sequence of
210 * differences that begins with:
211 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
212 * This code generates _exactly_ the same sequence.
214 phase = s->syt_offset_state;
215 index = phase % 13;
216 syt_offset = s->last_syt_offset;
217 syt_offset += 1386 + ((index && !(index & 3)) ||
218 phase == 146);
219 if (++phase >= 147)
220 phase = 0;
221 s->syt_offset_state = phase;
223 } else
224 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
225 s->last_syt_offset = syt_offset;
227 if (syt_offset < TICKS_PER_CYCLE) {
228 syt_offset += s->transfer_delay;
229 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
230 syt += syt_offset % TICKS_PER_CYCLE;
232 return syt & 0xffff;
233 } else {
234 return 0xffff; /* no info */
238 static void amdtp_write_s32(struct amdtp_out_stream *s,
239 struct snd_pcm_substream *pcm,
240 __be32 *buffer, unsigned int frames)
242 struct snd_pcm_runtime *runtime = pcm->runtime;
243 unsigned int channels, remaining_frames, frame_step, i, c;
244 const u32 *src;
246 channels = s->pcm_channels;
247 src = (void *)runtime->dma_area +
248 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
249 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
250 frame_step = s->data_block_quadlets - channels;
252 for (i = 0; i < frames; ++i) {
253 for (c = 0; c < channels; ++c) {
254 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
255 src++;
256 buffer++;
258 buffer += frame_step;
259 if (--remaining_frames == 0)
260 src = (void *)runtime->dma_area;
264 static void amdtp_write_s16(struct amdtp_out_stream *s,
265 struct snd_pcm_substream *pcm,
266 __be32 *buffer, unsigned int frames)
268 struct snd_pcm_runtime *runtime = pcm->runtime;
269 unsigned int channels, remaining_frames, frame_step, i, c;
270 const u16 *src;
272 channels = s->pcm_channels;
273 src = (void *)runtime->dma_area +
274 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
275 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
276 frame_step = s->data_block_quadlets - channels;
278 for (i = 0; i < frames; ++i) {
279 for (c = 0; c < channels; ++c) {
280 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
281 src++;
282 buffer++;
284 buffer += frame_step;
285 if (--remaining_frames == 0)
286 src = (void *)runtime->dma_area;
290 static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
291 __be32 *buffer, unsigned int frames)
293 unsigned int i, c;
295 for (i = 0; i < frames; ++i) {
296 for (c = 0; c < s->pcm_channels; ++c)
297 buffer[c] = cpu_to_be32(0x40000000);
298 buffer += s->data_block_quadlets;
302 static void amdtp_fill_midi(struct amdtp_out_stream *s,
303 __be32 *buffer, unsigned int frames)
305 unsigned int i;
307 for (i = 0; i < frames; ++i)
308 buffer[s->pcm_channels + i * s->data_block_quadlets] =
309 cpu_to_be32(0x80000000);
312 static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
314 __be32 *buffer;
315 unsigned int index, data_blocks, syt, ptr;
316 struct snd_pcm_substream *pcm;
317 struct fw_iso_packet packet;
318 int err;
320 if (s->packet_index < 0)
321 return;
322 index = s->packet_index;
324 syt = calculate_syt(s, cycle);
325 if (!(s->flags & CIP_BLOCKING)) {
326 data_blocks = calculate_data_blocks(s);
327 } else {
328 if (syt != 0xffff) {
329 data_blocks = s->syt_interval;
330 } else {
331 data_blocks = 0;
332 syt = 0xffffff;
336 buffer = s->buffer.packets[index].buffer;
337 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
338 (s->data_block_quadlets << 16) |
339 s->data_block_counter);
340 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
341 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
342 buffer += 2;
344 pcm = ACCESS_ONCE(s->pcm);
345 if (pcm)
346 s->transfer_samples(s, pcm, buffer, data_blocks);
347 else
348 amdtp_fill_pcm_silence(s, buffer, data_blocks);
349 if (s->midi_ports)
350 amdtp_fill_midi(s, buffer, data_blocks);
352 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
354 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
355 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
356 packet.skip = 0;
357 packet.tag = TAG_CIP;
358 packet.sy = 0;
359 packet.header_length = 0;
361 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
362 s->buffer.packets[index].offset);
363 if (err < 0) {
364 dev_err(&s->unit->device, "queueing error: %d\n", err);
365 s->packet_index = -1;
366 amdtp_out_stream_pcm_abort(s);
367 return;
370 if (++index >= QUEUE_LENGTH)
371 index = 0;
372 s->packet_index = index;
374 if (pcm) {
375 ptr = s->pcm_buffer_pointer + data_blocks;
376 if (ptr >= pcm->runtime->buffer_size)
377 ptr -= pcm->runtime->buffer_size;
378 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
380 s->pcm_period_pointer += data_blocks;
381 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
382 s->pcm_period_pointer -= pcm->runtime->period_size;
383 snd_pcm_period_elapsed(pcm);
388 static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
389 size_t header_length, void *header, void *data)
391 struct amdtp_out_stream *s = data;
392 unsigned int i, packets = header_length / 4;
395 * Compute the cycle of the last queued packet.
396 * (We need only the four lowest bits for the SYT, so we can ignore
397 * that bits 0-11 must wrap around at 3072.)
399 cycle += QUEUE_LENGTH - packets;
401 for (i = 0; i < packets; ++i)
402 queue_out_packet(s, ++cycle);
405 static int queue_initial_skip_packets(struct amdtp_out_stream *s)
407 struct fw_iso_packet skip_packet = {
408 .skip = 1,
410 unsigned int i;
411 int err;
413 for (i = 0; i < QUEUE_LENGTH; ++i) {
414 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
415 INTERRUPT_INTERVAL);
416 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
417 if (err < 0)
418 return err;
419 if (++s->packet_index >= QUEUE_LENGTH)
420 s->packet_index = 0;
423 return 0;
427 * amdtp_out_stream_start - start sending packets
428 * @s: the AMDTP output stream to start
429 * @channel: the isochronous channel on the bus
430 * @speed: firewire speed code
432 * The stream cannot be started until it has been configured with
433 * amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(),
434 * amdtp_out_stream_set_midi(), and amdtp_out_stream_set_format();
435 * and it must be started before any PCM or MIDI device can be started.
437 int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
439 static const struct {
440 unsigned int data_block;
441 unsigned int syt_offset;
442 } initial_state[] = {
443 [CIP_SFC_32000] = { 4, 3072 },
444 [CIP_SFC_48000] = { 6, 1024 },
445 [CIP_SFC_96000] = { 12, 1024 },
446 [CIP_SFC_192000] = { 24, 1024 },
447 [CIP_SFC_44100] = { 0, 67 },
448 [CIP_SFC_88200] = { 0, 67 },
449 [CIP_SFC_176400] = { 0, 67 },
451 int err;
453 mutex_lock(&s->mutex);
455 if (WARN_ON(!IS_ERR(s->context) ||
456 (!s->pcm_channels && !s->midi_ports))) {
457 err = -EBADFD;
458 goto err_unlock;
461 s->data_block_state = initial_state[s->sfc].data_block;
462 s->syt_offset_state = initial_state[s->sfc].syt_offset;
463 s->last_syt_offset = TICKS_PER_CYCLE;
465 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
466 amdtp_out_stream_get_max_payload(s),
467 DMA_TO_DEVICE);
468 if (err < 0)
469 goto err_unlock;
471 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
472 FW_ISO_CONTEXT_TRANSMIT,
473 channel, speed, 0,
474 out_packet_callback, s);
475 if (IS_ERR(s->context)) {
476 err = PTR_ERR(s->context);
477 if (err == -EBUSY)
478 dev_err(&s->unit->device,
479 "no free output stream on this controller\n");
480 goto err_buffer;
483 amdtp_out_stream_update(s);
485 s->packet_index = 0;
486 s->data_block_counter = 0;
487 err = queue_initial_skip_packets(s);
488 if (err < 0)
489 goto err_context;
491 err = fw_iso_context_start(s->context, -1, 0, 0);
492 if (err < 0)
493 goto err_context;
495 mutex_unlock(&s->mutex);
497 return 0;
499 err_context:
500 fw_iso_context_destroy(s->context);
501 s->context = ERR_PTR(-1);
502 err_buffer:
503 iso_packets_buffer_destroy(&s->buffer, s->unit);
504 err_unlock:
505 mutex_unlock(&s->mutex);
507 return err;
509 EXPORT_SYMBOL(amdtp_out_stream_start);
512 * amdtp_out_stream_update - update the stream after a bus reset
513 * @s: the AMDTP output stream
515 void amdtp_out_stream_update(struct amdtp_out_stream *s)
517 ACCESS_ONCE(s->source_node_id_field) =
518 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
520 EXPORT_SYMBOL(amdtp_out_stream_update);
523 * amdtp_out_stream_stop - stop sending packets
524 * @s: the AMDTP output stream to stop
526 * All PCM and MIDI devices of the stream must be stopped before the stream
527 * itself can be stopped.
529 void amdtp_out_stream_stop(struct amdtp_out_stream *s)
531 mutex_lock(&s->mutex);
533 if (IS_ERR(s->context)) {
534 mutex_unlock(&s->mutex);
535 return;
538 fw_iso_context_stop(s->context);
539 fw_iso_context_destroy(s->context);
540 s->context = ERR_PTR(-1);
541 iso_packets_buffer_destroy(&s->buffer, s->unit);
543 mutex_unlock(&s->mutex);
545 EXPORT_SYMBOL(amdtp_out_stream_stop);
548 * amdtp_out_stream_pcm_abort - abort the running PCM device
549 * @s: the AMDTP stream about to be stopped
551 * If the isochronous stream needs to be stopped asynchronously, call this
552 * function first to stop the PCM device.
554 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
556 struct snd_pcm_substream *pcm;
558 pcm = ACCESS_ONCE(s->pcm);
559 if (pcm) {
560 snd_pcm_stream_lock_irq(pcm);
561 if (snd_pcm_running(pcm))
562 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
563 snd_pcm_stream_unlock_irq(pcm);
566 EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);