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 <linux/sched.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/rawmidi.h>
20 #define TICKS_PER_CYCLE 3072
21 #define CYCLES_PER_SECOND 8000
22 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
24 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
26 /* isochronous header parameters */
27 #define ISO_DATA_LENGTH_SHIFT 16
30 /* common isochronous packet header parameters */
31 #define CIP_EOH (1u << 31)
32 #define CIP_EOH_MASK 0x80000000
33 #define CIP_FMT_AM (0x10 << 24)
34 #define CIP_FMT_MASK 0x3f000000
35 #define CIP_SYT_MASK 0x0000ffff
36 #define CIP_SYT_NO_INFO 0xffff
37 #define CIP_FDF_MASK 0x00ff0000
38 #define CIP_FDF_SFC_SHIFT 16
41 * Audio and Music transfer protocol specific parameters
42 * only "Clock-based rate control mode" is supported
44 #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
45 #define AMDTP_FDF_NO_DATA 0xff
46 #define AMDTP_DBS_MASK 0x00ff0000
47 #define AMDTP_DBS_SHIFT 16
48 #define AMDTP_DBC_MASK 0x000000ff
50 /* TODO: make these configurable */
51 #define INTERRUPT_INTERVAL 16
52 #define QUEUE_LENGTH 48
54 #define IN_PACKET_HEADER_SIZE 4
55 #define OUT_PACKET_HEADER_SIZE 0
57 static void pcm_period_tasklet(unsigned long data);
60 * amdtp_stream_init - initialize an AMDTP stream structure
61 * @s: the AMDTP stream to initialize
62 * @unit: the target of the stream
63 * @dir: the direction of stream
64 * @flags: the packet transmission method to use
66 int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
67 enum amdtp_stream_direction dir, enum cip_flags flags)
69 s->unit = fw_unit_get(unit);
72 s->context = ERR_PTR(-1);
73 mutex_init(&s->mutex);
74 tasklet_init(&s->period_tasklet, pcm_period_tasklet, (unsigned long)s);
77 init_waitqueue_head(&s->callback_wait);
78 s->callbacked = false;
83 EXPORT_SYMBOL(amdtp_stream_init);
86 * amdtp_stream_destroy - free stream resources
87 * @s: the AMDTP stream to destroy
89 void amdtp_stream_destroy(struct amdtp_stream *s)
91 WARN_ON(amdtp_stream_running(s));
92 mutex_destroy(&s->mutex);
95 EXPORT_SYMBOL(amdtp_stream_destroy);
97 const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
101 [CIP_SFC_88200] = 16,
102 [CIP_SFC_96000] = 16,
103 [CIP_SFC_176400] = 32,
104 [CIP_SFC_192000] = 32,
106 EXPORT_SYMBOL(amdtp_syt_intervals);
108 const unsigned int amdtp_rate_table[] = {
109 [CIP_SFC_32000] = 32000,
110 [CIP_SFC_44100] = 44100,
111 [CIP_SFC_48000] = 48000,
112 [CIP_SFC_88200] = 88200,
113 [CIP_SFC_96000] = 96000,
114 [CIP_SFC_176400] = 176400,
115 [CIP_SFC_192000] = 192000,
117 EXPORT_SYMBOL(amdtp_rate_table);
120 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
121 * @s: the AMDTP stream, which must be initialized.
122 * @runtime: the PCM substream runtime
124 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
125 struct snd_pcm_runtime *runtime)
129 /* AM824 in IEC 61883-6 can deliver 24bit data */
130 err = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
135 * Currently firewire-lib processes 16 packets in one software
136 * interrupt callback. This equals to 2msec but actually the
137 * interval of the interrupts has a jitter.
138 * Additionally, even if adding a constraint to fit period size to
139 * 2msec, actual calculated frames per period doesn't equal to 2msec,
140 * depending on sampling rate.
141 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
142 * Here let us use 5msec for safe period interrupt.
144 err = snd_pcm_hw_constraint_minmax(runtime,
145 SNDRV_PCM_HW_PARAM_PERIOD_TIME,
150 /* Non-Blocking stream has no more constraints */
151 if (!(s->flags & CIP_BLOCKING))
155 * One AMDTP packet can include some frames. In blocking mode, the
156 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
157 * depending on its sampling rate. For accurate period interrupt, it's
158 * preferrable to aligh period/buffer sizes to current SYT_INTERVAL.
160 * TODO: These constraints can be improved with propper rules.
161 * Currently apply LCM of SYT_INTEVALs.
163 err = snd_pcm_hw_constraint_step(runtime, 0,
164 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
167 err = snd_pcm_hw_constraint_step(runtime, 0,
168 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
172 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
175 * amdtp_stream_set_parameters - set stream parameters
176 * @s: the AMDTP stream to configure
177 * @rate: the sample rate
178 * @pcm_channels: the number of PCM samples in each data block, to be encoded
179 * as AM824 multi-bit linear audio
180 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
182 * The parameters must be set before the stream is started, and must not be
183 * changed while the stream is running.
185 void amdtp_stream_set_parameters(struct amdtp_stream *s,
187 unsigned int pcm_channels,
188 unsigned int midi_ports)
190 unsigned int i, sfc, midi_channels;
192 midi_channels = DIV_ROUND_UP(midi_ports, 8);
194 if (WARN_ON(amdtp_stream_running(s)) |
195 WARN_ON(pcm_channels > AMDTP_MAX_CHANNELS_FOR_PCM) |
196 WARN_ON(midi_channels > AMDTP_MAX_CHANNELS_FOR_MIDI))
199 for (sfc = 0; sfc < sizeof(amdtp_rate_table); ++sfc)
200 if (amdtp_rate_table[sfc] == rate)
206 s->pcm_channels = pcm_channels;
208 s->data_block_quadlets = s->pcm_channels + midi_channels;
209 s->midi_ports = midi_ports;
211 s->syt_interval = amdtp_syt_intervals[sfc];
213 /* default buffering in the device */
214 s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
215 if (s->flags & CIP_BLOCKING)
216 /* additional buffering needed to adjust for no-data packets */
217 s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
219 /* init the position map for PCM and MIDI channels */
220 for (i = 0; i < pcm_channels; i++)
221 s->pcm_positions[i] = i;
222 s->midi_position = s->pcm_channels;
224 EXPORT_SYMBOL(amdtp_stream_set_parameters);
227 * amdtp_stream_get_max_payload - get the stream's packet size
228 * @s: the AMDTP stream
230 * This function must not be called before the stream has been configured
231 * with amdtp_stream_set_parameters().
233 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
235 return 8 + s->syt_interval * s->data_block_quadlets * 4;
237 EXPORT_SYMBOL(amdtp_stream_get_max_payload);
239 static void amdtp_write_s16(struct amdtp_stream *s,
240 struct snd_pcm_substream *pcm,
241 __be32 *buffer, unsigned int frames);
242 static void amdtp_write_s32(struct amdtp_stream *s,
243 struct snd_pcm_substream *pcm,
244 __be32 *buffer, unsigned int frames);
245 static void amdtp_read_s32(struct amdtp_stream *s,
246 struct snd_pcm_substream *pcm,
247 __be32 *buffer, unsigned int frames);
250 * amdtp_stream_set_pcm_format - set the PCM format
251 * @s: the AMDTP stream to configure
252 * @format: the format of the ALSA PCM device
254 * The sample format must be set after the other paramters (rate/PCM channels/
255 * MIDI) and before the stream is started, and must not be changed while the
258 void amdtp_stream_set_pcm_format(struct amdtp_stream *s,
259 snd_pcm_format_t format)
261 if (WARN_ON(amdtp_stream_pcm_running(s)))
268 case SNDRV_PCM_FORMAT_S16:
269 if (s->direction == AMDTP_OUT_STREAM) {
270 s->transfer_samples = amdtp_write_s16;
275 case SNDRV_PCM_FORMAT_S32:
276 if (s->direction == AMDTP_OUT_STREAM)
277 s->transfer_samples = amdtp_write_s32;
279 s->transfer_samples = amdtp_read_s32;
283 EXPORT_SYMBOL(amdtp_stream_set_pcm_format);
286 * amdtp_stream_pcm_prepare - prepare PCM device for running
287 * @s: the AMDTP stream
289 * This function should be called from the PCM device's .prepare callback.
291 void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
293 tasklet_kill(&s->period_tasklet);
294 s->pcm_buffer_pointer = 0;
295 s->pcm_period_pointer = 0;
296 s->pointer_flush = true;
298 EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
300 static unsigned int calculate_data_blocks(struct amdtp_stream *s)
302 unsigned int phase, data_blocks;
304 if (s->flags & CIP_BLOCKING)
305 data_blocks = s->syt_interval;
306 else if (!cip_sfc_is_base_44100(s->sfc)) {
307 /* Sample_rate / 8000 is an integer, and precomputed. */
308 data_blocks = s->data_block_state;
310 phase = s->data_block_state;
313 * This calculates the number of data blocks per packet so that
314 * 1) the overall rate is correct and exactly synchronized to
316 * 2) packets with a rounded-up number of blocks occur as early
317 * as possible in the sequence (to prevent underruns of the
320 if (s->sfc == CIP_SFC_44100)
321 /* 6 6 5 6 5 6 5 ... */
322 data_blocks = 5 + ((phase & 1) ^
323 (phase == 0 || phase >= 40));
325 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
326 data_blocks = 11 * (s->sfc >> 1) + (phase == 0);
327 if (++phase >= (80 >> (s->sfc >> 1)))
329 s->data_block_state = phase;
335 static unsigned int calculate_syt(struct amdtp_stream *s,
338 unsigned int syt_offset, phase, index, syt;
340 if (s->last_syt_offset < TICKS_PER_CYCLE) {
341 if (!cip_sfc_is_base_44100(s->sfc))
342 syt_offset = s->last_syt_offset + s->syt_offset_state;
345 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
346 * n * SYT_INTERVAL * 24576000 / sample_rate
347 * Modulo TICKS_PER_CYCLE, the difference between successive
348 * elements is about 1386.23. Rounding the results of this
349 * formula to the SYT precision results in a sequence of
350 * differences that begins with:
351 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
352 * This code generates _exactly_ the same sequence.
354 phase = s->syt_offset_state;
356 syt_offset = s->last_syt_offset;
357 syt_offset += 1386 + ((index && !(index & 3)) ||
361 s->syt_offset_state = phase;
364 syt_offset = s->last_syt_offset - TICKS_PER_CYCLE;
365 s->last_syt_offset = syt_offset;
367 if (syt_offset < TICKS_PER_CYCLE) {
368 syt_offset += s->transfer_delay;
369 syt = (cycle + syt_offset / TICKS_PER_CYCLE) << 12;
370 syt += syt_offset % TICKS_PER_CYCLE;
372 return syt & CIP_SYT_MASK;
374 return CIP_SYT_NO_INFO;
378 static void amdtp_write_s32(struct amdtp_stream *s,
379 struct snd_pcm_substream *pcm,
380 __be32 *buffer, unsigned int frames)
382 struct snd_pcm_runtime *runtime = pcm->runtime;
383 unsigned int channels, remaining_frames, i, c;
386 channels = s->pcm_channels;
387 src = (void *)runtime->dma_area +
388 frames_to_bytes(runtime, s->pcm_buffer_pointer);
389 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
391 for (i = 0; i < frames; ++i) {
392 for (c = 0; c < channels; ++c) {
393 buffer[s->pcm_positions[c]] =
394 cpu_to_be32((*src >> 8) | 0x40000000);
397 buffer += s->data_block_quadlets;
398 if (--remaining_frames == 0)
399 src = (void *)runtime->dma_area;
403 static void amdtp_write_s16(struct amdtp_stream *s,
404 struct snd_pcm_substream *pcm,
405 __be32 *buffer, unsigned int frames)
407 struct snd_pcm_runtime *runtime = pcm->runtime;
408 unsigned int channels, remaining_frames, i, c;
411 channels = s->pcm_channels;
412 src = (void *)runtime->dma_area +
413 frames_to_bytes(runtime, s->pcm_buffer_pointer);
414 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
416 for (i = 0; i < frames; ++i) {
417 for (c = 0; c < channels; ++c) {
418 buffer[s->pcm_positions[c]] =
419 cpu_to_be32((*src << 8) | 0x40000000);
422 buffer += s->data_block_quadlets;
423 if (--remaining_frames == 0)
424 src = (void *)runtime->dma_area;
428 static void amdtp_read_s32(struct amdtp_stream *s,
429 struct snd_pcm_substream *pcm,
430 __be32 *buffer, unsigned int frames)
432 struct snd_pcm_runtime *runtime = pcm->runtime;
433 unsigned int channels, remaining_frames, i, c;
436 channels = s->pcm_channels;
437 dst = (void *)runtime->dma_area +
438 frames_to_bytes(runtime, s->pcm_buffer_pointer);
439 remaining_frames = runtime->buffer_size - s->pcm_buffer_pointer;
441 for (i = 0; i < frames; ++i) {
442 for (c = 0; c < channels; ++c) {
443 *dst = be32_to_cpu(buffer[s->pcm_positions[c]]) << 8;
446 buffer += s->data_block_quadlets;
447 if (--remaining_frames == 0)
448 dst = (void *)runtime->dma_area;
452 static void amdtp_fill_pcm_silence(struct amdtp_stream *s,
453 __be32 *buffer, unsigned int frames)
457 for (i = 0; i < frames; ++i) {
458 for (c = 0; c < s->pcm_channels; ++c)
459 buffer[s->pcm_positions[c]] = cpu_to_be32(0x40000000);
460 buffer += s->data_block_quadlets;
464 static void amdtp_fill_midi(struct amdtp_stream *s,
465 __be32 *buffer, unsigned int frames)
467 unsigned int f, port;
470 for (f = 0; f < frames; f++) {
471 buffer[s->midi_position] = 0;
472 b = (u8 *)&buffer[s->midi_position];
474 port = (s->data_block_counter + f) % 8;
475 if ((s->midi[port] == NULL) ||
476 (snd_rawmidi_transmit(s->midi[port], b + 1, 1) <= 0))
481 buffer += s->data_block_quadlets;
485 static void amdtp_pull_midi(struct amdtp_stream *s,
486 __be32 *buffer, unsigned int frames)
488 unsigned int f, port;
492 for (f = 0; f < frames; f++) {
493 port = (s->data_block_counter + f) % 8;
494 b = (u8 *)&buffer[s->midi_position];
497 if ((1 <= len) && (len <= 3) && (s->midi[port]))
498 snd_rawmidi_receive(s->midi[port], b + 1, len);
500 buffer += s->data_block_quadlets;
504 static void update_pcm_pointers(struct amdtp_stream *s,
505 struct snd_pcm_substream *pcm,
509 ptr = s->pcm_buffer_pointer + frames;
510 if (ptr >= pcm->runtime->buffer_size)
511 ptr -= pcm->runtime->buffer_size;
512 ACCESS_ONCE(s->pcm_buffer_pointer) = ptr;
514 s->pcm_period_pointer += frames;
515 if (s->pcm_period_pointer >= pcm->runtime->period_size) {
516 s->pcm_period_pointer -= pcm->runtime->period_size;
517 s->pointer_flush = false;
518 tasklet_hi_schedule(&s->period_tasklet);
522 static void pcm_period_tasklet(unsigned long data)
524 struct amdtp_stream *s = (void *)data;
525 struct snd_pcm_substream *pcm = ACCESS_ONCE(s->pcm);
528 snd_pcm_period_elapsed(pcm);
531 static int queue_packet(struct amdtp_stream *s,
532 unsigned int header_length,
533 unsigned int payload_length, bool skip)
535 struct fw_iso_packet p = {0};
538 if (IS_ERR(s->context))
541 p.interrupt = IS_ALIGNED(s->packet_index + 1, INTERRUPT_INTERVAL);
543 p.header_length = header_length;
544 p.payload_length = (!skip) ? payload_length : 0;
546 err = fw_iso_context_queue(s->context, &p, &s->buffer.iso_buffer,
547 s->buffer.packets[s->packet_index].offset);
549 dev_err(&s->unit->device, "queueing error: %d\n", err);
553 if (++s->packet_index >= QUEUE_LENGTH)
559 static inline int queue_out_packet(struct amdtp_stream *s,
560 unsigned int payload_length, bool skip)
562 return queue_packet(s, OUT_PACKET_HEADER_SIZE,
563 payload_length, skip);
566 static inline int queue_in_packet(struct amdtp_stream *s)
568 return queue_packet(s, IN_PACKET_HEADER_SIZE,
569 amdtp_stream_get_max_payload(s), false);
572 static void handle_out_packet(struct amdtp_stream *s, unsigned int syt)
575 unsigned int data_blocks, payload_length;
576 struct snd_pcm_substream *pcm;
578 if (s->packet_index < 0)
581 /* this module generate empty packet for 'no data' */
582 if (!(s->flags & CIP_BLOCKING) || (syt != CIP_SYT_NO_INFO))
583 data_blocks = calculate_data_blocks(s);
587 buffer = s->buffer.packets[s->packet_index].buffer;
588 buffer[0] = cpu_to_be32(ACCESS_ONCE(s->source_node_id_field) |
589 (s->data_block_quadlets << AMDTP_DBS_SHIFT) |
590 s->data_block_counter);
591 buffer[1] = cpu_to_be32(CIP_EOH | CIP_FMT_AM | AMDTP_FDF_AM824 |
592 (s->sfc << CIP_FDF_SFC_SHIFT) | syt);
595 pcm = ACCESS_ONCE(s->pcm);
597 s->transfer_samples(s, pcm, buffer, data_blocks);
599 amdtp_fill_pcm_silence(s, buffer, data_blocks);
601 amdtp_fill_midi(s, buffer, data_blocks);
603 s->data_block_counter = (s->data_block_counter + data_blocks) & 0xff;
605 payload_length = 8 + data_blocks * 4 * s->data_block_quadlets;
606 if (queue_out_packet(s, payload_length, false) < 0) {
607 s->packet_index = -1;
608 amdtp_stream_pcm_abort(s);
613 update_pcm_pointers(s, pcm, data_blocks);
616 static void handle_in_packet(struct amdtp_stream *s,
617 unsigned int payload_quadlets,
621 unsigned int data_blocks, data_block_quadlets, data_block_counter;
622 struct snd_pcm_substream *pcm = NULL;
624 cip_header[0] = be32_to_cpu(buffer[0]);
625 cip_header[1] = be32_to_cpu(buffer[1]);
628 * This module supports 'Two-quadlet CIP header with SYT field'.
629 * For convenience, also check FMT field is AM824 or not.
631 if (((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
632 ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH) ||
633 ((cip_header[1] & CIP_FMT_MASK) != CIP_FMT_AM)) {
634 dev_info_ratelimited(&s->unit->device,
635 "Invalid CIP header for AMDTP: %08X:%08X\n",
636 cip_header[0], cip_header[1]);
640 /* Calculate data blocks */
641 if (payload_quadlets < 3 ||
642 ((cip_header[1] & CIP_FDF_MASK) ==
643 (AMDTP_FDF_NO_DATA << CIP_FDF_SFC_SHIFT))) {
646 data_block_quadlets =
647 (cip_header[0] & AMDTP_DBS_MASK) >> AMDTP_DBS_SHIFT;
648 /* avoid division by zero */
649 if (data_block_quadlets == 0) {
650 dev_info_ratelimited(&s->unit->device,
651 "Detect invalid value in dbs field: %08X\n",
656 data_blocks = (payload_quadlets - 2) / data_block_quadlets;
659 /* Check data block counter continuity */
660 data_block_counter = cip_header[0] & AMDTP_DBC_MASK;
661 if (data_block_counter != s->data_block_counter) {
662 dev_info(&s->unit->device,
663 "Detect discontinuity of CIP: %02X %02X\n",
664 s->data_block_counter, data_block_counter);
668 if (data_blocks > 0) {
671 pcm = ACCESS_ONCE(s->pcm);
673 s->transfer_samples(s, pcm, buffer, data_blocks);
676 amdtp_pull_midi(s, buffer, data_blocks);
679 s->data_block_counter = (data_block_counter + data_blocks) & 0xff;
681 if (queue_in_packet(s) < 0)
685 update_pcm_pointers(s, pcm, data_blocks);
689 s->packet_index = -1;
690 amdtp_stream_pcm_abort(s);
693 static void out_stream_callback(struct fw_iso_context *context, u32 cycle,
694 size_t header_length, void *header,
697 struct amdtp_stream *s = private_data;
698 unsigned int i, syt, packets = header_length / 4;
701 * Compute the cycle of the last queued packet.
702 * (We need only the four lowest bits for the SYT, so we can ignore
703 * that bits 0-11 must wrap around at 3072.)
705 cycle += QUEUE_LENGTH - packets;
707 for (i = 0; i < packets; ++i) {
708 syt = calculate_syt(s, ++cycle);
709 handle_out_packet(s, syt);
711 fw_iso_context_queue_flush(s->context);
714 static void in_stream_callback(struct fw_iso_context *context, u32 cycle,
715 size_t header_length, void *header,
718 struct amdtp_stream *s = private_data;
719 unsigned int p, syt, packets, payload_quadlets;
720 __be32 *buffer, *headers = header;
722 /* The number of packets in buffer */
723 packets = header_length / IN_PACKET_HEADER_SIZE;
725 for (p = 0; p < packets; p++) {
726 if (s->packet_index < 0)
729 buffer = s->buffer.packets[s->packet_index].buffer;
731 /* Process sync slave stream */
732 if (s->sync_slave && s->sync_slave->callbacked) {
733 syt = be32_to_cpu(buffer[1]) & CIP_SYT_MASK;
734 handle_out_packet(s->sync_slave, syt);
737 /* The number of quadlets in this packet */
739 (be32_to_cpu(headers[p]) >> ISO_DATA_LENGTH_SHIFT) / 4;
740 handle_in_packet(s, payload_quadlets, buffer);
743 /* Queueing error or detecting discontinuity */
744 if (s->packet_index < 0) {
745 /* Abort sync slave. */
747 s->sync_slave->packet_index = -1;
748 amdtp_stream_pcm_abort(s->sync_slave);
753 /* when sync to device, flush the packets for slave stream */
754 if (s->sync_slave && s->sync_slave->callbacked)
755 fw_iso_context_queue_flush(s->sync_slave->context);
757 fw_iso_context_queue_flush(s->context);
760 /* processing is done by master callback */
761 static void slave_stream_callback(struct fw_iso_context *context, u32 cycle,
762 size_t header_length, void *header,
768 /* this is executed one time */
769 static void amdtp_stream_first_callback(struct fw_iso_context *context,
770 u32 cycle, size_t header_length,
771 void *header, void *private_data)
773 struct amdtp_stream *s = private_data;
776 * For in-stream, first packet has come.
777 * For out-stream, prepared to transmit first packet
779 s->callbacked = true;
780 wake_up(&s->callback_wait);
782 if (s->direction == AMDTP_IN_STREAM)
783 context->callback.sc = in_stream_callback;
784 else if ((s->flags & CIP_BLOCKING) && (s->flags & CIP_SYNC_TO_DEVICE))
785 context->callback.sc = slave_stream_callback;
787 context->callback.sc = out_stream_callback;
789 context->callback.sc(context, cycle, header_length, header, s);
793 * amdtp_stream_start - start transferring packets
794 * @s: the AMDTP stream to start
795 * @channel: the isochronous channel on the bus
796 * @speed: firewire speed code
798 * The stream cannot be started until it has been configured with
799 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
800 * device can be started.
802 int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
804 static const struct {
805 unsigned int data_block;
806 unsigned int syt_offset;
807 } initial_state[] = {
808 [CIP_SFC_32000] = { 4, 3072 },
809 [CIP_SFC_48000] = { 6, 1024 },
810 [CIP_SFC_96000] = { 12, 1024 },
811 [CIP_SFC_192000] = { 24, 1024 },
812 [CIP_SFC_44100] = { 0, 67 },
813 [CIP_SFC_88200] = { 0, 67 },
814 [CIP_SFC_176400] = { 0, 67 },
816 unsigned int header_size;
817 enum dma_data_direction dir;
820 mutex_lock(&s->mutex);
822 if (WARN_ON(amdtp_stream_running(s) ||
823 (s->data_block_quadlets < 1))) {
828 s->data_block_counter = 0;
829 s->data_block_state = initial_state[s->sfc].data_block;
830 s->syt_offset_state = initial_state[s->sfc].syt_offset;
831 s->last_syt_offset = TICKS_PER_CYCLE;
833 /* initialize packet buffer */
834 if (s->direction == AMDTP_IN_STREAM) {
835 dir = DMA_FROM_DEVICE;
836 type = FW_ISO_CONTEXT_RECEIVE;
837 header_size = IN_PACKET_HEADER_SIZE;
840 type = FW_ISO_CONTEXT_TRANSMIT;
841 header_size = OUT_PACKET_HEADER_SIZE;
843 err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH,
844 amdtp_stream_get_max_payload(s), dir);
848 s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
849 type, channel, speed, header_size,
850 amdtp_stream_first_callback, s);
851 if (IS_ERR(s->context)) {
852 err = PTR_ERR(s->context);
854 dev_err(&s->unit->device,
855 "no free stream on this controller\n");
859 amdtp_stream_update(s);
863 if (s->direction == AMDTP_IN_STREAM)
864 err = queue_in_packet(s);
866 err = queue_out_packet(s, 0, true);
869 } while (s->packet_index > 0);
871 /* NOTE: TAG1 matches CIP. This just affects in stream. */
872 tag = FW_ISO_CONTEXT_MATCH_TAG1;
873 if (s->flags & CIP_EMPTY_WITH_TAG0)
874 tag |= FW_ISO_CONTEXT_MATCH_TAG0;
876 s->callbacked = false;
877 err = fw_iso_context_start(s->context, -1, 0, tag);
881 mutex_unlock(&s->mutex);
886 fw_iso_context_destroy(s->context);
887 s->context = ERR_PTR(-1);
889 iso_packets_buffer_destroy(&s->buffer, s->unit);
891 mutex_unlock(&s->mutex);
895 EXPORT_SYMBOL(amdtp_stream_start);
898 * amdtp_stream_pcm_pointer - get the PCM buffer position
899 * @s: the AMDTP stream that transports the PCM data
901 * Returns the current buffer position, in frames.
903 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream *s)
905 /* this optimization is allowed to be racy */
906 if (s->pointer_flush && amdtp_stream_running(s))
907 fw_iso_context_flush_completions(s->context);
909 s->pointer_flush = true;
911 return ACCESS_ONCE(s->pcm_buffer_pointer);
913 EXPORT_SYMBOL(amdtp_stream_pcm_pointer);
916 * amdtp_stream_update - update the stream after a bus reset
917 * @s: the AMDTP stream
919 void amdtp_stream_update(struct amdtp_stream *s)
921 ACCESS_ONCE(s->source_node_id_field) =
922 (fw_parent_device(s->unit)->card->node_id & 0x3f) << 24;
924 EXPORT_SYMBOL(amdtp_stream_update);
927 * amdtp_stream_stop - stop sending packets
928 * @s: the AMDTP stream to stop
930 * All PCM and MIDI devices of the stream must be stopped before the stream
931 * itself can be stopped.
933 void amdtp_stream_stop(struct amdtp_stream *s)
935 mutex_lock(&s->mutex);
937 if (!amdtp_stream_running(s)) {
938 mutex_unlock(&s->mutex);
942 tasklet_kill(&s->period_tasklet);
943 fw_iso_context_stop(s->context);
944 fw_iso_context_destroy(s->context);
945 s->context = ERR_PTR(-1);
946 iso_packets_buffer_destroy(&s->buffer, s->unit);
948 s->callbacked = false;
950 mutex_unlock(&s->mutex);
952 EXPORT_SYMBOL(amdtp_stream_stop);
955 * amdtp_stream_pcm_abort - abort the running PCM device
956 * @s: the AMDTP stream about to be stopped
958 * If the isochronous stream needs to be stopped asynchronously, call this
959 * function first to stop the PCM device.
961 void amdtp_stream_pcm_abort(struct amdtp_stream *s)
963 struct snd_pcm_substream *pcm;
965 pcm = ACCESS_ONCE(s->pcm);
967 snd_pcm_stream_lock_irq(pcm);
968 if (snd_pcm_running(pcm))
969 snd_pcm_stop(pcm, SNDRV_PCM_STATE_XRUN);
970 snd_pcm_stream_unlock_irq(pcm);
973 EXPORT_SYMBOL(amdtp_stream_pcm_abort);