Skip to contentSkip to conversation

mfd: nct6694: Refactor transport layer and add HIF (eSPI) support

PATCH Superseded [email protected] 3 participants 7 messages Apr 8 – Apr 10

Subsystems (11)
Networking Network drivers USB GPIO +7 more subsystems

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 cover mfd: nct6694: Refactor transport layer and add HIF (eSPI) support

From: Ming Yu <[email protected]>

The Nuvoton NCT6694 is a peripheral expander that provides GPIO, I2C, CAN-FD, Watchdog, HWMON, PWM, and RTC sub-devices. Currently, the driver only supports USB as the host transport interface.

This series refactors the NCT6694 MFD core to support multiple transport backends and adds a new Host Interface (HIF) transport driver that communicates over eSPI using Super-I/O shared memory.

Changes since version 1:
- Reworked the Super-I/O access helpers.

Ming Yu (2):
mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA
mfd: Add Host Interface (HIF) support for Nuvoton NCT6694

 MAINTAINERS                         |   1 +
 drivers/gpio/gpio-nct6694.c         |  26 +-
 drivers/hwmon/nct6694-hwmon.c       |  21 -
 drivers/i2c/busses/i2c-nct6694.c    |  26 +-
 drivers/mfd/Kconfig                 |  47 ++-
 drivers/mfd/Makefile                |   3 +-
 drivers/mfd/nct6694-hif.c           | 634 ++++++++++++++++++++++++++++
 drivers/mfd/nct6694.c               | 180 ++++----
 drivers/net/can/usb/nct6694_canfd.c |  18 +-
 drivers/rtc/rtc-nct6694.c           |   7 -
 drivers/watchdog/nct6694_wdt.c      |  27 +-
 include/linux/mfd/nct6694.h         |  57 ++-
 12 files changed, 814 insertions(+), 233 deletions(-)
 create mode 100644 drivers/mfd/nct6694-hif.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA

From: Ming Yu <[email protected]>

Currently, the nct6694 core driver uses mfd_add_hotplug_devices() and an IDA to manage subdevice IDs.

Switch the core implementation to use the managed devm_mfd_add_devices() API, which simplifies the error handling and device lifecycle management. Concurrently, drop the custom IDA implementation and transition to using pdev->id.

---
 drivers/gpio/gpio-nct6694.c         | 19 +------
 drivers/i2c/busses/i2c-nct6694.c    | 19 +------
 drivers/mfd/nct6694.c               | 83 ++++++++++++-----------------
 drivers/net/can/usb/nct6694_canfd.c | 12 +----
 drivers/watchdog/nct6694_wdt.c      | 20 +------
 include/linux/mfd/nct6694.h         |  8 +--
 6 files changed, 43 insertions(+), 118 deletions(-)
drivers/gpio/gpio-nct6694.c +1 -18
index a8607f0d9915..3703a61209e6 100644
@@ -7,7 +7,6 @@
 
 #include <linux/bits.h>
 #include <linux/gpio/driver.h>
-#include <linux/idr.h>
 #include <linux/interrupt.h>
 #include <linux/mfd/nct6694.h>
 #include <linux/module.h>
@@ -381,14 +380,6 @@ static void nct6694_irq_dispose_mapping(void *d)
 	irq_dispose_mapping(data->irq);
 }
 
-static void nct6694_gpio_ida_free(void *d)
-{
-	struct nct6694_gpio_data *data = d;
-	struct nct6694 *nct6694 = data->nct6694;
-
-	ida_free(&nct6694->gpio_ida, data->group);
-}
-
 static int nct6694_gpio_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -403,15 +394,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	data->nct6694 = nct6694;
-
-	ret = ida_alloc(&nct6694->gpio_ida, GFP_KERNEL);
-	if (ret < 0)
-		return ret;
-	data->group = ret;
-
-	ret = devm_add_action_or_reset(dev, nct6694_gpio_ida_free, data);
-	if (ret)
-		return ret;
+	data->group = pdev->id;
 
 	names = devm_kcalloc(dev, NCT6694_NR_GPIO, sizeof(char *),
 			     GFP_KERNEL);
drivers/i2c/busses/i2c-nct6694.c +1 -18
index 1413ab6f9462..7d8ad997f6d2 100644
@@ -6,7 +6,6 @@
  */
 
 #include <linux/i2c.h>
-#include <linux/idr.h>
 #include <linux/kernel.h>
 #include <linux/mfd/nct6694.h>
 #include <linux/module.h>
@@ -134,14 +133,6 @@ static int nct6694_i2c_set_baudrate(struct nct6694_i2c_data *data)
 	return 0;
 }
 
-static void nct6694_i2c_ida_free(void *d)
-{
-	struct nct6694_i2c_data *data = d;
-	struct nct6694 *nct6694 = data->nct6694;
-
-	ida_free(&nct6694->i2c_ida, data->port);
-}
-
 static int nct6694_i2c_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -155,15 +146,7 @@ static int nct6694_i2c_probe(struct platform_device *pdev)
 
 	data->dev = dev;
 	data->nct6694 = nct6694;
-
-	ret = ida_alloc(&nct6694->i2c_ida, GFP_KERNEL);
-	if (ret < 0)
-		return ret;
-	data->port = ret;
-
-	ret = devm_add_action_or_reset(dev, nct6694_i2c_ida_free, data);
-	if (ret)
-		return ret;
+	data->port = pdev->id;
 
 	ret = nct6694_i2c_set_baudrate(data);
 	if (ret)
drivers/mfd/nct6694.c +34 -49

133 lines not on this page · raw message

drivers/net/can/usb/nct6694_canfd.c +2 -10
index e5f7f8849a73..29282c56430f 100644
@@ -8,7 +8,6 @@
 #include <linux/can/dev.h>
 #include <linux/can/rx-offload.h>
 #include <linux/ethtool.h>
-#include <linux/idr.h>
 #include <linux/irqdomain.h>
 #include <linux/kernel.h>
 #include <linux/mfd/nct6694.h>
@@ -725,15 +724,13 @@ static int nct6694_canfd_probe(struct platform_device *pdev)
 	struct net_device *ndev;
 	int port, irq, ret, can_clk;
 
-	port = ida_alloc(&nct6694->canfd_ida, GFP_KERNEL);
-	if (port < 0)
-		return port;
+	port = pdev->id;
 
 	irq = irq_create_mapping(nct6694->domain,
 				 NCT6694_IRQ_CAN0 + port);
 	if (!irq) {
 		ret = -EINVAL;
-		goto free_ida;
+		return ret;
 	}
 
 	ndev = alloc_candev(sizeof(struct nct6694_canfd_priv), 1);
@@ -796,24 +793,19 @@ static int nct6694_canfd_probe(struct platform_device *pdev)
 	free_candev(ndev);
 dispose_irq:
 	irq_dispose_mapping(irq);
-free_ida:
-	ida_free(&nct6694->canfd_ida, port);
 	return ret;
 }
 
 static void nct6694_canfd_remove(struct platform_device *pdev)
 {
 	struct nct6694_canfd_priv *priv = platform_get_drvdata(pdev);
-	struct nct6694 *nct6694 = priv->nct6694;
 	struct net_device *ndev = priv->ndev;
-	int port = ndev->dev_port;
 	int irq = ndev->irq;
 
 	unregister_candev(ndev);
 	can_rx_offload_del(&priv->offload);
 	free_candev(ndev);
 	irq_dispose_mapping(irq);
-	ida_free(&nct6694->canfd_ida, port);
 }
 
 static struct platform_driver nct6694_canfd_driver = {
drivers/watchdog/nct6694_wdt.c +1 -19
index bc3689bd4b6b..2b4b804a1739 100644
@@ -5,7 +5,6 @@
  * Copyright (C) 2025 Nuvoton Technology Corp.
  */
 
-#include <linux/idr.h>
 #include <linux/kernel.h>
 #include <linux/mfd/nct6694.h>
 #include <linux/module.h>
@@ -233,21 +232,12 @@ static const struct watchdog_ops nct6694_wdt_ops = {
 	.ping = nct6694_wdt_ping,
 };
 
-static void nct6694_wdt_ida_free(void *d)
-{
-	struct nct6694_wdt_data *data = d;
-	struct nct6694 *nct6694 = data->nct6694;
-
-	ida_free(&nct6694->wdt_ida, data->wdev_idx);
-}
-
 static int nct6694_wdt_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct nct6694 *nct6694 = dev_get_drvdata(dev->parent);
 	struct nct6694_wdt_data *data;
 	struct watchdog_device *wdev;
-	int ret;
 
 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
 	if (!data)
@@ -260,15 +250,7 @@ static int nct6694_wdt_probe(struct platform_device *pdev)
 
 	data->dev = dev;
 	data->nct6694 = nct6694;
-
-	ret = ida_alloc(&nct6694->wdt_ida, GFP_KERNEL);
-	if (ret < 0)
-		return ret;
-	data->wdev_idx = ret;
-
-	ret = devm_add_action_or_reset(dev, nct6694_wdt_ida_free, data);
-	if (ret)
-		return ret;
+	data->wdev_idx = pdev->id;
 
 	wdev = &data->wdev;
 	wdev->info = &nct6694_wdt_info;
include/linux/mfd/nct6694.h +4 -4
index 6eb9be2cd4a0..496da72949d9 100644
@@ -8,6 +8,10 @@
 #ifndef __MFD_NCT6694_H
 #define __MFD_NCT6694_H
 
+#include <linux/mutex.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>
+
 #define NCT6694_VENDOR_ID	0x0416
 #define NCT6694_PRODUCT_ID	0x200B
 #define NCT6694_INT_IN_EP	0x81
@@ -82,10 +86,6 @@ union __packed nct6694_usb_msg {
 
 struct nct6694 {
 	struct device *dev;
-	struct ida gpio_ida;
-	struct ida i2c_ida;
-	struct ida canfd_ida;
-	struct ida wdt_ida;
 	struct irq_domain *domain;
 	struct mutex access_lock;
 	spinlock_t irq_lock;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/2] mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA

Sashiko AI review found 2 potential issue(s): - [High] Use-After-Free and WARN_ON due to Teardown Inversion on USB disconnect and probe failure - [High] Sysfs Namespace Collision breaking support…

Sashiko AI review found 2 potential issue(s):
- [High] Use-After-Free and WARN_ON due to Teardown Inversion on USB disconnect and probe failure
- [High] Sysfs Namespace Collision breaking support for multiple USB devices
--

commit 35c3701128ee68ba67a073bb4beea066282a9dba
Author: Ming Yu <[email protected]>

mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA

This commit switches the nct6694 core driver to use devm_mfd_add_devices() for simplified device lifecycle management. It also drops the custom IDA implementation and transitions to using pdev->id.

Quoted 4 lines
drivers/mfd/nct6694.c +0 -0
index 308b2fda3055c..8ce2c4985aaba 100644

[ ... ]

drivers/mfd/nct6694.c static int nct6694_usb_probe(struct usb_interface *iface, · line 329

Quoted 6 lines
drivers/mfd/nct6694.c +1 -1
@@ -332,11 +326,11 @@ static int nct6694_usb_probe(struct usb_interface *iface,
 
 	usb_set_intfdata(iface, nct6694);
 
-	ret = mfd_add_hotplug_devices(dev, nct6694_devs, ARRAY_SIZE(nct6694_devs));
+	ret = devm_mfd_add_devices(dev, 0, nct6694_devs, ARRAY_SIZE(nct6694_devs), NULL, 0, NULL);

The previous mfd_add_hotplug_devices() call automatically used PLATFORM_DEVID_AUTO, which assigns globally unique IDs to prevent sysfs name collisions.

By switching to devm_mfd_add_devices() with an ID argument of 0, does this bind pdev->id to the static cell->id (0, 1, 2...)?

If a user connects a second identical NCT6694 USB device, it seems the MFD core might attempt to register devices with the exact same names (e.g., nct6694-gpio.0) on the global platform bus. Will this cause the platform bus to reject the second device with a -EEXIST error, breaking hotplug support for multiple devices?

[ ... ]

Quoted 14 lines
drivers/mfd/nct6694.c +1 -5
@@ -344,11 +338,7 @@ static int nct6694_usb_probe(struct usb_interface *iface,
 
 err_mfd:
 	usb_kill_urb(nct6694->int_in_urb);
-err_ida:
-	ida_destroy(&nct6694->wdt_ida);
-	ida_destroy(&nct6694->canfd_ida);
-	ida_destroy(&nct6694->i2c_ida);
-	ida_destroy(&nct6694->gpio_ida);
+err_irq_domain:
 	irq_domain_remove(nct6694->domain);
 err_urb:
 	usb_free_urb(nct6694->int_in_urb);

drivers/mfd/nct6694.c static void nct6694_usb_disconnect(struct usb_interface *iface) · line 353

Quoted 10 lines
drivers/mfd/nct6694.c +0 -5
@@ -359,12 +349,7 @@ static void nct6694_usb_disconnect(struct usb_interface *iface)
 {
 	struct nct6694 *nct6694 = usb_get_intfdata(iface);
 
-	mfd_remove_devices(nct6694->dev);
 	usb_kill_urb(nct6694->int_in_urb);
-	ida_destroy(&nct6694->wdt_ida);
-	ida_destroy(&nct6694->canfd_ida);
-	ida_destroy(&nct6694->i2c_ida);
-	ida_destroy(&nct6694->gpio_ida);
 	irq_domain_remove(nct6694->domain);

By relying on devres for MFD device removal, the child devices will now be unregistered during the devres cleanup phase, which happens after nct6694_usb_disconnect() returns.

However, irq_domain_remove() is still explicitly called here. Will this destroy and free the domain memory while the child devices are still registered and holding active IRQ mappings?

If the domain is removed early, it looks like it would trigger a WARN_ON in irq_domain_remove() due to active mappings. Furthermore, when devres later unregisters the child devices, their teardown paths (such as nct6694_irq_dispose_mapping() and nct6694_canfd_remove()) call irq_dispose_mapping() and access the already-freed IRQ domain. Can this lead to a use-after-free?

A similar ordering issue might also happen in the probe error path if devm_mfd_add_devices() partially fails, as the error path frees the IRQ domain before devres cleans up any successfully added child devices.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v2 1/2] mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA

In reply to [email protected]
Quoted 6 lines · On Wed, Apr 8, 2026 at 7:31 AM <[email protected]> wrote:

From: Ming Yu <[email protected]>

Currently, the nct6694 core driver uses mfd_add_hotplug_devices() and an IDA to manage subdevice IDs.

Quoted 7 lines

Switch the core implementation to use the managed devm_mfd_add_devices() API, which simplifies the error handling and device lifecycle management. Concurrently, drop the custom IDA implementation and transition to using pdev->id.

---

This does result in a nice code shrink but I'd split this commit into
two: one switching to using MFD_CELL_BASIC() with hard-coded devices IDs and one completing the transition to devres.

Bart

author
#4 lore raw

Re: [PATCH v2 1/2] mfd: nct6694: Switch to devm_mfd_add_devices() and drop IDA

Hi Bart, all,

Thanks for the review.

Bartosz Golaszewski <[email protected]> 於 2026年4月8日週三 下午3:25寫道:

Quoted 13 lines · On Wed, Apr 8, 2026 at 7:31 AM <[email protected]> wrote:
Quoted 11 lines

From: Ming Yu <[email protected]>

Currently, the nct6694 core driver uses mfd_add_hotplug_devices() and an IDA to manage subdevice IDs.

Switch the core implementation to use the managed devm_mfd_add_devices() API, which simplifies the error handling and device lifecycle management. Concurrently, drop the custom IDA implementation and transition to using pdev->id.

Quoted 7 lines
Quoted 2 lines

---

This does result in a nice code shrink but I'd split this commit into
two: one switching to using MFD_CELL_BASIC() with hard-coded devices IDs and one completing the transition to devres.

You are right that this change is trying to do too much at once, and splitting it as you suggested would make the series much cleaner.

After looking more closely at the ID handling and hotplug implications, I realized that switching to devm_mfd_add_devices() and dropping the IDA is not a good fit for this driver. The current mfd_add_hotplug_devices() path uses PLATFORM_DEVID_AUTO, which gives globally unique device IDs and avoids sysfs name collisions. If we switch to devm_mfd_add_devices() with fixed IDs, multiple identical NCT6694 devices can end up registering subdevices with the same platform device names, which would break hotplug support when more than one device is present.

So I think it is better not to pursue this direction further.

For the next revision, I will drop this part of the change and keep the existing MFD core logic, including the IDA usage. The series will focus on adding the nct6694-hif MFD driver only, and I will add the IDA initialization there as needed.

Thanks again for the suggestion and review.

Best regards,
Ming

#5 lore raw

PATCH v2 2/2 mfd: Add Host Interface (HIF) support for Nuvoton NCT6694

In reply to [email protected]

From: Ming Yu <[email protected]>

The Nuvoton NCT6694 also provides a Host Interface (HIF) via eSPI to the host to access its features.

Sub-devices can use the common functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupts that will be called when the HIF device triggers a shared memory interrupt.

To support multiple transports, the driver configuration is updated to allow selecting between the USB and HIF interfaces.

---
Changes since version 1:
- Drop function pointers from Super-I/O access and use static inline helpers with proper types.

 MAINTAINERS                         |   1 +
 drivers/gpio/gpio-nct6694.c         |   7 -
 drivers/hwmon/nct6694-hwmon.c       |  21 -
 drivers/i2c/busses/i2c-nct6694.c    |   7 -
 drivers/mfd/Kconfig                 |  47 ++-
 drivers/mfd/Makefile                |   3 +-
 drivers/mfd/nct6694-hif.c           | 634 ++++++++++++++++++++++++++++
 drivers/mfd/nct6694.c               |  97 +++--
 drivers/net/can/usb/nct6694_canfd.c |   6 -
 drivers/rtc/rtc-nct6694.c           |   7 -
 drivers/watchdog/nct6694_wdt.c      |   7 -
 include/linux/mfd/nct6694.h         |  51 ++-
 12 files changed, 772 insertions(+), 116 deletions(-)
 create mode 100644 drivers/mfd/nct6694-hif.c
MAINTAINERS +1 -0
index c3fe46d7c4bc..7b6241faa6df 100644
@@ -18899,6 +18899,7 @@ S:	Supported
 F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/hwmon/nct6694-hwmon.c
 F:	drivers/i2c/busses/i2c-nct6694.c
+F:	drivers/mfd/nct6694-hif.c
 F:	drivers/mfd/nct6694.c
 F:	drivers/net/can/usb/nct6694_canfd.c
 F:	drivers/rtc/rtc-nct6694.c
drivers/gpio/gpio-nct6694.c +0 -7
index 3703a61209e6..a279510ece89 100644
@@ -12,13 +12,6 @@
 #include <linux/module.h>
 #include <linux/platform_device.h>
 
-/*
- * USB command module type for NCT6694 GPIO controller.
- * This defines the module type used for communication with the NCT6694
- * GPIO controller over the USB interface.
- */
-#define NCT6694_GPIO_MOD	0xFF
-
 #define NCT6694_GPIO_VER	0x90
 #define NCT6694_GPIO_VALID	0x110
 #define NCT6694_GPI_DATA	0x120
drivers/hwmon/nct6694-hwmon.c +0 -21
index 6dcf22ca5018..581451875f2c 100644
@@ -15,13 +15,6 @@
 #include <linux/platform_device.h>
 #include <linux/slab.h>
 
-/*
- * USB command module type for NCT6694 report channel
- * This defines the module type used for communication with the NCT6694
- * report channel over the USB interface.
- */
-#define NCT6694_RPT_MOD			0xFF
-
 /* Report channel */
 /*
  * The report channel is used to report the status of the hardware monitor
@@ -38,13 +31,6 @@
 #define NCT6694_TIN_STS(x)		(0x6A + (x))
 #define NCT6694_FIN_STS(x)		(0x6E + (x))
 
-/*
- * USB command module type for NCT6694 HWMON controller.
- * This defines the module type used for communication with the NCT6694
- * HWMON controller over the USB interface.
- */
-#define NCT6694_HWMON_MOD		0x00
-
 /* Command 00h - Hardware Monitor Control */
 #define NCT6694_HWMON_CONTROL		0x00
 #define NCT6694_HWMON_CONTROL_SEL	0x00
@@ -53,13 +39,6 @@
 #define NCT6694_HWMON_ALARM		0x02
 #define NCT6694_HWMON_ALARM_SEL		0x00
 
-/*
- * USB command module type for NCT6694 PWM controller.
- * This defines the module type used for communication with the NCT6694
- * PWM controller over the USB interface.
- */
-#define NCT6694_PWM_MOD			0x01
-
 /* PWM Command - Manual Control */
 #define NCT6694_PWM_CONTROL		0x01
 #define NCT6694_PWM_CONTROL_SEL		0x00
drivers/i2c/busses/i2c-nct6694.c +0 -7
index 7d8ad997f6d2..7ee209a04d16 100644
@@ -11,13 +11,6 @@
 #include <linux/module.h>
 #include <linux/platform_device.h>
 
-/*
- * USB command module type for NCT6694 I2C controller.
- * This defines the module type used for communication with the NCT6694
- * I2C controller over the USB interface.
- */
-#define NCT6694_I2C_MOD			0x03
-
 /* Command 00h - I2C Deliver */
 #define NCT6694_I2C_DELIVER		0x00
 #define NCT6694_I2C_DELIVER_SEL		0x00
drivers/mfd/Kconfig +37 -10

56 lines not on this page · raw message

drivers/mfd/Makefile +2 -1
index e75e8045c28a..4cee9b74978c 100644
@@ -124,7 +124,8 @@ obj-$(CONFIG_MFD_MC13XXX_I2C)	+= mc13xxx-i2c.o
 
 obj-$(CONFIG_MFD_PF1550)	+= pf1550.o
 
-obj-$(CONFIG_MFD_NCT6694)	+= nct6694.o
+obj-$(CONFIG_MFD_NCT6694_HIF)	+= nct6694-hif.o
+obj-$(CONFIG_MFD_NCT6694_USB)	+= nct6694.o
 
 obj-$(CONFIG_MFD_CORE)		+= mfd-core.o
 
drivers/mfd/nct6694-hif.c new file +634 -0

634 lines not on this page · raw message

drivers/mfd/nct6694.c +67 -30

201 lines not on this page · raw message

drivers/net/can/usb/nct6694_canfd.c +0 -6
index 29282c56430f..05db00455f63 100644
@@ -17,12 +17,6 @@
 
 #define DEVICE_NAME "nct6694-canfd"
 
-/* USB command module type for NCT6694 CANfd controller.
- * This defines the module type used for communication with the NCT6694
- * CANfd controller over the USB interface.
- */
-#define NCT6694_CANFD_MOD			0x05
-
 /* Command 00h - CAN Setting and Initialization */
 #define NCT6694_CANFD_SETTING			0x00
 #define NCT6694_CANFD_SETTING_ACTIVE_CTRL1	BIT(0)
drivers/rtc/rtc-nct6694.c +0 -7
index 35401a0d9cf5..c06902f150c9 100644
@@ -14,13 +14,6 @@
 #include <linux/rtc.h>
 #include <linux/slab.h>
 
-/*
- * USB command module type for NCT6694 RTC controller.
- * This defines the module type used for communication with the NCT6694
- * RTC controller over the USB interface.
- */
-#define NCT6694_RTC_MOD		0x08
-
 /* Command 00h - RTC Time */
 #define NCT6694_RTC_TIME	0x0000
 #define NCT6694_RTC_TIME_SEL	0x00
drivers/watchdog/nct6694_wdt.c +0 -7
index 2b4b804a1739..847d8f1d1830 100644
@@ -19,13 +19,6 @@
 
 #define NCT6694_WDT_MAX_DEVS		2
 
-/*
- * USB command module type for NCT6694 WDT controller.
- * This defines the module type used for communication with the NCT6694
- * WDT controller over the USB interface.
- */
-#define NCT6694_WDT_MOD			0x07
-
 /* Command 00h - WDT Setup */
 #define NCT6694_WDT_SETUP		0x00
 #define NCT6694_WDT_SETUP_SEL(idx)	(idx ? 0x01 : 0x00)
include/linux/mfd/nct6694.h +31 -20

78 lines not on this page · raw message

Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 2/2] mfd: Add Host Interface (HIF) support for Nuvoton NCT6694

Sashiko AI review found 8 potential issue(s): - [High] devm_request_threaded_irq() with IRQF_SHARED and a NULL primary handler unconditionally fails. - [High] Writing a Linux virtual IRQ number to…

Sashiko AI review found 8 potential issue(s):
- [High] devm_request_threaded_irq() with IRQF_SHARED and a NULL primary handler unconditionally fails.
- [High] Writing a Linux virtual IRQ number to the SIO hardware register corrupts interrupt routing.
- [High] Use-After-Free during USB disconnect due to incorrect teardown ordering of the IRQ domain.
- [High] Direct DMA to stack variables causes kernel panics.
- [High] Missing endpoint count check allows out-of-bounds array access.
- [Medium] Unbalanced release_region() and unsynchronized hardware access due to ignored superio_enter() return value.
- [Medium] Devres teardown ordering creates a window for an unhandled interrupt storm.
- [Medium] Unmapped hardware interrupts invoke the handler for Linux IRQ 0, causing system instability. --

commit 60375817e31ea4cd313c9492ef71c36123f2e1d1
Author: Ming Yu <[email protected]>

mfd: Add Host Interface (HIF) support for Nuvoton NCT6694

This commit adds Host Interface (HIF) via eSPI for the Nuvoton NCT6694, introducing a new transport driver while reorganizing the core and USB driver. It provides common read/write functions and shared memory interrupt handling.

Quoted 5 lines
drivers/mfd/nct6694-hif.c new file +0 -0
new file mode 100644
index 0000000000000..7560754e74810

[ ... ]

Quoted 2 lines
drivers/mfd/nct6694-hif.c new file +2 -0
+static irqreturn_t nct6694_irq_handler(int irq, void *data)
+{

[ ... ]

drivers/mfd/nct6694-hif.c line 433

Quoted 9 lines
drivers/mfd/nct6694-hif.c new file +9 -0
+	while (intr_status) {
+		int irq = __ffs(intr_status);
+
+		generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq));
+		intr_status &= ~BIT(irq);
+	}
+
+	return IRQ_HANDLED;
+}

If the hardware sets an unexpected interrupt bit that hasn't been mapped, irq_find_mapping() returns 0. Does passing 0 directly into generic_handle_irq_safe() inadvertently invoke the handler for Linux IRQ 0, causing system instability? This applies to both this function and the USB interrupt handler.

[ ... ]

drivers/mfd/nct6694-hif.c line 454

Quoted 8 lines
drivers/mfd/nct6694-hif.c new file +8 -0
+static int nct6694_irq_init(struct nct6694 *nct6694, int irq)
+{
+	struct nct6694_hif_data *hdata = nct6694->priv;
+	struct nct6694_sio_data *sio_data = hdata->sio_data;
+	unsigned char cofs_ctl2;
+
+	/* Set SIRQ number */
+	superio_enter(sio_data);

What happens if the request_muxed_region() inside superio_enter() fails and returns -EBUSY? If the return value is ignored, the driver might proceed with unsynchronized hardware access and later call superio_exit(), releasing an I/O region it never successfully acquired.

drivers/mfd/nct6694-hif.c line 462

Quoted 8 lines
drivers/mfd/nct6694-hif.c new file +8 -0
+	superio_select(sio_data, SIO_REG_LD_SHM);
+	if (!superio_inb(sio_data, SIO_REG_SHM_ENABLE)) {
+		superio_exit(sio_data);
+		return -EIO;
+	}
+	hdata->shm_base = superio_inw(sio_data, SIO_REG_SHM_BASE_ADDR);
+
+	superio_outb(sio_data, SIO_REG_SHM_IRQ_NR, irq);

Could writing the Linux virtual IRQ number here misconfigure the hardware? The irq parameter comes from platform_get_irq(), which returns a dynamically allocated virtual IRQ number rather than the physical ISA or eSPI IRQ line expected by the Super I/O chip.

[ ... ]

Quoted 2 lines
drivers/mfd/nct6694-hif.c new file +2 -0
+static int nct6694_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/mfd/nct6694-hif.c line 605

Quoted 11 lines
drivers/mfd/nct6694-hif.c new file +11 -0
+	ret = nct6694_irq_init(data, irq);
+	if (ret)
+		return ret;
+
+	ret = devm_add_action_or_reset(dev, nct6694_irq_release, data);
+	if (ret)
+		return ret;
+
+	ret = devm_request_threaded_irq(dev, irq, NULL, nct6694_irq_handler,
+					IRQF_ONESHOT | IRQF_SHARED,
+					dev_name(dev), data);

The IRQ subsystem explicitly rejects shared interrupts without a primary handler, as the default primary handler unconditionally masks the shared line and starves other devices. Will this devm_request_threaded_irq() call unconditionally return -EINVAL and fail to initialize?

Also, devres actions execute in reverse order during removal. Since the hardware interrupt is enabled via nct6694_irq_init() before the IRQ handler is requested, the handler will be freed before the hardware interrupt is disabled during teardown. Could this create a window for an unhandled interrupt storm?

Quoted 4 lines
drivers/mfd/nct6694.c +0 -0
index 8ce2c4985aaba..903a0a7f0694c 100644

[ ... ]

Quoted 4 lines
drivers/mfd/nct6694.c +4 -0
+static int nct6694_usb_read_msg(struct nct6694 *nct6694,
+				const struct nct6694_cmd_header *cmd_hd,
+				void *buf)
+{

[ ... ]

drivers/mfd/nct6694.c

Quoted 3 lines
drivers/mfd/nct6694.c +3 -0
+	/* Receive data packet from USB device */
+	ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, NCT6694_BULK_IN_EP), buf,
+			   le16_to_cpu(cmd_hd->len), &rx_len, NCT6694_URB_TIMEOUT);

Child drivers often pass pointers to local stack variables as the buf argument. When passed directly to usb_bulk_msg(), does this cause a kernel panic if CONFIG_VMAP_STACK is enabled, due to performing DMA on a stack variable? Would it be safer to allocate a bounce buffer for the payload? This applies to nct6694_usb_write_msg() as well.

[ ... ]

Quoted 3 lines

static int nct6694_usb_probe(struct usb_interface *iface,
const struct usb_device_id *id)
{

[ ... ]

drivers/mfd/nct6694.c

Quoted 7 lines
drivers/mfd/nct6694.c +7 -0
+	interface = iface->cur_altsetting;
+
+	int_endpoint = &interface->endpoint[0].desc;
+	if (!usb_endpoint_is_int_in(int_endpoint)) {
+		ret = -ENODEV;
+		goto err_irq_domain;
+	}

If a malformed or malicious USB device presents an interface with zero endpoints, will this direct access to interface->endpoint[0] result in an out-of-bounds read?

[ ... ]

drivers/mfd/nct6694.c static int nct6694_usb_probe(struct usb_interface *iface, · line 392

Quoted 11 lines

static void nct6694_usb_disconnect(struct usb_interface *iface) {

drivers/mfd/nct6694.c +3 -2
 	struct nct6694 *nct6694 = usb_get_intfdata(iface);
+	struct nct6694_usb_data *udata = nct6694->priv;
 
-	usb_kill_urb(nct6694->int_in_urb);
+	usb_kill_urb(udata->int_in_urb);
 	irq_domain_remove(nct6694->domain);
-	usb_free_urb(nct6694->int_in_urb);
+	usb_free_urb(udata->int_in_urb);
 }

The child MFD devices are registered using devm_mfd_add_devices(), which means they will be unbound by devres after this disconnect function returns. If irq_domain_remove() is called here, will the child drivers attempt to access the already-freed IRQ domain during their devres cleanup routines, leading to a use-after-free?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2