[PATCH 0/3] ALSA: firewire-lib: report extra delay of PCM runtime
For reasons, all drivers in ALSA firewire stack have never reported extra delay for the runtime of PCM substream. The main reason is that the meaning of extra delay differs depending on driver design. Another technical reason is that no kernel API was provided to know the current hardware time.
I realized that the extra delay is helpful to user space PCM applications in the case of packet-oriented drivers since the drivers have a gap between the current transmission cycle and the latest transmission cycle to which the packet is processed (for PCM capture) or scheduled (for PCM playback). The amount of PCM frames delivered during the gap is usually invisible from the application as is in reported delay.
A commit baa914cd81f5 ("firewire: add kernel API to access CYCLE_TIME register") was already merged into Linux kernel v5.19 or later, and the unit drivers can read hardware time and calculate the current isochronous cycle. Moreover, a commit f0117128879b ("ALSA: firewire-lib: keep history to process isochronous packet") enables to keep the recent history of packets, including cycle count and data block count.
It is ready at last. This patchset adds computation of the extra delay.
Takashi Sakamoto (3): ALSA: firewire-lib: move parameter for pcm frame multiplier from context payload processing layer ALSA: firewire-lib: obsolete return value from context payload processing layer ALSA: firewire-lib: compute extra delay for runtime of PCM substream
sound/firewire/amdtp-am824.c | 50 +++++-------- sound/firewire/amdtp-stream.c | 108 +++++++++++++++++++++++++-- sound/firewire/amdtp-stream.h | 12 +-- sound/firewire/digi00x/amdtp-dot.c | 18 ++--- sound/firewire/fireface/amdtp-ff.c | 18 ++--- sound/firewire/motu/amdtp-motu.c | 18 ++--- sound/firewire/tascam/amdtp-tascam.c | 18 ++--- 7 files changed, 146 insertions(+), 96 deletions(-)
The current implementation delegates the task to calculate the number of processed PCM frames into the context payload processing layer. It looks good as long as frame calculation is done for a single purpose. Nevertheless, another purpose, the computation of extra delay for the runtime of PCM substream, requires frame calculation, too.
This commit refactors the current implementation so that the packet stream processing layer is responsible for the calculation of PCM frame, too. The member is moved to stream structure for multiplier between data block count and PCM frame count.
Signed-off-by: Takashi Sakamoto o-takashi@sakamocchi.jp --- sound/firewire/amdtp-am824.c | 34 +++++++++++++--------------- sound/firewire/amdtp-stream.c | 25 +++++++++++++++----- sound/firewire/amdtp-stream.h | 3 ++- sound/firewire/digi00x/amdtp-dot.c | 2 +- sound/firewire/fireface/amdtp-ff.c | 2 +- sound/firewire/motu/amdtp-motu.c | 2 +- sound/firewire/tascam/amdtp-tascam.c | 2 +- 7 files changed, 41 insertions(+), 29 deletions(-)
diff --git a/sound/firewire/amdtp-am824.c b/sound/firewire/amdtp-am824.c index cf55f7784d23..b849f529fcba 100644 --- a/sound/firewire/amdtp-am824.c +++ b/sound/firewire/amdtp-am824.c @@ -36,8 +36,6 @@ struct amdtp_am824 {
u8 pcm_positions[AM824_MAX_CHANNELS_FOR_PCM]; u8 midi_position; - - unsigned int frame_multiplier; };
/** @@ -59,8 +57,8 @@ int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate, { struct amdtp_am824 *p = s->protocol; unsigned int midi_channels; - unsigned int i; - int err; + unsigned int pcm_frame_multiplier; + int i, err;
if (amdtp_stream_running(s)) return -EINVAL; @@ -77,8 +75,18 @@ int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate, WARN_ON(midi_channels > AM824_MAX_CHANNELS_FOR_MIDI)) return -EINVAL;
- err = amdtp_stream_set_parameters(s, rate, - pcm_channels + midi_channels); + /* + * In IEC 61883-6, one data block represents one event. In ALSA, one + * event equals to one PCM frame. But Dice has a quirk at higher + * sampling rate to transfer two PCM frames in one data block. + */ + if (double_pcm_frames) + pcm_frame_multiplier = 2; + else + pcm_frame_multiplier = 1; + + err = amdtp_stream_set_parameters(s, rate, pcm_channels + midi_channels, + pcm_frame_multiplier); if (err < 0) return err;
@@ -88,16 +96,6 @@ int amdtp_am824_set_parameters(struct amdtp_stream *s, unsigned int rate, p->pcm_channels = pcm_channels; p->midi_ports = midi_ports;
- /* - * In IEC 61883-6, one data block represents one event. In ALSA, one - * event equals to one PCM frame. But Dice has a quirk at higher - * sampling rate to transfer two PCM frames in one data block. - */ - if (double_pcm_frames) - p->frame_multiplier = 2; - else - p->frame_multiplier = 1; - /* init the position map for PCM and MIDI channels */ for (i = 0; i < pcm_channels; i++) p->pcm_positions[i] = i; @@ -361,7 +359,7 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
if (pcm) { write_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); - pcm_frames += data_blocks * p->frame_multiplier; + pcm_frames += data_blocks * s->pcm_frame_multiplier; } else { write_pcm_silence(s, buf, data_blocks); } @@ -392,7 +390,7 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
if (pcm) { read_pcm_s32(s, pcm, buf, data_blocks, pcm_frames); - pcm_frames += data_blocks * p->frame_multiplier; + pcm_frames += data_blocks * s->pcm_frame_multiplier; }
if (p->midi_ports) { diff --git a/sound/firewire/amdtp-stream.c b/sound/firewire/amdtp-stream.c index 65720ae866cb..98a033afc5f8 100644 --- a/sound/firewire/amdtp-stream.c +++ b/sound/firewire/amdtp-stream.c @@ -271,12 +271,14 @@ EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints); * @s: the AMDTP stream to configure * @rate: the sample rate * @data_block_quadlets: the size of a data block in quadlet unit + * @pcm_frame_multiplier: the multiplier to compute the number of PCM frames by the number of AMDTP + * events. * * The parameters must be set before the stream is started, and must not be * changed while the stream is running. */ int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, - unsigned int data_block_quadlets) + unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier) { unsigned int sfc;
@@ -298,6 +300,8 @@ int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, if (s->flags & CIP_BLOCKING) s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
+ s->pcm_frame_multiplier = pcm_frame_multiplier; + return 0; } EXPORT_SYMBOL(amdtp_stream_set_parameters); @@ -1032,16 +1036,25 @@ static inline void cancel_stream(struct amdtp_stream *s) }
static void process_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *descs, + const struct pkt_desc *desc, unsigned int count) { struct snd_pcm_substream *pcm; - unsigned int pcm_frames; + int i;
pcm = READ_ONCE(s->pcm); - pcm_frames = s->process_ctx_payloads(s, descs, count, pcm); - if (pcm) - update_pcm_pointers(s, pcm, pcm_frames); + (void)s->process_ctx_payloads(s, desc, count, pcm); + + if (pcm) { + unsigned int data_block_count = 0; + + for (i = 0; i < count; ++i) { + data_block_count += desc->data_blocks; + desc = amdtp_stream_next_packet_desc(s, desc); + } + + update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier); + } }
static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length, diff --git a/sound/firewire/amdtp-stream.h b/sound/firewire/amdtp-stream.h index a8dd1c3ec8d9..35b48f9ddbf7 100644 --- a/sound/firewire/amdtp-stream.h +++ b/sound/firewire/amdtp-stream.h @@ -190,6 +190,7 @@ struct amdtp_stream { struct snd_pcm_substream *pcm; snd_pcm_uframes_t pcm_buffer_pointer; unsigned int pcm_period_pointer; + unsigned int pcm_frame_multiplier;
// To start processing content of packets at the same cycle in several contexts for // each direction. @@ -216,7 +217,7 @@ int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit, void amdtp_stream_destroy(struct amdtp_stream *s);
int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate, - unsigned int data_block_quadlets); + unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier); unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s);
void amdtp_stream_update(struct amdtp_stream *s); diff --git a/sound/firewire/digi00x/amdtp-dot.c b/sound/firewire/digi00x/amdtp-dot.c index fcae7d07aa03..b3f67af2d3b1 100644 --- a/sound/firewire/digi00x/amdtp-dot.c +++ b/sound/firewire/digi00x/amdtp-dot.c @@ -123,7 +123,7 @@ int amdtp_dot_set_parameters(struct amdtp_stream *s, unsigned int rate, * A first data channel is for MIDI messages, the rest is Multi Bit * Linear Audio data channel. */ - err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1); + err = amdtp_stream_set_parameters(s, rate, pcm_channels + 1, 1); if (err < 0) return err;
diff --git a/sound/firewire/fireface/amdtp-ff.c b/sound/firewire/fireface/amdtp-ff.c index 2402e2be87a6..27943b7f86fa 100644 --- a/sound/firewire/fireface/amdtp-ff.c +++ b/sound/firewire/fireface/amdtp-ff.c @@ -24,7 +24,7 @@ int amdtp_ff_set_parameters(struct amdtp_stream *s, unsigned int rate, p->pcm_channels = pcm_channels; data_channels = pcm_channels;
- return amdtp_stream_set_parameters(s, rate, data_channels); + return amdtp_stream_set_parameters(s, rate, data_channels, 1); }
static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm, diff --git a/sound/firewire/motu/amdtp-motu.c b/sound/firewire/motu/amdtp-motu.c index ea0063cec5fb..4153527b5e08 100644 --- a/sound/firewire/motu/amdtp-motu.c +++ b/sound/firewire/motu/amdtp-motu.c @@ -73,7 +73,7 @@ int amdtp_motu_set_parameters(struct amdtp_stream *s, unsigned int rate, data_chunks = formats->msg_chunks + pcm_chunks; data_block_quadlets = 1 + DIV_ROUND_UP(data_chunks * 3, 4);
- err = amdtp_stream_set_parameters(s, rate, data_block_quadlets); + err = amdtp_stream_set_parameters(s, rate, data_block_quadlets, 1); if (err < 0) return err;
diff --git a/sound/firewire/tascam/amdtp-tascam.c b/sound/firewire/tascam/amdtp-tascam.c index c367a6ee6121..bb4cf2d26d1b 100644 --- a/sound/firewire/tascam/amdtp-tascam.c +++ b/sound/firewire/tascam/amdtp-tascam.c @@ -29,7 +29,7 @@ int amdtp_tscm_set_parameters(struct amdtp_stream *s, unsigned int rate) if (s->direction == AMDTP_IN_STREAM) data_channels += 2;
- return amdtp_stream_set_parameters(s, rate, data_channels); + return amdtp_stream_set_parameters(s, rate, data_channels, 1); }
static void write_pcm_s32(struct amdtp_stream *s, struct snd_pcm_substream *pcm,
This commit obsoletes return value from the context payload processing layer since the multiplier between the data block count and PCM frame count was moved to the packet streaming processing layer.
Signed-off-by: Takashi Sakamoto o-takashi@sakamocchi.jp --- sound/firewire/amdtp-am824.c | 16 ++++------------ sound/firewire/amdtp-stream.c | 2 +- sound/firewire/amdtp-stream.h | 9 ++++----- sound/firewire/digi00x/amdtp-dot.c | 16 ++++------------ sound/firewire/fireface/amdtp-ff.c | 16 ++++------------ sound/firewire/motu/amdtp-motu.c | 16 ++++------------ sound/firewire/tascam/amdtp-tascam.c | 16 ++++------------ 7 files changed, 25 insertions(+), 66 deletions(-)
diff --git a/sound/firewire/amdtp-am824.c b/sound/firewire/amdtp-am824.c index b849f529fcba..3660c312bf33 100644 --- a/sound/firewire/amdtp-am824.c +++ b/sound/firewire/amdtp-am824.c @@ -344,10 +344,8 @@ static void read_midi_messages(struct amdtp_stream *s, __be32 *buffer, } }
-static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { struct amdtp_am824 *p = s->protocol; unsigned int pcm_frames = 0; @@ -371,14 +369,10 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
-static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { struct amdtp_am824 *p = s->protocol; unsigned int pcm_frames = 0; @@ -400,8 +394,6 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
/** diff --git a/sound/firewire/amdtp-stream.c b/sound/firewire/amdtp-stream.c index 98a033afc5f8..03e97faca797 100644 --- a/sound/firewire/amdtp-stream.c +++ b/sound/firewire/amdtp-stream.c @@ -1043,7 +1043,7 @@ static void process_ctx_payloads(struct amdtp_stream *s, int i;
pcm = READ_ONCE(s->pcm); - (void)s->process_ctx_payloads(s, desc, count, pcm); + s->process_ctx_payloads(s, desc, count, pcm);
if (pcm) { unsigned int data_block_count = 0; diff --git a/sound/firewire/amdtp-stream.h b/sound/firewire/amdtp-stream.h index 35b48f9ddbf7..b7ff44751ab9 100644 --- a/sound/firewire/amdtp-stream.h +++ b/sound/firewire/amdtp-stream.h @@ -107,11 +107,10 @@ struct pkt_desc { };
struct amdtp_stream; -typedef unsigned int (*amdtp_stream_process_ctx_payloads_t)( - struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm); +typedef void (*amdtp_stream_process_ctx_payloads_t)(struct amdtp_stream *s, + const struct pkt_desc *desc, + unsigned int count, + struct snd_pcm_substream *pcm);
struct amdtp_domain; struct amdtp_stream { diff --git a/sound/firewire/digi00x/amdtp-dot.c b/sound/firewire/digi00x/amdtp-dot.c index b3f67af2d3b1..7db0024495b7 100644 --- a/sound/firewire/digi00x/amdtp-dot.c +++ b/sound/firewire/digi00x/amdtp-dot.c @@ -341,10 +341,8 @@ void amdtp_dot_midi_trigger(struct amdtp_stream *s, unsigned int port, WRITE_ONCE(p->midi[port], midi); }
-static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -362,14 +360,10 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
-static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -390,8 +384,6 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
int amdtp_dot_init(struct amdtp_stream *s, struct fw_unit *unit, diff --git a/sound/firewire/fireface/amdtp-ff.c b/sound/firewire/fireface/amdtp-ff.c index 27943b7f86fa..76c9d33ed572 100644 --- a/sound/firewire/fireface/amdtp-ff.c +++ b/sound/firewire/fireface/amdtp-ff.c @@ -112,10 +112,8 @@ int amdtp_ff_add_pcm_hw_constraints(struct amdtp_stream *s, return amdtp_stream_add_pcm_hw_constraints(s, runtime); }
-static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -133,14 +131,10 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
-static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -156,8 +150,6 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
int amdtp_ff_init(struct amdtp_stream *s, struct fw_unit *unit, diff --git a/sound/firewire/motu/amdtp-motu.c b/sound/firewire/motu/amdtp-motu.c index 4153527b5e08..39ed57d2c5a0 100644 --- a/sound/firewire/motu/amdtp-motu.c +++ b/sound/firewire/motu/amdtp-motu.c @@ -328,10 +328,8 @@ static void cache_event_offsets(struct amdtp_motu_cache *cache, const __be32 *bu cache->tx_cycle_count = (cache->tx_cycle_count + 1) % CYCLES_PER_SECOND; }
-static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { struct snd_motu *motu = container_of(s, struct snd_motu, tx_stream); struct amdtp_motu *p = s->protocol; @@ -370,8 +368,6 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, if (trace_data_block_sph_enabled() || trace_data_block_message_enabled()) probe_tracepoints_events(s, desc, count); - - return pcm_frames; }
static void write_sph(struct amdtp_motu_cache *cache, __be32 *buffer, unsigned int data_blocks, @@ -396,10 +392,8 @@ static void write_sph(struct amdtp_motu_cache *cache, __be32 *buffer, unsigned i cache->rx_cycle_count = (cache->rx_cycle_count + 1) % CYCLES_PER_SECOND; }
-static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { struct amdtp_motu *p = s->protocol; const struct pkt_desc *cursor = desc; @@ -435,8 +429,6 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, if (trace_data_block_sph_enabled() || trace_data_block_message_enabled()) probe_tracepoints_events(s, desc, count); - - return pcm_frames; }
int amdtp_motu_init(struct amdtp_stream *s, struct fw_unit *unit, diff --git a/sound/firewire/tascam/amdtp-tascam.c b/sound/firewire/tascam/amdtp-tascam.c index bb4cf2d26d1b..0b42d6559008 100644 --- a/sound/firewire/tascam/amdtp-tascam.c +++ b/sound/firewire/tascam/amdtp-tascam.c @@ -176,10 +176,8 @@ static void read_status_messages(struct amdtp_stream *s, } }
-static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_ir_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -197,14 +195,10 @@ static unsigned int process_ir_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
-static unsigned int process_it_ctx_payloads(struct amdtp_stream *s, - const struct pkt_desc *desc, - unsigned int count, - struct snd_pcm_substream *pcm) +static void process_it_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, + unsigned int count, struct snd_pcm_substream *pcm) { unsigned int pcm_frames = 0; int i; @@ -222,8 +216,6 @@ static unsigned int process_it_ctx_payloads(struct amdtp_stream *s,
desc = amdtp_stream_next_packet_desc(s, desc); } - - return pcm_frames; }
int amdtp_tscm_init(struct amdtp_stream *s, struct fw_unit *unit,
All drivers in ALSA firewire stack have never reported extra delay for the runtime of PCM substream. There is some reason, but the main reason is that the meaning of extra delay differs depending on driver design, especially for the packet-oriented driver.
Here I define the extra delay for the case of IEC 61883-1/6. It is the number of PCM frames transferred or should be transferred between the current isochronous cycle and the isochronous cycle to which the latest isochronous packet arrived (in IR context) or is scheduled (in IT context).
A commit baa914cd81f5 ("firewire: add kernel API to access CYCLE_TIME register") allow unit drivers to read CYCLE_TIME of 1394 OHCI controller. It allows the drivers to compute the current isochronous cycle.
Additionally, a commit f0117128879b ("ALSA: firewire-lib: keep history to process isochronous packet") enables to save the history processing packets. It allows the driver to estimate the total number of data blocks in packets arriving shortly, or calculate the total number of data blocks in scheduled packets.
Now it is ready. This commit implements the computation of the extra delay.
Signed-off-by: Takashi Sakamoto o-takashi@sakamocchi.jp --- sound/firewire/amdtp-stream.c | 83 ++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-)
diff --git a/sound/firewire/amdtp-stream.c b/sound/firewire/amdtp-stream.c index 03e97faca797..16e45ac28607 100644 --- a/sound/firewire/amdtp-stream.c +++ b/sound/firewire/amdtp-stream.c @@ -352,6 +352,9 @@ void amdtp_stream_pcm_prepare(struct amdtp_stream *s) } EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
+#define prev_packet_desc(s, desc) \ + list_prev_entry_circular(desc, &s->packet_descs_list, link) + static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size, unsigned int pos, unsigned int count) { @@ -851,10 +854,15 @@ static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle, // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second. +static inline u32 compute_ohci_iso_ctx_cycle_count(u32 tstamp) +{ + return (((tstamp >> 13) & 0x07) * CYCLES_PER_SECOND) + (tstamp & 0x1fff); +} + static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp) { u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK; - return (((tstamp >> 13) & 0x07) * 8000) + (tstamp & 0x1fff); + return compute_ohci_iso_ctx_cycle_count(tstamp); }
static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend) @@ -865,6 +873,14 @@ static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend) return cycle; }
+static inline u32 decrement_ohci_cycle_count(u32 minuend, u32 subtrahend) +{ + if (minuend < subtrahend) + minuend += OHCI_SECOND_MODULUS * CYCLES_PER_SECOND; + + return minuend - subtrahend; +} + static int compare_ohci_cycle_count(u32 lval, u32 rval) { if (lval == rval) @@ -1035,6 +1051,63 @@ static inline void cancel_stream(struct amdtp_stream *s) WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN); }
+static snd_pcm_sframes_t compute_pcm_extra_delay(struct amdtp_stream *s, + const struct pkt_desc *desc, unsigned int count) +{ + unsigned int data_block_count = 0; + u32 latest_cycle; + u32 cycle_time; + u32 curr_cycle; + u32 cycle_gap; + int i, err; + + if (count == 0) + goto end; + + // Forward to the latest record. + for (i = 0; i < count - 1; ++i) + desc = amdtp_stream_next_packet_desc(s, desc); + latest_cycle = desc->cycle; + + err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time); + if (err < 0) + goto end; + + // Compute cycle count with lower 3 bits of second field and cycle field like timestamp + // format of 1394 OHCI isochronous context. + curr_cycle = compute_ohci_iso_ctx_cycle_count((cycle_time >> 12) & 0x0000ffff); + + if (s->direction == AMDTP_IN_STREAM) { + // NOTE: The AMDTP packet descriptor should be for the past isochronous cycle since + // it corresponds to arrived isochronous packet. + if (compare_ohci_cycle_count(latest_cycle, curr_cycle) > 0) + goto end; + cycle_gap = decrement_ohci_cycle_count(curr_cycle, latest_cycle); + + // NOTE: estimate delay by recent history of arrived AMDTP packets. The estimated + // value expectedly corresponds to a few packets (0-2) since the packet arrived at + // the most recent isochronous cycle has been already processed. + for (i = 0; i < cycle_gap; ++i) { + desc = amdtp_stream_next_packet_desc(s, desc); + data_block_count += desc->data_blocks; + } + } else { + // NOTE: The AMDTP packet descriptor should be for the future isochronous cycle + // since it was already scheduled. + if (compare_ohci_cycle_count(latest_cycle, curr_cycle) < 0) + goto end; + cycle_gap = decrement_ohci_cycle_count(latest_cycle, curr_cycle); + + // NOTE: use history of scheduled packets. + for (i = 0; i < cycle_gap; ++i) { + data_block_count += desc->data_blocks; + desc = prev_packet_desc(s, desc); + } + } +end: + return data_block_count * s->pcm_frame_multiplier; +} + static void process_ctx_payloads(struct amdtp_stream *s, const struct pkt_desc *desc, unsigned int count) @@ -1048,6 +1121,8 @@ static void process_ctx_payloads(struct amdtp_stream *s, if (pcm) { unsigned int data_block_count = 0;
+ pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count); + for (i = 0; i < count; ++i) { data_block_count += desc->data_blocks; desc = amdtp_stream_next_packet_desc(s, desc); @@ -1686,7 +1761,11 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed, else s->tag = TAG_CIP;
- descs = kcalloc(s->queue_size, sizeof(*descs), GFP_KERNEL); + // NOTE: When operating without hardIRQ/softIRQ, applications tends to call ioctl request + // for runtime of PCM substream in the interval equivalent to the size of PCM buffer. It + // could take a round over queue of AMDTP packet descriptors and small loss of history. For + // safe, keep more 8 elements for the queue, equivalent to 1 ms. + descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL); if (!descs) { err = -ENOMEM; goto err_context;
On Tue, 10 Jan 2023 14:49:30 +0100, Takashi Sakamoto wrote:
For reasons, all drivers in ALSA firewire stack have never reported extra delay for the runtime of PCM substream. The main reason is that the meaning of extra delay differs depending on driver design. Another technical reason is that no kernel API was provided to know the current hardware time.
I realized that the extra delay is helpful to user space PCM applications in the case of packet-oriented drivers since the drivers have a gap between the current transmission cycle and the latest transmission cycle to which the packet is processed (for PCM capture) or scheduled (for PCM playback). The amount of PCM frames delivered during the gap is usually invisible from the application as is in reported delay.
A commit baa914cd81f5 ("firewire: add kernel API to access CYCLE_TIME register") was already merged into Linux kernel v5.19 or later, and the unit drivers can read hardware time and calculate the current isochronous cycle. Moreover, a commit f0117128879b ("ALSA: firewire-lib: keep history to process isochronous packet") enables to keep the recent history of packets, including cycle count and data block count.
It is ready at last. This patchset adds computation of the extra delay.
Takashi Sakamoto (3): ALSA: firewire-lib: move parameter for pcm frame multiplier from context payload processing layer ALSA: firewire-lib: obsolete return value from context payload processing layer ALSA: firewire-lib: compute extra delay for runtime of PCM substream
Thanks, applied now.
Takashi
participants (2)
-
Takashi Iwai
-
Takashi Sakamoto