diff mbox series

ASoC: core: add support to card re-bind/unbind using component framework

Message ID 20180711084318.11786-1-srinivas.kandagatla@linaro.org
State New
Headers show
Series ASoC: core: add support to card re-bind/unbind using component framework | expand

Commit Message

Srinivas Kandagatla July 11, 2018, 8:43 a.m. UTC
This patch aims at add achieving dynamic behaviour of audio card when
the dependent components disappear and reappear.

With this patch the card is removed if any of the dependent component
is removed and card is added back if the dependent component comes back.
All this is done using component framework and matching based on
component name.

Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

---

During discussion regarding card re-binding when components unregister
and register back at https://lkml.org/lkml/2018/7/9/785 it was suggested
that component framework can be added into core to provide this feature.

With this new changes the card will re-bind once the dependent component
re-registers after unregistering. This works based on the match done
from component name using component framework.

I have tested this patch with qdsp start-stop usecase for more than 10000
times in loop on Qcom platforms.

I will send qdsp side cleanup patches once I get some feedback on this patch.


 include/sound/soc.h  |  5 ++++
 sound/soc/soc-core.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 70 insertions(+), 5 deletions(-)

-- 
2.16.2

Comments

Vinod Koul July 12, 2018, 10:59 a.m. UTC | #1
On 11-07-18, 09:43, Srinivas Kandagatla wrote:
> This patch aims at add achieving dynamic behaviour of audio card when

> the dependent components disappear and reappear.

> 

> With this patch the card is removed if any of the dependent component

> is removed and card is added back if the dependent component comes back.

> All this is done using component framework and matching based on

> component name.

> 

> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>


Looks fine mostly, some nitpicks below:

> ---

> 

> During discussion regarding card re-binding when components unregister

> and register back at https://lkml.org/lkml/2018/7/9/785 it was suggested

> that component framework can be added into core to provide this feature.

> 

> With this new changes the card will re-bind once the dependent component

> re-registers after unregistering. This works based on the match done

> from component name using component framework.

> 

> I have tested this patch with qdsp start-stop usecase for more than 10000

> times in loop on Qcom platforms.

> 

> I will send qdsp side cleanup patches once I get some feedback on this patch.

> 

> 

>  include/sound/soc.h  |  5 ++++

>  sound/soc/soc-core.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----

>  2 files changed, 70 insertions(+), 5 deletions(-)

> 

> diff --git a/include/sound/soc.h b/include/sound/soc.h

> index 870ba6b64817..b94149d29c0d 100644

> --- a/include/sound/soc.h

> +++ b/include/sound/soc.h

> @@ -17,6 +17,7 @@

>  #include <linux/workqueue.h>

>  #include <linux/interrupt.h>

>  #include <linux/kernel.h>

> +#include <linux/component.h>

>  #include <linux/regmap.h>

>  #include <linux/log2.h>

>  #include <sound/core.h>

> @@ -1088,6 +1089,10 @@ struct snd_soc_card {

>  

>  	struct work_struct deferred_resume_work;

>  

> +	/* component framework related */

> +	bool components_added;

> +	struct component_match *match;

> +

>  	/* lists of probed devices belonging to this card */

>  	struct list_head component_dev_list;

>  

> diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c

> index 6d33634b934b..377ed8e67686 100644

> --- a/sound/soc/soc-core.c

> +++ b/sound/soc/soc-core.c

> @@ -279,11 +279,31 @@ static inline void snd_soc_debugfs_exit(void)

>  

>  #endif

>  

> +static int snd_soc_card_comp_compare(struct device *dev, void *data)


Why not make last arg as name and avoid void *

> +{

> +	struct snd_soc_component *component = NULL;

> +	struct snd_soc_component *t;


why do you need two variables for this
> +

> +	lockdep_assert_held(&client_mutex);

> +	list_for_each_entry(t, &component_list, list) {

> +		if (dev == t->dev) {

> +			component = t;


you can skip this line and use t in below code.

> +			break;

> +		}

> +	}

> +

> +	if (component && !strcmp(component->name, data))


strncmp?

-- 
~Vinod
Vinod Koul July 12, 2018, 12:39 p.m. UTC | #2
On 12-07-18, 13:02, Srinivas Kandagatla wrote:
> Thanks Vinod for taking look at this!

> 

> On 12/07/18 11:59, Vinod wrote:

> > On 11-07-18, 09:43, Srinivas Kandagatla wrote:

> > > This patch aims at add achieving dynamic behaviour of audio card when

> > > the dependent components disappear and reappear.

> > > 

> > > With this patch the card is removed if any of the dependent component

> > > is removed and card is added back if the dependent component comes back.

> > > All this is done using component framework and matching based on

> > > component name.

> > > 

> > > Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

> > 

> > Looks fine mostly, some nitpicks below:

> > 

> > > ---

> > > 

> > > During discussion regarding card re-binding when components unregister

> > > and register back at https://lkml.org/lkml/2018/7/9/785 it was suggested

> > > that component framework can be added into core to provide this feature.

> > > 

> > > With this new changes the card will re-bind once the dependent component

> > > re-registers after unregistering. This works based on the match done

> > > from component name using component framework.

> > > 

> > > I have tested this patch with qdsp start-stop usecase for more than 10000

> > > times in loop on Qcom platforms.

> > > 

> > > I will send qdsp side cleanup patches once I get some feedback on this patch.

> > > 

> > > 

> > >   include/sound/soc.h  |  5 ++++

> > >   sound/soc/soc-core.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----

> > >   2 files changed, 70 insertions(+), 5 deletions(-)

> > > 

> > > diff --git a/include/sound/soc.h b/include/sound/soc.h

> > > index 870ba6b64817..b94149d29c0d 100644

> > > --- a/include/sound/soc.h

> > > +++ b/include/sound/soc.h

> > > @@ -17,6 +17,7 @@

> > >   #include <linux/workqueue.h>

> > >   #include <linux/interrupt.h>

> > >   #include <linux/kernel.h>

> > > +#include <linux/component.h>

> > >   #include <linux/regmap.h>

> > >   #include <linux/log2.h>

> > >   #include <sound/core.h>

> > > @@ -1088,6 +1089,10 @@ struct snd_soc_card {

> > >   	struct work_struct deferred_resume_work;

> > > +	/* component framework related */

> > > +	bool components_added;

> > > +	struct component_match *match;

> > > +

> > >   	/* lists of probed devices belonging to this card */

> > >   	struct list_head component_dev_list;

> > > diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c

> > > index 6d33634b934b..377ed8e67686 100644

> > > --- a/sound/soc/soc-core.c

> > > +++ b/sound/soc/soc-core.c

> > > @@ -279,11 +279,31 @@ static inline void snd_soc_debugfs_exit(void)

> > >   #endif

> > > +static int snd_soc_card_comp_compare(struct device *dev, void *data)

> > 

> > Why not make last arg as name and avoid void *

> > 

> 

> I can rename argument "data" to "name", but I can not change the prototype

> of the compare callback expected by component framework.


oh yes the data type can't be changed.

> 

> > > +{

> > > +	struct snd_soc_component *component = NULL;

> > > +	struct snd_soc_component *t;

> > 

> > why do you need two variables for this

> 

> We need two because one is iterator and other is find.

> 

> if we use just one we will endup with valid iterator item which may not have

> matched dev.

> 

> Or I can re-organize/simplify the code like this:

> 

> static int snd_soc_card_comp_compare(struct device *dev, void *name)

> {

>         struct snd_soc_component *t;

> 

>         lockdep_assert_held(&client_mutex);

>         list_for_each_entry(t, &component_list, list) {

>                 if (dev == t->dev) {

>                         if (!strcmp(t->name, name))

>                                 return 1;

>                 }

>         }

> 

>         return 0;

> }


looks better :)

> 

> > > +

> > > +	lockdep_assert_held(&client_mutex);

> > > +	list_for_each_entry(t, &component_list, list) {

> > > +		if (dev == t->dev) {

> > > +			component = t;

> > 

> > you can skip this line and use t in below code.

> > 

> > > +			break;

> > > +		}

> > > +	}

> > > +

> > > +	if (component && !strcmp(component->name, data))

> > 

> 

> 

> > strncmp?

> 

> AFAIU, strcmp should be safe here as component->name is generated/sanitized

> by core and would be of max len of NAME_SIZE.

> core uses strcmp in may places. Any particular reason you want me to move to

> strncmp?


I agree that name is generated but I don't trust strings :)

-- 
~Vinod
Takashi Iwai July 12, 2018, 3:42 p.m. UTC | #3
On Thu, 12 Jul 2018 17:26:00 +0200,
Pierre-Louis Bossart wrote:
> 

> On 7/11/18 3:43 AM, Srinivas Kandagatla wrote:

> > This patch aims at add achieving dynamic behaviour of audio card when

> > the dependent components disappear and reappear.

> >

> > With this patch the card is removed if any of the dependent component

> > is removed and card is added back if the dependent component comes back.

> > All this is done using component framework and matching based on

> > component name.

> 

> Humm, no real comment on this patch proper but rather on how userspace

> would deal with this dynamic behavior?

> We had similar opens when we worked on the BYT/CHT HDMI stuff and

> ended-up with quite a few issues in userspace (PulseAudio mainly)

> related to dynamic behavior at the kernel level as a result of

> plug/unplug. Also not sure how an Android HAL would deal with a card

> disappearing temporarily if DSP resources become unavailable or

> unresponsive. Any thoughts or guidance you might think of?


Was the card-level register / unregister really problematic with PA?
It's basically similar as the hotplug like USB, so I thought it would
work as is.


Takashi

> 

> >

> > Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

> > ---

> >

> > During discussion regarding card re-binding when components unregister

> > and register back at https://lkml.org/lkml/2018/7/9/785 it was suggested

> > that component framework can be added into core to provide this feature.

> >

> > With this new changes the card will re-bind once the dependent component

> > re-registers after unregistering. This works based on the match done

> > from component name using component framework.

> >

> > I have tested this patch with qdsp start-stop usecase for more than 10000

> > times in loop on Qcom platforms.

> >

> > I will send qdsp side cleanup patches once I get some feedback on this patch.

> >

> >

> >   include/sound/soc.h  |  5 ++++

> >   sound/soc/soc-core.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----

> >   2 files changed, 70 insertions(+), 5 deletions(-)

> >

> > diff --git a/include/sound/soc.h b/include/sound/soc.h

> > index 870ba6b64817..b94149d29c0d 100644

> > --- a/include/sound/soc.h

> > +++ b/include/sound/soc.h

> > @@ -17,6 +17,7 @@

> >   #include <linux/workqueue.h>

> >   #include <linux/interrupt.h>

> >   #include <linux/kernel.h>

> > +#include <linux/component.h>

> >   #include <linux/regmap.h>

> >   #include <linux/log2.h>

> >   #include <sound/core.h>

> > @@ -1088,6 +1089,10 @@ struct snd_soc_card {

> >     	struct work_struct deferred_resume_work;

> >   +	/* component framework related */

> > +	bool components_added;

> > +	struct component_match *match;

> > +

> >   	/* lists of probed devices belonging to this card */

> >   	struct list_head component_dev_list;

> >   diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c

> > index 6d33634b934b..377ed8e67686 100644

> > --- a/sound/soc/soc-core.c

> > +++ b/sound/soc/soc-core.c

> > @@ -279,11 +279,31 @@ static inline void snd_soc_debugfs_exit(void)

> >     #endif

> >   +static int snd_soc_card_comp_compare(struct device *dev, void

> > *data)

> > +{

> > +	struct snd_soc_component *component = NULL;

> > +	struct snd_soc_component *t;

> > +

> > +	lockdep_assert_held(&client_mutex);

> > +	list_for_each_entry(t, &component_list, list) {

> > +		if (dev == t->dev) {

> > +			component = t;

> > +			break;

> > +		}

> > +	}

> > +

> > +	if (component && !strcmp(component->name, data))

> > +		return 1;

> > +

> > +	return 0;

> > +}

> > +

> >   static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,

> >   			      struct snd_soc_component *component)

> >   {

> >   	struct snd_soc_rtdcom_list *rtdcom;

> >   	struct snd_soc_rtdcom_list *new_rtdcom;

> > +	char *cname;

> >     	for_each_rtdcom(rtd, rtdcom) {

> >   		/* already connected */

> > @@ -300,6 +320,13 @@ static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,

> >     	list_add_tail(&new_rtdcom->list, &rtd->component_list);

> >   +	if (!rtd->card->components_added) {

> > +		cname = devm_kasprintf(rtd->card->dev, GFP_KERNEL,

> > +				       "%s", component->name);

> > +		component_match_add(rtd->card->dev, &rtd->card->match,

> > +				    snd_soc_card_comp_compare, cname);

> > +	}

> > +

> >   	return 0;

> >   }

> >   @@ -835,6 +862,28 @@ static bool soc_is_dai_link_bound(struct

> > snd_soc_card *card,

> >   	return false;

> >   }

> >   +static int snd_soc_card_comp_bind(struct device *dev)

> > +{

> > +	struct snd_soc_card *card = dev_get_drvdata(dev);

> > +

> > +	if (card->instantiated)

> > +		return 0;

> > +

> > +	return snd_soc_register_card(card);

> > +}

> > +

> > +static void snd_soc_card_comp_unbind(struct device *dev)

> > +{

> > +	struct snd_soc_card *card = dev_get_drvdata(dev);

> > +

> > +	snd_soc_unregister_card(card);

> > +}

> > +

> > +static const struct component_master_ops snd_soc_card_comp_ops = {

> > +	.bind = snd_soc_card_comp_bind,

> > +	.unbind = snd_soc_card_comp_unbind,

> > +};

> > +

> >   static int soc_bind_dai_link(struct snd_soc_card *card,

> >   	struct snd_soc_dai_link *dai_link)

> >   {

> > @@ -2108,6 +2157,12 @@ static int snd_soc_instantiate_card(struct snd_soc_card *card)

> >     	card->instantiated = 1;

> >   	snd_soc_dapm_sync(&card->dapm);

> > +	if (!card->components_added) {

> > +		component_master_add_with_match(card->dev,

> > +						&snd_soc_card_comp_ops,

> > +						card->match);

> > +		card->components_added = true;

> > +	}

> >   	mutex_unlock(&card->mutex);

> >   	mutex_unlock(&client_mutex);

> >   @@ -3098,11 +3153,6 @@ static void

> > snd_soc_component_cleanup(struct snd_soc_component *component)

> >     static void snd_soc_component_del_unlocked(struct

> > snd_soc_component *component)

> >   {

> > -	struct snd_soc_card *card = component->card;

> > -

> > -	if (card)

> > -		snd_soc_unregister_card(card);

> > -

> >   	list_del(&component->list);

> >   }

> >   @@ -3169,8 +3219,17 @@ int snd_soc_add_component(struct device

> > *dev,

> >     	snd_soc_component_add(component);

> >   +	ret = component_add(dev, NULL);

> > +	if (ret < 0) {

> > +		dev_err(dev, "ASoC: Failed to add Component: %d\n", ret);

> > +		goto err_comp;

> > +	}

> > +

> >   	return 0;

> >   +err_comp:

> > +	soc_remove_component(component);

> > +	snd_soc_unregister_dais(component);

> >   err_cleanup:

> >   	snd_soc_component_cleanup(component);

> >   err_free:

> > @@ -3218,6 +3277,7 @@ static int __snd_soc_unregister_component(struct device *dev)

> >   	mutex_unlock(&client_mutex);

> >     	if (found) {

> > +		component_del(dev, NULL);

> >   		snd_soc_component_cleanup(component);

> >   	}

> >   

> >

> 

>
Pierre-Louis Bossart July 12, 2018, 3:54 p.m. UTC | #4
On 7/12/18 10:42 AM, Takashi Iwai wrote:
> On Thu, 12 Jul 2018 17:26:00 +0200,

> Pierre-Louis Bossart wrote:

>>

>> On 7/11/18 3:43 AM, Srinivas Kandagatla wrote:

>>> This patch aims at add achieving dynamic behaviour of audio card when

>>> the dependent components disappear and reappear.

>>>

>>> With this patch the card is removed if any of the dependent component

>>> is removed and card is added back if the dependent component comes back.

>>> All this is done using component framework and matching based on

>>> component name.

>>

>> Humm, no real comment on this patch proper but rather on how userspace

>> would deal with this dynamic behavior?

>> We had similar opens when we worked on the BYT/CHT HDMI stuff and

>> ended-up with quite a few issues in userspace (PulseAudio mainly)

>> related to dynamic behavior at the kernel level as a result of

>> plug/unplug. Also not sure how an Android HAL would deal with a card

>> disappearing temporarily if DSP resources become unavailable or

>> unresponsive. Any thoughts or guidance you might think of?

> 

> Was the card-level register / unregister really problematic with PA?


I don't recall exactly. There were multiple implementations over time, 
one with the card created on plug-in, one where the PCM devices became 
available/unavailable based on plug-in, overall all of them introduced 
issues

> It's basically similar as the hotplug like USB, so I thought it would

> work as is.


Such transitions would probably not break userspace but they result from 
an explicit user action. The DSP going off and back is less clear, you 
might want the system to wait before triggering re-routing to different 
outputs, etc.

Again I wasn't trying to push back but rather ask what the side effects 
of this patch might be for userspace.

> 

> 

> Takashi

> 

>>

>>>

>>> Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>

>>> ---

>>>

>>> During discussion regarding card re-binding when components unregister

>>> and register back at https://lkml.org/lkml/2018/7/9/785 it was suggested

>>> that component framework can be added into core to provide this feature.

>>>

>>> With this new changes the card will re-bind once the dependent component

>>> re-registers after unregistering. This works based on the match done

>>> from component name using component framework.

>>>

>>> I have tested this patch with qdsp start-stop usecase for more than 10000

>>> times in loop on Qcom platforms.

>>>

>>> I will send qdsp side cleanup patches once I get some feedback on this patch.

>>>

>>>

>>>    include/sound/soc.h  |  5 ++++

>>>    sound/soc/soc-core.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++----

>>>    2 files changed, 70 insertions(+), 5 deletions(-)

>>>

>>> diff --git a/include/sound/soc.h b/include/sound/soc.h

>>> index 870ba6b64817..b94149d29c0d 100644

>>> --- a/include/sound/soc.h

>>> +++ b/include/sound/soc.h

>>> @@ -17,6 +17,7 @@

>>>    #include <linux/workqueue.h>

>>>    #include <linux/interrupt.h>

>>>    #include <linux/kernel.h>

>>> +#include <linux/component.h>

>>>    #include <linux/regmap.h>

>>>    #include <linux/log2.h>

>>>    #include <sound/core.h>

>>> @@ -1088,6 +1089,10 @@ struct snd_soc_card {

>>>      	struct work_struct deferred_resume_work;

>>>    +	/* component framework related */

>>> +	bool components_added;

>>> +	struct component_match *match;

>>> +

>>>    	/* lists of probed devices belonging to this card */

>>>    	struct list_head component_dev_list;

>>>    diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c

>>> index 6d33634b934b..377ed8e67686 100644

>>> --- a/sound/soc/soc-core.c

>>> +++ b/sound/soc/soc-core.c

>>> @@ -279,11 +279,31 @@ static inline void snd_soc_debugfs_exit(void)

>>>      #endif

>>>    +static int snd_soc_card_comp_compare(struct device *dev, void

>>> *data)

>>> +{

>>> +	struct snd_soc_component *component = NULL;

>>> +	struct snd_soc_component *t;

>>> +

>>> +	lockdep_assert_held(&client_mutex);

>>> +	list_for_each_entry(t, &component_list, list) {

>>> +		if (dev == t->dev) {

>>> +			component = t;

>>> +			break;

>>> +		}

>>> +	}

>>> +

>>> +	if (component && !strcmp(component->name, data))

>>> +		return 1;

>>> +

>>> +	return 0;

>>> +}

>>> +

>>>    static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,

>>>    			      struct snd_soc_component *component)

>>>    {

>>>    	struct snd_soc_rtdcom_list *rtdcom;

>>>    	struct snd_soc_rtdcom_list *new_rtdcom;

>>> +	char *cname;

>>>      	for_each_rtdcom(rtd, rtdcom) {

>>>    		/* already connected */

>>> @@ -300,6 +320,13 @@ static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,

>>>      	list_add_tail(&new_rtdcom->list, &rtd->component_list);

>>>    +	if (!rtd->card->components_added) {

>>> +		cname = devm_kasprintf(rtd->card->dev, GFP_KERNEL,

>>> +				       "%s", component->name);

>>> +		component_match_add(rtd->card->dev, &rtd->card->match,

>>> +				    snd_soc_card_comp_compare, cname);

>>> +	}

>>> +

>>>    	return 0;

>>>    }

>>>    @@ -835,6 +862,28 @@ static bool soc_is_dai_link_bound(struct

>>> snd_soc_card *card,

>>>    	return false;

>>>    }

>>>    +static int snd_soc_card_comp_bind(struct device *dev)

>>> +{

>>> +	struct snd_soc_card *card = dev_get_drvdata(dev);

>>> +

>>> +	if (card->instantiated)

>>> +		return 0;

>>> +

>>> +	return snd_soc_register_card(card);

>>> +}

>>> +

>>> +static void snd_soc_card_comp_unbind(struct device *dev)

>>> +{

>>> +	struct snd_soc_card *card = dev_get_drvdata(dev);

>>> +

>>> +	snd_soc_unregister_card(card);

>>> +}

>>> +

>>> +static const struct component_master_ops snd_soc_card_comp_ops = {

>>> +	.bind = snd_soc_card_comp_bind,

>>> +	.unbind = snd_soc_card_comp_unbind,

>>> +};

>>> +

>>>    static int soc_bind_dai_link(struct snd_soc_card *card,

>>>    	struct snd_soc_dai_link *dai_link)

>>>    {

>>> @@ -2108,6 +2157,12 @@ static int snd_soc_instantiate_card(struct snd_soc_card *card)

>>>      	card->instantiated = 1;

>>>    	snd_soc_dapm_sync(&card->dapm);

>>> +	if (!card->components_added) {

>>> +		component_master_add_with_match(card->dev,

>>> +						&snd_soc_card_comp_ops,

>>> +						card->match);

>>> +		card->components_added = true;

>>> +	}

>>>    	mutex_unlock(&card->mutex);

>>>    	mutex_unlock(&client_mutex);

>>>    @@ -3098,11 +3153,6 @@ static void

>>> snd_soc_component_cleanup(struct snd_soc_component *component)

>>>      static void snd_soc_component_del_unlocked(struct

>>> snd_soc_component *component)

>>>    {

>>> -	struct snd_soc_card *card = component->card;

>>> -

>>> -	if (card)

>>> -		snd_soc_unregister_card(card);

>>> -

>>>    	list_del(&component->list);

>>>    }

>>>    @@ -3169,8 +3219,17 @@ int snd_soc_add_component(struct device

>>> *dev,

>>>      	snd_soc_component_add(component);

>>>    +	ret = component_add(dev, NULL);

>>> +	if (ret < 0) {

>>> +		dev_err(dev, "ASoC: Failed to add Component: %d\n", ret);

>>> +		goto err_comp;

>>> +	}

>>> +

>>>    	return 0;

>>>    +err_comp:

>>> +	soc_remove_component(component);

>>> +	snd_soc_unregister_dais(component);

>>>    err_cleanup:

>>>    	snd_soc_component_cleanup(component);

>>>    err_free:

>>> @@ -3218,6 +3277,7 @@ static int __snd_soc_unregister_component(struct device *dev)

>>>    	mutex_unlock(&client_mutex);

>>>      	if (found) {

>>> +		component_del(dev, NULL);

>>>    		snd_soc_component_cleanup(component);

>>>    	}

>>>    

>>>

>>

>>
Mark Brown July 12, 2018, 3:58 p.m. UTC | #5
On Thu, Jul 12, 2018 at 05:42:30PM +0200, Takashi Iwai wrote:
> Pierre-Louis Bossart wrote:

> > On 7/11/18 3:43 AM, Srinivas Kandagatla wrote:


> > > This patch aims at add achieving dynamic behaviour of audio card when

> > > the dependent components disappear and reappear.


> > Humm, no real comment on this patch proper but rather on how userspace

> > would deal with this dynamic behavior?

> > We had similar opens when we worked on the BYT/CHT HDMI stuff and

> > ended-up with quite a few issues in userspace (PulseAudio mainly)

> > related to dynamic behavior at the kernel level as a result of

> > plug/unplug. Also not sure how an Android HAL would deal with a card

> > disappearing temporarily if DSP resources become unavailable or

> > unresponsive. Any thoughts or guidance you might think of?


> Was the card-level register / unregister really problematic with PA?

> It's basically similar as the hotplug like USB, so I thought it would

> work as is.


Yeah, I'd expect normal desktop stuff to be able to cope with this due
to physically hotpluggable hardware.  The main use case for doing things
like this with module unload is in a development context where hopefully
people can also arrange to use a userspace that can cope or to do
whatever to reset it, any limitations in some userspace shouldn't prevent
people from having the facility available.
Vinod Koul July 12, 2018, 4:43 p.m. UTC | #6
On 12-07-18, 10:26, Pierre-Louis Bossart wrote:
> On 7/11/18 3:43 AM, Srinivas Kandagatla wrote:

> > This patch aims at add achieving dynamic behaviour of audio card when

> > the dependent components disappear and reappear.

> > 

> > With this patch the card is removed if any of the dependent component

> > is removed and card is added back if the dependent component comes back.

> > All this is done using component framework and matching based on

> > component name.

> 

> Humm, no real comment on this patch proper but rather on how userspace would

> deal with this dynamic behavior?


As Takashi pointed out card disappearing and appearing should not be an
issue. On this machine, I typically run a BT headset which disappears
and keep appearing, haven't seen issue :)

> We had similar opens when we worked on the BYT/CHT HDMI stuff and ended-up

> with quite a few issues in userspace (PulseAudio mainly) related to dynamic

> behavior at the kernel level as a result of plug/unplug.


IIRC those had something to do with gfx callback, register, interrupt
racing on BYT HDMI. Ramesh/Jerome might have more details if they
remember now :)

> Also not sure how

> an Android HAL would deal with a card disappearing temporarily if DSP

> resources become unavailable or unresponsive. Any thoughts or guidance you

> might think of?


I have not see userspace HAL code but my guess is that it deals with
card not being there.

Btw this was handled/buried in the driver atm and asking realized that
this is better suited in core, so Srini pushed it up. Current QC stack
expects DSP to go off and card disappear as a result. Once DSP is back
it would appear again (DSP is handled thru remoteproc, I think you
should have a look at it and see if it would suit your DSP needs too)

-- 
~Vinod
diff mbox series

Patch

diff --git a/include/sound/soc.h b/include/sound/soc.h
index 870ba6b64817..b94149d29c0d 100644
--- a/include/sound/soc.h
+++ b/include/sound/soc.h
@@ -17,6 +17,7 @@ 
 #include <linux/workqueue.h>
 #include <linux/interrupt.h>
 #include <linux/kernel.h>
+#include <linux/component.h>
 #include <linux/regmap.h>
 #include <linux/log2.h>
 #include <sound/core.h>
@@ -1088,6 +1089,10 @@  struct snd_soc_card {
 
 	struct work_struct deferred_resume_work;
 
+	/* component framework related */
+	bool components_added;
+	struct component_match *match;
+
 	/* lists of probed devices belonging to this card */
 	struct list_head component_dev_list;
 
diff --git a/sound/soc/soc-core.c b/sound/soc/soc-core.c
index 6d33634b934b..377ed8e67686 100644
--- a/sound/soc/soc-core.c
+++ b/sound/soc/soc-core.c
@@ -279,11 +279,31 @@  static inline void snd_soc_debugfs_exit(void)
 
 #endif
 
+static int snd_soc_card_comp_compare(struct device *dev, void *data)
+{
+	struct snd_soc_component *component = NULL;
+	struct snd_soc_component *t;
+
+	lockdep_assert_held(&client_mutex);
+	list_for_each_entry(t, &component_list, list) {
+		if (dev == t->dev) {
+			component = t;
+			break;
+		}
+	}
+
+	if (component && !strcmp(component->name, data))
+		return 1;
+
+	return 0;
+}
+
 static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
 			      struct snd_soc_component *component)
 {
 	struct snd_soc_rtdcom_list *rtdcom;
 	struct snd_soc_rtdcom_list *new_rtdcom;
+	char *cname;
 
 	for_each_rtdcom(rtd, rtdcom) {
 		/* already connected */
@@ -300,6 +320,13 @@  static int snd_soc_rtdcom_add(struct snd_soc_pcm_runtime *rtd,
 
 	list_add_tail(&new_rtdcom->list, &rtd->component_list);
 
+	if (!rtd->card->components_added) {
+		cname = devm_kasprintf(rtd->card->dev, GFP_KERNEL,
+				       "%s", component->name);
+		component_match_add(rtd->card->dev, &rtd->card->match,
+				    snd_soc_card_comp_compare, cname);
+	}
+
 	return 0;
 }
 
@@ -835,6 +862,28 @@  static bool soc_is_dai_link_bound(struct snd_soc_card *card,
 	return false;
 }
 
+static int snd_soc_card_comp_bind(struct device *dev)
+{
+	struct snd_soc_card *card = dev_get_drvdata(dev);
+
+	if (card->instantiated)
+		return 0;
+
+	return snd_soc_register_card(card);
+}
+
+static void snd_soc_card_comp_unbind(struct device *dev)
+{
+	struct snd_soc_card *card = dev_get_drvdata(dev);
+
+	snd_soc_unregister_card(card);
+}
+
+static const struct component_master_ops snd_soc_card_comp_ops = {
+	.bind = snd_soc_card_comp_bind,
+	.unbind = snd_soc_card_comp_unbind,
+};
+
 static int soc_bind_dai_link(struct snd_soc_card *card,
 	struct snd_soc_dai_link *dai_link)
 {
@@ -2108,6 +2157,12 @@  static int snd_soc_instantiate_card(struct snd_soc_card *card)
 
 	card->instantiated = 1;
 	snd_soc_dapm_sync(&card->dapm);
+	if (!card->components_added) {
+		component_master_add_with_match(card->dev,
+						&snd_soc_card_comp_ops,
+						card->match);
+		card->components_added = true;
+	}
 	mutex_unlock(&card->mutex);
 	mutex_unlock(&client_mutex);
 
@@ -3098,11 +3153,6 @@  static void snd_soc_component_cleanup(struct snd_soc_component *component)
 
 static void snd_soc_component_del_unlocked(struct snd_soc_component *component)
 {
-	struct snd_soc_card *card = component->card;
-
-	if (card)
-		snd_soc_unregister_card(card);
-
 	list_del(&component->list);
 }
 
@@ -3169,8 +3219,17 @@  int snd_soc_add_component(struct device *dev,
 
 	snd_soc_component_add(component);
 
+	ret = component_add(dev, NULL);
+	if (ret < 0) {
+		dev_err(dev, "ASoC: Failed to add Component: %d\n", ret);
+		goto err_comp;
+	}
+
 	return 0;
 
+err_comp:
+	soc_remove_component(component);
+	snd_soc_unregister_dais(component);
 err_cleanup:
 	snd_soc_component_cleanup(component);
 err_free:
@@ -3218,6 +3277,7 @@  static int __snd_soc_unregister_component(struct device *dev)
 	mutex_unlock(&client_mutex);
 
 	if (found) {
+		component_del(dev, NULL);
 		snd_soc_component_cleanup(component);
 	}