ALSA: firewire-tascam: reserve/release isochronous resources in pcm.hw_params/hw_free callbacks

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>
This commit is contained in:
Takashi Sakamoto 2019-06-02 16:12:48 +09:00 committed by Takashi Iwai
parent a364af2eec
commit 07b266424d
3 changed files with 71 additions and 42 deletions

View 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);

View 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;
err = snd_tscm_stream_get_rate(tscm, &curr_rate);
if (err < 0)
return err;
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);
err = set_clock(tscm, rate, INT_MAX);
if (err < 0)
return err;
err = keep_resources(tscm, rate, &tscm->tx_stream);
if (err < 0)
return err;
err = keep_resources(tscm, rate, &tscm->rx_stream);
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;
err = snd_tscm_stream_get_rate(tscm, &curr_rate);
if (err < 0)
return err;
if (curr_rate != rate ||
amdtp_streaming_error(&tscm->rx_stream) ||
if (amdtp_streaming_error(&tscm->rx_stream) ||
amdtp_streaming_error(&tscm->tx_stream)) {
finish_session(tscm);
amdtp_stream_stop(&tscm->rx_stream);
amdtp_stream_stop(&tscm->tx_stream);
fw_iso_resources_free(&tscm->tx_resources);
fw_iso_resources_free(&tscm->rx_resources);
finish_session(tscm);
}
if (!amdtp_stream_running(&tscm->rx_stream)) {
err = keep_resources(tscm, rate, &tscm->tx_stream);
if (err < 0)
goto error;
err = keep_resources(tscm, rate, &tscm->rx_stream);
if (err < 0)
goto error;
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;
if (tscm->substreams_counter == 0) {
amdtp_stream_stop(&tscm->tx_stream);
amdtp_stream_stop(&tscm->rx_stream);
amdtp_stream_stop(&tscm->tx_stream);
amdtp_stream_stop(&tscm->rx_stream);
finish_session(tscm);
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)

View 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);