Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * nzxt-kraken2.c - hwmon driver for NZXT Kraken X42/X52/X62/X72 coolers
0004  *
0005  * The device asynchronously sends HID reports (with id 0x04) twice a second to
0006  * communicate current fan speed, pump speed and coolant temperature.  The
0007  * device does not respond to Get_Report requests for this status report.
0008  *
0009  * Copyright 2019-2021  Jonas Malaco <jonas@protocubo.io>
0010  */
0011 
0012 #include <asm/unaligned.h>
0013 #include <linux/hid.h>
0014 #include <linux/hwmon.h>
0015 #include <linux/jiffies.h>
0016 #include <linux/module.h>
0017 
0018 #define STATUS_REPORT_ID    0x04
0019 #define STATUS_VALIDITY     2 /* seconds; equivalent to 4 missed updates */
0020 
0021 static const char *const kraken2_temp_label[] = {
0022     "Coolant",
0023 };
0024 
0025 static const char *const kraken2_fan_label[] = {
0026     "Fan",
0027     "Pump",
0028 };
0029 
0030 struct kraken2_priv_data {
0031     struct hid_device *hid_dev;
0032     struct device *hwmon_dev;
0033     s32 temp_input[1];
0034     u16 fan_input[2];
0035     unsigned long updated; /* jiffies */
0036 };
0037 
0038 static umode_t kraken2_is_visible(const void *data,
0039                   enum hwmon_sensor_types type,
0040                   u32 attr, int channel)
0041 {
0042     return 0444;
0043 }
0044 
0045 static int kraken2_read(struct device *dev, enum hwmon_sensor_types type,
0046             u32 attr, int channel, long *val)
0047 {
0048     struct kraken2_priv_data *priv = dev_get_drvdata(dev);
0049 
0050     if (time_after(jiffies, priv->updated + STATUS_VALIDITY * HZ))
0051         return -ENODATA;
0052 
0053     switch (type) {
0054     case hwmon_temp:
0055         *val = priv->temp_input[channel];
0056         break;
0057     case hwmon_fan:
0058         *val = priv->fan_input[channel];
0059         break;
0060     default:
0061         return -EOPNOTSUPP; /* unreachable */
0062     }
0063 
0064     return 0;
0065 }
0066 
0067 static int kraken2_read_string(struct device *dev, enum hwmon_sensor_types type,
0068                    u32 attr, int channel, const char **str)
0069 {
0070     switch (type) {
0071     case hwmon_temp:
0072         *str = kraken2_temp_label[channel];
0073         break;
0074     case hwmon_fan:
0075         *str = kraken2_fan_label[channel];
0076         break;
0077     default:
0078         return -EOPNOTSUPP; /* unreachable */
0079     }
0080     return 0;
0081 }
0082 
0083 static const struct hwmon_ops kraken2_hwmon_ops = {
0084     .is_visible = kraken2_is_visible,
0085     .read = kraken2_read,
0086     .read_string = kraken2_read_string,
0087 };
0088 
0089 static const struct hwmon_channel_info *kraken2_info[] = {
0090     HWMON_CHANNEL_INFO(temp,
0091                HWMON_T_INPUT | HWMON_T_LABEL),
0092     HWMON_CHANNEL_INFO(fan,
0093                HWMON_F_INPUT | HWMON_F_LABEL,
0094                HWMON_F_INPUT | HWMON_F_LABEL),
0095     NULL
0096 };
0097 
0098 static const struct hwmon_chip_info kraken2_chip_info = {
0099     .ops = &kraken2_hwmon_ops,
0100     .info = kraken2_info,
0101 };
0102 
0103 static int kraken2_raw_event(struct hid_device *hdev,
0104                  struct hid_report *report, u8 *data, int size)
0105 {
0106     struct kraken2_priv_data *priv;
0107 
0108     if (size < 7 || report->id != STATUS_REPORT_ID)
0109         return 0;
0110 
0111     priv = hid_get_drvdata(hdev);
0112 
0113     /*
0114      * The fractional byte of the coolant temperature has been observed to
0115      * be in the interval [1,9], but some of these steps are also
0116      * consistently skipped for certain integer parts.
0117      *
0118      * For the lack of a better idea, assume that the resolution is 0.1°C,
0119      * and that the missing steps are artifacts of how the firmware
0120      * processes the raw sensor data.
0121      */
0122     priv->temp_input[0] = data[1] * 1000 + data[2] * 100;
0123 
0124     priv->fan_input[0] = get_unaligned_be16(data + 3);
0125     priv->fan_input[1] = get_unaligned_be16(data + 5);
0126 
0127     priv->updated = jiffies;
0128 
0129     return 0;
0130 }
0131 
0132 static int kraken2_probe(struct hid_device *hdev,
0133              const struct hid_device_id *id)
0134 {
0135     struct kraken2_priv_data *priv;
0136     int ret;
0137 
0138     priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
0139     if (!priv)
0140         return -ENOMEM;
0141 
0142     priv->hid_dev = hdev;
0143     hid_set_drvdata(hdev, priv);
0144 
0145     /*
0146      * Initialize ->updated to STATUS_VALIDITY seconds in the past, making
0147      * the initial empty data invalid for kraken2_read without the need for
0148      * a special case there.
0149      */
0150     priv->updated = jiffies - STATUS_VALIDITY * HZ;
0151 
0152     ret = hid_parse(hdev);
0153     if (ret) {
0154         hid_err(hdev, "hid parse failed with %d\n", ret);
0155         return ret;
0156     }
0157 
0158     /*
0159      * Enable hidraw so existing user-space tools can continue to work.
0160      */
0161     ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
0162     if (ret) {
0163         hid_err(hdev, "hid hw start failed with %d\n", ret);
0164         goto fail_and_stop;
0165     }
0166 
0167     ret = hid_hw_open(hdev);
0168     if (ret) {
0169         hid_err(hdev, "hid hw open failed with %d\n", ret);
0170         goto fail_and_close;
0171     }
0172 
0173     priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "kraken2",
0174                               priv, &kraken2_chip_info,
0175                               NULL);
0176     if (IS_ERR(priv->hwmon_dev)) {
0177         ret = PTR_ERR(priv->hwmon_dev);
0178         hid_err(hdev, "hwmon registration failed with %d\n", ret);
0179         goto fail_and_close;
0180     }
0181 
0182     return 0;
0183 
0184 fail_and_close:
0185     hid_hw_close(hdev);
0186 fail_and_stop:
0187     hid_hw_stop(hdev);
0188     return ret;
0189 }
0190 
0191 static void kraken2_remove(struct hid_device *hdev)
0192 {
0193     struct kraken2_priv_data *priv = hid_get_drvdata(hdev);
0194 
0195     hwmon_device_unregister(priv->hwmon_dev);
0196 
0197     hid_hw_close(hdev);
0198     hid_hw_stop(hdev);
0199 }
0200 
0201 static const struct hid_device_id kraken2_table[] = {
0202     { HID_USB_DEVICE(0x1e71, 0x170e) }, /* NZXT Kraken X42/X52/X62/X72 */
0203     { }
0204 };
0205 
0206 MODULE_DEVICE_TABLE(hid, kraken2_table);
0207 
0208 static struct hid_driver kraken2_driver = {
0209     .name = "nzxt-kraken2",
0210     .id_table = kraken2_table,
0211     .probe = kraken2_probe,
0212     .remove = kraken2_remove,
0213     .raw_event = kraken2_raw_event,
0214 };
0215 
0216 static int __init kraken2_init(void)
0217 {
0218     return hid_register_driver(&kraken2_driver);
0219 }
0220 
0221 static void __exit kraken2_exit(void)
0222 {
0223     hid_unregister_driver(&kraken2_driver);
0224 }
0225 
0226 /*
0227  * When compiled into the kernel, initialize after the hid bus.
0228  */
0229 late_initcall(kraken2_init);
0230 module_exit(kraken2_exit);
0231 
0232 MODULE_LICENSE("GPL");
0233 MODULE_AUTHOR("Jonas Malaco <jonas@protocubo.io>");
0234 MODULE_DESCRIPTION("Hwmon driver for NZXT Kraken X42/X52/X62/X72 coolers");