Linux Input/HID development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Griffin Kroah-Hartman" <griffin.kroah@fairphone.com>
Cc: linux-input@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org, dmitry.torokhov@gmail.com
Subject: Re: [PATCH v4 2/4] Input - aw8695: Add driver for AW8695 haptics
Date: Thu, 19 Nov 2026 14:10:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-223265@kernel.org> (raw)
In-Reply-To: <20261119-aw8695_haptic_fp4_driver-v4-2-9d107334bdb2@fairphone.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] Input: aw8695: stale haptics->level after aw8695_close() and
  suspend
- [Medium] Input: aw8695: premature enable_work() and unchecked errors
  in PM callbacks
- [Medium] Input: aw8695: uncanceled play_work on unbind causes
  use-after-free
- [Low] Input: aw8695: unlocked double-read of haptics->level in
  aw8695_play_sine()
- [Low] Input: aw8695: redundant standby call in aw8695_stop() and style
  issues
--

commit 7f7a93d83cad192aaa964a8d15cf7b36fce9d8d3
Author: Luca Weiss <luca.weiss@fairphone.com>

Input - aw8695: Add driver for AW8695 haptics

This commit adds an input force-feedback driver for the I2C-connected
Awinic AW8695 LRA haptic controller, supporting sine wave playback in RAM
mode.

> diff --git a/drivers/input/misc/aw8695.c b/drivers/input/misc/aw8695.c
> new file mode 100644
> index 0000000000000..e83bdf029def8
> --- /dev/null
> +++ b/drivers/input/misc/aw8695.c
[ ... ]
> +#define AW8695_R_SPARE_REG			0x5d
> +#define Aw8695_R_SPARE_DEFAULT			0x68
[ ... ]
> +static int aw8695_play_mode(struct aw8695_data *haptics,
> +				 enum aw8695_work_mode mode)
> +{
> +	struct device *dev = &haptics->client->dev;
> +	int err;
> +
> +	switch (mode) {
> +	case AW8695_STANDBY_MODE:
> +		err = regmap_update_bits(haptics->regmap, AW8695_SYSINTM_REG,
> +					 AW8695_SYSINTM_UVLO_OFF, AW8695_SYSINTM_UVLO_OFF);
> +		if (err)
> +			return err;
> +
> +		return regmap_update_bits(haptics->regmap, AW8695_SYSCTRL_REG,
> +					  AW8695_SYSCTRL_WORK_MODE_MASK,
> +					  FIELD_PREP(AW8695_SYSCTRL_WORK_MODE_MASK,
> +						     AW8695_SYSCTRL_STANDBY));
> +	case AW8695_RAM_MODE:
> +		err = regmap_update_bits(haptics->regmap, AW8695_SYSCTRL_REG,
> +					 AW8695_SYSCTRL_PLAY_MODE_MASK,
> +					 FIELD_PREP(AW8695_SYSCTRL_PLAY_MODE_MASK,
> +						    AW8695_SYSCTRL_PLAY_MODE_RAM));
> +		if (err)
> +			return err;
> +
> +		err = regmap_update_bits(haptics->regmap, AW8695_SYSCTRL_REG,
> +					 AW8695_SYSCTRL_BST_MODE_MASK,
> +					 FIELD_PREP(AW8695_SYSCTRL_BST_MODE_MASK,
> +						    AW8695_SYSCTRL_BST_MODE_BYPASS));
> +		if (err)
> +			return err;
> +
> +		return aw8695_haptic_set_active(haptics);
> +	default:
> +		dev_err(dev, "Unhandled mode: %d\n", mode);
> +		return -EINVAL;
> +	}
> +}
> +
> +static int aw8695_haptics_play(struct input_dev *dev, void *data,
> +			       struct ff_effect *effect)
> +{
> +	struct aw8695_data *haptics = input_get_drvdata(dev);
> +	int level;
> +
> +	level = effect->u.rumble.strong_magnitude;
> +	if (!level)
> +		level = effect->u.rumble.weak_magnitude;
> +
> +	if (haptics->level == level)
> +		return 0;
> +
> +	haptics->level = level;
> +	schedule_work(&haptics->play_work);
> +
> +	return 0;
> +}
[ ... ]
> +static int aw8695_stop(struct aw8695_data *haptics)
> +{
> +	int err;
> +	unsigned int read_buf;
> +	struct device *dev = &haptics->client->dev;
> +
> +	err = regmap_update_bits(haptics->regmap, AW8695_GO_REG,
> +				 AW8695_GO_ENABLE, 0);
> +	if (err)
> +		return err;
> +
> +	err = regmap_read_poll_timeout(haptics->regmap, AW8695_GLB_STATE_REG, read_buf,
> +			(read_buf & 0x0f) == 0, 2000, 2000 * 100);
> +	if (err) {
> +		dev_err(dev, "Did not enter standby: %d\n	Trying to force it...\n", err);
> +		err = aw8695_play_mode(haptics, AW8695_STANDBY_MODE);
> +		return err;
> +	}
> +
> +	err = aw8695_play_mode(haptics, AW8695_STANDBY_MODE);
> +	if (err)
> +		return err;
> +
> +	return 0;
> +}

[Severity: Low]
This isn't a bug, but in aw8695_stop(), the error branch after
regmap_read_poll_timeout() calls aw8695_play_mode(haptics,
AW8695_STANDBY_MODE) and returns err, duplicating the code right after the
if block.

Also, aw8695_play_mode() has multiple failure points and returns the
results of regmap_update_bits() and aw8695_haptic_set_active() directly
instead of checking err and returning 0 on the success path, and
Aw8695_R_SPARE_DEFAULT has a lowercase 'w'.

> +
> +static int aw8695_play_sine(struct aw8695_data *haptics)
> +{
> +	int err;
> +
> +	err = aw8695_stop(haptics);
> +	if (err)
> +		return err;
[ ... ]
> +	err = regmap_write(haptics->regmap, AW8695_DATDBG, haptics->level * 0x80 / 0xffff);
> +	if (err)
> +		return err;
> +
> +	/* Configure for RAM mode */
> +	err = aw8695_play_mode(haptics, AW8695_RAM_MODE);
> +	if (err)
> +		return err;
> +
> +	/* Start vibration */
> +	err = regmap_update_bits(haptics->regmap, AW8695_GO_REG,
> +				 AW8695_GO_ENABLE, AW8695_GO_ENABLE);
> +	if (err)
> +		return err;
> +
> +	return 0;
> +}
> +
> +static void aw8695_close(struct input_dev *input)
> +{
> +	struct aw8695_data *haptics = input_get_drvdata(input);
> +	struct device *dev = &haptics->client->dev;
> +	int err;
> +
> +	cancel_work_sync(&haptics->play_work);
> +	err = aw8695_stop(haptics);
> +	if (err)
> +		dev_err(dev, "Failed to stop haptics: %d\n", err);
> +}

[Severity: Medium]
Can haptics->level remain non-zero after the hardware is stopped in
aw8695_close() or during system suspend?

aw8695_close() stops the actuator without resetting haptics->level to 0.
If the device is closed while haptics->level is non-zero (for example,
when inhibited via sysfs, or when cancel_work_sync() cancels a pending
start request), the chip enters standby while haptics->level keeps its old
magnitude.  Similarly, the ff-memless timer can still fire while suspended
and call aw8695_haptics_play(), which updates haptics->level even though
play_work is disabled.  In either case, a later request at the same
magnitude hits the haptics->level == level check in aw8695_haptics_play()
and returns early without vibrating.

> +
> +static void aw8695_haptics_play_work(struct work_struct *work)
> +{
> +	struct aw8695_data *haptics =
> +		container_of(work, struct aw8695_data, play_work);
> +	struct device *dev = &haptics->client->dev;
> +	int err;
> +
> +	if (haptics->suspend)
> +		return;
> +
> +	if (haptics->level)
> +		err = aw8695_play_sine(haptics);
> +	else
> +		err = aw8695_stop(haptics);
> +
> +	if (err)
> +		dev_err(dev, "Failed to execute work command: %d\n", err);
> +}

[Severity: Low]
What happens if aw8695_haptics_play() sets haptics->level to 0 while
aw8695_play_sine() is sleeping in aw8695_stop()?

aw8695_haptics_play_work() checks haptics->level without taking a local
snapshot, and aw8695_play_sine() reads haptics->level again when writing
AW8695_DATDBG.  If haptics->level becomes 0 in between, aw8695_play_sine()
writes a zero gain to AW8695_DATDBG and still enables AW8695_RAM_MODE and
AW8695_GO_ENABLE.

[ ... ]
> +static int aw8695_probe(struct i2c_client *client)
> +{
[ ... ]
> +	haptics->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
> +	if (IS_ERR(haptics->reset_gpio))
> +		return dev_err_probe(dev, PTR_ERR(haptics->reset_gpio),
> +				     "Failed to get reset gpio\n");
> +
> +	INIT_WORK(&haptics->play_work, aw8695_haptics_play_work);

[Severity: Medium]
Can haptics->play_work remain pending when the driver is unbound, causing
a use-after-free in aw8695_haptics_play_work()?

haptics->play_work is initialized with INIT_WORK() rather than
devm_work_autocancel(), and the driver has no remove callback, so only
aw8695_close() cancels the work.  If an open device with an active effect
is inhibited via sysfs, input_inhibit_device() calls aw8695_close(), which
leaves haptics->level non-zero.  Unbinding the driver while inhibited runs
the following sequence:

CPU0                                  kworker
__input_unregister_device()
  ...
    input_ff_flush()
      ...
        aw8695_haptics_play()
          haptics->level = 0
          schedule_work(&play_work)
    input_close_device()
      /* skipped: dev->inhibited */
devres_release_all()
  kfree(haptics)
                                      aw8695_haptics_play_work()
                                        aw8695_stop(haptics)
                                          <-- use-after-free

Using devm_work_autocancel() would ensure play_work is canceled on unbind.

[ ... ]
> +static int aw8695_suspend(struct device *dev)
> +{
> +	struct aw8695_data *haptics = dev_get_drvdata(dev);
> +
> +	if (haptics->suspend)
> +		return 0;
> +
> +	disable_work_sync(&haptics->play_work);
> +	aw8695_stop(haptics);
> +	haptics->suspend = true;
> +	haptics->level = 0;
> +	return 0;
> +}
> +
> +static int aw8695_resume(struct device *dev)
> +{
> +	struct aw8695_data *haptics = dev_get_drvdata(dev);
> +
> +	if (!haptics->suspend)
> +		return 0;
> +
> +	enable_work(&haptics->play_work);
> +	aw8695_haptic_set_active(haptics);
> +	aw8695_play_mode(haptics, AW8695_STANDBY_MODE);
> +	haptics->suspend = false;
> +	return 0;
> +}

[Severity: Medium]
Does calling enable_work() at the start of aw8695_resume() allow
aw8695_haptics_play_work() to run while haptics->suspend is still true?

CPU0                                  kworker
aw8695_resume()
  enable_work(&haptics->play_work)
  aw8695_haptic_set_active()
                                      aw8695_haptics_play_work()
                                        if (haptics->suspend)
                                          return;  <-- playback dropped
  aw8695_play_mode(..., STANDBY_MODE)
  haptics->suspend = false

Because aw8695_haptic_set_active() and aw8695_play_mode() perform sleeping
I2C transfers before haptics->suspend is cleared, any work scheduled during
resume exits early without programming the hardware and leaves
haptics->level out of sync.

Also, aw8695_resume() wakes the chip with aw8695_haptic_set_active() only
to immediately put it back into AW8695_STANDBY_MODE, and both
aw8695_suspend() and aw8695_resume() ignore the error returns from these
I2C helpers.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-aw8695_haptic_fp4_driver-v4-0-9d107334bdb2@fairphone.com?part=2

  reply	other threads:[~2026-11-19 14:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 13:54 [PATCH v4 0/4] AW8695 haptic driver Griffin Kroah-Hartman
2026-11-19 13:54 ` [PATCH v4 1/4] dt-bindings: input: Add compatibility for Awinic AW8695 Griffin Kroah-Hartman
2026-11-19 13:54 ` [PATCH v4 2/4] Input - aw8695: Add driver for AW8695 haptics Griffin Kroah-Hartman
2026-11-19 14:10   ` sashiko-bot [this message]
2026-11-19 13:54 ` [PATCH v4 3/4] arm64: dts: qcom: sm7225-fairphone-fp4: Add " Griffin Kroah-Hartman
2026-11-19 14:05   ` sashiko-bot
2026-11-19 13:54 ` [PATCH v4 4/4] MAINTAINERS: add entry for AWINIC haptic driver family Griffin Kroah-Hartman

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-223265@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dmitry.torokhov@gmail.com \
    --cc=griffin.kroah@fairphone.com \
    --cc=linux-input@vger.kernel.org \
    --cc=robh@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox