Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Reverse-engineered NZXT RGB & Fan Controller/Smart Device v2 driver.
0004  *
0005  * Copyright (c) 2021 Aleksandr Mezin
0006  */
0007 
0008 #include <linux/hid.h>
0009 #include <linux/hwmon.h>
0010 #include <linux/math.h>
0011 #include <linux/module.h>
0012 #include <linux/mutex.h>
0013 #include <linux/spinlock.h>
0014 #include <linux/wait.h>
0015 
0016 #include <asm/byteorder.h>
0017 #include <asm/unaligned.h>
0018 
0019 /*
0020  * The device has only 3 fan channels/connectors. But all HID reports have
0021  * space reserved for up to 8 channels.
0022  */
0023 #define FAN_CHANNELS 3
0024 #define FAN_CHANNELS_MAX 8
0025 
0026 #define UPDATE_INTERVAL_DEFAULT_MS 1000
0027 
0028 /* These strings match labels on the device exactly */
0029 static const char *const fan_label[] = {
0030     "FAN 1",
0031     "FAN 2",
0032     "FAN 3",
0033 };
0034 
0035 static const char *const curr_label[] = {
0036     "FAN 1 Current",
0037     "FAN 2 Current",
0038     "FAN 3 Current",
0039 };
0040 
0041 static const char *const in_label[] = {
0042     "FAN 1 Voltage",
0043     "FAN 2 Voltage",
0044     "FAN 3 Voltage",
0045 };
0046 
0047 enum {
0048     INPUT_REPORT_ID_FAN_CONFIG = 0x61,
0049     INPUT_REPORT_ID_FAN_STATUS = 0x67,
0050 };
0051 
0052 enum {
0053     FAN_STATUS_REPORT_SPEED = 0x02,
0054     FAN_STATUS_REPORT_VOLTAGE = 0x04,
0055 };
0056 
0057 enum {
0058     FAN_TYPE_NONE = 0,
0059     FAN_TYPE_DC = 1,
0060     FAN_TYPE_PWM = 2,
0061 };
0062 
0063 struct unknown_static_data {
0064     /*
0065      * Some configuration data? Stays the same after fan speed changes,
0066      * changes in fan configuration, reboots and driver reloads.
0067      *
0068      * The same data in multiple report types.
0069      *
0070      * Byte 12 seems to be the number of fan channels, but I am not sure.
0071      */
0072     u8 unknown1[14];
0073 } __packed;
0074 
0075 /*
0076  * The device sends this input report in response to "detect fans" command:
0077  * a 2-byte output report { 0x60, 0x03 }.
0078  */
0079 struct fan_config_report {
0080     /* report_id should be INPUT_REPORT_ID_FAN_CONFIG = 0x61 */
0081     u8 report_id;
0082     /* Always 0x03 */
0083     u8 magic;
0084     struct unknown_static_data unknown_data;
0085     /* Fan type as detected by the device. See FAN_TYPE_* enum. */
0086     u8 fan_type[FAN_CHANNELS_MAX];
0087 } __packed;
0088 
0089 /*
0090  * The device sends these reports at a fixed interval (update interval) -
0091  * one report with type = FAN_STATUS_REPORT_SPEED, and one report with type =
0092  * FAN_STATUS_REPORT_VOLTAGE per update interval.
0093  */
0094 struct fan_status_report {
0095     /* report_id should be INPUT_REPORT_ID_STATUS = 0x67 */
0096     u8 report_id;
0097     /* FAN_STATUS_REPORT_SPEED = 0x02 or FAN_STATUS_REPORT_VOLTAGE = 0x04 */
0098     u8 type;
0099     struct unknown_static_data unknown_data;
0100     /* Fan type as detected by the device. See FAN_TYPE_* enum. */
0101     u8 fan_type[FAN_CHANNELS_MAX];
0102 
0103     union {
0104         /* When type == FAN_STATUS_REPORT_SPEED */
0105         struct {
0106             /*
0107              * Fan speed, in RPM. Zero for channels without fans
0108              * connected.
0109              */
0110             __le16 fan_rpm[FAN_CHANNELS_MAX];
0111             /*
0112              * Fan duty cycle, in percent. Non-zero even for
0113              * channels without fans connected.
0114              */
0115             u8 duty_percent[FAN_CHANNELS_MAX];
0116             /*
0117              * Exactly the same values as duty_percent[], non-zero
0118              * for disconnected fans too.
0119              */
0120             u8 duty_percent_dup[FAN_CHANNELS_MAX];
0121             /* "Case Noise" in db */
0122             u8 noise_db;
0123         } __packed fan_speed;
0124         /* When type == FAN_STATUS_REPORT_VOLTAGE */
0125         struct {
0126             /*
0127              * Voltage, in millivolts. Non-zero even when fan is
0128              * not connected.
0129              */
0130             __le16 fan_in[FAN_CHANNELS_MAX];
0131             /*
0132              * Current, in milliamperes. Near-zero when
0133              * disconnected.
0134              */
0135             __le16 fan_current[FAN_CHANNELS_MAX];
0136         } __packed fan_voltage;
0137     } __packed;
0138 } __packed;
0139 
0140 #define OUTPUT_REPORT_SIZE 64
0141 
0142 enum {
0143     OUTPUT_REPORT_ID_INIT_COMMAND = 0x60,
0144     OUTPUT_REPORT_ID_SET_FAN_SPEED = 0x62,
0145 };
0146 
0147 enum {
0148     INIT_COMMAND_SET_UPDATE_INTERVAL = 0x02,
0149     INIT_COMMAND_DETECT_FANS = 0x03,
0150 };
0151 
0152 /*
0153  * This output report sets pwm duty cycle/target fan speed for one or more
0154  * channels.
0155  */
0156 struct set_fan_speed_report {
0157     /* report_id should be OUTPUT_REPORT_ID_SET_FAN_SPEED = 0x62 */
0158     u8 report_id;
0159     /* Should be 0x01 */
0160     u8 magic;
0161     /* To change fan speed on i-th channel, set i-th bit here */
0162     u8 channel_bit_mask;
0163     /*
0164      * Fan duty cycle/target speed in percent. For voltage-controlled fans,
0165      * the minimal voltage (duty_percent = 1) is about 9V.
0166      * Setting duty_percent to 0 (if the channel is selected in
0167      * channel_bit_mask) turns off the fan completely (regardless of the
0168      * control mode).
0169      */
0170     u8 duty_percent[FAN_CHANNELS_MAX];
0171 } __packed;
0172 
0173 struct drvdata {
0174     struct hid_device *hid;
0175     struct device *hwmon;
0176 
0177     u8 fan_duty_percent[FAN_CHANNELS];
0178     u16 fan_rpm[FAN_CHANNELS];
0179     bool pwm_status_received;
0180 
0181     u16 fan_in[FAN_CHANNELS];
0182     u16 fan_curr[FAN_CHANNELS];
0183     bool voltage_status_received;
0184 
0185     u8 fan_type[FAN_CHANNELS];
0186     bool fan_config_received;
0187 
0188     /*
0189      * wq is used to wait for *_received flags to become true.
0190      * All accesses to *_received flags and fan_* arrays are performed with
0191      * wq.lock held.
0192      */
0193     wait_queue_head_t wq;
0194     /*
0195      * mutex is used to:
0196      * 1) Prevent concurrent conflicting changes to update interval and pwm
0197      * values (after sending an output hid report, the corresponding field
0198      * in drvdata must be updated, and only then new output reports can be
0199      * sent).
0200      * 2) Synchronize access to output_buffer (well, the buffer is here,
0201      * because synchronization is necessary anyway - so why not get rid of
0202      * a kmalloc?).
0203      */
0204     struct mutex mutex;
0205     long update_interval;
0206     u8 output_buffer[OUTPUT_REPORT_SIZE];
0207 };
0208 
0209 static long scale_pwm_value(long val, long orig_max, long new_max)
0210 {
0211     if (val <= 0)
0212         return 0;
0213 
0214     /*
0215      * Positive values should not become zero: 0 completely turns off the
0216      * fan.
0217      */
0218     return max(1L, DIV_ROUND_CLOSEST(min(val, orig_max) * new_max, orig_max));
0219 }
0220 
0221 static void handle_fan_config_report(struct drvdata *drvdata, void *data, int size)
0222 {
0223     struct fan_config_report *report = data;
0224     int i;
0225 
0226     if (size < sizeof(struct fan_config_report))
0227         return;
0228 
0229     if (report->magic != 0x03)
0230         return;
0231 
0232     spin_lock(&drvdata->wq.lock);
0233 
0234     for (i = 0; i < FAN_CHANNELS; i++)
0235         drvdata->fan_type[i] = report->fan_type[i];
0236 
0237     drvdata->fan_config_received = true;
0238     wake_up_all_locked(&drvdata->wq);
0239     spin_unlock(&drvdata->wq.lock);
0240 }
0241 
0242 static void handle_fan_status_report(struct drvdata *drvdata, void *data, int size)
0243 {
0244     struct fan_status_report *report = data;
0245     int i;
0246 
0247     if (size < sizeof(struct fan_status_report))
0248         return;
0249 
0250     spin_lock(&drvdata->wq.lock);
0251 
0252     /*
0253      * The device sends INPUT_REPORT_ID_FAN_CONFIG = 0x61 report in response
0254      * to "detect fans" command. Only accept other data after getting 0x61,
0255      * to make sure that fan detection is complete. In particular, fan
0256      * detection resets pwm values.
0257      */
0258     if (!drvdata->fan_config_received) {
0259         spin_unlock(&drvdata->wq.lock);
0260         return;
0261     }
0262 
0263     for (i = 0; i < FAN_CHANNELS; i++) {
0264         if (drvdata->fan_type[i] == report->fan_type[i])
0265             continue;
0266 
0267         /*
0268          * This should not happen (if my expectations about the device
0269          * are correct).
0270          *
0271          * Even if the userspace sends fan detect command through
0272          * hidraw, fan config report should arrive first.
0273          */
0274         hid_warn_once(drvdata->hid,
0275                   "Fan %d type changed unexpectedly from %d to %d",
0276                   i, drvdata->fan_type[i], report->fan_type[i]);
0277         drvdata->fan_type[i] = report->fan_type[i];
0278     }
0279 
0280     switch (report->type) {
0281     case FAN_STATUS_REPORT_SPEED:
0282         for (i = 0; i < FAN_CHANNELS; i++) {
0283             drvdata->fan_rpm[i] =
0284                 get_unaligned_le16(&report->fan_speed.fan_rpm[i]);
0285             drvdata->fan_duty_percent[i] =
0286                 report->fan_speed.duty_percent[i];
0287         }
0288 
0289         drvdata->pwm_status_received = true;
0290         wake_up_all_locked(&drvdata->wq);
0291         break;
0292 
0293     case FAN_STATUS_REPORT_VOLTAGE:
0294         for (i = 0; i < FAN_CHANNELS; i++) {
0295             drvdata->fan_in[i] =
0296                 get_unaligned_le16(&report->fan_voltage.fan_in[i]);
0297             drvdata->fan_curr[i] =
0298                 get_unaligned_le16(&report->fan_voltage.fan_current[i]);
0299         }
0300 
0301         drvdata->voltage_status_received = true;
0302         wake_up_all_locked(&drvdata->wq);
0303         break;
0304     }
0305 
0306     spin_unlock(&drvdata->wq.lock);
0307 }
0308 
0309 static umode_t nzxt_smart2_hwmon_is_visible(const void *data,
0310                         enum hwmon_sensor_types type,
0311                         u32 attr, int channel)
0312 {
0313     switch (type) {
0314     case hwmon_pwm:
0315         switch (attr) {
0316         case hwmon_pwm_input:
0317         case hwmon_pwm_enable:
0318             return 0644;
0319 
0320         default:
0321             return 0444;
0322         }
0323 
0324     case hwmon_chip:
0325         switch (attr) {
0326         case hwmon_chip_update_interval:
0327             return 0644;
0328 
0329         default:
0330             return 0444;
0331         }
0332 
0333     default:
0334         return 0444;
0335     }
0336 }
0337 
0338 static int nzxt_smart2_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
0339                   u32 attr, int channel, long *val)
0340 {
0341     struct drvdata *drvdata = dev_get_drvdata(dev);
0342     int res = -EINVAL;
0343 
0344     if (type == hwmon_chip) {
0345         switch (attr) {
0346         case hwmon_chip_update_interval:
0347             *val = drvdata->update_interval;
0348             return 0;
0349 
0350         default:
0351             return -EINVAL;
0352         }
0353     }
0354 
0355     spin_lock_irq(&drvdata->wq.lock);
0356 
0357     switch (type) {
0358     case hwmon_pwm:
0359         /*
0360          * fancontrol:
0361          * 1) remembers pwm* values when it starts
0362          * 2) needs pwm*_enable to be 1 on controlled fans
0363          * So make sure we have correct data before allowing pwm* reads.
0364          * Returning errors for pwm of fan speed read can even cause
0365          * fancontrol to shut down. So the wait is unavoidable.
0366          */
0367         switch (attr) {
0368         case hwmon_pwm_enable:
0369             res = wait_event_interruptible_locked_irq(drvdata->wq,
0370                                   drvdata->fan_config_received);
0371             if (res)
0372                 goto unlock;
0373 
0374             *val = drvdata->fan_type[channel] != FAN_TYPE_NONE;
0375             break;
0376 
0377         case hwmon_pwm_mode:
0378             res = wait_event_interruptible_locked_irq(drvdata->wq,
0379                                   drvdata->fan_config_received);
0380             if (res)
0381                 goto unlock;
0382 
0383             *val = drvdata->fan_type[channel] == FAN_TYPE_PWM;
0384             break;
0385 
0386         case hwmon_pwm_input:
0387             res = wait_event_interruptible_locked_irq(drvdata->wq,
0388                                   drvdata->pwm_status_received);
0389             if (res)
0390                 goto unlock;
0391 
0392             *val = scale_pwm_value(drvdata->fan_duty_percent[channel],
0393                            100, 255);
0394             break;
0395         }
0396         break;
0397 
0398     case hwmon_fan:
0399         /*
0400          * It's not strictly necessary to wait for *_received in the
0401          * remaining cases (fancontrol doesn't care about them). But I'm
0402          * doing it to have consistent behavior.
0403          */
0404         if (attr == hwmon_fan_input) {
0405             res = wait_event_interruptible_locked_irq(drvdata->wq,
0406                                   drvdata->pwm_status_received);
0407             if (res)
0408                 goto unlock;
0409 
0410             *val = drvdata->fan_rpm[channel];
0411         }
0412         break;
0413 
0414     case hwmon_in:
0415         if (attr == hwmon_in_input) {
0416             res = wait_event_interruptible_locked_irq(drvdata->wq,
0417                                   drvdata->voltage_status_received);
0418             if (res)
0419                 goto unlock;
0420 
0421             *val = drvdata->fan_in[channel];
0422         }
0423         break;
0424 
0425     case hwmon_curr:
0426         if (attr == hwmon_curr_input) {
0427             res = wait_event_interruptible_locked_irq(drvdata->wq,
0428                                   drvdata->voltage_status_received);
0429             if (res)
0430                 goto unlock;
0431 
0432             *val = drvdata->fan_curr[channel];
0433         }
0434         break;
0435 
0436     default:
0437         break;
0438     }
0439 
0440 unlock:
0441     spin_unlock_irq(&drvdata->wq.lock);
0442     return res;
0443 }
0444 
0445 static int send_output_report(struct drvdata *drvdata, const void *data,
0446                   size_t data_size)
0447 {
0448     int ret;
0449 
0450     if (data_size > sizeof(drvdata->output_buffer))
0451         return -EINVAL;
0452 
0453     memcpy(drvdata->output_buffer, data, data_size);
0454 
0455     if (data_size < sizeof(drvdata->output_buffer))
0456         memset(drvdata->output_buffer + data_size, 0,
0457                sizeof(drvdata->output_buffer) - data_size);
0458 
0459     ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
0460                    sizeof(drvdata->output_buffer));
0461     return ret < 0 ? ret : 0;
0462 }
0463 
0464 static int set_pwm(struct drvdata *drvdata, int channel, long val)
0465 {
0466     int ret;
0467     u8 duty_percent = scale_pwm_value(val, 255, 100);
0468 
0469     struct set_fan_speed_report report = {
0470         .report_id = OUTPUT_REPORT_ID_SET_FAN_SPEED,
0471         .magic = 1,
0472         .channel_bit_mask = 1 << channel
0473     };
0474 
0475     ret = mutex_lock_interruptible(&drvdata->mutex);
0476     if (ret)
0477         return ret;
0478 
0479     report.duty_percent[channel] = duty_percent;
0480     ret = send_output_report(drvdata, &report, sizeof(report));
0481     if (ret)
0482         goto unlock;
0483 
0484     /*
0485      * pwmconfig and fancontrol scripts expect pwm writes to take effect
0486      * immediately (i. e. read from pwm* sysfs should return the value
0487      * written into it). The device seems to always accept pwm values - even
0488      * when there is no fan connected - so update pwm status without waiting
0489      * for a report, to make pwmconfig and fancontrol happy. Worst case -
0490      * if the device didn't accept new pwm value for some reason (never seen
0491      * this in practice) - it will be reported incorrectly only until next
0492      * update. This avoids "fan stuck" messages from pwmconfig, and
0493      * fancontrol setting fan speed to 100% during shutdown.
0494      */
0495     spin_lock_bh(&drvdata->wq.lock);
0496     drvdata->fan_duty_percent[channel] = duty_percent;
0497     spin_unlock_bh(&drvdata->wq.lock);
0498 
0499 unlock:
0500     mutex_unlock(&drvdata->mutex);
0501     return ret;
0502 }
0503 
0504 /*
0505  * Workaround for fancontrol/pwmconfig trying to write to pwm*_enable even if it
0506  * already is 1 and read-only. Otherwise, fancontrol won't restore pwm on
0507  * shutdown properly.
0508  */
0509 static int set_pwm_enable(struct drvdata *drvdata, int channel, long val)
0510 {
0511     long expected_val;
0512     int res;
0513 
0514     spin_lock_irq(&drvdata->wq.lock);
0515 
0516     res = wait_event_interruptible_locked_irq(drvdata->wq,
0517                           drvdata->fan_config_received);
0518     if (res) {
0519         spin_unlock_irq(&drvdata->wq.lock);
0520         return res;
0521     }
0522 
0523     expected_val = drvdata->fan_type[channel] != FAN_TYPE_NONE;
0524 
0525     spin_unlock_irq(&drvdata->wq.lock);
0526 
0527     return (val == expected_val) ? 0 : -EOPNOTSUPP;
0528 }
0529 
0530 /*
0531  * Control byte | Actual update interval in seconds
0532  * 0xff     | 65.5
0533  * 0xf7     | 63.46
0534  * 0x7f     | 32.74
0535  * 0x3f     | 16.36
0536  * 0x1f     | 8.17
0537  * 0x0f     | 4.07
0538  * 0x07     | 2.02
0539  * 0x03     | 1.00
0540  * 0x02     | 0.744
0541  * 0x01     | 0.488
0542  * 0x00     | 0.25
0543  */
0544 static u8 update_interval_to_control_byte(long interval)
0545 {
0546     if (interval <= 250)
0547         return 0;
0548 
0549     return clamp_val(1 + DIV_ROUND_CLOSEST(interval - 488, 256), 0, 255);
0550 }
0551 
0552 static long control_byte_to_update_interval(u8 control_byte)
0553 {
0554     if (control_byte == 0)
0555         return 250;
0556 
0557     return 488 + (control_byte - 1) * 256;
0558 }
0559 
0560 static int set_update_interval(struct drvdata *drvdata, long val)
0561 {
0562     u8 control = update_interval_to_control_byte(val);
0563     u8 report[] = {
0564         OUTPUT_REPORT_ID_INIT_COMMAND,
0565         INIT_COMMAND_SET_UPDATE_INTERVAL,
0566         0x01,
0567         0xe8,
0568         control,
0569         0x01,
0570         0xe8,
0571         control,
0572     };
0573     int ret;
0574 
0575     ret = send_output_report(drvdata, report, sizeof(report));
0576     if (ret)
0577         return ret;
0578 
0579     drvdata->update_interval = control_byte_to_update_interval(control);
0580     return 0;
0581 }
0582 
0583 static int init_device(struct drvdata *drvdata, long update_interval)
0584 {
0585     int ret;
0586     static const u8 detect_fans_report[] = {
0587         OUTPUT_REPORT_ID_INIT_COMMAND,
0588         INIT_COMMAND_DETECT_FANS,
0589     };
0590 
0591     ret = send_output_report(drvdata, detect_fans_report,
0592                  sizeof(detect_fans_report));
0593     if (ret)
0594         return ret;
0595 
0596     return set_update_interval(drvdata, update_interval);
0597 }
0598 
0599 static int nzxt_smart2_hwmon_write(struct device *dev,
0600                    enum hwmon_sensor_types type, u32 attr,
0601                    int channel, long val)
0602 {
0603     struct drvdata *drvdata = dev_get_drvdata(dev);
0604     int ret;
0605 
0606     switch (type) {
0607     case hwmon_pwm:
0608         switch (attr) {
0609         case hwmon_pwm_enable:
0610             return set_pwm_enable(drvdata, channel, val);
0611 
0612         case hwmon_pwm_input:
0613             return set_pwm(drvdata, channel, val);
0614 
0615         default:
0616             return -EINVAL;
0617         }
0618 
0619     case hwmon_chip:
0620         switch (attr) {
0621         case hwmon_chip_update_interval:
0622             ret = mutex_lock_interruptible(&drvdata->mutex);
0623             if (ret)
0624                 return ret;
0625 
0626             ret = set_update_interval(drvdata, val);
0627 
0628             mutex_unlock(&drvdata->mutex);
0629             return ret;
0630 
0631         default:
0632             return -EINVAL;
0633         }
0634 
0635     default:
0636         return -EINVAL;
0637     }
0638 }
0639 
0640 static int nzxt_smart2_hwmon_read_string(struct device *dev,
0641                      enum hwmon_sensor_types type, u32 attr,
0642                      int channel, const char **str)
0643 {
0644     switch (type) {
0645     case hwmon_fan:
0646         *str = fan_label[channel];
0647         return 0;
0648     case hwmon_curr:
0649         *str = curr_label[channel];
0650         return 0;
0651     case hwmon_in:
0652         *str = in_label[channel];
0653         return 0;
0654     default:
0655         return -EINVAL;
0656     }
0657 }
0658 
0659 static const struct hwmon_ops nzxt_smart2_hwmon_ops = {
0660     .is_visible = nzxt_smart2_hwmon_is_visible,
0661     .read = nzxt_smart2_hwmon_read,
0662     .read_string = nzxt_smart2_hwmon_read_string,
0663     .write = nzxt_smart2_hwmon_write,
0664 };
0665 
0666 static const struct hwmon_channel_info *nzxt_smart2_channel_info[] = {
0667     HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL,
0668                HWMON_F_INPUT | HWMON_F_LABEL,
0669                HWMON_F_INPUT | HWMON_F_LABEL),
0670     HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE,
0671                HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE,
0672                HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE),
0673     HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL,
0674                HWMON_I_INPUT | HWMON_I_LABEL,
0675                HWMON_I_INPUT | HWMON_I_LABEL),
0676     HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT | HWMON_C_LABEL,
0677                HWMON_C_INPUT | HWMON_C_LABEL,
0678                HWMON_C_INPUT | HWMON_C_LABEL),
0679     HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
0680     NULL
0681 };
0682 
0683 static const struct hwmon_chip_info nzxt_smart2_chip_info = {
0684     .ops = &nzxt_smart2_hwmon_ops,
0685     .info = nzxt_smart2_channel_info,
0686 };
0687 
0688 static int nzxt_smart2_hid_raw_event(struct hid_device *hdev,
0689                      struct hid_report *report, u8 *data, int size)
0690 {
0691     struct drvdata *drvdata = hid_get_drvdata(hdev);
0692     u8 report_id = *data;
0693 
0694     switch (report_id) {
0695     case INPUT_REPORT_ID_FAN_CONFIG:
0696         handle_fan_config_report(drvdata, data, size);
0697         break;
0698 
0699     case INPUT_REPORT_ID_FAN_STATUS:
0700         handle_fan_status_report(drvdata, data, size);
0701         break;
0702     }
0703 
0704     return 0;
0705 }
0706 
0707 static int __maybe_unused nzxt_smart2_hid_reset_resume(struct hid_device *hdev)
0708 {
0709     struct drvdata *drvdata = hid_get_drvdata(hdev);
0710 
0711     /*
0712      * Userspace is still frozen (so no concurrent sysfs attribute access
0713      * is possible), but raw_event can already be called concurrently.
0714      */
0715     spin_lock_bh(&drvdata->wq.lock);
0716     drvdata->fan_config_received = false;
0717     drvdata->pwm_status_received = false;
0718     drvdata->voltage_status_received = false;
0719     spin_unlock_bh(&drvdata->wq.lock);
0720 
0721     return init_device(drvdata, drvdata->update_interval);
0722 }
0723 
0724 static int nzxt_smart2_hid_probe(struct hid_device *hdev,
0725                  const struct hid_device_id *id)
0726 {
0727     struct drvdata *drvdata;
0728     int ret;
0729 
0730     drvdata = devm_kzalloc(&hdev->dev, sizeof(struct drvdata), GFP_KERNEL);
0731     if (!drvdata)
0732         return -ENOMEM;
0733 
0734     drvdata->hid = hdev;
0735     hid_set_drvdata(hdev, drvdata);
0736 
0737     init_waitqueue_head(&drvdata->wq);
0738 
0739     mutex_init(&drvdata->mutex);
0740     devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
0741             &drvdata->mutex);
0742 
0743     ret = hid_parse(hdev);
0744     if (ret)
0745         return ret;
0746 
0747     ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
0748     if (ret)
0749         return ret;
0750 
0751     ret = hid_hw_open(hdev);
0752     if (ret)
0753         goto out_hw_stop;
0754 
0755     hid_device_io_start(hdev);
0756 
0757     init_device(drvdata, UPDATE_INTERVAL_DEFAULT_MS);
0758 
0759     drvdata->hwmon =
0760         hwmon_device_register_with_info(&hdev->dev, "nzxtsmart2", drvdata,
0761                         &nzxt_smart2_chip_info, NULL);
0762     if (IS_ERR(drvdata->hwmon)) {
0763         ret = PTR_ERR(drvdata->hwmon);
0764         goto out_hw_close;
0765     }
0766 
0767     return 0;
0768 
0769 out_hw_close:
0770     hid_hw_close(hdev);
0771 
0772 out_hw_stop:
0773     hid_hw_stop(hdev);
0774     return ret;
0775 }
0776 
0777 static void nzxt_smart2_hid_remove(struct hid_device *hdev)
0778 {
0779     struct drvdata *drvdata = hid_get_drvdata(hdev);
0780 
0781     hwmon_device_unregister(drvdata->hwmon);
0782 
0783     hid_hw_close(hdev);
0784     hid_hw_stop(hdev);
0785 }
0786 
0787 static const struct hid_device_id nzxt_smart2_hid_id_table[] = {
0788     { HID_USB_DEVICE(0x1e71, 0x2006) }, /* NZXT Smart Device V2 */
0789     { HID_USB_DEVICE(0x1e71, 0x200d) }, /* NZXT Smart Device V2 */
0790     { HID_USB_DEVICE(0x1e71, 0x2009) }, /* NZXT RGB & Fan Controller */
0791     { HID_USB_DEVICE(0x1e71, 0x200e) }, /* NZXT RGB & Fan Controller */
0792     { HID_USB_DEVICE(0x1e71, 0x2010) }, /* NZXT RGB & Fan Controller */
0793     {},
0794 };
0795 
0796 static struct hid_driver nzxt_smart2_hid_driver = {
0797     .name = "nzxt-smart2",
0798     .id_table = nzxt_smart2_hid_id_table,
0799     .probe = nzxt_smart2_hid_probe,
0800     .remove = nzxt_smart2_hid_remove,
0801     .raw_event = nzxt_smart2_hid_raw_event,
0802 #ifdef CONFIG_PM
0803     .reset_resume = nzxt_smart2_hid_reset_resume,
0804 #endif
0805 };
0806 
0807 static int __init nzxt_smart2_init(void)
0808 {
0809     return hid_register_driver(&nzxt_smart2_hid_driver);
0810 }
0811 
0812 static void __exit nzxt_smart2_exit(void)
0813 {
0814     hid_unregister_driver(&nzxt_smart2_hid_driver);
0815 }
0816 
0817 MODULE_DEVICE_TABLE(hid, nzxt_smart2_hid_id_table);
0818 MODULE_AUTHOR("Aleksandr Mezin <mezin.alexander@gmail.com>");
0819 MODULE_DESCRIPTION("Driver for NZXT RGB & Fan Controller/Smart Device V2");
0820 MODULE_LICENSE("GPL");
0821 
0822 /*
0823  * With module_init()/module_hid_driver() and the driver built into the kernel:
0824  *
0825  * Driver 'nzxt_smart2' was unable to register with bus_type 'hid' because the
0826  * bus was not initialized.
0827  */
0828 late_initcall(nzxt_smart2_init);
0829 module_exit(nzxt_smart2_exit);