mbox series

[0/5] ALSA: control - add generic LED trigger code

Message ID 20210211111400.1131020-1-perex@perex.cz
Headers show
Series ALSA: control - add generic LED trigger code | expand

Message

Jaroslav Kysela Feb. 11, 2021, 11:13 a.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

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        | 407 ++++++++++++++++++++++++++++++++
 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, 638 insertions(+), 263 deletions(-)
 create mode 100644 sound/core/control_led.c

Comments

Takashi Iwai Feb. 12, 2021, 9:23 a.m. UTC | #1
On Thu, 11 Feb 2021 18:53:20 +0100,
Jaroslav Kysela wrote:
> 
> Dne 11. 02. 21 v 18:15 Takashi Iwai napsal(a):
> 
> >> 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
> 
> > One thing I still miss from the picture is how to deal with the case
> > like AMD ACP.  It has no mixer control to bundle with the LED trigger.
> > Your idea is to make a (dummy) user element and tie the LED trigger
> > with it?
> 
> Yes, the user-space code which guarantee the silence stream should create an
> user space control with the appropriate LED group access bits. The alsa-lib's
> softvol PCM plugin can do this silencing for example.

What control would it create?  In the case of softvol, it's a volume
control that really changes the volume.  For the mute LED, it's a
control turn on/off the mute?  If so, I wonder what makes better than
creating it from the kernel driver.  (Of course, we can list up like
"flexibility", etc, but it has a flip side of "complexity" and
"fragility"...)

> > Another slight concern is the possible regression: by moving the
> > mute-LED mode enum stuff into the sysfs, user will get
> > incompatibilities after the kernel update.  And it's not that trivial
> > to change the sysfs entry as default for each user.
> > It needs some detailed documentation or some temporary workaround
> > (e.g. keep providing the controls for now but warns if the value is
> > changed from the default value via the controls).
> 
> I don't think that we have a user space application which is using those
> controls (Pulseaudio or so..) in an abstract way. I think that it's really
> minor issue. We should probably concentrate for the main designed purpose
> (notify about the mute / silent state) and handle those add-on features as an
> experimental stuff.

I'm sure that there are users of the reverse mic-mute LED ("follow
capture" mode); the feature was added because of the explicit request
from my colleague, and this mode works no matter whether ALSA native
or PA is used.  Not sure about "on" and "off" mode; maybe there can be
some users who want to disable the LED.

But, yes, this is a minor issue and should be in a lower priority.
It's just as a reminder.


thanks,

Takashi
Jaroslav Kysela Feb. 12, 2021, 10:32 a.m. UTC | #2
Dne 12. 02. 21 v 10:23 Takashi Iwai napsal(a):
> On Thu, 11 Feb 2021 18:53:20 +0100,
> Jaroslav Kysela wrote:
>>
>> Dne 11. 02. 21 v 18:15 Takashi Iwai napsal(a):
>>
>>>> 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
>>
>>> One thing I still miss from the picture is how to deal with the case
>>> like AMD ACP.  It has no mixer control to bundle with the LED trigger.
>>> Your idea is to make a (dummy) user element and tie the LED trigger
>>> with it?
>>
>> Yes, the user-space code which guarantee the silence stream should create an
>> user space control with the appropriate LED group access bits. The alsa-lib's
>> softvol PCM plugin can do this silencing for example.
> 
> What control would it create?  In the case of softvol, it's a volume
> control that really changes the volume.  For the mute LED, it's a
> control turn on/off the mute?  If so, I wonder what makes better than
> creating it from the kernel driver.  (Of course, we can list up like
> "flexibility", etc, but it has a flip side of "complexity" and
> "fragility"...)

The current code handles both switch / volume for the marked control (assuming
that the minimal value - usually zero - is full mute). And actually, there are
snd_pcm_areas_silence() calls in the softvol plugin, so we know that the PCM
stream is not passed to the application at this point.

Condition:

      if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
          info.type == SNDRV_CTL_ELEM_TYPE_INTEGER)
          ... value.value.integer.value[i] != info.value.integer.min

The softvol plugin may be extended to add the mute switch control, of course.

>>> Another slight concern is the possible regression: by moving the
>>> mute-LED mode enum stuff into the sysfs, user will get
>>> incompatibilities after the kernel update.  And it's not that trivial
>>> to change the sysfs entry as default for each user.
>>> It needs some detailed documentation or some temporary workaround
>>> (e.g. keep providing the controls for now but warns if the value is
>>> changed from the default value via the controls).
>>
>> I don't think that we have a user space application which is using those
>> controls (Pulseaudio or so..) in an abstract way. I think that it's really
>> minor issue. We should probably concentrate for the main designed purpose
>> (notify about the mute / silent state) and handle those add-on features as an
>> experimental stuff.
> 
> I'm sure that there are users of the reverse mic-mute LED ("follow
> capture" mode); the feature was added because of the explicit request
> from my colleague, and this mode works no matter whether ALSA native
> or PA is used.
I see. It looks like a corner case. The proposed sysfs based code is also user
space independent. The issue with the HDA code is that it's card based, but
the system wide LEDs should not be tied to one sound card. We are seeing that
the hardware designers became very creative :-)

						Jaroslav
Jaroslav Kysela Feb. 14, 2021, 6:55 p.m. UTC | #3
Dne 12. 02. 21 v 13:28 Takashi Iwai napsal(a):
> On Fri, 12 Feb 2021 11:32:38 +0100,
> Jaroslav Kysela wrote:
>>
>> Dne 12. 02. 21 v 10:23 Takashi Iwai napsal(a):
>>> On Thu, 11 Feb 2021 18:53:20 +0100,
>>> Jaroslav Kysela wrote:
>>>>
>>>> Dne 11. 02. 21 v 18:15 Takashi Iwai napsal(a):
>>>>
>>>>>> 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
>>>>
>>>>> One thing I still miss from the picture is how to deal with the case
>>>>> like AMD ACP.  It has no mixer control to bundle with the LED trigger.
>>>>> Your idea is to make a (dummy) user element and tie the LED trigger
>>>>> with it?
>>>>
>>>> Yes, the user-space code which guarantee the silence stream should create an
>>>> user space control with the appropriate LED group access bits. The alsa-lib's
>>>> softvol PCM plugin can do this silencing for example.
>>>
>>> What control would it create?  In the case of softvol, it's a volume
>>> control that really changes the volume.  For the mute LED, it's a
>>> control turn on/off the mute?  If so, I wonder what makes better than
>>> creating it from the kernel driver.  (Of course, we can list up like
>>> "flexibility", etc, but it has a flip side of "complexity" and
>>> "fragility"...)
>>
>> The current code handles both switch / volume for the marked control (assuming
>> that the minimal value - usually zero - is full mute). And actually, there are
>> snd_pcm_areas_silence() calls in the softvol plugin, so we know that the PCM
>> stream is not passed to the application at this point.
>>
>> Condition:
>>
>>       if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
>>           info.type == SNDRV_CTL_ELEM_TYPE_INTEGER)
>>           ... value.value.integer.value[i] != info.value.integer.min
>>
>> The softvol plugin may be extended to add the mute switch control, of course.
> 
> Well, my question was what kind of mixer control will be added at all,
> although the chip does neither volume nor mute function.  Would we add
> a fake volume/switch like softvol, or would we add rather a control
> that is directly tied with the LED state?

I don't understand your question. If the user space marks the own vol/sw
control with the LED group, then it's tied with the LED state. I believe that
the control should be created in the code which make sure that the PCM stream
is silenced (like alsa-lib's softvol plugin).

						Jaroslav
Takashi Iwai Feb. 15, 2021, 7:50 a.m. UTC | #4
On Sun, 14 Feb 2021 19:55:21 +0100,
Jaroslav Kysela wrote:
> 
> Dne 12. 02. 21 v 13:28 Takashi Iwai napsal(a):
> > On Fri, 12 Feb 2021 11:32:38 +0100,
> > Jaroslav Kysela wrote:
> >>
> >> Dne 12. 02. 21 v 10:23 Takashi Iwai napsal(a):
> >>> On Thu, 11 Feb 2021 18:53:20 +0100,
> >>> Jaroslav Kysela wrote:
> >>>>
> >>>> Dne 11. 02. 21 v 18:15 Takashi Iwai napsal(a):
> >>>>
> >>>>>> 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
> >>>>
> >>>>> One thing I still miss from the picture is how to deal with the case
> >>>>> like AMD ACP.  It has no mixer control to bundle with the LED trigger.
> >>>>> Your idea is to make a (dummy) user element and tie the LED trigger
> >>>>> with it?
> >>>>
> >>>> Yes, the user-space code which guarantee the silence stream should create an
> >>>> user space control with the appropriate LED group access bits. The alsa-lib's
> >>>> softvol PCM plugin can do this silencing for example.
> >>>
> >>> What control would it create?  In the case of softvol, it's a volume
> >>> control that really changes the volume.  For the mute LED, it's a
> >>> control turn on/off the mute?  If so, I wonder what makes better than
> >>> creating it from the kernel driver.  (Of course, we can list up like
> >>> "flexibility", etc, but it has a flip side of "complexity" and
> >>> "fragility"...)
> >>
> >> The current code handles both switch / volume for the marked control (assuming
> >> that the minimal value - usually zero - is full mute). And actually, there are
> >> snd_pcm_areas_silence() calls in the softvol plugin, so we know that the PCM
> >> stream is not passed to the application at this point.
> >>
> >> Condition:
> >>
> >>       if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
> >>           info.type == SNDRV_CTL_ELEM_TYPE_INTEGER)
> >>           ... value.value.integer.value[i] != info.value.integer.min
> >>
> >> The softvol plugin may be extended to add the mute switch control, of course.
> > 
> > Well, my question was what kind of mixer control will be added at all,
> > although the chip does neither volume nor mute function.  Would we add
> > a fake volume/switch like softvol, or would we add rather a control
> > that is directly tied with the LED state?
> 
> I don't understand your question. If the user space marks the own vol/sw
> control with the LED group, then it's tied with the LED state. I believe that
> the control should be created in the code which make sure that the PCM stream
> is silenced (like alsa-lib's softvol plugin).

The softvol (or similar effect) is to be ignored by PA, as it's not
suitable with the timer-scheduled type of PCM streaming.  So the
control shouldn't have any actual effect of PCM stream itself but
merely for controlling the LED state.  If that's the case, it
shouldn't be named like "XXX Switch" or "XXX Volume", but it's a
control like "Mic Mute LED Status" -- and ironically, that's a kind of
thing we didn't want to take in the kernel side implementation...

That said, I see the value of a generic code for the LED control that
is tied with the existing volume/switch; there is already such an
implementation in HD-audio, and applying to the other drivers makes
sense.  OTOH, for the case for AMD ACP, I'm still not sure what's the
best way.


thanks,

Takashi




Takashi

> 
> 						Jaroslav
> 
> -- 
> Jaroslav Kysela <perex@perex.cz>
> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
>
Hans de Goede Feb. 15, 2021, 12:02 p.m. UTC | #5
Hi,

On 2/12/21 9:31 PM, Hans de Goede wrote:
> On 2/11/21 12:13 PM, Jaroslav Kysela wrote:

<snip>

>> 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
> 
> So I've been running some tests with this,combined with writing
> UCM profiles for hw volume control, for some Intel Bay- and
> CherryTrail devices using Intel's Low Power Engine (LPE) for audio,
> which uses the ASoC framework.
> 
> My work / experiments for this are progressing a bit slower then I
> would like, but that is not the fault of this patch-set, but rather
> an issue with hw-volume control mapping, see below for details.
> 
> Leaving the ASoC implementation details aside, this patch-set
> works quite nicely to get the speaker mute-LED to work.

I've spend some more time this weekend playing with this and I've also
added mic MUTE LED support for the ASoC rt5672 codec driver now using
this.

I will post a RFC patch series with the ASoC rt5672 codec driver LED
support soon, as adding an extra use-case for this will hopefully help
with reviewing this.

FWIW there were some challenges, but those were not related to the
driver API this patch set adds. The driver API works well for ASoC
codec drivers.

Regards,

Hans


p.s.

One open issue is the lockdep issue which I reported in my
previous email.
Jaroslav Kysela Feb. 15, 2021, 5:49 p.m. UTC | #6
Dne 15. 02. 21 v 8:50 Takashi Iwai napsal(a):
> On Sun, 14 Feb 2021 19:55:21 +0100,
> Jaroslav Kysela wrote:
>>
>> Dne 12. 02. 21 v 13:28 Takashi Iwai napsal(a):
>>> On Fri, 12 Feb 2021 11:32:38 +0100,
>>> Jaroslav Kysela wrote:
>>>>
>>>> Dne 12. 02. 21 v 10:23 Takashi Iwai napsal(a):
>>>>> On Thu, 11 Feb 2021 18:53:20 +0100,
>>>>> Jaroslav Kysela wrote:
>>>>>>
>>>>>> Dne 11. 02. 21 v 18:15 Takashi Iwai napsal(a):
>>>>>>
>>>>>>>> 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
>>>>>>
>>>>>>> One thing I still miss from the picture is how to deal with the case
>>>>>>> like AMD ACP.  It has no mixer control to bundle with the LED trigger.
>>>>>>> Your idea is to make a (dummy) user element and tie the LED trigger
>>>>>>> with it?
>>>>>>
>>>>>> Yes, the user-space code which guarantee the silence stream should create an
>>>>>> user space control with the appropriate LED group access bits. The alsa-lib's
>>>>>> softvol PCM plugin can do this silencing for example.
>>>>>
>>>>> What control would it create?  In the case of softvol, it's a volume
>>>>> control that really changes the volume.  For the mute LED, it's a
>>>>> control turn on/off the mute?  If so, I wonder what makes better than
>>>>> creating it from the kernel driver.  (Of course, we can list up like
>>>>> "flexibility", etc, but it has a flip side of "complexity" and
>>>>> "fragility"...)
>>>>
>>>> The current code handles both switch / volume for the marked control (assuming
>>>> that the minimal value - usually zero - is full mute). And actually, there are
>>>> snd_pcm_areas_silence() calls in the softvol plugin, so we know that the PCM
>>>> stream is not passed to the application at this point.
>>>>
>>>> Condition:
>>>>
>>>>       if (info.type == SNDRV_CTL_ELEM_TYPE_BOOLEAN ||
>>>>           info.type == SNDRV_CTL_ELEM_TYPE_INTEGER)
>>>>           ... value.value.integer.value[i] != info.value.integer.min
>>>>
>>>> The softvol plugin may be extended to add the mute switch control, of course.
>>>
>>> Well, my question was what kind of mixer control will be added at all,
>>> although the chip does neither volume nor mute function.  Would we add
>>> a fake volume/switch like softvol, or would we add rather a control
>>> that is directly tied with the LED state?
>>
>> I don't understand your question. If the user space marks the own vol/sw
>> control with the LED group, then it's tied with the LED state. I believe that
>> the control should be created in the code which make sure that the PCM stream
>> is silenced (like alsa-lib's softvol plugin).
> 
> The softvol (or similar effect) is to be ignored by PA, as it's not
> suitable with the timer-scheduled type of PCM streaming.  So the
> control shouldn't have any actual effect of PCM stream itself but
> merely for controlling the LED state.  If that's the case, it
> shouldn't be named like "XXX Switch" or "XXX Volume", but it's a
> control like "Mic Mute LED Status" -- and ironically, that's a kind of
> thing we didn't want to take in the kernel side implementation...

I see your point now. We can force softvol switch (not volume) for this kind
of hardware (AMD ACP) even for PA, so we know, that there's a code which
modifies the PCM stream in the chain.

Regarding time-scheduled I/O - for the capture, we're fine, because we can
silence the samples before they're processed in the user space. We also even
know, that the standard use for the microphone input is real-time, so the
buffering is really short. For playback, yes, the driver queued samples cannot
be altered, but it's usually no big security problem. The latency may be
seconds (or a bit more) in the corner case. If we need a proper solution, the
kernel PCM driver should be improved to take care (software based silence) or
the buffering must be reduced. I don't think that we need to change something
for ACP right now.

Also note that PA does the own stream silencing when the mixer path is muted,
so it's double win here (alsa-lib + PA) and the latency should be identical.

					Jaroslav