Skip to contentSkip to conversation

Add Nuvoton NCT6694 MFD drivers

PATCH Superseded Ming Yu 4 participants 23 messages Jan 14, 2025 – Jan 16, 2025

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

v5 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Ming Yu

author
#0 lore raw

v5 cover Add Nuvoton NCT6694 MFD drivers

This patch series introduces support for Nuvoton NCT6694, a peripheral expander based on USB interface. It models the chip as an MFD driver (1/7), GPIO driver(2/7), I2C Adapter driver(3/7), CANfd driver(4/7), WDT driver(5/7), HWMON driver(6/7), and RTC driver(7/7).

The MFD driver implements USB device functionality to issue custom-define USB bulk pipe packets for NCT6694. Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.

The following introduces the custom-define USB transactions:
nct6694_read_msg - Send bulk-out pipe to write request packet
Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet

nct6694_write_msg - Send bulk-out pipe to write request packet
Send bulk-out pipe to write data packet Receive bulk-in pipe to read response packet Receive bulk-in pipe to read data packet

Changes since version 4:
- Modify arguments in read/write function to a pointer to cmd_header
- Modify all callers that call the read/write function
- Move the nct6694_canfd.c to drivers/net/can/usb/
- Fix the missing rx offload function in nct6694_canfd.c
- Fix warngings in nct6694-hwmon.c

Changes since version 3:
- Modify array buffer to structure for each drivers
- Fix defines and comments for each drivers
- Add header <linux/bits.h> and use BIT macro in nct6694.c and gpio-nct6694.c
- Modify mutex_init() to devm_mutex_init()
- Add rx-offload helper in nct6694_canfd.c
- Drop watchdog_init_timeout() in nct6694_wdt.c
- Modify the division method to DIV_ROUND_CLOSEST() in nct6694-hwmon.c
- Drop private mutex and use rtc core lock in rtc-nct6694.c
- Modify device_set_wakeup_capable() to device_init_wakeup() in rtc-nct6694.c

Changes since version 2:
- Add MODULE_ALIAS() for each child driver
- Modify gpio line names be a local variable in gpio-nct6694.c
- Drop unnecessary platform_get_drvdata() in gpio-nct6694.c
- Rename each command in nct6694_canfd.c
- Modify each function name consistently in nct6694_canfd.c
- Modify the pretimeout validation procedure in nct6694_wdt.c
- Fix warnings in nct6694-hwmon.c

Changes since version 1:
- Implement IRQ domain to handle IRQ demux in nct6694.c
- Modify USB_DEVICE to USB_DEVICE_AND_INTERFACE_INFO API in nct6694.c
- Add each driver's command structure
- Fix USB functions in nct6694.c
- Fix platform driver registration in each child driver
- Sort each driver's header files alphabetically
- Drop unnecessary header in gpio-nct6694.c
- Add gpio line names in gpio-nct6694.c
- Fix errors and warnings in nct6694_canfd.c
- Fix TX-flow control in nct6694_canfd.c
- Fix warnings in nct6694_wdt.c
- Drop unnecessary logs in nct6694_wdt.c
- Modify start() function to setup device in nct6694_wdt.c
- Add voltage sensors functionality in nct6694-hwmon.c
- Add temperature sensors functionality in nct6694-hwmon.c
- Fix overwrite error return values in nct6694-hwmon.c
- Add write value limitation for each write() function in nct6694-hwmon.c
- Drop unnecessary logs in rtc-nct6694.c
- Fix overwrite error return values in rtc-nct6694.c
- Modify to use dev_err_probe API in rtc-nct6694.c

Ming Yu (7):
mfd: Add core driver for Nuvoton NCT6694
gpio: Add Nuvoton NCT6694 GPIO support
i2c: Add Nuvoton NCT6694 I2C support
can: Add Nuvoton NCT6694 CAN support
watchdog: Add Nuvoton NCT6694 WDT support
hwmon: Add Nuvoton NCT6694 HWMON support
rtc: Add Nuvoton NCT6694 RTC support

 MAINTAINERS                         |  13 +
 drivers/gpio/Kconfig                |  12 +
 drivers/gpio/Makefile               |   1 +
 drivers/gpio/gpio-nct6694.c         | 458 ++++++++++++++
 drivers/hwmon/Kconfig               |  10 +
 drivers/hwmon/Makefile              |   1 +
 drivers/hwmon/nct6694-hwmon.c       | 947 ++++++++++++++++++++++++++++
 drivers/i2c/busses/Kconfig          |  10 +
 drivers/i2c/busses/Makefile         |   1 +
 drivers/i2c/busses/i2c-nct6694.c    | 157 +++++
 drivers/mfd/Kconfig                 |  10 +
 drivers/mfd/Makefile                |   2 +
 drivers/mfd/nct6694.c               | 388 ++++++++++++
 drivers/net/can/usb/Kconfig         |  10 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 856 +++++++++++++++++++++++++
 drivers/rtc/Kconfig                 |  10 +
 drivers/rtc/Makefile                |   1 +
 drivers/rtc/rtc-nct6694.c           | 286 +++++++++
 drivers/watchdog/Kconfig            |  11 +
 drivers/watchdog/Makefile           |   1 +
 drivers/watchdog/nct6694_wdt.c      | 295 +++++++++
 include/linux/mfd/nct6694.h         | 109 ++++
 23 files changed, 3590 insertions(+)
 create mode 100644 drivers/gpio/gpio-nct6694.c
 create mode 100644 drivers/hwmon/nct6694-hwmon.c
 create mode 100644 drivers/i2c/busses/i2c-nct6694.c
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
 create mode 100644 drivers/rtc/rtc-nct6694.c
 create mode 100644 drivers/watchdog/nct6694_wdt.c
 create mode 100644 include/linux/mfd/nct6694.h
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v5 1/7 mfd: Add core driver for Nuvoton NCT6694

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 implements USB device functionality and shares the chip's peripherals as a child device.

Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.

---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 388 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 109 ++++++++++
 5 files changed, 516 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +7 -0
index a87ddad78e26..79568f82b98a 100644
@@ -16717,6 +16717,13 @@ F:	drivers/nubus/
 F:	include/linux/nubus.h
 F:	include/uapi/linux/nubus.h
 
+NUVOTON NCT6694 MFD DRIVER
+M:	Ming Yu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/mfd/nct6694.c
+F:	include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +10 -0
index ae23b317a64e..3e641bfeeb38 100644
@@ -1045,6 +1045,16 @@ config MFD_MENF21BMC
 	  This driver can also be built as a module. If so the module
 	  will be called menf21bmc.
 
+config MFD_NCT6694
+	tristate "Nuvoton NCT6694 support"
+	select MFD_CORE
+	depends on USB
+	help
+	  This adds support for Nuvoton USB device NCT6694 sharing peripherals
+	  This includes the USB device driver and core APIs.
+	  Additional drivers must be enabled in order to use the functionality
+	  of the device.
+
 config MFD_OCELOT
 	tristate "Microsemi Ocelot External Control Support"
 	depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index e057d6d6faef..3c902d3704dc 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX)	+= mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)	+= mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)	+= mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)	+= nct6694.o
+
 obj-$(CONFIG_MFD_CORE)		+= mfd-core.o
 
 ocelot-soc-objs			:= ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +388 -0

388 lines not on this page · raw message

include/linux/mfd/nct6694.h new file +109 -0

109 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH v5 1/7] mfd: Add core driver for Nuvoton NCT6694

Quoted 46 lines · Ming Yu

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 implements USB device functionality and shares the chip's peripherals as a child device.

Each child device can use the USB functions nct6694_read_msg() and nct6694_write_msg() to issue a command. They can also request interrupt that will be called when the USB device receives its interrupt pipe.

---
 MAINTAINERS                 |   7 +
 drivers/mfd/Kconfig         |  10 +
 drivers/mfd/Makefile        |   2 +
 drivers/mfd/nct6694.c       | 388 ++++++++++++++++++++++++++++++++++++
 include/linux/mfd/nct6694.h | 109 ++++++++++
 5 files changed, 516 insertions(+)
 create mode 100644 drivers/mfd/nct6694.c
 create mode 100644 include/linux/mfd/nct6694.h
MAINTAINERS +7 -0
index a87ddad78e26..79568f82b98a 100644
@@ -16717,6 +16717,13 @@ F:	drivers/nubus/
 F:	include/linux/nubus.h
 F:	include/uapi/linux/nubus.h
 
+NUVOTON NCT6694 MFD DRIVER
+M:	Ming Yu <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/mfd/nct6694.c
+F:	include/linux/mfd/nct6694.h
+
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
 M:	Antonino Daplas <[email protected]>
 L:	[email protected]
drivers/mfd/Kconfig +0 -0
index ae23b317a64e..3e641bfeeb38 100644

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1053

Quoted 9 lines
drivers/mfd/Kconfig +6 -0
@@ -1045,6 +1045,16 @@ config MFD_MENF21BMC
 	  This driver can also be built as a module. If so the module
 	  will be called menf21bmc.
 
+config MFD_NCT6694
+	tristate "Nuvoton NCT6694 support"
+	select MFD_CORE
+	depends on USB
+	help
+	  This adds support for Nuvoton USB device NCT6694 sharing peripherals

Missing full stop.

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1055

Quoted 2 lines
drivers/mfd/Kconfig +2 -0
+	  This includes the USB device driver and core APIs.
+	  Additional drivers must be enabled in order to use the functionality

New sentences do not have to be on new lines.

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1056

Quoted 1 line
drivers/mfd/Kconfig +1 -0
+	  of the device.

Please explain what this functionality is.

Quoted 23 lines
drivers/mfd/Kconfig +1 -0
+
 config MFD_OCELOT
 	tristate "Microsemi Ocelot External Control Support"
 	depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index e057d6d6faef..3c902d3704dc 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX)	+= mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)	+= mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)	+= mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)	+= nct6694.o
+
 obj-$(CONFIG_MFD_CORE)		+= mfd-core.o
 
 ocelot-soc-objs			:= ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +0 -0
new file mode 100644
index 000000000000..294b6b7a902e

drivers/mfd/nct6694.c line 3

Quoted 3 lines
drivers/mfd/nct6694.c new file +3 -0
@@ -0,0 +1,388 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 MFD driver based on USB interface.

No such thing as an MFD driver. What is this device?

Quoted 8 lines
drivers/mfd/nct6694.c new file +8 -0
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/bits.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>

drivers/mfd/nct6694.c line 21

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#define MFD_DEV_SIMPLE(_name)				\
+{							\
+	.name = NCT6694_DEV_##_name,			\

Device names are usually lower case.

drivers/mfd/nct6694.c line 22

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+}							\

MFD_CELL_NAME()

drivers/mfd/nct6694.c line 28

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+#define MFD_DEV_WITH_ID(_name, _id)			\
+{							\
+	.name = NCT6694_DEV_##_name,			\
+	.id = _id,					\
+}

MFD_CELL_BASIC()

Or add a new one to include/linux/mfd/core.h.

drivers/mfd/nct6694.c line 30

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+/* MFD device resources */

This comment is superfluous.

drivers/mfd/nct6694.c line 32

Quoted 2 lines
drivers/mfd/nct6694.c new file +2 -0
+static const struct mfd_cell nct6694_dev[] = {
+	MFD_DEV_WITH_ID(GPIO, 0x0),

Why doesn't PLATFORM_DEVID_AUTO work for you?

Quoted 33 lines
drivers/mfd/nct6694.c new file +33 -0
+	MFD_DEV_WITH_ID(GPIO, 0x1),
+	MFD_DEV_WITH_ID(GPIO, 0x2),
+	MFD_DEV_WITH_ID(GPIO, 0x3),
+	MFD_DEV_WITH_ID(GPIO, 0x4),
+	MFD_DEV_WITH_ID(GPIO, 0x5),
+	MFD_DEV_WITH_ID(GPIO, 0x6),
+	MFD_DEV_WITH_ID(GPIO, 0x7),
+	MFD_DEV_WITH_ID(GPIO, 0x8),
+	MFD_DEV_WITH_ID(GPIO, 0x9),
+	MFD_DEV_WITH_ID(GPIO, 0xA),
+	MFD_DEV_WITH_ID(GPIO, 0xB),
+	MFD_DEV_WITH_ID(GPIO, 0xC),
+	MFD_DEV_WITH_ID(GPIO, 0xD),
+	MFD_DEV_WITH_ID(GPIO, 0xE),
+	MFD_DEV_WITH_ID(GPIO, 0xF),
+
+	MFD_DEV_WITH_ID(I2C, 0x0),
+	MFD_DEV_WITH_ID(I2C, 0x1),
+	MFD_DEV_WITH_ID(I2C, 0x2),
+	MFD_DEV_WITH_ID(I2C, 0x3),
+	MFD_DEV_WITH_ID(I2C, 0x4),
+	MFD_DEV_WITH_ID(I2C, 0x5),
+
+	MFD_DEV_WITH_ID(CAN, 0x0),
+	MFD_DEV_WITH_ID(CAN, 0x1),
+
+	MFD_DEV_WITH_ID(WDT, 0x0),
+	MFD_DEV_WITH_ID(WDT, 0x1),
+
+	MFD_DEV_SIMPLE(HWMON),
+	MFD_DEV_SIMPLE(RTC),
+};
+

drivers/mfd/nct6694.c line 75

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+static int nct6694_response_err_handling(struct nct6694 *nct6694,
+					 unsigned char err_status)
+{
+	struct device *dev = &nct6694->udev->dev;
+
+	switch (err_status) {
+	case NCT6694_NO_ERROR:
+		return err_status;
+	case NCT6694_NOT_SUPPORT_ERROR:
+		dev_dbg(dev, "%s: Command is not supported!\n", __func__);

These should be dev_warns().

__func__ shouldn't be used in production code.

Users don't care about functions.

Quoted 45 lines
drivers/mfd/nct6694.c new file +45 -0
+		break;
+	case NCT6694_NO_RESPONSE_ERROR:
+		dev_dbg(dev, "%s: Command received no response!\n", __func__);
+		break;
+	case NCT6694_TIMEOUT_ERROR:
+		dev_dbg(dev, "%s: Command timed out!\n", __func__);
+		break;
+	case NCT6694_PENDING:
+		dev_dbg(dev, "%s: Command is pending!\n", __func__);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return -EIO;
+}
+
+int nct6694_read_msg(struct nct6694 *nct6694,
+		     struct nct6694_cmd_header *cmd_hd,
+		     void *buf)
+{
+	union nct6694_usb_msg *msg = nct6694->usb_msg;
+	int tx_len, rx_len, ret;
+
+	guard(mutex)(&nct6694->access_lock);
+
+	/* Send command packet to USB device */
+	memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+	msg->cmd_header.hctrl = NCT6694_HCTRL_GET;
+
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+			   &msg->cmd_header, sizeof(*msg), &tx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive response packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   &msg->response_header, sizeof(*msg), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+

drivers/mfd/nct6694.c line 130

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	/* Receive data packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	if (rx_len != le16_to_cpu(cmd_hd->len)) {
+		dev_dbg(&nct6694->udev->dev, "%s: Received length is not match!\n",

"does not match <something>"

drivers/mfd/nct6694.c line 129

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+			__func__);

This should be dev_err().

Quoted 43 lines
drivers/mfd/nct6694.c new file +43 -0
+		return -EIO;
+	}
+
+	return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_read_msg);
+
+int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+		      void *buf)
+{
+	union nct6694_usb_msg *msg = nct6694->usb_msg;
+	int tx_len, rx_len, ret;
+
+	guard(mutex)(&nct6694->access_lock);
+
+	/* Send command packet to USB device */
+	memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+	msg->cmd_header.hctrl = NCT6694_HCTRL_SET;
+
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+			   &msg->cmd_header, sizeof(*msg), &tx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Send data packet to USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &tx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive response packet from USB device */
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   &msg->response_header, sizeof(*msg), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	/* Receive data packet from USB device */

drivers/mfd/nct6694.c line 184

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	ret = usb_bulk_msg(nct6694->udev,
+			   usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+			   buf, le16_to_cpu(cmd_hd->len), &rx_len,
+			   nct6694->timeout);
+	if (ret)
+		return ret;
+
+	if (rx_len != le16_to_cpu(cmd_hd->len)) {
+		dev_dbg(&nct6694->udev->dev, "%s: Sent length is not match!\n",
+			__func__);

This should be dev_err().

Quoted 49 lines
drivers/mfd/nct6694.c new file +49 -0
+		return -EIO;
+	}
+
+	return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_write_msg);
+
+static void usb_int_callback(struct urb *urb)
+{
+	struct nct6694 *nct6694 = urb->context;
+	struct device *dev = &nct6694->udev->dev;
+	unsigned int *int_status = urb->transfer_buffer;
+	int ret;
+
+	switch (urb->status) {
+	case 0:
+		break;
+	case -ECONNRESET:
+	case -ENOENT:
+	case -ESHUTDOWN:
+		return;
+	default:
+		goto resubmit;
+	}
+
+	while (*int_status) {
+		int irq = __ffs(*int_status);
+
+		generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq));
+		*int_status &= ~BIT(irq);
+	}
+
+resubmit:
+	ret = usb_submit_urb(urb, GFP_ATOMIC);
+	if (ret)
+		dev_dbg(dev, "%s: Failed to resubmit urb, status %pe",
+			__func__, ERR_PTR(ret));
+}
+
+static void nct6694_irq_lock(struct irq_data *data)
+{
+	struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+
+	mutex_lock(&nct6694->irq_lock);
+}
+
+static void nct6694_irq_sync_unlock(struct irq_data *data)
+{
+	struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);

drivers/mfd/nct6694.c line 243

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+
+	mutex_unlock(&nct6694->irq_lock);
+}
+
+static void nct6694_irq_enable(struct irq_data *data)
+{
+	struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+	irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+	nct6694->irq_enable |= BIT(hwirq);

Changing a bit mask doesn't actually {en,dis}able an IRQ, right?

Quoted 43 lines
drivers/mfd/nct6694.c new file +43 -0
+}
+
+static void nct6694_irq_disable(struct irq_data *data)
+{
+	struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+	irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+	nct6694->irq_enable &= ~BIT(hwirq);
+}
+
+static struct irq_chip nct6694_irq_chip = {
+	.name = "nct6694-irq",
+	.flags = IRQCHIP_SKIP_SET_WAKE,
+	.irq_bus_lock = nct6694_irq_lock,
+	.irq_bus_sync_unlock = nct6694_irq_sync_unlock,
+	.irq_enable = nct6694_irq_enable,
+	.irq_disable = nct6694_irq_disable,
+};
+
+static int nct6694_irq_domain_map(struct irq_domain *d, unsigned int irq,
+				  irq_hw_number_t hw)
+{
+	struct nct6694 *nct6694 = d->host_data;
+
+	irq_set_chip_data(irq, nct6694);
+	irq_set_chip_and_handler(irq, &nct6694_irq_chip, handle_simple_irq);
+
+	return 0;
+}
+
+static void nct6694_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
+{
+	irq_set_chip_and_handler(irq, NULL, NULL);
+	irq_set_chip_data(irq, NULL);
+}
+
+static const struct irq_domain_ops nct6694_irq_domain_ops = {
+	.map	= nct6694_irq_domain_map,
+	.unmap	= nct6694_irq_domain_unmap,
+};
+
+static int nct6694_usb_probe(struct usb_interface *iface,
+			     const struct usb_device_id *id)

drivers/mfd/nct6694.c line 296

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+{
+	struct usb_device *udev = interface_to_usbdev(iface);
+	struct usb_endpoint_descriptor *int_endpoint;
+	struct usb_host_interface *interface;
+	struct device *dev = &udev->dev;
+	struct nct6694 *nct6694;
+	int pipe, maxp;
+	int ret;
+
+	interface = iface->cur_altsetting;

Initialise this during allocation.

drivers/mfd/nct6694.c line 310

Quoted 13 lines
drivers/mfd/nct6694.c new file +13 -0
+	int_endpoint = &interface->endpoint[0].desc;
+	if (!usb_endpoint_is_int_in(int_endpoint))
+		return -ENODEV;
+
+	nct6694 = devm_kzalloc(dev, sizeof(*nct6694), GFP_KERNEL);
+	if (!nct6694)
+		return -ENOMEM;
+
+	pipe = usb_rcvintpipe(udev, NCT6694_INT_IN_EP);
+	maxp = usb_maxpacket(udev, pipe);
+
+	nct6694->usb_msg = devm_kzalloc(dev, sizeof(union nct6694_usb_msg),
+					GFP_KERNEL);

Unwrap this - you can use up to 100-chars.

Quoted 11 lines
drivers/mfd/nct6694.c new file +11 -0
+	if (!nct6694->usb_msg)
+		return -ENOMEM;
+
+	nct6694->int_buffer = devm_kzalloc(dev, maxp, GFP_KERNEL);
+	if (!nct6694->int_buffer)
+		return -ENOMEM;
+
+	nct6694->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
+	if (!nct6694->int_in_urb)
+		return -ENOMEM;
+

drivers/mfd/nct6694.c line 331

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	nct6694->domain = irq_domain_add_simple(NULL, NCT6694_NR_IRQS, 0,
+						&nct6694_irq_domain_ops,
+						nct6694);
+	if (!nct6694->domain) {
+		ret = -ENODEV;
+		goto err_urb;
+	}
+
+	nct6694->udev = udev;
+	nct6694->timeout = NCT6694_URB_TIMEOUT;	/* Wait until urb complete */

"URB completes"?

drivers/mfd/nct6694.c line 344

Quoted 13 lines
drivers/mfd/nct6694.c new file +13 -0
+
+	devm_mutex_init(dev, &nct6694->access_lock);
+	devm_mutex_init(dev, &nct6694->irq_lock);
+
+	usb_fill_int_urb(nct6694->int_in_urb, udev, pipe,
+			 nct6694->int_buffer, maxp, usb_int_callback,
+			 nct6694, int_endpoint->bInterval);
+	ret = usb_submit_urb(nct6694->int_in_urb, GFP_KERNEL);
+	if (ret)
+		goto err_urb;
+
+	dev_set_drvdata(dev, nct6694);
+	usb_set_intfdata(iface, nct6694);

These two do the same thing. You don't need both.

Quoted 9 lines
drivers/mfd/nct6694.c new file +9 -0
+	ret = mfd_add_hotplug_devices(dev, nct6694_dev, ARRAY_SIZE(nct6694_dev));
+	if (ret)
+		goto err_mfd;
+
+	return 0;
+
+err_mfd:
+	usb_kill_urb(nct6694->int_in_urb);
+err_urb:

drivers/mfd/nct6694.c line 364

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+	usb_free_urb(nct6694->int_in_urb);
+	return ret;
+}
+
+static void nct6694_usb_disconnect(struct usb_interface *iface)
+{
+	struct usb_device *udev = interface_to_usbdev(iface);
+	struct nct6694 *nct6694 = usb_get_intfdata(iface);
+
+	mfd_remove_devices(&udev->dev);

Does devm_* work here?

Quoted 12 lines
drivers/mfd/nct6694.c new file +12 -0
+	usb_kill_urb(nct6694->int_in_urb);
+	usb_free_urb(nct6694->int_in_urb);
+}
+
+static const struct usb_device_id nct6694_ids[] = {
+	{ USB_DEVICE_AND_INTERFACE_INFO(NCT6694_VENDOR_ID,
+					NCT6694_PRODUCT_ID,
+					0xFF, 0x00, 0x00)},
+	{}
+};
+MODULE_DEVICE_TABLE(usb, nct6694_ids);
+

drivers/mfd/nct6694.c line 386

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+static struct usb_driver nct6694_usb_driver = {
+	.name	= "nct6694",
+	.id_table = nct6694_ids,
+	.probe = nct6694_usb_probe,
+	.disconnect = nct6694_usb_disconnect,
+};
+
+module_usb_driver(nct6694_usb_driver);
+
+MODULE_DESCRIPTION("USB-MFD driver for NCT6694");

Remove all references to MFD.

Quoted 14 lines
drivers/mfd/nct6694.c new file +2 -0
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
include/linux/mfd/nct6694.h new file +6 -0
new file mode 100644
index 000000000000..67ca835589ad
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Nuvoton NCT6694 USB transaction and data structure.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */

include/linux/mfd/nct6694.h line 16

Quoted 10 lines
include/linux/mfd/nct6694.h new file +10 -0
+
+#ifndef __MFD_NCT6694_H
+#define __MFD_NCT6694_H
+
+#define NCT6694_DEV_GPIO	"nct6694-gpio"
+#define NCT6694_DEV_I2C		"nct6694-i2c"
+#define NCT6694_DEV_CAN		"nct6694-can"
+#define NCT6694_DEV_WDT		"nct6694-wdt"
+#define NCT6694_DEV_HWMON	"nct6694-hwmon"
+#define NCT6694_DEV_RTC		"nct6694-rtc"

Use raw strings in place please.

include/linux/mfd/nct6694.h line 29

Quoted 12 lines
include/linux/mfd/nct6694.h new file +12 -0
+#define NCT6694_VENDOR_ID	0x0416
+#define NCT6694_PRODUCT_ID	0x200B
+#define NCT6694_INT_IN_EP	0x81
+#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
+
+enum nct6694_irq_id {

These are never used.

Quoted 51 lines
include/linux/mfd/nct6694.h new file +51 -0
+	NCT6694_IRQ_GPIO0 = 0,
+	NCT6694_IRQ_GPIO1,
+	NCT6694_IRQ_GPIO2,
+	NCT6694_IRQ_GPIO3,
+	NCT6694_IRQ_GPIO4,
+	NCT6694_IRQ_GPIO5,
+	NCT6694_IRQ_GPIO6,
+	NCT6694_IRQ_GPIO7,
+	NCT6694_IRQ_GPIO8,
+	NCT6694_IRQ_GPIO9,
+	NCT6694_IRQ_GPIOA,
+	NCT6694_IRQ_GPIOB,
+	NCT6694_IRQ_GPIOC,
+	NCT6694_IRQ_GPIOD,
+	NCT6694_IRQ_GPIOE,
+	NCT6694_IRQ_GPIOF,
+	NCT6694_IRQ_CAN1,
+	NCT6694_IRQ_CAN2,
+	NCT6694_IRQ_RTC,
+	NCT6694_NR_IRQS,
+};
+
+enum nct6694_response_err_status {
+	NCT6694_NO_ERROR = 0,
+	NCT6694_FORMAT_ERROR,
+	NCT6694_RESERVED1,
+	NCT6694_RESERVED2,
+	NCT6694_NOT_SUPPORT_ERROR,
+	NCT6694_NO_RESPONSE_ERROR,
+	NCT6694_TIMEOUT_ERROR,
+	NCT6694_PENDING,
+};
+
+struct __packed nct6694_cmd_header {
+	u8 rsv1;
+	u8 mod;
+	union __packed {
+		__le16 offset;
+		struct __packed {
+			u8 cmd;
+			u8 sel;
+		};
+	};
+	u8 hctrl;
+	u8 rsv2;
+	__le16 len;
+};
+
+struct __packed nct6694_response_header {
+	u8 sequence_id;
+	u8 sts;

include/linux/mfd/nct6694.h line 90

Quoted 10 lines
include/linux/mfd/nct6694.h new file +10 -0
+	u8 reserved[4];
+	__le16 len;
+};
+
+union __packed nct6694_usb_msg {
+	struct nct6694_cmd_header cmd_header;
+	struct nct6694_response_header response_header;
+};
+
+struct nct6694 {

Do all of these values need to be stored?

include/linux/mfd/nct6694.h line 99

Quoted 9 lines
include/linux/mfd/nct6694.h new file +9 -0
+	struct usb_device *udev;
+	struct urb *int_in_urb;
+	struct irq_domain *domain;
+	struct mutex access_lock;
+	struct mutex irq_lock;
+	union nct6694_usb_msg *usb_msg;
+	unsigned char *int_buffer;
+	unsigned int irq_enable;
+	/* time in msec to wait for the urb to the complete */

"Time" "URB"

Quoted 13 lines
include/linux/mfd/nct6694.h new file +10 -0
+	long timeout;
+};
+
+int nct6694_read_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+		     void *buf);
+
+int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+		      void *buf);
+
+#endif
Signature · 1 line
2.34.1
Signature · 1 line
Lee Jones [李琼斯]
author
#3 lore raw

Re: [PATCH v5 1/7] mfd: Add core driver for Nuvoton NCT6694

Dear Lee,

Thank you for your comments,

Lee Jones <[email protected]> 於 2025年1月16日 週四 上午12:04寫道:
...

Quoted 9 lines

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1053

Quoted 6 lines
drivers/mfd/Kconfig +6 -0
+config MFD_NCT6694
+     tristate "Nuvoton NCT6694 support"
+     select MFD_CORE
+     depends on USB
+     help
+       This adds support for Nuvoton USB device NCT6694 sharing peripherals

Missing full stop.

Quoted 9 lines

drivers/mfd/Kconfig config MFD_MENF21BMC · line 1055

Quoted 2 lines
drivers/mfd/Kconfig +2 -0
+       This includes the USB device driver and core APIs.
+       Additional drivers must be enabled in order to use the functionality

New sentences do not have to be on new lines.

Quoted 1 line

+ of the device.

Please explain what this functionality is.

Okay! I will fix these in the next patch.

Quoted 16 lines
Quoted 16 lines
drivers/mfd/Kconfig +1 -0
+
 config MFD_OCELOT
      tristate "Microsemi Ocelot External Control Support"
      depends on SPI_MASTER
drivers/mfd/Makefile +2 -0
index e057d6d6faef..3c902d3704dc 100644
@@ -121,6 +121,8 @@ obj-$(CONFIG_MFD_MC13XXX) += mc13xxx-core.o
 obj-$(CONFIG_MFD_MC13XXX_SPI)        += mc13xxx-spi.o
 obj-$(CONFIG_MFD_MC13XXX_I2C)        += mc13xxx-i2c.o
 
+obj-$(CONFIG_MFD_NCT6694)    += nct6694.o
+
 obj-$(CONFIG_MFD_CORE)               += mfd-core.o
 
Quoted 13 lines

drivers/mfd/nct6694.c line 3

Quoted 10 lines
drivers/mfd/Makefile +0 -0
 ocelot-soc-objs                      := ocelot-core.o ocelot-spi.o
drivers/mfd/nct6694.c new file +3 -0
new file mode 100644
index 000000000000..294b6b7a902e
@@ -0,0 +1,388 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 MFD driver based on USB interface.

No such thing as an MFD driver. What is this device?

Fix it in the v6.

Quoted 21 lines
Quoted 8 lines
drivers/mfd/nct6694.c new file +8 -0
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/bits.h>
+#include <linux/interrupt.h>
+#include <linux/irq.h>
+#include <linux/irqdomain.h>

drivers/mfd/nct6694.c line 21

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/usb.h>
+
+#define MFD_DEV_SIMPLE(_name)                                \
+{                                                    \
+     .name = NCT6694_DEV_##_name,                    \

Device names are usually lower case.

Quoted 4 lines

drivers/mfd/nct6694.c line 22

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+}                                                    \

MFD_CELL_NAME()

Fix it in the v6.

Quoted 10 lines

drivers/mfd/nct6694.c line 28

Quoted 5 lines
drivers/mfd/nct6694.c new file +5 -0
+#define MFD_DEV_WITH_ID(_name, _id)                  \
+{                                                    \
+     .name = NCT6694_DEV_##_name,                    \
+     .id = _id,                                      \
+}

MFD_CELL_BASIC()

Or add a new one to include/linux/mfd/core.h.

Fix it in the v6.

Quoted 4 lines

drivers/mfd/nct6694.c line 30

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+/* MFD device resources */

This comment is superfluous.

Drop it in the v6.

Quoted 5 lines

drivers/mfd/nct6694.c line 32

Quoted 2 lines
drivers/mfd/nct6694.c new file +2 -0
+static const struct mfd_cell nct6694_dev[] = {
+     MFD_DEV_WITH_ID(GPIO, 0x0),

Why doesn't PLATFORM_DEVID_AUTO work for you?

I need to manage these IDs to ensure that child devices can be properly utilized within their respective modules.

Quoted 46 lines
Quoted 33 lines
drivers/mfd/nct6694.c new file +33 -0
+     MFD_DEV_WITH_ID(GPIO, 0x1),
+     MFD_DEV_WITH_ID(GPIO, 0x2),
+     MFD_DEV_WITH_ID(GPIO, 0x3),
+     MFD_DEV_WITH_ID(GPIO, 0x4),
+     MFD_DEV_WITH_ID(GPIO, 0x5),
+     MFD_DEV_WITH_ID(GPIO, 0x6),
+     MFD_DEV_WITH_ID(GPIO, 0x7),
+     MFD_DEV_WITH_ID(GPIO, 0x8),
+     MFD_DEV_WITH_ID(GPIO, 0x9),
+     MFD_DEV_WITH_ID(GPIO, 0xA),
+     MFD_DEV_WITH_ID(GPIO, 0xB),
+     MFD_DEV_WITH_ID(GPIO, 0xC),
+     MFD_DEV_WITH_ID(GPIO, 0xD),
+     MFD_DEV_WITH_ID(GPIO, 0xE),
+     MFD_DEV_WITH_ID(GPIO, 0xF),
+
+     MFD_DEV_WITH_ID(I2C, 0x0),
+     MFD_DEV_WITH_ID(I2C, 0x1),
+     MFD_DEV_WITH_ID(I2C, 0x2),
+     MFD_DEV_WITH_ID(I2C, 0x3),
+     MFD_DEV_WITH_ID(I2C, 0x4),
+     MFD_DEV_WITH_ID(I2C, 0x5),
+
+     MFD_DEV_WITH_ID(CAN, 0x0),
+     MFD_DEV_WITH_ID(CAN, 0x1),
+
+     MFD_DEV_WITH_ID(WDT, 0x0),
+     MFD_DEV_WITH_ID(WDT, 0x1),
+
+     MFD_DEV_SIMPLE(HWMON),
+     MFD_DEV_SIMPLE(RTC),
+};
+

drivers/mfd/nct6694.c line 75

Quoted 10 lines
drivers/mfd/nct6694.c new file +10 -0
+static int nct6694_response_err_handling(struct nct6694 *nct6694,
+                                      unsigned char err_status)
+{
+     struct device *dev = &nct6694->udev->dev;
+
+     switch (err_status) {
+     case NCT6694_NO_ERROR:
+             return err_status;
+     case NCT6694_NOT_SUPPORT_ERROR:
+             dev_dbg(dev, "%s: Command is not supported!\n", __func__);

These should be dev_warns().

Quoted 4 lines

__func__ shouldn't be used in production code.

Users don't care about functions.

Fix it in the v6.

Quoted 49 lines
Quoted 49 lines
drivers/mfd/nct6694.c new file +49 -0
+             break;
+     case NCT6694_NO_RESPONSE_ERROR:
+             dev_dbg(dev, "%s: Command received no response!\n", __func__);
+             break;
+     case NCT6694_TIMEOUT_ERROR:
+             dev_dbg(dev, "%s: Command timed out!\n", __func__);
+             break;
+     case NCT6694_PENDING:
+             dev_dbg(dev, "%s: Command is pending!\n", __func__);
+             break;
+     default:
+             return -EINVAL;
+     }
+
+     return -EIO;
+}
+
+int nct6694_read_msg(struct nct6694 *nct6694,
+                  struct nct6694_cmd_header *cmd_hd,
+                  void *buf)
+{
+     union nct6694_usb_msg *msg = nct6694->usb_msg;
+     int tx_len, rx_len, ret;
+
+     guard(mutex)(&nct6694->access_lock);
+
+     /* Send command packet to USB device */
+     memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+     msg->cmd_header.hctrl = NCT6694_HCTRL_GET;
+
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+                        &msg->cmd_header, sizeof(*msg), &tx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive response packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        &msg->response_header, sizeof(*msg), &rx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive data packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &rx_len,
Quoted 9 lines

drivers/mfd/nct6694.c line 130

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     if (rx_len != le16_to_cpu(cmd_hd->len)) {
+             dev_dbg(&nct6694->udev->dev, "%s: Received length is not match!\n",

"does not match <something>"

Fix it in the v6.

Quoted 4 lines

drivers/mfd/nct6694.c line 129

Quoted 1 line
drivers/mfd/nct6694.c new file +1 -0
+                     __func__);

This should be dev_err().

Fix it in the v6.

Quoted 47 lines
Quoted 47 lines
drivers/mfd/nct6694.c new file +47 -0
+             return -EIO;
+     }
+
+     return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_read_msg);
+
+int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+                   void *buf)
+{
+     union nct6694_usb_msg *msg = nct6694->usb_msg;
+     int tx_len, rx_len, ret;
+
+     guard(mutex)(&nct6694->access_lock);
+
+     /* Send command packet to USB device */
+     memcpy(&msg->cmd_header, cmd_hd, sizeof(*cmd_hd));
+     msg->cmd_header.hctrl = NCT6694_HCTRL_SET;
+
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+                        &msg->cmd_header, sizeof(*msg), &tx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Send data packet to USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_sndbulkpipe(nct6694->udev, NCT6694_BULK_OUT_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &tx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive response packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        &msg->response_header, sizeof(*msg), &rx_len,
+                        nct6694->timeout);
+     if (ret)
+             return ret;
+
+     /* Receive data packet from USB device */
+     ret = usb_bulk_msg(nct6694->udev,
+                        usb_rcvbulkpipe(nct6694->udev, NCT6694_BULK_IN_EP),
+                        buf, le16_to_cpu(cmd_hd->len), &rx_len,
+                        nct6694->timeout);
Quoted 9 lines

drivers/mfd/nct6694.c line 184

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     if (ret)
+             return ret;
+
+     if (rx_len != le16_to_cpu(cmd_hd->len)) {
+             dev_dbg(&nct6694->udev->dev, "%s: Sent length is not match!\n",
+                     __func__);

This should be dev_err().

Fix it in the v6.

Quoted 53 lines
Quoted 53 lines
drivers/mfd/nct6694.c new file +53 -0
+             return -EIO;
+     }
+
+     return nct6694_response_err_handling(nct6694, msg->response_header.sts);
+}
+EXPORT_SYMBOL(nct6694_write_msg);
+
+static void usb_int_callback(struct urb *urb)
+{
+     struct nct6694 *nct6694 = urb->context;
+     struct device *dev = &nct6694->udev->dev;
+     unsigned int *int_status = urb->transfer_buffer;
+     int ret;
+
+     switch (urb->status) {
+     case 0:
+             break;
+     case -ECONNRESET:
+     case -ENOENT:
+     case -ESHUTDOWN:
+             return;
+     default:
+             goto resubmit;
+     }
+
+     while (*int_status) {
+             int irq = __ffs(*int_status);
+
+             generic_handle_irq_safe(irq_find_mapping(nct6694->domain, irq));
+             *int_status &= ~BIT(irq);
+     }
+
+resubmit:
+     ret = usb_submit_urb(urb, GFP_ATOMIC);
+     if (ret)
+             dev_dbg(dev, "%s: Failed to resubmit urb, status %pe",
+                     __func__, ERR_PTR(ret));
+}
+
+static void nct6694_irq_lock(struct irq_data *data)
+{
+     struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+
+     mutex_lock(&nct6694->irq_lock);
+}
+
+static void nct6694_irq_sync_unlock(struct irq_data *data)
+{
+     struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+
+     mutex_unlock(&nct6694->irq_lock);
+}
+
Quoted 9 lines

drivers/mfd/nct6694.c line 243

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+static void nct6694_irq_enable(struct irq_data *data)
+{
+     struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+     irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+     nct6694->irq_enable |= BIT(hwirq);

Changing a bit mask doesn't actually {en,dis}able an IRQ, right?

Yes, it's only used to record irq.

Quoted 47 lines
Quoted 47 lines
drivers/mfd/nct6694.c new file +47 -0
+}
+
+static void nct6694_irq_disable(struct irq_data *data)
+{
+     struct nct6694 *nct6694 = irq_data_get_irq_chip_data(data);
+     irq_hw_number_t hwirq = irqd_to_hwirq(data);
+
+     nct6694->irq_enable &= ~BIT(hwirq);
+}
+
+static struct irq_chip nct6694_irq_chip = {
+     .name = "nct6694-irq",
+     .flags = IRQCHIP_SKIP_SET_WAKE,
+     .irq_bus_lock = nct6694_irq_lock,
+     .irq_bus_sync_unlock = nct6694_irq_sync_unlock,
+     .irq_enable = nct6694_irq_enable,
+     .irq_disable = nct6694_irq_disable,
+};
+
+static int nct6694_irq_domain_map(struct irq_domain *d, unsigned int irq,
+                               irq_hw_number_t hw)
+{
+     struct nct6694 *nct6694 = d->host_data;
+
+     irq_set_chip_data(irq, nct6694);
+     irq_set_chip_and_handler(irq, &nct6694_irq_chip, handle_simple_irq);
+
+     return 0;
+}
+
+static void nct6694_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
+{
+     irq_set_chip_and_handler(irq, NULL, NULL);
+     irq_set_chip_data(irq, NULL);
+}
+
+static const struct irq_domain_ops nct6694_irq_domain_ops = {
+     .map    = nct6694_irq_domain_map,
+     .unmap  = nct6694_irq_domain_unmap,
+};
+
+static int nct6694_usb_probe(struct usb_interface *iface,
+                          const struct usb_device_id *id)
+{
+     struct usb_device *udev = interface_to_usbdev(iface);
+     struct usb_endpoint_descriptor *int_endpoint;
+     struct usb_host_interface *interface;
Quoted 9 lines

drivers/mfd/nct6694.c line 296

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     struct device *dev = &udev->dev;
+     struct nct6694 *nct6694;
+     int pipe, maxp;
+     int ret;
+
+     interface = iface->cur_altsetting;

Initialise this during allocation.

Should these be moved before usb_fill_int_urb()?

Quoted 7 lines
Quoted 7 lines
drivers/mfd/nct6694.c new file +7 -0
+     int_endpoint = &interface->endpoint[0].desc;
+     if (!usb_endpoint_is_int_in(int_endpoint))
+             return -ENODEV;
+
+     nct6694 = devm_kzalloc(dev, sizeof(*nct6694), GFP_KERNEL);
+     if (!nct6694)
+             return -ENOMEM;
Quoted 9 lines

drivers/mfd/nct6694.c line 310

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+
+     pipe = usb_rcvintpipe(udev, NCT6694_INT_IN_EP);
+     maxp = usb_maxpacket(udev, pipe);
+
+     nct6694->usb_msg = devm_kzalloc(dev, sizeof(union nct6694_usb_msg),
+                                     GFP_KERNEL);

Unwrap this - you can use up to 100-chars.

Fix it in the v6.

Quoted 15 lines
Quoted 15 lines
drivers/mfd/nct6694.c new file +15 -0
+     if (!nct6694->usb_msg)
+             return -ENOMEM;
+
+     nct6694->int_buffer = devm_kzalloc(dev, maxp, GFP_KERNEL);
+     if (!nct6694->int_buffer)
+             return -ENOMEM;
+
+     nct6694->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
+     if (!nct6694->int_in_urb)
+             return -ENOMEM;
+
+     nct6694->domain = irq_domain_add_simple(NULL, NCT6694_NR_IRQS, 0,
+                                             &nct6694_irq_domain_ops,
+                                             nct6694);
+     if (!nct6694->domain) {
Quoted 9 lines

drivers/mfd/nct6694.c line 331

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+             ret = -ENODEV;
+             goto err_urb;
+     }
+
+     nct6694->udev = udev;
+     nct6694->timeout = NCT6694_URB_TIMEOUT; /* Wait until urb complete */

"URB completes"?

Fix it in the v6.

Quoted 7 lines
Quoted 7 lines
drivers/mfd/nct6694.c new file +7 -0
+
+     devm_mutex_init(dev, &nct6694->access_lock);
+     devm_mutex_init(dev, &nct6694->irq_lock);
+
+     usb_fill_int_urb(nct6694->int_in_urb, udev, pipe,
+                      nct6694->int_buffer, maxp, usb_int_callback,
+                      nct6694, int_endpoint->bInterval);
Quoted 9 lines

drivers/mfd/nct6694.c line 344

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     ret = usb_submit_urb(nct6694->int_in_urb, GFP_KERNEL);
+     if (ret)
+             goto err_urb;
+
+     dev_set_drvdata(dev, nct6694);
+     usb_set_intfdata(iface, nct6694);

These two do the same thing. You don't need both.

Fix it in the v6.

Quoted 13 lines
Quoted 13 lines
drivers/mfd/nct6694.c new file +13 -0
+     ret = mfd_add_hotplug_devices(dev, nct6694_dev, ARRAY_SIZE(nct6694_dev));
+     if (ret)
+             goto err_mfd;
+
+     return 0;
+
+err_mfd:
+     usb_kill_urb(nct6694->int_in_urb);
+err_urb:
+     usb_free_urb(nct6694->int_in_urb);
+     return ret;
+}
+
Quoted 9 lines

drivers/mfd/nct6694.c line 364

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+static void nct6694_usb_disconnect(struct usb_interface *iface)
+{
+     struct usb_device *udev = interface_to_usbdev(iface);
+     struct nct6694 *nct6694 = usb_get_intfdata(iface);
+
+     mfd_remove_devices(&udev->dev);

Does devm_* work here?

No, or do you think mfd_add_hotplug_devices() should be replaced with devm_mfd_add_devices()?

Quoted 16 lines
Quoted 16 lines
drivers/mfd/nct6694.c new file +16 -0
+     usb_kill_urb(nct6694->int_in_urb);
+     usb_free_urb(nct6694->int_in_urb);
+}
+
+static const struct usb_device_id nct6694_ids[] = {
+     { USB_DEVICE_AND_INTERFACE_INFO(NCT6694_VENDOR_ID,
+                                     NCT6694_PRODUCT_ID,
+                                     0xFF, 0x00, 0x00)},
+     {}
+};
+MODULE_DEVICE_TABLE(usb, nct6694_ids);
+
+static struct usb_driver nct6694_usb_driver = {
+     .name   = "nct6694",
+     .id_table = nct6694_ids,
+     .probe = nct6694_usb_probe,
Quoted 9 lines

drivers/mfd/nct6694.c line 386

Quoted 6 lines
drivers/mfd/nct6694.c new file +6 -0
+     .disconnect = nct6694_usb_disconnect,
+};
+
+module_usb_driver(nct6694_usb_driver);
+
+MODULE_DESCRIPTION("USB-MFD driver for NCT6694");

Remove all references to MFD.

Fix it in the v6.

Quoted 18 lines
Quoted 18 lines
drivers/mfd/nct6694.c new file +2 -0
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
include/linux/mfd/nct6694.h new file +10 -0
new file mode 100644
index 000000000000..67ca835589ad
@@ -0,0 +1,109 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Nuvoton NCT6694 USB transaction and data structure.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#ifndef __MFD_NCT6694_H
+#define __MFD_NCT6694_H
+
Quoted 9 lines

include/linux/mfd/nct6694.h line 16

Quoted 6 lines
include/linux/mfd/nct6694.h new file +6 -0
+#define NCT6694_DEV_GPIO     "nct6694-gpio"
+#define NCT6694_DEV_I2C              "nct6694-i2c"
+#define NCT6694_DEV_CAN              "nct6694-can"
+#define NCT6694_DEV_WDT              "nct6694-wdt"
+#define NCT6694_DEV_HWMON    "nct6694-hwmon"
+#define NCT6694_DEV_RTC              "nct6694-rtc"

Use raw strings in place please.

Fix it in the v6.

Quoted 6 lines
Quoted 6 lines
include/linux/mfd/nct6694.h new file +6 -0
+#define NCT6694_VENDOR_ID    0x0416
+#define NCT6694_PRODUCT_ID   0x200B
+#define NCT6694_INT_IN_EP    0x81
+#define NCT6694_BULK_IN_EP   0x02
+#define NCT6694_BULK_OUT_EP  0x03
+
Quoted 9 lines

include/linux/mfd/nct6694.h line 29

Quoted 6 lines
include/linux/mfd/nct6694.h new file +6 -0
+#define NCT6694_HCTRL_SET    0x40
+#define NCT6694_HCTRL_GET    0x80
+
+#define NCT6694_URB_TIMEOUT  1000
+
+enum nct6694_irq_id {

These are never used.

Some of these numbers ared used by child devices to map the irq_domain. (e.g. NCT6694_IRQ_CAN1, NCT6694_IRQ_RTC)

Quoted 55 lines
Quoted 55 lines
include/linux/mfd/nct6694.h new file +55 -0
+     NCT6694_IRQ_GPIO0 = 0,
+     NCT6694_IRQ_GPIO1,
+     NCT6694_IRQ_GPIO2,
+     NCT6694_IRQ_GPIO3,
+     NCT6694_IRQ_GPIO4,
+     NCT6694_IRQ_GPIO5,
+     NCT6694_IRQ_GPIO6,
+     NCT6694_IRQ_GPIO7,
+     NCT6694_IRQ_GPIO8,
+     NCT6694_IRQ_GPIO9,
+     NCT6694_IRQ_GPIOA,
+     NCT6694_IRQ_GPIOB,
+     NCT6694_IRQ_GPIOC,
+     NCT6694_IRQ_GPIOD,
+     NCT6694_IRQ_GPIOE,
+     NCT6694_IRQ_GPIOF,
+     NCT6694_IRQ_CAN1,
+     NCT6694_IRQ_CAN2,
+     NCT6694_IRQ_RTC,
+     NCT6694_NR_IRQS,
+};
+
+enum nct6694_response_err_status {
+     NCT6694_NO_ERROR = 0,
+     NCT6694_FORMAT_ERROR,
+     NCT6694_RESERVED1,
+     NCT6694_RESERVED2,
+     NCT6694_NOT_SUPPORT_ERROR,
+     NCT6694_NO_RESPONSE_ERROR,
+     NCT6694_TIMEOUT_ERROR,
+     NCT6694_PENDING,
+};
+
+struct __packed nct6694_cmd_header {
+     u8 rsv1;
+     u8 mod;
+     union __packed {
+             __le16 offset;
+             struct __packed {
+                     u8 cmd;
+                     u8 sel;
+             };
+     };
+     u8 hctrl;
+     u8 rsv2;
+     __le16 len;
+};
+
+struct __packed nct6694_response_header {
+     u8 sequence_id;
+     u8 sts;
+     u8 reserved[4];
+     __le16 len;
+};
+
Quoted 9 lines

include/linux/mfd/nct6694.h line 90

Quoted 6 lines
include/linux/mfd/nct6694.h new file +6 -0
+union __packed nct6694_usb_msg {
+     struct nct6694_cmd_header cmd_header;
+     struct nct6694_response_header response_header;
+};
+
+struct nct6694 {

Do all of these values need to be stored?

I think that's right.

Quoted 11 lines

include/linux/mfd/nct6694.h line 99

Quoted 9 lines
include/linux/mfd/nct6694.h new file +9 -0
+     struct usb_device *udev;
+     struct urb *int_in_urb;
+     struct irq_domain *domain;
+     struct mutex access_lock;
+     struct mutex irq_lock;
+     union nct6694_usb_msg *usb_msg;
+     unsigned char *int_buffer;
+     unsigned int irq_enable;
+     /* time in msec to wait for the urb to the complete */

"Time" "URB"

Fix it in the v6.

Quoted 5 lines
Quoted 5 lines
include/linux/mfd/nct6694.h new file +5 -0
+     long timeout;
+};
+
+int nct6694_read_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+                  void *buf);
Quoted 9 lines
Quoted 8 lines
include/linux/mfd/nct6694.h new file +5 -1
+
+int nct6694_write_msg(struct nct6694 *nct6694, struct nct6694_cmd_header *cmd_hd,
+                   void *buf);
+
+#endif
--

2.34.1

Best regards,
Ming

author
#4 lore raw

PATCH v5 2/7 gpio: Add Nuvoton NCT6694 GPIO support

In reply to Ming Yu

This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                 |   1 +
 drivers/gpio/Kconfig        |  12 +
 drivers/gpio/Makefile       |   1 +
 drivers/gpio/gpio-nct6694.c | 458 ++++++++++++++++++++++++++++++++++++
 4 files changed, 472 insertions(+)
 create mode 100644 drivers/gpio/gpio-nct6694.c
MAINTAINERS +1 -0
index 79568f82b98a..f752051f4c3b 100644
@@ -16721,6 +16721,7 @@ NUVOTON NCT6694 MFD DRIVER
 M:	Ming Yu <[email protected]>
 L:	[email protected]
 S:	Supported
+F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	include/linux/mfd/nct6694.h
 
drivers/gpio/Kconfig +12 -0
index 93ee3aa092f8..808fbfdbff3b 100644
@@ -1461,6 +1461,18 @@ config GPIO_MAX77650
 	  GPIO driver for MAX77650/77651 PMIC from Maxim Semiconductor.
 	  These chips have a single pin that can be configured as GPIO.
 
+config GPIO_NCT6694
+	tristate "Nuvoton NCT6694 GPIO controller support"
+	depends on MFD_NCT6694
+	select GENERIC_IRQ_CHIP
+	select GPIOLIB_IRQCHIP
+	help
+	  This driver supports 8 GPIO pins per bank that can all be interrupt
+	  sources.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called gpio-nct6694.
+
 config GPIO_PALMAS
 	bool "TI PALMAS series PMICs GPIO"
 	depends on MFD_PALMAS
drivers/gpio/Makefile +1 -0
index af3ba4d81b58..def8487540ab 100644
@@ -121,6 +121,7 @@ obj-$(CONFIG_GPIO_MT7621)		+= gpio-mt7621.o
 obj-$(CONFIG_GPIO_MVEBU)		+= gpio-mvebu.o
 obj-$(CONFIG_GPIO_MXC)			+= gpio-mxc.o
 obj-$(CONFIG_GPIO_MXS)			+= gpio-mxs.o
+obj-$(CONFIG_GPIO_NCT6694)		+= gpio-nct6694.o
 obj-$(CONFIG_GPIO_NOMADIK)		+= gpio-nomadik.o
 obj-$(CONFIG_GPIO_NPCM_SGPIO)		+= gpio-npcm-sgpio.o
 obj-$(CONFIG_GPIO_OCTEON)		+= gpio-octeon.o
drivers/gpio/gpio-nct6694.c new file +458 -0

458 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#6 lore raw

Re: [PATCH v5 2/7] gpio: Add Nuvoton NCT6694 GPIO support

Dear Bartosz,

Understood!

Thanks,
Ming

Bartosz Golaszewski <[email protected]> 於 2025年1月14日 週二 下午8:12寫道:

Quoted 13 lines · Ming Yu
Quoted 6 lines

This driver supports GPIO and IRQ functionality for NCT6694 MFD device based on USB interface.

---

Please pick up review tags when resending.

author
#7 lore raw

PATCH v5 3/7 i2c: Add Nuvoton NCT6694 I2C support

In reply to Ming Yu

This driver supports I2C adapter functionality for NCT6694 MFD device based on USB interface, each I2C controller use default baudrate(100K).

---
 MAINTAINERS                      |   1 +
 drivers/i2c/busses/Kconfig       |  10 ++
 drivers/i2c/busses/Makefile      |   1 +
 drivers/i2c/busses/i2c-nct6694.c | 157 +++++++++++++++++++++++++++++++
 4 files changed, 169 insertions(+)
 create mode 100644 drivers/i2c/busses/i2c-nct6694.c
MAINTAINERS +1 -0
index f752051f4c3b..4e72f749cdf2 100644
@@ -16722,6 +16722,7 @@ M:	Ming Yu <[email protected]>
 L:	[email protected]
 S:	Supported
 F:	drivers/gpio/gpio-nct6694.c
+F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	include/linux/mfd/nct6694.h
 
drivers/i2c/busses/Kconfig +10 -0
index ceb3ecdf884b..a3f60e6953bd 100644
@@ -1328,6 +1328,16 @@ config I2C_LJCA
 	  This driver can also be built as a module.  If so, the module
 	  will be called i2c-ljca.
 
+config I2C_NCT6694
+	tristate "Nuvoton NCT6694 I2C adapter support"
+	depends on MFD_NCT6694
+	help
+	  If you say yes to this option, support will be included for Nuvoton
+	  NCT6694, a USB to I2C interface.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called i2c-nct6694.
+
 config I2C_CP2615
 	tristate "Silicon Labs CP2615 USB sound card and I2C adapter"
 	depends on USB
drivers/i2c/busses/Makefile +1 -0
index 1c2a4510abe4..c5a60116dd54 100644
@@ -133,6 +133,7 @@ obj-$(CONFIG_I2C_GXP)		+= i2c-gxp.o
 obj-$(CONFIG_I2C_DIOLAN_U2C)	+= i2c-diolan-u2c.o
 obj-$(CONFIG_I2C_DLN2)		+= i2c-dln2.o
 obj-$(CONFIG_I2C_LJCA)		+= i2c-ljca.o
+obj-$(CONFIG_I2C_NCT6694)	+= i2c-nct6694.o
 obj-$(CONFIG_I2C_CP2615) += i2c-cp2615.o
 obj-$(CONFIG_I2C_PARPORT)	+= i2c-parport.o
 obj-$(CONFIG_I2C_PCI1XXXX)	+= i2c-mchp-pci1xxxx.o
drivers/i2c/busses/i2c-nct6694.c new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#8 lore raw

PATCH v5 4/7 can: Add Nuvoton NCT6694 CAN support

In reply to Ming Yu

This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                         |   1 +
 drivers/net/can/usb/Kconfig         |  10 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 856 ++++++++++++++++++++++++++++
 4 files changed, 868 insertions(+)
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
MAINTAINERS +1 -0
index 4e72f749cdf2..6e9b78202d6f 100644
@@ -16724,6 +16724,7 @@ S:	Supported
 F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
+F:	drivers/net/can/usb/nct6694_canfd.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/usb/Kconfig +10 -0
index 9dae0c71a2e1..53254012cdc4 100644
@@ -133,6 +133,16 @@ config CAN_MCBA_USB
 	  This driver supports the CAN BUS Analyzer interface
 	  from Microchip (http://www.microchip.com/development-tools/).
 
+config CAN_NCT6694
+	tristate "Nuvoton NCT6694 Socket CANfd support"
+	depends on MFD_NCT6694
+	help
+	  If you say yes to this option, support will be included for Nuvoton
+	  NCT6694, a USB device to socket CANfd controller.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called nct6694_canfd.
+
 config CAN_PEAK_USB
 	tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
 	help
drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +856 -0

856 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Hi Ming,

Now the structure finally looks good! I did a deeper review than before.

Right now, I am mostly concerned of the double use of mutexes:

- nct6694_can_priv->lock in this can driver
- nct6694->access_lock in the core driver

checkpatch.pl actually suggests you to add comments to those mutexes:

CHECK: struct mutex definition without comment #146: FILE: drivers/net/can/usb/nct6694_canfd.c:146:
+ struct mutex lock;

Quoted 29 lines · Ming Yu

This driver supports Socket CANfd functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                         |   1 +
 drivers/net/can/usb/Kconfig         |  10 +
 drivers/net/can/usb/Makefile        |   1 +
 drivers/net/can/usb/nct6694_canfd.c | 856 ++++++++++++++++++++++++++++
 4 files changed, 868 insertions(+)
 create mode 100644 drivers/net/can/usb/nct6694_canfd.c
MAINTAINERS +1 -0
index 4e72f749cdf2..6e9b78202d6f 100644
@@ -16724,6 +16724,7 @@ S:      Supported
 F:     drivers/gpio/gpio-nct6694.c
 F:     drivers/i2c/busses/i2c-nct6694.c
 F:     drivers/mfd/nct6694.c
+F:     drivers/net/can/usb/nct6694_canfd.c
 F:     include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/net/can/usb/Kconfig +0 -0
index 9dae0c71a2e1..53254012cdc4 100644

drivers/net/can/usb/Kconfig config CAN_MCBA_USB · line 138

Quoted 6 lines
drivers/net/can/usb/Kconfig +3 -0
@@ -133,6 +133,16 @@ config CAN_MCBA_USB
          This driver supports the CAN BUS Analyzer interface
          from Microchip (http://www.microchip.com/development-tools/).
 
+config CAN_NCT6694
+       tristate "Nuvoton NCT6694 Socket CANfd support"
+       depends on MFD_NCT6694

Your driver uses the CAN rx offload. You need to select it here.

select CAN_RX_OFFLOAD

drivers/net/can/usb/Kconfig config CAN_MCBA_USB · line 144

Quoted 6 lines
drivers/net/can/usb/Kconfig +6 -0
+       help
+         If you say yes to this option, support will be included for Nuvoton
+         NCT6694, a USB device to socket CANfd controller.
+
+         This driver can also be built as a module. If so, the module will
+         be called nct6694_canfd.

Here, the name is nct6694_canfd...

Quoted 29 lines
drivers/net/can/usb/Kconfig +0 -0
 config CAN_PEAK_USB
        tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD"
        help
drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
new file mode 100644
index 000000000000..7a15c39021ff
@@ -0,0 +1,856 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/can/dev.h>
+#include <linux/can/rx-offload.h>

drivers/net/can/usb/nct6694_canfd.c line 19

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+#include <linux/ethtool.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/platform_device.h>
+
+#define DRVNAME "nct6694-can"

... but here, it is nct6694-can.

Use a consistent name between the module name and the driver name.

drivers/net/can/usb/nct6694_canfd.c line 30

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+/*
+ * 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_CAN_MOD                        0x05
+
+/* Command 00h - CAN Setting and Initialization */
+#define NCT6694_CAN_SETTING            0x00
+#define NCT6694_CAN_SETTING_SEL(idx)   (idx ? 0x01 : 0x00)

What are the possible values for idx? Isn't it only 0 or 1? If so, no need for this NCT6694_CAN_SETTING_SEL() macro. Directly assign the channel index to the selector field.

drivers/net/can/usb/nct6694_canfd.c line 42

Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+#define NCT6694_CAN_SETTING_CTRL1_MON  BIT(0)
+#define NCT6694_CAN_SETTING_CTRL1_NISO BIT(1)
+#define NCT6694_CAN_SETTING_CTRL1_LBCK BIT(2)
+
+/* Command 01h - CAN Information */
+#define NCT6694_CAN_INFORMATION                0x01
+#define NCT6694_CAN_INFORMATION_SEL    0x00
+
+/* Command 02h - CAN Event */
+#define NCT6694_CAN_EVENT              0x02
+#define NCT6694_CAN_EVENT_SEL(idx, mask)       \
+       ((idx ? 0x80 : 0x00) | ((mask) & 0xFF))

Can idx and mask really overlap? Shouldn't this be:

  #define NCT6694_CAN_EVENT_SEL(idx, mask)  \
          ((idx ? 0x80 : 0x00) | ((mask) & 0x7F))

drivers/net/can/usb/nct6694_canfd.c line 53

Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+#define NCT6694_CAN_EVENT_ERR          BIT(0)
+#define NCT6694_CAN_EVENT_STATUS       BIT(1)
+#define NCT6694_CAN_EVENT_TX_EVT       BIT(2)
+#define NCT6694_CAN_EVENT_RX_EVT       BIT(3)
+#define NCT6694_CAN_EVENT_REC          BIT(4)
+#define NCT6694_CAN_EVENT_TEC          BIT(5)
+#define NCT6694_CAN_EVENT_MASK         GENMASK(3, 0)
+#define NCT6694_CAN_EVT_TX_FIFO_EMPTY  BIT(7)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_DATA_LOST   BIT(5)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_HALF_FULL   BIT(6)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_DATA_IN     BIT(7)  /* Read-clear*/

Some of those macro are not used:

drivers/net/can/usb/nct6694_canfd.c:52: warning: macro
"NCT6694_CAN_EVT_RX_HALF_FULL" is not used [-Wunused-macros]

     52 | #define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6) /* Read-clear */
        |
  drivers/net/can/usb/nct6694_canfd.c:43: warning: macro
"NCT6694_CAN_EVENT_ERR" is not used [-Wunused-macros]
     43 | #define NCT6694_CAN_EVENT_ERR  BIT(0)
        |
  drivers/net/can/usb/nct6694_canfd.c:44: warning: macro
"NCT6694_CAN_EVENT_STATUS" is not used [-Wunused-macros]
     44 | #define NCT6694_CAN_EVENT_STATUS BIT(1)
        |
  drivers/net/can/usb/nct6694_canfd.c:46: warning: macro
"NCT6694_CAN_EVENT_RX_EVT" is not used [-Wunused-macros]
     46 | #define NCT6694_CAN_EVENT_RX_EVT BIT(3)
        |
  drivers/net/can/usb/nct6694_canfd.c:45: warning: macro
"NCT6694_CAN_EVENT_TX_EVT" is not used [-Wunused-macros]
     45 | #define NCT6694_CAN_EVENT_TX_EVT BIT(2)
        |

Is this OK?

drivers/net/can/usb/nct6694_canfd.c line 63

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
+/* Command 10h - CAN Deliver */
+#define NCT6694_CAN_DELIVER            0x10
+#define NCT6694_CAN_DELIVER_SEL(buf_cnt)       \
+       ((buf_cnt) & 0xFF)
+
+/* Command 11h - CAN Receive */
+#define NCT6694_CAN_RECEIVE            0x11
+#define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)  \
+       ((idx ? 0x80 : 0x00) | ((buf_cnt) & 0xFF))

Can idx and buf_cnt really overlap? Shouldn't this be:

  #define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)  \
          ((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))
Quoted 53 lines
drivers/net/can/usb/nct6694_canfd.c new file +53 -0
+#define NCT6694_CAN_FRAME_TAG_CAN0     0xC0
+#define NCT6694_CAN_FRAME_TAG_CAN1     0xC1
+#define NCT6694_CAN_FRAME_FLAG_EFF     BIT(0)
+#define NCT6694_CAN_FRAME_FLAG_RTR     BIT(1)
+#define NCT6694_CAN_FRAME_FLAG_FD      BIT(2)
+#define NCT6694_CAN_FRAME_FLAG_BRS     BIT(3)
+#define NCT6694_CAN_FRAME_FLAG_ERR     BIT(4)
+
+#define NCT6694_NAPI_WEIGHT            32
+
+enum nct6694_event_err {
+       NCT6694_CAN_EVT_ERR_NO_ERROR = 0,
+       NCT6694_CAN_EVT_ERR_CRC_ERROR,
+       NCT6694_CAN_EVT_ERR_STUFF_ERROR,
+       NCT6694_CAN_EVT_ERR_ACK_ERROR,
+       NCT6694_CAN_EVT_ERR_FORM_ERROR,
+       NCT6694_CAN_EVT_ERR_BIT_ERROR,
+       NCT6694_CAN_EVT_ERR_TIMEOUT_ERROR,
+       NCT6694_CAN_EVT_ERR_UNKNOWN_ERROR,
+};
+
+enum nct6694_event_status {
+       NCT6694_CAN_EVT_STS_ERROR_ACTIVE = 0,
+       NCT6694_CAN_EVT_STS_ERROR_PASSIVE,
+       NCT6694_CAN_EVT_STS_BUS_OFF,
+       NCT6694_CAN_EVT_STS_WARNING,
+};
+
+struct __packed nct6694_can_setting {
+       __le32 nbr;
+       __le32 dbr;
+       u8 active;
+       u8 reserved[3];
+       __le16 ctrl1;
+       __le16 ctrl2;
+       __le32 nbtp;
+       __le32 dbtp;
+};
+
+struct __packed nct6694_can_information {
+       u8 tx_fifo_cnt;
+       u8 rx_fifo_cnt;
+       u8 reserved[2];
+       __le32 can_clk;
+};
+
+struct __packed nct6694_can_event {
+       u8 err;
+       u8 status;
+       u8 tx_evt;
+       u8 rx_evt;
+       u8 rec;
+       u8 tec;

drivers/net/can/usb/nct6694_canfd.c line 127

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       u8 reserved[2];
+};
+
+struct __packed nct6694_can_frame {
+       u8 tag;
+       u8 flag;
+       u8 reserved;
+       u8 length;
+       __le32 id;
+       u8 data[64];

Nitpick, use CANFD_MAX_DLEN here:

          u8 data[CANFD_MAX_DLEN];
Quoted 99 lines
drivers/net/can/usb/nct6694_canfd.c new file +99 -0
+};
+
+union __packed nct6694_can_tx {
+       struct nct6694_can_frame frame;
+       struct nct6694_can_setting setting;
+};
+
+union __packed nct6694_can_rx {
+       struct nct6694_can_event event[2];
+       struct nct6694_can_frame frame;
+       struct nct6694_can_information info;
+};
+
+struct nct6694_can_priv {
+       struct can_priv can;    /* must be the first member */
+       struct can_rx_offload offload;
+       struct net_device *ndev;
+       struct nct6694 *nct6694;
+       struct mutex lock;
+       struct sk_buff *tx_skb;
+       struct workqueue_struct *wq;
+       struct work_struct tx_work;
+       union nct6694_can_tx *tx;
+       union nct6694_can_rx *rx;
+       unsigned char can_idx;
+};
+
+static inline struct nct6694_can_priv *rx_offload_to_priv(struct can_rx_offload *offload)
+{
+       return container_of(offload, struct nct6694_can_priv, offload);
+}
+
+static const struct can_bittiming_const nct6694_can_bittiming_nominal_const = {
+       .name = DRVNAME,
+       .tseg1_min = 2,
+       .tseg1_max = 256,
+       .tseg2_min = 2,
+       .tseg2_max = 128,
+       .sjw_max = 128,
+       .brp_min = 1,
+       .brp_max = 511,
+       .brp_inc = 1,
+};
+
+static const struct can_bittiming_const nct6694_can_bittiming_data_const = {
+       .name = DRVNAME,
+       .tseg1_min = 1,
+       .tseg1_max = 32,
+       .tseg2_min = 1,
+       .tseg2_max = 16,
+       .sjw_max = 16,
+       .brp_min = 1,
+       .brp_max = 31,
+       .brp_inc = 1,
+};
+
+static void nct6694_can_rx_offload(struct can_rx_offload *offload,
+                                  struct sk_buff *skb)
+{
+       struct nct6694_can_priv *priv = rx_offload_to_priv(offload);
+       int ret;
+
+       ret = can_rx_offload_queue_tail(offload, skb);
+       if (ret)
+               priv->ndev->stats.rx_fifo_errors++;
+}
+
+static void nct6694_can_handle_lost_msg(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct net_device_stats *stats = &ndev->stats;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+
+       netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
+
+       stats->rx_errors++;
+       stats->rx_over_errors++;
+
+       skb = alloc_can_err_skb(ndev, &cf);
+       if (!skb)
+               return;
+
+       cf->can_id |= CAN_ERR_CRTL;
+       cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_rx(struct net_device *ndev, u8 rx_evt)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_frame *frame = &priv->rx->frame;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_RECEIVE,
+               .sel = NCT6694_CAN_RECEIVE_SEL(priv->can_idx, 1),
+               .len = cpu_to_le16(sizeof(*frame))
+       };

drivers/net/can/usb/nct6694_canfd.c line 236

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       struct canfd_frame *cfd;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+       int ret;
+
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
+       if (ret)
+               return;
+
+       if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) {

Reduce scope of variable when possible: move declaration of cfd here:

                struct canfd_frame *cfd;

drivers/net/can/usb/nct6694_canfd.c line 242

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               skb = alloc_canfd_skb(priv->ndev, &cfd);
+               if (!skb)
+                       return;
+
+               cfd->can_id = le32_to_cpu(frame->id);
+               cfd->len = frame->length;

No. I asked you to sanitize the length in this message:

https://lore.kernel.org/linux-can/[email protected]/

Never use the length as is.

drivers/net/can/usb/nct6694_canfd.c line 251

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF)
+                       cfd->can_id |= CAN_EFF_FLAG;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_BRS)
+                       cfd->flags |= CANFD_BRS;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_ERR)
+                       cfd->flags |= CANFD_ESI;
+
+               memcpy(cfd->data, frame->data, cfd->len);
+       } else {

Reduce scope of variable when possible: move declaration of cf here:

                struct canfd_frame *cf;

drivers/net/can/usb/nct6694_canfd.c line 257

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               skb = alloc_can_skb(priv->ndev, &cf);
+               if (!skb)
+                       return;
+
+               cf->can_id = le32_to_cpu(frame->id);
+               cf->len = frame->length;

Ditto, sanitize the length.

drivers/net/can/usb/nct6694_canfd.c line 263

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF)
+                       cf->can_id |= CAN_EFF_FLAG;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_RTR)
+                       cf->can_id |= CAN_RTR_FLAG;
+
+               memcpy(cf->data, frame->data, cf->len);

Only copy can data if the frame is not an RTR frame.

                  if (frame->flag & NCT6694_CAN_FRAME_FLAG_RTR)
                          cf->can_id |= CAN_RTR_FLAG;
                  else
                          memcpy(cf->data, frame->data, cf->len);

I already asked you to do this in below comment:

https://lore.kernel.org/linux-can/[email protected]/

Quoted 26 lines
drivers/net/can/usb/nct6694_canfd.c new file +26 -0
+       }
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_clean(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+       if (priv->tx_skb || netif_queue_stopped(ndev))
+               ndev->stats.tx_errors++;
+       dev_kfree_skb(priv->tx_skb);
+       priv->tx_skb = NULL;
+}
+
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+       bec->rxerr = evt[priv->can_idx].rec;
+       bec->txerr = evt[priv->can_idx].tec;
+
+       return 0;
+}
+
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+                                           u8 status)

drivers/net/can/usb/nct6694_canfd.c line 318

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       enum can_state new_state = priv->can.state;
+       enum can_state rx_state, tx_state;
+       struct can_berr_counter bec;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+
+       nct6694_can_get_berr_counter(ndev, &bec);
+       can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state);

Here, you set up tx_state and rx_state...

Quoted 16 lines
drivers/net/can/usb/nct6694_canfd.c new file +16 -0
+       switch (status) {
+       case NCT6694_CAN_EVT_STS_ERROR_ACTIVE:
+               new_state = CAN_STATE_ERROR_ACTIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_ERROR_PASSIVE:
+               new_state = CAN_STATE_ERROR_PASSIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_BUS_OFF:
+               new_state = CAN_STATE_BUS_OFF;
+               break;
+       case NCT6694_CAN_EVT_STS_WARNING:
+               new_state = CAN_STATE_ERROR_WARNING;
+               break;
+       default:
+               netdev_err(ndev, "Receive unknown CAN status event.\n");
+               return;

drivers/net/can/usb/nct6694_canfd.c line 345

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       }
+
+       /* state hasn't changed */
+       if (new_state == priv->can.state)
+               return;
+
+       skb = alloc_can_err_skb(ndev, &cf);
+
+       tx_state = bec.txerr >= bec.rxerr ? new_state : 0;
+       rx_state = bec.txerr <= bec.rxerr ? new_state : 0;

... but you never used the values returned by can_state_get_by_berr_counter() and just overwrote the tx and rx state.

What is the logic here? Why do you need to manually adjust those two values? Isn't the logic in can_change_state() sufficient?

drivers/net/can/usb/nct6694_canfd.c line 348

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       can_change_state(ndev, cf, tx_state, rx_state);
+
+       if (new_state == CAN_STATE_BUS_OFF) {

Same for the new_state. The function can_change_state() calculate the new state from tx_state and rx_state and save it under can_priv->state. But here, you do your own calculation.

Only keep one of the two. If your device already tells you the state, then fine! Just use the information from your device and do not use can_change_state(). Here, you are doing double work resulting in a weird mix.

Quoted 79 lines
drivers/net/can/usb/nct6694_canfd.c new file +79 -0
+               can_bus_off(ndev);
+       } else if (skb) {
+               cf->can_id |= CAN_ERR_CNT;
+               cf->data[6] = bec.txerr;
+               cf->data[7] = bec.rxerr;
+       }
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_handle_bus_err(struct net_device *ndev, u8 bus_err)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct can_frame *cf;
+       struct sk_buff *skb;
+
+       if (bus_err == NCT6694_CAN_EVT_ERR_NO_ERROR)
+               return;
+
+       priv->can.can_stats.bus_error++;
+
+       skb = alloc_can_err_skb(ndev, &cf);
+       if (skb)
+               cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
+
+       switch (bus_err) {
+       case NCT6694_CAN_EVT_ERR_CRC_ERROR:
+               netdev_dbg(ndev, "CRC error\n");
+               ndev->stats.rx_errors++;
+               if (skb)
+                       cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
+               break;
+
+       case NCT6694_CAN_EVT_ERR_STUFF_ERROR:
+               netdev_dbg(ndev, "Stuff error\n");
+               ndev->stats.rx_errors++;
+               if (skb)
+                       cf->data[2] |= CAN_ERR_PROT_STUFF;
+               break;
+
+       case NCT6694_CAN_EVT_ERR_ACK_ERROR:
+               netdev_dbg(ndev, "Ack error\n");
+               ndev->stats.tx_errors++;
+               if (skb) {
+                       cf->can_id |= CAN_ERR_ACK;
+                       cf->data[2] |= CAN_ERR_PROT_TX;
+               }
+               break;
+
+       case NCT6694_CAN_EVT_ERR_FORM_ERROR:
+               netdev_dbg(ndev, "Form error\n");
+               ndev->stats.rx_errors++;
+               if (skb)
+                       cf->data[2] |= CAN_ERR_PROT_FORM;
+               break;
+
+       case NCT6694_CAN_EVT_ERR_BIT_ERROR:
+               netdev_dbg(ndev, "Bit error\n");
+               ndev->stats.tx_errors++;
+               if (skb)
+                       cf->data[2] |= CAN_ERR_PROT_TX | CAN_ERR_PROT_BIT;
+               break;
+
+       default:
+               break;
+       }
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_tx_irq(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct net_device_stats *stats = &ndev->stats;
+
+       guard(mutex)(&priv->lock);
+       stats->tx_bytes += can_get_echo_skb(ndev, 0, NULL);
+       stats->tx_packets++;
+       netif_wake_queue(ndev);

drivers/net/can/usb/nct6694_canfd.c line 437

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+}
+
+static irqreturn_t nct6694_can_irq(int irq, void *data)
+{
+       struct net_device *ndev = data;
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 tx_evt, rx_evt, bus_err, can_status;
+       u8 mask_sts = NCT6694_CAN_EVENT_MASK;

No need for the mask_sts variable. Directly use NCT6694_CAN_EVENT_MASK.

drivers/net/can/usb/nct6694_canfd.c line 442

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+       irqreturn_t handled = IRQ_NONE;
+       int can_idx = priv->can_idx;
+       int ret;
+
+       scoped_guard(mutex, &priv->lock) {

Reduce scope of variable when possible: move the declarations of cmd_hd and ret here.

Quoted 61 lines
drivers/net/can/usb/nct6694_canfd.c new file +61 -0
+               cmd_hd = (struct nct6694_cmd_header) {
+                       .mod = NCT6694_CAN_MOD,
+                       .cmd = NCT6694_CAN_EVENT,
+                       .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask_sts),
+                       .len = cpu_to_le16(sizeof(priv->rx->event))
+               };
+
+               ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+               if (ret < 0)
+                       return handled;
+
+               tx_evt = evt[can_idx].tx_evt;
+               rx_evt = evt[can_idx].rx_evt;
+               bus_err = evt[can_idx].err;
+               can_status = evt[can_idx].status;
+       }
+
+       if (rx_evt & NCT6694_CAN_EVT_RX_DATA_IN) {
+               nct6694_can_rx(ndev, rx_evt);
+               handled = IRQ_HANDLED;
+       }
+
+       if (rx_evt & NCT6694_CAN_EVT_RX_DATA_LOST) {
+               nct6694_can_handle_lost_msg(ndev);
+               handled = IRQ_HANDLED;
+       }
+
+       if (can_status) {
+               nct6694_can_handle_state_change(ndev, can_status);
+               handled = IRQ_HANDLED;
+       }
+
+       if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) {
+               nct6694_handle_bus_err(ndev, bus_err);
+               handled = IRQ_HANDLED;
+       }
+
+       if (handled)
+               can_rx_offload_threaded_irq_finish(&priv->offload);
+
+       if (tx_evt & NCT6694_CAN_EVT_TX_FIFO_EMPTY)
+               nct6694_can_tx_irq(ndev);
+
+       return handled;
+}
+
+static void nct6694_can_tx(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_frame *frame = &priv->tx->frame;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_DELIVER,
+               .sel = NCT6694_CAN_DELIVER_SEL(1),
+               .len = cpu_to_le16(sizeof(*frame))
+       };
+       struct net_device_stats *stats = &ndev->stats;
+       struct sk_buff *skb = priv->tx_skb;
+       struct canfd_frame *cfd;
+       struct can_frame *cf;
+       u32 txid;

drivers/net/can/usb/nct6694_canfd.c line 513

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       int err;
+
+       memset(frame, 0, sizeof(*frame));
+
+       if (priv->can_idx == 0)
+               frame->tag = NCT6694_CAN_FRAME_TAG_CAN0;
+       else
+               frame->tag = NCT6694_CAN_FRAME_TAG_CAN1;
+
+       if (can_is_canfd_skb(skb)) {

Reduce scope of variable when possible: move declaration of cfd here:

                struct canfd_frame *cfd;
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+               cfd = (struct canfd_frame *)priv->tx_skb->data;
+
+               if (cfd->flags & CANFD_BRS)
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_BRS;
+
+               if (cfd->can_id & CAN_EFF_FLAG) {
+                       txid = cfd->can_id & CAN_EFF_MASK;

drivers/net/can/usb/nct6694_canfd.c line 530

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF;
+               } else {
+                       txid = cfd->can_id & CAN_SFF_MASK;
+               }
+               frame->flag |= NCT6694_CAN_FRAME_FLAG_FD;
+               frame->id = cpu_to_le32(txid);
+               frame->length = cfd->len;
+
+               memcpy(frame->data, cfd->data, cfd->len);
+       } else {

Reduce scope of variable when possible: move declaration of cf here:

                struct canfd_frame *cf;
Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+               cf = (struct can_frame *)priv->tx_skb->data;
+
+               if (cf->can_id & CAN_RTR_FLAG)
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR;
+

drivers/net/can/usb/nct6694_canfd.c line 545

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+               if (cf->can_id & CAN_EFF_FLAG) {
+                       txid = cf->can_id & CAN_EFF_MASK;
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF;
+               } else {
+                       txid = cf->can_id & CAN_SFF_MASK;
+               }
+               frame->id = cpu_to_le32(txid);
+               frame->length = cf->len;
+
+               memcpy(frame->data, cf->data, cf->len);

Don't copy cf->data if the can frame is a RTR frame.

Quoted 209 lines
drivers/net/can/usb/nct6694_canfd.c new file +209 -0
+       }
+
+       err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
+       if (err) {
+               netdev_err(ndev, "%s: Tx FIFO full!\n", __func__);
+               can_free_echo_skb(ndev, 0, NULL);
+               stats->tx_dropped++;
+               stats->tx_errors++;
+               netif_wake_queue(ndev);
+       }
+}
+
+static void nct6694_can_tx_work(struct work_struct *work)
+{
+       struct nct6694_can_priv *priv = container_of(work,
+                                                    struct nct6694_can_priv,
+                                                    tx_work);
+       struct net_device *ndev = priv->ndev;
+
+       guard(mutex)(&priv->lock);
+
+       if (priv->tx_skb) {
+               if (priv->can.state == CAN_STATE_BUS_OFF) {
+                       nct6694_can_clean(ndev);
+               } else {
+                       nct6694_can_tx(ndev);
+                       can_put_echo_skb(priv->tx_skb, ndev, 0, 0);
+                       priv->tx_skb = NULL;
+               }
+       }
+}
+
+static netdev_tx_t nct6694_can_start_xmit(struct sk_buff *skb,
+                                         struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+       if (can_dev_dropped_skb(ndev, skb))
+               return NETDEV_TX_OK;
+
+       if (priv->tx_skb) {
+               netdev_err(ndev, "hard_xmit called while tx busy\n");
+               return NETDEV_TX_BUSY;
+       }
+
+       netif_stop_queue(ndev);
+       priv->tx_skb = skb;
+       queue_work(priv->wq, &priv->tx_work);
+
+       return NETDEV_TX_OK;
+}
+
+static int nct6694_can_start(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_setting *setting = &priv->tx->setting;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_SETTING,
+               .sel = NCT6694_CAN_SETTING_SEL(priv->can_idx),
+               .len = cpu_to_le16(sizeof(*setting))
+       };
+       const struct can_bittiming *n_bt = &priv->can.bittiming;
+       const struct can_bittiming *d_bt = &priv->can.data_bittiming;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       memset(setting, 0, sizeof(*setting));
+       setting->nbr = cpu_to_le32(n_bt->bitrate);
+       setting->dbr = cpu_to_le32(d_bt->bitrate);
+
+       if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
+               setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_MON);
+
+       if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
+           priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
+               setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_NISO);
+
+       if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
+               setting->ctrl1 |= cpu_to_le16(NCT6694_CAN_SETTING_CTRL1_LBCK);
+
+       ret = nct6694_write_msg(priv->nct6694, &cmd_hd, setting);
+       if (ret)
+               return ret;
+
+       priv->can.state = CAN_STATE_ERROR_ACTIVE;
+
+       return ret;
+}
+
+static int nct6694_can_stop(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+       netif_stop_queue(ndev);
+       free_irq(ndev->irq, ndev);
+       destroy_workqueue(priv->wq);
+       priv->wq = NULL;
+       nct6694_can_clean(ndev);
+       priv->can.state = CAN_STATE_STOPPED;
+       can_rx_offload_disable(&priv->offload);
+       close_candev(ndev);
+
+       return 0;
+}
+
+static int nct6694_can_set_mode(struct net_device *ndev, enum can_mode mode)
+{
+       switch (mode) {
+       case CAN_MODE_START:
+               nct6694_can_clean(ndev);
+               nct6694_can_start(ndev);
+               netif_wake_queue(ndev);
+               break;
+       default:
+               return -EOPNOTSUPP;
+       }
+
+       return 0;
+}
+
+static int nct6694_can_open(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       int ret;
+
+       ret = open_candev(ndev);
+       if (ret)
+               return ret;
+
+       can_rx_offload_enable(&priv->offload);
+
+       ret = request_threaded_irq(ndev->irq, NULL,
+                                  nct6694_can_irq, IRQF_ONESHOT,
+                                  "nct6694_can", ndev);
+       if (ret) {
+               netdev_err(ndev, "Failed to request IRQ\n");
+               goto close_candev;
+       }
+
+       priv->wq = alloc_ordered_workqueue("%s-nct6694_wq",
+                                          WQ_FREEZABLE | WQ_MEM_RECLAIM,
+                                          ndev->name);
+       if (!priv->wq) {
+               ret = -ENOMEM;
+               goto free_irq;
+       }
+
+       priv->tx_skb = NULL;
+
+       ret = nct6694_can_start(ndev);
+       if (ret)
+               goto destroy_wq;
+
+       netif_start_queue(ndev);
+
+       return 0;
+
+destroy_wq:
+       destroy_workqueue(priv->wq);
+free_irq:
+       free_irq(ndev->irq, ndev);
+close_candev:
+       can_rx_offload_disable(&priv->offload);
+       close_candev(ndev);
+       return ret;
+}
+
+static const struct net_device_ops nct6694_can_netdev_ops = {
+       .ndo_open = nct6694_can_open,
+       .ndo_stop = nct6694_can_stop,
+       .ndo_start_xmit = nct6694_can_start_xmit,
+       .ndo_change_mtu = can_change_mtu,
+};
+
+static const struct ethtool_ops nct6694_can_ethtool_ops = {
+       .get_ts_info = ethtool_op_get_ts_info,
+};
+
+static int nct6694_can_get_clock(struct nct6694_can_priv *priv)
+{
+       struct nct6694_can_information *info = &priv->rx->info;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_INFORMATION,
+               .sel = NCT6694_CAN_INFORMATION_SEL,
+               .len = cpu_to_le16(sizeof(*info))
+       };
+       int ret;
+
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, info);
+       if (ret)
+               return ret;
+
+       return le32_to_cpu(info->can_clk);
+}
+
+static int nct6694_can_probe(struct platform_device *pdev)
+{
+       const struct mfd_cell *cell = mfd_get_cell(pdev);
+       struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+       struct nct6694_can_priv *priv;
+       struct net_device *ndev;
+       int ret, irq, can_clk;
+
+       irq = irq_create_mapping(nct6694->domain,
+                                NCT6694_IRQ_CAN1 + cell->id);
+       if (!irq)

drivers/net/can/usb/nct6694_canfd.c line 764

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+               return irq;
+
+       ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1);
+       if (!ndev)
+               return -ENOMEM;
+
+       ndev->irq = irq;
+       ndev->flags |= IFF_ECHO;
+       ndev->netdev_ops = &nct6694_can_netdev_ops;
+       ndev->ethtool_ops = &nct6694_can_ethtool_ops;

Your device has two CAN interfaces, right? Do not forget to populate netdev->dev_port.

          netdev->dev_port = cell->id;
Quoted 81 lines
drivers/net/can/usb/nct6694_canfd.c new file +81 -0
+       priv = netdev_priv(ndev);
+       priv->nct6694 = nct6694;
+       priv->ndev = ndev;
+
+       priv->tx = devm_kzalloc(&pdev->dev, sizeof(union nct6694_can_tx),
+                               GFP_KERNEL);
+       if (!priv->tx) {
+               ret = -ENOMEM;
+               goto free_candev;
+       }
+
+       priv->rx = devm_kzalloc(&pdev->dev, sizeof(union nct6694_can_rx),
+                               GFP_KERNEL);
+       if (!priv->rx) {
+               ret = -ENOMEM;
+               goto free_candev;
+       }
+
+       can_clk = nct6694_can_get_clock(priv);
+       if (can_clk < 0) {
+               ret = dev_err_probe(&pdev->dev, can_clk,
+                                   "Failed to get clock\n");
+               goto free_candev;
+       }
+
+       devm_mutex_init(&pdev->dev, &priv->lock);
+       INIT_WORK(&priv->tx_work, nct6694_can_tx_work);
+
+       priv->can_idx = cell->id;
+       priv->can.state = CAN_STATE_STOPPED;
+       priv->can.clock.freq = can_clk;
+       priv->can.bittiming_const = &nct6694_can_bittiming_nominal_const;
+       priv->can.data_bittiming_const = &nct6694_can_bittiming_data_const;
+       priv->can.do_set_mode = nct6694_can_set_mode;
+       priv->can.do_get_berr_counter = nct6694_can_get_berr_counter;
+
+       priv->can.ctrlmode = CAN_CTRLMODE_FD;
+
+       priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK            |
+                                      CAN_CTRLMODE_LISTENONLY          |
+                                      CAN_CTRLMODE_FD                  |
+                                      CAN_CTRLMODE_FD_NON_ISO          |
+                                      CAN_CTRLMODE_BERR_REPORTING;
+
+       ret = can_rx_offload_add_manual(ndev, &priv->offload,
+                                       NCT6694_NAPI_WEIGHT);
+       if (ret) {
+               dev_err_probe(&pdev->dev, ret, "Failed to add rx_offload\n");
+               goto free_candev;
+       }
+
+       platform_set_drvdata(pdev, priv);
+       SET_NETDEV_DEV(priv->ndev, &pdev->dev);
+
+       ret = register_candev(priv->ndev);
+       if (ret)
+               goto del_rx_offload;
+
+       return 0;
+
+del_rx_offload:
+       can_rx_offload_del(&priv->offload);
+free_candev:
+       free_candev(ndev);
+       return ret;
+}
+
+static void nct6694_can_remove(struct platform_device *pdev)
+{
+       struct nct6694_can_priv *priv = platform_get_drvdata(pdev);
+
+       cancel_work_sync(&priv->tx_work);
+       unregister_candev(priv->ndev);
+       can_rx_offload_del(&priv->offload);
+       free_candev(priv->ndev);
+}
+
+static struct platform_driver nct6694_can_driver = {
+       .driver = {
+               .name   = DRVNAME,
+       },

drivers/net/can/usb/nct6694_canfd.c line 856

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       .probe          = nct6694_can_probe,
+       .remove         = nct6694_can_remove,
+};
+
+module_platform_driver(nct6694_can_driver);
+
+MODULE_DESCRIPTION("USB-CAN FD driver for NCT6694");
+MODULE_AUTHOR("Ming Yu <[email protected]>");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:nct6694-can");

Yours sincerely,
Vincent Mailhol

author
#10 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Dear Vincent,

Thank you for your reply,
I'll add comments to describe these locks in the next patch,

Vincent Mailhol <[email protected]> 於 2025年1月14日 週二 下午4:06寫道:

Quoted 1 line

...

Quoted 8 lines

drivers/net/can/usb/Kconfig config CAN_MCBA_USB · line 138

Quoted 3 lines
drivers/net/can/usb/Kconfig +3 -0
+config CAN_NCT6694
+       tristate "Nuvoton NCT6694 Socket CANfd support"
+       depends on MFD_NCT6694

Your driver uses the CAN rx offload. You need to select it here.

select CAN_RX_OFFLOAD

Understood! I'll add it in v6.

Quoted 51 lines

drivers/net/can/usb/Kconfig config CAN_MCBA_USB · line 144

Quoted 6 lines
drivers/net/can/usb/Kconfig +6 -0
+       help
+         If you say yes to this option, support will be included for Nuvoton
+         NCT6694, a USB device to socket CANfd controller.
+
+         This driver can also be built as a module. If so, the module will
+         be called nct6694_canfd.

Here, the name is nct6694_canfd...

Quoted 29 lines

config CAN_PEAK_USB
tristate "PEAK PCAN-USB/USB Pro interfaces for CAN 2.0b/CAN-FD" help

drivers/net/can/usb/Makefile +1 -0
index 8b11088e9a59..fcafb1ac262e 100644
@@ -11,5 +11,6 @@ obj-$(CONFIG_CAN_F81604) += f81604.o
 obj-$(CONFIG_CAN_GS_USB) += gs_usb.o
 obj-$(CONFIG_CAN_KVASER_USB) += kvaser_usb/
 obj-$(CONFIG_CAN_MCBA_USB) += mcba_usb.o
+obj-$(CONFIG_CAN_NCT6694) += nct6694_canfd.o
 obj-$(CONFIG_CAN_PEAK_USB) += peak_usb/
 obj-$(CONFIG_CAN_UCAN) += ucan.o
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
new file mode 100644
index 000000000000..7a15c39021ff
@@ -0,0 +1,856 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Nuvoton NCT6694 Socket CANfd driver based on USB interface.
+ *
+ * Copyright (C) 2024 Nuvoton Technology Corp.
+ */
+
+#include <linux/can/dev.h>
+#include <linux/can/rx-offload.h>

drivers/net/can/usb/nct6694_canfd.c line 19

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+#include <linux/ethtool.h>
+#include <linux/irqdomain.h>
+#include <linux/kernel.h>
+#include <linux/mfd/core.h>
+#include <linux/mfd/nct6694.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/platform_device.h>
+
+#define DRVNAME "nct6694-can"

... but here, it is nct6694-can.

Quoted 2 lines

Use a consistent name between the module name and the driver name.

Okay, Fix it in v6.

Quoted 6 lines
Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+/*
+ * 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_CAN_MOD                        0x05
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 30

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+
+/* Command 00h - CAN Setting and Initialization */
+#define NCT6694_CAN_SETTING            0x00
+#define NCT6694_CAN_SETTING_SEL(idx)   (idx ? 0x01 : 0x00)

What are the possible values for idx? Isn't it only 0 or 1? If so, no need for this NCT6694_CAN_SETTING_SEL() macro. Directly assign the channel index to the selector field.

Okay, Fix it in v6.

Quoted 15 lines

drivers/net/can/usb/nct6694_canfd.c line 42

Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+#define NCT6694_CAN_SETTING_CTRL1_MON  BIT(0)
+#define NCT6694_CAN_SETTING_CTRL1_NISO BIT(1)
+#define NCT6694_CAN_SETTING_CTRL1_LBCK BIT(2)
+
+/* Command 01h - CAN Information */
+#define NCT6694_CAN_INFORMATION                0x01
+#define NCT6694_CAN_INFORMATION_SEL    0x00
+
+/* Command 02h - CAN Event */
+#define NCT6694_CAN_EVENT              0x02
+#define NCT6694_CAN_EVENT_SEL(idx, mask)       \
+       ((idx ? 0x80 : 0x00) | ((mask) & 0xFF))

Can idx and mask really overlap? Shouldn't this be:

Quoted 3 lines
  #define NCT6694_CAN_EVENT_SEL(idx, mask)  \
          ((idx ? 0x80 : 0x00) | ((mask) & 0x7F))

Sorry, you're right, I'll fix it in v6.

Quoted 35 lines

drivers/net/can/usb/nct6694_canfd.c line 53

Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+#define NCT6694_CAN_EVENT_ERR          BIT(0)
+#define NCT6694_CAN_EVENT_STATUS       BIT(1)
+#define NCT6694_CAN_EVENT_TX_EVT       BIT(2)
+#define NCT6694_CAN_EVENT_RX_EVT       BIT(3)
+#define NCT6694_CAN_EVENT_REC          BIT(4)
+#define NCT6694_CAN_EVENT_TEC          BIT(5)
+#define NCT6694_CAN_EVENT_MASK         GENMASK(3, 0)
+#define NCT6694_CAN_EVT_TX_FIFO_EMPTY  BIT(7)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_DATA_LOST   BIT(5)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_HALF_FULL   BIT(6)  /* Read-clear */
+#define NCT6694_CAN_EVT_RX_DATA_IN     BIT(7)  /* Read-clear*/

Some of those macro are not used:

drivers/net/can/usb/nct6694_canfd.c:52: warning: macro
"NCT6694_CAN_EVT_RX_HALF_FULL" is not used [-Wunused-macros]

     52 | #define NCT6694_CAN_EVT_RX_HALF_FULL BIT(6) /* Read-clear */
        |
  drivers/net/can/usb/nct6694_canfd.c:43: warning: macro
"NCT6694_CAN_EVENT_ERR" is not used [-Wunused-macros]
     43 | #define NCT6694_CAN_EVENT_ERR  BIT(0)
        |
  drivers/net/can/usb/nct6694_canfd.c:44: warning: macro
"NCT6694_CAN_EVENT_STATUS" is not used [-Wunused-macros]
     44 | #define NCT6694_CAN_EVENT_STATUS BIT(1)
        |
  drivers/net/can/usb/nct6694_canfd.c:46: warning: macro
"NCT6694_CAN_EVENT_RX_EVT" is not used [-Wunused-macros]
     46 | #define NCT6694_CAN_EVENT_RX_EVT BIT(3)
        |
  drivers/net/can/usb/nct6694_canfd.c:45: warning: macro
"NCT6694_CAN_EVENT_TX_EVT" is not used [-Wunused-macros]
     45 | #define NCT6694_CAN_EVENT_TX_EVT BIT(2)
        |
Quoted 2 lines

Is this OK?

Yes, these macros are replaced by NCT6694_CAN_EVENT_MASK, I'll drop them in the next patch.

Quoted 12 lines

drivers/net/can/usb/nct6694_canfd.c line 63

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
+/* Command 10h - CAN Deliver */
+#define NCT6694_CAN_DELIVER            0x10
+#define NCT6694_CAN_DELIVER_SEL(buf_cnt)       \
+       ((buf_cnt) & 0xFF)
+
+/* Command 11h - CAN Receive */
+#define NCT6694_CAN_RECEIVE            0x11
+#define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)  \
+       ((idx ? 0x80 : 0x00) | ((buf_cnt) & 0xFF))

Can idx and buf_cnt really overlap? Shouldn't this be:

Quoted 3 lines
  #define NCT6694_CAN_RECEIVE_SEL(idx, buf_cnt)  \
          ((idx ? 0x80 : 0x00) | ((buf_cnt) & 0x7F))

Fix it in v6.

Quoted 66 lines
Quoted 53 lines
drivers/net/can/usb/nct6694_canfd.c new file +53 -0
+#define NCT6694_CAN_FRAME_TAG_CAN0     0xC0
+#define NCT6694_CAN_FRAME_TAG_CAN1     0xC1
+#define NCT6694_CAN_FRAME_FLAG_EFF     BIT(0)
+#define NCT6694_CAN_FRAME_FLAG_RTR     BIT(1)
+#define NCT6694_CAN_FRAME_FLAG_FD      BIT(2)
+#define NCT6694_CAN_FRAME_FLAG_BRS     BIT(3)
+#define NCT6694_CAN_FRAME_FLAG_ERR     BIT(4)
+
+#define NCT6694_NAPI_WEIGHT            32
+
+enum nct6694_event_err {
+       NCT6694_CAN_EVT_ERR_NO_ERROR = 0,
+       NCT6694_CAN_EVT_ERR_CRC_ERROR,
+       NCT6694_CAN_EVT_ERR_STUFF_ERROR,
+       NCT6694_CAN_EVT_ERR_ACK_ERROR,
+       NCT6694_CAN_EVT_ERR_FORM_ERROR,
+       NCT6694_CAN_EVT_ERR_BIT_ERROR,
+       NCT6694_CAN_EVT_ERR_TIMEOUT_ERROR,
+       NCT6694_CAN_EVT_ERR_UNKNOWN_ERROR,
+};
+
+enum nct6694_event_status {
+       NCT6694_CAN_EVT_STS_ERROR_ACTIVE = 0,
+       NCT6694_CAN_EVT_STS_ERROR_PASSIVE,
+       NCT6694_CAN_EVT_STS_BUS_OFF,
+       NCT6694_CAN_EVT_STS_WARNING,
+};
+
+struct __packed nct6694_can_setting {
+       __le32 nbr;
+       __le32 dbr;
+       u8 active;
+       u8 reserved[3];
+       __le16 ctrl1;
+       __le16 ctrl2;
+       __le32 nbtp;
+       __le32 dbtp;
+};
+
+struct __packed nct6694_can_information {
+       u8 tx_fifo_cnt;
+       u8 rx_fifo_cnt;
+       u8 reserved[2];
+       __le32 can_clk;
+};
+
+struct __packed nct6694_can_event {
+       u8 err;
+       u8 status;
+       u8 tx_evt;
+       u8 rx_evt;
+       u8 rec;
+       u8 tec;

drivers/net/can/usb/nct6694_canfd.c line 127

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       u8 reserved[2];
+};
+
+struct __packed nct6694_can_frame {
+       u8 tag;
+       u8 flag;
+       u8 reserved;
+       u8 length;
+       __le32 id;
+       u8 data[64];

Nitpick, use CANFD_MAX_DLEN here:

Quoted 2 lines
          u8 data[CANFD_MAX_DLEN];

Fix it in v6.

Quoted 2 lines
Quoted 2 lines
drivers/net/can/usb/nct6694_canfd.c new file +2 -0
+};
+

...

Quoted 23 lines
Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+static void nct6694_can_rx(struct net_device *ndev, u8 rx_evt)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_frame *frame = &priv->rx->frame;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_RECEIVE,
+               .sel = NCT6694_CAN_RECEIVE_SEL(priv->can_idx, 1),
+               .len = cpu_to_le16(sizeof(*frame))
+       };

drivers/net/can/usb/nct6694_canfd.c line 236

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       struct canfd_frame *cfd;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+       int ret;
+
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, frame);
+       if (ret)
+               return;
+
+       if (frame->flag & NCT6694_CAN_FRAME_FLAG_FD) {

Reduce scope of variable when possible: move declaration of cfd here:

Quoted 2 lines
                struct canfd_frame *cfd;

Okay! Fix it in v6.

Quoted 7 lines

drivers/net/can/usb/nct6694_canfd.c line 242

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               skb = alloc_canfd_skb(priv->ndev, &cfd);
+               if (!skb)
+                       return;
+
+               cfd->can_id = le32_to_cpu(frame->id);
+               cfd->len = frame->length;
Quoted 6 lines

No. I asked you to sanitize the length in this message:

https://lore.kernel.org/linux-can/[email protected]/

Never use the length as is.

Sorry! I misunderstood your meaning.
I'll Fix it to cfd->len = canfd_sanitize_len(frame->length).

Quoted 10 lines

drivers/net/can/usb/nct6694_canfd.c line 251

Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF)
+                       cfd->can_id |= CAN_EFF_FLAG;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_BRS)
+                       cfd->flags |= CANFD_BRS;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_ERR)
+                       cfd->flags |= CANFD_ESI;
+
+               memcpy(cfd->data, frame->data, cfd->len);
+       } else {
Quoted 4 lines

Reduce scope of variable when possible: move declaration of cf here:

                struct canfd_frame *cf;

Fix it in v6.

Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 257

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               skb = alloc_can_skb(priv->ndev, &cf);
+               if (!skb)
+                       return;
+
+               cf->can_id = le32_to_cpu(frame->id);
+               cf->len = frame->length;

Ditto, sanitize the length.

Fix it in v6.

Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 263

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_EFF)
+                       cf->can_id |= CAN_EFF_FLAG;
+               if (frame->flag & NCT6694_CAN_FRAME_FLAG_RTR)
+                       cf->can_id |= CAN_RTR_FLAG;
+
+               memcpy(cf->data, frame->data, cf->len);

Only copy can data if the frame is not an RTR frame.

Quoted 9 lines
                  if (frame->flag & NCT6694_CAN_FRAME_FLAG_RTR)
                          cf->can_id |= CAN_RTR_FLAG;
                  else
                          memcpy(cf->data, frame->data, cf->len);

I already asked you to do this in below comment:

https://lore.kernel.org/linux-can/[email protected]/

Sorry for forgetting the part, I'll fix it in the next patch.

Quoted 33 lines
Quoted 33 lines
drivers/net/can/usb/nct6694_canfd.c new file +33 -0
+       }
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+
+static void nct6694_can_clean(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+       if (priv->tx_skb || netif_queue_stopped(ndev))
+               ndev->stats.tx_errors++;
+       dev_kfree_skb(priv->tx_skb);
+       priv->tx_skb = NULL;
+}
+
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

Quoted 59 lines
Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+       bec->rxerr = evt[priv->can_idx].rec;
+       bec->txerr = evt[priv->can_idx].tec;
+
+       return 0;
+}
+
+static void nct6694_can_handle_state_change(struct net_device *ndev,
+                                           u8 status)

drivers/net/can/usb/nct6694_canfd.c line 318

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       enum can_state new_state = priv->can.state;
+       enum can_state rx_state, tx_state;
+       struct can_berr_counter bec;
+       struct can_frame *cf;
+       struct sk_buff *skb;
+
+       nct6694_can_get_berr_counter(ndev, &bec);
+       can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state);

Here, you set up tx_state and rx_state...

Quoted 16 lines
drivers/net/can/usb/nct6694_canfd.c new file +16 -0
+       switch (status) {
+       case NCT6694_CAN_EVT_STS_ERROR_ACTIVE:
+               new_state = CAN_STATE_ERROR_ACTIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_ERROR_PASSIVE:
+               new_state = CAN_STATE_ERROR_PASSIVE;
+               break;
+       case NCT6694_CAN_EVT_STS_BUS_OFF:
+               new_state = CAN_STATE_BUS_OFF;
+               break;
+       case NCT6694_CAN_EVT_STS_WARNING:
+               new_state = CAN_STATE_ERROR_WARNING;
+               break;
+       default:
+               netdev_err(ndev, "Receive unknown CAN status event.\n");
+               return;

drivers/net/can/usb/nct6694_canfd.c line 345

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       }
+
+       /* state hasn't changed */
+       if (new_state == priv->can.state)
+               return;
+
+       skb = alloc_can_err_skb(ndev, &cf);
+
+       tx_state = bec.txerr >= bec.rxerr ? new_state : 0;
+       rx_state = bec.txerr <= bec.rxerr ? new_state : 0;

... but you never used the values returned by can_state_get_by_berr_counter() and just overwrote the tx and rx state.

What is the logic here? Why do you need to manually adjust those two values? Isn't the logic in can_change_state() sufficient?

drivers/net/can/usb/nct6694_canfd.c line 348

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       can_change_state(ndev, cf, tx_state, rx_state);
+
+       if (new_state == CAN_STATE_BUS_OFF) {
Quoted 9 lines

Same for the new_state. The function can_change_state() calculate the new state from tx_state and rx_state and save it under can_priv->state. But here, you do your own calculation.

Only keep one of the two. If your device already tells you the state, then fine! Just use the information from your device and do not use can_change_state(). Here, you are doing double work resulting in a weird mix.

Okay! I will revert nct6694_can_handle_state_change() back to the v3 version.

Quoted 10 lines
Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+               can_bus_off(ndev);
+       } else if (skb) {
+               cf->can_id |= CAN_ERR_CNT;
+               cf->data[6] = bec.txerr;
+               cf->data[7] = bec.rxerr;
+       }
+
+       nct6694_can_rx_offload(&priv->offload, skb);
+}
+

...

Quoted 11 lines

drivers/net/can/usb/nct6694_canfd.c line 437

Quoted 8 lines
drivers/net/can/usb/nct6694_canfd.c new file +8 -0
+static irqreturn_t nct6694_can_irq(int irq, void *data)
+{
+       struct net_device *ndev = data;
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 tx_evt, rx_evt, bus_err, can_status;
+       u8 mask_sts = NCT6694_CAN_EVENT_MASK;

No need for the mask_sts variable. Directly use NCT6694_CAN_EVENT_MASK.

Okay! Fix it in v6.

Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 442

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+       irqreturn_t handled = IRQ_NONE;
+       int can_idx = priv->can_idx;
+       int ret;
+
+       scoped_guard(mutex, &priv->lock) {

Reduce scope of variable when possible: move the declarations of cmd_hd and ret here.

Okay! Fix it in v6.

Quoted 7 lines
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+               cmd_hd = (struct nct6694_cmd_header) {
+                       .mod = NCT6694_CAN_MOD,
+                       .cmd = NCT6694_CAN_EVENT,
+                       .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask_sts),
+                       .len = cpu_to_le16(sizeof(priv->rx->event))
+               };
+

...

Quoted 28 lines
Quoted 15 lines
drivers/net/can/usb/nct6694_canfd.c new file +15 -0
+static void nct6694_can_tx(struct net_device *ndev)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_frame *frame = &priv->tx->frame;
+       struct nct6694_cmd_header cmd_hd = {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_DELIVER,
+               .sel = NCT6694_CAN_DELIVER_SEL(1),
+               .len = cpu_to_le16(sizeof(*frame))
+       };
+       struct net_device_stats *stats = &ndev->stats;
+       struct sk_buff *skb = priv->tx_skb;
+       struct canfd_frame *cfd;
+       struct can_frame *cf;
+       u32 txid;

drivers/net/can/usb/nct6694_canfd.c line 513

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+       int err;
+
+       memset(frame, 0, sizeof(*frame));
+
+       if (priv->can_idx == 0)
+               frame->tag = NCT6694_CAN_FRAME_TAG_CAN0;
+       else
+               frame->tag = NCT6694_CAN_FRAME_TAG_CAN1;
+
+       if (can_is_canfd_skb(skb)) {

Reduce scope of variable when possible: move declaration of cfd here:

Quoted 2 lines
                struct canfd_frame *cfd;

Okay! Fix it in v6.

Quoted 20 lines
Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+               cfd = (struct canfd_frame *)priv->tx_skb->data;
+
+               if (cfd->flags & CANFD_BRS)
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_BRS;
+
+               if (cfd->can_id & CAN_EFF_FLAG) {
+                       txid = cfd->can_id & CAN_EFF_MASK;

drivers/net/can/usb/nct6694_canfd.c line 530

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF;
+               } else {
+                       txid = cfd->can_id & CAN_SFF_MASK;
+               }
+               frame->flag |= NCT6694_CAN_FRAME_FLAG_FD;
+               frame->id = cpu_to_le32(txid);
+               frame->length = cfd->len;
+
+               memcpy(frame->data, cfd->data, cfd->len);
+       } else {

Reduce scope of variable when possible: move declaration of cf here:

Quoted 2 lines
                struct canfd_frame *cf;

Okay! Fix it in v6.

Quoted 9 lines
Quoted 9 lines
drivers/net/can/usb/nct6694_canfd.c new file +9 -0
+               cf = (struct can_frame *)priv->tx_skb->data;
+
+               if (cf->can_id & CAN_RTR_FLAG)
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_RTR;
+
+               if (cf->can_id & CAN_EFF_FLAG) {
+                       txid = cf->can_id & CAN_EFF_MASK;
+                       frame->flag |= NCT6694_CAN_FRAME_FLAG_EFF;
+               } else {
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 545

Quoted 6 lines
drivers/net/can/usb/nct6694_canfd.c new file +6 -0
+                       txid = cf->can_id & CAN_SFF_MASK;
+               }
+               frame->id = cpu_to_le32(txid);
+               frame->length = cf->len;
+
+               memcpy(frame->data, cf->data, cf->len);

Don't copy cf->data if the can frame is a RTR frame.

Okay! Fix it in v6.

Quoted 12 lines
Quoted 12 lines
drivers/net/can/usb/nct6694_canfd.c new file +12 -0
+       }
+
+       err = nct6694_write_msg(priv->nct6694, &cmd_hd, frame);
+       if (err) {
+               netdev_err(ndev, "%s: Tx FIFO full!\n", __func__);
+               can_free_echo_skb(ndev, 0, NULL);
+               stats->tx_dropped++;
+               stats->tx_errors++;
+               netif_wake_queue(ndev);
+       }
+}
+

...

Quoted 25 lines
Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+static int nct6694_can_probe(struct platform_device *pdev)
+{
+       const struct mfd_cell *cell = mfd_get_cell(pdev);
+       struct nct6694 *nct6694 = dev_get_drvdata(pdev->dev.parent);
+       struct nct6694_can_priv *priv;
+       struct net_device *ndev;
+       int ret, irq, can_clk;
+
+       irq = irq_create_mapping(nct6694->domain,
+                                NCT6694_IRQ_CAN1 + cell->id);
+       if (!irq)

drivers/net/can/usb/nct6694_canfd.c line 764

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+               return irq;
+
+       ndev = alloc_candev(sizeof(struct nct6694_can_priv), 1);
+       if (!ndev)
+               return -ENOMEM;
+
+       ndev->irq = irq;
+       ndev->flags |= IFF_ECHO;
+       ndev->netdev_ops = &nct6694_can_netdev_ops;
+       ndev->ethtool_ops = &nct6694_can_ethtool_ops;

Your device has two CAN interfaces, right? Do not forget to populate netdev->dev_port.

Quoted 2 lines
          netdev->dev_port = cell->id;

Okay! I'll add it in v6.

Quoted 4 lines

drivers/net/can/usb/nct6694_canfd.c line 769

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+       priv = netdev_priv(ndev);
+       priv->nct6694 = nct6694;
+       priv->ndev = ndev;
+

Best regards,
Ming

#11 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Quoted 6 lines · Ming Yu

Dear Vincent,

Thank you for your reply,
I'll add comments to describe these locks in the next patch,

Vincent Mailhol <[email protected]> 於 2025年1月14日 週二 下午4:06寫道:

(...)

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
drivers/net/can/usb/nct6694_canfd.c new file +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Quoted 5 lines
Quoted 5 lines

drivers/net/can/usb/nct6694_canfd.c line 305

Quoted 5 lines
drivers/net/can/usb/nct6694_canfd.c new file +5 -0
+       bec->rxerr = evt[priv->can_idx].rec;
+       bec->txerr = evt[priv->can_idx].tec;
+
+       return 0;
+}

Yours sincerely,
Vincent Mailhol

author
#12 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Vincent Mailhol <[email protected]> 於 2025年1月14日 週二 下午11:12寫道:

Quoted 1 line

...

Quoted 39 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
drivers/net/can/usb/nct6694_canfd.c new file +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

Quoted 9 lines
        struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Since nct6694_read_msg()/nct6694_write_msg() process URBs via usb_bulk_msg(), the transferred data must not be located on the stack. For more details about allocating buffers for transmitting data, please refer to the link:
https://lore.kernel.org/linux-can/[email protected]/

Thanks,
Ming

#13 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Quoted 52 lines · Ming Yu

Vincent Mailhol <[email protected]> 於 2025年1月14日 週二 下午11:12寫道:

Quoted 1 line

...

Quoted 39 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
drivers/net/can/usb/nct6694_canfd.c new file +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

Quoted 9 lines
        struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Quoted 5 lines

Since nct6694_read_msg()/nct6694_write_msg() process URBs via usb_bulk_msg(), the transferred data must not be located on the stack. For more details about allocating buffers for transmitting data, please refer to the link:
https://lore.kernel.org/linux-can/[email protected]/

Ack, I forgot that you can not use stack memory in usb_bulk_msg().

Then, instead, you can either:

- do a dynamic memory allocation directly in the function (good for when you are outside of the hot path, for example struct nct6694_can_setting)

- and for the other structures which are part of the hot path (typically struct nct6694_can_frame) continue to use a dynamically allocated buffer stored in your priv but change the type of nct6694_can_tx and nct6694_can_rx from union to structures.

And no more overlaps, thus no more need for the mutex.

Yours sincerely,
Vincent Mailhol

author
#14 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Vincent Mailhol <[email protected]> 於 2025年1月15日 週三 上午11:36寫道:

Quoted 63 lines
Quoted 49 lines
Quoted 39 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
drivers/net/can/usb/nct6694_canfd.c new file +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

drivers/net/can/usb/nct6694_canfd.c line 299

Quoted 3 lines
drivers/net/can/usb/nct6694_canfd.c new file +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

Quoted 9 lines
        struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Quoted 5 lines

Since nct6694_read_msg()/nct6694_write_msg() process URBs via usb_bulk_msg(), the transferred data must not be located on the stack. For more details about allocating buffers for transmitting data, please refer to the link:
https://lore.kernel.org/linux-can/[email protected]/

Ack, I forgot that you can not use stack memory in usb_bulk_msg().

Then, instead, you can either:

- do a dynamic memory allocation directly in the function (good for when you are outside of the hot path, for example struct nct6694_can_setting)

Quoted 7 lines

- and for the other structures which are part of the hot path (typically struct nct6694_can_frame) continue to use a dynamically allocated buffer stored in your priv but change the type of nct6694_can_tx and nct6694_can_rx from union to structures.

And no more overlaps, thus no more need for the mutex.

Understood, I will remove the unions and add members to private structure in the next patch.
e.g.

struct nct6694_can_priv {
    struct can_priv can;
    ...
    struct nct6694_can_frame tx;
    struct nct6694_can_frame rx;
};

And do dynamic memory allocation for struct nct6694_can_setting and struct nct6694_can_information.

In addition, I would like to know your thoughts on how struct nct6694_can_event[2] should be handled?
It is utilized in both nct6694_can_get_berr_counter() and nct6694_can_irq(), with the latter being called more frequently during runtime.

Thanks,
Ming

#15 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Quoted 81 lines · Ming Yu

Vincent Mailhol <[email protected]> 於 2025年1月15日 週三 上午11:36寫道:

Quoted 63 lines
Quoted 49 lines
Quoted 39 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
(quoted diff) +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

Quoted 9 lines
        struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Quoted 5 lines

Since nct6694_read_msg()/nct6694_write_msg() process URBs via usb_bulk_msg(), the transferred data must not be located on the stack. For more details about allocating buffers for transmitting data, please refer to the link:
https://lore.kernel.org/linux-can/[email protected]/

Ack, I forgot that you can not use stack memory in usb_bulk_msg().

Then, instead, you can either:

- do a dynamic memory allocation directly in the function (good for when you are outside of the hot path, for example struct nct6694_can_setting)

Quoted 7 lines

- and for the other structures which are part of the hot path (typically struct nct6694_can_frame) continue to use a dynamically allocated buffer stored in your priv but change the type of nct6694_can_tx and nct6694_can_rx from union to structures.

And no more overlaps, thus no more need for the mutex.

Understood, I will remove the unions and add members to private structure in the next patch.
e.g.

struct nct6694_can_priv {
    struct can_priv can;
    ...
    struct nct6694_can_frame tx;
    struct nct6694_can_frame rx;
};
Quoted 8 lines

And do dynamic memory allocation for struct nct6694_can_setting and struct nct6694_can_information.

In addition, I would like to know your thoughts on how struct nct6694_can_event[2] should be handled?
It is utilized in both nct6694_can_get_berr_counter() and nct6694_can_irq(), with the latter being called more frequently during runtime.

For the nct6694_can_event in nct6694_can_irq(), I would say it is part of the hot path and thus you can have it in your struct nct6694_can_priv.

For the nct6694_can_get_berr_counter(), the easiest is actually to just add the error counter structure to your nct6694_can_priv:

          struct can_berr_counter berr_cnt;

Each time you receive an event, you update this local error counter copy, and this way, in your nct6694_can_get_berr_counter(), no more need to query your device, you just return the berr_cnt which is saved locally.

Yours sincerely,
Vincent Mailhol

author
#16 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Vincent Mailhol <[email protected]> 於 2025年1月15日 週三 下午2:44寫道:

Quoted 99 lines · Ming Yu
Quoted 81 lines

Vincent Mailhol <[email protected]> 於 2025年1月15日 週三 上午11:36寫道:

Quoted 63 lines
Quoted 49 lines
Quoted 39 lines
Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
(quoted diff) +18 -0
+static int nct6694_can_get_berr_counter(const struct net_device *ndev,
+                                       struct can_berr_counter *bec)
+{
+       struct nct6694_can_priv *priv = netdev_priv(ndev);
+       struct nct6694_can_event *evt = priv->rx->event;
+       struct nct6694_cmd_header cmd_hd;
+       u8 mask = NCT6694_CAN_EVENT_REC | NCT6694_CAN_EVENT_TEC;
+       int ret;
+
+       guard(mutex)(&priv->lock);
+
+       cmd_hd = (struct nct6694_cmd_header) {
+               .mod = NCT6694_CAN_MOD,
+               .cmd = NCT6694_CAN_EVENT,
+               .sel = NCT6694_CAN_EVENT_SEL(priv->can_idx, mask),
+               .len = cpu_to_le16(sizeof(priv->rx->event))
+       };
+
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+       ret = nct6694_read_msg(priv->nct6694, &cmd_hd, evt);
+       if (ret < 0)
+               return ret;

You are holding the priv->lock mutex before calling nct6694_read_msg(). But nct6694_read_msg() then holds the nct6694->access_lock mutex. Why do you need a double mutex here? What kind of race scenario are you trying to prevent here?

Quoted 4 lines

I think priv->lock need to be placed here to prevent priv->rx from being assigned by other functions, and nct6694->access_lock ensures that the nct6694_read_msg() transaction is completed. But in this case, cmd_hd does not need to be in priv->lock's scope.

So, the only reason for holding priv->lock is because priv->rx is shared between functions.

struct nct6694_can_event is only 8 bytes. And you only need it for the life time of the function so it can simply be declared on the stack:

Quoted 9 lines
        struct nct6694_can_event evt;

and with this, no more need to hold the lock. And the same thing also applies to the other functions.

Here, by trying to optimize the memory for only a few bytes, you are getting a huge penalty on the performance by putting locks on all the functions. This is not a good tradeoff.

Quoted 5 lines

Since nct6694_read_msg()/nct6694_write_msg() process URBs via usb_bulk_msg(), the transferred data must not be located on the stack. For more details about allocating buffers for transmitting data, please refer to the link:
https://lore.kernel.org/linux-can/[email protected]/

Ack, I forgot that you can not use stack memory in usb_bulk_msg().

Then, instead, you can either:

- do a dynamic memory allocation directly in the function (good for when you are outside of the hot path, for example struct nct6694_can_setting)

Quoted 7 lines

- and for the other structures which are part of the hot path (typically struct nct6694_can_frame) continue to use a dynamically allocated buffer stored in your priv but change the type of nct6694_can_tx and nct6694_can_rx from union to structures.

And no more overlaps, thus no more need for the mutex.

Understood, I will remove the unions and add members to private structure in the next patch.
e.g.

struct nct6694_can_priv {
    struct can_priv can;
    ...
    struct nct6694_can_frame tx;
    struct nct6694_can_frame rx;
};
Quoted 8 lines

And do dynamic memory allocation for struct nct6694_can_setting and struct nct6694_can_information.

In addition, I would like to know your thoughts on how struct nct6694_can_event[2] should be handled?
It is utilized in both nct6694_can_get_berr_counter() and nct6694_can_irq(), with the latter being called more frequently during runtime.

For the nct6694_can_event in nct6694_can_irq(), I would say it is part of the hot path and thus you can have it in your struct nct6694_can_priv.

For the nct6694_can_get_berr_counter(), the easiest is actually to just add the error counter structure to your nct6694_can_priv:

Quoted 7 lines
          struct can_berr_counter berr_cnt;

Each time you receive an event, you update this local error counter copy, and this way, in your nct6694_can_get_berr_counter(), no more need to query your device, you just return the berr_cnt which is saved locally.

Understood! I will make these modifications in the next patch.

Best regards,
Ming

#17 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

In reply to Ming Yu

On 14/01/2025 at 12:30, Ming Yu wrote:

(...)

drivers/net/can/usb/nct6694_canfd.c line 275

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+static void nct6694_can_clean(struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+	if (priv->tx_skb || netif_queue_stopped(ndev))
+		ndev->stats.tx_errors++;
+	dev_kfree_skb(priv->tx_skb);

Use:

  	can_flush_echo_skb(ndev);

(related to the following comments).

Quoted 2 lines
(quoted diff) +2 -0
+	priv->tx_skb = NULL;
+}

(...)

drivers/net/can/usb/nct6694_canfd.c line 568

Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+static void nct6694_can_tx_work(struct work_struct *work)
+{
+	struct nct6694_can_priv *priv = container_of(work,
+						     struct nct6694_can_priv,
+						     tx_work);
+	struct net_device *ndev = priv->ndev;
+
+	guard(mutex)(&priv->lock);
+
+	if (priv->tx_skb) {
+		if (priv->can.state == CAN_STATE_BUS_OFF) {

Just stop the queue when the can bus is off so that you do not have do check the bus status each time a frame is sent.

Quoted 14 lines
drivers/net/can/usb/nct6694_canfd.c new file +14 -0
+			nct6694_can_clean(ndev);
+		} else {
+			nct6694_can_tx(ndev);
+			can_put_echo_skb(priv->tx_skb, ndev, 0, 0);
+			priv->tx_skb = NULL;
+		}
+	}
+}
+
+static netdev_tx_t nct6694_can_start_xmit(struct sk_buff *skb,
+					  struct net_device *ndev)
+{
+	struct nct6694_can_priv *priv = netdev_priv(ndev);
+

drivers/net/can/usb/nct6694_canfd.c line 592

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+	if (can_dev_dropped_skb(ndev, skb))
+		return NETDEV_TX_OK;
+
+	if (priv->tx_skb) {
+		netdev_err(ndev, "hard_xmit called while tx busy\n");
+		return NETDEV_TX_BUSY;
+	}
+
+	netif_stop_queue(ndev);
+	priv->tx_skb = skb;

Here, you can directly do:

  	can_put_echo_skb(skb, ndev, 0, 0);

The skb remains accessible under priv->can.echo_skb[0]. With this, you can remove the priv->tx_skb field.

drivers/net/can/usb/nct6694_canfd.c line 596

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+	queue_work(priv->wq, &priv->tx_work);
+
+	return NETDEV_TX_OK;
+}

Yours sincerely,
Vincent Mailhol

author
#18 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Hi Vincent,

I will remove priv->tx_skb in the next patch, but it seems that can_flush_echo_skb() has not been EXPORT_SYMBOL_GPL().

I would like to know if nct6694_can_clean() requires modification.

Vincent Mailhol <[email protected]> 於 2025年1月16日 週四 上午12:45寫道:

Quoted 63 lines

drivers/net/can/usb/nct6694_canfd.c line 275

Quoted 7 lines
drivers/net/can/usb/nct6694_canfd.c new file +7 -0
+static void nct6694_can_clean(struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+
+     if (priv->tx_skb || netif_queue_stopped(ndev))
+             ndev->stats.tx_errors++;
+     dev_kfree_skb(priv->tx_skb);

Use:

        can_flush_echo_skb(ndev);

(related to the following comments).

Quoted 2 lines
(quoted diff) +2 -0
+     priv->tx_skb = NULL;
+}

(...)

drivers/net/can/usb/nct6694_canfd.c line 568

Quoted 11 lines
drivers/net/can/usb/nct6694_canfd.c new file +11 -0
+static void nct6694_can_tx_work(struct work_struct *work)
+{
+     struct nct6694_can_priv *priv = container_of(work,
+                                                  struct nct6694_can_priv,
+                                                  tx_work);
+     struct net_device *ndev = priv->ndev;
+
+     guard(mutex)(&priv->lock);
+
+     if (priv->tx_skb) {
+             if (priv->can.state == CAN_STATE_BUS_OFF) {

Just stop the queue when the can bus is off so that you do not have do check the bus status each time a frame is sent.

Quoted 14 lines
drivers/net/can/usb/nct6694_canfd.c new file +14 -0
+                     nct6694_can_clean(ndev);
+             } else {
+                     nct6694_can_tx(ndev);
+                     can_put_echo_skb(priv->tx_skb, ndev, 0, 0);
+                     priv->tx_skb = NULL;
+             }
+     }
+}
+
+static netdev_tx_t nct6694_can_start_xmit(struct sk_buff *skb,
+                                       struct net_device *ndev)
+{
+     struct nct6694_can_priv *priv = netdev_priv(ndev);
+

drivers/net/can/usb/nct6694_canfd.c line 592

Quoted 10 lines
drivers/net/can/usb/nct6694_canfd.c new file +10 -0
+     if (can_dev_dropped_skb(ndev, skb))
+             return NETDEV_TX_OK;
+
+     if (priv->tx_skb) {
+             netdev_err(ndev, "hard_xmit called while tx busy\n");
+             return NETDEV_TX_BUSY;
+     }
+
+     netif_stop_queue(ndev);
+     priv->tx_skb = skb;

Here, you can directly do:

        can_put_echo_skb(skb, ndev, 0, 0);
Quoted 8 lines

The skb remains accessible under priv->can.echo_skb[0]. With this, you can remove the priv->tx_skb field.

drivers/net/can/usb/nct6694_canfd.c line 596

Quoted 4 lines
drivers/net/can/usb/nct6694_canfd.c new file +4 -0
+     queue_work(priv->wq, &priv->tx_work);
+
+     return NETDEV_TX_OK;
+}

Thanks,
Ming

#19 lore raw

Re: [PATCH v5 4/7] can: Add Nuvoton NCT6694 CAN support

Quoted 6 lines · Ming Yu

Hi Vincent,

I will remove priv->tx_skb in the next patch, but it seems that can_flush_echo_skb() has not been EXPORT_SYMBOL_GPL().

I would like to know if nct6694_can_clean() requires modification.

Indeed, you can can_free_echo_skb(ndev, 0, NULL) instead of can_flush_echo_skb(ndev). That one is exported.

Yours sincerely,
Vincent Mailhol

author
#20 lore raw

PATCH v5 5/7 watchdog: Add Nuvoton NCT6694 WDT support

In reply to Ming Yu

This driver supports Watchdog timer functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                    |   1 +
 drivers/watchdog/Kconfig       |  11 ++
 drivers/watchdog/Makefile      |   1 +
 drivers/watchdog/nct6694_wdt.c | 295 +++++++++++++++++++++++++++++++++
 4 files changed, 308 insertions(+)
 create mode 100644 drivers/watchdog/nct6694_wdt.c
MAINTAINERS +1 -0
index 6e9b78202d6f..a772747d493f 100644
@@ -16725,6 +16725,7 @@ F:	drivers/gpio/gpio-nct6694.c
 F:	drivers/i2c/busses/i2c-nct6694.c
 F:	drivers/mfd/nct6694.c
 F:	drivers/net/can/usb/nct6694_canfd.c
+F:	drivers/watchdog/nct6694_wdt.c
 F:	include/linux/mfd/nct6694.h
 
 NVIDIA (rivafb and nvidiafb) FRAMEBUFFER DRIVER
drivers/watchdog/Kconfig +11 -0
index f81705f8539a..4c4f826368c4 100644
@@ -748,6 +748,17 @@ config MAX77620_WATCHDOG
 	  MAX77620 chips. To compile this driver as a module,
 	  choose M here: the module will be called max77620_wdt.
 
+config NCT6694_WATCHDOG
+	tristate "Nuvoton NCT6694 watchdog support"
+	depends on MFD_NCT6694
+	select WATCHDOG_CORE
+	help
+	  Say Y here to support Nuvoton NCT6694 watchdog timer
+	  functionality.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nct6694_wdt.
+
 config IMX2_WDT
 	tristate "IMX2+ Watchdog"
 	depends on ARCH_MXC || ARCH_LAYERSCAPE || COMPILE_TEST
drivers/watchdog/Makefile +1 -0
index 8411626fa162..de2a04ff8a92 100644
@@ -232,6 +232,7 @@ obj-$(CONFIG_WM831X_WATCHDOG) += wm831x_wdt.o
 obj-$(CONFIG_WM8350_WATCHDOG) += wm8350_wdt.o
 obj-$(CONFIG_MAX63XX_WATCHDOG) += max63xx_wdt.o
 obj-$(CONFIG_MAX77620_WATCHDOG) += max77620_wdt.o
+obj-$(CONFIG_NCT6694_WATCHDOG) += nct6694_wdt.o
 obj-$(CONFIG_ZIIRAVE_WATCHDOG) += ziirave_wdt.o
 obj-$(CONFIG_SOFT_WATCHDOG) += softdog.o
 obj-$(CONFIG_MENF21BMC_WATCHDOG) += menf21bmc_wdt.o
drivers/watchdog/nct6694_wdt.c new file +295 -0

295 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#21 lore raw

PATCH v5 6/7 hwmon: Add Nuvoton NCT6694 HWMON support

In reply to Ming Yu

This driver supports Hardware monitor functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS                   |   1 +
 drivers/hwmon/Kconfig         |  10 +
 drivers/hwmon/Makefile        |   1 +
 drivers/hwmon/nct6694-hwmon.c | 947 ++++++++++++++++++++++++++++++++++
 4 files changed, 959 insertions(+)
 create mode 100644 drivers/hwmon/nct6694-hwmon.c
MAINTAINERS +1 -0
index a772747d493f..a14a2d65b8a6 100644
@@ -16722,6 +16722,7 @@ M:	Ming Yu <[email protected]>
 L:	[email protected]
 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/net/can/usb/nct6694_canfd.c
drivers/hwmon/Kconfig +10 -0
index dd376602f3f1..df40986424bd 100644
@@ -1636,6 +1636,16 @@ config SENSORS_NCT6683
 	  This driver can also be built as a module. If so, the module
 	  will be called nct6683.
 
+config SENSORS_NCT6694
+	tristate "Nuvoton NCT6694 Hardware Monitor support"
+	depends on MFD_NCT6694
+	help
+	  Say Y here to support Nuvoton NCT6694 hardware monitoring
+	  functionality.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called nct6694-hwmon.
+
 config SENSORS_NCT6775_CORE
 	tristate
 	select REGMAP
drivers/hwmon/Makefile +1 -0
index b827b92f2a78..27a43e67cdb7 100644
@@ -168,6 +168,7 @@ obj-$(CONFIG_SENSORS_MLXREG_FAN) += mlxreg-fan.o
 obj-$(CONFIG_SENSORS_MENF21BMC_HWMON) += menf21bmc_hwmon.o
 obj-$(CONFIG_SENSORS_MR75203)	+= mr75203.o
 obj-$(CONFIG_SENSORS_NCT6683)	+= nct6683.o
+obj-$(CONFIG_SENSORS_NCT6694)	+= nct6694-hwmon.o
 obj-$(CONFIG_SENSORS_NCT6775_CORE) += nct6775-core.o
 nct6775-objs			:= nct6775-platform.o
 obj-$(CONFIG_SENSORS_NCT6775)	+= nct6775.o
drivers/hwmon/nct6694-hwmon.c new file +947 -0

947 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#22 lore raw

PATCH v5 7/7 rtc: Add Nuvoton NCT6694 RTC support

In reply to Ming Yu

This driver supports RTC functionality for NCT6694 MFD device based on USB interface.

---
 MAINTAINERS               |   1 +
 drivers/rtc/Kconfig       |  10 ++
 drivers/rtc/Makefile      |   1 +
 drivers/rtc/rtc-nct6694.c | 286 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 298 insertions(+)
 create mode 100644 drivers/rtc/rtc-nct6694.c
MAINTAINERS +1 -0
index a14a2d65b8a6..5275ff7a793c 100644
@@ -16726,6 +16726,7 @@ F:	drivers/hwmon/nct6694-hwmon.c
 F:	drivers/i2c/busses/i2c-nct6694.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
 F:	include/linux/mfd/nct6694.h
 
drivers/rtc/Kconfig +10 -0
index a60bcc791a48..aeab67acbc84 100644
@@ -416,6 +416,16 @@ config RTC_DRV_NCT3018Y
 	   This driver can also be built as a module, if so, the module will be
 	   called "rtc-nct3018y".
 
+config RTC_DRV_NCT6694
+	tristate "Nuvoton NCT6694 RTC support"
+	depends on MFD_NCT6694
+	help
+	  If you say yes to this option, support will be included for Nuvoton
+	  NCT6694, a USB device to RTC.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called rtc-nct6694.
+
 config RTC_DRV_RK808
 	tristate "Rockchip RK805/RK808/RK809/RK817/RK818 RTC"
 	depends on MFD_RK8XX
drivers/rtc/Makefile +1 -0
index 489b4ab07068..d0d6f4a4972e 100644
@@ -118,6 +118,7 @@ obj-$(CONFIG_RTC_DRV_MXC)	+= rtc-mxc.o
 obj-$(CONFIG_RTC_DRV_MXC_V2)	+= rtc-mxc_v2.o
 obj-$(CONFIG_RTC_DRV_GAMECUBE)	+= rtc-gamecube.o
 obj-$(CONFIG_RTC_DRV_NCT3018Y)	+= rtc-nct3018y.o
+obj-$(CONFIG_RTC_DRV_NCT6694)	+= rtc-nct6694.o
 obj-$(CONFIG_RTC_DRV_NTXEC)	+= rtc-ntxec.o
 obj-$(CONFIG_RTC_DRV_OMAP)	+= rtc-omap.o
 obj-$(CONFIG_RTC_DRV_OPAL)	+= rtc-opal.o
drivers/rtc/rtc-nct6694.c new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
2.34.1