All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Victor Chernyakin" <chernyakin.victor.j@outlook.com>
Cc: linux-hwmon@vger.kernel.org, linux-input@vger.kernel.org,
	dmitry.torokhov@gmail.com
Subject: Re: [PATCH] hwmon: Add Razer PWM PC Fan Controller driver
Date: Sat, 05 Dec 2026 19:36:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-251860@kernel.org> (raw)
In-Reply-To: <SN1PR12MB25286AB7CB896D4B92FD8914A5492@SN1PR12MB2528.namprd12.prod.outlook.com>

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

  reply	other threads:[~2026-12-05 19:36 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 19:13 [PATCH] hwmon: Add Razer PWM PC Fan Controller driver Victor Chernyakin
2026-12-05 19:36 ` sashiko-bot [this message]
2026-12-05 19:40   ` Guenter Roeck
2026-12-05 22:18   ` Victor Chernyakin
2026-12-05 23:46     ` Guenter Roeck
2026-12-06  0:06       ` Victor Chernyakin
2026-12-05 19:37 ` Guenter Roeck
2026-12-05 22:26   ` Victor Chernyakin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251860@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=chernyakin.victor.j@outlook.com \
    --cc=dmitry.torokhov@gmail.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.