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 <sound/pcm.h>
17 #define TICKS_PER_CYCLE 3072
18 #define CYCLES_PER_SECOND 8000
19 #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
21 #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 µs */
25 #define CIP_EOH (1u << 31)
26 #define CIP_FMT_AM (0x10 << 24)
27 #define AMDTP_FDF_AM824 (0 << 19)
28 #define AMDTP_FDF_SFC_SHIFT 16
30 /* TODO: make these configurable */
31 #define INTERRUPT_INTERVAL 16
32 #define QUEUE_LENGTH 48
35 * amdtp_out_stream_init - initialize an AMDTP output stream structure
36 * @s: the AMDTP output stream to initialize
37 * @unit: the target of the stream
38 * @flags: the packet transmission method to use
40 int amdtp_out_stream_init(struct amdtp_out_stream
*s
, struct fw_unit
*unit
,
41 enum cip_out_flags flags
)
43 s
->unit
= fw_unit_get(unit
);
45 s
->context
= ERR_PTR(-1);
46 mutex_init(&s
->mutex
);
51 EXPORT_SYMBOL(amdtp_out_stream_init
);
54 * amdtp_out_stream_destroy - free stream resources
55 * @s: the AMDTP output stream to destroy
57 void amdtp_out_stream_destroy(struct amdtp_out_stream
*s
)
59 WARN_ON(!IS_ERR(s
->context
));
60 mutex_destroy(&s
->mutex
);
63 EXPORT_SYMBOL(amdtp_out_stream_destroy
);
66 * amdtp_out_stream_set_rate - set the sample rate
67 * @s: the AMDTP output stream to configure
68 * @rate: the sample rate
70 * The sample rate must be set before the stream is started, and must not be
71 * changed while the stream is running.
73 void amdtp_out_stream_set_rate(struct amdtp_out_stream
*s
, unsigned int rate
)
77 unsigned int syt_interval
;
79 [CIP_SFC_32000
] = { 32000, 8, },
80 [CIP_SFC_44100
] = { 44100, 8, },
81 [CIP_SFC_48000
] = { 48000, 8, },
82 [CIP_SFC_88200
] = { 88200, 16, },
83 [CIP_SFC_96000
] = { 96000, 16, },
84 [CIP_SFC_176400
] = { 176400, 32, },
85 [CIP_SFC_192000
] = { 192000, 32, },
89 if (WARN_ON(!IS_ERR(s
->context
)))
92 for (sfc
= 0; sfc
< ARRAY_SIZE(rate_info
); ++sfc
)
93 if (rate_info
[sfc
].rate
== rate
)
100 s
->syt_interval
= rate_info
[sfc
].syt_interval
;
102 /* default buffering in the device */
103 s
->transfer_delay
= TRANSFER_DELAY_TICKS
- TICKS_PER_CYCLE
;
104 if (s
->flags
& CIP_BLOCKING
)
105 /* additional buffering needed to adjust for no-data packets */
106 s
->transfer_delay
+= TICKS_PER_SECOND
* s
->syt_interval
/ rate
;
108 EXPORT_SYMBOL(amdtp_out_stream_set_rate
);
111 * amdtp_out_stream_get_max_payload - get the stream's packet size
112 * @s: the AMDTP output stream
114 * This function must not be called before the stream has been configured
115 * with amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(), and
116 * amdtp_out_stream_set_midi().
118 unsigned int amdtp_out_stream_get_max_payload(struct amdtp_out_stream
*s
)
120 s
->data_block_quadlets
= s
->pcm_channels
;
121 s
->data_block_quadlets
+= DIV_ROUND_UP(s
->midi_ports
, 8);
123 return 8 + s
->syt_interval
* s
->data_block_quadlets
* 4;
125 EXPORT_SYMBOL(amdtp_out_stream_get_max_payload
);
127 static void amdtp_write_s16(struct amdtp_out_stream
*s
,
128 struct snd_pcm_substream
*pcm
,
129 __be32
*buffer
, unsigned int frames
);
130 static void amdtp_write_s32(struct amdtp_out_stream
*s
,
131 struct snd_pcm_substream
*pcm
,
132 __be32
*buffer
, unsigned int frames
);
135 * amdtp_out_stream_set_pcm_format - set the PCM format
136 * @s: the AMDTP output stream to configure
137 * @format: the format of the ALSA PCM device
139 * The sample format must be set before the stream is started, and must not be
140 * changed while the stream is running.
142 void amdtp_out_stream_set_pcm_format(struct amdtp_out_stream
*s
,
143 snd_pcm_format_t format
)
145 if (WARN_ON(!IS_ERR(s
->context
)))
152 case SNDRV_PCM_FORMAT_S16
:
153 s
->transfer_samples
= amdtp_write_s16
;
155 case SNDRV_PCM_FORMAT_S32
:
156 s
->transfer_samples
= amdtp_write_s32
;
160 EXPORT_SYMBOL(amdtp_out_stream_set_pcm_format
);
162 static unsigned int calculate_data_blocks(struct amdtp_out_stream
*s
)
164 unsigned int phase
, data_blocks
;
166 if (!cip_sfc_is_base_44100(s
->sfc
)) {
167 /* Sample_rate / 8000 is an integer, and precomputed. */
168 data_blocks
= s
->data_block_state
;
170 phase
= s
->data_block_state
;
173 * This calculates the number of data blocks per packet so that
174 * 1) the overall rate is correct and exactly synchronized to
176 * 2) packets with a rounded-up number of blocks occur as early
177 * as possible in the sequence (to prevent underruns of the
180 if (s
->sfc
== CIP_SFC_44100
)
181 /* 6 6 5 6 5 6 5 ... */
182 data_blocks
= 5 + ((phase
& 1) ^
183 (phase
== 0 || phase
>= 40));
185 /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
186 data_blocks
= 11 * (s
->sfc
>> 1) + (phase
== 0);
187 if (++phase
>= (80 >> (s
->sfc
>> 1)))
189 s
->data_block_state
= phase
;
195 static unsigned int calculate_syt(struct amdtp_out_stream
*s
,
198 unsigned int syt_offset
, phase
, index
, syt
;
200 if (s
->last_syt_offset
< TICKS_PER_CYCLE
) {
201 if (!cip_sfc_is_base_44100(s
->sfc
))
202 syt_offset
= s
->last_syt_offset
+ s
->syt_offset_state
;
205 * The time, in ticks, of the n'th SYT_INTERVAL sample is:
206 * n * SYT_INTERVAL * 24576000 / sample_rate
207 * Modulo TICKS_PER_CYCLE, the difference between successive
208 * elements is about 1386.23. Rounding the results of this
209 * formula to the SYT precision results in a sequence of
210 * differences that begins with:
211 * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
212 * This code generates _exactly_ the same sequence.
214 phase
= s
->syt_offset_state
;
216 syt_offset
= s
->last_syt_offset
;
217 syt_offset
+= 1386 + ((index
&& !(index
& 3)) ||
221 s
->syt_offset_state
= phase
;
224 syt_offset
= s
->last_syt_offset
- TICKS_PER_CYCLE
;
225 s
->last_syt_offset
= syt_offset
;
227 if (syt_offset
< TICKS_PER_CYCLE
) {
228 syt_offset
+= s
->transfer_delay
;
229 syt
= (cycle
+ syt_offset
/ TICKS_PER_CYCLE
) << 12;
230 syt
+= syt_offset
% TICKS_PER_CYCLE
;
234 return 0xffff; /* no info */
238 static void amdtp_write_s32(struct amdtp_out_stream
*s
,
239 struct snd_pcm_substream
*pcm
,
240 __be32
*buffer
, unsigned int frames
)
242 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
243 unsigned int channels
, remaining_frames
, frame_step
, i
, c
;
246 channels
= s
->pcm_channels
;
247 src
= (void *)runtime
->dma_area
+
248 s
->pcm_buffer_pointer
* (runtime
->frame_bits
/ 8);
249 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
250 frame_step
= s
->data_block_quadlets
- channels
;
252 for (i
= 0; i
< frames
; ++i
) {
253 for (c
= 0; c
< channels
; ++c
) {
254 *buffer
= cpu_to_be32((*src
>> 8) | 0x40000000);
258 buffer
+= frame_step
;
259 if (--remaining_frames
== 0)
260 src
= (void *)runtime
->dma_area
;
264 static void amdtp_write_s16(struct amdtp_out_stream
*s
,
265 struct snd_pcm_substream
*pcm
,
266 __be32
*buffer
, unsigned int frames
)
268 struct snd_pcm_runtime
*runtime
= pcm
->runtime
;
269 unsigned int channels
, remaining_frames
, frame_step
, i
, c
;
272 channels
= s
->pcm_channels
;
273 src
= (void *)runtime
->dma_area
+
274 s
->pcm_buffer_pointer
* (runtime
->frame_bits
/ 8);
275 remaining_frames
= runtime
->buffer_size
- s
->pcm_buffer_pointer
;
276 frame_step
= s
->data_block_quadlets
- channels
;
278 for (i
= 0; i
< frames
; ++i
) {
279 for (c
= 0; c
< channels
; ++c
) {
280 *buffer
= cpu_to_be32((*src
<< 8) | 0x40000000);
284 buffer
+= frame_step
;
285 if (--remaining_frames
== 0)
286 src
= (void *)runtime
->dma_area
;
290 static void amdtp_fill_pcm_silence(struct amdtp_out_stream
*s
,
291 __be32
*buffer
, unsigned int frames
)
295 for (i
= 0; i
< frames
; ++i
) {
296 for (c
= 0; c
< s
->pcm_channels
; ++c
)
297 buffer
[c
] = cpu_to_be32(0x40000000);
298 buffer
+= s
->data_block_quadlets
;
302 static void amdtp_fill_midi(struct amdtp_out_stream
*s
,
303 __be32
*buffer
, unsigned int frames
)
307 for (i
= 0; i
< frames
; ++i
)
308 buffer
[s
->pcm_channels
+ i
* s
->data_block_quadlets
] =
309 cpu_to_be32(0x80000000);
312 static void queue_out_packet(struct amdtp_out_stream
*s
, unsigned int cycle
)
315 unsigned int index
, data_blocks
, syt
, ptr
;
316 struct snd_pcm_substream
*pcm
;
317 struct fw_iso_packet packet
;
320 if (s
->packet_index
< 0)
322 index
= s
->packet_index
;
324 syt
= calculate_syt(s
, cycle
);
325 if (!(s
->flags
& CIP_BLOCKING
)) {
326 data_blocks
= calculate_data_blocks(s
);
329 data_blocks
= s
->syt_interval
;
336 buffer
= s
->buffer
.packets
[index
].buffer
;
337 buffer
[0] = cpu_to_be32(ACCESS_ONCE(s
->source_node_id_field
) |
338 (s
->data_block_quadlets
<< 16) |
339 s
->data_block_counter
);
340 buffer
[1] = cpu_to_be32(CIP_EOH
| CIP_FMT_AM
| AMDTP_FDF_AM824
|
341 (s
->sfc
<< AMDTP_FDF_SFC_SHIFT
) | syt
);
344 pcm
= ACCESS_ONCE(s
->pcm
);
346 s
->transfer_samples(s
, pcm
, buffer
, data_blocks
);
348 amdtp_fill_pcm_silence(s
, buffer
, data_blocks
);
350 amdtp_fill_midi(s
, buffer
, data_blocks
);
352 s
->data_block_counter
= (s
->data_block_counter
+ data_blocks
) & 0xff;
354 packet
.payload_length
= 8 + data_blocks
* 4 * s
->data_block_quadlets
;
355 packet
.interrupt
= IS_ALIGNED(index
+ 1, INTERRUPT_INTERVAL
);
357 packet
.tag
= TAG_CIP
;
359 packet
.header_length
= 0;
361 err
= fw_iso_context_queue(s
->context
, &packet
, &s
->buffer
.iso_buffer
,
362 s
->buffer
.packets
[index
].offset
);
364 dev_err(&s
->unit
->device
, "queueing error: %d\n", err
);
365 s
->packet_index
= -1;
366 amdtp_out_stream_pcm_abort(s
);
370 if (++index
>= QUEUE_LENGTH
)
372 s
->packet_index
= index
;
375 ptr
= s
->pcm_buffer_pointer
+ data_blocks
;
376 if (ptr
>= pcm
->runtime
->buffer_size
)
377 ptr
-= pcm
->runtime
->buffer_size
;
378 ACCESS_ONCE(s
->pcm_buffer_pointer
) = ptr
;
380 s
->pcm_period_pointer
+= data_blocks
;
381 if (s
->pcm_period_pointer
>= pcm
->runtime
->period_size
) {
382 s
->pcm_period_pointer
-= pcm
->runtime
->period_size
;
383 snd_pcm_period_elapsed(pcm
);
388 static void out_packet_callback(struct fw_iso_context
*context
, u32 cycle
,
389 size_t header_length
, void *header
, void *data
)
391 struct amdtp_out_stream
*s
= data
;
392 unsigned int i
, packets
= header_length
/ 4;
395 * Compute the cycle of the last queued packet.
396 * (We need only the four lowest bits for the SYT, so we can ignore
397 * that bits 0-11 must wrap around at 3072.)
399 cycle
+= QUEUE_LENGTH
- packets
;
401 for (i
= 0; i
< packets
; ++i
)
402 queue_out_packet(s
, ++cycle
);
405 static int queue_initial_skip_packets(struct amdtp_out_stream
*s
)
407 struct fw_iso_packet skip_packet
= {
413 for (i
= 0; i
< QUEUE_LENGTH
; ++i
) {
414 skip_packet
.interrupt
= IS_ALIGNED(s
->packet_index
+ 1,
416 err
= fw_iso_context_queue(s
->context
, &skip_packet
, NULL
, 0);
419 if (++s
->packet_index
>= QUEUE_LENGTH
)
427 * amdtp_out_stream_start - start sending packets
428 * @s: the AMDTP output stream to start
429 * @channel: the isochronous channel on the bus
430 * @speed: firewire speed code
432 * The stream cannot be started until it has been configured with
433 * amdtp_out_stream_set_rate(), amdtp_out_stream_set_pcm(),
434 * amdtp_out_stream_set_midi(), and amdtp_out_stream_set_format();
435 * and it must be started before any PCM or MIDI device can be started.
437 int amdtp_out_stream_start(struct amdtp_out_stream
*s
, int channel
, int speed
)
439 static const struct {
440 unsigned int data_block
;
441 unsigned int syt_offset
;
442 } initial_state
[] = {
443 [CIP_SFC_32000
] = { 4, 3072 },
444 [CIP_SFC_48000
] = { 6, 1024 },
445 [CIP_SFC_96000
] = { 12, 1024 },
446 [CIP_SFC_192000
] = { 24, 1024 },
447 [CIP_SFC_44100
] = { 0, 67 },
448 [CIP_SFC_88200
] = { 0, 67 },
449 [CIP_SFC_176400
] = { 0, 67 },
453 mutex_lock(&s
->mutex
);
455 if (WARN_ON(!IS_ERR(s
->context
) ||
456 (!s
->pcm_channels
&& !s
->midi_ports
))) {
461 s
->data_block_state
= initial_state
[s
->sfc
].data_block
;
462 s
->syt_offset_state
= initial_state
[s
->sfc
].syt_offset
;
463 s
->last_syt_offset
= TICKS_PER_CYCLE
;
465 err
= iso_packets_buffer_init(&s
->buffer
, s
->unit
, QUEUE_LENGTH
,
466 amdtp_out_stream_get_max_payload(s
),
471 s
->context
= fw_iso_context_create(fw_parent_device(s
->unit
)->card
,
472 FW_ISO_CONTEXT_TRANSMIT
,
474 out_packet_callback
, s
);
475 if (IS_ERR(s
->context
)) {
476 err
= PTR_ERR(s
->context
);
478 dev_err(&s
->unit
->device
,
479 "no free output stream on this controller\n");
483 amdtp_out_stream_update(s
);
486 s
->data_block_counter
= 0;
487 err
= queue_initial_skip_packets(s
);
491 err
= fw_iso_context_start(s
->context
, -1, 0, 0);
495 mutex_unlock(&s
->mutex
);
500 fw_iso_context_destroy(s
->context
);
501 s
->context
= ERR_PTR(-1);
503 iso_packets_buffer_destroy(&s
->buffer
, s
->unit
);
505 mutex_unlock(&s
->mutex
);
509 EXPORT_SYMBOL(amdtp_out_stream_start
);
512 * amdtp_out_stream_update - update the stream after a bus reset
513 * @s: the AMDTP output stream
515 void amdtp_out_stream_update(struct amdtp_out_stream
*s
)
517 ACCESS_ONCE(s
->source_node_id_field
) =
518 (fw_parent_device(s
->unit
)->card
->node_id
& 0x3f) << 24;
520 EXPORT_SYMBOL(amdtp_out_stream_update
);
523 * amdtp_out_stream_stop - stop sending packets
524 * @s: the AMDTP output stream to stop
526 * All PCM and MIDI devices of the stream must be stopped before the stream
527 * itself can be stopped.
529 void amdtp_out_stream_stop(struct amdtp_out_stream
*s
)
531 mutex_lock(&s
->mutex
);
533 if (IS_ERR(s
->context
)) {
534 mutex_unlock(&s
->mutex
);
538 fw_iso_context_stop(s
->context
);
539 fw_iso_context_destroy(s
->context
);
540 s
->context
= ERR_PTR(-1);
541 iso_packets_buffer_destroy(&s
->buffer
, s
->unit
);
543 mutex_unlock(&s
->mutex
);
545 EXPORT_SYMBOL(amdtp_out_stream_stop
);
548 * amdtp_out_stream_pcm_abort - abort the running PCM device
549 * @s: the AMDTP stream about to be stopped
551 * If the isochronous stream needs to be stopped asynchronously, call this
552 * function first to stop the PCM device.
554 void amdtp_out_stream_pcm_abort(struct amdtp_out_stream
*s
)
556 struct snd_pcm_substream
*pcm
;
558 pcm
= ACCESS_ONCE(s
->pcm
);
560 snd_pcm_stream_lock_irq(pcm
);
561 if (snd_pcm_running(pcm
))
562 snd_pcm_stop(pcm
, SNDRV_PCM_STATE_XRUN
);
563 snd_pcm_stream_unlock_irq(pcm
);
566 EXPORT_SYMBOL(amdtp_out_stream_pcm_abort
);