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)
25 * Nominally 3125 bytes/second, but the MIDI port's clock might be
26 * 1% too slow, and the bus clock 100 ppm too fast.
28 #define MIDI_BYTES_PER_SECOND 3093
31 * Several devices look only at the first eight data blocks.
32 * In any case, this is more than enough for the MIDI data rate.
34 #define MAX_MIDI_RX_BLOCKS 8
36 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
38 /* isochronous header parameters */
39 #define ISO_DATA_LENGTH_SHIFT 16
42 /* common isochronous packet header parameters */
43 #define CIP_EOH (1u << 31)
44 #define CIP_EOH_MASK 0x80000000
45 #define CIP_FMT_AM (0x10 << 24)
46 #define CIP_FMT_MASK 0x3f000000
47 #define CIP_SYT_MASK 0x0000ffff
48 #define CIP_SYT_NO_INFO 0xffff
49 #define CIP_FDF_MASK 0x00ff0000
50 #define CIP_FDF_SFC_SHIFT 16
53 * Audio and Music transfer protocol specific parameters
54 * only "Clock-based rate control mode" is supported
56 #define AMDTP_FDF_AM824 (0 << (CIP_FDF_SFC_SHIFT + 3))
57 #define AMDTP_FDF_NO_DATA 0xff
58 #define AMDTP_DBS_MASK 0x00ff0000
59 #define AMDTP_DBS_SHIFT 16
60 #define AMDTP_DBC_MASK 0x000000ff
62 /* TODO: make these configurable */
63 #define INTERRUPT_INTERVAL 16
64 #define QUEUE_LENGTH 48
66 #define IN_PACKET_HEADER_SIZE 4
67 #define OUT_PACKET_HEADER_SIZE 0
69 static void pcm_period_tasklet(unsigned long data
);
72 * amdtp_stream_init - initialize an AMDTP stream structure
73 * @s: the AMDTP stream to initialize
74 * @unit: the target of the stream
75 * @dir: the direction of stream
76 * @flags: the packet transmission method to use
78 int amdtp_stream_init(struct amdtp_stream
*s
, struct fw_unit
*unit
,
79 enum amdtp_stream_direction dir
, enum cip_flags flags
)
84 s
->context
= ERR_PTR(-1);
85 mutex_init(&s
->mutex
);
86 tasklet_init(&s
->period_tasklet
, pcm_period_tasklet
, (unsigned long)s
);
89 init_waitqueue_head(&s
->callback_wait
);
90 s
->callbacked
= false;
95 EXPORT_SYMBOL(amdtp_stream_init
);
98 * amdtp_stream_destroy - free stream resources
99 * @s: the AMDTP stream to destroy
101 void amdtp_stream_destroy(struct amdtp_stream
*s
)
103 WARN_ON(amdtp_stream_running(s
));
104 mutex_destroy(&s
->mutex
);
106 EXPORT_SYMBOL(amdtp_stream_destroy
);
108 const unsigned int amdtp_syt_intervals
[CIP_SFC_COUNT
] = {
112 [CIP_SFC_88200
] = 16,
113 [CIP_SFC_96000
] = 16,
114 [CIP_SFC_176400
] = 32,
115 [CIP_SFC_192000
] = 32,
117 EXPORT_SYMBOL(amdtp_syt_intervals
);
119 const unsigned int amdtp_rate_table
[CIP_SFC_COUNT
] = {
120 [CIP_SFC_32000
] = 32000,
121 [CIP_SFC_44100
] = 44100,
122 [CIP_SFC_48000
] = 48000,
123 [CIP_SFC_88200
] = 88200,
124 [CIP_SFC_96000
] = 96000,
125 [CIP_SFC_176400
] = 176400,
126 [CIP_SFC_192000
] = 192000,
128 EXPORT_SYMBOL(amdtp_rate_table
);
131 * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
132 * @s: the AMDTP stream, which must be initialized.
133 * @runtime: the PCM substream runtime
135 int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream
*s
,
136 struct snd_pcm_runtime
*runtime
)
140 /* AM824 in IEC 61883-6 can deliver 24bit data */
141 err
= snd_pcm_hw_constraint_msbits(runtime
, 0, 32, 24);
146 * Currently firewire-lib processes 16 packets in one software
147 * interrupt callback. This equals to 2msec but actually the
148 * interval of the interrupts has a jitter.
149 * Additionally, even if adding a constraint to fit period size to
150 * 2msec, actual calculated frames per period doesn't equal to 2msec,
151 * depending on sampling rate.
152 * Anyway, the interval to call snd_pcm_period_elapsed() cannot 2msec.
153 * Here let us use 5msec for safe period interrupt.
155 err
= snd_pcm_hw_constraint_minmax(runtime
,
156 SNDRV_PCM_HW_PARAM_PERIOD_TIME
,
161 /* Non-Blocking stream has no more constraints */
162 if (!(s
->flags
& CIP_BLOCKING
))
166 * One AMDTP packet can include some frames. In blocking mode, the
167 * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
168 * depending on its sampling rate. For accurate period interrupt, it's
169 * preferrable to align period/buffer sizes to current SYT_INTERVAL.
171 * TODO: These constraints can be improved with proper rules.
172 * Currently apply LCM of SYT_INTERVALs.
174 err
= snd_pcm_hw_constraint_step(runtime
, 0,
175 SNDRV_PCM_HW_PARAM_PERIOD_SIZE
, 32);
178 err
= snd_pcm_hw_constraint_step(runtime
, 0,
179 SNDRV_PCM_HW_PARAM_BUFFER_SIZE
, 32);
183 EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints
);
186 * amdtp_stream_set_parameters - set stream parameters
187 * @s: the AMDTP stream to configure
188 * @rate: the sample rate
189 * @pcm_channels: the number of PCM samples in each data block, to be encoded
190 * as AM824 multi-bit linear audio
191 * @midi_ports: the number of MIDI ports (i.e., MPX-MIDI Data Channels)
193 * The parameters must be set before the stream is started, and must not be
194 * changed while the stream is running.
196 void amdtp_stream_set_parameters(struct amdtp_stream
*s
,
198 unsigned int pcm_channels
,
199 unsigned int midi_ports
)
201 unsigned int i
, sfc
, midi_channels
;
203 midi_channels
= DIV_ROUND_UP(midi_ports
, 8);
205 if (WARN_ON(amdtp_stream_running(s
)) |
206 WARN_ON(pcm_channels
> AMDTP_MAX_CHANNELS_FOR_PCM
) |
207 WARN_ON(midi_channels
> AMDTP_MAX_CHANNELS_FOR_MIDI
))
210 for (sfc
= 0; sfc
< ARRAY_SIZE(amdtp_rate_table
); ++sfc
)
211 if (amdtp_rate_table
[sfc
] == rate
)
217 s
->pcm_channels
= pcm_channels
;
219 s
->data_block_quadlets
= s
->pcm_channels
+ midi_channels
;
220 s
->midi_ports
= midi_ports
;
222 s
->syt_interval
= amdtp_syt_intervals
[sfc
];
224 /* default buffering in the device */
225 s
->transfer_delay
= TRANSFER_DELAY_TICKS
- TICKS_PER_CYCLE
;
226 if (s
->flags
& CIP_BLOCKING
)
227 /* additional buffering needed to adjust for no-data packets */
228 s
->transfer_delay
+= TICKS_PER_SECOND
* s
->syt_interval
/ rate
;
230 /* init the position map for PCM and MIDI channels */
231 for (i
= 0; i
< pcm_channels
; i
++)
232 s
->pcm_positions
[i
] = i
;
233 s
->midi_position
= s
->pcm_channels
;
236 * We do not know the actual MIDI FIFO size of most devices. Just
237 * assume two bytes, i.e., one byte can be received over the bus while
238 * the previous one is transmitted over MIDI.
239 * (The value here is adjusted for midi_ratelimit_per_packet().)
241 s
->midi_fifo_limit
= rate
- MIDI_BYTES_PER_SECOND
* s
->syt_interval
+ 1;
243 EXPORT_SYMBOL(amdtp_stream_set_parameters
);
246 * amdtp_stream_get_max_payload - get the stream's packet size
247 * @s: the AMDTP stream
249 * This function must not be called before the stream has been configured
250 * with amdtp_stream_set_parameters().
252 unsigned int amdtp_stream_get_max_payload(struct amdtp_stream
*s
)
254 return 8 + s
->syt_interval
* s
->data_block_quadlets
* 4;
256 EXPORT_SYMBOL(amdtp_stream_get_max_payload
);
258 static void amdtp_write_s16(struct amdtp_stream
*s
,
259 struct snd_pcm_substream
*pcm
,
260 __be32
*buffer
, unsigned int frames
);
261 static void amdtp_write_s32(struct amdtp_stream
*s
,
262 struct snd_pcm_substream
*pcm
,
263 __be32
*buffer
, unsigned int frames
);
264 static void amdtp_read_s32(struct amdtp_stream
*s
,
265 struct snd_pcm_substream
*pcm
,
266 __be32
*buffer
, unsigned int frames
);
269 * amdtp_stream_set_pcm_format - set the PCM format
270 * @s: the AMDTP stream to configure
271 * @format: the format of the ALSA PCM device
273 * The sample format must be set after the other parameters (rate/PCM channels/
274 * MIDI) and before the stream is started, and must not be changed while the
277 void amdtp_stream_set_pcm_format(struct amdtp_stream
*s
,
278 snd_pcm_format_t format
)
280 if (WARN_ON(amdtp_stream_pcm_running(s
)))
287 case SNDRV_PCM_FORMAT_S16
:
288 if (s
->direction
== AMDTP_OUT_STREAM
) {
289 s
->transfer_samples
= amdtp_write_s16
;
294 case SNDRV_PCM_FORMAT_S32
:
295 if (s
->direction
== AMDTP_OUT_STREAM
)
296 s
->transfer_samples
= amdtp_write_s32
;
298 s
->transfer_samples
= amdtp_read_s32
;
302 EXPORT_SYMBOL(amdtp_stream_set_pcm_format
);
305 * amdtp_stream_pcm_prepare - prepare PCM device for running
306 * @s: the AMDTP stream
308 * This function should be called from the PCM device's .prepare callback.
310 void amdtp_stream_pcm_prepare(struct amdtp_stream
*s
)
312 tasklet_kill(&s
->period_tasklet
);
313 s
->pcm_buffer_pointer
= 0;
314 s
->pcm_period_pointer
= 0;
315 s
->pointer_flush
= true;
317 EXPORT_SYMBOL(amdtp_stream_pcm_prepare
);
319 static unsigned int calculate_data_blocks(struct amdtp_stream
*s
)
321 unsigned int phase
, data_blocks
;
323 if (s
->flags
& CIP_BLOCKING
)
324 data_blocks
= s
->syt_interval
;
325 else if (!cip_sfc_is_base_44100(s
->sfc
)) {
326 /* Sample_rate / 8000 is an integer, and precomputed. */
327 data_blocks
= s
->data_block_state
;
329 phase
= s
->data_block_state
;
332 * This calculates the number of data blocks per packet so that
333 * 1) the overall rate is correct and exactly synchronized to
335 * 2) packets with a rounded-up number of blocks occur as early
336 * as possible in the sequence (to prevent underruns of the
339 if (s
->sfc
== CIP_SFC_44100
)
340 /* 6 6 5 6 5 6 5 ... */
341 data_blocks
= 5 + ((phase
& 1) ^
342 (phase
== 0 || phase
>= 40));
344 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
345 data_blocks
= 11 * (s
->sfc
>> 1) + (phase
== 0);
346 if (++phase
>= (80 >> (s
->sfc
>> 1)))
348 s
->data_block_state
= phase
;
354 static unsigned int calculate_syt(struct amdtp_stream
*s
,
357 unsigned int syt_offset
, phase
, index
, syt
;
359 if (s
->last_syt_offset
< TICKS_PER_CYCLE
) {
360 if (!cip_sfc_is_base_44100(s
->sfc
))
361 syt_offset
= s
->last_syt_offset
+ s
->syt_offset_state
;
364 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
365 * n * SYT_INTERVAL * 24576000 / sample_rate
366 * Modulo TICKS_PER_CYCLE, the difference between successive
367 * elements is about 1386.23. Rounding the results of this
368 * formula to the SYT precision results in a sequence of
369 * differences that begins with:
370 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
371 * This code generates _exactly_ the same sequence.
373 phase
= s
->syt_offset_state
;
375 syt_offset
= s
->last_syt_offset
;
376 syt_offset
+= 1386 + ((index
&& !(index
& 3)) ||
380 s
->syt_offset_state
= phase
;
383 syt_offset
= s
->last_syt_offset
- TICKS_PER_CYCLE
;
384 s
->last_syt_offset
= syt_offset
;
386 if (syt_offset
< TICKS_PER_CYCLE
) {
387 syt_offset
+= s
->transfer_delay
;
388 syt
= (cycle
+ syt_offset
/ TICKS_PER_CYCLE
) << 12;
389 syt
+= syt_offset
% TICKS_PER_CYCLE
;
391 return syt
& CIP_SYT_MASK
;
393 return CIP_SYT_NO_INFO
;
397 static void amdtp_write_s32(struct amdtp_stream
*s
,
398 struct snd_pcm_substream
*pcm
,
399 __be32
*buffer
, unsigned int frames
)
401 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
402 unsigned int channels
, remaining_frames
, i
, c
;
405 channels
= s
->pcm_channels
;
406 src
= (void *)runtime
->dma_area
+
407 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
408 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
410 for (i
= 0; i
< frames
; ++i
) {
411 for (c
= 0; c
< channels
; ++c
) {
412 buffer
[s
->pcm_positions
[c
]] =
413 cpu_to_be32((*src
>> 8) | 0x40000000);
416 buffer
+= s
->data_block_quadlets
;
417 if (--remaining_frames
== 0)
418 src
= (void *)runtime
->dma_area
;
422 static void amdtp_write_s16(struct amdtp_stream
*s
,
423 struct snd_pcm_substream
*pcm
,
424 __be32
*buffer
, unsigned int frames
)
426 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
427 unsigned int channels
, remaining_frames
, i
, c
;
430 channels
= s
->pcm_channels
;
431 src
= (void *)runtime
->dma_area
+
432 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
433 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
435 for (i
= 0; i
< frames
; ++i
) {
436 for (c
= 0; c
< channels
; ++c
) {
437 buffer
[s
->pcm_positions
[c
]] =
438 cpu_to_be32((*src
<< 8) | 0x42000000);
441 buffer
+= s
->data_block_quadlets
;
442 if (--remaining_frames
== 0)
443 src
= (void *)runtime
->dma_area
;
447 static void amdtp_read_s32(struct amdtp_stream
*s
,
448 struct snd_pcm_substream
*pcm
,
449 __be32
*buffer
, unsigned int frames
)
451 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
452 unsigned int channels
, remaining_frames
, i
, c
;
455 channels
= s
->pcm_channels
;
456 dst
= (void *)runtime
->dma_area
+
457 frames_to_bytes(runtime
, s
->pcm_buffer_pointer
);
458 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
460 for (i
= 0; i
< frames
; ++i
) {
461 for (c
= 0; c
< channels
; ++c
) {
462 *dst
= be32_to_cpu(buffer
[s
->pcm_positions
[c
]]) << 8;
465 buffer
+= s
->data_block_quadlets
;
466 if (--remaining_frames
== 0)
467 dst
= (void *)runtime
->dma_area
;
471 static void amdtp_fill_pcm_silence(struct amdtp_stream
*s
,
472 __be32
*buffer
, unsigned int frames
)
476 for (i
= 0; i
< frames
; ++i
) {
477 for (c
= 0; c
< s
->pcm_channels
; ++c
)
478 buffer
[s
->pcm_positions
[c
]] = cpu_to_be32(0x40000000);
479 buffer
+= s
->data_block_quadlets
;
484 * To avoid sending MIDI bytes at too high a rate, assume that the receiving
485 * device has a FIFO, and track how much it is filled. This values increases
486 * by one whenever we send one byte in a packet, but the FIFO empties at
487 * a constant rate independent of our packet rate. One packet has syt_interval
488 * samples, so the number of bytes that empty out of the FIFO, per packet(!),
489 * is MIDI_BYTES_PER_SECOND * syt_interval / sample_rate. To avoid storing
490 * fractional values, the values in midi_fifo_used[] are measured in bytes
491 * multiplied by the sample rate.
493 static bool midi_ratelimit_per_packet(struct amdtp_stream
*s
, unsigned int port
)
497 used
= s
->midi_fifo_used
[port
];
498 if (used
== 0) /* common shortcut */
501 used
-= MIDI_BYTES_PER_SECOND
* s
->syt_interval
;
503 s
->midi_fifo_used
[port
] = used
;
505 return used
< s
->midi_fifo_limit
;
508 static void midi_rate_use_one_byte(struct amdtp_stream
*s
, unsigned int port
)
510 s
->midi_fifo_used
[port
] += amdtp_rate_table
[s
->sfc
];
513 static void amdtp_fill_midi(struct amdtp_stream
*s
,
514 __be32
*buffer
, unsigned int frames
)
516 unsigned int f
, port
;
519 for (f
= 0; f
< frames
; f
++) {
520 b
= (u8
*)&buffer
[s
->midi_position
];
522 port
= (s
->data_block_counter
+ f
) % 8;
523 if (f
< MAX_MIDI_RX_BLOCKS
&&
524 midi_ratelimit_per_packet(s
, port
) &&
525 s
->midi
[port
] != NULL
&&
526 snd_rawmidi_transmit(s
->midi
[port
], &b
[1], 1) == 1) {
527 midi_rate_use_one_byte(s
, port
);
536 buffer
+= s
->data_block_quadlets
;
540 static void amdtp_pull_midi(struct amdtp_stream
*s
,
541 __be32
*buffer
, unsigned int frames
)
543 unsigned int f
, port
;
547 for (f
= 0; f
< frames
; f
++) {
548 port
= (s
->data_block_counter
+ f
) % 8;
549 b
= (u8
*)&buffer
[s
->midi_position
];
552 if ((1 <= len
) && (len
<= 3) && (s
->midi
[port
]))
553 snd_rawmidi_receive(s
->midi
[port
], b
+ 1, len
);
555 buffer
+= s
->data_block_quadlets
;
559 static void update_pcm_pointers(struct amdtp_stream
*s
,
560 struct snd_pcm_substream
*pcm
,
566 * In IEC 61883-6, one data block represents one event. In ALSA, one
567 * event equals to one PCM frame. But Dice has a quirk to transfer
568 * two PCM frames in one data block.
570 if (s
->double_pcm_frames
)
573 ptr
= s
->pcm_buffer_pointer
+ frames
;
574 if (ptr
>= pcm
->runtime
->buffer_size
)
575 ptr
-= pcm
->runtime
->buffer_size
;
576 ACCESS_ONCE(s
->pcm_buffer_pointer
) = ptr
;
578 s
->pcm_period_pointer
+= frames
;
579 if (s
->pcm_period_pointer
>= pcm
->runtime
->period_size
) {
580 s
->pcm_period_pointer
-= pcm
->runtime
->period_size
;
581 s
->pointer_flush
= false;
582 tasklet_hi_schedule(&s
->period_tasklet
);
586 static void pcm_period_tasklet(unsigned long data
)
588 struct amdtp_stream
*s
= (void *)data
;
589 struct snd_pcm_substream
*pcm
= ACCESS_ONCE(s
->pcm
);
592 snd_pcm_period_elapsed(pcm
);
595 static int queue_packet(struct amdtp_stream
*s
,
596 unsigned int header_length
,
597 unsigned int payload_length
, bool skip
)
599 struct fw_iso_packet p
= {0};
602 if (IS_ERR(s
->context
))
605 p
.interrupt
= IS_ALIGNED(s
->packet_index
+ 1, INTERRUPT_INTERVAL
);
607 p
.header_length
= header_length
;
608 p
.payload_length
= (!skip
) ? payload_length
: 0;
610 err
= fw_iso_context_queue(s
->context
, &p
, &s
->buffer
.iso_buffer
,
611 s
->buffer
.packets
[s
->packet_index
].offset
);
613 dev_err(&s
->unit
->device
, "queueing error: %d\n", err
);
617 if (++s
->packet_index
>= QUEUE_LENGTH
)
623 static inline int queue_out_packet(struct amdtp_stream
*s
,
624 unsigned int payload_length
, bool skip
)
626 return queue_packet(s
, OUT_PACKET_HEADER_SIZE
,
627 payload_length
, skip
);
630 static inline int queue_in_packet(struct amdtp_stream
*s
)
632 return queue_packet(s
, IN_PACKET_HEADER_SIZE
,
633 amdtp_stream_get_max_payload(s
), false);
636 static void handle_out_packet(struct amdtp_stream
*s
, unsigned int syt
)
639 unsigned int data_blocks
, payload_length
;
640 struct snd_pcm_substream
*pcm
;
642 if (s
->packet_index
< 0)
645 /* this module generate empty packet for 'no data' */
646 if (!(s
->flags
& CIP_BLOCKING
) || (syt
!= CIP_SYT_NO_INFO
))
647 data_blocks
= calculate_data_blocks(s
);
651 buffer
= s
->buffer
.packets
[s
->packet_index
].buffer
;
652 buffer
[0] = cpu_to_be32(ACCESS_ONCE(s
->source_node_id_field
) |
653 (s
->data_block_quadlets
<< AMDTP_DBS_SHIFT
) |
654 s
->data_block_counter
);
655 buffer
[1] = cpu_to_be32(CIP_EOH
| CIP_FMT_AM
| AMDTP_FDF_AM824
|
656 (s
->sfc
<< CIP_FDF_SFC_SHIFT
) | syt
);
659 pcm
= ACCESS_ONCE(s
->pcm
);
661 s
->transfer_samples(s
, pcm
, buffer
, data_blocks
);
663 amdtp_fill_pcm_silence(s
, buffer
, data_blocks
);
665 amdtp_fill_midi(s
, buffer
, data_blocks
);
667 s
->data_block_counter
= (s
->data_block_counter
+ data_blocks
) & 0xff;
669 payload_length
= 8 + data_blocks
* 4 * s
->data_block_quadlets
;
670 if (queue_out_packet(s
, payload_length
, false) < 0) {
671 s
->packet_index
= -1;
672 amdtp_stream_pcm_abort(s
);
677 update_pcm_pointers(s
, pcm
, data_blocks
);
680 static void handle_in_packet(struct amdtp_stream
*s
,
681 unsigned int payload_quadlets
,
685 unsigned int data_blocks
, data_block_quadlets
, data_block_counter
,
687 struct snd_pcm_substream
*pcm
= NULL
;
690 cip_header
[0] = be32_to_cpu(buffer
[0]);
691 cip_header
[1] = be32_to_cpu(buffer
[1]);
694 * This module supports 'Two-quadlet CIP header with SYT field'.
695 * For convenience, also check FMT field is AM824 or not.
697 if (((cip_header
[0] & CIP_EOH_MASK
) == CIP_EOH
) ||
698 ((cip_header
[1] & CIP_EOH_MASK
) != CIP_EOH
) ||
699 ((cip_header
[1] & CIP_FMT_MASK
) != CIP_FMT_AM
)) {
700 dev_info_ratelimited(&s
->unit
->device
,
701 "Invalid CIP header for AMDTP: %08X:%08X\n",
702 cip_header
[0], cip_header
[1]);
706 /* Calculate data blocks */
707 if (payload_quadlets
< 3 ||
708 ((cip_header
[1] & CIP_FDF_MASK
) ==
709 (AMDTP_FDF_NO_DATA
<< CIP_FDF_SFC_SHIFT
))) {
712 data_block_quadlets
=
713 (cip_header
[0] & AMDTP_DBS_MASK
) >> AMDTP_DBS_SHIFT
;
714 /* avoid division by zero */
715 if (data_block_quadlets
== 0) {
716 dev_info_ratelimited(&s
->unit
->device
,
717 "Detect invalid value in dbs field: %08X\n",
721 if (s
->flags
& CIP_WRONG_DBS
)
722 data_block_quadlets
= s
->data_block_quadlets
;
724 data_blocks
= (payload_quadlets
- 2) / data_block_quadlets
;
727 /* Check data block counter continuity */
728 data_block_counter
= cip_header
[0] & AMDTP_DBC_MASK
;
729 if (data_blocks
== 0 && (s
->flags
& CIP_EMPTY_HAS_WRONG_DBC
) &&
730 s
->data_block_counter
!= UINT_MAX
)
731 data_block_counter
= s
->data_block_counter
;
733 if (((s
->flags
& CIP_SKIP_DBC_ZERO_CHECK
) &&
734 data_block_counter
== s
->tx_first_dbc
) ||
735 s
->data_block_counter
== UINT_MAX
) {
737 } else if (!(s
->flags
& CIP_DBC_IS_END_EVENT
)) {
738 lost
= data_block_counter
!= s
->data_block_counter
;
740 if ((data_blocks
> 0) && (s
->tx_dbc_interval
> 0))
741 dbc_interval
= s
->tx_dbc_interval
;
743 dbc_interval
= data_blocks
;
745 lost
= data_block_counter
!=
746 ((s
->data_block_counter
+ dbc_interval
) & 0xff);
750 dev_info(&s
->unit
->device
,
751 "Detect discontinuity of CIP: %02X %02X\n",
752 s
->data_block_counter
, data_block_counter
);
756 if (data_blocks
> 0) {
759 pcm
= ACCESS_ONCE(s
->pcm
);
761 s
->transfer_samples(s
, pcm
, buffer
, data_blocks
);
764 amdtp_pull_midi(s
, buffer
, data_blocks
);
767 if (s
->flags
& CIP_DBC_IS_END_EVENT
)
768 s
->data_block_counter
= data_block_counter
;
770 s
->data_block_counter
=
771 (data_block_counter
+ data_blocks
) & 0xff;
773 if (queue_in_packet(s
) < 0)
777 update_pcm_pointers(s
, pcm
, data_blocks
);
781 s
->packet_index
= -1;
782 amdtp_stream_pcm_abort(s
);
785 static void out_stream_callback(struct fw_iso_context
*context
, u32 cycle
,
786 size_t header_length
, void *header
,
789 struct amdtp_stream
*s
= private_data
;
790 unsigned int i
, syt
, packets
= header_length
/ 4;
793 * Compute the cycle of the last queued packet.
794 * (We need only the four lowest bits for the SYT, so we can ignore
795 * that bits 0-11 must wrap around at 3072.)
797 cycle
+= QUEUE_LENGTH
- packets
;
799 for (i
= 0; i
< packets
; ++i
) {
800 syt
= calculate_syt(s
, ++cycle
);
801 handle_out_packet(s
, syt
);
803 fw_iso_context_queue_flush(s
->context
);
806 static void in_stream_callback(struct fw_iso_context
*context
, u32 cycle
,
807 size_t header_length
, void *header
,
810 struct amdtp_stream
*s
= private_data
;
811 unsigned int p
, syt
, packets
, payload_quadlets
;
812 __be32
*buffer
, *headers
= header
;
814 /* The number of packets in buffer */
815 packets
= header_length
/ IN_PACKET_HEADER_SIZE
;
817 for (p
= 0; p
< packets
; p
++) {
818 if (s
->packet_index
< 0)
821 buffer
= s
->buffer
.packets
[s
->packet_index
].buffer
;
823 /* Process sync slave stream */
824 if (s
->sync_slave
&& s
->sync_slave
->callbacked
) {
825 syt
= be32_to_cpu(buffer
[1]) & CIP_SYT_MASK
;
826 handle_out_packet(s
->sync_slave
, syt
);
829 /* The number of quadlets in this packet */
831 (be32_to_cpu(headers
[p
]) >> ISO_DATA_LENGTH_SHIFT
) / 4;
832 handle_in_packet(s
, payload_quadlets
, buffer
);
835 /* Queueing error or detecting discontinuity */
836 if (s
->packet_index
< 0) {
837 /* Abort sync slave. */
839 s
->sync_slave
->packet_index
= -1;
840 amdtp_stream_pcm_abort(s
->sync_slave
);
845 /* when sync to device, flush the packets for slave stream */
846 if (s
->sync_slave
&& s
->sync_slave
->callbacked
)
847 fw_iso_context_queue_flush(s
->sync_slave
->context
);
849 fw_iso_context_queue_flush(s
->context
);
852 /* processing is done by master callback */
853 static void slave_stream_callback(struct fw_iso_context
*context
, u32 cycle
,
854 size_t header_length
, void *header
,
860 /* this is executed one time */
861 static void amdtp_stream_first_callback(struct fw_iso_context
*context
,
862 u32 cycle
, size_t header_length
,
863 void *header
, void *private_data
)
865 struct amdtp_stream
*s
= private_data
;
868 * For in-stream, first packet has come.
869 * For out-stream, prepared to transmit first packet
871 s
->callbacked
= true;
872 wake_up(&s
->callback_wait
);
874 if (s
->direction
== AMDTP_IN_STREAM
)
875 context
->callback
.sc
= in_stream_callback
;
876 else if ((s
->flags
& CIP_BLOCKING
) && (s
->flags
& CIP_SYNC_TO_DEVICE
))
877 context
->callback
.sc
= slave_stream_callback
;
879 context
->callback
.sc
= out_stream_callback
;
881 context
->callback
.sc(context
, cycle
, header_length
, header
, s
);
885 * amdtp_stream_start - start transferring packets
886 * @s: the AMDTP stream to start
887 * @channel: the isochronous channel on the bus
888 * @speed: firewire speed code
890 * The stream cannot be started until it has been configured with
891 * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
892 * device can be started.
894 int amdtp_stream_start(struct amdtp_stream
*s
, int channel
, int speed
)
896 static const struct {
897 unsigned int data_block
;
898 unsigned int syt_offset
;
899 } initial_state
[] = {
900 [CIP_SFC_32000
] = { 4, 3072 },
901 [CIP_SFC_48000
] = { 6, 1024 },
902 [CIP_SFC_96000
] = { 12, 1024 },
903 [CIP_SFC_192000
] = { 24, 1024 },
904 [CIP_SFC_44100
] = { 0, 67 },
905 [CIP_SFC_88200
] = { 0, 67 },
906 [CIP_SFC_176400
] = { 0, 67 },
908 unsigned int header_size
;
909 enum dma_data_direction dir
;
912 mutex_lock(&s
->mutex
);
914 if (WARN_ON(amdtp_stream_running(s
) ||
915 (s
->data_block_quadlets
< 1))) {
920 if (s
->direction
== AMDTP_IN_STREAM
&&
921 s
->flags
& CIP_SKIP_INIT_DBC_CHECK
)
922 s
->data_block_counter
= UINT_MAX
;
924 s
->data_block_counter
= 0;
925 s
->data_block_state
= initial_state
[s
->sfc
].data_block
;
926 s
->syt_offset_state
= initial_state
[s
->sfc
].syt_offset
;
927 s
->last_syt_offset
= TICKS_PER_CYCLE
;
929 /* initialize packet buffer */
930 if (s
->direction
== AMDTP_IN_STREAM
) {
931 dir
= DMA_FROM_DEVICE
;
932 type
= FW_ISO_CONTEXT_RECEIVE
;
933 header_size
= IN_PACKET_HEADER_SIZE
;
936 type
= FW_ISO_CONTEXT_TRANSMIT
;
937 header_size
= OUT_PACKET_HEADER_SIZE
;
939 err
= iso_packets_buffer_init(&s
->buffer
, s
->unit
, QUEUE_LENGTH
,
940 amdtp_stream_get_max_payload(s
), dir
);
944 s
->context
= fw_iso_context_create(fw_parent_device(s
->unit
)->card
,
945 type
, channel
, speed
, header_size
,
946 amdtp_stream_first_callback
, s
);
947 if (IS_ERR(s
->context
)) {
948 err
= PTR_ERR(s
->context
);
950 dev_err(&s
->unit
->device
,
951 "no free stream on this controller\n");
955 amdtp_stream_update(s
);
959 if (s
->direction
== AMDTP_IN_STREAM
)
960 err
= queue_in_packet(s
);
962 err
= queue_out_packet(s
, 0, true);
965 } while (s
->packet_index
> 0);
967 /* NOTE: TAG1 matches CIP. This just affects in stream. */
968 tag
= FW_ISO_CONTEXT_MATCH_TAG1
;
969 if (s
->flags
& CIP_EMPTY_WITH_TAG0
)
970 tag
|= FW_ISO_CONTEXT_MATCH_TAG0
;
972 s
->callbacked
= false;
973 err
= fw_iso_context_start(s
->context
, -1, 0, tag
);
977 mutex_unlock(&s
->mutex
);
982 fw_iso_context_destroy(s
->context
);
983 s
->context
= ERR_PTR(-1);
985 iso_packets_buffer_destroy(&s
->buffer
, s
->unit
);
987 mutex_unlock(&s
->mutex
);
991 EXPORT_SYMBOL(amdtp_stream_start
);
994 * amdtp_stream_pcm_pointer - get the PCM buffer position
995 * @s: the AMDTP stream that transports the PCM data
997 * Returns the current buffer position, in frames.
999 unsigned long amdtp_stream_pcm_pointer(struct amdtp_stream
*s
)
1001 /* this optimization is allowed to be racy */
1002 if (s
->pointer_flush
&& amdtp_stream_running(s
))
1003 fw_iso_context_flush_completions(s
->context
);
1005 s
->pointer_flush
= true;
1007 return ACCESS_ONCE(s
->pcm_buffer_pointer
);
1009 EXPORT_SYMBOL(amdtp_stream_pcm_pointer
);
1012 * amdtp_stream_update - update the stream after a bus reset
1013 * @s: the AMDTP stream
1015 void amdtp_stream_update(struct amdtp_stream
*s
)
1017 ACCESS_ONCE(s
->source_node_id_field
) =
1018 (fw_parent_device(s
->unit
)->card
->node_id
& 0x3f) << 24;
1020 EXPORT_SYMBOL(amdtp_stream_update
);
1023 * amdtp_stream_stop - stop sending packets
1024 * @s: the AMDTP stream to stop
1026 * All PCM and MIDI devices of the stream must be stopped before the stream
1027 * itself can be stopped.
1029 void amdtp_stream_stop(struct amdtp_stream
*s
)
1031 mutex_lock(&s
->mutex
);
1033 if (!amdtp_stream_running(s
)) {
1034 mutex_unlock(&s
->mutex
);
1038 tasklet_kill(&s
->period_tasklet
);
1039 fw_iso_context_stop(s
->context
);
1040 fw_iso_context_destroy(s
->context
);
1041 s
->context
= ERR_PTR(-1);
1042 iso_packets_buffer_destroy(&s
->buffer
, s
->unit
);
1044 s
->callbacked
= false;
1046 mutex_unlock(&s
->mutex
);
1048 EXPORT_SYMBOL(amdtp_stream_stop
);
1051 * amdtp_stream_pcm_abort - abort the running PCM device
1052 * @s: the AMDTP stream about to be stopped
1054 * If the isochronous stream needs to be stopped asynchronously, call this
1055 * function first to stop the PCM device.
1057 void amdtp_stream_pcm_abort(struct amdtp_stream
*s
)
1059 struct snd_pcm_substream
*pcm
;
1061 pcm
= ACCESS_ONCE(s
->pcm
);
1063 snd_pcm_stop_xrun(pcm
);
1065 EXPORT_SYMBOL(amdtp_stream_pcm_abort
);