blob: 09f70ee4d04a572a10d493a08906f5a36a6ab256 [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
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
39 */
40int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
41 enum cip_out_flags flags)
42{
43 if (flags != CIP_NONBLOCKING)
44 return -EINVAL;
45
46 s->unit = fw_unit_get(unit);
47 s->flags = flags;
48 s->context = ERR_PTR(-1);
49 mutex_init(&s->mutex);
50
51 return 0;
52}
53EXPORT_SYMBOL(amdtp_out_stream_init);
54
55/**
56 * amdtp_out_stream_destroy - free stream resources
57 * @s: the AMDTP output stream to destroy
58 */
59void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
60{
61 WARN_ON(!IS_ERR(s->context));
62 mutex_destroy(&s->mutex);
63 fw_unit_put(s->unit);
64}
65EXPORT_SYMBOL(amdtp_out_stream_destroy);
66
67/**
68 * amdtp_out_stream_set_rate - set the sample rate
69 * @s: the AMDTP output stream to configure
70 * @rate: the sample rate
71 *
72 * The sample rate must be set before the stream is started, and must not be
73 * changed while the stream is running.
74 */
75void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
76{
77 static const struct {
78 unsigned int rate;
79 unsigned int syt_interval;
80 } rate_info[] = {
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, },
88 };
89 unsigned int sfc;
90
91 if (WARN_ON(!IS_ERR(s->context)))
92 return;
93
94 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
95 if (rate_info[sfc].rate == rate) {
96 s->sfc = sfc;
97 s->syt_interval = rate_info[sfc].syt_interval;
98 return;
99 }
100 WARN_ON(1);
101}
102EXPORT_SYMBOL(amdtp_out_stream_set_rate);
103
104/**
105 * amdtp_out_stream_get_max_payload - get the stream's packet size
106 * @s: the AMDTP output stream
107 *
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().
111 */
112unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
113{
114 static const unsigned int max_data_blocks[] = {
115 [CIP_SFC_32000] = 4,
116 [CIP_SFC_44100] = 6,
117 [CIP_SFC_48000] = 6,
118 [CIP_SFC_88200] = 12,
119 [CIP_SFC_96000] = 12,
120 [CIP_SFC_176400] = 23,
121 [CIP_SFC_192000] = 24,
122 };
123
124 s->data_block_quadlets = s->pcm_channels;
125 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
126
127 return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
128}
129EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
130
131static void amdtp_write_s16(struct amdtp_out_stream *s,
132 struct snd_pcm_substream *pcm,
133 __be32 *buffer, unsigned int frames);
134static void amdtp_write_s32(struct amdtp_out_stream *s,
135 struct snd_pcm_substream *pcm,
136 __be32 *buffer, unsigned int frames);
137
138/**
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
142 *
143 * The sample format must be set before the stream is started, and must not be
144 * changed while the stream is running.
145 */
146void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
147 snd_pcm_format_t format)
148{
149 if (WARN_ON(!IS_ERR(s->context)))
150 return;
151
152 switch (format) {
153 default:
154 WARN_ON(1);
155 /* fall through */
156 case SNDRV_PCM_FORMAT_S16:
157 s->transfer_samples = amdtp_write_s16;
158 break;
159 case SNDRV_PCM_FORMAT_S32:
160 s->transfer_samples = amdtp_write_s32;
161 break;
162 }
163}
164EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
165
166static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
167{
168 unsigned int phase, data_blocks;
169
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;
173 } else {
174 phase = s->data_block_state;
175
176 /*
177 * This calculates the number of data blocks per packet so that
178 * 1) the overall rate is correct and exactly synchronized to
179 * the bus clock, and
180 * 2) packets with a rounded-up number of blocks occur as early
181 * as possible in the sequence (to prevent underruns of the
182 * device's buffer).
183 */
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));
188 else
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)))
192 phase = 0;
193 s->data_block_state = phase;
194 }
195
196 return data_blocks;
197}
198
199static unsigned int calculate_syt(struct amdtp_out_stream *s,
200 unsigned int cycle)
201{
202 unsigned int syt_offset, phase, index, syt;
203
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;
207 else {
208 /*
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.
217 */
218 phase = s->syt_offset_state;
219 index = phase % 13;
220 syt_offset = s->last_syt_offset;
221 syt_offset += 1386 + ((index && !(index & 3)) ||
222 phase == 146);
223 if (++phase >= 147)
224 phase = 0;
225 s->syt_offset_state = phase;
226 }
227 } else
228 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
229 s->last_syt_offset = syt_offset;
230
231 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
232 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
233 syt += syt_offset % TICKS_PER_CYCLE;
234
235 return syt & 0xffff;
236}
237
238static void amdtp_write_s32(struct amdtp_out_stream *s,
239 struct snd_pcm_substream *pcm,
240 __be32 *buffer, unsigned int frames)
241{
242 struct snd_pcm_runtime *runtime = pcm->runtime;
243 unsigned int channels, remaining_frames, frame_step, i, c;
244 const u32 *src;
245
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;
251
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++;
257 }
258 buffer += frame_step;
259 if (--remaining_frames == 0)
260 src = (void *)runtime->dma_area;
261 }
262}
263
264static void amdtp_write_s16(struct amdtp_out_stream *s,
265 struct snd_pcm_substream *pcm,
266 __be32 *buffer, unsigned int frames)
267{
268 struct snd_pcm_runtime *runtime = pcm->runtime;
269 unsigned int channels, remaining_frames, frame_step, i, c;
270 const u16 *src;
271
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;
277
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++;
283 }
284 buffer += frame_step;
285 if (--remaining_frames == 0)
286 src = (void *)runtime->dma_area;
287 }
288}
289
290static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
291 __be32 *buffer, unsigned int frames)
292{
293 unsigned int i, c;
294
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;
299 }
300}
301
302static void amdtp_fill_midi(struct amdtp_out_stream *s,
303 __be32 *buffer, unsigned int frames)
304{
305 unsigned int i;
306
307 for (i = 0; i < frames; ++i)
308 buffer[s->pcm_channels + i * s->data_block_quadlets] =
309 cpu_to_be32(0x80000000);
310}
311
312static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
313{
314 __be32 *buffer;
315 unsigned int data_blocks, syt, ptr;
316 struct snd_pcm_substream *pcm;
317 struct fw_iso_packet packet;
318 int err;
319
320 data_blocks = calculate_data_blocks(s);
321 syt = calculate_syt(s, cycle);
322
323 buffer = s->buffer.packets[s->packet_counter].buffer;
324 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
325 (s->data_block_quadlets << 16) |
326 s->data_block_counter);
327 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
328 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
329 buffer += 2;
330
331 pcm = ACCESS_ONCE(s->pcm);
332 if (pcm)
333 s->transfer_samples(s, pcm, buffer, data_blocks);
334 else
335 amdtp_fill_pcm_silence(s, buffer, data_blocks);
336 if (s->midi_ports)
337 amdtp_fill_midi(s, buffer, data_blocks);
338
339 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
340
341 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
342 packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
343 INTERRUPT_INTERVAL);
344 packet.skip = 0;
345 packet.tag = TAG_CIP;
346 packet.sy = 0;
347 packet.header_length = 0;
348
349 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
350 s->buffer.packets[s->packet_counter].offset);
351 if (err < 0)
352 dev_err(&s->unit->device, "queueing error: %d\n", err);
353
354 if (++s->packet_counter >= QUEUE_LENGTH)
355 s->packet_counter = 0;
356
357 if (pcm) {
358 ptr = s->pcm_buffer_pointer + data_blocks;
359 if (ptr >= pcm->runtime->buffer_size)
360 ptr -= pcm->runtime->buffer_size;
361 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
362
363 s->pcm_period_pointer += data_blocks;
364 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
365 s->pcm_period_pointer -= pcm->runtime->period_size;
366 snd_pcm_period_elapsed(pcm);
367 }
368 }
369}
370
371static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
372 size_t header_length, void *header, void *data)
373{
374 struct amdtp_out_stream *s = data;
375 unsigned int i, packets = header_length / 4;
376
377 /*
378 * Compute the cycle of the last queued packet.
379 * (We need only the four lowest bits for the SYT, so we can ignore
380 * that bits 0-11 must wrap around at 3072.)
381 */
382 cycle += QUEUE_LENGTH - packets;
383
384 for (i = 0; i < packets; ++i)
385 queue_out_packet(s, ++cycle);
386}
387
388static int queue_initial_skip_packets(struct amdtp_out_stream *s)
389{
390 struct fw_iso_packet skip_packet = {
391 .skip = 1,
392 };
393 unsigned int i;
394 int err;
395
396 for (i = 0; i < QUEUE_LENGTH; ++i) {
397 skip_packet.interrupt = IS_ALIGNED(s->packet_counter + 1,
398 INTERRUPT_INTERVAL);
399 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
400 if (err < 0)
401 return err;
402 if (++s->packet_counter >= QUEUE_LENGTH)
403 s->packet_counter = 0;
404 }
405
406 return 0;
407}
408
409/**
410 * amdtp_out_stream_start - start sending packets
411 * @s: the AMDTP output stream to start
412 * @channel: the isochronous channel on the bus
413 * @speed: firewire speed code
414 *
415 * The stream cannot be started until it has been configured with
416 * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
417 * amdtp_out_stream_set_midi(); and it must be started before any
418 * PCM or MIDI device can be started.
419 */
420int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
421{
422 static const struct {
423 unsigned int data_block;
424 unsigned int syt_offset;
425 } initial_state[] = {
426 [CIP_SFC_32000] = { 4, 3072 },
427 [CIP_SFC_48000] = { 6, 1024 },
428 [CIP_SFC_96000] = { 12, 1024 },
429 [CIP_SFC_192000] = { 24, 1024 },
430 [CIP_SFC_44100] = { 0, 67 },
431 [CIP_SFC_88200] = { 0, 67 },
432 [CIP_SFC_176400] = { 0, 67 },
433 };
434 int err;
435
436 mutex_lock(&s->mutex);
437
438 if (WARN_ON(!IS_ERR(s->context) ||
439 (!s->pcm_channels && !s->midi_ports))) {
440 err = -EBADFD;
441 goto err_unlock;
442 }
443
444 s->data_block_state = initial_state[s->sfc].data_block;
445 s->syt_offset_state = initial_state[s->sfc].syt_offset;
446 s->last_syt_offset = TICKS_PER_CYCLE;
447
448 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
449 amdtp_out_stream_get_max_payload(s),
450 DMA_TO_DEVICE);
451 if (err < 0)
452 goto err_unlock;
453
454 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
455 FW_ISO_CONTEXT_TRANSMIT,
456 channel, speed, 0,
457 out_packet_callback, s);
458 if (IS_ERR(s->context)) {
459 err = PTR_ERR(s->context);
460 if (err == -EBUSY)
461 dev_err(&s->unit->device,
462 "no free output stream on this controller\n");
463 goto err_buffer;
464 }
465
466 amdtp_out_stream_update(s);
467
468 s->packet_counter = 0;
469 s->data_block_counter = 0;
470 err = queue_initial_skip_packets(s);
471 if (err < 0)
472 goto err_context;
473
474 err = fw_iso_context_start(s->context, -1, 0, 0);
475 if (err < 0)
476 goto err_context;
477
478 mutex_unlock(&s->mutex);
479
480 return 0;
481
482err_context:
483 fw_iso_context_destroy(s->context);
484 s->context = ERR_PTR(-1);
485err_buffer:
486 iso_packets_buffer_destroy(&s->buffer, s->unit);
487err_unlock:
488 mutex_unlock(&s->mutex);
489
490 return err;
491}
492EXPORT_SYMBOL(amdtp_out_stream_start);
493
494/**
495 * amdtp_out_stream_update - update the stream after a bus reset
496 * @s: the AMDTP output stream
497 */
498void amdtp_out_stream_update(struct amdtp_out_stream *s)
499{
500 ACCESS_ONCE(s->source_node_id_field) =
501 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
502}
503EXPORT_SYMBOL(amdtp_out_stream_update);
504
505/**
506 * amdtp_out_stream_stop - stop sending packets
507 * @s: the AMDTP output stream to stop
508 *
509 * All PCM and MIDI devices of the stream must be stopped before the stream
510 * itself can be stopped.
511 */
512void amdtp_out_stream_stop(struct amdtp_out_stream *s)
513{
514 mutex_lock(&s->mutex);
515
516 if (IS_ERR(s->context)) {
517 mutex_unlock(&s->mutex);
518 return;
519 }
520
521 fw_iso_context_stop(s->context);
522 fw_iso_context_destroy(s->context);
523 s->context = ERR_PTR(-1);
524 iso_packets_buffer_destroy(&s->buffer, s->unit);
525
526 mutex_unlock(&s->mutex);
527}
528EXPORT_SYMBOL(amdtp_out_stream_stop);
529
530/**
531 * amdtp_out_stream_pcm_abort - abort the running PCM device
532 * @s: the AMDTP stream about to be stopped
533 *
534 * If the isochronous stream needs to be stopped asynchronously, call this
535 * function first to stop the PCM device.
536 */
537void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
538{
539 struct snd_pcm_substream *pcm;
540
541 pcm = ACCESS_ONCE(s->pcm);
542 if (pcm) {
543 snd_pcm_stream_lock_irq(pcm);
544 if (snd_pcm_running(pcm))
545 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
546 snd_pcm_stream_unlock_irq(pcm);
547 }
548}
549EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);