| // SPDX-License-Identifier: GPL-2.0+ | |
| /* | |
| * hwmon driver for Asus ROG Ryujin AIO coolers. | |
| * | |
| * Copyright 2024 Aleksa Savic <savicaleksa83@gmail.com> | |
| */ | |
| #include <linux/debugfs.h> | |
| #include <linux/hid.h> | |
| #include <linux/hwmon.h> | |
| #include <linux/jiffies.h> | |
| #include <linux/module.h> | |
| #include <linux/spinlock.h> | |
| #include <linux/unaligned.h> | |
| #define DRIVER_NAME "asus_rog_ryujin" | |
| #define USB_VENDOR_ID_ASUS_ROG 0x0b05 | |
| #define USB_PRODUCT_ID_RYUJIN_AIO 0x1988 /* ASUS ROG RYUJIN II 360 */ | |
| #define USB_PRODUCT_ID_RYUJIN_III_EXTREME 0x1bcb | |
| #define USB_PRODUCT_ID_RYUJIN_III_EVA 0x1ade | |
| #define USB_PRODUCT_ID_RYUJIN_III_WHITE 0x1ada | |
| struct rog_ryujin_device_info { | |
| u8 temp_offset; | |
| u8 pump_speed_offset; | |
| u8 fan_speed_offset; | |
| u8 duty_channel; | |
| bool has_controller; | |
| }; | |
| static const struct rog_ryujin_device_info rog_ryujin_ii_360_info = { | |
| .temp_offset = 3, | |
| .pump_speed_offset = 5, | |
| .fan_speed_offset = 7, | |
| .duty_channel = 0, | |
| .has_controller = true, | |
| }; | |
| static const struct rog_ryujin_device_info rog_ryujin_iii_info = { | |
| .temp_offset = 5, | |
| .pump_speed_offset = 7, | |
| .fan_speed_offset = 10, | |
| .duty_channel = 1, | |
| .has_controller = false, | |
| }; | |
| #define STATUS_VALIDITY 1500 /* ms */ | |
| #define MAX_REPORT_LENGTH 65 | |
| /* Cooler duty report offsets */ | |
| #define RYUJIN_PUMP_SOURCE 3 | |
| #define RYUJIN_PUMP_DUTY 4 | |
| #define RYUJIN_INTERNAL_FAN_DUTY 5 | |
| /* Controller status (speeds) report offsets */ | |
| #define RYUJIN_CONTROLLER_SPEED_1 5 | |
| #define RYUJIN_CONTROLLER_SPEED_2 7 | |
| #define RYUJIN_CONTROLLER_SPEED_3 9 | |
| #define RYUJIN_CONTROLLER_SPEED_4 3 | |
| /* Controller duty report offsets */ | |
| #define RYUJIN_CONTROLLER_DUTY 4 | |
| /* Control commands and their inner offsets */ | |
| #define RYUJIN_CMD_PREFIX 0xEC | |
| static const u8 get_cooler_status_cmd[] = { RYUJIN_CMD_PREFIX, 0x99 }; | |
| static const u8 get_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x9A }; | |
| static const u8 get_controller_speed_cmd[] = { RYUJIN_CMD_PREFIX, 0xA0 }; | |
| static const u8 get_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0xA1 }; | |
| #define RYUJIN_SET_COOLER_CHANNEL_OFFSET 2 | |
| #define RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET 3 | |
| #define RYUJIN_SET_COOLER_FAN_DUTY_OFFSET 4 | |
| static const u8 set_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x1A, 0x00, 0x00, 0x00 }; | |
| /* Pump control sources in the set cooler duty command and the cooler duty report */ | |
| #define RYUJIN_SOURCE_HEADER 0 | |
| #define RYUJIN_SOURCE_USB 1 | |
| #define RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET 4 | |
| static const u8 set_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x21, 0x00, 0x00, 0x00 }; | |
| /* Command lengths */ | |
| #define GET_CMD_LENGTH 2 /* Same length for all get commands */ | |
| #define SET_CMD_LENGTH 5 /* Same length for all set commands */ | |
| /* Command response headers */ | |
| #define RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE 0x19 | |
| #define RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE 0x1A | |
| #define RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE 0x20 | |
| #define RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE 0x21 | |
| static const char *const rog_ryujin_temp_label[] = { | |
| "Coolant temp" | |
| }; | |
| static const char *const rog_ryujin_speed_label[] = { | |
| "Pump speed", | |
| "Internal fan speed", | |
| "Controller fan 1 speed", | |
| "Controller fan 2 speed", | |
| "Controller fan 3 speed", | |
| "Controller fan 4 speed", | |
| }; | |
| struct rog_ryujin_data { | |
| struct hid_device *hdev; | |
| struct device *hwmon_dev; | |
| const struct rog_ryujin_device_info *info; | |
| /* For reinitializing the completions below */ | |
| spinlock_t status_report_request_lock; | |
| struct completion cooler_status_received; | |
| struct completion controller_status_received; | |
| struct completion cooler_duty_received; | |
| struct completion controller_duty_received; | |
| struct completion cooler_duty_set; | |
| struct completion controller_duty_set; | |
| /* Sensor data */ | |
| s32 temp_input[1]; | |
| u16 speed_input[6]; /* Pump, internal fan and four controller fan speeds in RPM */ | |
| u8 duty_input[3]; /* Pump, internal fan and controller fan duty in PWM */ | |
| u8 pump_source; /* Pump control source from the cooler duty report */ | |
| u8 *buffer; | |
| unsigned long updated; /* jiffies */ | |
| }; | |
| static int rog_ryujin_percent_to_pwm(u16 val) | |
| { | |
| return DIV_ROUND_CLOSEST(val * 255, 100); | |
| } | |
| static int rog_ryujin_pwm_to_percent(long val) | |
| { | |
| return DIV_ROUND_CLOSEST(val * 100, 255); | |
| } | |
| static umode_t rog_ryujin_is_visible(const void *data, | |
| enum hwmon_sensor_types type, u32 attr, int channel) | |
| { | |
| const struct rog_ryujin_data *priv = data; | |
| switch (type) { | |
| case hwmon_temp: | |
| switch (attr) { | |
| case hwmon_temp_label: | |
| case hwmon_temp_input: | |
| return 0444; | |
| default: | |
| break; | |
| } | |
| break; | |
| case hwmon_fan: | |
| if (channel >= 2 && !priv->info->has_controller) | |
| return 0; | |
| switch (attr) { | |
| case hwmon_fan_label: | |
| case hwmon_fan_input: | |
| return 0444; | |
| default: | |
| break; | |
| } | |
| break; | |
| case hwmon_pwm: | |
| if (channel >= 2 && !priv->info->has_controller) | |
| return 0; | |
| switch (attr) { | |
| case hwmon_pwm_input: | |
| return 0644; | |
| default: | |
| break; | |
| } | |
| break; | |
| default: | |
| break; | |
| } | |
| return 0; | |
| } | |
| /* Writes the command to the device with the rest of the report filled with zeroes */ | |
| static int rog_ryujin_write_expanded(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length) | |
| { | |
| memcpy_and_pad(priv->buffer, MAX_REPORT_LENGTH, cmd, cmd_length, 0x00); | |
| return hid_hw_output_report(priv->hdev, priv->buffer, MAX_REPORT_LENGTH); | |
| } | |
| static int rog_ryujin_execute_cmd(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length, | |
| struct completion *status_completion) | |
| { | |
| unsigned long flags; | |
| int ret; | |
| /* | |
| * Disable raw event parsing for a moment to safely reinitialize the | |
| * completion. Reinit is done because hidraw could have triggered | |
| * the raw event parsing and marked the passed in completion as done. | |
| */ | |
| spin_lock_irqsave(&priv->status_report_request_lock, flags); | |
| reinit_completion(status_completion); | |
| spin_unlock_irqrestore(&priv->status_report_request_lock, flags); | |
| /* Send command for getting data */ | |
| ret = rog_ryujin_write_expanded(priv, cmd, cmd_length); | |
| if (ret < 0) | |
| return ret; | |
| ret = wait_for_completion_interruptible_timeout(status_completion, | |
| msecs_to_jiffies(STATUS_VALIDITY)); | |
| if (ret == 0) | |
| return -ETIMEDOUT; | |
| else if (ret < 0) | |
| return ret; | |
| return 0; | |
| } | |
| static int rog_ryujin_get_status(struct rog_ryujin_data *priv) | |
| { | |
| int ret; | |
| if (!time_after(jiffies, priv->updated + msecs_to_jiffies(STATUS_VALIDITY))) { | |
| /* Data is up to date */ | |
| return 0; | |
| } | |
| /* Retrieve cooler status */ | |
| ret = | |
| rog_ryujin_execute_cmd(priv, get_cooler_status_cmd, GET_CMD_LENGTH, | |
| &priv->cooler_status_received); | |
| if (ret < 0) | |
| return ret; | |
| if (priv->info->has_controller) { | |
| /* Retrieve controller status (speeds) */ | |
| ret = rog_ryujin_execute_cmd(priv, get_controller_speed_cmd, | |
| GET_CMD_LENGTH, | |
| &priv->controller_status_received); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| /* Retrieve cooler duty */ | |
| ret = | |
| rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH, | |
| &priv->cooler_duty_received); | |
| if (ret < 0) | |
| return ret; | |
| if (priv->info->has_controller) { | |
| /* Retrieve controller duty */ | |
| ret = rog_ryujin_execute_cmd(priv, get_controller_duty_cmd, | |
| GET_CMD_LENGTH, | |
| &priv->controller_duty_received); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| priv->updated = jiffies; | |
| return 0; | |
| } | |
| static int rog_ryujin_read(struct device *dev, enum hwmon_sensor_types type, | |
| u32 attr, int channel, long *val) | |
| { | |
| struct rog_ryujin_data *priv = dev_get_drvdata(dev); | |
| int ret = rog_ryujin_get_status(priv); | |
| if (ret < 0) | |
| return ret; | |
| switch (type) { | |
| case hwmon_temp: | |
| *val = priv->temp_input[channel]; | |
| break; | |
| case hwmon_fan: | |
| *val = priv->speed_input[channel]; | |
| break; | |
| case hwmon_pwm: | |
| switch (attr) { | |
| case hwmon_pwm_input: | |
| *val = priv->duty_input[channel]; | |
| break; | |
| default: | |
| return -EOPNOTSUPP; | |
| } | |
| break; | |
| default: | |
| return -EOPNOTSUPP; /* unreachable */ | |
| } | |
| return 0; | |
| } | |
| static int rog_ryujin_read_string(struct device *dev, enum hwmon_sensor_types type, | |
| u32 attr, int channel, const char **str) | |
| { | |
| switch (type) { | |
| case hwmon_temp: | |
| *str = rog_ryujin_temp_label[channel]; | |
| break; | |
| case hwmon_fan: | |
| *str = rog_ryujin_speed_label[channel]; | |
| break; | |
| default: | |
| return -EOPNOTSUPP; /* unreachable */ | |
| } | |
| return 0; | |
| } | |
| /* | |
| * On devices with a non-zero duty_channel (the RYUJIN III), the channel byte of | |
| * the set cooler duty command selects the pump control source. | |
| * RYUJIN_SOURCE_HEADER hands the pump to its internal controller, which follows | |
| * the PWM of the header its 4-pin cable is plugged into. RYUJIN_SOURCE_USB | |
| * makes the pump follow the pump duty in the command. The cooler duty report | |
| * returns the current source. | |
| * | |
| * A fan duty write sends the reported source back, so that it leaves the pump | |
| * control source as it is. A pump duty write selects USB. The cooler does not | |
| * pass on a pump duty equal to the one it reports, and after power-on it | |
| * reports 40% while a pump handed over at that duty runs at full speed. So | |
| * when a pump duty write asks for the reported duty, write a duty 1% away | |
| * first. | |
| */ | |
| static int rog_ryujin_select_pump_source(struct rog_ryujin_data *priv, u8 *set_cmd, | |
| int channel) | |
| { | |
| u8 duty = set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET]; | |
| int ret; | |
| if (channel == 1) { | |
| set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = priv->pump_source; | |
| return 0; | |
| } | |
| set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = RYUJIN_SOURCE_USB; | |
| if (duty != rog_ryujin_pwm_to_percent(priv->duty_input[0])) | |
| return 0; | |
| set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = duty < 100 ? duty + 1 : duty - 1; | |
| ret = rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, &priv->cooler_duty_set); | |
| set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = duty; | |
| return ret; | |
| } | |
| static int rog_ryujin_write_fixed_duty(struct rog_ryujin_data *priv, int channel, int val) | |
| { | |
| u8 set_cmd[SET_CMD_LENGTH]; | |
| int ret; | |
| if (channel < 2) { | |
| /* | |
| * Retrieve cooler duty since both pump and internal fan are set | |
| * together, then write back with one of them modified. | |
| */ | |
| ret = | |
| rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH, | |
| &priv->cooler_duty_received); | |
| if (ret < 0) | |
| return ret; | |
| memcpy(set_cmd, set_cooler_duty_cmd, SET_CMD_LENGTH); | |
| set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = priv->info->duty_channel; | |
| /* Cooler duties are set as 0-100% */ | |
| val = rog_ryujin_pwm_to_percent(val); | |
| if (channel == 0) { | |
| /* Cooler pump duty */ | |
| set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = val; | |
| set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] = | |
| rog_ryujin_pwm_to_percent(priv->duty_input[1]); | |
| } else if (channel == 1) { | |
| /* Cooler internal fan duty */ | |
| set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = | |
| rog_ryujin_pwm_to_percent(priv->duty_input[0]); | |
| set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] = val; | |
| } | |
| if (priv->info->duty_channel) { | |
| ret = rog_ryujin_select_pump_source(priv, set_cmd, channel); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| return rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, &priv->cooler_duty_set); | |
| } else { | |
| /* | |
| * Controller fan duty (channel == 2). No need to retrieve current | |
| * duty, so just send the command. | |
| */ | |
| memcpy(set_cmd, set_controller_duty_cmd, SET_CMD_LENGTH); | |
| set_cmd[RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET] = val; | |
| ret = | |
| rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, | |
| &priv->controller_duty_set); | |
| if (ret < 0) | |
| return ret; | |
| } | |
| /* Lock onto this value until next refresh cycle */ | |
| priv->duty_input[channel] = val; | |
| return 0; | |
| } | |
| static int rog_ryujin_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, | |
| long val) | |
| { | |
| struct rog_ryujin_data *priv = dev_get_drvdata(dev); | |
| int ret; | |
| switch (type) { | |
| case hwmon_pwm: | |
| switch (attr) { | |
| case hwmon_pwm_input: | |
| if (val < 0 || val > 255) | |
| return -EINVAL; | |
| ret = rog_ryujin_write_fixed_duty(priv, channel, val); | |
| if (ret < 0) | |
| return ret; | |
| break; | |
| default: | |
| return -EOPNOTSUPP; | |
| } | |
| break; | |
| default: | |
| return -EOPNOTSUPP; | |
| } | |
| return 0; | |
| } | |
| static const struct hwmon_ops rog_ryujin_hwmon_ops = { | |
| .is_visible = rog_ryujin_is_visible, | |
| .read = rog_ryujin_read, | |
| .read_string = rog_ryujin_read_string, | |
| .write = rog_ryujin_write | |
| }; | |
| static const struct hwmon_channel_info *rog_ryujin_info[] = { | |
| HWMON_CHANNEL_INFO(temp, | |
| HWMON_T_INPUT | HWMON_T_LABEL), | |
| HWMON_CHANNEL_INFO(fan, | |
| HWMON_F_INPUT | HWMON_F_LABEL, | |
| HWMON_F_INPUT | HWMON_F_LABEL, | |
| HWMON_F_INPUT | HWMON_F_LABEL, | |
| HWMON_F_INPUT | HWMON_F_LABEL, | |
| HWMON_F_INPUT | HWMON_F_LABEL, | |
| HWMON_F_INPUT | HWMON_F_LABEL), | |
| HWMON_CHANNEL_INFO(pwm, | |
| HWMON_PWM_INPUT, | |
| HWMON_PWM_INPUT, | |
| HWMON_PWM_INPUT), | |
| NULL | |
| }; | |
| static const struct hwmon_chip_info rog_ryujin_chip_info = { | |
| .ops = &rog_ryujin_hwmon_ops, | |
| .info = rog_ryujin_info, | |
| }; | |
| static int rog_ryujin_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, | |
| int size) | |
| { | |
| struct rog_ryujin_data *priv = hid_get_drvdata(hdev); | |
| unsigned long flags; | |
| if (size < 2 || data[0] != RYUJIN_CMD_PREFIX) | |
| return 0; | |
| spin_lock_irqsave(&priv->status_report_request_lock, flags); | |
| if (data[1] == RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE) { | |
| if (size <= priv->info->temp_offset + 1 || | |
| size <= priv->info->pump_speed_offset + 1 || | |
| size <= priv->info->fan_speed_offset + 1) | |
| goto unlock; | |
| /* Received coolant temp and speeds of pump and internal fan */ | |
| priv->temp_input[0] = data[priv->info->temp_offset] * 1000 + | |
| data[priv->info->temp_offset + 1] * 100; | |
| priv->speed_input[0] = | |
| get_unaligned_le16(data + priv->info->pump_speed_offset); | |
| priv->speed_input[1] = | |
| get_unaligned_le16(data + priv->info->fan_speed_offset); | |
| if (!completion_done(&priv->cooler_status_received)) | |
| complete_all(&priv->cooler_status_received); | |
| } else if (data[1] == RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE) { | |
| if (size <= RYUJIN_CONTROLLER_SPEED_3 + 1) | |
| goto unlock; | |
| /* Received speeds of four fans attached to the controller */ | |
| priv->speed_input[2] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_1); | |
| priv->speed_input[3] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_2); | |
| priv->speed_input[4] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_3); | |
| priv->speed_input[5] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_4); | |
| if (!completion_done(&priv->controller_status_received)) | |
| complete_all(&priv->controller_status_received); | |
| } else if (data[1] == RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE) { | |
| if (size <= RYUJIN_INTERNAL_FAN_DUTY) | |
| goto unlock; | |
| /* Received report for pump and internal fan duties (in %) */ | |
| if (data[RYUJIN_PUMP_DUTY] == 0 && data[RYUJIN_INTERNAL_FAN_DUTY] == 0) { | |
| /* | |
| * We received a report with zeroes for duty in both places. | |
| * The device returns this as a confirmation that setting values | |
| * is successful. If we initiated a write, mark it as complete. | |
| */ | |
| if (!completion_done(&priv->cooler_duty_set)) | |
| complete_all(&priv->cooler_duty_set); | |
| else if (!completion_done(&priv->cooler_duty_received)) | |
| /* | |
| * We didn't initiate a write, but received both zeroes. | |
| * This means that either both duties are actually zero, | |
| * or that we received a success report caused by userspace. | |
| * We're expecting a report, so parse it. | |
| */ | |
| goto read_cooler_duty; | |
| goto unlock; | |
| } | |
| read_cooler_duty: | |
| priv->pump_source = data[RYUJIN_PUMP_SOURCE]; | |
| priv->duty_input[0] = rog_ryujin_percent_to_pwm(data[RYUJIN_PUMP_DUTY]); | |
| priv->duty_input[1] = rog_ryujin_percent_to_pwm(data[RYUJIN_INTERNAL_FAN_DUTY]); | |
| if (!completion_done(&priv->cooler_duty_received)) | |
| complete_all(&priv->cooler_duty_received); | |
| } else if (data[1] == RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE) { | |
| if (size <= RYUJIN_CONTROLLER_DUTY) | |
| goto unlock; | |
| /* Received report for controller duty for fans (in PWM) */ | |
| if (data[RYUJIN_CONTROLLER_DUTY] == 0) { | |
| /* | |
| * We received a report with a zero for duty. The device returns this as | |
| * a confirmation that setting the controller duty value was successful. | |
| * If we initiated a write, mark it as complete. | |
| */ | |
| if (!completion_done(&priv->controller_duty_set)) | |
| complete_all(&priv->controller_duty_set); | |
| else if (!completion_done(&priv->controller_duty_received)) | |
| /* | |
| * We didn't initiate a write, but received a zero for duty. | |
| * This means that either the duty is actually zero, or that | |
| * we received a success report caused by userspace. | |
| * We're expecting a report, so parse it. | |
| */ | |
| goto read_controller_duty; | |
| goto unlock; | |
| } | |
| read_controller_duty: | |
| priv->duty_input[2] = data[RYUJIN_CONTROLLER_DUTY]; | |
| if (!completion_done(&priv->controller_duty_received)) | |
| complete_all(&priv->controller_duty_received); | |
| } | |
| unlock: | |
| spin_unlock_irqrestore(&priv->status_report_request_lock, flags); | |
| return 0; | |
| } | |
| static int rog_ryujin_probe(struct hid_device *hdev, const struct hid_device_id *id) | |
| { | |
| struct rog_ryujin_data *priv; | |
| int ret; | |
| if (!id->driver_data) | |
| return -EINVAL; | |
| priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL); | |
| if (!priv) | |
| return -ENOMEM; | |
| priv->hdev = hdev; | |
| priv->info = (const struct rog_ryujin_device_info *)id->driver_data; | |
| hid_set_drvdata(hdev, priv); | |
| /* | |
| * Initialize priv->updated to STATUS_VALIDITY seconds in the past, making | |
| * the initial empty data invalid for rog_ryujin_read() without the need for | |
| * a special case there. | |
| */ | |
| priv->updated = jiffies - msecs_to_jiffies(STATUS_VALIDITY); | |
| ret = hid_parse(hdev); | |
| if (ret) { | |
| hid_err(hdev, "hid parse failed with %d\n", ret); | |
| return ret; | |
| } | |
| /* Enable hidraw so existing user-space tools can continue to work */ | |
| ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); | |
| if (ret) { | |
| hid_err(hdev, "hid hw start failed with %d\n", ret); | |
| return ret; | |
| } | |
| ret = hid_hw_open(hdev); | |
| if (ret) { | |
| hid_err(hdev, "hid hw open failed with %d\n", ret); | |
| goto fail_and_stop; | |
| } | |
| priv->buffer = devm_kzalloc(&hdev->dev, MAX_REPORT_LENGTH, GFP_KERNEL); | |
| if (!priv->buffer) { | |
| ret = -ENOMEM; | |
| goto fail_and_close; | |
| } | |
| spin_lock_init(&priv->status_report_request_lock); | |
| init_completion(&priv->cooler_status_received); | |
| init_completion(&priv->controller_status_received); | |
| init_completion(&priv->cooler_duty_received); | |
| init_completion(&priv->controller_duty_received); | |
| init_completion(&priv->cooler_duty_set); | |
| init_completion(&priv->controller_duty_set); | |
| priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "rog_ryujin", | |
| priv, &rog_ryujin_chip_info, NULL); | |
| if (IS_ERR(priv->hwmon_dev)) { | |
| ret = PTR_ERR(priv->hwmon_dev); | |
| hid_err(hdev, "hwmon registration failed with %d\n", ret); | |
| goto fail_and_close; | |
| } | |
| return 0; | |
| fail_and_close: | |
| hid_hw_close(hdev); | |
| fail_and_stop: | |
| hid_hw_stop(hdev); | |
| return ret; | |
| } | |
| static void rog_ryujin_remove(struct hid_device *hdev) | |
| { | |
| struct rog_ryujin_data *priv = hid_get_drvdata(hdev); | |
| hwmon_device_unregister(priv->hwmon_dev); | |
| hid_hw_close(hdev); | |
| hid_hw_stop(hdev); | |
| } | |
| static const struct hid_device_id rog_ryujin_table[] = { | |
| { HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_AIO), | |
| .driver_data = (kernel_ulong_t)&rog_ryujin_ii_360_info }, | |
| { HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_EXTREME), | |
| .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info }, | |
| { HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_EVA), | |
| .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info }, | |
| { HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_WHITE), | |
| .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info }, | |
| { } | |
| }; | |
| MODULE_DEVICE_TABLE(hid, rog_ryujin_table); | |
| static struct hid_driver rog_ryujin_driver = { | |
| .name = "rog_ryujin", | |
| .id_table = rog_ryujin_table, | |
| .probe = rog_ryujin_probe, | |
| .remove = rog_ryujin_remove, | |
| .raw_event = rog_ryujin_raw_event, | |
| }; | |
| static int __init rog_ryujin_init(void) | |
| { | |
| return hid_register_driver(&rog_ryujin_driver); | |
| } | |
| static void __exit rog_ryujin_exit(void) | |
| { | |
| hid_unregister_driver(&rog_ryujin_driver); | |
| } | |
| /* When compiled into the kernel, initialize after the HID bus */ | |
| late_initcall(rog_ryujin_init); | |
| module_exit(rog_ryujin_exit); | |
| MODULE_LICENSE("GPL"); | |
| MODULE_AUTHOR("Aleksa Savic <savicaleksa83@gmail.com>"); | |
| MODULE_DESCRIPTION("Hwmon driver for Asus ROG Ryujin AIO coolers"); |