Loading...
1/*
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
34static void pcm_period_tasklet(unsigned long data);
35
36/**
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
41 */
42int amdtp_out_stream_init(struct amdtp_out_stream *s, struct fw_unit *unit,
43 enum cip_out_flags flags)
44{
45 s->unit = fw_unit_get(unit);
46 s->flags = flags;
47 s->context = ERR_PTR(-1);
48 mutex_init(&s->mutex);
49 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
50 s->packet_index = 0;
51
52 return 0;
53}
54EXPORT_SYMBOL(amdtp_out_stream_init);
55
56/**
57 * amdtp_out_stream_destroy - free stream resources
58 * @s: the AMDTP output stream to destroy
59 */
60void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
61{
62 WARN_ON(amdtp_out_stream_running(s));
63 mutex_destroy(&s->mutex);
64 fw_unit_put(s->unit);
65}
66EXPORT_SYMBOL(amdtp_out_stream_destroy);
67
68const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
69 [CIP_SFC_32000] = 8,
70 [CIP_SFC_44100] = 8,
71 [CIP_SFC_48000] = 8,
72 [CIP_SFC_88200] = 16,
73 [CIP_SFC_96000] = 16,
74 [CIP_SFC_176400] = 32,
75 [CIP_SFC_192000] = 32,
76};
77EXPORT_SYMBOL(amdtp_syt_intervals);
78
79/**
80 * amdtp_out_stream_set_parameters - set stream parameters
81 * @s: the AMDTP output stream to configure
82 * @rate: the sample rate
83 * @pcm_channels: the number of PCM samples in each data block, to be encoded
84 * as AM824 multi-bit linear audio
85 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
86 *
87 * The parameters must be set before the stream is started, and must not be
88 * changed while the stream is running.
89 */
90void amdtp_out_stream_set_parameters(struct amdtp_out_stream *s,
91 unsigned int rate,
92 unsigned int pcm_channels,
93 unsigned int midi_ports)
94{
95 static const unsigned int rates[] = {
96 [CIP_SFC_32000] = 32000,
97 [CIP_SFC_44100] = 44100,
98 [CIP_SFC_48000] = 48000,
99 [CIP_SFC_88200] = 88200,
100 [CIP_SFC_96000] = 96000,
101 [CIP_SFC_176400] = 176400,
102 [CIP_SFC_192000] = 192000,
103 };
104 unsigned int sfc;
105
106 if (WARN_ON(amdtp_out_stream_running(s)))
107 return;
108
109 for (sfc = 0; sfc < CIP_SFC_COUNT; ++sfc)
110 if (rates[sfc] == rate)
111 goto sfc_found;
112 WARN_ON(1);
113 return;
114
115sfc_found:
116 s->dual_wire = (s->flags & CIP_HI_DUALWIRE) && sfc > CIP_SFC_96000;
117 if (s->dual_wire) {
118 sfc -= 2;
119 rate /= 2;
120 pcm_channels *= 2;
121 }
122 s->sfc = sfc;
123 s->data_block_quadlets = pcm_channels + DIV_ROUND_UP(midi_ports, 8);
124 s->pcm_channels = pcm_channels;
125 s->midi_ports = midi_ports;
126
127 s->syt_interval = amdtp_syt_intervals[sfc];
128
129 /* default buffering in the device */
130 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
131 if (s->flags & CIP_BLOCKING)
132 /* additional buffering needed to adjust for no-data packets */
133 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
134}
135EXPORT_SYMBOL(amdtp_out_stream_set_parameters);
136
137/**
138 * amdtp_out_stream_get_max_payload - get the stream's packet size
139 * @s: the AMDTP output stream
140 *
141 * This function must not be called before the stream has been configured
142 * with amdtp_out_stream_set_parameters().
143 */
144unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
145{
146 return 8 + s->syt_interval * s->data_block_quadlets * 4;
147}
148EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
149
150static void amdtp_write_s16(struct amdtp_out_stream *s,
151 struct snd_pcm_substream *pcm,
152 __be32 *buffer, unsigned int frames);
153static void amdtp_write_s32(struct amdtp_out_stream *s,
154 struct snd_pcm_substream *pcm,
155 __be32 *buffer, unsigned int frames);
156static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s,
157 struct snd_pcm_substream *pcm,
158 __be32 *buffer, unsigned int frames);
159static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s,
160 struct snd_pcm_substream *pcm,
161 __be32 *buffer, unsigned int frames);
162
163/**
164 * amdtp_out_stream_set_pcm_format - set the PCM format
165 * @s: the AMDTP output stream to configure
166 * @format: the format of the ALSA PCM device
167 *
168 * The sample format must be set after the other paramters (rate/PCM channels/
169 * MIDI) and before the stream is started, and must not be changed while the
170 * stream is running.
171 */
172void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
173 snd_pcm_format_t format)
174{
175 if (WARN_ON(amdtp_out_stream_running(s)))
176 return;
177
178 switch (format) {
179 default:
180 WARN_ON(1);
181 /* fall through */
182 case SNDRV_PCM_FORMAT_S16:
183 if (s->dual_wire)
184 s->transfer_samples = amdtp_write_s16_dualwire;
185 else
186 s->transfer_samples = amdtp_write_s16;
187 break;
188 case SNDRV_PCM_FORMAT_S32:
189 if (s->dual_wire)
190 s->transfer_samples = amdtp_write_s32_dualwire;
191 else
192 s->transfer_samples = amdtp_write_s32;
193 break;
194 }
195}
196EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
197
198/**
199 * amdtp_out_stream_pcm_prepare - prepare PCM device for running
200 * @s: the AMDTP output stream
201 *
202 * This function should be called from the PCM device's .prepare callback.
203 */
204void amdtp_out_stream_pcm_prepare(struct amdtp_out_stream *s)
205{
206 tasklet_kill(&s->period_tasklet);
207 s->pcm_buffer_pointer = 0;
208 s->pcm_period_pointer = 0;
209 s->pointer_flush = true;
210}
211EXPORT_SYMBOL(amdtp_out_stream_pcm_prepare);
212
213static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
214{
215 unsigned int phase, data_blocks;
216
217 if (!cip_sfc_is_base_44100(s->sfc)) {
218 /* Sample_rate / 8000 is an integer, and precomputed. */
219 data_blocks = s->data_block_state;
220 } else {
221 phase = s->data_block_state;
222
223 /*
224 * This calculates the number of data blocks per packet so that
225 * 1) the overall rate is correct and exactly synchronized to
226 * the bus clock, and
227 * 2) packets with a rounded-up number of blocks occur as early
228 * as possible in the sequence (to prevent underruns of the
229 * device's buffer).
230 */
231 if (s->sfc == CIP_SFC_44100)
232 /* 6 6 5 6 5 6 5 ... */
233 data_blocks = 5 + ((phase & 1) ^
234 (phase == 0 || phase >= 40));
235 else
236 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
237 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
238 if (++phase >= (80 >> (s->sfc >> 1)))
239 phase = 0;
240 s->data_block_state = phase;
241 }
242
243 return data_blocks;
244}
245
246static unsigned int calculate_syt(struct amdtp_out_stream *s,
247 unsigned int cycle)
248{
249 unsigned int syt_offset, phase, index, syt;
250
251 if (s->last_syt_offset < TICKS_PER_CYCLE) {
252 if (!cip_sfc_is_base_44100(s->sfc))
253 syt_offset = s->last_syt_offset + s->syt_offset_state;
254 else {
255 /*
256 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
257 * n * SYT_INTERVAL * 24576000 / sample_rate
258 * Modulo TICKS_PER_CYCLE, the difference between successive
259 * elements is about 1386.23. Rounding the results of this
260 * formula to the SYT precision results in a sequence of
261 * differences that begins with:
262 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
263 * This code generates _exactly_ the same sequence.
264 */
265 phase = s->syt_offset_state;
266 index = phase % 13;
267 syt_offset = s->last_syt_offset;
268 syt_offset += 1386 + ((index && !(index & 3)) ||
269 phase == 146);
270 if (++phase >= 147)
271 phase = 0;
272 s->syt_offset_state = phase;
273 }
274 } else
275 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
276 s->last_syt_offset = syt_offset;
277
278 if (syt_offset < TICKS_PER_CYCLE) {
279 syt_offset += s->transfer_delay;
280 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
281 syt += syt_offset % TICKS_PER_CYCLE;
282
283 return syt & 0xffff;
284 } else {
285 return 0xffff; /* no info */
286 }
287}
288
289static void amdtp_write_s32(struct amdtp_out_stream *s,
290 struct snd_pcm_substream *pcm,
291 __be32 *buffer, unsigned int frames)
292{
293 struct snd_pcm_runtime *runtime = pcm->runtime;
294 unsigned int channels, remaining_frames, frame_step, i, c;
295 const u32 *src;
296
297 channels = s->pcm_channels;
298 src = (void *)runtime->dma_area +
299 frames_to_bytes(runtime, s->pcm_buffer_pointer);
300 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
301 frame_step = s->data_block_quadlets - channels;
302
303 for (i = 0; i < frames; ++i) {
304 for (c = 0; c < channels; ++c) {
305 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
306 src++;
307 buffer++;
308 }
309 buffer += frame_step;
310 if (--remaining_frames == 0)
311 src = (void *)runtime->dma_area;
312 }
313}
314
315static void amdtp_write_s16(struct amdtp_out_stream *s,
316 struct snd_pcm_substream *pcm,
317 __be32 *buffer, unsigned int frames)
318{
319 struct snd_pcm_runtime *runtime = pcm->runtime;
320 unsigned int channels, remaining_frames, frame_step, i, c;
321 const u16 *src;
322
323 channels = s->pcm_channels;
324 src = (void *)runtime->dma_area +
325 frames_to_bytes(runtime, s->pcm_buffer_pointer);
326 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
327 frame_step = s->data_block_quadlets - channels;
328
329 for (i = 0; i < frames; ++i) {
330 for (c = 0; c < channels; ++c) {
331 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
332 src++;
333 buffer++;
334 }
335 buffer += frame_step;
336 if (--remaining_frames == 0)
337 src = (void *)runtime->dma_area;
338 }
339}
340
341static void amdtp_write_s32_dualwire(struct amdtp_out_stream *s,
342 struct snd_pcm_substream *pcm,
343 __be32 *buffer, unsigned int frames)
344{
345 struct snd_pcm_runtime *runtime = pcm->runtime;
346 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
347 const u32 *src;
348
349 channels = s->pcm_channels;
350 src = (void *)runtime->dma_area +
351 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
352 frame_adjust_1 = channels - 1;
353 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
354
355 channels /= 2;
356 for (i = 0; i < frames; ++i) {
357 for (c = 0; c < channels; ++c) {
358 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
359 src++;
360 buffer += 2;
361 }
362 buffer -= frame_adjust_1;
363 for (c = 0; c < channels; ++c) {
364 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
365 src++;
366 buffer += 2;
367 }
368 buffer -= frame_adjust_2;
369 }
370}
371
372static void amdtp_write_s16_dualwire(struct amdtp_out_stream *s,
373 struct snd_pcm_substream *pcm,
374 __be32 *buffer, unsigned int frames)
375{
376 struct snd_pcm_runtime *runtime = pcm->runtime;
377 unsigned int channels, frame_adjust_1, frame_adjust_2, i, c;
378 const u16 *src;
379
380 channels = s->pcm_channels;
381 src = (void *)runtime->dma_area +
382 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
383 frame_adjust_1 = channels - 1;
384 frame_adjust_2 = 1 - (s->data_block_quadlets - channels);
385
386 channels /= 2;
387 for (i = 0; i < frames; ++i) {
388 for (c = 0; c < channels; ++c) {
389 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
390 src++;
391 buffer += 2;
392 }
393 buffer -= frame_adjust_1;
394 for (c = 0; c < channels; ++c) {
395 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
396 src++;
397 buffer += 2;
398 }
399 buffer -= frame_adjust_2;
400 }
401}
402
403static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
404 __be32 *buffer, unsigned int frames)
405{
406 unsigned int i, c;
407
408 for (i = 0; i < frames; ++i) {
409 for (c = 0; c < s->pcm_channels; ++c)
410 buffer[c] = cpu_to_be32(0x40000000);
411 buffer += s->data_block_quadlets;
412 }
413}
414
415static void amdtp_fill_midi(struct amdtp_out_stream *s,
416 __be32 *buffer, unsigned int frames)
417{
418 unsigned int i;
419
420 for (i = 0; i < frames; ++i)
421 buffer[s->pcm_channels + i * s->data_block_quadlets] =
422 cpu_to_be32(0x80000000);
423}
424
425static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
426{
427 __be32 *buffer;
428 unsigned int index, data_blocks, syt, ptr;
429 struct snd_pcm_substream *pcm;
430 struct fw_iso_packet packet;
431 int err;
432
433 if (s->packet_index < 0)
434 return;
435 index = s->packet_index;
436
437 /* this module generate empty packet for 'no data' */
438 syt = calculate_syt(s, cycle);
439 if (!(s->flags & CIP_BLOCKING))
440 data_blocks = calculate_data_blocks(s);
441 else if (syt != 0xffff)
442 data_blocks = s->syt_interval;
443 else
444 data_blocks = 0;
445
446 buffer = s->buffer.packets[index].buffer;
447 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
448 (s->data_block_quadlets << 16) |
449 s->data_block_counter);
450 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
451 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
452 buffer += 2;
453
454 pcm = ACCESS_ONCE(s->pcm);
455 if (pcm)
456 s->transfer_samples(s, pcm, buffer, data_blocks);
457 else
458 amdtp_fill_pcm_silence(s, buffer, data_blocks);
459 if (s->midi_ports)
460 amdtp_fill_midi(s, buffer, data_blocks);
461
462 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
463
464 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
465 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
466 packet.skip = 0;
467 packet.tag = TAG_CIP;
468 packet.sy = 0;
469 packet.header_length = 0;
470
471 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
472 s->buffer.packets[index].offset);
473 if (err < 0) {
474 dev_err(&s->unit->device, "queueing error: %d\n", err);
475 s->packet_index = -1;
476 amdtp_out_stream_pcm_abort(s);
477 return;
478 }
479
480 if (++index >= QUEUE_LENGTH)
481 index = 0;
482 s->packet_index = index;
483
484 if (pcm) {
485 if (s->dual_wire)
486 data_blocks *= 2;
487
488 ptr = s->pcm_buffer_pointer + data_blocks;
489 if (ptr >= pcm->runtime->buffer_size)
490 ptr -= pcm->runtime->buffer_size;
491 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
492
493 s->pcm_period_pointer += data_blocks;
494 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
495 s->pcm_period_pointer -= pcm->runtime->period_size;
496 s->pointer_flush = false;
497 tasklet_hi_schedule(&s->period_tasklet);
498 }
499 }
500}
501
502static void pcm_period_tasklet(unsigned long data)
503{
504 struct amdtp_out_stream *s = (void *)data;
505 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
506
507 if (pcm)
508 snd_pcm_period_elapsed(pcm);
509}
510
511static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
512 size_t header_length, void *header, void *data)
513{
514 struct amdtp_out_stream *s = data;
515 unsigned int i, packets = header_length / 4;
516
517 /*
518 * Compute the cycle of the last queued packet.
519 * (We need only the four lowest bits for the SYT, so we can ignore
520 * that bits 0-11 must wrap around at 3072.)
521 */
522 cycle += QUEUE_LENGTH - packets;
523
524 for (i = 0; i < packets; ++i)
525 queue_out_packet(s, ++cycle);
526 fw_iso_context_queue_flush(s->context);
527}
528
529static int queue_initial_skip_packets(struct amdtp_out_stream *s)
530{
531 struct fw_iso_packet skip_packet = {
532 .skip = 1,
533 };
534 unsigned int i;
535 int err;
536
537 for (i = 0; i < QUEUE_LENGTH; ++i) {
538 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
539 INTERRUPT_INTERVAL);
540 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
541 if (err < 0)
542 return err;
543 if (++s->packet_index >= QUEUE_LENGTH)
544 s->packet_index = 0;
545 }
546
547 return 0;
548}
549
550/**
551 * amdtp_out_stream_start - start sending packets
552 * @s: the AMDTP output stream to start
553 * @channel: the isochronous channel on the bus
554 * @speed: firewire speed code
555 *
556 * The stream cannot be started until it has been configured with
557 * amdtp_out_stream_set_parameters() and amdtp_out_stream_set_pcm_format(),
558 * and it must be started before any PCM or MIDI device can be started.
559 */
560int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
561{
562 static const struct {
563 unsigned int data_block;
564 unsigned int syt_offset;
565 } initial_state[] = {
566 [CIP_SFC_32000] = { 4, 3072 },
567 [CIP_SFC_48000] = { 6, 1024 },
568 [CIP_SFC_96000] = { 12, 1024 },
569 [CIP_SFC_192000] = { 24, 1024 },
570 [CIP_SFC_44100] = { 0, 67 },
571 [CIP_SFC_88200] = { 0, 67 },
572 [CIP_SFC_176400] = { 0, 67 },
573 };
574 int err;
575
576 mutex_lock(&s->mutex);
577
578 if (WARN_ON(amdtp_out_stream_running(s) ||
579 (!s->pcm_channels && !s->midi_ports))) {
580 err = -EBADFD;
581 goto err_unlock;
582 }
583
584 s->data_block_state = initial_state[s->sfc].data_block;
585 s->syt_offset_state = initial_state[s->sfc].syt_offset;
586 s->last_syt_offset = TICKS_PER_CYCLE;
587
588 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
589 amdtp_out_stream_get_max_payload(s),
590 DMA_TO_DEVICE);
591 if (err < 0)
592 goto err_unlock;
593
594 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
595 FW_ISO_CONTEXT_TRANSMIT,
596 channel, speed, 0,
597 out_packet_callback, s);
598 if (IS_ERR(s->context)) {
599 err = PTR_ERR(s->context);
600 if (err == -EBUSY)
601 dev_err(&s->unit->device,
602 "no free output stream on this controller\n");
603 goto err_buffer;
604 }
605
606 amdtp_out_stream_update(s);
607
608 s->packet_index = 0;
609 s->data_block_counter = 0;
610 err = queue_initial_skip_packets(s);
611 if (err < 0)
612 goto err_context;
613
614 err = fw_iso_context_start(s->context, -1, 0, 0);
615 if (err < 0)
616 goto err_context;
617
618 mutex_unlock(&s->mutex);
619
620 return 0;
621
622err_context:
623 fw_iso_context_destroy(s->context);
624 s->context = ERR_PTR(-1);
625err_buffer:
626 iso_packets_buffer_destroy(&s->buffer, s->unit);
627err_unlock:
628 mutex_unlock(&s->mutex);
629
630 return err;
631}
632EXPORT_SYMBOL(amdtp_out_stream_start);
633
634/**
635 * amdtp_out_stream_pcm_pointer - get the PCM buffer position
636 * @s: the AMDTP output stream that transports the PCM data
637 *
638 * Returns the current buffer position, in frames.
639 */
640unsigned long amdtp_out_stream_pcm_pointer(struct amdtp_out_stream *s)
641{
642 /* this optimization is allowed to be racy */
643 if (s->pointer_flush)
644 fw_iso_context_flush_completions(s->context);
645 else
646 s->pointer_flush = true;
647
648 return ACCESS_ONCE(s->pcm_buffer_pointer);
649}
650EXPORT_SYMBOL(amdtp_out_stream_pcm_pointer);
651
652/**
653 * amdtp_out_stream_update - update the stream after a bus reset
654 * @s: the AMDTP output stream
655 */
656void amdtp_out_stream_update(struct amdtp_out_stream *s)
657{
658 ACCESS_ONCE(s->source_node_id_field) =
659 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
660}
661EXPORT_SYMBOL(amdtp_out_stream_update);
662
663/**
664 * amdtp_out_stream_stop - stop sending packets
665 * @s: the AMDTP output stream to stop
666 *
667 * All PCM and MIDI devices of the stream must be stopped before the stream
668 * itself can be stopped.
669 */
670void amdtp_out_stream_stop(struct amdtp_out_stream *s)
671{
672 mutex_lock(&s->mutex);
673
674 if (!amdtp_out_stream_running(s)) {
675 mutex_unlock(&s->mutex);
676 return;
677 }
678
679 tasklet_kill(&s->period_tasklet);
680 fw_iso_context_stop(s->context);
681 fw_iso_context_destroy(s->context);
682 s->context = ERR_PTR(-1);
683 iso_packets_buffer_destroy(&s->buffer, s->unit);
684
685 mutex_unlock(&s->mutex);
686}
687EXPORT_SYMBOL(amdtp_out_stream_stop);
688
689/**
690 * amdtp_out_stream_pcm_abort - abort the running PCM device
691 * @s: the AMDTP stream about to be stopped
692 *
693 * If the isochronous stream needs to be stopped asynchronously, call this
694 * function first to stop the PCM device.
695 */
696void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
697{
698 struct snd_pcm_substream *pcm;
699
700 pcm = ACCESS_ONCE(s->pcm);
701 if (pcm) {
702 snd_pcm_stream_lock_irq(pcm);
703 if (snd_pcm_running(pcm))
704 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
705 snd_pcm_stream_unlock_irq(pcm);
706 }
707}
708EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);
1/*
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 s->packet_index = 0;
51
52 return 0;
53}
54EXPORT_SYMBOL(amdtp_out_stream_init);
55
56/**
57 * amdtp_out_stream_destroy - free stream resources
58 * @s: the AMDTP output stream to destroy
59 */
60void amdtp_out_stream_destroy(struct amdtp_out_stream *s)
61{
62 WARN_ON(!IS_ERR(s->context));
63 mutex_destroy(&s->mutex);
64 fw_unit_put(s->unit);
65}
66EXPORT_SYMBOL(amdtp_out_stream_destroy);
67
68/**
69 * amdtp_out_stream_set_rate - set the sample rate
70 * @s: the AMDTP output stream to configure
71 * @rate: the sample rate
72 *
73 * The sample rate must be set before the stream is started, and must not be
74 * changed while the stream is running.
75 */
76void amdtp_out_stream_set_rate(struct amdtp_out_stream *s, unsigned int rate)
77{
78 static const struct {
79 unsigned int rate;
80 unsigned int syt_interval;
81 } rate_info[] = {
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, },
89 };
90 unsigned int sfc;
91
92 if (WARN_ON(!IS_ERR(s->context)))
93 return;
94
95 for (sfc = 0; sfc < ARRAY_SIZE(rate_info); ++sfc)
96 if (rate_info[sfc].rate == rate) {
97 s->sfc = sfc;
98 s->syt_interval = rate_info[sfc].syt_interval;
99 return;
100 }
101 WARN_ON(1);
102}
103EXPORT_SYMBOL(amdtp_out_stream_set_rate);
104
105/**
106 * amdtp_out_stream_get_max_payload - get the stream's packet size
107 * @s: the AMDTP output stream
108 *
109 * This function must not be called before the stream has been configured
110 * with amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
111 * amdtp_out_stream_set_midi().
112 */
113unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream *s)
114{
115 static const unsigned int max_data_blocks[] = {
116 [CIP_SFC_32000] = 4,
117 [CIP_SFC_44100] = 6,
118 [CIP_SFC_48000] = 6,
119 [CIP_SFC_88200] = 12,
120 [CIP_SFC_96000] = 12,
121 [CIP_SFC_176400] = 23,
122 [CIP_SFC_192000] = 24,
123 };
124
125 s->data_block_quadlets = s->pcm_channels;
126 s->data_block_quadlets += DIV_ROUND_UP(s->midi_ports, 8);
127
128 return 8 + max_data_blocks[s->sfc] * 4 * s->data_block_quadlets;
129}
130EXPORT_SYMBOL(amdtp_out_stream_get_max_payload);
131
132static void amdtp_write_s16(struct amdtp_out_stream *s,
133 struct snd_pcm_substream *pcm,
134 __be32 *buffer, unsigned int frames);
135static void amdtp_write_s32(struct amdtp_out_stream *s,
136 struct snd_pcm_substream *pcm,
137 __be32 *buffer, unsigned int frames);
138
139/**
140 * amdtp_out_stream_set_pcm_format - set the PCM format
141 * @s: the AMDTP output stream to configure
142 * @format: the format of the ALSA PCM device
143 *
144 * The sample format must be set before the stream is started, and must not be
145 * changed while the stream is running.
146 */
147void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream *s,
148 snd_pcm_format_t format)
149{
150 if (WARN_ON(!IS_ERR(s->context)))
151 return;
152
153 switch (format) {
154 default:
155 WARN_ON(1);
156 /* fall through */
157 case SNDRV_PCM_FORMAT_S16:
158 s->transfer_samples = amdtp_write_s16;
159 break;
160 case SNDRV_PCM_FORMAT_S32:
161 s->transfer_samples = amdtp_write_s32;
162 break;
163 }
164}
165EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format);
166
167static unsigned int calculate_data_blocks(struct amdtp_out_stream *s)
168{
169 unsigned int phase, data_blocks;
170
171 if (!cip_sfc_is_base_44100(s->sfc)) {
172 /* Sample_rate / 8000 is an integer, and precomputed. */
173 data_blocks = s->data_block_state;
174 } else {
175 phase = s->data_block_state;
176
177 /*
178 * This calculates the number of data blocks per packet so that
179 * 1) the overall rate is correct and exactly synchronized to
180 * the bus clock, and
181 * 2) packets with a rounded-up number of blocks occur as early
182 * as possible in the sequence (to prevent underruns of the
183 * device's buffer).
184 */
185 if (s->sfc == CIP_SFC_44100)
186 /* 6 6 5 6 5 6 5 ... */
187 data_blocks = 5 + ((phase & 1) ^
188 (phase == 0 || phase >= 40));
189 else
190 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
191 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
192 if (++phase >= (80 >> (s->sfc >> 1)))
193 phase = 0;
194 s->data_block_state = phase;
195 }
196
197 return data_blocks;
198}
199
200static unsigned int calculate_syt(struct amdtp_out_stream *s,
201 unsigned int cycle)
202{
203 unsigned int syt_offset, phase, index, syt;
204
205 if (s->last_syt_offset < TICKS_PER_CYCLE) {
206 if (!cip_sfc_is_base_44100(s->sfc))
207 syt_offset = s->last_syt_offset + s->syt_offset_state;
208 else {
209 /*
210 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
211 * n * SYT_INTERVAL * 24576000 / sample_rate
212 * Modulo TICKS_PER_CYCLE, the difference between successive
213 * elements is about 1386.23. Rounding the results of this
214 * formula to the SYT precision results in a sequence of
215 * differences that begins with:
216 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
217 * This code generates _exactly_ the same sequence.
218 */
219 phase = s->syt_offset_state;
220 index = phase % 13;
221 syt_offset = s->last_syt_offset;
222 syt_offset += 1386 + ((index && !(index & 3)) ||
223 phase == 146);
224 if (++phase >= 147)
225 phase = 0;
226 s->syt_offset_state = phase;
227 }
228 } else
229 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
230 s->last_syt_offset = syt_offset;
231
232 if (syt_offset < TICKS_PER_CYCLE) {
233 syt_offset += TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
234 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
235 syt += syt_offset % TICKS_PER_CYCLE;
236
237 return syt & 0xffff;
238 } else {
239 return 0xffff; /* no info */
240 }
241}
242
243static void amdtp_write_s32(struct amdtp_out_stream *s,
244 struct snd_pcm_substream *pcm,
245 __be32 *buffer, unsigned int frames)
246{
247 struct snd_pcm_runtime *runtime = pcm->runtime;
248 unsigned int channels, remaining_frames, frame_step, i, c;
249 const u32 *src;
250
251 channels = s->pcm_channels;
252 src = (void *)runtime->dma_area +
253 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
254 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
255 frame_step = s->data_block_quadlets - channels;
256
257 for (i = 0; i < frames; ++i) {
258 for (c = 0; c < channels; ++c) {
259 *buffer = cpu_to_be32((*src >> 8) | 0x40000000);
260 src++;
261 buffer++;
262 }
263 buffer += frame_step;
264 if (--remaining_frames == 0)
265 src = (void *)runtime->dma_area;
266 }
267}
268
269static void amdtp_write_s16(struct amdtp_out_stream *s,
270 struct snd_pcm_substream *pcm,
271 __be32 *buffer, unsigned int frames)
272{
273 struct snd_pcm_runtime *runtime = pcm->runtime;
274 unsigned int channels, remaining_frames, frame_step, i, c;
275 const u16 *src;
276
277 channels = s->pcm_channels;
278 src = (void *)runtime->dma_area +
279 s->pcm_buffer_pointer * (runtime->frame_bits / 8);
280 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
281 frame_step = s->data_block_quadlets - channels;
282
283 for (i = 0; i < frames; ++i) {
284 for (c = 0; c < channels; ++c) {
285 *buffer = cpu_to_be32((*src << 8) | 0x40000000);
286 src++;
287 buffer++;
288 }
289 buffer += frame_step;
290 if (--remaining_frames == 0)
291 src = (void *)runtime->dma_area;
292 }
293}
294
295static void amdtp_fill_pcm_silence(struct amdtp_out_stream *s,
296 __be32 *buffer, unsigned int frames)
297{
298 unsigned int i, c;
299
300 for (i = 0; i < frames; ++i) {
301 for (c = 0; c < s->pcm_channels; ++c)
302 buffer[c] = cpu_to_be32(0x40000000);
303 buffer += s->data_block_quadlets;
304 }
305}
306
307static void amdtp_fill_midi(struct amdtp_out_stream *s,
308 __be32 *buffer, unsigned int frames)
309{
310 unsigned int i;
311
312 for (i = 0; i < frames; ++i)
313 buffer[s->pcm_channels + i * s->data_block_quadlets] =
314 cpu_to_be32(0x80000000);
315}
316
317static void queue_out_packet(struct amdtp_out_stream *s, unsigned int cycle)
318{
319 __be32 *buffer;
320 unsigned int index, data_blocks, syt, ptr;
321 struct snd_pcm_substream *pcm;
322 struct fw_iso_packet packet;
323 int err;
324
325 if (s->packet_index < 0)
326 return;
327 index = s->packet_index;
328
329 data_blocks = calculate_data_blocks(s);
330 syt = calculate_syt(s, cycle);
331
332 buffer = s->buffer.packets[index].buffer;
333 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
334 (s->data_block_quadlets << 16) |
335 s->data_block_counter);
336 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
337 (s->sfc << AMDTP_FDF_SFC_SHIFT) | syt);
338 buffer += 2;
339
340 pcm = ACCESS_ONCE(s->pcm);
341 if (pcm)
342 s->transfer_samples(s, pcm, buffer, data_blocks);
343 else
344 amdtp_fill_pcm_silence(s, buffer, data_blocks);
345 if (s->midi_ports)
346 amdtp_fill_midi(s, buffer, data_blocks);
347
348 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
349
350 packet.payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
351 packet.interrupt = IS_ALIGNED(index + 1, INTERRUPT_INTERVAL);
352 packet.skip = 0;
353 packet.tag = TAG_CIP;
354 packet.sy = 0;
355 packet.header_length = 0;
356
357 err = fw_iso_context_queue(s->context, &packet, &s->buffer.iso_buffer,
358 s->buffer.packets[index].offset);
359 if (err < 0) {
360 dev_err(&s->unit->device, "queueing error: %d\n", err);
361 s->packet_index = -1;
362 amdtp_out_stream_pcm_abort(s);
363 return;
364 }
365
366 if (++index >= QUEUE_LENGTH)
367 index = 0;
368 s->packet_index = index;
369
370 if (pcm) {
371 ptr = s->pcm_buffer_pointer + data_blocks;
372 if (ptr >= pcm->runtime->buffer_size)
373 ptr -= pcm->runtime->buffer_size;
374 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
375
376 s->pcm_period_pointer += data_blocks;
377 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
378 s->pcm_period_pointer -= pcm->runtime->period_size;
379 snd_pcm_period_elapsed(pcm);
380 }
381 }
382}
383
384static void out_packet_callback(struct fw_iso_context *context, u32 cycle,
385 size_t header_length, void *header, void *data)
386{
387 struct amdtp_out_stream *s = data;
388 unsigned int i, packets = header_length / 4;
389
390 /*
391 * Compute the cycle of the last queued packet.
392 * (We need only the four lowest bits for the SYT, so we can ignore
393 * that bits 0-11 must wrap around at 3072.)
394 */
395 cycle += QUEUE_LENGTH - packets;
396
397 for (i = 0; i < packets; ++i)
398 queue_out_packet(s, ++cycle);
399 fw_iso_context_queue_flush(s->context);
400}
401
402static int queue_initial_skip_packets(struct amdtp_out_stream *s)
403{
404 struct fw_iso_packet skip_packet = {
405 .skip = 1,
406 };
407 unsigned int i;
408 int err;
409
410 for (i = 0; i < QUEUE_LENGTH; ++i) {
411 skip_packet.interrupt = IS_ALIGNED(s->packet_index + 1,
412 INTERRUPT_INTERVAL);
413 err = fw_iso_context_queue(s->context, &skip_packet, NULL, 0);
414 if (err < 0)
415 return err;
416 if (++s->packet_index >= QUEUE_LENGTH)
417 s->packet_index = 0;
418 }
419
420 return 0;
421}
422
423/**
424 * amdtp_out_stream_start - start sending packets
425 * @s: the AMDTP output stream to start
426 * @channel: the isochronous channel on the bus
427 * @speed: firewire speed code
428 *
429 * The stream cannot be started until it has been configured with
430 * amdtp_out_stream_set_hw_params(), amdtp_out_stream_set_pcm(), and
431 * amdtp_out_stream_set_midi(); and it must be started before any
432 * PCM or MIDI device can be started.
433 */
434int amdtp_out_stream_start(struct amdtp_out_stream *s, int channel, int speed)
435{
436 static const struct {
437 unsigned int data_block;
438 unsigned int syt_offset;
439 } initial_state[] = {
440 [CIP_SFC_32000] = { 4, 3072 },
441 [CIP_SFC_48000] = { 6, 1024 },
442 [CIP_SFC_96000] = { 12, 1024 },
443 [CIP_SFC_192000] = { 24, 1024 },
444 [CIP_SFC_44100] = { 0, 67 },
445 [CIP_SFC_88200] = { 0, 67 },
446 [CIP_SFC_176400] = { 0, 67 },
447 };
448 int err;
449
450 mutex_lock(&s->mutex);
451
452 if (WARN_ON(!IS_ERR(s->context) ||
453 (!s->pcm_channels && !s->midi_ports))) {
454 err = -EBADFD;
455 goto err_unlock;
456 }
457
458 s->data_block_state = initial_state[s->sfc].data_block;
459 s->syt_offset_state = initial_state[s->sfc].syt_offset;
460 s->last_syt_offset = TICKS_PER_CYCLE;
461
462 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
463 amdtp_out_stream_get_max_payload(s),
464 DMA_TO_DEVICE);
465 if (err < 0)
466 goto err_unlock;
467
468 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
469 FW_ISO_CONTEXT_TRANSMIT,
470 channel, speed, 0,
471 out_packet_callback, s);
472 if (IS_ERR(s->context)) {
473 err = PTR_ERR(s->context);
474 if (err == -EBUSY)
475 dev_err(&s->unit->device,
476 "no free output stream on this controller\n");
477 goto err_buffer;
478 }
479
480 amdtp_out_stream_update(s);
481
482 s->packet_index = 0;
483 s->data_block_counter = 0;
484 err = queue_initial_skip_packets(s);
485 if (err < 0)
486 goto err_context;
487
488 err = fw_iso_context_start(s->context, -1, 0, 0);
489 if (err < 0)
490 goto err_context;
491
492 mutex_unlock(&s->mutex);
493
494 return 0;
495
496err_context:
497 fw_iso_context_destroy(s->context);
498 s->context = ERR_PTR(-1);
499err_buffer:
500 iso_packets_buffer_destroy(&s->buffer, s->unit);
501err_unlock:
502 mutex_unlock(&s->mutex);
503
504 return err;
505}
506EXPORT_SYMBOL(amdtp_out_stream_start);
507
508/**
509 * amdtp_out_stream_update - update the stream after a bus reset
510 * @s: the AMDTP output stream
511 */
512void amdtp_out_stream_update(struct amdtp_out_stream *s)
513{
514 ACCESS_ONCE(s->source_node_id_field) =
515 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
516}
517EXPORT_SYMBOL(amdtp_out_stream_update);
518
519/**
520 * amdtp_out_stream_stop - stop sending packets
521 * @s: the AMDTP output stream to stop
522 *
523 * All PCM and MIDI devices of the stream must be stopped before the stream
524 * itself can be stopped.
525 */
526void amdtp_out_stream_stop(struct amdtp_out_stream *s)
527{
528 mutex_lock(&s->mutex);
529
530 if (IS_ERR(s->context)) {
531 mutex_unlock(&s->mutex);
532 return;
533 }
534
535 fw_iso_context_stop(s->context);
536 fw_iso_context_destroy(s->context);
537 s->context = ERR_PTR(-1);
538 iso_packets_buffer_destroy(&s->buffer, s->unit);
539
540 mutex_unlock(&s->mutex);
541}
542EXPORT_SYMBOL(amdtp_out_stream_stop);
543
544/**
545 * amdtp_out_stream_pcm_abort - abort the running PCM device
546 * @s: the AMDTP stream about to be stopped
547 *
548 * If the isochronous stream needs to be stopped asynchronously, call this
549 * function first to stop the PCM device.
550 */
551void amdtp_out_stream_pcm_abort(struct amdtp_out_stream *s)
552{
553 struct snd_pcm_substream *pcm;
554
555 pcm = ACCESS_ONCE(s->pcm);
556 if (pcm) {
557 snd_pcm_stream_lock_irq(pcm);
558 if (snd_pcm_running(pcm))
559 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
560 snd_pcm_stream_unlock_irq(pcm);
561 }
562}
563EXPORT_SYMBOL(amdtp_out_stream_pcm_abort);