ALSA: firewire-lib: postpone to start IR context
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Fri, 18 Oct 2019 06:19:11 +0000 (15:19 +0900)
committerTakashi Iwai <tiwai@suse.de>
Sat, 19 Oct 2019 07:18:27 +0000 (09:18 +0200)
Some devices have a quirk to postpone transmission of isoc packet for
several dozen or hundred isoc cycles since configured to transmit.
Furthermore, some devices have a quirk to transmit isoc packet with
discontinued data of its header.

In 1394 OHCI specification, software allows to start isoc context with
certain isoc cycle. Linux firewire subsystem has kernel API to use it
as well.

This commit uses the functionality of 1394 OHCI controller to handle
the quirks. At present, this feature is convenient to ALSA bebob and
fireface driver. As a result, some devices can be safely handled, as
long as I know:
 - MAudio FireWire solo
 - MAudio ProFire Lightbridge
 - MAudio FireWire 410
 - Roland FA-66

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191018061911.24909-7-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 48be31eae9a5be08947514937f2c40f866ccae05..37d38efb4c877f8084400b92a4c9e87599fc007c 100644 (file)
@@ -9,6 +9,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/firewire.h>
+#include <linux/firewire-constants.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <sound/pcm.h>
@@ -983,13 +984,16 @@ static void amdtp_stream_master_first_callback(struct fw_iso_context *context,
  * @d: the AMDTP domain to which the AMDTP stream belongs
  * @is_irq_target: whether isoc context for the AMDTP stream is used to generate
  *                hardware IRQ.
+ * @start_cycle: the isochronous cycle to start the context. Start immediately
+ *              if negative value is given.
  *
  * The stream cannot be started until it has been configured with
  * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
  * device can be started.
  */
 static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
-                             struct amdtp_domain *d, bool is_irq_target)
+                             struct amdtp_domain *d, bool is_irq_target,
+                             int start_cycle)
 {
        static const struct {
                unsigned int data_block;
@@ -1146,7 +1150,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
                tag |= FW_ISO_CONTEXT_MATCH_TAG0;
 
        s->callbacked = false;
-       err = fw_iso_context_start(s->context, -1, 0, tag);
+       err = fw_iso_context_start(s->context, start_cycle, 0, tag);
        if (err < 0)
                goto err_pkt_descs;
 
@@ -1339,14 +1343,40 @@ int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
 }
 EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
 
+static int get_current_cycle_time(struct fw_card *fw_card, int *cur_cycle)
+{
+       int generation;
+       int rcode;
+       __be32 reg;
+       u32 data;
+
+       // This is a request to local 1394 OHCI controller and expected to
+       // complete without any event waiting.
+       generation = fw_card->generation;
+       smp_rmb();      // node_id vs. generation.
+       rcode = fw_run_transaction(fw_card, TCODE_READ_QUADLET_REQUEST,
+                                  fw_card->node_id, generation, SCODE_100,
+                                  CSR_REGISTER_BASE + CSR_CYCLE_TIME,
+                                  &reg, sizeof(reg));
+       if (rcode != RCODE_COMPLETE)
+               return -EIO;
+
+       data = be32_to_cpu(reg);
+       *cur_cycle = data >> 12;
+
+       return 0;
+}
+
 /**
  * amdtp_domain_start - start sending packets for isoc context in the domain.
  * @d: the AMDTP domain.
+ * @ir_delay_cycle: the cycle delay to start all IR contexts.
  */
-int amdtp_domain_start(struct amdtp_domain *d)
+int amdtp_domain_start(struct amdtp_domain *d, unsigned int ir_delay_cycle)
 {
        struct amdtp_stream *s;
-       int err = 0;
+       int cycle;
+       int err;
 
        // Select an IT context as IRQ target.
        list_for_each_entry(s, &d->streams, list) {
@@ -1357,17 +1387,54 @@ int amdtp_domain_start(struct amdtp_domain *d)
                return -ENXIO;
        d->irq_target = s;
 
+       if (ir_delay_cycle > 0) {
+               struct fw_card *fw_card = fw_parent_device(s->unit)->card;
+
+               err = get_current_cycle_time(fw_card, &cycle);
+               if (err < 0)
+                       return err;
+
+               // No need to care overflow in cycle field because of enough
+               // width.
+               cycle += ir_delay_cycle;
+
+               // Round up to sec field.
+               if ((cycle & 0x00001fff) >= CYCLES_PER_SECOND) {
+                       unsigned int sec;
+
+                       // The sec field can overflow.
+                       sec = (cycle & 0xffffe000) >> 13;
+                       cycle = (++sec << 13) |
+                               ((cycle & 0x00001fff) / CYCLES_PER_SECOND);
+               }
+
+               // In OHCI 1394 specification, lower 2 bits are available for
+               // sec field.
+               cycle &= 0x00007fff;
+       } else {
+               cycle = -1;
+       }
+
        list_for_each_entry(s, &d->streams, list) {
+               int cycle_match;
+
+               if (s->direction == AMDTP_IN_STREAM) {
+                       cycle_match = cycle;
+               } else {
+                       // IT context starts immediately.
+                       cycle_match = -1;
+               }
+
                if (s != d->irq_target) {
                        err = amdtp_stream_start(s, s->channel, s->speed, d,
-                                                false);
+                                                false, cycle_match);
                        if (err < 0)
                                goto error;
                }
        }
 
        s = d->irq_target;
-       err = amdtp_stream_start(s, s->channel, s->speed, d, true);
+       err = amdtp_stream_start(s, s->channel, s->speed, d, true, -1);
        if (err < 0)
                goto error;
 
index c4bde69c2d4fa8d53e9b7536a39e83fc55bed315..f2d44e2dc3c8a960c8ecdb2a3056ea1aa22f82a5 100644 (file)
@@ -288,7 +288,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);
+int amdtp_domain_start(struct amdtp_domain *d, unsigned int ir_delay_cycle);
 void amdtp_domain_stop(struct amdtp_domain *d);
 
 static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
index 5e4a61458be24792f539c1c4f7038e469ca3ef4f..7ac0d9f495c47ca2a5d423ff9981c9457853334d 100644 (file)
@@ -658,7 +658,15 @@ int snd_bebob_stream_start_duplex(struct snd_bebob *bebob)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&bebob->domain);
+               // The device postpones start of transmission mostly for 1 sec
+               // after receives packets firstly. For safe, IR context starts
+               // 1.5 sec (=12000 cycles) later. This is within 2.0 sec
+               // (=CALLBACK_TIMEOUT).
+               // Furthermore, some devices transfer isoc packets with
+               // discontinuous counter in the beginning of packet streaming.
+               // The delay has an effect to avoid detection of this
+               // discontinuity.
+               err = amdtp_domain_start(&bebob->domain, 12000);
                if (err < 0)
                        goto error;
 
index 0cff346e805264b3e4807bc2686ce5b540ef4c95..6a3d60913e10ca85afeea21f3a8bfe2f49119390 100644 (file)
@@ -462,7 +462,7 @@ int snd_dice_stream_start_duplex(struct snd_dice *dice)
                        goto error;
                }
 
-               err = amdtp_domain_start(&dice->domain);
+               err = amdtp_domain_start(&dice->domain, 0);
                if (err < 0)
                        goto error;
 
index 0c539188ba18871d1015b8a419654daf4a1542b7..405d6903bfbc3d0eb1e5ba3c52aa1ef4afac6b5b 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);
+               err = amdtp_domain_start(&dg00x->domain, 0);
                if (err < 0)
                        goto error;
 
index a13754f914e8aad33dbd3e8a66efa8bce6411f0c..63b79c4a54059db4fee7ab15c1f9b27ca2ff8874 100644 (file)
@@ -184,6 +184,7 @@ int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
         */
        if (!amdtp_stream_running(&ff->rx_stream)) {
                int spd = fw_parent_device(ff->unit)->max_speed;
+               unsigned int ir_delay_cycle;
 
                err = ff->spec->protocol->begin_session(ff, rate);
                if (err < 0)
@@ -199,7 +200,14 @@ int snd_ff_stream_start_duplex(struct snd_ff *ff, unsigned int rate)
                if (err < 0)
                        goto error;
 
-               err = amdtp_domain_start(&ff->domain);
+               // The device postpones start of transmission mostly for several
+               // cycles after receiving packets firstly.
+               if (ff->spec->protocol == &snd_ff_protocol_ff800)
+                       ir_delay_cycle = 800;   // = 100 msec
+               else
+                       ir_delay_cycle = 16;    // = 2 msec
+
+               err = amdtp_domain_start(&ff->domain, ir_delay_cycle);
                if (err < 0)
                        goto error;
 
index f35a33d4d4e651868821e95884d56950a06c484d..2206af0fef4224aaff49d7963719d087be3d74e6 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);
+               err = amdtp_domain_start(&efw->domain, 0);
                if (err < 0)
                        goto error;
 
index 9975770c9b1ffa6575acd782b319ab00bc190cf2..a17ddceb1becd6f4d1bd14ea8be19abf8230c8e0 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);
+               err = amdtp_domain_start(&motu->domain, 0);
                if (err < 0)
                        goto stop_streams;
 
index 995e9c5bd84bd8f0dcfa6c94f082f7cd4d464d0f..501a80094bf7d3aefc1cec9ae4e8981146498743 100644 (file)
@@ -355,7 +355,7 @@ int snd_oxfw_stream_start_duplex(struct snd_oxfw *oxfw)
                        }
                }
 
-               err = amdtp_domain_start(&oxfw->domain);
+               err = amdtp_domain_start(&oxfw->domain, 0);
                if (err < 0)
                        goto error;
 
index a9b3b7eb6d216ad1d4ea1f0165f0fb8d83ea7f42..eb07e1decf9bad3770548e19b8ed2dee14793e17 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);
+               err = amdtp_domain_start(&tscm->domain, 0);
                if (err < 0)
                        return err;