xref: /linux/drivers/hwmon/asus_rog_ryujin.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * hwmon driver for Asus ROG Ryujin AIO coolers.
4  *
5  * Copyright 2024 Aleksa Savic <savicaleksa83@gmail.com>
6  */
7 
8 #include <linux/debugfs.h>
9 #include <linux/hid.h>
10 #include <linux/hwmon.h>
11 #include <linux/jiffies.h>
12 #include <linux/module.h>
13 #include <linux/spinlock.h>
14 #include <linux/unaligned.h>
15 
16 #define DRIVER_NAME	"asus_rog_ryujin"
17 
18 #define USB_VENDOR_ID_ASUS_ROG		0x0b05
19 #define USB_PRODUCT_ID_RYUJIN_AIO	0x1988	/* ASUS ROG RYUJIN II 360 */
20 #define USB_PRODUCT_ID_RYUJIN_III_EXTREME	0x1bcb
21 #define USB_PRODUCT_ID_RYUJIN_III_EVA		0x1ade
22 #define USB_PRODUCT_ID_RYUJIN_III_WHITE		0x1ada
23 #define USB_PRODUCT_ID_RYUJIN_III		0x1aa2
24 
25 struct rog_ryujin_device_info {
26 	u8 temp_offset;
27 	u8 pump_speed_offset;
28 	u8 fan_speed_offset;
29 	u8 duty_channel;
30 	bool has_controller;
31 };
32 
33 static const struct rog_ryujin_device_info rog_ryujin_ii_360_info = {
34 	.temp_offset = 3,
35 	.pump_speed_offset = 5,
36 	.fan_speed_offset = 7,
37 	.duty_channel = 0,
38 	.has_controller = true,
39 };
40 
41 static const struct rog_ryujin_device_info rog_ryujin_iii_info = {
42 	.temp_offset = 5,
43 	.pump_speed_offset = 7,
44 	.fan_speed_offset = 10,
45 	.duty_channel = 1,
46 	.has_controller = false,
47 };
48 
49 #define STATUS_VALIDITY		1500	/* ms */
50 #define MAX_REPORT_LENGTH	65
51 
52 /* Cooler duty report offsets */
53 #define RYUJIN_PUMP_SOURCE		3
54 #define RYUJIN_PUMP_DUTY		4
55 #define RYUJIN_INTERNAL_FAN_DUTY	5
56 
57 /* Controller status (speeds) report offsets */
58 #define RYUJIN_CONTROLLER_SPEED_1	5
59 #define RYUJIN_CONTROLLER_SPEED_2	7
60 #define RYUJIN_CONTROLLER_SPEED_3	9
61 #define RYUJIN_CONTROLLER_SPEED_4	3
62 
63 /* Controller duty report offsets */
64 #define RYUJIN_CONTROLLER_DUTY		4
65 
66 /* Control commands and their inner offsets */
67 #define RYUJIN_CMD_PREFIX	0xEC
68 
69 static const u8 get_cooler_status_cmd[] = { RYUJIN_CMD_PREFIX, 0x99 };
70 static const u8 get_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x9A };
71 static const u8 get_controller_speed_cmd[] = { RYUJIN_CMD_PREFIX, 0xA0 };
72 static const u8 get_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0xA1 };
73 
74 #define RYUJIN_SET_COOLER_CHANNEL_OFFSET	2
75 #define RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET	3
76 #define RYUJIN_SET_COOLER_FAN_DUTY_OFFSET	4
77 static const u8 set_cooler_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x1A, 0x00, 0x00, 0x00 };
78 
79 /* Pump control sources in the set cooler duty command and the cooler duty report */
80 #define RYUJIN_SOURCE_HEADER	0
81 #define RYUJIN_SOURCE_USB	1
82 
83 #define RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET	4
84 static const u8 set_controller_duty_cmd[] = { RYUJIN_CMD_PREFIX, 0x21, 0x00, 0x00, 0x00 };
85 
86 /* Command lengths */
87 #define GET_CMD_LENGTH	2	/* Same length for all get commands */
88 #define SET_CMD_LENGTH	5	/* Same length for all set commands */
89 
90 /* Command response headers */
91 #define RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE		0x19
92 #define RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE		0x1A
93 #define RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE	0x20
94 #define RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE		0x21
95 
96 static const char *const rog_ryujin_temp_label[] = {
97 	"Coolant temp"
98 };
99 
100 static const char *const rog_ryujin_speed_label[] = {
101 	"Pump speed",
102 	"Internal fan speed",
103 	"Controller fan 1 speed",
104 	"Controller fan 2 speed",
105 	"Controller fan 3 speed",
106 	"Controller fan 4 speed",
107 };
108 
109 struct rog_ryujin_data {
110 	struct hid_device *hdev;
111 	struct device *hwmon_dev;
112 	const struct rog_ryujin_device_info *info;
113 	/* For reinitializing the completions below */
114 	spinlock_t status_report_request_lock;
115 	struct completion cooler_status_received;
116 	struct completion controller_status_received;
117 	struct completion cooler_duty_received;
118 	struct completion controller_duty_received;
119 	struct completion cooler_duty_set;
120 	struct completion controller_duty_set;
121 
122 	/* Sensor data */
123 	s32 temp_input[1];
124 	u16 speed_input[6];	/* Pump, internal fan and four controller fan speeds in RPM */
125 	u8 duty_input[3];	/* Pump, internal fan and controller fan duty in PWM */
126 	u8 pump_source;		/* Pump control source from the cooler duty report */
127 
128 	u8 *buffer;
129 	unsigned long updated;	/* jiffies */
130 };
131 
132 static int rog_ryujin_percent_to_pwm(u16 val)
133 {
134 	return DIV_ROUND_CLOSEST(val * 255, 100);
135 }
136 
137 static int rog_ryujin_pwm_to_percent(long val)
138 {
139 	return DIV_ROUND_CLOSEST(val * 100, 255);
140 }
141 
142 static umode_t rog_ryujin_is_visible(const void *data,
143 				     enum hwmon_sensor_types type, u32 attr, int channel)
144 {
145 	const struct rog_ryujin_data *priv = data;
146 
147 	switch (type) {
148 	case hwmon_temp:
149 		switch (attr) {
150 		case hwmon_temp_label:
151 		case hwmon_temp_input:
152 			return 0444;
153 		default:
154 			break;
155 		}
156 		break;
157 	case hwmon_fan:
158 		if (channel >= 2 && !priv->info->has_controller)
159 			return 0;
160 		switch (attr) {
161 		case hwmon_fan_label:
162 		case hwmon_fan_input:
163 			return 0444;
164 		default:
165 			break;
166 		}
167 		break;
168 	case hwmon_pwm:
169 		if (channel >= 2 && !priv->info->has_controller)
170 			return 0;
171 		switch (attr) {
172 		case hwmon_pwm_input:
173 			return 0644;
174 		default:
175 			break;
176 		}
177 		break;
178 	default:
179 		break;
180 	}
181 
182 	return 0;
183 }
184 
185 /* Writes the command to the device with the rest of the report filled with zeroes */
186 static int rog_ryujin_write_expanded(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length)
187 {
188 	memcpy_and_pad(priv->buffer, MAX_REPORT_LENGTH, cmd, cmd_length, 0x00);
189 	return hid_hw_output_report(priv->hdev, priv->buffer, MAX_REPORT_LENGTH);
190 }
191 
192 static int rog_ryujin_execute_cmd(struct rog_ryujin_data *priv, const u8 *cmd, int cmd_length,
193 				  struct completion *status_completion)
194 {
195 	unsigned long flags;
196 	int ret;
197 
198 	/*
199 	 * Disable raw event parsing for a moment to safely reinitialize the
200 	 * completion. Reinit is done because hidraw could have triggered
201 	 * the raw event parsing and marked the passed in completion as done.
202 	 */
203 	spin_lock_irqsave(&priv->status_report_request_lock, flags);
204 	reinit_completion(status_completion);
205 	spin_unlock_irqrestore(&priv->status_report_request_lock, flags);
206 
207 	/* Send command for getting data */
208 	ret = rog_ryujin_write_expanded(priv, cmd, cmd_length);
209 	if (ret < 0)
210 		goto out_complete;
211 
212 	ret = wait_for_completion_interruptible_timeout(status_completion,
213 							msecs_to_jiffies(STATUS_VALIDITY));
214 	if (ret == 0)
215 		ret = -ETIMEDOUT;
216 	if (ret < 0)
217 		goto out_complete;
218 
219 	return 0;
220 
221 out_complete:
222 	spin_lock_irqsave(&priv->status_report_request_lock, flags);
223 	if (!completion_done(status_completion))
224 		complete_all(status_completion);
225 	spin_unlock_irqrestore(&priv->status_report_request_lock, flags);
226 	return ret;
227 }
228 
229 static int rog_ryujin_get_status(struct rog_ryujin_data *priv)
230 {
231 	int ret;
232 
233 	if (!time_after(jiffies, priv->updated + msecs_to_jiffies(STATUS_VALIDITY))) {
234 		/* Data is up to date */
235 		return 0;
236 	}
237 
238 	/* Retrieve cooler status */
239 	ret =
240 	    rog_ryujin_execute_cmd(priv, get_cooler_status_cmd, GET_CMD_LENGTH,
241 				   &priv->cooler_status_received);
242 	if (ret < 0)
243 		return ret;
244 
245 	if (priv->info->has_controller) {
246 		/* Retrieve controller status (speeds) */
247 		ret = rog_ryujin_execute_cmd(priv, get_controller_speed_cmd,
248 					     GET_CMD_LENGTH,
249 					     &priv->controller_status_received);
250 		if (ret < 0)
251 			return ret;
252 	}
253 
254 	/* Retrieve cooler duty */
255 	ret =
256 	    rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH,
257 				   &priv->cooler_duty_received);
258 	if (ret < 0)
259 		return ret;
260 
261 	if (priv->info->has_controller) {
262 		/* Retrieve controller duty */
263 		ret = rog_ryujin_execute_cmd(priv, get_controller_duty_cmd,
264 					     GET_CMD_LENGTH,
265 					     &priv->controller_duty_received);
266 		if (ret < 0)
267 			return ret;
268 	}
269 
270 	priv->updated = jiffies;
271 	return 0;
272 }
273 
274 static int rog_ryujin_read(struct device *dev, enum hwmon_sensor_types type,
275 			   u32 attr, int channel, long *val)
276 {
277 	struct rog_ryujin_data *priv = dev_get_drvdata(dev);
278 	int ret = rog_ryujin_get_status(priv);
279 
280 	if (ret < 0)
281 		return ret;
282 
283 	switch (type) {
284 	case hwmon_temp:
285 		*val = priv->temp_input[channel];
286 		break;
287 	case hwmon_fan:
288 		*val = priv->speed_input[channel];
289 		break;
290 	case hwmon_pwm:
291 		switch (attr) {
292 		case hwmon_pwm_input:
293 			*val = priv->duty_input[channel];
294 			break;
295 		default:
296 			return -EOPNOTSUPP;
297 		}
298 		break;
299 	default:
300 		return -EOPNOTSUPP;	/* unreachable */
301 	}
302 
303 	return 0;
304 }
305 
306 static int rog_ryujin_read_string(struct device *dev, enum hwmon_sensor_types type,
307 				  u32 attr, int channel, const char **str)
308 {
309 	switch (type) {
310 	case hwmon_temp:
311 		*str = rog_ryujin_temp_label[channel];
312 		break;
313 	case hwmon_fan:
314 		*str = rog_ryujin_speed_label[channel];
315 		break;
316 	default:
317 		return -EOPNOTSUPP;	/* unreachable */
318 	}
319 
320 	return 0;
321 }
322 
323 /*
324  * On devices with a non-zero duty_channel (the RYUJIN III), the channel byte of
325  * the set cooler duty command selects the pump control source.
326  * RYUJIN_SOURCE_HEADER hands the pump to its internal controller, which follows
327  * the PWM of the header its 4-pin cable is plugged into. RYUJIN_SOURCE_USB
328  * makes the pump follow the pump duty in the command. The cooler duty report
329  * returns the current source.
330  *
331  * A fan duty write sends the reported source back, so that it leaves the pump
332  * control source as it is. A pump duty write selects USB. The cooler does not
333  * pass on a pump duty equal to the one it reports, and after power-on it
334  * reports 40% while a pump handed over at that duty runs at full speed. So
335  * when a pump duty write asks for the reported duty, write a duty 1% away
336  * first.
337  */
338 static int rog_ryujin_select_pump_source(struct rog_ryujin_data *priv, u8 *set_cmd,
339 					 int channel)
340 {
341 	u8 duty = set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET];
342 	int ret;
343 
344 	if (channel == 1) {
345 		set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = priv->pump_source;
346 		return 0;
347 	}
348 
349 	set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = RYUJIN_SOURCE_USB;
350 
351 	if (duty != rog_ryujin_pwm_to_percent(priv->duty_input[0]))
352 		return 0;
353 
354 	set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = duty < 100 ? duty + 1 : duty - 1;
355 	ret = rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, &priv->cooler_duty_set);
356 	set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = duty;
357 
358 	return ret;
359 }
360 
361 static int rog_ryujin_write_fixed_duty(struct rog_ryujin_data *priv, int channel, int val)
362 {
363 	u8 set_cmd[SET_CMD_LENGTH];
364 	int ret;
365 
366 	if (channel < 2) {
367 		/*
368 		 * Retrieve cooler duty since both pump and internal fan are set
369 		 * together, then write back with one of them modified.
370 		 */
371 		ret =
372 		    rog_ryujin_execute_cmd(priv, get_cooler_duty_cmd, GET_CMD_LENGTH,
373 					   &priv->cooler_duty_received);
374 		if (ret < 0)
375 			return ret;
376 
377 		memcpy(set_cmd, set_cooler_duty_cmd, SET_CMD_LENGTH);
378 		set_cmd[RYUJIN_SET_COOLER_CHANNEL_OFFSET] = priv->info->duty_channel;
379 
380 		/* Cooler duties are set as 0-100% */
381 		val = rog_ryujin_pwm_to_percent(val);
382 
383 		if (channel == 0) {
384 			/* Cooler pump duty */
385 			set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] = val;
386 			set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] =
387 			    rog_ryujin_pwm_to_percent(priv->duty_input[1]);
388 		} else if (channel == 1) {
389 			/* Cooler internal fan duty */
390 			set_cmd[RYUJIN_SET_COOLER_PUMP_DUTY_OFFSET] =
391 			    rog_ryujin_pwm_to_percent(priv->duty_input[0]);
392 			set_cmd[RYUJIN_SET_COOLER_FAN_DUTY_OFFSET] = val;
393 		}
394 
395 		if (priv->info->duty_channel) {
396 			ret = rog_ryujin_select_pump_source(priv, set_cmd, channel);
397 			if (ret < 0)
398 				return ret;
399 		}
400 
401 		return rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH, &priv->cooler_duty_set);
402 	} else {
403 		/*
404 		 * Controller fan duty (channel == 2). No need to retrieve current
405 		 * duty, so just send the command.
406 		 */
407 		memcpy(set_cmd, set_controller_duty_cmd, SET_CMD_LENGTH);
408 		set_cmd[RYUJIN_SET_CONTROLLER_FAN_DUTY_OFFSET] = val;
409 
410 		ret =
411 		    rog_ryujin_execute_cmd(priv, set_cmd, SET_CMD_LENGTH,
412 					   &priv->controller_duty_set);
413 		if (ret < 0)
414 			return ret;
415 	}
416 
417 	/* Lock onto this value until next refresh cycle */
418 	priv->duty_input[channel] = val;
419 
420 	return 0;
421 }
422 
423 static int rog_ryujin_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
424 			    long val)
425 {
426 	struct rog_ryujin_data *priv = dev_get_drvdata(dev);
427 	int ret;
428 
429 	switch (type) {
430 	case hwmon_pwm:
431 		switch (attr) {
432 		case hwmon_pwm_input:
433 			if (val < 0 || val > 255)
434 				return -EINVAL;
435 
436 			ret = rog_ryujin_write_fixed_duty(priv, channel, val);
437 			if (ret < 0)
438 				return ret;
439 			break;
440 		default:
441 			return -EOPNOTSUPP;
442 		}
443 		break;
444 	default:
445 		return -EOPNOTSUPP;
446 	}
447 
448 	return 0;
449 }
450 
451 static const struct hwmon_ops rog_ryujin_hwmon_ops = {
452 	.is_visible = rog_ryujin_is_visible,
453 	.read = rog_ryujin_read,
454 	.read_string = rog_ryujin_read_string,
455 	.write = rog_ryujin_write
456 };
457 
458 static const struct hwmon_channel_info *rog_ryujin_info[] = {
459 	HWMON_CHANNEL_INFO(temp,
460 			   HWMON_T_INPUT | HWMON_T_LABEL),
461 	HWMON_CHANNEL_INFO(fan,
462 			   HWMON_F_INPUT | HWMON_F_LABEL,
463 			   HWMON_F_INPUT | HWMON_F_LABEL,
464 			   HWMON_F_INPUT | HWMON_F_LABEL,
465 			   HWMON_F_INPUT | HWMON_F_LABEL,
466 			   HWMON_F_INPUT | HWMON_F_LABEL,
467 			   HWMON_F_INPUT | HWMON_F_LABEL),
468 	HWMON_CHANNEL_INFO(pwm,
469 			   HWMON_PWM_INPUT,
470 			   HWMON_PWM_INPUT,
471 			   HWMON_PWM_INPUT),
472 	NULL
473 };
474 
475 static const struct hwmon_chip_info rog_ryujin_chip_info = {
476 	.ops = &rog_ryujin_hwmon_ops,
477 	.info = rog_ryujin_info,
478 };
479 
480 static int rog_ryujin_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
481 				int size)
482 {
483 	struct rog_ryujin_data *priv = hid_get_drvdata(hdev);
484 	unsigned long flags;
485 
486 	if (size < 2 || data[0] != RYUJIN_CMD_PREFIX)
487 		return 0;
488 
489 	spin_lock_irqsave(&priv->status_report_request_lock, flags);
490 
491 	if (data[1] == RYUJIN_GET_COOLER_STATUS_CMD_RESPONSE) {
492 		if (size <= priv->info->temp_offset + 1 ||
493 		    size <= priv->info->pump_speed_offset + 1 ||
494 		    size <= priv->info->fan_speed_offset + 1)
495 			goto unlock;
496 
497 		/* Received coolant temp and speeds of pump and internal fan */
498 		priv->temp_input[0] = data[priv->info->temp_offset] * 1000 +
499 			data[priv->info->temp_offset + 1] * 100;
500 		priv->speed_input[0] =
501 			get_unaligned_le16(data + priv->info->pump_speed_offset);
502 		priv->speed_input[1] =
503 			get_unaligned_le16(data + priv->info->fan_speed_offset);
504 
505 		if (!completion_done(&priv->cooler_status_received))
506 			complete_all(&priv->cooler_status_received);
507 	} else if (data[1] == RYUJIN_GET_CONTROLLER_SPEED_CMD_RESPONSE) {
508 		if (size <= RYUJIN_CONTROLLER_SPEED_3 + 1)
509 			goto unlock;
510 
511 		/* Received speeds of four fans attached to the controller */
512 		priv->speed_input[2] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_1);
513 		priv->speed_input[3] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_2);
514 		priv->speed_input[4] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_3);
515 		priv->speed_input[5] = get_unaligned_le16(data + RYUJIN_CONTROLLER_SPEED_4);
516 
517 		if (!completion_done(&priv->controller_status_received))
518 			complete_all(&priv->controller_status_received);
519 	} else if (data[1] == RYUJIN_GET_COOLER_DUTY_CMD_RESPONSE) {
520 		if (size <= RYUJIN_INTERNAL_FAN_DUTY)
521 			goto unlock;
522 
523 		/* Received report for pump and internal fan duties (in %) */
524 		if (data[RYUJIN_PUMP_DUTY] == 0 && data[RYUJIN_INTERNAL_FAN_DUTY] == 0) {
525 			/*
526 			 * We received a report with zeroes for duty in both places.
527 			 * The device returns this as a confirmation that setting values
528 			 * is successful. If we initiated a write, mark it as complete.
529 			 */
530 			if (!completion_done(&priv->cooler_duty_set))
531 				complete_all(&priv->cooler_duty_set);
532 			else if (!completion_done(&priv->cooler_duty_received))
533 				/*
534 				 * We didn't initiate a write, but received both zeroes.
535 				 * This means that either both duties are actually zero,
536 				 * or that we received a success report caused by userspace.
537 				 * We're expecting a report, so parse it.
538 				 */
539 				goto read_cooler_duty;
540 			goto unlock;
541 		}
542 read_cooler_duty:
543 		priv->pump_source = data[RYUJIN_PUMP_SOURCE];
544 		priv->duty_input[0] = rog_ryujin_percent_to_pwm(data[RYUJIN_PUMP_DUTY]);
545 		priv->duty_input[1] = rog_ryujin_percent_to_pwm(data[RYUJIN_INTERNAL_FAN_DUTY]);
546 
547 		if (!completion_done(&priv->cooler_duty_received))
548 			complete_all(&priv->cooler_duty_received);
549 	} else if (data[1] == RYUJIN_GET_CONTROLLER_DUTY_CMD_RESPONSE) {
550 		if (size <= RYUJIN_CONTROLLER_DUTY)
551 			goto unlock;
552 
553 		/* Received report for controller duty for fans (in PWM) */
554 		if (data[RYUJIN_CONTROLLER_DUTY] == 0) {
555 			/*
556 			 * We received a report with a zero for duty. The device returns this as
557 			 * a confirmation that setting the controller duty value was successful.
558 			 * If we initiated a write, mark it as complete.
559 			 */
560 			if (!completion_done(&priv->controller_duty_set))
561 				complete_all(&priv->controller_duty_set);
562 			else if (!completion_done(&priv->controller_duty_received))
563 				/*
564 				 * We didn't initiate a write, but received a zero for duty.
565 				 * This means that either the duty is actually zero, or that
566 				 * we received a success report caused by userspace.
567 				 * We're expecting a report, so parse it.
568 				 */
569 				goto read_controller_duty;
570 			goto unlock;
571 		}
572 read_controller_duty:
573 		priv->duty_input[2] = data[RYUJIN_CONTROLLER_DUTY];
574 
575 		if (!completion_done(&priv->controller_duty_received))
576 			complete_all(&priv->controller_duty_received);
577 	}
578 
579 unlock:
580 	spin_unlock_irqrestore(&priv->status_report_request_lock, flags);
581 	return 0;
582 }
583 
584 static int rog_ryujin_probe(struct hid_device *hdev, const struct hid_device_id *id)
585 {
586 	struct rog_ryujin_data *priv;
587 	int ret;
588 
589 	if (!id->driver_data)
590 		return -EINVAL;
591 
592 	priv = devm_kzalloc_obj(&hdev->dev, *priv);
593 	if (!priv)
594 		return -ENOMEM;
595 
596 	priv->hdev = hdev;
597 	priv->info = (const struct rog_ryujin_device_info *)id->driver_data;
598 	hid_set_drvdata(hdev, priv);
599 
600 	/*
601 	 * Initialize priv->updated to STATUS_VALIDITY seconds in the past, making
602 	 * the initial empty data invalid for rog_ryujin_read() without the need for
603 	 * a special case there.
604 	 */
605 	priv->updated = jiffies - msecs_to_jiffies(STATUS_VALIDITY);
606 
607 	ret = hid_parse(hdev);
608 	if (ret) {
609 		hid_err(hdev, "hid parse failed with %d\n", ret);
610 		return ret;
611 	}
612 
613 	/* Enable hidraw so existing user-space tools can continue to work */
614 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
615 	if (ret) {
616 		hid_err(hdev, "hid hw start failed with %d\n", ret);
617 		return ret;
618 	}
619 
620 	ret = hid_hw_open(hdev);
621 	if (ret) {
622 		hid_err(hdev, "hid hw open failed with %d\n", ret);
623 		goto fail_and_stop;
624 	}
625 
626 	priv->buffer = devm_kzalloc(&hdev->dev, MAX_REPORT_LENGTH, GFP_KERNEL);
627 	if (!priv->buffer) {
628 		ret = -ENOMEM;
629 		goto fail_and_close;
630 	}
631 
632 	spin_lock_init(&priv->status_report_request_lock);
633 	init_completion(&priv->cooler_status_received);
634 	init_completion(&priv->controller_status_received);
635 	init_completion(&priv->cooler_duty_received);
636 	init_completion(&priv->controller_duty_received);
637 	init_completion(&priv->cooler_duty_set);
638 	init_completion(&priv->controller_duty_set);
639 
640 	/*
641 	 * Start completions in the completed state so rog_ryujin_raw_event()
642 	 * only sees !completion_done() while rog_ryujin_execute_cmd() is
643 	 * awaiting a response.
644 	 */
645 	complete_all(&priv->cooler_status_received);
646 	complete_all(&priv->controller_status_received);
647 	complete_all(&priv->cooler_duty_received);
648 	complete_all(&priv->controller_duty_received);
649 	complete_all(&priv->cooler_duty_set);
650 	complete_all(&priv->controller_duty_set);
651 
652 	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "rog_ryujin",
653 							  priv, &rog_ryujin_chip_info, NULL);
654 	if (IS_ERR(priv->hwmon_dev)) {
655 		ret = PTR_ERR(priv->hwmon_dev);
656 		hid_err(hdev, "hwmon registration failed with %d\n", ret);
657 		goto fail_and_close;
658 	}
659 
660 	return 0;
661 
662 fail_and_close:
663 	hid_hw_close(hdev);
664 fail_and_stop:
665 	hid_hw_stop(hdev);
666 	return ret;
667 }
668 
669 static void rog_ryujin_remove(struct hid_device *hdev)
670 {
671 	struct rog_ryujin_data *priv = hid_get_drvdata(hdev);
672 
673 	hwmon_device_unregister(priv->hwmon_dev);
674 
675 	hid_hw_close(hdev);
676 	hid_hw_stop(hdev);
677 }
678 
679 static const struct hid_device_id rog_ryujin_table[] = {
680 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_AIO),
681 	  .driver_data = (kernel_ulong_t)&rog_ryujin_ii_360_info },
682 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_EXTREME),
683 	  .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info },
684 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_EVA),
685 	  .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info },
686 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III_WHITE),
687 	  .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info },
688 	{ HID_USB_DEVICE(USB_VENDOR_ID_ASUS_ROG, USB_PRODUCT_ID_RYUJIN_III),
689 	  .driver_data = (kernel_ulong_t)&rog_ryujin_iii_info },
690 	{ }
691 };
692 
693 MODULE_DEVICE_TABLE(hid, rog_ryujin_table);
694 
695 static struct hid_driver rog_ryujin_driver = {
696 	.name = "rog_ryujin",
697 	.id_table = rog_ryujin_table,
698 	.probe = rog_ryujin_probe,
699 	.remove = rog_ryujin_remove,
700 	.raw_event = rog_ryujin_raw_event,
701 };
702 
703 static int __init rog_ryujin_init(void)
704 {
705 	return hid_register_driver(&rog_ryujin_driver);
706 }
707 
708 static void __exit rog_ryujin_exit(void)
709 {
710 	hid_unregister_driver(&rog_ryujin_driver);
711 }
712 
713 /* When compiled into the kernel, initialize after the HID bus */
714 late_initcall(rog_ryujin_init);
715 module_exit(rog_ryujin_exit);
716 
717 MODULE_LICENSE("GPL");
718 MODULE_AUTHOR("Aleksa Savic <savicaleksa83@gmail.com>");
719 MODULE_DESCRIPTION("Hwmon driver for Asus ROG Ryujin AIO coolers");
720