Linux Input/HID development
 help / color / mirror / Atom feed
* [PATCH] hwmon: Add Razer PWM PC Fan Controller driver
@ 2026-12-05 19:13 Victor Chernyakin
  2026-12-05 19:36 ` sashiko-bot
  2026-12-05 19:37 ` Guenter Roeck
  0 siblings, 2 replies; 4+ messages in thread
From: Victor Chernyakin @ 2026-12-05 19:13 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Victor Chernyakin,
	Jiri Kosina, Benjamin Tissoires, linux-hwmon, linux-doc,
	linux-kernel, linux-input

This is a USB fan controller (VID 0x1532, PID 0x0F3C) that supports
up to 8 connected fans. The driver exposes their RPM readings and allows
setting their PWM duty cycles.

The controller can report which of its ports actually have fans connected,
and whether those fans are 3-wire or 4-wire. The driver uses this
information to limit the exposed sysfs attributes to meaningful ones.

Although the controller supports hotplugging fans (it can generate an event
when this happens), I've chosen to not implement this in the driver,
because it seems like a big headache (manually tracking and dynamically
adding/removing all those sysfs files) for little gain (is anyone actually
hotplugging their fans?). Instead the driver just queries which fans are
present once at startup and goes with that.

Signed-off-by: Victor Chernyakin <chernyakin.victor.j@outlook.com>
---

I've included two nonstandard sysfs files that expose the controller's
firmware version and serial number. I know the contributing guidelines
discourage nonstandard attributes, but this seems like a reasonable use,
for this kind of extra info? If not I can remove them.

Tested by manually poking sysfs attributes, and in CoolerControl.

---
 Documentation/hwmon/index.rst                 |   1 +
 .../hwmon/razer_pwm_pc_fan_controller.rst     |  39 ++
 MAINTAINERS                                   |   7 +
 drivers/hid/hid-quirks.c                      |   1 +
 drivers/hwmon/Kconfig                         |  10 +
 drivers/hwmon/Makefile                        |   1 +
 drivers/hwmon/razer_pwm_pc_fan_controller.c   | 472 ++++++++++++++++++
 7 files changed, 531 insertions(+)
 create mode 100644 Documentation/hwmon/razer_pwm_pc_fan_controller.rst
 create mode 100644 drivers/hwmon/razer_pwm_pc_fan_controller.c

diff --git a/Documentation/hwmon/index.rst b/Documentation/hwmon/index.rst
index 9955a525436a..ebe686e9547f 100644
--- a/Documentation/hwmon/index.rst
+++ b/Documentation/hwmon/index.rst
@@ -235,6 +235,7 @@ Hardware Monitoring Kernel Drivers
    q54sj108a2
    qnap-mcu-hwmon
    raspberrypi-hwmon
+   razer_pwm_pc_fan_controller
    sbrmi
    sbtsi_temp
    sch5627
diff --git a/Documentation/hwmon/razer_pwm_pc_fan_controller.rst b/Documentation/hwmon/razer_pwm_pc_fan_controller.rst
new file mode 100644
index 000000000000..c90da5df1bfa
--- /dev/null
+++ b/Documentation/hwmon/razer_pwm_pc_fan_controller.rst
@@ -0,0 +1,39 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+Kernel driver razer_pwm_pc_fan_controller
+=========================================
+
+Supported devices:
+
+* Razer PWM PC Fan Controller (USB, VID 0x1532, PID 0x0F3C)
+
+Author: Victor Chernyakin <chernyakin.victor.j@outlook.com>
+
+Description
+-----------
+
+This driver provides hwmon support for the Razer PWM PC Fan Controller, a USB
+fan controller that supports up to 8 3-wire or 4-wire fans.
+
+At startup, the driver queries which of the controller's ports have fans
+connected, and doesn't query this again. This means that if a fan is hotplugged,
+the change will not be detected until the controller itself is disconnected
+and reconnected, or the system is rebooted.
+
+Sysfs entries
+-------------
+
+================= ==== ========================================================
+fan[1-8]_input    RO   Fan speed in RPM. Only exposed for those ports that
+                       have a fan connected (whether 3-wire or 4-wire).
+pwm[1-8]          RW   PWM duty cycle (0-255). Reads returns the last
+                       successfully written value. Initialized to 127.
+                       Only exposed for those ports that have a 4-wire fan
+		       connected. Because the controller's firmware doesn't
+		       allow setting a duty cycle less than 33%, any values
+		       below 85 are rounded up to 85 (expect for 0, which
+		       disables the fan).
+firmware_version  RO   The controller's firmware version in the format
+                       "%u.%u.%u.%u".
+serial_number     RO   The controller's serial number as an ASCII string.
+================= ==== ========================================================
diff --git a/MAINTAINERS b/MAINTAINERS
index 4d926c47ad8d..4483023994e1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -22834,6 +22834,13 @@ F:	drivers/clk/clk-rp1.c
 F:	drivers/misc/rp1/
 F:	drivers/pinctrl/pinctrl-rp1.c
 
+RAZER PWM PC FAN CONTROLLER DRIVER
+M:	Victor Chernyakin <chernyakin.victor.j@outlook.com>
+L:	linux-hwmon@vger.kernel.org
+S:	Maintained
+F:	Documentation/hwmon/razer_pwm_pc_fan_controller.rst
+F:	drivers/hwmon/razer_pwm_pc_fan_controller.c
+
 RC-CORE / LIRC FRAMEWORK
 M:	Sean Young <sean@mess.org>
 L:	linux-media@vger.kernel.org
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 96a36c5ba04f..641582a5005c 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -973,6 +973,7 @@ static const struct hid_device_id hid_ignore_list[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_PETZL, USB_DEVICE_ID_PETZL_HEADLAMP) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
+	{ HID_USB_DEVICE(USB_VENDOR_ID_RAZER, 0x0F3C) },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_SAI, USB_DEVICE_ID_CYPRESS_HIDCOM) },
 #if IS_ENABLED(CONFIG_MOUSE_SYNAPTICS_USB)
 	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_TP) },
diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig
index fecff8610ea8..79093c48781f 100644
--- a/drivers/hwmon/Kconfig
+++ b/drivers/hwmon/Kconfig
@@ -1981,6 +1981,16 @@ config SENSORS_RASPBERRYPI_HWMON
 	  This driver can also be built as a module. If so, the module
 	  will be called raspberrypi-hwmon.
 
+config SENSORS_RAZER_PWM_PC_FAN_CONTROLLER
+	tristate "Razer PWM PC Fan Controller"
+	depends on USB
+	help
+	  This driver provides support for the 8-channel Razer PWM PC Fan
+	  Controller.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called razer_pwm_pc_fan_controller.
+
 config SENSORS_SL28CPLD
 	tristate "Kontron sl28cpld hardware monitoring driver"
 	depends on MFD_SL28CPLD || COMPILE_TEST
diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile
index 1229b6b3996d..abf23654a3a5 100644
--- a/drivers/hwmon/Makefile
+++ b/drivers/hwmon/Makefile
@@ -207,6 +207,7 @@ obj-$(CONFIG_SENSORS_PT5161L)	+= pt5161l.o
 obj-$(CONFIG_SENSORS_PWM_FAN)	+= pwm-fan.o
 obj-$(CONFIG_SENSORS_QNAP_MCU_HWMON)	+= qnap-mcu-hwmon.o
 obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON)	+= raspberrypi-hwmon.o
+obj-$(CONFIG_SENSORS_RAZER_PWM_PC_FAN_CONTROLLER) += razer_pwm_pc_fan_controller.o
 obj-$(CONFIG_SENSORS_SBTSI)	+= sbtsi_temp.o
 obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
 obj-$(CONFIG_SENSORS_SCH5627)	+= sch5627.o
diff --git a/drivers/hwmon/razer_pwm_pc_fan_controller.c b/drivers/hwmon/razer_pwm_pc_fan_controller.c
new file mode 100644
index 000000000000..dc6dbe096e11
--- /dev/null
+++ b/drivers/hwmon/razer_pwm_pc_fan_controller.c
@@ -0,0 +1,472 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Razer PWM PC Fan Controller driver
+ *
+ * Based on a userspace C# driver by Evan Mulawski, a kernel driver
+ * derived from it by SlimRG, and partly on code from the OpenRazer project.
+ *
+ * Copyright (C) 2026 Victor Chernyakin <chernyakin.victor.j@outlook.com>
+ *
+ * As with all Razer device drivers, this driver was developed by reverse
+ * engineering Razer's config program Synapse. The controller's protocol
+ * is still not 100% understood, hence some fields here have funny
+ * names like "always_1".
+ *
+ * This driver was developed against firmware version 1.1.0.0. If you're seeing
+ * issues, one thing to check is whether your firmware version differs.
+ */
+
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/hid.h>
+#include <linux/hwmon.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/sysfs.h>
+#include <linux/usb.h>
+
+#define RAZER_PWM_CHANNEL_COUNT		8
+
+#define RAZER_PWM_STATUS_BUSY		0x01
+#define RAZER_PWM_STATUS_SUCCESS	0x02
+#define RAZER_PWM_STATUS_ERROR		0x03
+#define RAZER_PWM_STATUS_TIMEDOUT	0x04
+#define RAZER_PWM_STATUS_INVALID	0x05
+
+#define RAZER_PWM_CMD(_command, _payload_len)	\
+	.command = cpu_to_be16(_command),	\
+	.payload_len = (_payload_len)
+
+#define RAZER_PWM_CMD_GET_SERIAL_NUMBER		RAZER_PWM_CMD(0x0082, 16)
+#define RAZER_PWM_CMD_GET_FIRMWARE_VERSION	RAZER_PWM_CMD(0x0087,  4)
+#define RAZER_PWM_CMD_GET_FAN_INFO		RAZER_PWM_CMD(0x00B5, 80)
+#define RAZER_PWM_CMD_SET_CHANNEL_MODE		RAZER_PWM_CMD(0x0D02,  4)
+#define RAZER_PWM_CMD_SET_DUTY_CYCLE		RAZER_PWM_CMD(0x0D0D,  3)
+#define RAZER_PWM_CMD_GET_RPM			RAZER_PWM_CMD(0x0D81,  6)
+
+#define RAZER_PWM_CHANNEL_MODE_MANUAL		0x04
+#define RAZER_PWM_CHANNEL_MODE_QUIET		0x05
+#define RAZER_PWM_CHANNEL_MODE_NORMAL		0x06
+#define RAZER_PWM_CHANNEL_MODE_PERFORMANCE	0x07
+
+#define RAZER_PWM_FAN_TYPE_NOT_PRESENT	0
+#define RAZER_PWM_FAN_TYPE_4WIRE	1
+#define RAZER_PWM_FAN_TYPE_3WIRE	2
+
+struct __packed razer_pwm_report {
+	u8 status;
+	u8 sequence_number;
+	__be16 remaining_count;
+	u8 protocol_type;
+	u8 payload_len;
+	__be16 command;
+	union {
+		u8 payload[80];
+
+		char serial_number[16]; /* ASCII string, null terminated. */
+
+		struct __packed {
+			u8 major;
+			u8 minor;
+			u8 patch;
+			u8 hotfix;
+		} firmware_version;
+
+		struct __packed {
+			u8 always_5; /* Is this why the channels below are + 5? */
+			u8 always_8; /* The number of channels? */
+			u8 fan_type[RAZER_PWM_CHANNEL_COUNT];
+		} fan_info;
+
+		struct __packed {
+			u8 always_1;
+			u8 channel_plus_5;
+			u8 percent;
+		} set_duty_cycle;
+
+		struct __packed {
+			u8 always_1;
+			u8 channel_plus_5;
+			u8 mode;
+		} set_channel_mode;
+
+		struct __packed {
+			u8 always_1;
+			u8 channel_plus_5;
+			__be32 rpm;
+		} get_rpm;
+	};
+	u8 checksum;
+	u8 reserved;
+};
+static_assert(sizeof(struct razer_pwm_report) == 90);
+
+struct razer_pwm_dev {
+	__dma_from_device_group_begin();
+	struct razer_pwm_report report;
+	__dma_from_device_group_end();
+	struct usb_device *usbdev;
+	u8 cached_pwm[RAZER_PWM_CHANNEL_COUNT]; /* [0, 255] */
+	bool cached_pwm_might_be_stale[RAZER_PWM_CHANNEL_COUNT];
+	u8 fan_type[RAZER_PWM_CHANNEL_COUNT];
+	u8 current_sequence_number;
+};
+
+static u8 razer_pwm_compute_checksum(const struct razer_pwm_report *report)
+{
+	u8 checksum = 0;
+
+	/* Not all the report's bytes participate in the checksum. */
+	for (int i = 2; i < 88; i++)
+		checksum ^= ((const u8 *)report)[i];
+
+	return checksum;
+}
+
+static int razer_pwm_send_report(struct razer_pwm_dev *d)
+{
+	unsigned long timeout;
+	int ret;
+
+	++d->current_sequence_number;
+	d->current_sequence_number %= 31; /* Synapse also wraps around after 31. */
+
+	d->report.sequence_number = d->current_sequence_number;
+	d->report.checksum = razer_pwm_compute_checksum(&d->report);
+
+	ret = usb_control_msg(d->usbdev, usb_sndctrlpipe(d->usbdev, 0),
+			      HID_REQ_SET_REPORT,
+			      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
+			      0x300, 0, &d->report, sizeof(d->report),
+			      USB_CTRL_SET_TIMEOUT);
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(d->report))
+		return -EIO;
+
+	timeout = jiffies + msecs_to_jiffies(500);
+	for (;;) {
+		msleep(10); /* Synapse seems to use approximately this delay. */
+
+		ret = usb_control_msg(d->usbdev, usb_rcvctrlpipe(d->usbdev, 0),
+				      HID_REQ_GET_REPORT,
+				      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
+				      0x300, 0, &d->report, sizeof(d->report),
+				      USB_CTRL_GET_TIMEOUT);
+		if (ret < 0)
+			return ret;
+		if (ret != sizeof(d->report))
+			return -EIO;
+
+		if (d->report.checksum != razer_pwm_compute_checksum(&d->report))
+			return -EIO;
+
+		if (d->report.sequence_number != d->current_sequence_number ||
+		    d->report.status == RAZER_PWM_STATUS_BUSY) {
+			if (time_after(jiffies, timeout))
+				return -ETIMEDOUT;
+
+			continue;
+		}
+
+		switch (d->report.status) {
+		case RAZER_PWM_STATUS_SUCCESS:
+			return 0;
+		case RAZER_PWM_STATUS_TIMEDOUT:
+			return -ETIMEDOUT;
+		case RAZER_PWM_STATUS_INVALID:
+			return -EINVAL;
+		default:
+			return -EIO;
+		}
+	}
+}
+
+static int razer_pwm_set_pwm(struct razer_pwm_dev *d, u8 channel, u8 val)
+{
+	int ret;
+
+	d->report = (struct razer_pwm_report) {
+		RAZER_PWM_CMD_SET_CHANNEL_MODE,
+		.set_channel_mode = {
+			.always_1 = 1,
+			.channel_plus_5 = channel + 5,
+			.mode = RAZER_PWM_CHANNEL_MODE_MANUAL,
+		},
+	};
+	ret = razer_pwm_send_report(d);
+	if (ret)
+		return ret;
+
+	/*
+	 * The controller's firmware refuses to set any duty cycle lower
+	 * than 33% (it rounds up any lower values to 33%, expect of course for
+	 * the value 0, which disables the fan). Reflect that behaviour here.
+	 */
+	if (val != 0)
+		val = umax(val, 255 / 3);
+
+	d->report = (struct razer_pwm_report) {
+		RAZER_PWM_CMD_SET_DUTY_CYCLE,
+		.set_duty_cycle = {
+			.always_1 = 1,
+			.channel_plus_5 = channel + 5,
+			.percent = DIV_ROUND_CLOSEST(val * 100, 255),
+		},
+	};
+	ret = razer_pwm_send_report(d);
+	d->cached_pwm_might_be_stale[channel] = !!ret;
+	if (ret)
+		return ret;
+
+	d->cached_pwm[channel] = val;
+	return 0;
+}
+
+static int razer_pwm_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
+				u32 attr, int channel, long *val)
+{
+	struct razer_pwm_dev *d = dev_get_drvdata(dev);
+	int ret;
+
+	switch (type) {
+	case hwmon_fan:
+		switch (attr) {
+		case hwmon_fan_input:
+			d->report = (struct razer_pwm_report) {
+				RAZER_PWM_CMD_GET_RPM,
+				.get_rpm = {
+					.always_1 = 1,
+					.channel_plus_5 = channel + 5,
+				},
+			};
+			ret = razer_pwm_send_report(d);
+			if (ret)
+				return ret;
+
+			*val = be32_to_cpu(d->report.get_rpm.rpm);
+			return 0;
+		default:
+			return -EOPNOTSUPP;
+		}
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (d->cached_pwm_might_be_stale[channel])
+				return -ENODATA;
+
+			*val = d->cached_pwm[channel];
+			return 0;
+		default:
+			return -EOPNOTSUPP;
+		}
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static int razer_pwm_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
+				 u32 attr, int channel, long val)
+{
+	struct razer_pwm_dev *d = dev_get_drvdata(dev);
+
+	switch (type) {
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			if (val < 0 || val > 255)
+				return -EINVAL;
+
+			return razer_pwm_set_pwm(d, channel, val);
+		default:
+			return -EOPNOTSUPP;
+		}
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static umode_t razer_pwm_hwmon_is_visible(const void *drvdata,
+					  enum hwmon_sensor_types type,
+					  u32 attr, int channel)
+{
+	const struct razer_pwm_dev *d = drvdata;
+
+	switch (type) {
+	case hwmon_fan:
+		switch (attr) {
+		case hwmon_fan_input:
+			switch (d->fan_type[channel]) {
+			case RAZER_PWM_FAN_TYPE_3WIRE:
+			case RAZER_PWM_FAN_TYPE_4WIRE:
+				return 0444;
+			default:
+				return 0;
+			}
+		default:
+			return 0;
+		}
+	case hwmon_pwm:
+		switch (attr) {
+		case hwmon_pwm_input:
+			switch (d->fan_type[channel]) {
+			case RAZER_PWM_FAN_TYPE_4WIRE:
+				return 0644;
+			default:
+				return 0;
+			}
+		default:
+			return 0;
+		}
+	default:
+		return 0;
+	}
+}
+
+static ssize_t firmware_version_show(struct device *dev,
+				     struct device_attribute *attr, char *buf)
+{
+	struct razer_pwm_dev *d = dev_get_drvdata(dev);
+	int ret;
+
+	d->report = (struct razer_pwm_report) {
+		RAZER_PWM_CMD_GET_FIRMWARE_VERSION,
+	};
+	ret = razer_pwm_send_report(d);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%u.%u.%u.%u\n",
+			  d->report.firmware_version.major,
+			  d->report.firmware_version.minor,
+			  d->report.firmware_version.patch,
+			  d->report.firmware_version.hotfix);
+}
+
+static ssize_t serial_number_show(struct device *dev,
+				  struct device_attribute *attr, char *buf)
+{
+	struct razer_pwm_dev *d = dev_get_drvdata(dev);
+	int ret;
+
+	d->report = (struct razer_pwm_report) {
+		RAZER_PWM_CMD_GET_SERIAL_NUMBER,
+	};
+	ret = razer_pwm_send_report(d);
+	if (ret)
+		return ret;
+
+	return sysfs_emit(buf, "%.*s\n",
+			  (int)sizeof(d->report.serial_number),
+			  d->report.serial_number);
+}
+
+static const DEVICE_ATTR_RO(firmware_version);
+static const DEVICE_ATTR_RO(serial_number);
+
+static const struct attribute *const razer_pwm_attrs[] = {
+	&dev_attr_firmware_version.attr,
+	&dev_attr_serial_number.attr,
+	NULL
+};
+
+ATTRIBUTE_GROUPS(razer_pwm);
+
+static const struct hwmon_ops razer_pwm_hwmon_ops = {
+	.is_visible = razer_pwm_hwmon_is_visible,
+	.read = razer_pwm_hwmon_read,
+	.write = razer_pwm_hwmon_write,
+};
+
+static const struct hwmon_channel_info *const razer_pwm_hwmon_info[] = {
+	HWMON_CHANNEL_INFO(fan,
+			   HWMON_F_INPUT, HWMON_F_INPUT,
+			   HWMON_F_INPUT, HWMON_F_INPUT,
+			   HWMON_F_INPUT, HWMON_F_INPUT,
+			   HWMON_F_INPUT, HWMON_F_INPUT),
+	HWMON_CHANNEL_INFO(pwm,
+			   HWMON_PWM_INPUT, HWMON_PWM_INPUT,
+			   HWMON_PWM_INPUT, HWMON_PWM_INPUT,
+			   HWMON_PWM_INPUT, HWMON_PWM_INPUT,
+			   HWMON_PWM_INPUT, HWMON_PWM_INPUT),
+	NULL
+};
+
+static const struct hwmon_chip_info razer_pwm_chip_info = {
+	.ops = &razer_pwm_hwmon_ops,
+	.info = razer_pwm_hwmon_info,
+};
+
+static int razer_pwm_suspend(struct usb_interface *intf, pm_message_t message)
+{
+	return 0;
+}
+
+static int razer_pwm_resume(struct usb_interface *intf)
+{
+	struct razer_pwm_dev *d = usb_get_intfdata(intf);
+
+	for (int channel = 0; channel < RAZER_PWM_CHANNEL_COUNT; channel++) {
+		if (d->fan_type[channel] == RAZER_PWM_FAN_TYPE_4WIRE)
+			(void)razer_pwm_set_pwm(d, channel, d->cached_pwm[channel]);
+	}
+
+	return 0;
+}
+
+static int razer_pwm_probe(struct usb_interface *intf, const struct usb_device_id *id)
+{
+	struct device *hwmon_dev;
+	struct razer_pwm_dev *d;
+	int ret;
+
+	d = devm_kzalloc(&intf->dev, sizeof(*d), GFP_KERNEL);
+	if (!d)
+		return -ENOMEM;
+
+	d->usbdev = interface_to_usbdev(intf);
+	usb_set_intfdata(intf, d);
+
+	d->report = (struct razer_pwm_report) {
+		RAZER_PWM_CMD_GET_FAN_INFO,
+	};
+	ret = razer_pwm_send_report(d);
+	if (ret)
+		return ret;
+
+	memcpy(d->fan_type, d->report.fan_info.fan_type, sizeof(d->fan_type));
+
+	/* Set fans to 50% by default. */
+	memset(d->cached_pwm, 127, sizeof(d->cached_pwm));
+	razer_pwm_resume(intf);
+
+	hwmon_dev = devm_hwmon_device_register_with_info(&intf->dev,
+							 "razer_pwm_pc_fan_controller",
+							 d,
+							 &razer_pwm_chip_info,
+							 razer_pwm_groups);
+	return PTR_ERR_OR_ZERO(hwmon_dev);
+}
+
+static void razer_pwm_disconnect(struct usb_interface *intf) {}
+
+static const struct usb_device_id razer_pwm_table[] = {
+	{ USB_DEVICE_INTERFACE_NUMBER(0x1532, 0x0F3C, 0x00) },
+	{ }
+};
+MODULE_DEVICE_TABLE(usb, razer_pwm_table);
+
+static struct usb_driver razer_pwm_driver = {
+	.name = "razer_pwm_pc_fan_controller",
+	.id_table = razer_pwm_table,
+	.probe = razer_pwm_probe,
+	.disconnect = razer_pwm_disconnect,
+	.suspend = razer_pwm_suspend,
+	.resume = razer_pwm_resume,
+};
+module_usb_driver(razer_pwm_driver);
+
+MODULE_AUTHOR("Victor Chernyakin <chernyakin.victor.j@outlook.com>");
+MODULE_DESCRIPTION("Razer PWM PC Fan Controller driver");
+MODULE_LICENSE("GPL");
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: Add Razer PWM PC Fan Controller driver
  2026-12-05 19:13 [PATCH] hwmon: Add Razer PWM PC Fan Controller driver Victor Chernyakin
@ 2026-12-05 19:36 ` sashiko-bot
  2026-12-05 19:40   ` Guenter Roeck
  2026-12-05 19:37 ` Guenter Roeck
  1 sibling, 1 reply; 4+ messages in thread
From: sashiko-bot @ 2026-12-05 19:36 UTC (permalink / raw)
  To: Victor Chernyakin; +Cc: linux-hwmon, linux-input, dmitry.torokhov

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [High] HID: unconditional hid_ignore_list entry breaks hidraw for
  1532:0F3C
- [High] hwmon: missing hwmon_lock() in razer_pwm custom sysfs show
  callbacks
- [Medium] hwmon: razer_pwm_disconnect() allows USB control I/O after
  returning
- [Medium] hwmon: razer_pwm_set_pwm() leaves cached PWM valid after mode
  error
- [Medium] hwmon: missing .reset_resume in razer_pwm_driver drops PWM
  state on resume
- [Medium] hwmon: partial union init in razer_pwm_report leaves tail
  bytes uninitialized
- [Low] hwmon: non-standard firmware_version and serial_number sysfs
  attributes
- [Low] hwmon: off-by-one sequence wrap-around in
  razer_pwm_send_report()
--

commit 34454e665e3840bb53e9795a612560c2739013e6
Author: Victor Chernyakin <chernyakin.victor.j@outlook.com>

hwmon: Add Razer PWM PC Fan Controller driver

This commit adds a USB hwmon driver for the Razer PWM PC Fan Controller,
exposing fan speed readings, PWM duty cycle control, and firmware/serial
attributes, and adds the device to the HID ignore list.

> diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
> index 793db001a62b7..17813b2557666 100644
> --- a/drivers/hid/hid-quirks.c
> +++ b/drivers/hid/hid-quirks.c
> @@ -976,6 +976,7 @@ static const struct hid_device_id hid_ignore_list[] = {
>  	{ HID_USB_DEVICE(USB_VENDOR_ID_PETZL, USB_DEVICE_ID_PETZL_HEADLAMP) },
>  	{ HID_USB_DEVICE(USB_VENDOR_ID_PHILIPS, USB_DEVICE_ID_PHILIPS_IEEE802154_DONGLE) },
>  	{ HID_USB_DEVICE(USB_VENDOR_ID_POWERCOM, USB_DEVICE_ID_POWERCOM_UPS) },
> +	{ HID_USB_DEVICE(USB_VENDOR_ID_RAZER, 0x0F3C) },
>  	{ HID_USB_DEVICE(USB_VENDOR_ID_SAI, USB_DEVICE_ID_CYPRESS_HIDCOM) },
>  #if IS_ENABLED(CONFIG_MOUSE_SYNAPTICS_USB)
>  	{ HID_USB_DEVICE(USB_VENDOR_ID_SYNAPTICS, USB_DEVICE_ID_SYNAPTICS_TP) },

[Severity: High]
Does adding 0x1532:0x0F3C unconditionally to hid_ignore_list[] break
existing userspace hidraw access?

When CONFIG_SENSORS_RAZER_PWM_PC_FAN_CONTROLLER is disabled, usbhid still
rejects the device because hid_gets_squirk() applies HID_QUIRK_IGNORE to
every entry in hid_ignore_list[], leaving userspace tools with no
/dev/hidraw* node and no kernel driver bound.  In addition,
hid_ignore_list[] blocks all HID interfaces on the device even though
razer_pwm_table only binds interface 0x00.

Since razer_pwm_send_report() only sends standard HID Feature Report
requests (HID_REQ_SET_REPORT and HID_REQ_GET_REPORT), could this driver be
implemented as a struct hid_driver using hid_hw_raw_request() instead,
avoiding hid_ignore_list[] altogether?

> diff --git a/drivers/hwmon/razer_pwm_pc_fan_controller.c b/drivers/hwmon/razer_pwm_pc_fan_controller.c
> new file mode 100644
> index 0000000000000..dc6dbe096e11b
> --- /dev/null
> +++ b/drivers/hwmon/razer_pwm_pc_fan_controller.c

[ ... ]

> +static int razer_pwm_send_report(struct razer_pwm_dev *d)
> +{
> +	unsigned long timeout;
> +	int ret;
> +
> +	++d->current_sequence_number;
> +	d->current_sequence_number %= 31; /* Synapse also wraps around after 31. */
> +
> +	d->report.sequence_number = d->current_sequence_number;

[Severity: Low]
Is there an off-by-one in the sequence number wrap-around?

Using %= 31 wraps 31 to 0, so sequence number 31 is never emitted and the
counter wraps after 30 instead of after 31 as the comment states.  When
d->current_sequence_number wraps to 0 on every 31st transfer, an all-zero
response would also pass both the checksum and sequence_number checks and
return -EIO immediately instead of retrying.

[ ... ]

> +static int razer_pwm_set_pwm(struct razer_pwm_dev *d, u8 channel, u8 val)
> +{
> +	int ret;
> +
> +	d->report = (struct razer_pwm_report) {
> +		RAZER_PWM_CMD_SET_CHANNEL_MODE,
> +		.set_channel_mode = {
> +			.always_1 = 1,
> +			.channel_plus_5 = channel + 5,
> +			.mode = RAZER_PWM_CHANNEL_MODE_MANUAL,
> +		},
> +	};

[Severity: Medium]
Can assigning a compound literal with a smaller union member leave the
remaining bytes of d->report uninitialized?

On compilers without -fzero-init-padding-bits=all (such as GCC 8.1 through
14), bytes beyond the designated union member are unspecified.  For
RAZER_PWM_CMD_SET_CHANNEL_MODE, payload_len is 4 while set_channel_mode
only defines 3 bytes, so byte 3 of the command payload and the remaining
tail bytes in the 80-byte union can contain stale response bytes or
uninitialized stack data that get checksummed and sent over USB.  The same
applies to the .set_duty_cycle and .get_rpm assignments in
razer_pwm_set_pwm() and razer_pwm_hwmon_read().

Zeroing d->report with memset() before populating the request fields would
avoid this across all supported compilers.

> +	ret = razer_pwm_send_report(d);
> +	if (ret)
> +		return ret;
> +
> +	/*
> +	 * The controller's firmware refuses to set any duty cycle lower
> +	 * than 33% (it rounds up any lower values to 33%, expect of course for
> +	 * the value 0, which disables the fan). Reflect that behaviour here.
> +	 */
> +	if (val != 0)
> +		val = umax(val, 255 / 3);
> +
> +	d->report = (struct razer_pwm_report) {
> +		RAZER_PWM_CMD_SET_DUTY_CYCLE,
> +		.set_duty_cycle = {
> +			.always_1 = 1,
> +			.channel_plus_5 = channel + 5,
> +			.percent = DIV_ROUND_CLOSEST(val * 100, 255),
> +		},
> +	};
> +	ret = razer_pwm_send_report(d);
> +	d->cached_pwm_might_be_stale[channel] = !!ret;
> +	if (ret)
> +		return ret;

[Severity: Medium]
If the first razer_pwm_send_report() for RAZER_PWM_CMD_SET_CHANNEL_MODE
fails, does this leave d->cached_pwm_might_be_stale[channel] as false?

During probe and resume, razer_pwm_resume() ignores errors from
razer_pwm_set_pwm(), and d->cached_pwm_might_be_stale[] starts out false
with d->cached_pwm[] pre-populated to 127.  If SET_CHANNEL_MODE fails,
subsequent reads of pwm[1-8] return the unapplied cached value instead of
-ENODATA.

Setting d->cached_pwm_might_be_stale[channel] = true before sending
SET_CHANNEL_MODE (or on its error path) would keep the cache state
consistent.

[ ... ]

> +static ssize_t firmware_version_show(struct device *dev,
> +				     struct device_attribute *attr, char *buf)
> +{
> +	struct razer_pwm_dev *d = dev_get_drvdata(dev);
> +	int ret;
> +
> +	d->report = (struct razer_pwm_report) {
> +		RAZER_PWM_CMD_GET_FIRMWARE_VERSION,
> +	};
> +	ret = razer_pwm_send_report(d);
> +	if (ret)
> +		return ret;
> +
> +	return sysfs_emit(buf, "%u.%u.%u.%u\n",
> +			  d->report.firmware_version.major,
> +			  d->report.firmware_version.minor,
> +			  d->report.firmware_version.patch,
> +			  d->report.firmware_version.hotfix);
> +}
> +
> +static ssize_t serial_number_show(struct device *dev,
> +				  struct device_attribute *attr, char *buf)
> +{
> +	struct razer_pwm_dev *d = dev_get_drvdata(dev);
> +	int ret;
> +
> +	d->report = (struct razer_pwm_report) {
> +		RAZER_PWM_CMD_GET_SERIAL_NUMBER,
> +	};
> +	ret = razer_pwm_send_report(d);

[Severity: High]
Can firmware_version_show() and serial_number_show() race with hwmon
callbacks on the shared d->report buffer and d->current_sequence_number?

Standard hwmon_ops callbacks are serialized by hwdev->lock in
hwmon_attr_show() and hwmon_attr_store(), but custom attributes passed via
extra_groups are invoked through dev_attr_show() without holding it:

CPU0                            CPU1
hwmon_attr_show()
  lock(&hwdev->lock)
  razer_pwm_hwmon_read()
    d->report = GET_RPM
    razer_pwm_send_report()
      usb_control_msg(SET)
      msleep(10)
                                firmware_version_show()
                                  d->report = GET_FW_VER  <-- overwrites
                                  razer_pwm_send_report()
                                    ++d->current_sequence_number
                                    usb_control_msg(SET)  <-- DMA race
      usb_control_msg(GET)      <-- corrupted buffer / seq mismatch

Because d->report is passed directly to usb_control_msg() for DMA, this
also violates DMA buffer ownership while a transfer is in flight and lets
any unprivileged reader of these 0444 files corrupt concurrent PWM writes
or RPM reads.  Acquiring hwmon_lock(dev) (for instance via
guard(hwmon_lock)(dev)) in both show callbacks would serialize all access.

[ ... ]

> +static const DEVICE_ATTR_RO(firmware_version);
> +static const DEVICE_ATTR_RO(serial_number);
> +
> +static const struct attribute *const razer_pwm_attrs[] = {
> +	&dev_attr_firmware_version.attr,
> +	&dev_attr_serial_number.attr,
> +	NULL
> +};

[Severity: Low]
Should firmware_version and serial_number be exposed through debugfs
instead of non-standard hwmon sysfs attributes?

Documentation/hwmon/submitting-patches.rst discourages non-standard sysfs
attributes without prior discussion and ABI documentation, and other USB
hwmon drivers (such as aquacomputer_d5next, corsair-cpro, and
nzxt-kraken3) read static metadata once at probe time and expose it in
debugfs instead of issuing live USB control transfers on every sysfs read.

[ ... ]

> +	hwmon_dev = devm_hwmon_device_register_with_info(&intf->dev,
> +							 "razer_pwm_pc_fan_controller",
> +							 d,
> +							 &razer_pwm_chip_info,
> +							 razer_pwm_groups);
> +	return PTR_ERR_OR_ZERO(hwmon_dev);
> +}
> +
> +static void razer_pwm_disconnect(struct usb_interface *intf) {}

[Severity: Medium]
Does using devm_hwmon_device_register_with_info() with an empty
razer_pwm_disconnect() allow usb_control_msg() calls to run after
disconnect() returns?

During unbind, __device_release_driver() runs usb_unbind_interface() and
driver->disconnect() before device_unbind_cleanup() runs devres cleanup to
unregister the hwmon device.  Because usb_disable_interface() does not
disable ep0, active or newly entered sysfs callbacks can keep issuing
usb_control_msg() transfers while usb_unbind_interface() resets interface
state and autosuspends the device, violating the usb_control_msg()
contract that disconnect() must wait for in-flight calls to complete.

Registering with hwmon_device_register_with_info() and calling
hwmon_device_unregister() in razer_pwm_disconnect() would ensure sysfs
callbacks drain before disconnect() returns.

[ ... ]

> +static struct usb_driver razer_pwm_driver = {
> +	.name = "razer_pwm_pc_fan_controller",
> +	.id_table = razer_pwm_table,
> +	.probe = razer_pwm_probe,
> +	.disconnect = razer_pwm_disconnect,
> +	.suspend = razer_pwm_suspend,
> +	.resume = razer_pwm_resume,
> +};

[Severity: Medium]
Should razer_pwm_driver also set .reset_resume = razer_pwm_resume?

When the USB port loses power during suspend or hibernation,
usb_resume_interface() skips driver->resume() if driver->reset_resume is
NULL and sets intf->needs_binding = 1 instead.  USB core then unbinds and
re-probes the driver, recreating the hwmon device and resetting all 4-wire
fans back to 50% (127) instead of restoring d->cached_pwm[].

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/SN1PR12MB25286AB7CB896D4B92FD8914A5492@SN1PR12MB2528.namprd12.prod.outlook.com?part=1

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: Add Razer PWM PC Fan Controller driver
  2026-12-05 19:13 [PATCH] hwmon: Add Razer PWM PC Fan Controller driver Victor Chernyakin
  2026-12-05 19:36 ` sashiko-bot
@ 2026-12-05 19:37 ` Guenter Roeck
  1 sibling, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2026-12-05 19:37 UTC (permalink / raw)
  To: Victor Chernyakin
  Cc: Jonathan Corbet, Shuah Khan, Randy Dunlap, Jiri Kosina,
	Benjamin Tissoires, linux-hwmon, linux-doc, linux-kernel,
	linux-input

On Sat, Dec 05, 2026 at 01:13:36PM -0600, Victor Chernyakin wrote:
> This is a USB fan controller (VID 0x1532, PID 0x0F3C) that supports
> up to 8 connected fans. The driver exposes their RPM readings and allows
> setting their PWM duty cycles.
> 
> The controller can report which of its ports actually have fans connected,
> and whether those fans are 3-wire or 4-wire. The driver uses this
> information to limit the exposed sysfs attributes to meaningful ones.
> 
> Although the controller supports hotplugging fans (it can generate an event
> when this happens), I've chosen to not implement this in the driver,
> because it seems like a big headache (manually tracking and dynamically
> adding/removing all those sysfs files) for little gain (is anyone actually
> hotplugging their fans?). Instead the driver just queries which fans are
> present once at startup and goes with that.
> 
> Signed-off-by: Victor Chernyakin <chernyakin.victor.j@outlook.com>
> ---
> 
> I've included two nonstandard sysfs files that expose the controller's
> firmware version and serial number. I know the contributing guidelines
> discourage nonstandard attributes, but this seems like a reasonable use,
> for this kind of extra info? If not I can remove them.
> 

Please use debugfs for such attributes.

> Tested by manually poking sysfs attributes, and in CoolerControl.

Not an actual review, but this is what checkpatch reports.

CHECK: Please use a blank line after function/struct/union/enum declarations
#404: FILE: drivers/hwmon/razer_pwm_pc_fan_controller.c:103:
+};
+static_assert(sizeof(struct razer_pwm_report) == 90);

--> Add empty line

WARNING: msleep < 20ms can sleep for up to 20ms; see function description of msleep().
#451: FILE: drivers/hwmon/razer_pwm_pc_fan_controller.c:150:
+		msleep(10); /* Synapse seems to use approximately this delay. */

--> Use fsleep().

Let's wait and see what Sashiko has to say.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] hwmon: Add Razer PWM PC Fan Controller driver
  2026-12-05 19:36 ` sashiko-bot
@ 2026-12-05 19:40   ` Guenter Roeck
  0 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2026-12-05 19:40 UTC (permalink / raw)
  To: sashiko-reviews, Victor Chernyakin
  Cc: linux-hwmon, linux-input, dmitry.torokhov

Victor,

On 12/5/26 11:36, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 8 potential
> issues to consider:
> - [High] HID: unconditional hid_ignore_list entry breaks hidraw for
>    1532:0F3C
> - [High] hwmon: missing hwmon_lock() in razer_pwm custom sysfs show
>    callbacks
> - [Medium] hwmon: razer_pwm_disconnect() allows USB control I/O after
>    returning
> - [Medium] hwmon: razer_pwm_set_pwm() leaves cached PWM valid after mode
>    error
> - [Medium] hwmon: missing .reset_resume in razer_pwm_driver drops PWM
>    state on resume
> - [Medium] hwmon: partial union init in razer_pwm_report leaves tail
>    bytes uninitialized
> - [Low] hwmon: non-standard firmware_version and serial_number sysfs
>    attributes
> - [Low] hwmon: off-by-one sequence wrap-around in
>    razer_pwm_send_report()
> --

I had a quick look, and those all seeem to be real problems.
Please fix.

Thanks,
Guenter


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05 19:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 19:13 [PATCH] hwmon: Add Razer PWM PC Fan Controller driver Victor Chernyakin
2026-12-05 19:36 ` sashiko-bot
2026-12-05 19:40   ` Guenter Roeck
2026-12-05 19:37 ` Guenter Roeck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox