mfd: nct6694: Refactor transport layer and add HIF (eSPI) support
Subsystems (8)
v8 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 driver to support multiple transport backends and adds a Host Interface (HIF) transport driver that communicates over eSPI using Super-I/O shared memory.
Patches 1-5 fix pre-existing bugs in the current driver. They carry a Fixes tag and can be picked up independently of the rest of the series.
Patches 6-12 isolate the USB transport, extract the transport-agnostic core into nct6694-core.c and wrap the firmware command interface behind regmap, so that the sub-device drivers no longer depend on the transport. Patch 13 adds the HIF transport on top.
Changes since version 7:
- Marked the GPIO controller as sleeping.
- Validated the USB endpoints with usb_check_{bulk,int}_endpoints().
- Checked the length of received USB packets.
- Ignored interrupts without a mapping.
- Moved all fixes to the start of the series.
- Embedded struct nct6694 in the transport data and dropped the priv pointer.
- Renamed variables and buffers consistently.
- Retrieved struct nct6694 from the driver data in the core, and let the transports register their own MFD cells.
- Returned -ENOMEM when the IRQ domain cannot be created.
- Kept MFD_NCT6694 as the core symbol and added MFD_NCT6694_USB.
- Folded the interrupt URB submission fix into the core extraction.
- HIF: requested the interrupt after the core is set up, so that it is freed before the IRQ domain is removed.
Changes since version 6:
- Transfer command payloads through a buffer owned by the transport.
- Locate the interrupt IN endpoint with usb_find_int_in_endpoint().
- Submit the interrupt URB after the core has created the IRQ domain.
- HIF: several robustness fixes in the interrupt and shared memory setup.
Changes since version 5:
- Replaced the read_msg/write_msg function pointers with a regmap-based transport abstraction, and added nct6694_write_read_msg().
Changes since version 4:
- Split the monolithic patch into a series of logical commits.
Changes since version 3:
- Removed redundant module type macro definitions from the sub-device drivers.
Changes since version 2:
- Restored per-device IDA and PLATFORM_DEVID_AUTO.
- Validated the irq_find_mapping() return value.
- Checked the superio_enter() return value.
Changes since version 1:
- Reworked the Super-I/O access helpers.
Ming Yu (13):
gpio: nct6694: Mark the GPIO controller as sleeping
mfd: nct6694: Validate the USB endpoints
mfd: nct6694: Check the length of received USB packets
mfd: nct6694: Ignore interrupts without a mapping
mfd: nct6694: Transfer data packets via a dedicated buffer
mfd: nct6694: Move module type macros to shared header
mfd: nct6694: Refactor USB-specific data into nct6694_usb_data
mfd: nct6694: Rename USB transport functions with _usb_ prefix
mfd: nct6694: Rename driver to nct6694-usb
mfd: nct6694: Extract core device management into a separate module
mfd: nct6694: Introduce regmap-based transport abstraction
mfd: nct6694: Add a Kconfig symbol for the USB transport
mfd: nct6694: Add Host Interface (HIF) eSPI transport driver
MAINTAINERS | 2 +- drivers/gpio/gpio-nct6694.c | 9 +- drivers/hwmon/nct6694-hwmon.c | 21 - drivers/i2c/busses/i2c-nct6694.c | 9 +- drivers/mfd/Kconfig | 34 +- drivers/mfd/Makefile | 4 +- drivers/mfd/nct6694-core.c | 111 ++++++ drivers/mfd/nct6694-hif.c | 572 ++++++++++++++++++++++++++++ drivers/mfd/nct6694-usb.c | 414 ++++++++++++++++++++ drivers/mfd/nct6694.c | 388 ------------------- 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 | 91 ++++- 14 files changed, 1205 insertions(+), 470 deletions(-) create mode 100644 drivers/mfd/nct6694-core.c create mode 100644 drivers/mfd/nct6694-hif.c create mode 100644 drivers/mfd/nct6694-usb.c delete mode 100644 drivers/mfd/nct6694.c
Signature · 1 line
2.34.1
PATCH v8 1/13 gpio: nct6694: Mark the GPIO controller as sleeping
From: Ming Yu <[email protected]>
The GPIO callbacks take a mutex and issue USB transfers, so this GPIO chip must not be used in atomic context. Change the can_sleep switch in struct gpio_chip to true.
---
Changes in v8:
- New patch. Set can_sleep, as the GPIO callbacks sleep on USB I/O.
drivers/gpio/gpio-nct6694.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-nct6694.c +1 -1
@@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev) data->gpio.set_config = nct6694_set_config; data->gpio.init_valid_mask = nct6694_init_valid_mask; data->gpio.base =-1 ; - data->gpio.can_sleep = false; + data->gpio.can_sleep = true; data->gpio.owner =THIS_MODULE ; data->gpio.ngpio =NCT6694_NR_GPIO ;
Signature · 1 line
2.34.1
PATCH v8 2/13 mfd: nct6694: Validate the USB endpoints
In reply to [email protected]From: Ming Yu <[email protected]>
The probe reads endpoint[0] of the current altsetting without checking that the interface describes any endpoint, and the bulk endpoints used for the command transfers are never validated. A malformed device can make the driver read past the endpoint array or submit URBs to endpoints of the wrong type.
The interface exposes several interrupt and bulk endpoints, of which only EP1 IN, EP2 IN and EP3 OUT carry the command interface, so the endpoints cannot be looked up by type. Check that the expected endpoints are present with usb_check_{bulk,int}_endpoints(), and take the polling interval from the interrupt endpoint actually used.
Define the interrupt endpoint by its number, like the bulk endpoints, as the pipe macros expect an endpoint number rather than an address.
---
Changes in v8:
- Also validate the bulk endpoints. Keep the fixed endpoint numbers and check them with usb_check_{bulk,int}_endpoints() instead of looking up the first endpoint of each type, as the interface exposes several interrupt and bulk endpoints.
- Take the polling interval from the interrupt endpoint actually used, and define it by its number like the bulk endpoints.
Changes in v7:
- New patch.
drivers/mfd/nct6694.c | 29 +++++++++++++++++------------ include/linux/mfd/nct6694.h | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-)
drivers/mfd/nct6694.c +17 -12
@@ -273,13 +273,25 @@ static const struct irq_domain_ops nct6694_irq_domain_ops = {static int nct6694_usb_probe (struct usb_interface *iface,const struct usb_device_id *id) { +static const u8 bulk_ep_addr[] = { +USB_DIR_IN |NCT6694_BULK_IN_EP , +USB_DIR_OUT |NCT6694_BULK_OUT_EP , +0 + }; +static const u8 int_ep_addr[] = { +USB_DIR_IN |NCT6694_INT_IN_EP , +0 + };struct usb_device *udev =interface_to_usbdev (iface); -struct usb_endpoint_descriptor *int_endpoint; -struct usb_host_interface *interface;struct device *dev = &iface->dev;struct nct6694 *nct6694; +unsigned int int_pipe;int ret; +if (!usb_check_bulk_endpoints (iface, bulk_ep_addr) || + !usb_check_int_endpoints (iface, int_ep_addr)) +return -ENODEV ; + nct6694 =devm_kzalloc (dev,sizeof (*nct6694),GFP_KERNEL );if (!nct6694)return -ENOMEM ;
@@ -318,17 +330,10 @@ static int nct6694_usb_probe(struct usb_interface *iface,if (ret) goto err_ida; - interface = iface->cur_altsetting; - - int_endpoint = &interface->endpoint[0 ].desc; -if (!usb_endpoint_is_int_in (int_endpoint)) { - ret = -ENODEV ; - goto err_ida; - } - -usb_fill_int_urb (nct6694->int_in_urb, udev,usb_rcvintpipe (udev,NCT6694_INT_IN_EP ), + int_pipe =usb_rcvintpipe (udev,NCT6694_INT_IN_EP ); +usb_fill_int_urb (nct6694->int_in_urb, udev, int_pipe, nct6694->int_buffer,sizeof (*nct6694->int_buffer), usb_int_callback, - nct6694, int_endpoint->bInterval); + nct6694,usb_pipe_endpoint (udev, int_pipe)->desc.bInterval); ret =usb_submit_urb (nct6694->int_in_urb,GFP_KERNEL );if (ret)
include/linux/mfd/nct6694.h +1 -1
@@ -10,7 +10,7 @@#define NCT6694_VENDOR_ID 0x0416#define NCT6694_PRODUCT_ID 0x200B -#define NCT6694_INT_IN_EP 0x81 +#define NCT6694_INT_IN_EP 0x01#define NCT6694_BULK_IN_EP 0x02#define NCT6694_BULK_OUT_EP 0x03
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 02/13] mfd: nct6694: Validate the USB endpoints
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
From: Ming Yu <[email protected]>
The probe reads endpoint[0] of the current altsetting without checking that the interface describes any endpoint, and the bulk endpoints used for the command transfers are never validated. A malformed device can make the driver read past the endpoint array or submit URBs to endpoints of the wrong type.
The interface exposes several interrupt and bulk endpoints, of which only EP1 IN, EP2 IN and EP3 OUT carry the command interface, so the endpoints cannot be looked up by type. Check that the expected endpoints are present with usb_check_{bulk,int}_endpoints(), and take the polling interval from the interrupt endpoint actually used.
Quoted 5 lines
Define the interrupt endpoint by its number, like the bulk endpoints, as the pipe macros expect an endpoint number rather than an address.
Fixes 51dad33ede63 ("mfd: Add core driver for Nuvoton NCT6694")Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v8 3/13 mfd: nct6694: Check the length of received USB packets
In reply to [email protected]From: Ming Yu <[email protected]>
The response status and the interrupt status are parsed without checking how many bytes were actually received. A short or zero-length packet leaves stale data in the buffer, which is then taken as a valid status and may repeatedly dispatch the same interrupts.
Reject short response packets and ignore short interrupt packets.
---
Changes in v8:
- New patch. Check the length of the response and interrupt packets before parsing them.
drivers/mfd/nct6694.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/mfd/nct6694.c +15 -0
@@ -117,6 +117,12 @@ int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *cif (ret)return ret; +if (rx_len !=sizeof (msg->response_header)) { +dev_err (nct6694->dev,"Expected response length %zu, but got %d\n" , +sizeof (msg->response_header), rx_len); +return -EIO ; + } +/* 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 );
@@ -173,6 +179,12 @@ int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *if (ret)return ret; +if (rx_len !=sizeof (msg->response_header)) { +dev_err (nct6694->dev,"Expected response length %zu, but got %d\n" , +sizeof (msg->response_header), rx_len); +return -EIO ; + } +/* 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 );
@@ -207,6 +219,9 @@ static void usb_int_callback(struct urb *urb) goto resubmit; } +if (urb->actual_length <sizeof (*status_le)) + goto resubmit; + int_status =le32_to_cpu (*status_le);while (int_status) {
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 03/13] mfd: nct6694: Check the length of received USB packets
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mfd: nct6694: irq_enable is write-only dead code with missing hardware mask sync --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mfd: nct6694: irq_enable is write-only dead code with missing hardware mask sync --
commit 2c09e36bb163557eb6e9fd5c79ae3e61a1464462
Author: Ming Yu <[email protected]>
mfd: nct6694: Check the length of received USB packets
The response status and the interrupt status are parsed without checking how many bytes were actually received. A short or zero-length packet leaves stale data in the buffer, which is then taken as a valid status and may repeatedly dispatch the same interrupts.
Reject short response packets and ignore short interrupt packets.
Quoted 4 lines
drivers/mfd/nct6694.c +0
-0
[ ... ]
Quoted 10 lines
drivers/mfd/nct6694.c +3
-0@@ -207,6 +219,9 @@ static void usb_int_callback(struct urb *urb) goto resubmit; } +if (urb->actual_length <sizeof (*status_le)) + goto resubmit; + int_status =le32_to_cpu (*status_le);while (int_status) {
Quoted 5 lines
int irq = __ffs(int_status); generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq)); int_status &= ~BIT(irq); }
[Severity: Medium]
Are all set bits in int_status dispatched regardless of whether they are enabled in nct6694->irq_enable?
Looking at nct6694_irq_enable(), the nct6694->irq_enable field is updated when an interrupt is enabled:
drivers/mfd/nct6694.c:nct6694_irq_enable() {
...
guard(spinlock_irqsave)(&nct6694->irq_lock);
nct6694->irq_enable |= BIT(hwirq);
}However, it doesn't appear this mask is ever synced to the device hardware (for example, via an irq_bus_sync_unlock operation), nor is it read by the software interrupt handler in usb_int_callback() to mask out disabled interrupts.
If the device's top-level interrupts are level-triggered, could the lack of hardware masking cause the device to continuously send urb packets when a child driver disables an interrupt?
This might lead to bounded interrupt storms at the usb polling rate, as the urb is repeatedly received, ignored by the irq core, and resubmitted.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v8 4/13 mfd: nct6694: Ignore interrupts without a mapping
In reply to [email protected]From: Ming Yu <[email protected]>
The return value of irq_find_mapping() is passed straight to generic_handle_irq_safe(). If the device reports a hardware interrupt that has no mapping, irq_find_mapping() returns 0 and the handler of the system's IRQ 0 is invoked instead.
Use generic_handle_domain_irq_safe(), which ignores unmapped hardware interrupts.
---
Changes in v8:
- New patch. Use generic_handle_domain_irq_safe() so that unmapped interrupts are ignored.
drivers/mfd/nct6694.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/nct6694.c +1 -1
@@ -227,7 +227,7 @@ static void usb_int_callback(struct urb *urb)while (int_status) {int irq =__ffs (int_status); -generic_handle_irq_safe (irq_find_mapping (nct6694->domain, irq)); +generic_handle_domain_irq_safe (nct6694->domain, irq); int_status &= ~BIT (irq); }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 04/13] mfd: nct6694: Ignore interrupts without a mapping
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Ming Yu <[email protected]>
The return value of
irq_find_mapping()is passed straight togeneric_handle_irq_safe(). If the device reports a hardware interrupt that has no mapping,irq_find_mapping()returns 0 and the handler of the system's IRQ 0 is invoked instead.
Quoted 5 lines
Use
generic_handle_domain_irq_safe(), which ignores unmapped hardware interrupts.Fixes 51dad33ede63 ("mfd: Add core driver for Nuvoton NCT6694")Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v8 5/13 mfd: nct6694: Transfer data packets via a dedicated buffer
In reply to [email protected]From: Ming Yu <[email protected]>
The caller's buffer is passed straight to usb_bulk_msg(). Sub-device drivers embed those buffers in their private data, so they are neither cacheline aligned nor exclusively owned by the transfer, and mapping them for DMA can corrupt adjacent fields on non-coherent architectures.
Transfer the data packets through a buffer owned by the transport, and reject commands exceeding the maximum data length.
---
Changes in v8:
- Moved before the refactoring so it applies to the original driver.
- Renamed xfer_buf to data_buf, len to data_len and NCT6694_MAX_PACKET_SIZE to NCT6694_MAX_DATA_LEN.
Changes in v7:
- New patch.
drivers/mfd/nct6694.c | 37 +++++++++++++++++++++++++++---------- include/linux/mfd/nct6694.h | 4 ++++ 2 files changed, 31 insertions(+), 10 deletions(-)
drivers/mfd/nct6694.c +27 -10
@@ -98,8 +98,12 @@ int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *c {union nct6694_usb_msg *msg = nct6694->usb_msg;struct usb_device *udev = nct6694->udev; +u16 data_len =le16_to_cpu (cmd_hd->len);int tx_len, rx_len, ret; +if (data_len >NCT6694_MAX_DATA_LEN ) +return -EINVAL ; +guard (mutex)(&nct6694->access_lock);memcpy (&msg->cmd_header, cmd_hd,sizeof (*cmd_hd));
@@ -124,17 +128,19 @@ int nct6694_read_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *c }/* 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 ); + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), nct6694->data_buf, + data_len, &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret; -if (rx_len !=le16_to_cpu (cmd_hd->len)) { +if (rx_len != data_len) {dev_err (nct6694->dev,"Expected received length %d, but got %d\n" , -le16_to_cpu (cmd_hd->len), rx_len); + data_len, rx_len);return -EIO ; } +memcpy (buf, nct6694->data_buf, data_len); +return nct6694_response_err_handling (nct6694, msg->response_header.sts); }EXPORT_SYMBOL_GPL (nct6694_read_msg);
@@ -154,12 +160,17 @@ int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header * {union nct6694_usb_msg *msg = nct6694->usb_msg;struct usb_device *udev = nct6694->udev; +u16 data_len =le16_to_cpu (cmd_hd->len);int tx_len, rx_len, ret; +if (data_len >NCT6694_MAX_DATA_LEN ) +return -EINVAL ; +guard (mutex)(&nct6694->access_lock);memcpy (&msg->cmd_header, cmd_hd,sizeof (*cmd_hd)); msg->cmd_header.hctrl =NCT6694_HCTRL_SET ; +memcpy (nct6694->data_buf, buf, data_len);/* Send command packet to USB device */ ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, NCT6694_BULK_OUT_EP), &msg->cmd_header,
@@ -168,8 +179,8 @@ int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header *return ret;/* Send data packet to USB device */ - ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), buf, -le16_to_cpu (cmd_hd->len), &tx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), nct6694->data_buf, + data_len, &tx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;
@@ -186,17 +197,19 @@ int nct6694_write_msg(struct nct6694 *nct6694, const struct nct6694_cmd_header * }/* 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 ); + ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), nct6694->data_buf, + data_len, &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret; -if (rx_len !=le16_to_cpu (cmd_hd->len)) { +if (rx_len != data_len) {dev_err (nct6694->dev,"Expected transmitted length %d, but got %d\n" , -le16_to_cpu (cmd_hd->len), rx_len); + data_len, rx_len);return -EIO ; } +memcpy (buf, nct6694->data_buf, data_len); +return nct6694_response_err_handling (nct6694, msg->response_header.sts); }EXPORT_SYMBOL_GPL (nct6694_write_msg);
@@ -315,6 +328,10 @@ static int nct6694_usb_probe(struct usb_interface *iface,if (!nct6694->usb_msg)return -ENOMEM ; + nct6694->data_buf =devm_kzalloc (dev,NCT6694_MAX_DATA_LEN ,GFP_KERNEL ); +if (!nct6694->data_buf) +return -ENOMEM ; + nct6694->int_buffer =devm_kzalloc (dev,sizeof (*nct6694->int_buffer),GFP_KERNEL );if (!nct6694->int_buffer)return -ENOMEM ;
include/linux/mfd/nct6694.h +4 -0
@@ -19,6 +19,9 @@#define NCT6694_URB_TIMEOUT 1000 +/* Maximum data packet length the firmware accepts in a single command */ +#define NCT6694_MAX_DATA_LEN 0x3F0 +enum nct6694_irq_id {NCT6694_IRQ_GPIO0 =0 ,NCT6694_IRQ_GPIO1 ,
@@ -92,6 +95,7 @@ struct nct6694 {struct urb *int_in_urb;struct usb_device *udev;union nct6694_usb_msg *usb_msg; +void *data_buf;__le32 *int_buffer;unsigned int irq_enable; };
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 05/13] mfd: nct6694: Transfer data packets via a dedicated buffer
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Ming Yu <[email protected]>
The caller's buffer is passed straight to
usb_bulk_msg(). Sub-device drivers embed those buffers in their private data, so they are neither cacheline aligned nor exclusively owned by the transfer, and mapping them for DMA can corrupt adjacent fields on non-coherent architectures.
Quoted 5 lines
Transfer the data packets through a buffer owned by the transport, and reject commands exceeding the maximum data length.
Fixes 51dad33ede63 ("mfd: Add core driver for Nuvoton NCT6694")Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v8 6/13 mfd: nct6694: Move module type macros to shared header
In reply to [email protected]From: Ming Yu <[email protected]>
Each sub-device driver defines the module type used by its own commands. The upcoming HIF transport also needs to recognise NCT6694_RPT_MOD, as report accesses are served from a separate register space rather than through the command mailbox.
Move all module types into the shared header so that the transports and the sub-device drivers use a single set of definitions.
No functional change.
---
Changes in v8:
- Reworded the commit message to explain why the transport needs the module types.
Changes in v7:
- None.
Changes in v6:
- None.
Changes in v5:
- Split from the monolithic v4 patch to follow the single logical change principle.
drivers/gpio/gpio-nct6694.c | 7 ------- drivers/hwmon/nct6694-hwmon.c | 21 --------------------- drivers/i2c/busses/i2c-nct6694.c | 7 ------- 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 | 9 +++++++++ 7 files changed, 9 insertions(+), 55 deletions(-)
drivers/gpio/gpio-nct6694.c +0 -7
@@ -13,13 +13,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
@@ -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
@@ -12,13 +12,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/net/can/usb/nct6694_canfd.c +0 -6
@@ -18,12 +18,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
@@ -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
@@ -20,13 +20,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 +9 -0
@@ -8,6 +8,15 @@#ifndef __MFD_NCT6694_H#define __MFD_NCT6694_H +#define NCT6694_HWMON_MOD 0x00 +#define NCT6694_PWM_MOD 0x01 +#define NCT6694_I2C_MOD 0x03 +#define NCT6694_CANFD_MOD 0x05 +#define NCT6694_WDT_MOD 0x07 +#define NCT6694_RTC_MOD 0x08 +#define NCT6694_RPT_MOD 0xFF +#define NCT6694_GPIO_MOD NCT6694_RPT_MOD +#define NCT6694_VENDOR_ID 0x0416#define NCT6694_PRODUCT_ID 0x200B#define NCT6694_INT_IN_EP 0x01
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 06/13] mfd: nct6694: Move module type macros to shared header
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Ming Yu <[email protected]>
Each sub-device driver defines the module type used by its own commands. The upcoming HIF transport also needs to recognise
NCT6694_RPT_MOD, as report accesses are served from a separate register space rather than through the command mailbox.
Quoted 6 lines
Move all module types into the shared header so that the transports and the sub-device drivers use a single set of definitions.
No functional change.
Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v8 06/13] mfd: nct6694: Move module type macros to shared header
In reply to [email protected]Hi Ming,
Quoted 7 lines · On Wed, Oct 07, 2026 at 05:20:55PM +0800, [email protected] wrote:
From: Ming Yu <[email protected]>
Each sub-device driver defines the module type used by its own commands. The upcoming HIF transport also needs to recognise
NCT6694_RPT_MOD, as report accesses are served from a separate register space rather than through the command mailbox.
Quoted 6 lines
Move all module types into the shared header so that the transports and the sub-device drivers use a single set of definitions.
No functional change.
Signed-off-by Ming Yu <[email protected]>
Thanks,
Andi
PATCH v8 7/13 mfd: nct6694: Refactor USB-specific data into nct6694_usb_data
In reply to [email protected]From: Ming Yu <[email protected]>
Separate the USB transport fields from the core nct6694 structure into a new nct6694_usb_data structure, which embeds struct nct6694 and is retrieved with container_of(). USB-only definitions are moved from the shared header into the USB driver.
While at it, use consistent names for the USB device, the buffers and the core structure.
No functional change.
---
Changes in v8:
- Embedded struct nct6694 in nct6694_usb_data and dropped the priv pointer.
- Renamed udev to usb_dev, udata to usb_data, usb_msg to hdr_buf, int_buffer to int_buf, int_in_urb to int_urb, and nct6694 to ddata.
Changes in v7:
- None.
Changes in v6:
- None.
Changes in v5:
- Split from the monolithic v4 patch to follow the single logical change principle.
drivers/mfd/nct6694.c | 243 +++++++++++++++++++++--------------- include/linux/mfd/nct6694.h | 26 +--- 2 files changed, 143 insertions(+), 126 deletions(-)
drivers/mfd/nct6694.c +139 -104
@@ -22,6 +22,34 @@#include <linux/spinlock.h> #include <linux/usb.h> +#define NCT6694_VENDOR_ID 0x0416 +#define NCT6694_PRODUCT_ID 0x200B +#define NCT6694_INT_IN_EP 0x01 +#define NCT6694_BULK_IN_EP 0x02 +#define NCT6694_BULK_OUT_EP 0x03 + +#define NCT6694_URB_TIMEOUT 1000 + +union __packed nct6694_usb_hdr { +struct nct6694_cmd_header cmd_header; +struct nct6694_response_header response_header; +}; + +struct nct6694_usb_data { +struct nct6694 core; +struct mutex access_lock; +struct usb_device *usb_dev; +struct urb *int_urb; +union nct6694_usb_hdr *hdr_buf; +void *data_buf; +__le32 *int_buf; +}; + +static struct nct6694_usb_data *to_nct6694_usb_data (struct nct6694 *ddata) +{ + return container_of(ddata, struct nct6694_usb_data, core); +} +static const struct mfd_cell nct6694_devs[] = {MFD_CELL_NAME ("nct6694-gpio" ),MFD_CELL_NAME ("nct6694-gpio" ),
@@ -58,22 +86,22 @@ static const struct mfd_cell nct6694_devs[] = { MFD_CELL_NAME("nct6694-rtc"), }; -static int nct6694_response_err_handling (struct nct6694 *nct6694,unsigned char err_status) +static int nct6694_response_err_handling (struct nct6694 *ddata,unsigned char err_status) {switch (err_status) {case NCT6694_NO_ERROR :return 0 ;case NCT6694_NOT_SUPPORT_ERROR : -dev_err (nct6694->dev,"Command is not supported!\n" ); +dev_err (ddata->dev,"Command is not supported!\n" );break ;case NCT6694_NO_RESPONSE_ERROR : -dev_warn (nct6694->dev,"Command received no response!\n" ); +dev_warn (ddata->dev,"Command received no response!\n" );break ;case NCT6694_TIMEOUT_ERROR : -dev_warn (nct6694->dev,"Command timed out!\n" ); +dev_warn (ddata->dev,"Command timed out!\n" );break ;case NCT6694_PENDING : -dev_err (nct6694->dev,"Command is pending!\n" ); +dev_err (ddata->dev,"Command is pending!\n" );break ;default :return -EINVAL ;
@@ -84,7 +112,7 @@ static int nct6694_response_err_handling(struct nct6694 *nct6694, unsigned char/** * nct6694_read_msg() - Read message from NCT6694 device -* @nct6694: NCT6694 device pointer +* @ddata: NCT6694 device pointer * @cmd_hd: command header structure * @buf: buffer to store the response data *
@@ -94,60 +122,61 @@ static int nct6694_response_err_handling(struct nct6694 *nct6694, unsigned char* * Return: Negative value on error or 0 on success. */ -int nct6694_read_msg (struct nct6694 *nct6694,const struct nct6694_cmd_header *cmd_hd,void *buf) +int nct6694_read_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf) { -union nct6694_usb_msg *msg = nct6694->usb_msg; -struct usb_device *udev = nct6694->udev; +struct nct6694_usb_data *usb_data =to_nct6694_usb_data (ddata); +union nct6694_usb_hdr *hdr = usb_data->hdr_buf; +struct usb_device *usb_dev = usb_data->usb_dev;u16 data_len =le16_to_cpu (cmd_hd->len);int tx_len, rx_len, ret;if (data_len >NCT6694_MAX_DATA_LEN )return -EINVAL ; -guard (mutex)(&nct6694->access_lock); +guard (mutex)(&usb_data->access_lock); -memcpy (&msg->cmd_header, cmd_hd,sizeof (*cmd_hd)); - msg->cmd_header.hctrl =NCT6694_HCTRL_GET ; +memcpy (&hdr->cmd_header, cmd_hd,sizeof (*cmd_hd)); + hdr->cmd_header.hctrl =NCT6694_HCTRL_GET ;/* Send command packet to USB device */ - ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), &msg->cmd_header, -sizeof (*msg), &tx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_sndbulkpipe (usb_dev,NCT6694_BULK_OUT_EP ), + &hdr->cmd_header,sizeof (*hdr), &tx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;/* Receive response packet from USB device */ - ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), &msg->response_header, -sizeof (*msg), &rx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_rcvbulkpipe (usb_dev,NCT6694_BULK_IN_EP ), + &hdr->response_header,sizeof (*hdr), &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret; -if (rx_len !=sizeof (msg->response_header)) { -dev_err (nct6694->dev,"Expected response length %zu, but got %d\n" , -sizeof (msg->response_header), rx_len); +if (rx_len !=sizeof (hdr->response_header)) { +dev_err (ddata->dev,"Expected response length %zu, but got %d\n" , +sizeof (hdr->response_header), rx_len);return -EIO ; }/* Receive data packet from USB device */ - ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), nct6694->data_buf, - data_len, &rx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_rcvbulkpipe (usb_dev,NCT6694_BULK_IN_EP ), + usb_data->data_buf, data_len, &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;if (rx_len != data_len) { -dev_err (nct6694->dev,"Expected received length %d, but got %d\n" , +dev_err (ddata->dev,"Expected received length %d, but got %d\n" , data_len, rx_len);return -EIO ; } -memcpy (buf, nct6694->data_buf, data_len); +memcpy (buf, usb_data->data_buf, data_len); -return nct6694_response_err_handling (nct6694, msg->response_header.sts); +return nct6694_response_err_handling (ddata, hdr->response_header.sts); }EXPORT_SYMBOL_GPL (nct6694_read_msg);/** * nct6694_write_msg() - Write message to NCT6694 device -* @nct6694: NCT6694 device pointer +* @ddata: NCT6694 device pointer * @cmd_hd: command header structure * @buf: buffer containing the data to be sent *
@@ -156,67 +185,68 @@ EXPORT_SYMBOL_GPL(nct6694_read_msg);* * Return: Negative value on error or 0 on success. */ -int nct6694_write_msg (struct nct6694 *nct6694,const struct nct6694_cmd_header *cmd_hd,void *buf) +int nct6694_write_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf) { -union nct6694_usb_msg *msg = nct6694->usb_msg; -struct usb_device *udev = nct6694->udev; +struct nct6694_usb_data *usb_data =to_nct6694_usb_data (ddata); +union nct6694_usb_hdr *hdr = usb_data->hdr_buf; +struct usb_device *usb_dev = usb_data->usb_dev;u16 data_len =le16_to_cpu (cmd_hd->len);int tx_len, rx_len, ret;if (data_len >NCT6694_MAX_DATA_LEN )return -EINVAL ; -guard (mutex)(&nct6694->access_lock); +guard (mutex)(&usb_data->access_lock); -memcpy (&msg->cmd_header, cmd_hd,sizeof (*cmd_hd)); - msg->cmd_header.hctrl =NCT6694_HCTRL_SET ; -memcpy (nct6694->data_buf, buf, data_len); +memcpy (&hdr->cmd_header, cmd_hd,sizeof (*cmd_hd)); + hdr->cmd_header.hctrl =NCT6694_HCTRL_SET ; +memcpy (usb_data->data_buf, buf, data_len);/* Send command packet to USB device */ - ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), &msg->cmd_header, -sizeof (*msg), &tx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_sndbulkpipe (usb_dev,NCT6694_BULK_OUT_EP ), + &hdr->cmd_header,sizeof (*hdr), &tx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;/* Send data packet to USB device */ - ret =usb_bulk_msg (udev,usb_sndbulkpipe (udev,NCT6694_BULK_OUT_EP ), nct6694->data_buf, - data_len, &tx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_sndbulkpipe (usb_dev,NCT6694_BULK_OUT_EP ), + usb_data->data_buf, data_len, &tx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;/* Receive response packet from USB device */ - ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), &msg->response_header, -sizeof (*msg), &rx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_rcvbulkpipe (usb_dev,NCT6694_BULK_IN_EP ), + &hdr->response_header,sizeof (*hdr), &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret; -if (rx_len !=sizeof (msg->response_header)) { -dev_err (nct6694->dev,"Expected response length %zu, but got %d\n" , -sizeof (msg->response_header), rx_len); +if (rx_len !=sizeof (hdr->response_header)) { +dev_err (ddata->dev,"Expected response length %zu, but got %d\n" , +sizeof (hdr->response_header), rx_len);return -EIO ; }/* Receive data packet from USB device */ - ret =usb_bulk_msg (udev,usb_rcvbulkpipe (udev,NCT6694_BULK_IN_EP ), nct6694->data_buf, - data_len, &rx_len,NCT6694_URB_TIMEOUT ); + ret =usb_bulk_msg (usb_dev,usb_rcvbulkpipe (usb_dev,NCT6694_BULK_IN_EP ), + usb_data->data_buf, data_len, &rx_len,NCT6694_URB_TIMEOUT );if (ret)return ret;if (rx_len != data_len) { -dev_err (nct6694->dev,"Expected transmitted length %d, but got %d\n" , +dev_err (ddata->dev,"Expected transmitted length %d, but got %d\n" , data_len, rx_len);return -EIO ; } -memcpy (buf, nct6694->data_buf, data_len); +memcpy (buf, usb_data->data_buf, data_len); -return nct6694_response_err_handling (nct6694, msg->response_header.sts); +return nct6694_response_err_handling (ddata, hdr->response_header.sts); }EXPORT_SYMBOL_GPL (nct6694_write_msg);static void usb_int_callback (struct urb *urb) { -struct nct6694 *nct6694 = urb->context; +struct nct6694 *ddata = urb->context;__le32 *status_le = urb->transfer_buffer;u32 int_status;int ret;
@@ -240,34 +270,34 @@ static void usb_int_callback(struct urb *urb)while (int_status) {int irq =__ffs (int_status); -generic_handle_domain_irq_safe (nct6694->domain, irq); +generic_handle_domain_irq_safe (ddata->domain, irq); int_status &= ~BIT (irq); } resubmit: ret =usb_submit_urb (urb,GFP_ATOMIC );if (ret) -dev_warn (nct6694->dev,"Failed to resubmit urb, status %pe" ,ERR_PTR (ret)); +dev_warn (ddata->dev,"Failed to resubmit urb, status %pe" ,ERR_PTR (ret)); }static void nct6694_irq_enable (struct irq_data *data) { -struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data); +struct nct6694 *ddata =irq_data_get_irq_chip_data (data);irq_hw_number_t hwirq =irqd_to_hwirq (data); -guard (spinlock_irqsave)(&nct6694->irq_lock); +guard (spinlock_irqsave)(&ddata->irq_lock); - nct6694->irq_enable |=BIT (hwirq); + ddata->irq_enable |=BIT (hwirq); }static void nct6694_irq_disable (struct irq_data *data) { -struct nct6694 *nct6694 =irq_data_get_irq_chip_data (data); +struct nct6694 *ddata =irq_data_get_irq_chip_data (data);irq_hw_number_t hwirq =irqd_to_hwirq (data); -guard (spinlock_irqsave)(&nct6694->irq_lock); +guard (spinlock_irqsave)(&ddata->irq_lock); - nct6694->irq_enable &= ~BIT (hwirq); + ddata->irq_enable &= ~BIT (hwirq); }static const struct irq_chip nct6694_irq_chip = {
@@ -279,9 +309,9 @@ static const struct irq_chip nct6694_irq_chip = {static int nct6694_irq_domain_map (struct irq_domain *d,unsigned int irq,irq_hw_number_t hw) { -struct nct6694 *nct6694 = d->host_data; +struct nct6694 *ddata = d->host_data; -irq_set_chip_data (irq, nct6694); +irq_set_chip_data (irq, ddata);irq_set_chip_and_handler (irq, &nct6694_irq_chip, handle_simple_irq);return 0 ;
@@ -310,9 +340,10 @@ static int nct6694_usb_probe(struct usb_interface *iface,USB_DIR_IN |NCT6694_INT_IN_EP , 0 }; -struct usb_device *udev =interface_to_usbdev (iface); +struct usb_device *usb_dev =interface_to_usbdev (iface); +struct nct6694_usb_data *usb_data;struct device *dev = &iface->dev; -struct nct6694 *nct6694; +struct nct6694 *ddata;unsigned int int_pipe;int ret;
@@ -320,58 +351,61 @@ static int nct6694_usb_probe(struct usb_interface *iface, !usb_check_int_endpoints(iface, int_ep_addr))return -ENODEV ; - nct6694 =devm_kzalloc (dev,sizeof (*nct6694),GFP_KERNEL ); -if (!nct6694) + usb_data =devm_kzalloc (dev,sizeof (*usb_data),GFP_KERNEL ); +if (!usb_data)return -ENOMEM ; - nct6694->usb_msg =devm_kzalloc (dev,sizeof (union nct6694_usb_msg ),GFP_KERNEL ); -if (!nct6694->usb_msg) + usb_data->hdr_buf =devm_kzalloc (dev,sizeof (*usb_data->hdr_buf),GFP_KERNEL ); +if (!usb_data->hdr_buf)return -ENOMEM ; - nct6694->data_buf =devm_kzalloc (dev,NCT6694_MAX_DATA_LEN ,GFP_KERNEL ); -if (!nct6694->data_buf) + usb_data->data_buf =devm_kzalloc (dev,NCT6694_MAX_DATA_LEN ,GFP_KERNEL ); +if (!usb_data->data_buf)return -ENOMEM ; - nct6694->int_buffer =devm_kzalloc (dev,sizeof (*nct6694->int_buffer),GFP_KERNEL ); -if (!nct6694->int_buffer) + usb_data->int_buf =devm_kzalloc (dev,sizeof (*usb_data->int_buf),GFP_KERNEL ); +if (!usb_data->int_buf)return -ENOMEM ; - nct6694->int_in_urb =usb_alloc_urb (0 ,GFP_KERNEL ); -if (!nct6694->int_in_urb) + usb_data->int_urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!usb_data->int_urb)return -ENOMEM ; - nct6694->domain =irq_domain_create_simple (NULL ,NCT6694_NR_IRQS ,0 , - &nct6694_irq_domain_ops, - nct6694); -if (!nct6694->domain) { + ddata = &usb_data->core; + + ddata->domain =irq_domain_create_simple (NULL ,NCT6694_NR_IRQS ,0 , + &nct6694_irq_domain_ops, + ddata); +if (!ddata->domain) { ret = -ENODEV ; goto err_urb; } - nct6694->dev = dev; - nct6694->udev = udev; + ddata->dev = dev; + usb_data->usb_dev = usb_dev; -ida_init (&nct6694->gpio_ida); -ida_init (&nct6694->i2c_ida); -ida_init (&nct6694->canfd_ida); -ida_init (&nct6694->wdt_ida); +ida_init (&ddata->gpio_ida); +ida_init (&ddata->i2c_ida); +ida_init (&ddata->canfd_ida); +ida_init (&ddata->wdt_ida); -spin_lock_init (&nct6694->irq_lock); +spin_lock_init (&ddata->irq_lock); - ret =devm_mutex_init (dev, &nct6694->access_lock); + ret =devm_mutex_init (dev, &usb_data->access_lock);if (ret) goto err_ida; - int_pipe =usb_rcvintpipe (udev,NCT6694_INT_IN_EP ); -usb_fill_int_urb (nct6694->int_in_urb, udev, int_pipe, - nct6694->int_buffer,sizeof (*nct6694->int_buffer), usb_int_callback, - nct6694,usb_pipe_endpoint (udev, int_pipe)->desc.bInterval); + int_pipe =usb_rcvintpipe (usb_dev,NCT6694_INT_IN_EP ); +usb_fill_int_urb (usb_data->int_urb, usb_dev, int_pipe, + usb_data->int_buf,sizeof (*usb_data->int_buf), + usb_int_callback, ddata, +usb_pipe_endpoint (usb_dev, int_pipe)->desc.bInterval); - ret =usb_submit_urb (nct6694->int_in_urb,GFP_KERNEL ); + ret =usb_submit_urb (usb_data->int_urb,GFP_KERNEL );if (ret) goto err_ida; -usb_set_intfdata (iface, nct6694); +usb_set_intfdata (iface, ddata); ret =mfd_add_hotplug_devices (dev, nct6694_devs,ARRAY_SIZE (nct6694_devs));if (ret)
@@ -380,30 +414,31 @@ static int nct6694_usb_probe(struct usb_interface *iface,return 0 ; err_mfd: -usb_kill_urb (nct6694->int_in_urb); +usb_kill_urb (usb_data->int_urb); err_ida: -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); +ida_destroy (&ddata->wdt_ida); +ida_destroy (&ddata->canfd_ida); +ida_destroy (&ddata->i2c_ida); +ida_destroy (&ddata->gpio_ida); +irq_domain_remove (ddata->domain); err_urb: -usb_free_urb (nct6694->int_in_urb); +usb_free_urb (usb_data->int_urb);return ret; }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); -usb_free_urb (nct6694->int_in_urb); +struct nct6694 *ddata =usb_get_intfdata (iface); +struct nct6694_usb_data *usb_data =to_nct6694_usb_data (ddata); + +mfd_remove_devices (ddata->dev); +usb_kill_urb (usb_data->int_urb); +ida_destroy (&ddata->wdt_ida); +ida_destroy (&ddata->canfd_ida); +ida_destroy (&ddata->i2c_ida); +ida_destroy (&ddata->gpio_ida); +irq_domain_remove (ddata->domain); +usb_free_urb (usb_data->int_urb); }static const struct usb_device_id nct6694_ids[] = {
include/linux/mfd/nct6694.h +4 -22
@@ -2,7 +2,8 @@/* * Copyright (C) 2025 Nuvoton Technology Corp. * -* Nuvoton NCT6694 USB transaction and data structure. +* Nuvoton NCT6694 core definitions shared by the transport driver +* and the sub-device drivers. */ #ifndef __MFD_NCT6694_H
@@ -17,17 +18,9 @@#define NCT6694_RPT_MOD 0xFF#define NCT6694_GPIO_MOD NCT6694_RPT_MOD -#define NCT6694_VENDOR_ID 0x0416 -#define NCT6694_PRODUCT_ID 0x200B -#define NCT6694_INT_IN_EP 0x01 -#define NCT6694_BULK_IN_EP 0x02 -#define NCT6694_BULK_OUT_EP 0x03 -#define NCT6694_HCTRL_SET 0x40#define NCT6694_HCTRL_GET 0x80 -#define NCT6694_URB_TIMEOUT 1000 -/* Maximum data packet length the firmware accepts in a single command */ #define NCT6694_MAX_DATA_LEN 0x3F0
@@ -87,11 +80,6 @@ struct __packed nct6694_response_header {__le16 len; }; -union __packed nct6694_usb_msg { -struct nct6694_cmd_header cmd_header; -struct nct6694_response_header response_header; -}; -struct nct6694 {struct device *dev;struct ida gpio_ida;
@@ -99,17 +87,11 @@ struct nct6694 {struct ida canfd_ida;struct ida wdt_ida;struct irq_domain *domain; -struct mutex access_lock;spinlock_t irq_lock; -struct urb *int_in_urb; -struct usb_device *udev; -union nct6694_usb_msg *usb_msg; -void *data_buf; -__le32 *int_buffer; unsigned int irq_enable; }; -int nct6694_read_msg (struct nct6694 *nct6694,const struct nct6694_cmd_header *cmd_hd,void *buf); -int nct6694_write_msg (struct nct6694 *nct6694,const struct nct6694_cmd_header *cmd_hd,void *buf); +int nct6694_read_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf); +int nct6694_write_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf);#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 07/13] mfd: nct6694: Refactor USB-specific data into nct6694_usb_data
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Ming Yu <[email protected]>
Separate the USB transport fields from the core nct6694 structure into a new
nct6694_usb_datastructure, which embeds struct nct6694 and is retrieved withcontainer_of(). USB-only definitions are moved from the shared header into the USB driver.
Quoted 6 lines
While at it, use consistent names for the USB device, the buffers and the core structure.
No functional change.
Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v8 8/13 mfd: nct6694: Rename USB transport functions with _usb_ prefix
In reply to [email protected]From: Ming Yu <[email protected]>
Add a _usb_ prefix to the USB transport functions, tables and driver name to mark them as USB specific, in preparation for the core and HIF transport added in later patches.
Keep nct6694_{read,write}_msg() as static inline wrappers in the shared header so that the sub-device drivers remain untouched; they are removed once the regmap-based transport abstraction is in place.
No functional change.
---
Changes in v8:
- None.
Changes in v7:
- None.
Changes in v6:
- New patch replacing the v5 function-pointer transport abstraction:
rename the exported I/O functions with an _usb_ prefix and add transitional inline nct6694_{read,write}_msg() wrappers in the shared header so sub-device drivers stay untouched in this commit.
drivers/mfd/nct6694.c | 40 ++++++++++++++++++++----------------- include/linux/mfd/nct6694.h | 22 ++++++++++++++++++-- 2 files changed, 42 insertions(+), 20 deletions(-)
drivers/mfd/nct6694.c +22 -18
@@ -50,7 +50,7 @@ static struct nct6694_usb_data *to_nct6694_usb_data(struct nct6694 *ddata) return container_of(ddata, struct nct6694_usb_data, core); } -static const struct mfd_cell nct6694_devs[] = { +static const struct mfd_cell nct6694_usb_devs[] = {MFD_CELL_NAME ("nct6694-gpio" ),MFD_CELL_NAME ("nct6694-gpio" ),MFD_CELL_NAME ("nct6694-gpio" ),
@@ -86,7 +86,7 @@ static const struct mfd_cell nct6694_devs[] = { MFD_CELL_NAME("nct6694-rtc"), }; -static int nct6694_response_err_handling (struct nct6694 *ddata,unsigned char err_status) +static int nct6694_usb_err_handling (struct nct6694 *ddata,unsigned char err_status) {switch (err_status) {case NCT6694_NO_ERROR :
@@ -111,7 +111,7 @@ static int nct6694_response_err_handling(struct nct6694 *ddata, unsigned char er }/** -* nct6694_read_msg() - Read message from NCT6694 device +* nct6694_usb_read_msg() - Read message from NCT6694 device * @ddata: NCT6694 device pointer * @cmd_hd: command header structure * @buf: buffer to store the response data
@@ -122,7 +122,9 @@ static int nct6694_response_err_handling(struct nct6694 *ddata, unsigned char er* * Return: Negative value on error or 0 on success. */ -int nct6694_read_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf) +int nct6694_usb_read_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) {struct nct6694_usb_data *usb_data =to_nct6694_usb_data (ddata);union nct6694_usb_hdr *hdr = usb_data->hdr_buf;
@@ -170,12 +172,12 @@ int nct6694_read_msg(struct nct6694 *ddata, const struct nct6694_cmd_header *cmdmemcpy (buf, usb_data->data_buf, data_len); -return nct6694_response_err_handling (ddata, hdr->response_header.sts); +return nct6694_usb_err_handling (ddata, hdr->response_header.sts); } -EXPORT_SYMBOL_GPL (nct6694_read_msg); +EXPORT_SYMBOL_GPL (nct6694_usb_read_msg);/** -* nct6694_write_msg() - Write message to NCT6694 device +* nct6694_usb_write_msg() - Write message to NCT6694 device * @ddata: NCT6694 device pointer * @cmd_hd: command header structure * @buf: buffer containing the data to be sent
@@ -185,7 +187,9 @@ EXPORT_SYMBOL_GPL(nct6694_read_msg);* * Return: Negative value on error or 0 on success. */ -int nct6694_write_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf) +int nct6694_usb_write_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) {struct nct6694_usb_data *usb_data =to_nct6694_usb_data (ddata);union nct6694_usb_hdr *hdr = usb_data->hdr_buf;
@@ -240,11 +244,11 @@ int nct6694_write_msg(struct nct6694 *ddata, const struct nct6694_cmd_header *cmmemcpy (buf, usb_data->data_buf, data_len); -return nct6694_response_err_handling (ddata, hdr->response_header.sts); +return nct6694_usb_err_handling (ddata, hdr->response_header.sts); } -EXPORT_SYMBOL_GPL (nct6694_write_msg); +EXPORT_SYMBOL_GPL (nct6694_usb_write_msg); -static void usb_int_callback (struct urb *urb) +static void nct6694_usb_int_callback (struct urb *urb) {struct nct6694 *ddata = urb->context;__le32 *status_le = urb->transfer_buffer;
@@ -398,7 +402,7 @@ static int nct6694_usb_probe(struct usb_interface *iface, int_pipe =usb_rcvintpipe (usb_dev,NCT6694_INT_IN_EP );usb_fill_int_urb (usb_data->int_urb, usb_dev, int_pipe, usb_data->int_buf,sizeof (*usb_data->int_buf), - usb_int_callback, ddata, + nct6694_usb_int_callback, ddata,usb_pipe_endpoint (usb_dev, int_pipe)->desc.bInterval); ret =usb_submit_urb (usb_data->int_urb,GFP_KERNEL );
@@ -407,7 +411,7 @@ static int nct6694_usb_probe(struct usb_interface *iface,usb_set_intfdata (iface, ddata); - ret =mfd_add_hotplug_devices (dev, nct6694_devs,ARRAY_SIZE (nct6694_devs)); + ret =mfd_add_hotplug_devices (dev, nct6694_usb_devs,ARRAY_SIZE (nct6694_usb_devs));if (ret) goto err_mfd;
@@ -441,20 +445,20 @@ static void nct6694_usb_disconnect(struct usb_interface *iface)usb_free_urb (usb_data->int_urb); } -static const struct usb_device_id nct6694_ids[] = { +static const struct usb_device_id nct6694_usb_ids[] = { {USB_DEVICE_AND_INTERFACE_INFO (NCT6694_VENDOR_ID ,NCT6694_PRODUCT_ID ,0xFF ,0x00 ,0x00 ) }, { } }; -MODULE_DEVICE_TABLE (usb, nct6694_ids); +MODULE_DEVICE_TABLE (usb, nct6694_usb_ids);static struct usb_driver nct6694_usb_driver = { - .name ="nct6694" , - .id_table = nct6694_ids, + .name ="nct6694-usb" , + .id_table = nct6694_usb_ids, .probe = nct6694_usb_probe, .disconnect = nct6694_usb_disconnect, };module_usb_driver (nct6694_usb_driver); -MODULE_DESCRIPTION ("Nuvoton NCT6694 core driver" ); +MODULE_DESCRIPTION ("Nuvoton NCT6694 USB transport driver" );MODULE_AUTHOR ("Ming Yu <[email protected]>" );MODULE_LICENSE ("GPL" );
include/linux/mfd/nct6694.h +20 -2
@@ -91,7 +91,25 @@ struct nct6694 { unsigned int irq_enable; }; -int nct6694_read_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf); -int nct6694_write_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf); +int nct6694_usb_read_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf); +int nct6694_usb_write_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf); + +static inline int nct6694_read_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) +{ +return nct6694_usb_read_msg (ddata, cmd_hd, buf); +} + +static inline int nct6694_write_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) +{ +return nct6694_usb_write_msg (ddata, cmd_hd, buf); +}#endif
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 08/13] mfd: nct6694: Rename USB transport functions with _usb_ prefix
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] mfd: commit message and code mismatch regarding bespoke accessors - [Low] mfd: nct6694…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] mfd: commit message and code mismatch regarding bespoke accessors
- [Low] mfd: nct6694: driver-specific header placed in global include directory --
commit e54fbe76bc141f2f5c4c0b850b0ed14aa9497ce0
Author: Ming Yu <[email protected]>
mfd: nct6694: Rename USB transport functions with _usb_ prefix
Add a _usb_ prefix to the USB transport functions, tables and driver name to mark them as USB specific, in preparation for the core and HIF transport added in later patches.
Quoted 3 lines
Keep nct6694_{read,write}_msg() as static inline wrappers in the shared header so that the sub-device drivers remain untouched; they are removed once the regmap-based transport abstraction is in place.
[Severity: Medium]
The commit message mentions that these inline wrappers will be removed once the regmap-based transport abstraction is in place. However, it appears that in later patches (e.g., patch 11), these wrappers are modified to call regmap APIs instead of being removed, leaving sub-device drivers still using them.
Since the MFD subsystem guidelines prohibit bespoke accessors and prefer standard APIs like regmap to be used directly by child drivers, could these wrappers be fully removed as promised, or should the commit message be updated if they are intended to remain?
include/linux/mfd/nct6694.h struct nct6694 { · line 92
Quoted 7 lines
include/linux/mfd/nct6694.h +0
-0@@ -91,7 +91,25 @@ struct nct6694 {unsigned int irq_enable; };
[Severity: Low]
Does this driver-specific header belong in the global include/linux/mfd/ directory?
The MFD subsystem guidelines state that headers should remain local to drivers/mfd/ if they are only used by the parent and its immediate children. Exposing parent-initialized private resources like the ida structures here promotes tight coupling between the parent and child drivers.
[ ... ]
include/linux/mfd/nct6694.h struct nct6694 { · line 113
Quoted 13 lines
include/linux/mfd/nct6694.h +13
-0+static inline int nct6694_read_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) +{ +return nct6694_usb_read_msg (ddata, cmd_hd, buf); +} + +static inline int nct6694_write_msg (struct nct6694 *ddata, +const struct nct6694_cmd_header *cmd_hd, +void *buf) +{ +return nct6694_usb_write_msg (ddata, cmd_hd, buf); +}
[Severity: Low]
Is it necessary to provide these bespoke parent-child accessors in a global header?
The MFD guidelines prefer child drivers to use standard APIs like regmap directly to avoid tight coupling between the parent and child. Could the sub-devices be updated to use standard abstractions instead?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v8 9/13 mfd: nct6694: Rename driver to nct6694-usb
In reply to [email protected]From: Ming Yu <[email protected]>
Rename nct6694.c to nct6694-usb.c to reflect that it implements the USB transport, in preparation for the core and HIF transport added in later patches. The module is renamed to nct6694-usb accordingly.
No functional change.
---
Changes in v8:
- Only rename the file; the Kconfig split moves to the patch that introduces nct6694-core.c.
Changes in v7:
- None.
Changes in v6:
- None.
Changes in v5:
- Split from the monolithic v4 patch to follow the single logical change principle.
MAINTAINERS | 2 +-
drivers/mfd/Makefile | 2 +-
drivers/mfd/{nct6694.c => nct6694-usb.c} | 0
3 files changed, 2 insertions(+), 2 deletions(-)
rename drivers/mfd/{nct6694.c => nct6694-usb.c} (100%)MAINTAINERS +1 -1
@@ -19539,7 +19539,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.c +F: drivers/mfd/nct6694-usb.c F: drivers/net/can/usb/nct6694_canfd.c F: drivers/rtc/rtc-nct6694.c F: drivers/watchdog/nct6694_wdt.c
drivers/mfd/Makefile +1 -1
@@ -124,7 +124,7 @@ obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o obj-$(CONFIG_MFD_PF1550) += pf1550.o -obj-$(CONFIG_MFD_NCT6694) += nct6694.o +obj-$(CONFIG_MFD_NCT6694) += nct6694-usb.o obj-$(CONFIG_MFD_CORE) += mfd-core.o
drivers/mfd/nct6694-usb.c renamed from drivers/mfd/nct6694.c +0 -0
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 09/13] mfd: nct6694: Rename driver to nct6694-usb
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines
From: Ming Yu <[email protected]>
Rename
nct6694.ctonct6694-usb.cto reflect that it implements the USB transport, in preparation for the core and HIF transport added in later patches. The module is renamed to nct6694-usb accordingly.No functional change.
Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v8 10/13 mfd: nct6694: Extract core device management into a separate module
In reply to [email protected]From: Ming Yu <[email protected]>
Move the IRQ domain and IDA handling, which do not depend on the transport, from the USB driver into a new nct6694-core module. The core retrieves struct nct6694 from the driver data of the transport device, and the transport keeps registering its own MFD cells.
The USB driver now creates the IRQ domain before submitting the interrupt URB, and kills the URB before the domain is removed.
No functional change.
---
Changes in v8:
- Pass only the transport device to the core and retrieve struct nct6694 from its driver data.
- Let the transport register its own MFD cells.
- Rename nct6694_core_{probe,remove}() to nct6694_device_{init,exit}().
- Return -ENOMEM when the IRQ domain cannot be created.
- Folded in the v7 patch "Submit the interrupt URB after the core is registered", whose issue no longer arises.
- Moved the Kconfig split to a later patch.
Changes in v7:
- None.
Changes in v6:
- Reordered to sit before the transport-abstraction patch; no functional change.
Changes in v5:
- Split from the monolithic v4 patch to follow the single logical change principle.
MAINTAINERS | 2 +- drivers/mfd/Makefile | 1 + drivers/mfd/nct6694-core.c | 111 ++++++++++++++++++++++++++++++++++++ drivers/mfd/nct6694-usb.c | 98 +++++-------------------------- include/linux/mfd/nct6694.h | 3 + 5 files changed, 130 insertions(+), 85 deletions(-) create mode 100644 drivers/mfd/nct6694-core.c
MAINTAINERS +1 -1
@@ -19539,7 +19539,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-usb.c +F: drivers/mfd/nct6694-*.c F: drivers/net/can/usb/nct6694_canfd.c F: drivers/rtc/rtc-nct6694.c F: drivers/watchdog/nct6694_wdt.c
drivers/mfd/Makefile +1 -0
@@ -124,6 +124,7 @@ obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o obj-$(CONFIG_MFD_PF1550) += pf1550.o +obj-$(CONFIG_MFD_NCT6694) += nct6694-core.o obj-$(CONFIG_MFD_NCT6694) += nct6694-usb.o obj-$(CONFIG_MFD_CORE) += mfd-core.o
drivers/mfd/nct6694-core.c new file +111 -0
111 lines not on this page · raw message
drivers/mfd/nct6694-usb.c +14 -84
148 lines not on this page · raw message
include/linux/mfd/nct6694.h +3 -0
@@ -91,6 +91,9 @@ struct nct6694 {unsigned int irq_enable; }; +int nct6694_device_init (struct device *dev); +void nct6694_device_exit (struct device *dev); +int nct6694_usb_read_msg (struct nct6694 *ddata,const struct nct6694_cmd_header *cmd_hd,void *buf);
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 10/13] mfd: nct6694: Extract core device management into a separate module
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
From: Ming Yu <[email protected]>
Move the IRQ domain and IDA handling, which do not depend on the transport, from the USB driver into a new nct6694-core module. The core retrieves struct nct6694 from the driver data of the transport device, and the transport keeps registering its own MFD cells.
Quoted 6 lines
The USB driver now creates the IRQ domain before submitting the interrupt URB, and kills the URB before the domain is removed.
No functional change.
Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
PATCH v8 11/13 mfd: nct6694: Introduce regmap-based transport abstraction
In reply to [email protected]From: Ming Yu <[email protected]>
Sub-device drivers call into the USB transport directly, so a second transport cannot be added without touching all of them.
Wrap the transport behind a regmap bus. The command header is mapped onto a single 32-bit register by packing the host control byte, the module id and the 16-bit offset, so that the sub-device drivers can use the regmap bulk accessors. The access_lock is dropped as regmap already serialises the bus accesses.
Add nct6694_write_read_msg() for the commands that send a request and read the reply back in the same firmware message, and use it for the I2C deliver command.
---
Changes in v8:
- Renamed the transport buffer arguments to tx_buf/rx_buf.
- Dropped max_raw_read/max_raw_write from the regmap_config, as they are ignored when a regmap_bus is provided.
Changes in v7:
- Made the USB transport helpers static and gave nct6694_usb_write_msg() separate @tx/@rx buffers, so a SET command only copies the firmware reply back when the caller asks for it.
- Documented in the shared header why nct6694_write_read_msg() is expressed as a read of a SET register.
Changes in v6:
- New patch. Replaces the v5 function-pointer abstraction with a regmap_bus based transport: the firmware command header is packed into a single 32-bit regmap register and sub-device drivers use the regmap bulk accessors. Adds nct6694_write_read_msg() for request/response commands and drops the per-transport access_lock (regmap already serialises bus accesses).
drivers/i2c/busses/i2c-nct6694.c | 2 +- drivers/mfd/Kconfig | 1 + drivers/mfd/nct6694-usb.c | 102 ++++++++++++++++++------------- include/linux/mfd/nct6694.h | 53 +++++++++++++--- 4 files changed, 108 insertions(+), 50 deletions(-)
drivers/i2c/busses/i2c-nct6694.c +1 -1
@@ -77,7 +77,7 @@ static int nct6694_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int deliver->addr =i2c_8bit_addr_from_msg (msg_temp);if (msg_temp->flags &I2C_M_RD ) { deliver->r_cnt = msg_temp->len; - ret =nct6694_write_msg (data->nct6694, &cmd_hd, deliver); + ret =nct6694_write_read_msg (data->nct6694, &cmd_hd, deliver);if (ret <0 )return ret;
drivers/mfd/Kconfig +1 -0
@@ -1166,6 +1166,7 @@ config MFD_MENF21BMC config MFD_NCT6694 tristate "Nuvoton NCT6694 support" select MFD_CORE + select REGMAP depends on USB help This enables support for the Nuvoton USB device NCT6694, which shares
drivers/mfd/nct6694-usb.c +61 -41
167 lines not on this page · raw message
include/linux/mfd/nct6694.h +45 -8
82 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 11/13] mfd: nct6694: Introduce regmap-based transport abstraction
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mfd: nct6694: silent truncation of large regmap transfers due to missing max_raw_read/write…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mfd: nct6694: silent truncation of large regmap transfers due to missing max_raw_read/write limits --
commit 057eae63d78f233d0d15c4fc0f83dc841a2be3ea
Author: Ming Yu <[email protected]>
mfd: nct6694: Introduce regmap-based transport abstraction
This patch wraps the NCT6694 USB transport behind a regmap bus, allowing sub-device drivers to use regmap bulk accessors. It packs the command header into a single 32-bit register and implements read and write callbacks for the custom regmap bus.
Quoted 4 lines
drivers/mfd/nct6694-usb.c +0
-0
[ ... ]
drivers/mfd/nct6694-usb.c int nct6694_usb_write_msg(struct nct6694 *ddata, · line 233
Quoted 10 lines
drivers/mfd/nct6694-usb.c +10
-0+static int nct6694_usb_regmap_read (void *context,const void *reg_buf, +size_t reg_size,void *val_buf, +size_t val_size) +{ +struct nct6694 *ddata = context; +u32 reg =get_unaligned_be32 (reg_buf); +const struct nct6694_cmd_header cmd_hd = { + .mod =FIELD_GET (NCT6694_REG_MOD , reg), + .offset =cpu_to_le16 (FIELD_GET (NCT6694_REG_OFFSET , reg)), + .len =cpu_to_le16 (val_size),
[Severity: Medium]
Does this silently truncate large read transfers?
The patch drops max_raw_read and max_raw_write from the regmap_config because they are ignored when a regmap_bus is provided, but they do not appear to be added to the new nct6694_usb_regmap_bus structure.
Because the bus limits default to 0 (unlimited), the regmap core will not automatically chunk large requests. If a sub-device requests a transfer larger than 65535 bytes, the unbounded val_size is cast to 16-bit via cpu_to_le16() here, which silently truncates it.
If the truncated length falls below NCT6694_MAX_DATA_LEN, the subsequent length check in nct6694_usb_read_msg() passes. The callback would then return 0 (success) while leaving the majority of the caller's buffer uninitialized.
Quoted 7 lines
drivers/mfd/nct6694-usb.c +7
-0+ }; + +if (FIELD_GET (NCT6694_REG_HCTRL , reg) ==NCT6694_HCTRL_SET ) +return nct6694_usb_write_msg (ddata, &cmd_hd, val_buf, val_buf); + +return nct6694_usb_read_msg (ddata, &cmd_hd, val_buf); +}
drivers/mfd/nct6694-usb.c int nct6694_usb_write_msg(struct nct6694 *ddata, · line 250
Quoted 10 lines
drivers/mfd/nct6694-usb.c +10
-0+ +static int nct6694_usb_regmap_write (void *context,const void *data, +size_t count) +{ +struct nct6694 *ddata = context; +u32 reg =get_unaligned_be32 (data); +const struct nct6694_cmd_header cmd_hd = { + .mod =FIELD_GET (NCT6694_REG_MOD , reg), + .offset =cpu_to_le16 (FIELD_GET (NCT6694_REG_OFFSET , reg)), + .len =cpu_to_le16 (count -sizeof (reg)),
[Severity: Medium]
Could this cause the same truncation issue for write transfers?
Without a max_raw_write limit on the bus, the unbounded count is silently truncated to 16 bits here, which could lead to silently dropping data on large writes.
Quoted 4 lines
drivers/mfd/nct6694-usb.c +4
-0+ }; + +return nct6694_usb_write_msg (ddata, &cmd_hd, data +sizeof (reg),NULL ); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Re: [PATCH v8 11/13] mfd: nct6694: Introduce regmap-based transport abstraction
In reply to [email protected]Hi Ming,
Quoted 11 lines · On Wed, Oct 07, 2026 at 05:21:00PM +0800, [email protected] wrote:
From: Ming Yu <[email protected]>
Sub-device drivers call into the USB transport directly, so a second transport cannot be added without touching all of them.
Wrap the transport behind a regmap bus. The command header is mapped onto a single 32-bit register by packing the host control byte, the module id and the 16-bit offset, so that the sub-device drivers can use the regmap bulk accessors. The
access_lockis dropped as regmap already serialises the bus accesses.
Quoted 5 lines
Add
nct6694_write_read_msg()for the commands that send a request and read the reply back in the same firmware message, and use it for the I2C deliver command.Signed-off-by Ming Yu <[email protected]>
Andi
PATCH v8 12/13 mfd: nct6694: Add a Kconfig symbol for the USB transport
In reply to [email protected]From: Ming Yu <[email protected]>
Now that the sub-device drivers only access the device through regmap, the core no longer depends on USB.
Build the USB transport under a new MFD_NCT6694_USB symbol and keep MFD_NCT6694 for the core. MFD_NCT6694_USB defaults to MFD_NCT6694 so that existing configurations keep the USB transport enabled.
---
Changes in v8:
- New patch. Split out of v7 "Rename driver to nct6694-usb and update Kconfig"; MFD_NCT6694 remains the user visible core symbol and MFD_NCT6694_USB defaults to it to keep existing configurations working.
drivers/mfd/Kconfig | 20 ++++++++++++++++---- drivers/mfd/Makefile | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-)
drivers/mfd/Kconfig +16 -4
@@ -1167,18 +1167,30 @@ config MFD_NCT6694 tristate "Nuvoton NCT6694 support" select MFD_CORE select REGMAP - depends on USB help - This enables support for the Nuvoton USB device NCT6694, which shares + This enables support for the Nuvoton NCT6694, which shares peripherals. The Nuvoton NCT6694 is a peripheral expander with 16 GPIO chips, 6 I2C controllers, 2 CANfd controllers, 2 Watchdog timers, ADC, PWM, and RTC. - This driver provides core APIs to access the NCT6694 hardware - monitoring and control features. + This driver provides the core support shared by the transport + interface drivers, at least one of which must be enabled. Additional drivers must be enabled to utilize the specific functionalities of the device. +config MFD_NCT6694_USB + tristate "Nuvoton NCT6694 USB interface support" + depends on MFD_NCT6694 && USB + default MFD_NCT6694 + help + This enables support for the Nuvoton NCT6694 connected through + its USB interface. The driver uses USB bulk transfers to send + commands to the device firmware and an interrupt transfer to + receive its interrupt status. + + To compile this driver as a module, choose M here: the module + will be called nct6694-usb. + config MFD_OCELOT tristate "Microsemi Ocelot External Control Support" depends on SPI_MASTER
drivers/mfd/Makefile +1 -1
@@ -125,7 +125,7 @@ obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o obj-$(CONFIG_MFD_PF1550) += pf1550.o obj-$(CONFIG_MFD_NCT6694) += nct6694-core.o -obj-$(CONFIG_MFD_NCT6694) += nct6694-usb.o +obj-$(CONFIG_MFD_NCT6694_USB) += nct6694-usb.o obj-$(CONFIG_MFD_CORE) += mfd-core.o
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 12/13] mfd: nct6694: Add a Kconfig symbol for the USB transport
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mfd: nct6694_irq_enable() tracks state but fails to mask hardware interrupts --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mfd: nct6694_irq_enable() tracks state but fails to mask hardware interrupts --
commit bcc3da085a9a041b0467d9dc36ab1c8f97cd9de2
Author: Ming Yu <[email protected]>
mfd: nct6694: Add a Kconfig symbol for the USB transport
Now that the sub-device drivers only access the device through regmap, the core no longer depends on USB. The USB transport is built under a new MFD_NCT6694_USB symbol.
Quoted 6 lines
drivers/mfd/nct6694-core.c new file +0
-0@@ -0,0 +1,111 @@
[ ... ]
drivers/mfd/nct6694-core.c
Quoted 9 lines
drivers/mfd/nct6694-core.c new file +9
-0+static void nct6694_irq_enable (struct irq_data *data) +{ +struct nct6694 *ddata =irq_data_get_irq_chip_data (data); +irq_hw_number_t hwirq =irqd_to_hwirq (data); + +guard (spinlock_irqsave)(&ddata->irq_lock); + + ddata->irq_enable |=BIT (hwirq); +}
[Severity: Medium]
Does this code successfully mask hardware interrupts?
It looks like ddata->irq_enable is updated here to track the state, but is never actually evaluated when processing incoming interrupts.
[ ... ]
Quoted 6 lines
drivers/mfd/nct6694-usb.c new file +0
-0@@ -0,0 +1,414 @@
[ ... ]
Quoted 19 lines
drivers/mfd/nct6694-usb.c new file +19
-0+static void nct6694_usb_int_callback (struct urb *urb) +{ +struct nct6694 *ddata = urb->context; +__le32 *status_le = urb->transfer_buffer; +u32 int_status; +int ret; + +switch (urb->status) { +case 0 : +break ; +case -ECONNRESET : +case -ENOENT : +case -ESHUTDOWN : +return ; +default : + goto resubmit; + } + +if (urb->actual_length <sizeof (*status_le))
drivers/mfd/nct6694-usb.c
Quoted 10 lines
drivers/mfd/nct6694-usb.c new file +10
-0+ goto resubmit; + + int_status =le32_to_cpu (*status_le); + +while (int_status) { +int irq =__ffs (int_status); + +generic_handle_domain_irq_safe (ddata->domain, irq); + int_status &= ~BIT (irq); + }
[Severity: Medium]
Since ddata->irq_enable isn't consulted here, won't this result in continuously firing disabled hardware interrupts to the kernel?
Should int_status be filtered against ddata->irq_enable before calling generic_handle_domain_irq_safe()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
PATCH v8 13/13 mfd: nct6694: Add Host Interface (HIF) eSPI transport driver
In reply to [email protected]From: Ming Yu <[email protected]>
Add support for the Host Interface (HIF) transport over eSPI for the Nuvoton NCT6694.
The driver locates the Super-I/O to configure the shared memory base address and SIRQ, and implements a regmap_bus that exchanges firmware commands through the shared memory window. Report accesses are served from a separate MMIO regmap.
---
Changes in v8:
- Embedded struct nct6694 in nct6694_hif_data and dropped the priv pointer; renamed hdata to hif_data and the transfer buffers and lengths consistently with the USB transport.
- Adapted to nct6694_device_{init,exit}() and register the MFD cells from the transport.
- Request the interrupt after the core is set up, so that it is freed before the IRQ domain is removed.
- Dropped the max_raw_read/max_raw_write settings, which are ignored with a regmap_bus.
Changes in v7:
- Zero the tail of the caller buffer when the firmware answers with fewer bytes than requested, and split the transfer helper into separate @tx/@rx buffers instead of a shared scratch buffer.
- Request the interrupt before registering the core, so devres frees it after the sub-devices are removed. Mask the interrupt and clear a stale status in nct6694_hif_irq_init() so the handler cannot run before the IRQ domain exists.
- Return IRQ_HANDLED once the interrupt status has been cleared.
- Skip hardware interrupts that have no mapping instead of dispatching virq 0.
- Bail out with -ENODEV if the BIOS left the shared memory base address unconfigured.
- Program the SIRQ register with the hardware interrupt number rather than the Linux virq.
Changes in v6:
- Reworked the transport to implement a regmap_bus instead of the v5 read_msg/write_msg function pointers.
Changes in v5:
- Split from the monolithic v4 patch.
- Adapted to re-use the newly introduced nct6694_core_probe() and abstracted I/O APIs.
Changes since version 3:
- Remove redundant module type macro definitions from sub-device drivers that are now provided by the shared header <linux/mfd/nct6694.h>, fixing -Wmacro-redefined warnings.
Changes since version 2:
- Restore per-device IDA and mfd_add_hotplug_devices()/PLATFORM_DEVID_AUTO to avoid child device ID conflicts with multiple NCT6694 chips.
- Validate irq_find_mapping() return value before dispatching IRQs.
- Check superio_enter() return value in nct6694_irq_init().
Changes since version 1:
- Drop function pointers from Super-I/O access and use static inline helpers with proper types.
drivers/mfd/Kconfig | 13 + drivers/mfd/Makefile | 1 + drivers/mfd/nct6694-hif.c | 572 ++++++++++++++++++++++++++++++++++++ include/linux/mfd/nct6694.h | 2 +- 4 files changed, 587 insertions(+), 1 deletion(-) create mode 100644 drivers/mfd/nct6694-hif.c
drivers/mfd/Kconfig +13 -0
@@ -1178,6 +1178,19 @@ config MFD_NCT6694 Additional drivers must be enabled to utilize the specific functionalities of the device. +config MFD_NCT6694_HIF + tristate "Nuvoton NCT6694 HIF (eSPI) interface support" + depends on MFD_NCT6694 && HAS_IOPORT && ACPI + select REGMAP_MMIO + help + This enables support for the Nuvoton NCT6694 connected through + the Host Interface (HIF) over eSPI. The driver uses the Super-I/O + configuration and the shared memory window to send commands to + the device firmware. + + To compile this driver as a module, choose M here: the module + will be called nct6694-hif. + config MFD_NCT6694_USB tristate "Nuvoton NCT6694 USB interface support" depends on MFD_NCT6694 && USB
drivers/mfd/Makefile +1 -0
@@ -125,6 +125,7 @@ obj-$(CONFIG_MFD_MC13XXX_I2C) += mc13xxx-i2c.o obj-$(CONFIG_MFD_PF1550) += pf1550.o obj-$(CONFIG_MFD_NCT6694) += nct6694-core.o +obj-$(CONFIG_MFD_NCT6694_HIF) += nct6694-hif.o obj-$(CONFIG_MFD_NCT6694_USB) += nct6694-usb.o obj-$(CONFIG_MFD_CORE) += mfd-core.o
drivers/mfd/nct6694-hif.c new file +572 -0
572 lines not on this page · raw message
include/linux/mfd/nct6694.h +1 -1
@@ -2,7 +2,7 @@/* * Copyright (C) 2025 Nuvoton Technology Corp. * -* Nuvoton NCT6694 core definitions shared by the transport driver +* Nuvoton NCT6694 core definitions shared by the transport drivers * and the sub-device drivers. */
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v8 13/13] mfd: nct6694: Add Host Interface (HIF) eSPI transport driver
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
From: Ming Yu <[email protected]>
Add support for the Host Interface (HIF) transport over eSPI for the Nuvoton NCT6694.
The driver locates the Super-I/O to configure the shared memory base address and SIRQ, and implements a
regmap_busthat exchanges firmware commands through the shared memory window. Report accesses are served from a separate MMIO regmap.Signed-off-by Ming Yu <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Bartosz Golaszewski
AppliedRe: (subset) [PATCH v8 00/13] mfd: nct6694: Refactor transport layer and add HIF (eSPI) support
In reply to [email protected]Quoted 11 lines · On Wed, 07 Oct 2026 17:20:49 +0800, [email protected] wrote:
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 driver to support multiple transport backends and adds a Host Interface (HIF) transport driver that communicates over eSPI using Super-I/O shared memory.
[...]
Applied, thanks!
[01/13] gpio: nct6694: Mark the GPIO controller as sleeping
https://git.kernel.org/brgl/c/fcbc48dc650d2f58bfe19c4486830de0e9cad62e
Best regards,
Signature · 1 line
Bartosz Golaszewski <[email protected]>