ALSA: firewire-lib: add replay target to cache sequence of packet
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Thu, 27 May 2021 12:26:09 +0000 (21:26 +0900)
committerTakashi Iwai <tiwai@suse.de>
Fri, 28 May 2021 08:44:01 +0000 (10:44 +0200)
In design of audio and music unit in IEEE 1394 bus, feedback of
effective sampling transfer frequency (STF) is delivered by packets
transferred from device. The devices supported by ALSA firewire stack
are categorized to three groups regarding to it.

 * Group 1:
   * Echo Audio Fireworks board module
   * Oxford Semiconductor OXFW971 ASIC
   * Digidesign Digi00x family
   * Tascam FireWire series
   * RME Fireface series

 * Group 2:
   * BridgeCo. DM1000/DM1100/DM1500 ASICs for BeBoB solution
   * TC Applied Technologies DICE ASICs

 * Group 3:
   * Mark of the Unicord FireWire series

In group 1, the effective STF is determined by the sequence of the number
of events per packet. In group 2, the sequence of presentation timestamp
expressed in syt field of CIP header is interpreted as well. In group 3,
the presentation timestamp is expressed in source packet header (SPH) of
each data block.

I note that some models doesn't take care of effective STF with large
internal buffer. It's reasonable to name it as group 0:

 * Group 0
   * Oxford Semiconductor OXFW970 ASIC

The effective STF is known to be slightly different from nominal STF for
all of devices, and to be different between the devices. Furthermore, the
effective STF is known to be shifted for long-period transmission. This
makes it hard for software to satisfy the effective STF when processing
packets to the device.

The effective STF is deterministic as a result of analyzing the batch of
packet transferred from the device. For the analysis, caching the sequence
of parameter in the packet is required.

This commit adds an option so that AMDTP domain structure takes AMDTP
stream structure to cache the sequence of parameters in packet transferred
from the device. The parameters are offset ticks of syt field against the
cycle to receive the packet and the number of data blocks per packet.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20210527122611.173711-2-o-takashi@sakamocchi.jp
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/firewire/amdtp-stream.c
sound/firewire/amdtp-stream.h
sound/firewire/bebob/bebob_stream.c
sound/firewire/dice/dice-stream.c
sound/firewire/digi00x/digi00x-stream.c
sound/firewire/fireface/ff-stream.c
sound/firewire/fireworks/fireworks_stream.c
sound/firewire/motu/motu-stream.c
sound/firewire/oxfw/oxfw-stream.c
sound/firewire/tascam/tascam-stream.c

index 68ffbc33f6929badd8be0005c873284a739576ce..860942ffb1f1b7e91a5a3d1c1d5af2323da5f60a 100644 (file)
@@ -52,6 +52,7 @@
 #define CIP_FDF_NO_DATA                0xff
 #define CIP_SYT_MASK           0x0000ffff
 #define CIP_SYT_NO_INFO                0xffff
+#define CIP_SYT_CYCLE_MODULUS  16
 #define CIP_NO_DATA            ((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO)
 
 #define CIP_HEADER_SIZE                (sizeof(__be32) * CIP_HEADER_QUADLETS)
@@ -473,6 +474,52 @@ static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *desc
        s->ctx_data.rx.syt_offset_state = state;
 }
 
+static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle,
+                                      unsigned int transfer_delay)
+{
+       unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f;
+       unsigned int syt_cycle_lo = (syt & 0xf000) >> 12;
+       unsigned int syt_offset;
+
+       // Round up.
+       if (syt_cycle_lo < cycle_lo)
+               syt_cycle_lo += CIP_SYT_CYCLE_MODULUS;
+       syt_cycle_lo -= cycle_lo;
+
+       // Subtract transfer delay so that the synchronization offset is not so large
+       // at transmission.
+       syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff);
+       if (syt_offset < transfer_delay)
+               syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE;
+
+       return syt_offset - transfer_delay;
+}
+
+static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *descs, unsigned int desc_count)
+{
+       const unsigned int transfer_delay = s->transfer_delay;
+       const unsigned int cache_size = s->ctx_data.tx.cache.size;
+       struct seq_desc *cache = s->ctx_data.tx.cache.descs;
+       unsigned int cache_tail = s->ctx_data.tx.cache.tail;
+       bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
+       int i;
+
+       for (i = 0; i < desc_count; ++i) {
+               struct seq_desc *dst = cache + cache_tail;
+               const struct pkt_desc *src = descs + i;
+
+               if (aware_syt && src->syt != CIP_SYT_NO_INFO)
+                       dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay);
+               else
+                       dst->syt_offset = CIP_SYT_NO_INFO;
+               dst->data_blocks = src->data_blocks;
+
+               cache_tail = (cache_tail + 1) % cache_size;
+       }
+
+       s->ctx_data.tx.cache.tail = cache_tail;
+}
+
 static void pool_ideal_seq_descs(struct amdtp_stream *s, unsigned int count)
 {
        struct seq_desc *descs = s->ctx_data.rx.seq.descs;
@@ -1107,7 +1154,12 @@ static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_
                        return;
                }
        } else {
+               struct amdtp_domain *d = s->domain;
+
                process_ctx_payloads(s, s->pkt_descs, desc_count);
+
+               if (d->replay.enable)
+                       cache_seq(s, s->pkt_descs, desc_count);
        }
 
        for (i = 0; i < packets; ++i) {
@@ -1463,6 +1515,18 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
                s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
                s->ctx_data.tx.ctx_header_size = ctx_header_size;
                s->ctx_data.tx.event_starts = false;
+
+               if (s->domain->replay.enable) {
+                       // struct fw_iso_context.drop_overflow_headers is false therefore it's
+                       // possible to cache much unexpectedly.
+                       s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2,
+                                                         queue_size * 3 / 2);
+                       s->ctx_data.tx.cache.tail = 0;
+                       s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size,
+                                               sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL);
+                       if (!s->ctx_data.tx.cache.descs)
+                               goto err_context;
+               }
        } else {
                static const struct {
                        unsigned int data_block;
@@ -1543,8 +1607,12 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
 err_pkt_descs:
        kfree(s->pkt_descs);
 err_context:
-       if (s->direction == AMDTP_OUT_STREAM)
+       if (s->direction == AMDTP_OUT_STREAM) {
                kfree(s->ctx_data.rx.seq.descs);
+       } else {
+               if (s->domain->replay.enable)
+                       kfree(s->ctx_data.tx.cache.descs);
+       }
        fw_iso_context_destroy(s->context);
        s->context = ERR_PTR(-1);
 err_buffer:
@@ -1655,8 +1723,12 @@ static void amdtp_stream_stop(struct amdtp_stream *s)
        iso_packets_buffer_destroy(&s->buffer, s->unit);
        kfree(s->pkt_descs);
 
-       if (s->direction == AMDTP_OUT_STREAM)
+       if (s->direction == AMDTP_OUT_STREAM) {
                kfree(s->ctx_data.rx.seq.descs);
+       } else {
+               if (s->domain->replay.enable)
+                       kfree(s->ctx_data.tx.cache.descs);
+       }
 
        mutex_unlock(&s->mutex);
 }
@@ -1735,8 +1807,10 @@ EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
  * @d: the AMDTP domain.
  * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
  *                      contexts.
+ * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in
+ *             IT context.
  */
-int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles)
+int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq)
 {
        unsigned int events_per_buffer = d->events_per_buffer;
        unsigned int events_per_period = d->events_per_period;
@@ -1744,6 +1818,8 @@ int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles)
        struct amdtp_stream *s;
        int err;
 
+       d->replay.enable = replay_seq;
+
        // Select an IT context as IRQ target.
        list_for_each_entry(s, &d->streams, list) {
                if (s->direction == AMDTP_OUT_STREAM)
index 34294776f9e827e944b2691988268536a8ebbc59..ddfb885b61131127c6c1952919946eed261a4769 100644 (file)
@@ -141,6 +141,12 @@ struct amdtp_stream {
 
                        // The device starts multiplexing events to the packet.
                        bool event_starts;
+
+                       struct {
+                               struct seq_desc *descs;
+                               unsigned int size;
+                               unsigned int tail;
+                       } cache;
                } tx;
                struct {
                        // To generate CIP header.
@@ -292,6 +298,10 @@ struct amdtp_domain {
                unsigned int tx_start;
                unsigned int rx_start;
        } processing_cycle;
+
+       struct {
+               bool enable;
+       } replay;
 };
 
 int amdtp_domain_init(struct amdtp_domain *d);
@@ -300,7 +310,7 @@ void amdtp_domain_destroy(struct amdtp_domain *d);
 int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
                            int channel, int speed);
 
-int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles);
+int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq);
 void amdtp_domain_stop(struct amdtp_domain *d);
 
 static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
index 91306da1bafe17d93ace0c050473df56bd5b2517..fb776f871133cd2e147c658d591547945bc22529 100644 (file)
@@ -652,7 +652,7 @@ int snd_bebob_stream_start_duplex(struct snd_bebob *bebob)
                // MEMO: In the early stage of packet streaming, the device transfers NODATA packets.
                // After several hundred cycles, it begins to multiplex event into the packet with
                // syt information.
-               err = amdtp_domain_start(&bebob->domain, tx_init_skip_cycles);
+               err = amdtp_domain_start(&bebob->domain, tx_init_skip_cycles, false);
                if (err < 0)
                        goto error;
 
index a9a0fe9635ddb93c2aebcdaa4b9e7ef82a6881f9..064f28f1822b729b33275a14f0b43be3739126f1 100644 (file)
@@ -459,7 +459,7 @@ int snd_dice_stream_start_duplex(struct snd_dice *dice)
                        goto error;
                }
 
-               err = amdtp_domain_start(&dice->domain, 0);
+               err = amdtp_domain_start(&dice->domain, 0, false);
                if (err < 0)
                        goto error;
 
index f11aaff2e24806a8c98f500f8cab181686182a29..5daba75a5bf3b132042c7f626981a751f4079d66 100644 (file)
@@ -375,7 +375,7 @@ int snd_dg00x_stream_start_duplex(struct snd_dg00x *dg00x)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&dg00x->domain, 0);
+               err = amdtp_domain_start(&dg00x->domain, 0, false);
                if (err < 0)
                        goto error;
 
index 53a21fb95add98d8344336678e4af4c996be6c4c..12779e7caf22df5e7d6c9fea9ad0726362dc46f0 100644 (file)
@@ -199,7 +199,7 @@ int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&ff->domain, 0);
+               err = amdtp_domain_start(&ff->domain, 0, false);
                if (err < 0)
                        goto error;
 
index 858cd6085c1fa451c3435e17d724dd7c538f10c6..0106fa6d1eaf586c9cdc54340f4890e74609f24e 100644 (file)
@@ -272,7 +272,7 @@ int snd_efw_stream_start_duplex(struct snd_efw *efw)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&efw->domain, 0);
+               err = amdtp_domain_start(&efw->domain, 0, false);
                if (err < 0)
                        goto error;
 
index 925241ae2551248d33a761cfe3e1dc072f893015..5af9d7487cdc521a3bfc6c3b264a2801fde35dbf 100644 (file)
@@ -260,7 +260,7 @@ int snd_motu_stream_start_duplex(struct snd_motu *motu)
                if (err < 0)
                        goto stop_streams;
 
-               err = amdtp_domain_start(&motu->domain, 0);
+               err = amdtp_domain_start(&motu->domain, 0, false);
                if (err < 0)
                        goto stop_streams;
 
index 4121d95e161f54604a64a7c8c52c70957ed223b0..a6e97a37f1295a9993a58f691cd998cd5af599a0 100644 (file)
@@ -354,7 +354,7 @@ int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
                        }
                }
 
-               err = amdtp_domain_start(&oxfw->domain, 0);
+               err = amdtp_domain_start(&oxfw->domain, 0, false);
                if (err < 0)
                        goto error;
 
index 296ecf5f6ddc5b54d1af03d08c91f8aabe8ead06..eb4c7c47f8e9163d35ada5f46a4e77f26421161d 100644 (file)
@@ -473,7 +473,7 @@ int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&tscm->domain, 0);
+               err = amdtp_domain_start(&tscm->domain, 0, false);
                if (err < 0)
                        return err;