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