1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Reverse-engineered NZXT RGB & Fan Controller/Smart Device v2 driver.
5 * Copyright (c) 2021 Aleksandr Mezin
9 #include <linux/hwmon.h>
10 #include <linux/math.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/spinlock.h>
14 #include <linux/wait.h>
16 #include <asm/byteorder.h>
17 #include <linux/unaligned.h>
20 * The device has only 3 fan channels/connectors. But all HID reports have
21 * space reserved for up to 8 channels.
23 #define FAN_CHANNELS 3
24 #define FAN_CHANNELS_MAX 8
26 #define UPDATE_INTERVAL_DEFAULT_MS 1000
28 /* These strings match labels on the device exactly */
29 static const char *const fan_label
[] = {
35 static const char *const curr_label
[] = {
41 static const char *const in_label
[] = {
48 INPUT_REPORT_ID_FAN_CONFIG
= 0x61,
49 INPUT_REPORT_ID_FAN_STATUS
= 0x67,
53 FAN_STATUS_REPORT_SPEED
= 0x02,
54 FAN_STATUS_REPORT_VOLTAGE
= 0x04,
63 struct unknown_static_data
{
65 * Some configuration data? Stays the same after fan speed changes,
66 * changes in fan configuration, reboots and driver reloads.
68 * The same data in multiple report types.
70 * Byte 12 seems to be the number of fan channels, but I am not sure.
76 * The device sends this input report in response to "detect fans" command:
77 * a 2-byte output report { 0x60, 0x03 }.
79 struct fan_config_report
{
80 /* report_id should be INPUT_REPORT_ID_FAN_CONFIG = 0x61 */
84 struct unknown_static_data unknown_data
;
85 /* Fan type as detected by the device. See FAN_TYPE_* enum. */
86 u8 fan_type
[FAN_CHANNELS_MAX
];
90 * The device sends these reports at a fixed interval (update interval) -
91 * one report with type = FAN_STATUS_REPORT_SPEED, and one report with type =
92 * FAN_STATUS_REPORT_VOLTAGE per update interval.
94 struct fan_status_report
{
95 /* report_id should be INPUT_REPORT_ID_STATUS = 0x67 */
97 /* FAN_STATUS_REPORT_SPEED = 0x02 or FAN_STATUS_REPORT_VOLTAGE = 0x04 */
99 struct unknown_static_data unknown_data
;
100 /* Fan type as detected by the device. See FAN_TYPE_* enum. */
101 u8 fan_type
[FAN_CHANNELS_MAX
];
104 /* When type == FAN_STATUS_REPORT_SPEED */
107 * Fan speed, in RPM. Zero for channels without fans
110 __le16 fan_rpm
[FAN_CHANNELS_MAX
];
112 * Fan duty cycle, in percent. Non-zero even for
113 * channels without fans connected.
115 u8 duty_percent
[FAN_CHANNELS_MAX
];
117 * Exactly the same values as duty_percent[], non-zero
118 * for disconnected fans too.
120 u8 duty_percent_dup
[FAN_CHANNELS_MAX
];
121 /* "Case Noise" in db */
123 } __packed fan_speed
;
124 /* When type == FAN_STATUS_REPORT_VOLTAGE */
127 * Voltage, in millivolts. Non-zero even when fan is
130 __le16 fan_in
[FAN_CHANNELS_MAX
];
132 * Current, in milliamperes. Near-zero when
135 __le16 fan_current
[FAN_CHANNELS_MAX
];
136 } __packed fan_voltage
;
140 #define OUTPUT_REPORT_SIZE 64
143 OUTPUT_REPORT_ID_INIT_COMMAND
= 0x60,
144 OUTPUT_REPORT_ID_SET_FAN_SPEED
= 0x62,
148 INIT_COMMAND_SET_UPDATE_INTERVAL
= 0x02,
149 INIT_COMMAND_DETECT_FANS
= 0x03,
153 * This output report sets pwm duty cycle/target fan speed for one or more
156 struct set_fan_speed_report
{
157 /* report_id should be OUTPUT_REPORT_ID_SET_FAN_SPEED = 0x62 */
161 /* To change fan speed on i-th channel, set i-th bit here */
164 * Fan duty cycle/target speed in percent. For voltage-controlled fans,
165 * the minimal voltage (duty_percent = 1) is about 9V.
166 * Setting duty_percent to 0 (if the channel is selected in
167 * channel_bit_mask) turns off the fan completely (regardless of the
170 u8 duty_percent
[FAN_CHANNELS_MAX
];
174 struct hid_device
*hid
;
175 struct device
*hwmon
;
177 u8 fan_duty_percent
[FAN_CHANNELS
];
178 u16 fan_rpm
[FAN_CHANNELS
];
179 bool pwm_status_received
;
181 u16 fan_in
[FAN_CHANNELS
];
182 u16 fan_curr
[FAN_CHANNELS
];
183 bool voltage_status_received
;
185 u8 fan_type
[FAN_CHANNELS
];
186 bool fan_config_received
;
189 * wq is used to wait for *_received flags to become true.
190 * All accesses to *_received flags and fan_* arrays are performed with
193 wait_queue_head_t wq
;
196 * 1) Prevent concurrent conflicting changes to update interval and pwm
197 * values (after sending an output hid report, the corresponding field
198 * in drvdata must be updated, and only then new output reports can be
200 * 2) Synchronize access to output_buffer (well, the buffer is here,
201 * because synchronization is necessary anyway - so why not get rid of
205 long update_interval
;
206 u8 output_buffer
[OUTPUT_REPORT_SIZE
];
209 static long scale_pwm_value(long val
, long orig_max
, long new_max
)
215 * Positive values should not become zero: 0 completely turns off the
218 return max(1L, DIV_ROUND_CLOSEST(min(val
, orig_max
) * new_max
, orig_max
));
221 static void handle_fan_config_report(struct drvdata
*drvdata
, void *data
, int size
)
223 struct fan_config_report
*report
= data
;
226 if (size
< sizeof(struct fan_config_report
))
229 if (report
->magic
!= 0x03)
232 spin_lock(&drvdata
->wq
.lock
);
234 for (i
= 0; i
< FAN_CHANNELS
; i
++)
235 drvdata
->fan_type
[i
] = report
->fan_type
[i
];
237 drvdata
->fan_config_received
= true;
238 wake_up_all_locked(&drvdata
->wq
);
239 spin_unlock(&drvdata
->wq
.lock
);
242 static void handle_fan_status_report(struct drvdata
*drvdata
, void *data
, int size
)
244 struct fan_status_report
*report
= data
;
247 if (size
< sizeof(struct fan_status_report
))
250 spin_lock(&drvdata
->wq
.lock
);
253 * The device sends INPUT_REPORT_ID_FAN_CONFIG = 0x61 report in response
254 * to "detect fans" command. Only accept other data after getting 0x61,
255 * to make sure that fan detection is complete. In particular, fan
256 * detection resets pwm values.
258 if (!drvdata
->fan_config_received
) {
259 spin_unlock(&drvdata
->wq
.lock
);
263 for (i
= 0; i
< FAN_CHANNELS
; i
++) {
264 if (drvdata
->fan_type
[i
] == report
->fan_type
[i
])
268 * This should not happen (if my expectations about the device
271 * Even if the userspace sends fan detect command through
272 * hidraw, fan config report should arrive first.
274 hid_warn_once(drvdata
->hid
,
275 "Fan %d type changed unexpectedly from %d to %d",
276 i
, drvdata
->fan_type
[i
], report
->fan_type
[i
]);
277 drvdata
->fan_type
[i
] = report
->fan_type
[i
];
280 switch (report
->type
) {
281 case FAN_STATUS_REPORT_SPEED
:
282 for (i
= 0; i
< FAN_CHANNELS
; i
++) {
283 drvdata
->fan_rpm
[i
] =
284 get_unaligned_le16(&report
->fan_speed
.fan_rpm
[i
]);
285 drvdata
->fan_duty_percent
[i
] =
286 report
->fan_speed
.duty_percent
[i
];
289 drvdata
->pwm_status_received
= true;
290 wake_up_all_locked(&drvdata
->wq
);
293 case FAN_STATUS_REPORT_VOLTAGE
:
294 for (i
= 0; i
< FAN_CHANNELS
; i
++) {
296 get_unaligned_le16(&report
->fan_voltage
.fan_in
[i
]);
297 drvdata
->fan_curr
[i
] =
298 get_unaligned_le16(&report
->fan_voltage
.fan_current
[i
]);
301 drvdata
->voltage_status_received
= true;
302 wake_up_all_locked(&drvdata
->wq
);
306 spin_unlock(&drvdata
->wq
.lock
);
309 static umode_t
nzxt_smart2_hwmon_is_visible(const void *data
,
310 enum hwmon_sensor_types type
,
311 u32 attr
, int channel
)
316 case hwmon_pwm_input
:
317 case hwmon_pwm_enable
:
326 case hwmon_chip_update_interval
:
338 static int nzxt_smart2_hwmon_read(struct device
*dev
, enum hwmon_sensor_types type
,
339 u32 attr
, int channel
, long *val
)
341 struct drvdata
*drvdata
= dev_get_drvdata(dev
);
344 if (type
== hwmon_chip
) {
346 case hwmon_chip_update_interval
:
347 *val
= drvdata
->update_interval
;
355 spin_lock_irq(&drvdata
->wq
.lock
);
361 * 1) remembers pwm* values when it starts
362 * 2) needs pwm*_enable to be 1 on controlled fans
363 * So make sure we have correct data before allowing pwm* reads.
364 * Returning errors for pwm of fan speed read can even cause
365 * fancontrol to shut down. So the wait is unavoidable.
368 case hwmon_pwm_enable
:
369 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
370 drvdata
->fan_config_received
);
374 *val
= drvdata
->fan_type
[channel
] != FAN_TYPE_NONE
;
378 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
379 drvdata
->fan_config_received
);
383 *val
= drvdata
->fan_type
[channel
] == FAN_TYPE_PWM
;
386 case hwmon_pwm_input
:
387 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
388 drvdata
->pwm_status_received
);
392 *val
= scale_pwm_value(drvdata
->fan_duty_percent
[channel
],
400 * It's not strictly necessary to wait for *_received in the
401 * remaining cases (fancontrol doesn't care about them). But I'm
402 * doing it to have consistent behavior.
404 if (attr
== hwmon_fan_input
) {
405 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
406 drvdata
->pwm_status_received
);
410 *val
= drvdata
->fan_rpm
[channel
];
415 if (attr
== hwmon_in_input
) {
416 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
417 drvdata
->voltage_status_received
);
421 *val
= drvdata
->fan_in
[channel
];
426 if (attr
== hwmon_curr_input
) {
427 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
428 drvdata
->voltage_status_received
);
432 *val
= drvdata
->fan_curr
[channel
];
441 spin_unlock_irq(&drvdata
->wq
.lock
);
445 static int send_output_report(struct drvdata
*drvdata
, const void *data
,
450 if (data_size
> sizeof(drvdata
->output_buffer
))
453 memcpy(drvdata
->output_buffer
, data
, data_size
);
455 if (data_size
< sizeof(drvdata
->output_buffer
))
456 memset(drvdata
->output_buffer
+ data_size
, 0,
457 sizeof(drvdata
->output_buffer
) - data_size
);
459 ret
= hid_hw_output_report(drvdata
->hid
, drvdata
->output_buffer
,
460 sizeof(drvdata
->output_buffer
));
461 return ret
< 0 ? ret
: 0;
464 static int set_pwm(struct drvdata
*drvdata
, int channel
, long val
)
467 u8 duty_percent
= scale_pwm_value(val
, 255, 100);
469 struct set_fan_speed_report report
= {
470 .report_id
= OUTPUT_REPORT_ID_SET_FAN_SPEED
,
472 .channel_bit_mask
= 1 << channel
475 ret
= mutex_lock_interruptible(&drvdata
->mutex
);
479 report
.duty_percent
[channel
] = duty_percent
;
480 ret
= send_output_report(drvdata
, &report
, sizeof(report
));
485 * pwmconfig and fancontrol scripts expect pwm writes to take effect
486 * immediately (i. e. read from pwm* sysfs should return the value
487 * written into it). The device seems to always accept pwm values - even
488 * when there is no fan connected - so update pwm status without waiting
489 * for a report, to make pwmconfig and fancontrol happy. Worst case -
490 * if the device didn't accept new pwm value for some reason (never seen
491 * this in practice) - it will be reported incorrectly only until next
492 * update. This avoids "fan stuck" messages from pwmconfig, and
493 * fancontrol setting fan speed to 100% during shutdown.
495 spin_lock_bh(&drvdata
->wq
.lock
);
496 drvdata
->fan_duty_percent
[channel
] = duty_percent
;
497 spin_unlock_bh(&drvdata
->wq
.lock
);
500 mutex_unlock(&drvdata
->mutex
);
505 * Workaround for fancontrol/pwmconfig trying to write to pwm*_enable even if it
506 * already is 1 and read-only. Otherwise, fancontrol won't restore pwm on
509 static int set_pwm_enable(struct drvdata
*drvdata
, int channel
, long val
)
514 spin_lock_irq(&drvdata
->wq
.lock
);
516 res
= wait_event_interruptible_locked_irq(drvdata
->wq
,
517 drvdata
->fan_config_received
);
519 spin_unlock_irq(&drvdata
->wq
.lock
);
523 expected_val
= drvdata
->fan_type
[channel
] != FAN_TYPE_NONE
;
525 spin_unlock_irq(&drvdata
->wq
.lock
);
527 return (val
== expected_val
) ? 0 : -EOPNOTSUPP
;
531 * Control byte | Actual update interval in seconds
544 static u8
update_interval_to_control_byte(long interval
)
549 return clamp_val(1 + DIV_ROUND_CLOSEST(interval
- 488, 256), 0, 255);
552 static long control_byte_to_update_interval(u8 control_byte
)
554 if (control_byte
== 0)
557 return 488 + (control_byte
- 1) * 256;
560 static int set_update_interval(struct drvdata
*drvdata
, long val
)
562 u8 control
= update_interval_to_control_byte(val
);
564 OUTPUT_REPORT_ID_INIT_COMMAND
,
565 INIT_COMMAND_SET_UPDATE_INTERVAL
,
575 ret
= send_output_report(drvdata
, report
, sizeof(report
));
579 drvdata
->update_interval
= control_byte_to_update_interval(control
);
583 static int init_device(struct drvdata
*drvdata
, long update_interval
)
586 static const u8 detect_fans_report
[] = {
587 OUTPUT_REPORT_ID_INIT_COMMAND
,
588 INIT_COMMAND_DETECT_FANS
,
591 ret
= send_output_report(drvdata
, detect_fans_report
,
592 sizeof(detect_fans_report
));
596 return set_update_interval(drvdata
, update_interval
);
599 static int nzxt_smart2_hwmon_write(struct device
*dev
,
600 enum hwmon_sensor_types type
, u32 attr
,
601 int channel
, long val
)
603 struct drvdata
*drvdata
= dev_get_drvdata(dev
);
609 case hwmon_pwm_enable
:
610 return set_pwm_enable(drvdata
, channel
, val
);
612 case hwmon_pwm_input
:
613 return set_pwm(drvdata
, channel
, val
);
621 case hwmon_chip_update_interval
:
622 ret
= mutex_lock_interruptible(&drvdata
->mutex
);
626 ret
= set_update_interval(drvdata
, val
);
628 mutex_unlock(&drvdata
->mutex
);
640 static int nzxt_smart2_hwmon_read_string(struct device
*dev
,
641 enum hwmon_sensor_types type
, u32 attr
,
642 int channel
, const char **str
)
646 *str
= fan_label
[channel
];
649 *str
= curr_label
[channel
];
652 *str
= in_label
[channel
];
659 static const struct hwmon_ops nzxt_smart2_hwmon_ops
= {
660 .is_visible
= nzxt_smart2_hwmon_is_visible
,
661 .read
= nzxt_smart2_hwmon_read
,
662 .read_string
= nzxt_smart2_hwmon_read_string
,
663 .write
= nzxt_smart2_hwmon_write
,
666 static const struct hwmon_channel_info
* const nzxt_smart2_channel_info
[] = {
667 HWMON_CHANNEL_INFO(fan
, HWMON_F_INPUT
| HWMON_F_LABEL
,
668 HWMON_F_INPUT
| HWMON_F_LABEL
,
669 HWMON_F_INPUT
| HWMON_F_LABEL
),
670 HWMON_CHANNEL_INFO(pwm
, HWMON_PWM_INPUT
| HWMON_PWM_MODE
| HWMON_PWM_ENABLE
,
671 HWMON_PWM_INPUT
| HWMON_PWM_MODE
| HWMON_PWM_ENABLE
,
672 HWMON_PWM_INPUT
| HWMON_PWM_MODE
| HWMON_PWM_ENABLE
),
673 HWMON_CHANNEL_INFO(in
, HWMON_I_INPUT
| HWMON_I_LABEL
,
674 HWMON_I_INPUT
| HWMON_I_LABEL
,
675 HWMON_I_INPUT
| HWMON_I_LABEL
),
676 HWMON_CHANNEL_INFO(curr
, HWMON_C_INPUT
| HWMON_C_LABEL
,
677 HWMON_C_INPUT
| HWMON_C_LABEL
,
678 HWMON_C_INPUT
| HWMON_C_LABEL
),
679 HWMON_CHANNEL_INFO(chip
, HWMON_C_UPDATE_INTERVAL
),
683 static const struct hwmon_chip_info nzxt_smart2_chip_info
= {
684 .ops
= &nzxt_smart2_hwmon_ops
,
685 .info
= nzxt_smart2_channel_info
,
688 static int nzxt_smart2_hid_raw_event(struct hid_device
*hdev
,
689 struct hid_report
*report
, u8
*data
, int size
)
691 struct drvdata
*drvdata
= hid_get_drvdata(hdev
);
692 u8 report_id
= *data
;
695 case INPUT_REPORT_ID_FAN_CONFIG
:
696 handle_fan_config_report(drvdata
, data
, size
);
699 case INPUT_REPORT_ID_FAN_STATUS
:
700 handle_fan_status_report(drvdata
, data
, size
);
707 static int __maybe_unused
nzxt_smart2_hid_reset_resume(struct hid_device
*hdev
)
709 struct drvdata
*drvdata
= hid_get_drvdata(hdev
);
712 * Userspace is still frozen (so no concurrent sysfs attribute access
713 * is possible), but raw_event can already be called concurrently.
715 spin_lock_bh(&drvdata
->wq
.lock
);
716 drvdata
->fan_config_received
= false;
717 drvdata
->pwm_status_received
= false;
718 drvdata
->voltage_status_received
= false;
719 spin_unlock_bh(&drvdata
->wq
.lock
);
721 return init_device(drvdata
, drvdata
->update_interval
);
724 static void mutex_fini(void *lock
)
729 static int nzxt_smart2_hid_probe(struct hid_device
*hdev
,
730 const struct hid_device_id
*id
)
732 struct drvdata
*drvdata
;
735 drvdata
= devm_kzalloc(&hdev
->dev
, sizeof(struct drvdata
), GFP_KERNEL
);
740 hid_set_drvdata(hdev
, drvdata
);
742 init_waitqueue_head(&drvdata
->wq
);
744 mutex_init(&drvdata
->mutex
);
745 ret
= devm_add_action_or_reset(&hdev
->dev
, mutex_fini
, &drvdata
->mutex
);
749 ret
= hid_parse(hdev
);
753 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
757 ret
= hid_hw_open(hdev
);
761 hid_device_io_start(hdev
);
763 init_device(drvdata
, UPDATE_INTERVAL_DEFAULT_MS
);
766 hwmon_device_register_with_info(&hdev
->dev
, "nzxtsmart2", drvdata
,
767 &nzxt_smart2_chip_info
, NULL
);
768 if (IS_ERR(drvdata
->hwmon
)) {
769 ret
= PTR_ERR(drvdata
->hwmon
);
783 static void nzxt_smart2_hid_remove(struct hid_device
*hdev
)
785 struct drvdata
*drvdata
= hid_get_drvdata(hdev
);
787 hwmon_device_unregister(drvdata
->hwmon
);
793 static const struct hid_device_id nzxt_smart2_hid_id_table
[] = {
794 { HID_USB_DEVICE(0x1e71, 0x2006) }, /* NZXT Smart Device V2 */
795 { HID_USB_DEVICE(0x1e71, 0x200d) }, /* NZXT Smart Device V2 */
796 { HID_USB_DEVICE(0x1e71, 0x200f) }, /* NZXT Smart Device V2 */
797 { HID_USB_DEVICE(0x1e71, 0x2009) }, /* NZXT RGB & Fan Controller */
798 { HID_USB_DEVICE(0x1e71, 0x200e) }, /* NZXT RGB & Fan Controller */
799 { HID_USB_DEVICE(0x1e71, 0x2010) }, /* NZXT RGB & Fan Controller */
800 { HID_USB_DEVICE(0x1e71, 0x2011) }, /* NZXT RGB & Fan Controller (6 RGB) */
801 { HID_USB_DEVICE(0x1e71, 0x2019) }, /* NZXT RGB & Fan Controller (6 RGB) */
802 { HID_USB_DEVICE(0x1e71, 0x2020) }, /* NZXT RGB & Fan Controller (6 RGB) */
806 static struct hid_driver nzxt_smart2_hid_driver
= {
807 .name
= "nzxt-smart2",
808 .id_table
= nzxt_smart2_hid_id_table
,
809 .probe
= nzxt_smart2_hid_probe
,
810 .remove
= nzxt_smart2_hid_remove
,
811 .raw_event
= nzxt_smart2_hid_raw_event
,
813 .reset_resume
= nzxt_smart2_hid_reset_resume
,
817 static int __init
nzxt_smart2_init(void)
819 return hid_register_driver(&nzxt_smart2_hid_driver
);
822 static void __exit
nzxt_smart2_exit(void)
824 hid_unregister_driver(&nzxt_smart2_hid_driver
);
827 MODULE_DEVICE_TABLE(hid
, nzxt_smart2_hid_id_table
);
828 MODULE_AUTHOR("Aleksandr Mezin <mezin.alexander@gmail.com>");
829 MODULE_DESCRIPTION("Driver for NZXT RGB & Fan Controller/Smart Device V2");
830 MODULE_LICENSE("GPL");
833 * With module_init()/module_hid_driver() and the driver built into the kernel:
835 * Driver 'nzxt_smart2' was unable to register with bus_type 'hid' because the
836 * bus was not initialized.
838 late_initcall(nzxt_smart2_init
);
839 module_exit(nzxt_smart2_exit
);