All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Laurentiu Mihalcea" <laurentiumihalcea111@gmail.com>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support
Date: Fri, 27 Nov 2026 17:36:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-237999@kernel.org> (raw)
In-Reply-To: <20261127170039.663-3-laurentiumihalcea111@gmail.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] ASoC: wm8962: build failure when CONFIG_GPIOLIB or
  GPIOLIB_IRQCHIP is disabled
- [High] ASoC: wm8962: setting gpio_chip.init_valid_mask disables GPIO2
  and GPIO3
- [High] ASoC: wm8962: lifecycle mismatch and UAF on gc->irq.domain in
  wm8962_irq()
- [High] ASoC: wm8962: lost GPIO IRQs due to runtime suspend and reset
  in wm8962_irq()
- [Medium] ASoC: wm8962: bitwise AND with WM8962_GPIO_FN_IRQ in
  wm8962_init_gpio_irq()
- [Medium] ASoC: wm8962: missing direction_input/get_direction breaks
  direct GPIO IRQ use
--

commit 836469865b058b5ef16dbf8d43d225333a1a1717
Author: Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>

ASoC: wm8962: add GPIO interrupt controller support

This commit registers a GPIO irqchip on the WM8962 codec so that GPIO5 and
GPIO6 can be used as interrupt sources by consumer devices.

> diff --git a/sound/soc/codecs/wm8962.c b/sound/soc/codecs/wm8962.c
> index 7122d93a742c5..3bbaacb08aafa 100644
> --- a/sound/soc/codecs/wm8962.c
> +++ b/sound/soc/codecs/wm8962.c
> @@ -3141,13 +3141,34 @@ static void wm8962_mic_work(struct work_struct *work)
>  			    WM8962_MICD_IRQ_POL, irq_pol);
>  }
>  
> +static int wm8962_irq_status_masked(struct wm8962_priv *wm8962,
> +				    unsigned int status_reg,
> +				    unsigned int mask_reg,
> +				    unsigned int *status)
> +{
> +	int ret;
> +	unsigned int mask;
> +
> +	ret = regmap_read(wm8962->regmap, mask_reg, &mask);
> +	if (ret != 0)
> +		return ret;
> +
> +	ret = regmap_read(wm8962->regmap, status_reg, status);
> +	if (ret != 0)
> +		return ret;
> +
> +	*status &= ~mask;
> +
> +	return 0;
> +}
> +
>  static irqreturn_t wm8962_irq(int irq, void *data)
>  {
>  	struct device *dev = data;
>  	struct wm8962_priv *wm8962 = dev_get_drvdata(dev);
> -	unsigned int mask;
> -	unsigned int active;
> -	int reg, ret;
> +	unsigned int active_1, active_2;
> +	struct gpio_chip *gc = &wm8962->gpio_chip;
> +	int reg, ret, nirq;

[Severity: High]
Does this fail to build when CONFIG_GPIOLIB or CONFIG_GPIOLIB_IRQCHIP is
disabled?

SND_SOC_WM8962 neither depends on GPIOLIB nor selects GPIOLIB_IRQCHIP.
wm8962_priv.gpio_chip is only declared under #ifdef CONFIG_GPIOLIB, and
gpio_chip.irq as well as GPIOCHIP_IRQ_RESOURCE_HELPERS are only available
under CONFIG_GPIOLIB_IRQCHIP, so referencing wm8962->gpio_chip,
gc->irq.domain, and wm8962_irq_chip breaks builds without those options.

>  
>  	ret = pm_runtime_resume_and_get(dev);
>  	if (ret < 0) {
> @@ -3155,43 +3176,51 @@ static irqreturn_t wm8962_irq(int irq, void *data)
>  		return IRQ_NONE;
>  	}
>  
> -	ret = regmap_read(wm8962->regmap, WM8962_INTERRUPT_STATUS_2_MASK,
> -			  &mask);
> +	ret = wm8962_irq_status_masked(wm8962,
> +				       WM8962_INTERRUPT_STATUS_1,
> +				       WM8962_INTERRUPT_STATUS_1_MASK,
> +				       &active_1);

[Severity: High]
Are GPIO5 and GPIO6 interrupts lost whenever the codec is runtime
suspended?

Nothing holds a runtime PM reference on the codec while a GPIO IRQ is
requested (for example via irq_domain_set_pm_device()).  When the codec is
idle in RPM_SUSPENDED and a GPIO interrupt fires, pm_runtime_resume_and_get()
runs wm8962_runtime_resume() -> wm8962_reset(), which resets the hardware and
clears WM8962_INTERRUPT_STATUS_1 before wm8962_irq_status_masked() reads it.
In addition, commit "ASoC: wm8962: fix interrupt mis-firing during
runtime_resume()" later in this series disables wm8962->irq in
wm8962_runtime_suspend(), preventing GPIO interrupts from firing at all while
suspended.

[ ... ]

> @@ -3226,6 +3255,22 @@ static irqreturn_t wm8962_irq(int irq, void *data)
>  				   msecs_to_jiffies(250));
>  	}
>  
> +	if (active_1 & WM8962_GP5_EINT) {
> +		nirq = irq_find_mapping(gc->irq.domain, 4);
> +		if (likely(nirq > 0))
> +			handle_nested_irq(nirq);
> +		else
> +			dev_warn(dev, "Unmapped GP5 interrupt\n");
> +	}
> +
> +	if (active_1 & WM8962_GP6_EINT) {
> +		nirq = irq_find_mapping(gc->irq.domain, 5);
> +		if (likely(nirq > 0))
> +			handle_nested_irq(nirq);
> +		else
> +			dev_warn(dev, "Unmapped GP6 interrupt\n");
> +	}

[Severity: High]
Can irq_find_mapping() dereference a freed gc->irq.domain or pass NULL
before the ASoC component is bound?

wm8962_irq() is requested in wm8962_i2c_probe() and wm8962_init_gpio_irq()
unmasks GP5 and GP6 interrupts immediately, whereas gpio_chip is added in
wm8962_probe() and removed in wm8962_remove() -> wm8962_free_gpio().
Before wm8962_probe() runs, gc->irq.domain is NULL, which causes
__irq_resolve_mapping() to fall back to irq_default_domain.  When the sound
card unbinds, gpiochip_remove() frees gc->irq.domain while GP5/GP6
interrupts remain unmasked and wm8962_irq() is still active, causing a
use-after-free on subsequent GP5/GP6 interrupts.

[ ... ]

> @@ -3555,6 +3600,18 @@ static int wm8962_gpio_get(struct gpio_chip *chip, unsigned int offset)
>  	return (val & WM8962_GP5_LVL) >> WM8962_GP5_LVL_SHIFT;
>  }
>  
> +static int wm8962_init_valid_mask(struct gpio_chip *chip,
> +				  unsigned long *valid_mask,
> +				  unsigned int ngpios)
> +{
> +	bitmap_zero(valid_mask, ngpios);
> +
> +	/* only GPIO5/GPIO6 can be used as interrupt sources! */
> +	bitmap_set(valid_mask, 4, 2);
> +
> +	return 0;
> +}
> +
>  static const struct gpio_chip wm8962_template_chip = {
>  	.label			= "wm8962",
>  	.owner			= THIS_MODULE,
> @@ -3562,13 +3619,42 @@ static const struct gpio_chip wm8962_template_chip = {
>  	.direction_output	= wm8962_gpio_direction_out,
>  	.set			= wm8962_gpio_set,
>  	.get			= wm8962_gpio_get,
> +	.init_valid_mask	= wm8962_init_valid_mask,
>  	.can_sleep		= 1,
>  };

[Severity: High]
Does setting wm8962_init_valid_mask on gpio_chip.init_valid_mask disable
GPIO2 and GPIO3 as output GPIOs?

gpio_chip.init_valid_mask restricts which GPIO lines exist for all gpiolib
operations, so clearing everything except offsets 4 and 5 causes
gpiod_request_commit() to reject GPIO2 (offset 1) and GPIO3 (offset 2) with
-EINVAL.  To restrict only which lines can be mapped as interrupts, this
callback should be assigned to girq->init_valid_mask instead.

>  
> +/*
> + * TODO: implement mask()/unmask()/bus_sync() if required. At the moment, we
> + * operate under the assumption that the pins' function is set to IRQ using
> + * the "gpio-cfg" DT property. The interrupts are unmasked regardless of the
> + * pins' function!
> + */
> +static const struct irq_chip wm8962_irq_chip = {
> +	.flags			= IRQCHIP_IMMUTABLE,
> +	GPIOCHIP_IRQ_RESOURCE_HELPERS,
> +};

[Severity: Medium]
Can gpiochip_lock_as_irq() succeed when wm8962_template_chip defines
neither .direction_input nor .get_direction?

During gpiochip_add_data_with_key(), gpiolib sets GPIOD_FLAG_IS_OUT on every
line when both .get_direction and .direction_input are NULL.  When a consumer
requests a GPIO5 or GPIO6 interrupt, GPIOCHIP_IRQ_RESOURCE_HELPERS calls
gpiochip_lock_as_irq(), which sees GPIOD_FLAG_IS_OUT set and rejects the
interrupt request with -EIO.

> +
> +static void wm8962_init_gpio_irq(struct wm8962_priv *wm8962)
> +{
> +	unsigned int val = ~(WM8962_IM_GP5_EINT | WM8962_IM_GP6_EINT);
> +	unsigned int mask = WM8962_IM_GP5_EINT | WM8962_IM_GP6_EINT;
> +
> +	if (!(wm8962->pdata.gpio_init[4] & WM8962_GPIO_FN_IRQ))
> +		val |= WM8962_IM_GP5_EINT;
> +
> +	if (!(wm8962->pdata.gpio_init[5] & WM8962_GPIO_FN_IRQ))
> +		val |= WM8962_IM_GP6_EINT;

[Severity: Medium]
Should these checks mask with WM8962_GP5_FN_MASK / WM8962_GP6_FN_MASK and
compare equal to WM8962_GPIO_FN_IRQ instead of using a bitwise AND?

GP5_FN and GP6_FN are 5-bit function codes where WM8962_GPIO_FN_IRQ is 3,
so testing with & WM8962_GPIO_FN_IRQ matches any non-IRQ function with bit
0 or bit 1 set (such as WM8962_GPIO_FN_LOGIC or WM8962_GPIO_FN_MICD) and
unmasks GP5/GP6 interrupts when the pins are not configured for IRQ output.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127170039.663-1-laurentiumihalcea111@gmail.com?part=2

  reply	other threads:[~2026-11-27 17:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 17:00 [PATCH 0/3] ASoC: wm8962: make GPIO-based HP detection work Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 1/3] ASoC: wm8962: implement GPIO .get callback Laurentiu Mihalcea
2026-11-27 17:22   ` sashiko-bot
2026-11-30 11:18   ` Charles Keepax
2026-12-01 21:36     ` Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 2/3] ASoC: wm8962: add GPIO interrupt controller support Laurentiu Mihalcea
2026-11-27 17:36   ` sashiko-bot [this message]
2026-11-30 11:25   ` Charles Keepax
2026-12-01 22:35     ` Laurentiu Mihalcea
2026-11-27 17:00 ` [PATCH 3/3] ASoC: wm8962: fix interrupt mis-firing during runtime_resume() Laurentiu Mihalcea
2026-11-27 17:30   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-237999@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=laurentiumihalcea111@gmail.com \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.