ALSA: firewire-tascam: reserve/release isochronous resources in pcm.hw_params/hw_free...
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>
Sun, 2 Jun 2019 07:12:48 +0000 (16:12 +0900)
committerTakashi Iwai <tiwai@suse.de>
Tue, 11 Jun 2019 09:36:19 +0000 (11:36 +0200)
Once allocated, isochronous resources are available for packet
streaming, even if the streaming is cancelled. For this reason,
current implementation handles allocation of the resources and
starting packet streaming at the same time. However, this brings
complicated procedure to start packet streaming.

This commit separates the allocation and starting. The allocation is
done in pcm.hw_params callback and available till pcm.hw_free callback.
Even if any XRUN occurs, pcm.prepare callback is done to restart
packet streaming for allocated the resources.

There are two points to stop packet streaming; in pcm.hw_params and
pcm.prepare callbacks.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/firewire/tascam/tascam-pcm.c
sound/firewire/tascam/tascam-stream.c
sound/firewire/tascam/tascam.h

index e4cc8990e1953c2c550ebd591a2cff434097adea..962ef1212ac0d4187f01097d1b0130b413e65280 100644 (file)
@@ -96,12 +96,16 @@ static int pcm_capture_hw_params(struct snd_pcm_substream *substream,
                return err;
 
        if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
+               unsigned int rate = params_rate(hw_params);
+
                mutex_lock(&tscm->mutex);
-               tscm->substreams_counter++;
+               err = snd_tscm_stream_reserve_duplex(tscm, rate);
+               if (err >= 0)
+                       ++tscm->substreams_counter;
                mutex_unlock(&tscm->mutex);
        }
 
-       return 0;
+       return err;
 }
 
 static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
@@ -116,12 +120,16 @@ static int pcm_playback_hw_params(struct snd_pcm_substream *substream,
                return err;
 
        if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) {
+               unsigned int rate = params_rate(hw_params);
+
                mutex_lock(&tscm->mutex);
-               tscm->substreams_counter++;
+               err = snd_tscm_stream_reserve_duplex(tscm, rate);
+               if (err >= 0)
+                       ++tscm->substreams_counter;
                mutex_unlock(&tscm->mutex);
        }
 
-       return 0;
+       return err;
 }
 
 static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
@@ -131,9 +139,10 @@ static int pcm_capture_hw_free(struct snd_pcm_substream *substream)
        mutex_lock(&tscm->mutex);
 
        if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
-               tscm->substreams_counter--;
+               --tscm->substreams_counter;
 
        snd_tscm_stream_stop_duplex(tscm);
+       snd_tscm_stream_release_duplex(tscm);
 
        mutex_unlock(&tscm->mutex);
 
@@ -147,9 +156,10 @@ static int pcm_playback_hw_free(struct snd_pcm_substream *substream)
        mutex_lock(&tscm->mutex);
 
        if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN)
-               tscm->substreams_counter--;
+               --tscm->substreams_counter;
 
        snd_tscm_stream_stop_duplex(tscm);
+       snd_tscm_stream_release_duplex(tscm);
 
        mutex_unlock(&tscm->mutex);
 
index 6ad14927405009d4cd8da98230a9aa113b66dfa2..18d554d46be58ebd865f483e54fa9926f7b2fc3f 100644 (file)
@@ -166,7 +166,7 @@ static int set_stream_formats(struct snd_tscm *tscm, unsigned int rate)
        __be32 reg;
        int err;
 
-       /* Set an option for unknown purpose. */
+       // Set an option for unknown purpose.
        reg = cpu_to_be32(0x00200000);
        err = snd_fw_transaction(tscm->unit, TCODE_WRITE_QUADLET_REQUEST,
                                 TSCM_ADDR_BASE + TSCM_OFFSET_SET_OPTION,
@@ -174,11 +174,7 @@ static int set_stream_formats(struct snd_tscm *tscm, unsigned int rate)
        if (err < 0)
                return err;
 
-       err = enable_data_channels(tscm);
-       if (err < 0)
-               return err;
-
-       return set_clock(tscm, rate, INT_MAX);
+       return enable_data_channels(tscm);
 }
 
 static void finish_session(struct snd_tscm *tscm)
@@ -348,38 +344,66 @@ void snd_tscm_stream_destroy_duplex(struct snd_tscm *tscm)
        fw_iso_resources_destroy(&tscm->tx_resources);
 }
 
-int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate)
+int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate)
 {
        unsigned int curr_rate;
        int err;
 
-       if (tscm->substreams_counter == 0)
-               return 0;
-
        err = snd_tscm_stream_get_rate(tscm, &curr_rate);
        if (err < 0)
                return err;
-       if (curr_rate != rate ||
-           amdtp_streaming_error(&tscm->rx_stream) ||
-           amdtp_streaming_error(&tscm->tx_stream)) {
-               finish_session(tscm);
 
+       if (tscm->substreams_counter == 0 || rate != curr_rate) {
                amdtp_stream_stop(&tscm->rx_stream);
                amdtp_stream_stop(&tscm->tx_stream);
 
+               finish_session(tscm);
+
                fw_iso_resources_free(&tscm->tx_resources);
                fw_iso_resources_free(&tscm->rx_resources);
-       }
 
-       if (!amdtp_stream_running(&tscm->rx_stream)) {
+               err = set_clock(tscm, rate, INT_MAX);
+               if (err < 0)
+                       return err;
+
                err = keep_resources(tscm, rate, &tscm->tx_stream);
                if (err < 0)
-                       goto error;
+                       return err;
 
                err = keep_resources(tscm, rate, &tscm->rx_stream);
-               if (err < 0)
-                       goto error;
+               if (err < 0) {
+                       fw_iso_resources_free(&tscm->tx_resources);
+                       return err;
+               }
+       }
+
+       return 0;
+}
+
+void snd_tscm_stream_release_duplex(struct snd_tscm *tscm)
+{
+       if (tscm->substreams_counter == 0) {
+               fw_iso_resources_free(&tscm->tx_resources);
+               fw_iso_resources_free(&tscm->rx_resources);
+       }
+}
+
+int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate)
+{
+       int err;
+
+       if (tscm->substreams_counter == 0)
+               return 0;
+
+       if (amdtp_streaming_error(&tscm->rx_stream) ||
+           amdtp_streaming_error(&tscm->tx_stream)) {
+               amdtp_stream_stop(&tscm->rx_stream);
+               amdtp_stream_stop(&tscm->tx_stream);
 
+               finish_session(tscm);
+       }
+
+       if (!amdtp_stream_running(&tscm->rx_stream)) {
                err = set_stream_formats(tscm, rate);
                if (err < 0)
                        goto error;
@@ -422,24 +446,17 @@ error:
 
        finish_session(tscm);
 
-       fw_iso_resources_free(&tscm->tx_resources);
-       fw_iso_resources_free(&tscm->rx_resources);
-
        return err;
 }
 
 void snd_tscm_stream_stop_duplex(struct snd_tscm *tscm)
 {
-       if (tscm->substreams_counter > 0)
-               return;
-
-       amdtp_stream_stop(&tscm->tx_stream);
-       amdtp_stream_stop(&tscm->rx_stream);
-
-       finish_session(tscm);
+       if (tscm->substreams_counter == 0) {
+               amdtp_stream_stop(&tscm->tx_stream);
+               amdtp_stream_stop(&tscm->rx_stream);
 
-       fw_iso_resources_free(&tscm->tx_resources);
-       fw_iso_resources_free(&tscm->rx_resources);
+               finish_session(tscm);
+       }
 }
 
 void snd_tscm_stream_lock_changed(struct snd_tscm *tscm)
index 6a411ee0dcf1ab60f6e701e025ed0b7bfda19ded..1cb75c374f118fdc869621b5348b829526129cc8 100644 (file)
@@ -147,6 +147,8 @@ int snd_tscm_stream_get_clock(struct snd_tscm *tscm,
 int snd_tscm_stream_init_duplex(struct snd_tscm *tscm);
 void snd_tscm_stream_update_duplex(struct snd_tscm *tscm);
 void snd_tscm_stream_destroy_duplex(struct snd_tscm *tscm);
+int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate);
+void snd_tscm_stream_release_duplex(struct snd_tscm *tscm);
 int snd_tscm_stream_start_duplex(struct snd_tscm *tscm, unsigned int rate);
 void snd_tscm_stream_stop_duplex(struct snd_tscm *tscm);