1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * hwmon driver for Aquacomputer devices (D5 Next, Farbwerk, Farbwerk 360, Octo,
4  * Quadro)
5  *
6  * Aquacomputer devices send HID reports (with ID 0x01) every second to report
7  * sensor values.
8  *
9  * Copyright 2021 Aleksa Savic <savicaleksa83@gmail.com>
10  * Copyright 2022 Jack Doan <me@jackdoan.com>
11  */
12 
13 #include <linux/crc16.h>
14 #include <linux/debugfs.h>
15 #include <linux/hid.h>
16 #include <linux/hwmon.h>
17 #include <linux/jiffies.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/seq_file.h>
21 #include <asm/unaligned.h>
22 
23 #define USB_VENDOR_ID_AQUACOMPUTER	0x0c70
24 #define USB_PRODUCT_ID_FARBWERK		0xf00a
25 #define USB_PRODUCT_ID_QUADRO		0xf00d
26 #define USB_PRODUCT_ID_D5NEXT		0xf00e
27 #define USB_PRODUCT_ID_FARBWERK360	0xf010
28 #define USB_PRODUCT_ID_OCTO		0xf011
29 
30 enum kinds { d5next, farbwerk, farbwerk360, octo, quadro };
31 
32 static const char *const aqc_device_names[] = {
33 	[d5next] = "d5next",
34 	[farbwerk] = "farbwerk",
35 	[farbwerk360] = "farbwerk360",
36 	[octo] = "octo",
37 	[quadro] = "quadro"
38 };
39 
40 #define DRIVER_NAME			"aquacomputer_d5next"
41 
42 #define STATUS_REPORT_ID		0x01
43 #define STATUS_UPDATE_INTERVAL		(2 * HZ)	/* In seconds */
44 #define SERIAL_FIRST_PART		3
45 #define SERIAL_SECOND_PART		5
46 #define FIRMWARE_VERSION		13
47 
48 #define CTRL_REPORT_ID			0x03
49 
50 /* The HID report that the official software always sends
51  * after writing values, currently same for all devices
52  */
53 #define SECONDARY_CTRL_REPORT_ID	0x02
54 #define SECONDARY_CTRL_REPORT_SIZE	0x0B
55 
56 static u8 secondary_ctrl_report[] = {
57 	0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x34, 0xC6
58 };
59 
60 /* Register offsets for all Aquacomputer devices */
61 #define AQC_TEMP_SENSOR_SIZE		0x02
62 #define AQC_TEMP_SENSOR_DISCONNECTED	0x7FFF
63 #define AQC_FAN_PERCENT_OFFSET		0x00
64 #define AQC_FAN_VOLTAGE_OFFSET		0x02
65 #define AQC_FAN_CURRENT_OFFSET		0x04
66 #define AQC_FAN_POWER_OFFSET		0x06
67 #define AQC_FAN_SPEED_OFFSET		0x08
68 
69 /* Register offsets for the D5 Next pump */
70 #define D5NEXT_POWER_CYCLES		0x18
71 #define D5NEXT_COOLANT_TEMP		0x57
72 #define D5NEXT_NUM_FANS			2
73 #define D5NEXT_NUM_SENSORS		1
74 #define D5NEXT_PUMP_OFFSET		0x6c
75 #define D5NEXT_FAN_OFFSET		0x5f
76 #define D5NEXT_5V_VOLTAGE		0x39
77 #define D5NEXT_12V_VOLTAGE		0x37
78 #define D5NEXT_CTRL_REPORT_SIZE		0x329
79 static u8 d5next_sensor_fan_offsets[] = { D5NEXT_PUMP_OFFSET, D5NEXT_FAN_OFFSET };
80 
81 /* Pump and fan speed registers in D5 Next control report (from 0-100%) */
82 static u16 d5next_ctrl_fan_offsets[] = { 0x97, 0x42 };
83 
84 /* Register offsets for the Farbwerk RGB controller */
85 #define FARBWERK_NUM_SENSORS		4
86 #define FARBWERK_SENSOR_START		0x2f
87 
88 /* Register offsets for the Farbwerk 360 RGB controller */
89 #define FARBWERK360_NUM_SENSORS		4
90 #define FARBWERK360_SENSOR_START	0x32
91 
92 /* Register offsets for the Octo fan controller */
93 #define OCTO_POWER_CYCLES		0x18
94 #define OCTO_NUM_FANS			8
95 #define OCTO_NUM_SENSORS		4
96 #define OCTO_SENSOR_START		0x3D
97 #define OCTO_CTRL_REPORT_SIZE		0x65F
98 static u8 octo_sensor_fan_offsets[] = { 0x7D, 0x8A, 0x97, 0xA4, 0xB1, 0xBE, 0xCB, 0xD8 };
99 
100 /* Fan speed registers in Octo control report (from 0-100%) */
101 static u16 octo_ctrl_fan_offsets[] = { 0x5B, 0xB0, 0x105, 0x15A, 0x1AF, 0x204, 0x259, 0x2AE };
102 
103 /* Register offsets for the Quadro fan controller */
104 #define QUADRO_POWER_CYCLES		0x18
105 #define QUADRO_NUM_FANS			4
106 #define QUADRO_NUM_SENSORS		4
107 #define QUADRO_SENSOR_START		0x34
108 #define QUADRO_CTRL_REPORT_SIZE		0x3c1
109 #define QUADRO_FLOW_SENSOR_OFFSET	0x6e
110 static u8 quadro_sensor_fan_offsets[] = { 0x70, 0x7D, 0x8A, 0x97 };
111 
112 /* Fan speed registers in Quadro control report (from 0-100%) */
113 static u16 quadro_ctrl_fan_offsets[] = { 0x36, 0x8b, 0xe0, 0x135 };
114 
115 /* Labels for D5 Next */
116 static const char *const label_d5next_temp[] = {
117 	"Coolant temp"
118 };
119 
120 static const char *const label_d5next_speeds[] = {
121 	"Pump speed",
122 	"Fan speed"
123 };
124 
125 static const char *const label_d5next_power[] = {
126 	"Pump power",
127 	"Fan power"
128 };
129 
130 static const char *const label_d5next_voltages[] = {
131 	"Pump voltage",
132 	"Fan voltage",
133 	"+5V voltage",
134 	"+12V voltage"
135 };
136 
137 static const char *const label_d5next_current[] = {
138 	"Pump current",
139 	"Fan current"
140 };
141 
142 /* Labels for Farbwerk, Farbwerk 360 and Octo and Quadro temperature sensors */
143 static const char *const label_temp_sensors[] = {
144 	"Sensor 1",
145 	"Sensor 2",
146 	"Sensor 3",
147 	"Sensor 4"
148 };
149 
150 /* Labels for Octo and Quadro (except speed) */
151 static const char *const label_fan_speed[] = {
152 	"Fan 1 speed",
153 	"Fan 2 speed",
154 	"Fan 3 speed",
155 	"Fan 4 speed",
156 	"Fan 5 speed",
157 	"Fan 6 speed",
158 	"Fan 7 speed",
159 	"Fan 8 speed"
160 };
161 
162 static const char *const label_fan_power[] = {
163 	"Fan 1 power",
164 	"Fan 2 power",
165 	"Fan 3 power",
166 	"Fan 4 power",
167 	"Fan 5 power",
168 	"Fan 6 power",
169 	"Fan 7 power",
170 	"Fan 8 power"
171 };
172 
173 static const char *const label_fan_voltage[] = {
174 	"Fan 1 voltage",
175 	"Fan 2 voltage",
176 	"Fan 3 voltage",
177 	"Fan 4 voltage",
178 	"Fan 5 voltage",
179 	"Fan 6 voltage",
180 	"Fan 7 voltage",
181 	"Fan 8 voltage"
182 };
183 
184 static const char *const label_fan_current[] = {
185 	"Fan 1 current",
186 	"Fan 2 current",
187 	"Fan 3 current",
188 	"Fan 4 current",
189 	"Fan 5 current",
190 	"Fan 6 current",
191 	"Fan 7 current",
192 	"Fan 8 current"
193 };
194 
195 /* Labels for Quadro fan speeds */
196 static const char *const label_quadro_speeds[] = {
197 	"Fan 1 speed",
198 	"Fan 2 speed",
199 	"Fan 3 speed",
200 	"Fan 4 speed",
201 	"Flow speed [dL/h]"
202 };
203 
204 struct aqc_data {
205 	struct hid_device *hdev;
206 	struct device *hwmon_dev;
207 	struct dentry *debugfs;
208 	struct mutex mutex;	/* Used for locking access when reading and writing PWM values */
209 	enum kinds kind;
210 	const char *name;
211 
212 	int buffer_size;
213 	u8 *buffer;
214 	int checksum_start;
215 	int checksum_length;
216 	int checksum_offset;
217 
218 	int num_fans;
219 	u8 *fan_sensor_offsets;
220 	u16 *fan_ctrl_offsets;
221 	int num_temp_sensors;
222 	int temp_sensor_start_offset;
223 	u16 power_cycle_count_offset;
224 	u8 flow_sensor_offset;
225 
226 	/* General info, same across all devices */
227 	u32 serial_number[2];
228 	u16 firmware_version;
229 
230 	/* How many times the device was powered on, if available */
231 	u32 power_cycles;
232 
233 	/* Sensor values */
234 	s32 temp_input[4];
235 	u16 speed_input[8];
236 	u32 power_input[8];
237 	u16 voltage_input[8];
238 	u16 current_input[8];
239 
240 	/* Label values */
241 	const char *const *temp_label;
242 	const char *const *speed_label;
243 	const char *const *power_label;
244 	const char *const *voltage_label;
245 	const char *const *current_label;
246 
247 	unsigned long updated;
248 };
249 
250 /* Converts from centi-percent */
251 static int aqc_percent_to_pwm(u16 val)
252 {
253 	return DIV_ROUND_CLOSEST(val * 255, 100 * 100);
254 }
255 
256 /* Converts to centi-percent */
257 static int aqc_pwm_to_percent(long val)
258 {
259 	if (val < 0 || val > 255)
260 		return -EINVAL;
261 
262 	return DIV_ROUND_CLOSEST(val * 100 * 100, 255);
263 }
264 
265 /* Expects the mutex to be locked */
266 static int aqc_get_ctrl_data(struct aqc_data *priv)
267 {
268 	int ret;
269 
270 	memset(priv->buffer, 0x00, priv->buffer_size);
271 	ret = hid_hw_raw_request(priv->hdev, CTRL_REPORT_ID, priv->buffer, priv->buffer_size,
272 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
273 	if (ret < 0)
274 		ret = -ENODATA;
275 
276 	return ret;
277 }
278 
279 /* Expects the mutex to be locked */
280 static int aqc_send_ctrl_data(struct aqc_data *priv)
281 {
282 	int ret;
283 	u16 checksum;
284 
285 	/* Init and xorout value for CRC-16/USB is 0xffff */
286 	checksum = crc16(0xffff, priv->buffer + priv->checksum_start, priv->checksum_length);
287 	checksum ^= 0xffff;
288 
289 	/* Place the new checksum at the end of the report */
290 	put_unaligned_be16(checksum, priv->buffer + priv->checksum_offset);
291 
292 	/* Send the patched up report back to the device */
293 	ret = hid_hw_raw_request(priv->hdev, CTRL_REPORT_ID, priv->buffer, priv->buffer_size,
294 				 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
295 	if (ret < 0)
296 		return ret;
297 
298 	/* The official software sends this report after every change, so do it here as well */
299 	ret = hid_hw_raw_request(priv->hdev, SECONDARY_CTRL_REPORT_ID, secondary_ctrl_report,
300 				 SECONDARY_CTRL_REPORT_SIZE, HID_FEATURE_REPORT,
301 				 HID_REQ_SET_REPORT);
302 	return ret;
303 }
304 
305 /* Refreshes the control buffer and returns value at offset */
306 static int aqc_get_ctrl_val(struct aqc_data *priv, int offset)
307 {
308 	int ret;
309 
310 	mutex_lock(&priv->mutex);
311 
312 	ret = aqc_get_ctrl_data(priv);
313 	if (ret < 0)
314 		goto unlock_and_return;
315 
316 	ret = get_unaligned_be16(priv->buffer + offset);
317 
318 unlock_and_return:
319 	mutex_unlock(&priv->mutex);
320 	return ret;
321 }
322 
323 static int aqc_set_ctrl_val(struct aqc_data *priv, int offset, long val)
324 {
325 	int ret;
326 
327 	mutex_lock(&priv->mutex);
328 
329 	ret = aqc_get_ctrl_data(priv);
330 	if (ret < 0)
331 		goto unlock_and_return;
332 
333 	put_unaligned_be16((u16)val, priv->buffer + offset);
334 
335 	ret = aqc_send_ctrl_data(priv);
336 
337 unlock_and_return:
338 	mutex_unlock(&priv->mutex);
339 	return ret;
340 }
341 
342 static umode_t aqc_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
343 {
344 	const struct aqc_data *priv = data;
345 
346 	switch (type) {
347 	case hwmon_temp:
348 		if (channel < priv->num_temp_sensors)
349 			return 0444;
350 		break;
351 	case hwmon_pwm:
352 		if (priv->fan_ctrl_offsets && channel < priv->num_fans) {
353 			switch (attr) {
354 			case hwmon_pwm_input:
355 				return 0644;
356 			default:
357 				break;
358 			}
359 		}
360 		break;
361 	case hwmon_fan:
362 		switch (priv->kind) {
363 		case quadro:
364 			/* Special case to support flow sensor */
365 			if (channel < priv->num_fans + 1)
366 				return 0444;
367 			break;
368 		default:
369 			if (channel < priv->num_fans)
370 				return 0444;
371 			break;
372 		}
373 		break;
374 	case hwmon_power:
375 	case hwmon_curr:
376 		if (channel < priv->num_fans)
377 			return 0444;
378 		break;
379 	case hwmon_in:
380 		switch (priv->kind) {
381 		case d5next:
382 			/* Special case to support +5V and +12V voltage sensors */
383 			if (channel < priv->num_fans + 2)
384 				return 0444;
385 			break;
386 		default:
387 			if (channel < priv->num_fans)
388 				return 0444;
389 			break;
390 		}
391 		break;
392 	default:
393 		break;
394 	}
395 
396 	return 0;
397 }
398 
399 static int aqc_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
400 		    int channel, long *val)
401 {
402 	int ret;
403 	struct aqc_data *priv = dev_get_drvdata(dev);
404 
405 	if (time_after(jiffies, priv->updated + STATUS_UPDATE_INTERVAL))
406 		return -ENODATA;
407 
408 	switch (type) {
409 	case hwmon_temp:
410 		if (priv->temp_input[channel] == -ENODATA)
411 			return -ENODATA;
412 
413 		*val = priv->temp_input[channel];
414 		break;
415 	case hwmon_fan:
416 		*val = priv->speed_input[channel];
417 		break;
418 	case hwmon_power:
419 		*val = priv->power_input[channel];
420 		break;
421 	case hwmon_pwm:
422 		if (priv->fan_ctrl_offsets) {
423 			ret = aqc_get_ctrl_val(priv, priv->fan_ctrl_offsets[channel]);
424 			if (ret < 0)
425 				return ret;
426 
427 			*val = aqc_percent_to_pwm(ret);
428 		}
429 		break;
430 	case hwmon_in:
431 		*val = priv->voltage_input[channel];
432 		break;
433 	case hwmon_curr:
434 		*val = priv->current_input[channel];
435 		break;
436 	default:
437 		return -EOPNOTSUPP;
438 	}
439 
440 	return 0;
441 }
442 
443 static int aqc_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
444 			   int channel, const char **str)
445 {
446 	struct aqc_data *priv = dev_get_drvdata(dev);
447 
448 	switch (type) {
449 	case hwmon_temp:
450 		*str = priv->temp_label[channel];
451 		break;
452 	case hwmon_fan:
453 		*str = priv->speed_label[channel];
454 		break;
455 	case hwmon_power:
456 		*str = priv->power_label[channel];
457 		break;
458 	case hwmon_in:
459 		*str = priv->voltage_label[channel];
460 		break;
461 	case hwmon_curr:
462 		*str = priv->current_label[channel];
463 		break;
464 	default:
465 		return -EOPNOTSUPP;
466 	}
467 
468 	return 0;
469 }
470 
471 static int aqc_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
472 		     long val)
473 {
474 	int ret, pwm_value;
475 	struct aqc_data *priv = dev_get_drvdata(dev);
476 
477 	switch (type) {
478 	case hwmon_pwm:
479 		switch (attr) {
480 		case hwmon_pwm_input:
481 			if (priv->fan_ctrl_offsets) {
482 				pwm_value = aqc_pwm_to_percent(val);
483 				if (pwm_value < 0)
484 					return pwm_value;
485 
486 				ret = aqc_set_ctrl_val(priv, priv->fan_ctrl_offsets[channel],
487 						       pwm_value);
488 				if (ret < 0)
489 					return ret;
490 			}
491 			break;
492 		default:
493 			break;
494 		}
495 		break;
496 	default:
497 		return -EOPNOTSUPP;
498 	}
499 
500 	return 0;
501 }
502 
503 static const struct hwmon_ops aqc_hwmon_ops = {
504 	.is_visible = aqc_is_visible,
505 	.read = aqc_read,
506 	.read_string = aqc_read_string,
507 	.write = aqc_write
508 };
509 
510 static const struct hwmon_channel_info *aqc_info[] = {
511 	HWMON_CHANNEL_INFO(temp,
512 			   HWMON_T_INPUT | HWMON_T_LABEL,
513 			   HWMON_T_INPUT | HWMON_T_LABEL,
514 			   HWMON_T_INPUT | HWMON_T_LABEL,
515 			   HWMON_T_INPUT | HWMON_T_LABEL),
516 	HWMON_CHANNEL_INFO(fan,
517 			   HWMON_F_INPUT | HWMON_F_LABEL,
518 			   HWMON_F_INPUT | HWMON_F_LABEL,
519 			   HWMON_F_INPUT | HWMON_F_LABEL,
520 			   HWMON_F_INPUT | HWMON_F_LABEL,
521 			   HWMON_F_INPUT | HWMON_F_LABEL,
522 			   HWMON_F_INPUT | HWMON_F_LABEL,
523 			   HWMON_F_INPUT | HWMON_F_LABEL,
524 			   HWMON_F_INPUT | HWMON_F_LABEL),
525 	HWMON_CHANNEL_INFO(power,
526 			   HWMON_P_INPUT | HWMON_P_LABEL,
527 			   HWMON_P_INPUT | HWMON_P_LABEL,
528 			   HWMON_P_INPUT | HWMON_P_LABEL,
529 			   HWMON_P_INPUT | HWMON_P_LABEL,
530 			   HWMON_P_INPUT | HWMON_P_LABEL,
531 			   HWMON_P_INPUT | HWMON_P_LABEL,
532 			   HWMON_P_INPUT | HWMON_P_LABEL,
533 			   HWMON_P_INPUT | HWMON_P_LABEL),
534 	HWMON_CHANNEL_INFO(pwm,
535 			   HWMON_PWM_INPUT,
536 			   HWMON_PWM_INPUT,
537 			   HWMON_PWM_INPUT,
538 			   HWMON_PWM_INPUT,
539 			   HWMON_PWM_INPUT,
540 			   HWMON_PWM_INPUT,
541 			   HWMON_PWM_INPUT,
542 			   HWMON_PWM_INPUT),
543 	HWMON_CHANNEL_INFO(in,
544 			   HWMON_I_INPUT | HWMON_I_LABEL,
545 			   HWMON_I_INPUT | HWMON_I_LABEL,
546 			   HWMON_I_INPUT | HWMON_I_LABEL,
547 			   HWMON_I_INPUT | HWMON_I_LABEL,
548 			   HWMON_I_INPUT | HWMON_I_LABEL,
549 			   HWMON_I_INPUT | HWMON_I_LABEL,
550 			   HWMON_I_INPUT | HWMON_I_LABEL,
551 			   HWMON_I_INPUT | HWMON_I_LABEL),
552 	HWMON_CHANNEL_INFO(curr,
553 			   HWMON_C_INPUT | HWMON_C_LABEL,
554 			   HWMON_C_INPUT | HWMON_C_LABEL,
555 			   HWMON_C_INPUT | HWMON_C_LABEL,
556 			   HWMON_C_INPUT | HWMON_C_LABEL,
557 			   HWMON_C_INPUT | HWMON_C_LABEL,
558 			   HWMON_C_INPUT | HWMON_C_LABEL,
559 			   HWMON_C_INPUT | HWMON_C_LABEL,
560 			   HWMON_C_INPUT | HWMON_C_LABEL),
561 	NULL
562 };
563 
564 static const struct hwmon_chip_info aqc_chip_info = {
565 	.ops = &aqc_hwmon_ops,
566 	.info = aqc_info,
567 };
568 
569 static int aqc_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
570 {
571 	int i, sensor_value;
572 	struct aqc_data *priv;
573 
574 	if (report->id != STATUS_REPORT_ID)
575 		return 0;
576 
577 	priv = hid_get_drvdata(hdev);
578 
579 	/* Info provided with every report */
580 	priv->serial_number[0] = get_unaligned_be16(data + SERIAL_FIRST_PART);
581 	priv->serial_number[1] = get_unaligned_be16(data + SERIAL_SECOND_PART);
582 	priv->firmware_version = get_unaligned_be16(data + FIRMWARE_VERSION);
583 
584 	/* Temperature sensor readings */
585 	for (i = 0; i < priv->num_temp_sensors; i++) {
586 		sensor_value = get_unaligned_be16(data +
587 						  priv->temp_sensor_start_offset +
588 						  i * AQC_TEMP_SENSOR_SIZE);
589 		if (sensor_value == AQC_TEMP_SENSOR_DISCONNECTED)
590 			priv->temp_input[i] = -ENODATA;
591 		else
592 			priv->temp_input[i] = sensor_value * 10;
593 	}
594 
595 	/* Fan speed and related readings */
596 	for (i = 0; i < priv->num_fans; i++) {
597 		priv->speed_input[i] =
598 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] + AQC_FAN_SPEED_OFFSET);
599 		priv->power_input[i] =
600 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
601 				       AQC_FAN_POWER_OFFSET) * 10000;
602 		priv->voltage_input[i] =
603 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
604 				       AQC_FAN_VOLTAGE_OFFSET) * 10;
605 		priv->current_input[i] =
606 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] + AQC_FAN_CURRENT_OFFSET);
607 	}
608 
609 	if (priv->power_cycle_count_offset != 0)
610 		priv->power_cycles = get_unaligned_be32(data + priv->power_cycle_count_offset);
611 
612 	/* Special-case sensor readings */
613 	switch (priv->kind) {
614 	case d5next:
615 		priv->voltage_input[2] = get_unaligned_be16(data + D5NEXT_5V_VOLTAGE) * 10;
616 		priv->voltage_input[3] = get_unaligned_be16(data + D5NEXT_12V_VOLTAGE) * 10;
617 		break;
618 	case quadro:
619 		priv->speed_input[4] = get_unaligned_be16(data + priv->flow_sensor_offset);
620 		break;
621 	default:
622 		break;
623 	}
624 
625 	priv->updated = jiffies;
626 
627 	return 0;
628 }
629 
630 #ifdef CONFIG_DEBUG_FS
631 
632 static int serial_number_show(struct seq_file *seqf, void *unused)
633 {
634 	struct aqc_data *priv = seqf->private;
635 
636 	seq_printf(seqf, "%05u-%05u\n", priv->serial_number[0], priv->serial_number[1]);
637 
638 	return 0;
639 }
640 DEFINE_SHOW_ATTRIBUTE(serial_number);
641 
642 static int firmware_version_show(struct seq_file *seqf, void *unused)
643 {
644 	struct aqc_data *priv = seqf->private;
645 
646 	seq_printf(seqf, "%u\n", priv->firmware_version);
647 
648 	return 0;
649 }
650 DEFINE_SHOW_ATTRIBUTE(firmware_version);
651 
652 static int power_cycles_show(struct seq_file *seqf, void *unused)
653 {
654 	struct aqc_data *priv = seqf->private;
655 
656 	seq_printf(seqf, "%u\n", priv->power_cycles);
657 
658 	return 0;
659 }
660 DEFINE_SHOW_ATTRIBUTE(power_cycles);
661 
662 static void aqc_debugfs_init(struct aqc_data *priv)
663 {
664 	char name[64];
665 
666 	scnprintf(name, sizeof(name), "%s_%s-%s", "aquacomputer", priv->name,
667 		  dev_name(&priv->hdev->dev));
668 
669 	priv->debugfs = debugfs_create_dir(name, NULL);
670 	debugfs_create_file("serial_number", 0444, priv->debugfs, priv, &serial_number_fops);
671 	debugfs_create_file("firmware_version", 0444, priv->debugfs, priv, &firmware_version_fops);
672 
673 	if (priv->power_cycle_count_offset != 0)
674 		debugfs_create_file("power_cycles", 0444, priv->debugfs, priv, &power_cycles_fops);
675 }
676 
677 #else
678 
679 static void aqc_debugfs_init(struct aqc_data *priv)
680 {
681 }
682 
683 #endif
684 
685 static int aqc_probe(struct hid_device *hdev, const struct hid_device_id *id)
686 {
687 	struct aqc_data *priv;
688 	int ret;
689 
690 	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
691 	if (!priv)
692 		return -ENOMEM;
693 
694 	priv->hdev = hdev;
695 	hid_set_drvdata(hdev, priv);
696 
697 	priv->updated = jiffies - STATUS_UPDATE_INTERVAL;
698 
699 	ret = hid_parse(hdev);
700 	if (ret)
701 		return ret;
702 
703 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
704 	if (ret)
705 		return ret;
706 
707 	ret = hid_hw_open(hdev);
708 	if (ret)
709 		goto fail_and_stop;
710 
711 	switch (hdev->product) {
712 	case USB_PRODUCT_ID_D5NEXT:
713 		priv->kind = d5next;
714 
715 		priv->num_fans = D5NEXT_NUM_FANS;
716 		priv->fan_sensor_offsets = d5next_sensor_fan_offsets;
717 		priv->fan_ctrl_offsets = d5next_ctrl_fan_offsets;
718 		priv->num_temp_sensors = D5NEXT_NUM_SENSORS;
719 		priv->temp_sensor_start_offset = D5NEXT_COOLANT_TEMP;
720 		priv->power_cycle_count_offset = D5NEXT_POWER_CYCLES;
721 		priv->buffer_size = D5NEXT_CTRL_REPORT_SIZE;
722 
723 		priv->temp_label = label_d5next_temp;
724 		priv->speed_label = label_d5next_speeds;
725 		priv->power_label = label_d5next_power;
726 		priv->voltage_label = label_d5next_voltages;
727 		priv->current_label = label_d5next_current;
728 		break;
729 	case USB_PRODUCT_ID_FARBWERK:
730 		priv->kind = farbwerk;
731 
732 		priv->num_fans = 0;
733 		priv->num_temp_sensors = FARBWERK_NUM_SENSORS;
734 		priv->temp_sensor_start_offset = FARBWERK_SENSOR_START;
735 		priv->temp_label = label_temp_sensors;
736 		break;
737 	case USB_PRODUCT_ID_FARBWERK360:
738 		priv->kind = farbwerk360;
739 
740 		priv->num_fans = 0;
741 		priv->num_temp_sensors = FARBWERK360_NUM_SENSORS;
742 		priv->temp_sensor_start_offset = FARBWERK360_SENSOR_START;
743 		priv->temp_label = label_temp_sensors;
744 		break;
745 	case USB_PRODUCT_ID_OCTO:
746 		priv->kind = octo;
747 
748 		priv->num_fans = OCTO_NUM_FANS;
749 		priv->fan_sensor_offsets = octo_sensor_fan_offsets;
750 		priv->fan_ctrl_offsets = octo_ctrl_fan_offsets;
751 		priv->num_temp_sensors = OCTO_NUM_SENSORS;
752 		priv->temp_sensor_start_offset = OCTO_SENSOR_START;
753 		priv->power_cycle_count_offset = OCTO_POWER_CYCLES;
754 		priv->buffer_size = OCTO_CTRL_REPORT_SIZE;
755 
756 		priv->temp_label = label_temp_sensors;
757 		priv->speed_label = label_fan_speed;
758 		priv->power_label = label_fan_power;
759 		priv->voltage_label = label_fan_voltage;
760 		priv->current_label = label_fan_current;
761 		break;
762 	case USB_PRODUCT_ID_QUADRO:
763 		priv->kind = quadro;
764 
765 		priv->num_fans = QUADRO_NUM_FANS;
766 		priv->fan_sensor_offsets = quadro_sensor_fan_offsets;
767 		priv->fan_ctrl_offsets = quadro_ctrl_fan_offsets;
768 		priv->num_temp_sensors = QUADRO_NUM_SENSORS;
769 		priv->temp_sensor_start_offset = QUADRO_SENSOR_START;
770 		priv->power_cycle_count_offset = QUADRO_POWER_CYCLES;
771 		priv->buffer_size = QUADRO_CTRL_REPORT_SIZE;
772 		priv->flow_sensor_offset = QUADRO_FLOW_SENSOR_OFFSET;
773 
774 		priv->temp_label = label_temp_sensors;
775 		priv->speed_label = label_quadro_speeds;
776 		priv->power_label = label_fan_power;
777 		priv->voltage_label = label_fan_voltage;
778 		priv->current_label = label_fan_current;
779 		break;
780 	default:
781 		break;
782 	}
783 
784 	if (priv->buffer_size != 0) {
785 		priv->checksum_start = 0x01;
786 		priv->checksum_length = priv->buffer_size - 3;
787 		priv->checksum_offset = priv->buffer_size - 2;
788 	}
789 
790 	priv->name = aqc_device_names[priv->kind];
791 
792 	priv->buffer = devm_kzalloc(&hdev->dev, priv->buffer_size, GFP_KERNEL);
793 	if (!priv->buffer) {
794 		ret = -ENOMEM;
795 		goto fail_and_close;
796 	}
797 
798 	mutex_init(&priv->mutex);
799 
800 	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, priv->name, priv,
801 							  &aqc_chip_info, NULL);
802 
803 	if (IS_ERR(priv->hwmon_dev)) {
804 		ret = PTR_ERR(priv->hwmon_dev);
805 		goto fail_and_close;
806 	}
807 
808 	aqc_debugfs_init(priv);
809 
810 	return 0;
811 
812 fail_and_close:
813 	hid_hw_close(hdev);
814 fail_and_stop:
815 	hid_hw_stop(hdev);
816 	return ret;
817 }
818 
819 static void aqc_remove(struct hid_device *hdev)
820 {
821 	struct aqc_data *priv = hid_get_drvdata(hdev);
822 
823 	debugfs_remove_recursive(priv->debugfs);
824 	hwmon_device_unregister(priv->hwmon_dev);
825 
826 	hid_hw_close(hdev);
827 	hid_hw_stop(hdev);
828 }
829 
830 static const struct hid_device_id aqc_table[] = {
831 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_D5NEXT) },
832 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_FARBWERK) },
833 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_FARBWERK360) },
834 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_OCTO) },
835 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_QUADRO) },
836 	{ }
837 };
838 
839 MODULE_DEVICE_TABLE(hid, aqc_table);
840 
841 static struct hid_driver aqc_driver = {
842 	.name = DRIVER_NAME,
843 	.id_table = aqc_table,
844 	.probe = aqc_probe,
845 	.remove = aqc_remove,
846 	.raw_event = aqc_raw_event,
847 };
848 
849 static int __init aqc_init(void)
850 {
851 	return hid_register_driver(&aqc_driver);
852 }
853 
854 static void __exit aqc_exit(void)
855 {
856 	hid_unregister_driver(&aqc_driver);
857 }
858 
859 /* Request to initialize after the HID bus to ensure it's not being loaded before */
860 late_initcall(aqc_init);
861 module_exit(aqc_exit);
862 
863 MODULE_LICENSE("GPL");
864 MODULE_AUTHOR("Aleksa Savic <savicaleksa83@gmail.com>");
865 MODULE_AUTHOR("Jack Doan <me@jackdoan.com>");
866 MODULE_DESCRIPTION("Hwmon driver for Aquacomputer devices");
867