mbox series

[v2,0/5] ALSA: control - add generic LED API

Message ID 20210215172418.1322825-1-perex@perex.cz
Headers show
Series ALSA: control - add generic LED API | expand

Message

Jaroslav Kysela Feb. 15, 2021, 5:24 p.m. UTC
This patchset tries to resolve the diversity in the audio LED
control among the ALSA drivers. A new control layer registration
is introduced which allows to run additional operations on
top of the elementary ALSA sound controls.

A new control access group (three bits in the access flags)
was introduced to carry the LED group information for
the sound controls. The low-level sound drivers can just
mark those controls using this access group. This information
is exported to the user space and eventually the user space
can create sound controls which can belong to a LED group.

The actual state ('route') evaluation is really easy
(the minimal value check for all channels / controls / cards).
If there's more complicated logic for a given hardware,
the card driver may eventually export a new read-only
sound control for the LED group and do the logic itself.

The new LED trigger control code is completely separated
and possibly optional (there's no symbol dependency).
The full code separation allows eventually to move this
LED trigger control to the user space in future.
Actually it replaces the already present functionality
in the kernel space (HDA drivers) and allows a quick adoption
for the recent hardware (SoundWire / ASoC codecs).

# lsmod | grep snd_ctl_led
snd_ctl_led            16384  0

The sound driver implementation is really easy:

1) call snd_ctl_led_request() when control LED layer should be
   automatically activated
   / it calls module_request("snd-ctl-led") on demand /
2) mark all related kcontrols with
	SNDRV_CTL_ELEM_ACCESS_SPK_LED or
	SNDRV_CTL_ELEM_ACCESS_MIC_LED

v2 changes:
  - fix the locking - remove the controls_rwsem read lock
    in the element get (the consistency is already protected
    with the global snd_ctl_led_mutex and possible partial
    value writes are catched with the next value change
    notification callback)
  - rename state to brightness and show the brightness
    unsigned integer value instead the text on/off string
    (sync with the LED core routines)
  - remove snd_ctl_led_hello() function (CI warning)
  - make snd_ctl_led_get_by_access() function static (CI warning)
  - move snd_ctl_layer_rwsem lock before the registraction
    callback call in snd_ctl_register_layer() - optimization
v1:
  - https://lore.kernel.org/alsa-devel/20210211111400.1131020-1-perex@perex.cz/
Original RFC:
  - https://lore.kernel.org/alsa-devel/20210207201157.869972-1-perex@perex.cz/

Cc: Hans de Goede <hdegoede@redhat.com>
Cc: Perry Yuan <Perry.Yuan@dell.com>

Jaroslav Kysela (5):
  ALSA: control - introduce snd_ctl_notify_one() helper
  ALSA: control - add layer registration routines
  ALSA: control - add generic LED trigger module as the new control
    layer
  ALSA: HDA - remove the custom implementation for the audio LED trigger
  ALSA: control - add sysfs support to the LED trigger module

 include/sound/control.h         |  27 ++-
 include/uapi/sound/asound.h     |   6 +-
 sound/core/Kconfig              |   6 +
 sound/core/Makefile             |   2 +
 sound/core/control.c            | 173 ++++++++++++---
 sound/core/control_led.c        | 378 ++++++++++++++++++++++++++++++++
 sound/pci/hda/Kconfig           |   4 +-
 sound/pci/hda/hda_codec.c       |  69 +-----
 sound/pci/hda/hda_generic.c     | 162 +++-----------
 sound/pci/hda/hda_generic.h     |  15 +-
 sound/pci/hda/hda_local.h       |  16 +-
 sound/pci/hda/patch_ca0132.c    |   4 +-
 sound/pci/hda/patch_realtek.c   |   2 +-
 sound/pci/hda/patch_sigmatel.c  |   6 +-
 sound/pci/hda/thinkpad_helper.c |   2 +-
 15 files changed, 609 insertions(+), 263 deletions(-)
 create mode 100644 sound/core/control_led.c

Comments

Hans de Goede Feb. 21, 2021, 7:32 p.m. UTC | #1
Hi,

On 2/21/21 7:01 PM, Jaroslav Kysela wrote:
> Dne 21. 02. 21 v 14:14 Hans de Goede napsal(a):
> 
>>> v2 changes:
>>>   - fix the locking - remove the controls_rwsem read lock
>>>     in the element get (the consistency is already protected
>>>     with the global snd_ctl_led_mutex and possible partial
>>>     value writes are catched with the next value change
>>>     notification callback)
>>
>> I'm afraid that lockdep still is unhappy. With v2 there is a new
>> (different) lockdep warning.
> 
>> If you can send me another fixup-diff then I'll make sure to
>> test this before you do a v3, so that we can be sure that
>> all cases which my setup catches are resolved before sending
>> out v3.
> 
> Thank you for your test. This change (on top of v2) should resolve this
> remaining lockdep:

I can confirm that I'm not seeing any more lockdeps warning after
applying this on top of v2 of the series.

Regards,

Hans



> diff --git a/sound/core/control.c b/sound/core/control.c
> index c9f062fada0a..494f0154e8be 100644
> --- a/sound/core/control.c
> +++ b/sound/core/control.c
> @@ -2051,7 +2051,9 @@ void snd_ctl_register_layer(struct snd_ctl_layer_ops *lops)
>  	for (card_number = 0; card_number < SNDRV_CARDS; card_number++) {
>  		card = snd_card_ref(card_number);
>  		if (card) {
> +			down_read(&card->controls_rwsem);
>  			lops->lregister(card);
> +			up_read(&card->controls_rwsem);
>  			snd_card_unref(card);
>  		}
>  	}
> @@ -2113,10 +2115,12 @@ static int snd_ctl_dev_register(struct snd_device *device)
>  				  &snd_ctl_f_ops, card, &card->ctl_dev);
>  	if (err < 0)
>  		return err;
> +	down_read(&card->controls_rwsem);
>  	down_read(&snd_ctl_layer_rwsem);
>  	for (lops = snd_ctl_layer; lops; lops = lops->next)
>  		lops->lregister(card);
>  	up_read(&snd_ctl_layer_rwsem);
> +	up_read(&card->controls_rwsem);
>  	return 0;
>  }
> 
> @@ -2137,10 +2141,12 @@ static int snd_ctl_dev_disconnect(struct snd_device
> *device)
>  	}
>  	read_unlock_irqrestore(&card->ctl_files_rwlock, flags);
> 
> +	down_read(&card->controls_rwsem);
>  	down_read(&snd_ctl_layer_rwsem);
>  	for (lops = snd_ctl_layer; lops; lops = lops->next)
>  		lops->ldisconnect(card);
>  	up_read(&snd_ctl_layer_rwsem);
> +	up_read(&card->controls_rwsem);
> 
>  	return snd_unregister_device(&card->ctl_dev);
>  }
> diff --git a/sound/core/control_led.c b/sound/core/control_led.c
> index cafe4c82ca35..b8bb8fd46686 100644
> --- a/sound/core/control_led.c
> +++ b/sound/core/control_led.c
> @@ -235,11 +235,10 @@ static void snd_ctl_led_register(struct snd_card *card)
>  	mutex_lock(&snd_ctl_led_mutex);
>  	snd_ctl_led_card_valid[card->number] = true;
>  	mutex_unlock(&snd_ctl_led_mutex);
> -	down_read(&card->controls_rwsem);
> +	/* the register callback is already called with held card->controls_rwsem */
>  	list_for_each_entry(kctl, &card->controls, list)
>  		for (ioff = 0; ioff < kctl->count; ioff++)
>  			snd_ctl_led_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, kctl, ioff);
> -	up_read(&card->controls_rwsem);
>  	snd_ctl_led_refresh();
>  }
> 
> 
> 					Jaroslav
>