[PATCH] HID: mcp2221: Lock GPIO request preparation

From: Haowen Tu

Date: Thu Nov 12 2026 - 22:30:45 EST


GPIO requests share txbuf and gp_idx with other GPIO operations and with
I2C/SMBus transfers. However, several GPIO paths modify these fields
before acquiring mcp->lock. A GPIO callback waiting for an ongoing
transfer can therefore overwrite its command buffer or change the index
used to interpret the response, despite the transfer holding the mutex.

Acquire the mutex before preparing GPIO get, set, direction and SRAM
requests, and hold it through the response wait. Also snapshot the GPIO
direction before releasing the mutex so another request cannot replace
the result before it is returned to the caller.

Fixes: 328de1c519c5 ("HID: mcp2221: add GPIO functionality support")
Signed-off-by: Haowen Tu <tuhaowen@xxxxxxxxxxxxx>
---
drivers/hid/hid-mcp2221.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/drivers/hid/hid-mcp2221.c b/drivers/hid/hid-mcp2221.c
index 3f05d6e288f9..408c38828cc9 100644
--- a/drivers/hid/hid-mcp2221.c
+++ b/drivers/hid/hid-mcp2221.c
@@ -643,10 +643,11 @@ static int mcp_gpio_read_sram(struct mcp2221 *mcp)
{
int ret;

+ mutex_lock(&mcp->lock);
+
memset(mcp->txbuf, 0, 64);
mcp->txbuf[0] = MCP2221_GET_SRAM_SETTINGS;

- mutex_lock(&mcp->lock);
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 64);
mutex_unlock(&mcp->lock);

@@ -682,6 +683,8 @@ static int mcp2221_check_gpio_pinfunc(struct mcp2221 *mcp)
if (!needgpiofix)
return 0;

+ mutex_lock(&mcp->lock);
+
/*
* Set all bytes to 0, so Bit 7 is not set. The chip
* only changes content of a register when bit 7 is set.
@@ -706,7 +709,6 @@ static int mcp2221_check_gpio_pinfunc(struct mcp2221 *mcp)
}
}

- mutex_lock(&mcp->lock);
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 64);
mutex_unlock(&mcp->lock);

@@ -719,11 +721,12 @@ static int mcp_gpio_get(struct gpio_chip *gc,
int ret;
struct mcp2221 *mcp = gpiochip_get_data(gc);

+ mutex_lock(&mcp->lock);
+
mcp->txbuf[0] = MCP2221_GPIO_GET;

mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);

- mutex_lock(&mcp->lock);
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
mutex_unlock(&mcp->lock);

@@ -735,6 +738,8 @@ static int mcp_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
struct mcp2221 *mcp = gpiochip_get_data(gc);
int ret;

+ mutex_lock(&mcp->lock);
+
memset(mcp->txbuf, 0, 18);
mcp->txbuf[0] = MCP2221_GPIO_SET;

@@ -743,7 +748,6 @@ static int mcp_gpio_set(struct gpio_chip *gc, unsigned int offset, int value)
mcp->txbuf[mcp->gp_idx - 1] = 1;
mcp->txbuf[mcp->gp_idx] = !!value;

- mutex_lock(&mcp->lock);
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 18);
mutex_unlock(&mcp->lock);

@@ -801,19 +805,22 @@ static int mcp_gpio_get_direction(struct gpio_chip *gc,
{
int ret;
struct mcp2221 *mcp = gpiochip_get_data(gc);
+ u8 direction;
+
+ mutex_lock(&mcp->lock);

mcp->txbuf[0] = MCP2221_GPIO_GET;

mcp->gp_idx = offsetof(struct mcp_get_gpio, gpio[offset]);

- mutex_lock(&mcp->lock);
ret = mcp_send_data_req_status(mcp, mcp->txbuf, 1);
+ direction = mcp->gpio_dir;
mutex_unlock(&mcp->lock);

if (ret)
return ret;

- if (mcp->gpio_dir == MCP2221_DIR_IN)
+ if (direction == MCP2221_DIR_IN)
return GPIO_LINE_DIRECTION_IN;

return GPIO_LINE_DIRECTION_OUT;
--
2.50.1