From d3d10a4a1b1950c2d20714c2511b5f58c99bab81 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 21 May 2019 23:57:34 +0900 Subject: ALSA: firewire-lib: use union for directional parameters Some parameters of struct amdtp_stream is dependent on direction. This commit uses union for such parameters to distinguish from common parameters. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks_stream.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 827161bc269c..74e122e6e68a 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -165,13 +165,13 @@ int snd_efw_stream_init_duplex(struct snd_efw *efw) (efw->firmware_version == 0x5070000 || efw->firmware_version == 0x5070300 || efw->firmware_version == 0x5080000)) - efw->tx_stream.tx_first_dbc = 0x02; + efw->tx_stream.ctx_data.tx.first_dbc = 0x02; /* AudioFire9 always reports wrong dbs. */ if (efw->is_af9) efw->tx_stream.flags |= CIP_WRONG_DBS; /* Firmware version 5.5 reports fixed interval for dbc. */ if (efw->firmware_version == 0x5050000) - efw->tx_stream.tx_dbc_interval = 8; + efw->tx_stream.ctx_data.tx.dbc_interval = 8; err = init_stream(efw, &efw->rx_stream); if (err < 0) { -- cgit v1.2.3 From 1dc5921082d48dc53e48e88ae2f9a24b9737307d Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Wed, 12 Jun 2019 17:44:08 +0900 Subject: ALSA: fireworks: unify substream counter This commit is a part of preparation to perform allocation/release of isochronous resources in pcm.hw_params/hw_free callbacks. It's inconvenient to handle two isochronous context separately each other. This commit unifies the counters to handle the two at the same time. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks.h | 3 +-- sound/firewire/fireworks/fireworks_midi.c | 8 ++++---- sound/firewire/fireworks/fireworks_pcm.c | 8 ++++---- sound/firewire/fireworks/fireworks_stream.c | 27 ++++++++++----------------- 4 files changed, 19 insertions(+), 27 deletions(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks.h b/sound/firewire/fireworks/fireworks.h index 9b19c7f05d57..42a73038ba4d 100644 --- a/sound/firewire/fireworks/fireworks.h +++ b/sound/firewire/fireworks/fireworks.h @@ -89,8 +89,7 @@ struct snd_efw { struct amdtp_stream rx_stream; struct cmp_connection out_conn; struct cmp_connection in_conn; - unsigned int capture_substreams; - unsigned int playback_substreams; + unsigned int substreams_counter; /* hardware metering parameters */ unsigned int phys_out; diff --git a/sound/firewire/fireworks/fireworks_midi.c b/sound/firewire/fireworks/fireworks_midi.c index f5da2cd4ce42..7e78cdfe699e 100644 --- a/sound/firewire/fireworks/fireworks_midi.c +++ b/sound/firewire/fireworks/fireworks_midi.c @@ -18,7 +18,7 @@ static int midi_capture_open(struct snd_rawmidi_substream *substream) goto end; mutex_lock(&efw->mutex); - efw->capture_substreams++; + ++efw->substreams_counter; err = snd_efw_stream_start_duplex(efw, 0); mutex_unlock(&efw->mutex); if (err < 0) @@ -38,7 +38,7 @@ static int midi_playback_open(struct snd_rawmidi_substream *substream) goto end; mutex_lock(&efw->mutex); - efw->playback_substreams++; + ++efw->substreams_counter; err = snd_efw_stream_start_duplex(efw, 0); mutex_unlock(&efw->mutex); if (err < 0) @@ -52,7 +52,7 @@ static int midi_capture_close(struct snd_rawmidi_substream *substream) struct snd_efw *efw = substream->rmidi->private_data; mutex_lock(&efw->mutex); - efw->capture_substreams--; + --efw->substreams_counter; snd_efw_stream_stop_duplex(efw); mutex_unlock(&efw->mutex); @@ -65,7 +65,7 @@ static int midi_playback_close(struct snd_rawmidi_substream *substream) struct snd_efw *efw = substream->rmidi->private_data; mutex_lock(&efw->mutex); - efw->playback_substreams--; + --efw->substreams_counter; snd_efw_stream_stop_duplex(efw); mutex_unlock(&efw->mutex); diff --git a/sound/firewire/fireworks/fireworks_pcm.c b/sound/firewire/fireworks/fireworks_pcm.c index aed566d82726..a66f6a381dac 100644 --- a/sound/firewire/fireworks/fireworks_pcm.c +++ b/sound/firewire/fireworks/fireworks_pcm.c @@ -232,7 +232,7 @@ static int pcm_capture_hw_params(struct snd_pcm_substream *substream, if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { mutex_lock(&efw->mutex); - efw->capture_substreams++; + ++efw->substreams_counter; mutex_unlock(&efw->mutex); } @@ -251,7 +251,7 @@ static int pcm_playback_hw_params(struct snd_pcm_substream *substream, if (substream->runtime->status->state == SNDRV_PCM_STATE_OPEN) { mutex_lock(&efw->mutex); - efw->playback_substreams++; + ++efw->substreams_counter; mutex_unlock(&efw->mutex); } @@ -264,7 +264,7 @@ static int pcm_capture_hw_free(struct snd_pcm_substream *substream) if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) { mutex_lock(&efw->mutex); - efw->capture_substreams--; + --efw->substreams_counter; mutex_unlock(&efw->mutex); } @@ -278,7 +278,7 @@ static int pcm_playback_hw_free(struct snd_pcm_substream *substream) if (substream->runtime->status->state != SNDRV_PCM_STATE_OPEN) { mutex_lock(&efw->mutex); - efw->playback_substreams--; + --efw->substreams_counter; mutex_unlock(&efw->mutex); } diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 74e122e6e68a..2df39befcde0 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -194,9 +194,9 @@ int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) unsigned int curr_rate; int err = 0; - /* Need no substreams */ - if (efw->playback_substreams == 0 && efw->capture_substreams == 0) - goto end; + // Need no substreams. + if (efw->substreams_counter == 0) + return -EIO; /* * Considering JACK/FFADO streaming: @@ -206,19 +206,15 @@ int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) if (err < 0) goto end; - /* packet queueing error */ - if (amdtp_streaming_error(&efw->tx_stream)) - stop_stream(efw, &efw->tx_stream); - if (amdtp_streaming_error(&efw->rx_stream)) - stop_stream(efw, &efw->rx_stream); - /* stop streams if rate is different */ err = snd_efw_command_get_sampling_rate(efw, &curr_rate); if (err < 0) goto end; if (rate == 0) rate = curr_rate; - if (rate != curr_rate) { + if (rate != curr_rate || + amdtp_streaming_error(&efw->tx_stream) || + amdtp_streaming_error(&efw->rx_stream)) { stop_stream(efw, &efw->tx_stream); stop_stream(efw, &efw->rx_stream); } @@ -237,13 +233,12 @@ int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) } } - /* start slave if needed */ - if (efw->capture_substreams > 0 && - !amdtp_stream_running(&efw->tx_stream)) { + if (!amdtp_stream_running(&efw->tx_stream)) { err = start_stream(efw, &efw->tx_stream, rate); if (err < 0) { dev_err(&efw->unit->device, "fail to start AMDTP slave stream:%d\n", err); + stop_stream(efw, &efw->tx_stream); stop_stream(efw, &efw->rx_stream); } } @@ -253,11 +248,9 @@ end: void snd_efw_stream_stop_duplex(struct snd_efw *efw) { - if (efw->capture_substreams == 0) { + if (efw->substreams_counter == 0) { stop_stream(efw, &efw->tx_stream); - - if (efw->playback_substreams == 0) - stop_stream(efw, &efw->rx_stream); + stop_stream(efw, &efw->rx_stream); } } -- cgit v1.2.3 From 3d7250667ea96e7f9738caa6d5af85d87982066e Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Wed, 12 Jun 2019 17:44:11 +0900 Subject: ALSA: fireworks: configure sampling transfer frequency in pcm.hw_params callback This commit is a part of preparation to perform allocation/release of isochronous resources in pcm.hw_params/hw_free callbacks. At present, several operations are done in pcm.prepare callback. To reduce load of the callback, This commit splits out an operation to set sampling transfer frequency in pcm.hw_params callback. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks.h | 3 +- sound/firewire/fireworks/fireworks_midi.c | 7 +++- sound/firewire/fireworks/fireworks_pcm.c | 14 ++++--- sound/firewire/fireworks/fireworks_stream.c | 65 +++++++++++++++++++---------- 4 files changed, 57 insertions(+), 32 deletions(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks.h b/sound/firewire/fireworks/fireworks.h index 42a73038ba4d..0c1802aa7923 100644 --- a/sound/firewire/fireworks/fireworks.h +++ b/sound/firewire/fireworks/fireworks.h @@ -206,7 +206,8 @@ int snd_efw_command_get_sampling_rate(struct snd_efw *efw, unsigned int *rate); int snd_efw_command_set_sampling_rate(struct snd_efw *efw, unsigned int rate); int snd_efw_stream_init_duplex(struct snd_efw *efw); -int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate); +int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate); +int snd_efw_stream_start_duplex(struct snd_efw *efw); void snd_efw_stream_stop_duplex(struct snd_efw *efw); void snd_efw_stream_update_duplex(struct snd_efw *efw); void snd_efw_stream_destroy_duplex(struct snd_efw *efw); diff --git a/sound/firewire/fireworks/fireworks_midi.c b/sound/firewire/fireworks/fireworks_midi.c index ee5dc7be70b6..6d3d942e2dce 100644 --- a/sound/firewire/fireworks/fireworks_midi.c +++ b/sound/firewire/fireworks/fireworks_midi.c @@ -18,8 +18,11 @@ static int midi_open(struct snd_rawmidi_substream *substream) goto end; mutex_lock(&efw->mutex); - ++efw->substreams_counter; - err = snd_efw_stream_start_duplex(efw, 0); + err = snd_efw_stream_reserve_duplex(efw, 0); + if (err >= 0) { + ++efw->substreams_counter; + err = snd_efw_stream_start_duplex(efw); + } mutex_unlock(&efw->mutex); if (err < 0) snd_efw_stream_lock_release(efw); diff --git a/sound/firewire/fireworks/fireworks_pcm.c b/sound/firewire/fireworks/fireworks_pcm.c index 398a6ad04c5f..287fc05d5917 100644 --- a/sound/firewire/fireworks/fireworks_pcm.c +++ b/sound/firewire/fireworks/fireworks_pcm.c @@ -231,12 +231,16 @@ static int pcm_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(&efw->mutex); - ++efw->substreams_counter; + err = snd_efw_stream_reserve_duplex(efw, rate); + if (err >= 0) + ++efw->substreams_counter; mutex_unlock(&efw->mutex); } - return 0; + return err; } static int pcm_hw_free(struct snd_pcm_substream *substream) @@ -257,10 +261,9 @@ static int pcm_hw_free(struct snd_pcm_substream *substream) static int pcm_capture_prepare(struct snd_pcm_substream *substream) { struct snd_efw *efw = substream->private_data; - struct snd_pcm_runtime *runtime = substream->runtime; int err; - err = snd_efw_stream_start_duplex(efw, runtime->rate); + err = snd_efw_stream_start_duplex(efw); if (err >= 0) amdtp_stream_pcm_prepare(&efw->tx_stream); @@ -269,10 +272,9 @@ static int pcm_capture_prepare(struct snd_pcm_substream *substream) static int pcm_playback_prepare(struct snd_pcm_substream *substream) { struct snd_efw *efw = substream->private_data; - struct snd_pcm_runtime *runtime = substream->runtime; int err; - err = snd_efw_stream_start_duplex(efw, runtime->rate); + err = snd_efw_stream_start_duplex(efw); if (err >= 0) amdtp_stream_pcm_prepare(&efw->rx_stream); diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 2df39befcde0..e1ebead583e9 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -189,47 +189,63 @@ end: return err; } -int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) +int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) { unsigned int curr_rate; - int err = 0; - - // Need no substreams. - if (efw->substreams_counter == 0) - return -EIO; + int err; - /* - * Considering JACK/FFADO streaming: - * TODO: This can be removed hwdep functionality becomes popular. - */ + // Considering JACK/FFADO streaming: + // TODO: This can be removed hwdep functionality becomes popular. err = check_connection_used_by_others(efw, &efw->rx_stream); if (err < 0) - goto end; + return err; - /* stop streams if rate is different */ + // stop streams if rate is different. err = snd_efw_command_get_sampling_rate(efw, &curr_rate); if (err < 0) - goto end; + return err; if (rate == 0) rate = curr_rate; - if (rate != curr_rate || - amdtp_streaming_error(&efw->tx_stream) || - amdtp_streaming_error(&efw->rx_stream)) { + if (rate != curr_rate) { stop_stream(efw, &efw->tx_stream); stop_stream(efw, &efw->rx_stream); } - /* master should be always running */ - if (!amdtp_stream_running(&efw->rx_stream)) { + if (efw->substreams_counter == 0 || rate != curr_rate) { err = snd_efw_command_set_sampling_rate(efw, rate); if (err < 0) - goto end; + return err; + } + + return 0; +} + +int snd_efw_stream_start_duplex(struct snd_efw *efw) +{ + unsigned int rate; + int err = 0; + + // Need no substreams. + if (efw->substreams_counter == 0) + return -EIO; + + err = snd_efw_command_get_sampling_rate(efw, &rate); + if (err < 0) + return err; + if (amdtp_streaming_error(&efw->rx_stream) || + amdtp_streaming_error(&efw->tx_stream)) { + stop_stream(efw, &efw->rx_stream); + stop_stream(efw, &efw->tx_stream); + } + + /* master should be always running */ + if (!amdtp_stream_running(&efw->rx_stream)) { err = start_stream(efw, &efw->rx_stream, rate); if (err < 0) { dev_err(&efw->unit->device, "fail to start AMDTP master stream:%d\n", err); - goto end; + goto error; } } @@ -238,11 +254,14 @@ int snd_efw_stream_start_duplex(struct snd_efw *efw, unsigned int rate) if (err < 0) { dev_err(&efw->unit->device, "fail to start AMDTP slave stream:%d\n", err); - stop_stream(efw, &efw->tx_stream); - stop_stream(efw, &efw->rx_stream); + goto error; } } -end: + + return 0; +error: + stop_stream(efw, &efw->rx_stream); + stop_stream(efw, &efw->tx_stream); return err; } -- cgit v1.2.3 From 206cf896d6e9024dbdf5722c4676f2e671191b97 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Wed, 12 Jun 2019 17:44:12 +0900 Subject: ALSA: fireworks: configure stream parameters in pcm.hw_params callback This commit is a part of preparation to perform allocation/release of isochronous resources in pcm.hw_params/hw_free callbacks. This commit splits out an operation to configure stream parameters into pcm.hw_params callback. In pcm.prepare callback, establishing connections and start isochronous contexts. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks_stream.c | 82 +++++++++++++++++------------ 1 file changed, 49 insertions(+), 33 deletions(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index e1ebead583e9..1abc15760513 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -52,54 +52,38 @@ stop_stream(struct snd_efw *efw, struct amdtp_stream *stream) cmp_connection_break(&efw->in_conn); } -static int -start_stream(struct snd_efw *efw, struct amdtp_stream *stream, - unsigned int sampling_rate) +static int start_stream(struct snd_efw *efw, struct amdtp_stream *stream, + unsigned int rate) { struct cmp_connection *conn; - unsigned int mode, pcm_channels, midi_ports; int err; - err = snd_efw_get_multiplier_mode(sampling_rate, &mode); - if (err < 0) - goto end; - if (stream == &efw->tx_stream) { + if (stream == &efw->tx_stream) conn = &efw->out_conn; - pcm_channels = efw->pcm_capture_channels[mode]; - midi_ports = efw->midi_out_ports; - } else { + else conn = &efw->in_conn; - pcm_channels = efw->pcm_playback_channels[mode]; - midi_ports = efw->midi_in_ports; - } - err = amdtp_am824_set_parameters(stream, sampling_rate, - pcm_channels, midi_ports, false); - if (err < 0) - goto end; - - /* establish connection via CMP */ + // Establish connection via CMP. err = cmp_connection_establish(conn, - amdtp_stream_get_max_payload(stream)); + amdtp_stream_get_max_payload(stream)); if (err < 0) - goto end; + return err; - /* start amdtp stream */ - err = amdtp_stream_start(stream, - conn->resources.channel, - conn->speed); + // Start amdtp stream. + err = amdtp_stream_start(stream, conn->resources.channel, conn->speed); if (err < 0) { - stop_stream(efw, stream); - goto end; + cmp_connection_break(conn); + return err; } - /* wait first callback */ + // Wait first callback. if (!amdtp_stream_wait_callback(stream, CALLBACK_TIMEOUT)) { - stop_stream(efw, stream); - err = -ETIMEDOUT; + amdtp_stream_stop(stream); + cmp_connection_break(conn); + return -ETIMEDOUT; } -end: - return err; + + return 0; } /* @@ -189,6 +173,24 @@ end: return err; } +static int keep_resources(struct snd_efw *efw, struct amdtp_stream *stream, + unsigned int rate, unsigned int mode) +{ + unsigned int pcm_channels; + unsigned int midi_ports; + + if (stream == &efw->tx_stream) { + pcm_channels = efw->pcm_capture_channels[mode]; + midi_ports = efw->midi_out_ports; + } else { + pcm_channels = efw->pcm_playback_channels[mode]; + midi_ports = efw->midi_in_ports; + } + + return amdtp_am824_set_parameters(stream, rate, pcm_channels, + midi_ports, false); +} + int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) { unsigned int curr_rate; @@ -212,9 +214,23 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) } if (efw->substreams_counter == 0 || rate != curr_rate) { + unsigned int mode; + err = snd_efw_command_set_sampling_rate(efw, rate); if (err < 0) return err; + + err = snd_efw_get_multiplier_mode(rate, &mode); + if (err < 0) + return err; + + err = keep_resources(efw, &efw->tx_stream, rate, mode); + if (err < 0) + return err; + + err = keep_resources(efw, &efw->rx_stream, rate, mode); + if (err < 0) + return err; } return 0; -- cgit v1.2.3 From 5ad840728ac3f56008da03481a96d3ed175e7c3f Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Wed, 12 Jun 2019 17:44:13 +0900 Subject: ALSA: fireworks: don't set XRUN in stop streaming When stopping packet streaming, no need to stop PCM substream with XRUN state. This commit suppresses it. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks_stream.c | 1 - 1 file changed, 1 deletion(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 1abc15760513..61342c49dc38 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -43,7 +43,6 @@ end: static void stop_stream(struct snd_efw *efw, struct amdtp_stream *stream) { - amdtp_stream_pcm_abort(stream); amdtp_stream_stop(stream); if (stream == &efw->tx_stream) -- cgit v1.2.3 From 7bc93821a70adc621df443c8b7a4745023c36e7c Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Sat, 15 Jun 2019 18:11:01 +0900 Subject: ALSA: firewire-lib: split allocation of isochronous resources from establishment of connection In current implementation, establishment connection corresponds to allocation of isochronous resources. Although this is an ideal implementation of CMP described in IEC 61883-1, it's not enough efficient to recover PCM substream multiplexed in packet streaming. The packet streaming can always restart on the same allocated isochronous resources even if the previous packet streaming corrupted. This commit splits allocation of isochronous resources from establishment of connection so that CMP runs with allocated isochronous resources. Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/bebob/bebob_stream.c | 30 ++++++++---- sound/firewire/cmp.c | 74 +++++++++++++++++------------ sound/firewire/cmp.h | 7 ++- sound/firewire/fireworks/fireworks_stream.c | 22 +++++++-- sound/firewire/oxfw/oxfw-stream.c | 18 +++++-- 5 files changed, 100 insertions(+), 51 deletions(-) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/bebob/bebob_stream.c b/sound/firewire/bebob/bebob_stream.c index 9ef4663d13e5..1070a675179d 100644 --- a/sound/firewire/bebob/bebob_stream.c +++ b/sound/firewire/bebob/bebob_stream.c @@ -404,13 +404,11 @@ static int make_both_connections(struct snd_bebob *bebob) { int err = 0; - err = cmp_connection_establish(&bebob->out_conn, - amdtp_stream_get_max_payload(&bebob->tx_stream)); + err = cmp_connection_establish(&bebob->out_conn); if (err < 0) return err; - err = cmp_connection_establish(&bebob->in_conn, - amdtp_stream_get_max_payload(&bebob->rx_stream)); + err = cmp_connection_establish(&bebob->in_conn); if (err < 0) { cmp_connection_break(&bebob->out_conn); return err; @@ -533,14 +531,23 @@ static int keep_resources(struct snd_bebob *bebob, struct amdtp_stream *stream, unsigned int rate, unsigned int index) { struct snd_bebob_stream_formation *formation; + struct cmp_connection *conn; + int err; - if (stream == &bebob->tx_stream) + if (stream == &bebob->tx_stream) { formation = bebob->tx_stream_formations + index; - else + conn = &bebob->out_conn; + } else { formation = bebob->rx_stream_formations + index; + conn = &bebob->in_conn; + } + + err = amdtp_am824_set_parameters(stream, rate, formation->pcm, + formation->midi, false); + if (err < 0) + return err; - return amdtp_am824_set_parameters(stream, rate, formation->pcm, - formation->midi, false); + return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); } int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate) @@ -591,8 +598,10 @@ int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate) return err; err = keep_resources(bebob, &bebob->rx_stream, rate, index); - if (err < 0) + if (err < 0) { + cmp_connection_release(&bebob->out_conn); return err; + } } return 0; @@ -685,6 +694,9 @@ void snd_bebob_stream_stop_duplex(struct snd_bebob *bebob) amdtp_stream_stop(&bebob->tx_stream); break_both_connections(bebob); + + cmp_connection_release(&bebob->out_conn); + cmp_connection_release(&bebob->in_conn); } } diff --git a/sound/firewire/cmp.c b/sound/firewire/cmp.c index ae3bc1940efa..5dedc4f31842 100644 --- a/sound/firewire/cmp.c +++ b/sound/firewire/cmp.c @@ -185,6 +185,37 @@ void cmp_connection_destroy(struct cmp_connection *c) } EXPORT_SYMBOL(cmp_connection_destroy); +int cmp_connection_reserve(struct cmp_connection *c, + unsigned int max_payload_bytes) +{ + int err; + + mutex_lock(&c->mutex); + + if (WARN_ON(c->resources.allocated)) { + err = -EBUSY; + goto end; + } + + c->speed = min(c->max_speed, + fw_parent_device(c->resources.unit)->max_speed); + + err = fw_iso_resources_allocate(&c->resources, max_payload_bytes, + c->speed); +end: + mutex_unlock(&c->mutex); + + return err; +} +EXPORT_SYMBOL(cmp_connection_reserve); + +void cmp_connection_release(struct cmp_connection *c) +{ + mutex_lock(&c->mutex); + fw_iso_resources_free(&c->resources); + mutex_unlock(&c->mutex); +} +EXPORT_SYMBOL(cmp_connection_release); static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr) { @@ -270,25 +301,18 @@ static int pcr_set_check(struct cmp_connection *c, __be32 pcr) * When this function succeeds, the caller is responsible for starting * transmitting packets. */ -int cmp_connection_establish(struct cmp_connection *c, - unsigned int max_payload_bytes) +int cmp_connection_establish(struct cmp_connection *c) { int err; - if (WARN_ON(c->connected)) - return -EISCONN; - - c->speed = min(c->max_speed, - fw_parent_device(c->resources.unit)->max_speed); - mutex_lock(&c->mutex); -retry_after_bus_reset: - err = fw_iso_resources_allocate(&c->resources, - max_payload_bytes, c->speed); - if (err < 0) - goto err_mutex; + if (WARN_ON(c->connected)) { + mutex_unlock(&c->mutex); + return -EISCONN; + } +retry_after_bus_reset: if (c->direction == CMP_OUTPUT) err = pcr_modify(c, opcr_set_modify, pcr_set_check, ABORT_ON_BUS_RESET); @@ -297,21 +321,13 @@ retry_after_bus_reset: ABORT_ON_BUS_RESET); if (err == -EAGAIN) { - fw_iso_resources_free(&c->resources); - goto retry_after_bus_reset; + err = fw_iso_resources_update(&c->resources); + if (err >= 0) + goto retry_after_bus_reset; } - if (err < 0) - goto err_resources; - - c->connected = true; - - mutex_unlock(&c->mutex); - - return 0; + if (err >= 0) + c->connected = true; -err_resources: - fw_iso_resources_free(&c->resources); -err_mutex: mutex_unlock(&c->mutex); return err; @@ -351,14 +367,12 @@ int cmp_connection_update(struct cmp_connection *c) SUCCEED_ON_BUS_RESET); if (err < 0) - goto err_resources; + goto err_unconnect; mutex_unlock(&c->mutex); return 0; -err_resources: - fw_iso_resources_free(&c->resources); err_unconnect: c->connected = false; mutex_unlock(&c->mutex); @@ -395,8 +409,6 @@ void cmp_connection_break(struct cmp_connection *c) if (err < 0) cmp_error(c, "plug is still connected\n"); - fw_iso_resources_free(&c->resources); - c->connected = false; mutex_unlock(&c->mutex); diff --git a/sound/firewire/cmp.h b/sound/firewire/cmp.h index b60b415caa8f..26ab88000e34 100644 --- a/sound/firewire/cmp.h +++ b/sound/firewire/cmp.h @@ -42,8 +42,11 @@ int cmp_connection_init(struct cmp_connection *connection, int cmp_connection_check_used(struct cmp_connection *connection, bool *used); void cmp_connection_destroy(struct cmp_connection *connection); -int cmp_connection_establish(struct cmp_connection *connection, - unsigned int max_payload); +int cmp_connection_reserve(struct cmp_connection *connection, + unsigned int max_payload); +void cmp_connection_release(struct cmp_connection *connection); + +int cmp_connection_establish(struct cmp_connection *connection); int cmp_connection_update(struct cmp_connection *connection); void cmp_connection_break(struct cmp_connection *connection); diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 61342c49dc38..81c1bb209a89 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -63,8 +63,7 @@ static int start_stream(struct snd_efw *efw, struct amdtp_stream *stream, conn = &efw->in_conn; // Establish connection via CMP. - err = cmp_connection_establish(conn, - amdtp_stream_get_max_payload(stream)); + err = cmp_connection_establish(conn); if (err < 0) return err; @@ -177,17 +176,25 @@ static int keep_resources(struct snd_efw *efw, struct amdtp_stream *stream, { unsigned int pcm_channels; unsigned int midi_ports; + struct cmp_connection *conn; + int err; if (stream == &efw->tx_stream) { pcm_channels = efw->pcm_capture_channels[mode]; midi_ports = efw->midi_out_ports; + conn = &efw->out_conn; } else { pcm_channels = efw->pcm_playback_channels[mode]; midi_ports = efw->midi_in_ports; + conn = &efw->in_conn; } - return amdtp_am824_set_parameters(stream, rate, pcm_channels, - midi_ports, false); + err = amdtp_am824_set_parameters(stream, rate, pcm_channels, + midi_ports, false); + if (err < 0) + return err; + + return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); } int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) @@ -228,8 +235,10 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) return err; err = keep_resources(efw, &efw->rx_stream, rate, mode); - if (err < 0) + if (err < 0) { + cmp_connection_release(&efw->in_conn); return err; + } } return 0; @@ -285,6 +294,9 @@ void snd_efw_stream_stop_duplex(struct snd_efw *efw) if (efw->substreams_counter == 0) { stop_stream(efw, &efw->tx_stream); stop_stream(efw, &efw->rx_stream); + + cmp_connection_release(&efw->out_conn); + cmp_connection_release(&efw->in_conn); } } diff --git a/sound/firewire/oxfw/oxfw-stream.c b/sound/firewire/oxfw/oxfw-stream.c index 837733f10736..a8bc798731ff 100644 --- a/sound/firewire/oxfw/oxfw-stream.c +++ b/sound/firewire/oxfw/oxfw-stream.c @@ -111,8 +111,7 @@ static int start_stream(struct snd_oxfw *oxfw, struct amdtp_stream *stream) else conn = &oxfw->out_conn; - err = cmp_connection_establish(conn, - amdtp_stream_get_max_payload(stream)); + err = cmp_connection_establish(conn); if (err < 0) return err; @@ -203,15 +202,18 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) enum avc_general_plug_dir dir; u8 **formats; struct snd_oxfw_stream_formation formation; + struct cmp_connection *conn; int i; int err; if (stream == &oxfw->rx_stream) { dir = AVC_GENERAL_PLUG_DIR_IN; formats = oxfw->rx_stream_formats; + conn = &oxfw->in_conn; } else { dir = AVC_GENERAL_PLUG_DIR_OUT; formats = oxfw->tx_stream_formats; + conn = &oxfw->out_conn; } err = snd_oxfw_stream_get_current_formation(oxfw, dir, &formation); @@ -239,8 +241,12 @@ static int keep_resources(struct snd_oxfw *oxfw, struct amdtp_stream *stream) if (formation.pcm == 0) return -EINVAL; - return amdtp_am824_set_parameters(stream, formation.rate, formation.pcm, + err = amdtp_am824_set_parameters(stream, formation.rate, formation.pcm, formation.midi * 8, false); + if (err < 0) + return err; + + return cmp_connection_reserve(conn, amdtp_stream_get_max_payload(stream)); } int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw, @@ -299,8 +305,10 @@ int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw, if (oxfw->has_output) { err = keep_resources(oxfw, &oxfw->tx_stream); - if (err < 0) + if (err < 0) { + cmp_connection_release(&oxfw->in_conn); return err; + } } } @@ -361,10 +369,12 @@ void snd_oxfw_stream_stop_duplex(struct snd_oxfw *oxfw) if (oxfw->substreams_count == 0) { amdtp_stream_stop(&oxfw->rx_stream); cmp_connection_break(&oxfw->in_conn); + cmp_connection_release(&oxfw->in_conn); if (oxfw->has_output) { amdtp_stream_stop(&oxfw->tx_stream); cmp_connection_break(&oxfw->out_conn); + cmp_connection_release(&oxfw->out_conn); } } } -- cgit v1.2.3 From a9679dd31dd379e7e14a13c55bd0a3214c18fe47 Mon Sep 17 00:00:00 2001 From: Takashi Sakamoto Date: Tue, 18 Jun 2019 22:26:21 +0900 Subject: ALSA: fireworks: ensure to release isochronous resources in pcm.hw_params callback When stopping packet streaming in reserve function for duplex streams, isochronous resources should be released. Fixes: 7bc93821a70a ("ALSA: firewire-lib: split allocation of isochronous resources from establishment of connection") Signed-off-by: Takashi Sakamoto Signed-off-by: Takashi Iwai --- sound/firewire/fireworks/fireworks_stream.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'sound/firewire/fireworks/fireworks_stream.c') diff --git a/sound/firewire/fireworks/fireworks_stream.c b/sound/firewire/fireworks/fireworks_stream.c index 81c1bb209a89..16cf635a6f57 100644 --- a/sound/firewire/fireworks/fireworks_stream.c +++ b/sound/firewire/fireworks/fireworks_stream.c @@ -217,6 +217,9 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate) if (rate != curr_rate) { stop_stream(efw, &efw->tx_stream); stop_stream(efw, &efw->rx_stream); + + cmp_connection_release(&efw->out_conn); + cmp_connection_release(&efw->in_conn); } if (efw->substreams_counter == 0 || rate != curr_rate) { -- cgit v1.2.3