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, Aquaero, Aquastream Ultimate)
5  *
6  * Aquacomputer devices send HID reports (with ID 0x01) every second to report
7  * sensor values, except for devices that communicate through the
8  * legacy way (currently, Poweradjust 3).
9  *
10  * Copyright 2021 Aleksa Savic <savicaleksa83@gmail.com>
11  * Copyright 2022 Jack Doan <me@jackdoan.com>
12  */
13 
14 #include <linux/crc16.h>
15 #include <linux/debugfs.h>
16 #include <linux/hid.h>
17 #include <linux/hwmon.h>
18 #include <linux/jiffies.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/seq_file.h>
22 #include <asm/unaligned.h>
23 
24 #define USB_VENDOR_ID_AQUACOMPUTER	0x0c70
25 #define USB_PRODUCT_ID_AQUAERO		0xf001
26 #define USB_PRODUCT_ID_FARBWERK		0xf00a
27 #define USB_PRODUCT_ID_QUADRO		0xf00d
28 #define USB_PRODUCT_ID_D5NEXT		0xf00e
29 #define USB_PRODUCT_ID_FARBWERK360	0xf010
30 #define USB_PRODUCT_ID_OCTO		0xf011
31 #define USB_PRODUCT_ID_HIGHFLOWNEXT	0xf012
32 #define USB_PRODUCT_ID_AQUASTREAMULT	0xf00b
33 #define USB_PRODUCT_ID_POWERADJUST3	0xf0bd
34 
35 enum kinds {
36 	d5next, farbwerk, farbwerk360, octo, quadro,
37 	highflownext, aquaero, poweradjust3, aquastreamult
38 };
39 
40 static const char *const aqc_device_names[] = {
41 	[d5next] = "d5next",
42 	[farbwerk] = "farbwerk",
43 	[farbwerk360] = "farbwerk360",
44 	[octo] = "octo",
45 	[quadro] = "quadro",
46 	[highflownext] = "highflownext",
47 	[aquaero] = "aquaero",
48 	[aquastreamult] = "aquastreamultimate",
49 	[poweradjust3] = "poweradjust3"
50 };
51 
52 #define DRIVER_NAME			"aquacomputer_d5next"
53 
54 #define STATUS_REPORT_ID		0x01
55 #define STATUS_UPDATE_INTERVAL		(2 * HZ)	/* In seconds */
56 #define SERIAL_PART_OFFSET		2
57 
58 #define CTRL_REPORT_ID			0x03
59 
60 /* The HID report that the official software always sends
61  * after writing values, currently same for all devices
62  */
63 #define SECONDARY_CTRL_REPORT_ID	0x02
64 #define SECONDARY_CTRL_REPORT_SIZE	0x0B
65 
66 static u8 secondary_ctrl_report[] = {
67 	0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x34, 0xC6
68 };
69 
70 /* Report IDs for legacy devices */
71 #define POWERADJUST3_STATUS_REPORT_ID	0x03
72 
73 /* Info, sensor sizes and offsets for most Aquacomputer devices */
74 #define AQC_SERIAL_START		0x3
75 #define AQC_FIRMWARE_VERSION		0xD
76 
77 #define AQC_SENSOR_SIZE			0x02
78 #define AQC_TEMP_SENSOR_DISCONNECTED	0x7FFF
79 #define AQC_FAN_PERCENT_OFFSET		0x00
80 #define AQC_FAN_VOLTAGE_OFFSET		0x02
81 #define AQC_FAN_CURRENT_OFFSET		0x04
82 #define AQC_FAN_POWER_OFFSET		0x06
83 #define AQC_FAN_SPEED_OFFSET		0x08
84 
85 /* Specs of the Aquaero fan controllers */
86 #define AQUAERO_SERIAL_START			0x07
87 #define AQUAERO_FIRMWARE_VERSION		0x0B
88 #define AQUAERO_NUM_FANS			4
89 #define AQUAERO_NUM_SENSORS			8
90 #define AQUAERO_NUM_VIRTUAL_SENSORS		8
91 #define AQUAERO_NUM_CALC_VIRTUAL_SENSORS	4
92 #define AQUAERO_NUM_FLOW_SENSORS		2
93 
94 /* Sensor report offsets for Aquaero fan controllers */
95 #define AQUAERO_SENSOR_START			0x65
96 #define AQUAERO_VIRTUAL_SENSOR_START		0x85
97 #define AQUAERO_CALC_VIRTUAL_SENSOR_START	0x95
98 #define AQUAERO_FLOW_SENSORS_START		0xF9
99 #define AQUAERO_FAN_VOLTAGE_OFFSET		0x04
100 #define AQUAERO_FAN_CURRENT_OFFSET		0x06
101 #define AQUAERO_FAN_POWER_OFFSET		0x08
102 #define AQUAERO_FAN_SPEED_OFFSET		0x00
103 static u16 aquaero_sensor_fan_offsets[] = { 0x167, 0x173, 0x17f, 0x18B };
104 
105 /* Specs of the D5 Next pump */
106 #define D5NEXT_NUM_FANS			2
107 #define D5NEXT_NUM_SENSORS		1
108 #define D5NEXT_NUM_VIRTUAL_SENSORS	8
109 #define D5NEXT_CTRL_REPORT_SIZE		0x329
110 
111 /* Sensor report offsets for the D5 Next pump */
112 #define D5NEXT_POWER_CYCLES		0x18
113 #define D5NEXT_COOLANT_TEMP		0x57
114 #define D5NEXT_PUMP_OFFSET		0x6c
115 #define D5NEXT_FAN_OFFSET		0x5f
116 #define D5NEXT_5V_VOLTAGE		0x39
117 #define D5NEXT_12V_VOLTAGE		0x37
118 #define D5NEXT_VIRTUAL_SENSORS_START	0x3f
119 static u16 d5next_sensor_fan_offsets[] = { D5NEXT_PUMP_OFFSET, D5NEXT_FAN_OFFSET };
120 
121 /* Control report offsets for the D5 Next pump */
122 #define D5NEXT_TEMP_CTRL_OFFSET		0x2D	/* Temperature sensor offsets location */
123 static u16 d5next_ctrl_fan_offsets[] = { 0x97, 0x42 };	/* Pump and fan speed (from 0-100%) */
124 
125 /* Specs of the Aquastream Ultimate pump */
126 /* Pump does not follow the standard structure, so only consider the fan */
127 #define AQUASTREAMULT_NUM_FANS		1
128 #define AQUASTREAMULT_NUM_SENSORS	2
129 
130 /* Sensor report offsets for the Aquastream Ultimate pump */
131 #define AQUASTREAMULT_SENSOR_START		0x2D
132 #define AQUASTREAMULT_PUMP_OFFSET		0x51
133 #define AQUASTREAMULT_PUMP_VOLTAGE		0x3D
134 #define AQUASTREAMULT_PUMP_CURRENT		0x53
135 #define AQUASTREAMULT_PUMP_POWER		0x55
136 #define AQUASTREAMULT_FAN_OFFSET		0x41
137 #define AQUASTREAMULT_PRESSURE_OFFSET		0x57
138 #define AQUASTREAMULT_FLOW_SENSOR_OFFSET	0x37
139 #define AQUASTREAMULT_FAN_VOLTAGE_OFFSET	0x02
140 #define AQUASTREAMULT_FAN_CURRENT_OFFSET	0x00
141 #define AQUASTREAMULT_FAN_POWER_OFFSET		0x04
142 #define AQUASTREAMULT_FAN_SPEED_OFFSET		0x06
143 static u16 aquastreamult_sensor_fan_offsets[] = { AQUASTREAMULT_FAN_OFFSET };
144 
145 /* Spec and sensor report offset for the Farbwerk RGB controller */
146 #define FARBWERK_NUM_SENSORS		4
147 #define FARBWERK_SENSOR_START		0x2f
148 
149 /* Specs of the Farbwerk 360 RGB controller */
150 #define FARBWERK360_NUM_SENSORS			4
151 #define FARBWERK360_NUM_VIRTUAL_SENSORS		16
152 #define FARBWERK360_CTRL_REPORT_SIZE		0x682
153 
154 /* Sensor report offsets for the Farbwerk 360 */
155 #define FARBWERK360_SENSOR_START		0x32
156 #define FARBWERK360_VIRTUAL_SENSORS_START	0x3a
157 
158 /* Control report offsets for the Farbwerk 360 */
159 #define FARBWERK360_TEMP_CTRL_OFFSET		0x8
160 
161 /* Specs of the Octo fan controller */
162 #define OCTO_NUM_FANS			8
163 #define OCTO_NUM_SENSORS		4
164 #define OCTO_NUM_VIRTUAL_SENSORS	16
165 #define OCTO_CTRL_REPORT_SIZE		0x65F
166 
167 /* Sensor report offsets for the Octo */
168 #define OCTO_POWER_CYCLES		0x18
169 #define OCTO_SENSOR_START		0x3D
170 #define OCTO_VIRTUAL_SENSORS_START	0x45
171 static u16 octo_sensor_fan_offsets[] = { 0x7D, 0x8A, 0x97, 0xA4, 0xB1, 0xBE, 0xCB, 0xD8 };
172 
173 /* Control report offsets for the Octo */
174 #define OCTO_TEMP_CTRL_OFFSET		0xA
175 /* Fan speed offsets (0-100%) */
176 static u16 octo_ctrl_fan_offsets[] = { 0x5B, 0xB0, 0x105, 0x15A, 0x1AF, 0x204, 0x259, 0x2AE };
177 
178 /* Specs of Quadro fan controller */
179 #define QUADRO_NUM_FANS			4
180 #define QUADRO_NUM_SENSORS		4
181 #define QUADRO_NUM_VIRTUAL_SENSORS	16
182 #define QUADRO_NUM_FLOW_SENSORS		1
183 #define QUADRO_CTRL_REPORT_SIZE		0x3c1
184 
185 /* Sensor report offsets for the Quadro */
186 #define QUADRO_POWER_CYCLES		0x18
187 #define QUADRO_SENSOR_START		0x34
188 #define QUADRO_VIRTUAL_SENSORS_START	0x3c
189 #define QUADRO_FLOW_SENSOR_OFFSET	0x6e
190 static u16 quadro_sensor_fan_offsets[] = { 0x70, 0x7D, 0x8A, 0x97 };
191 
192 /* Control report offsets for the Quadro */
193 #define QUADRO_TEMP_CTRL_OFFSET		0xA
194 #define QUADRO_FLOW_PULSES_CTRL_OFFSET	0x6
195 static u16 quadro_ctrl_fan_offsets[] = { 0x37, 0x8c, 0xe1, 0x136 }; /* Fan speed offsets (0-100%) */
196 
197 /* Specs of High Flow Next flow sensor */
198 #define HIGHFLOWNEXT_NUM_SENSORS	2
199 #define HIGHFLOWNEXT_NUM_FLOW_SENSORS	1
200 
201 /* Sensor report offsets for the High Flow Next */
202 #define HIGHFLOWNEXT_SENSOR_START	85
203 #define HIGHFLOWNEXT_FLOW		81
204 #define HIGHFLOWNEXT_WATER_QUALITY	89
205 #define HIGHFLOWNEXT_POWER		91
206 #define HIGHFLOWNEXT_CONDUCTIVITY	95
207 #define HIGHFLOWNEXT_5V_VOLTAGE		97
208 #define HIGHFLOWNEXT_5V_VOLTAGE_USB	99
209 
210 /* Specs of the Poweradjust 3 */
211 #define POWERADJUST3_NUM_SENSORS	1
212 #define POWERADJUST3_SENSOR_REPORT_SIZE	0x32
213 
214 /* Sensor report offsets for the Poweradjust 3 */
215 #define POWERADJUST3_SENSOR_START	0x03
216 
217 /* Labels for D5 Next */
218 static const char *const label_d5next_temp[] = {
219 	"Coolant temp"
220 };
221 
222 static const char *const label_d5next_speeds[] = {
223 	"Pump speed",
224 	"Fan speed"
225 };
226 
227 static const char *const label_d5next_power[] = {
228 	"Pump power",
229 	"Fan power"
230 };
231 
232 static const char *const label_d5next_voltages[] = {
233 	"Pump voltage",
234 	"Fan voltage",
235 	"+5V voltage",
236 	"+12V voltage"
237 };
238 
239 static const char *const label_d5next_current[] = {
240 	"Pump current",
241 	"Fan current"
242 };
243 
244 /* Labels for Aquaero, Farbwerk, Farbwerk 360 and Octo and Quadro temperature sensors */
245 static const char *const label_temp_sensors[] = {
246 	"Sensor 1",
247 	"Sensor 2",
248 	"Sensor 3",
249 	"Sensor 4",
250 	"Sensor 5",
251 	"Sensor 6",
252 	"Sensor 7",
253 	"Sensor 8"
254 };
255 
256 static const char *const label_virtual_temp_sensors[] = {
257 	"Virtual sensor 1",
258 	"Virtual sensor 2",
259 	"Virtual sensor 3",
260 	"Virtual sensor 4",
261 	"Virtual sensor 5",
262 	"Virtual sensor 6",
263 	"Virtual sensor 7",
264 	"Virtual sensor 8",
265 	"Virtual sensor 9",
266 	"Virtual sensor 10",
267 	"Virtual sensor 11",
268 	"Virtual sensor 12",
269 	"Virtual sensor 13",
270 	"Virtual sensor 14",
271 	"Virtual sensor 15",
272 	"Virtual sensor 16",
273 };
274 
275 static const char *const label_aquaero_calc_temp_sensors[] = {
276 	"Calc. virtual sensor 1",
277 	"Calc. virtual sensor 2",
278 	"Calc. virtual sensor 3",
279 	"Calc. virtual sensor 4"
280 };
281 
282 /* Labels for Octo and Quadro (except speed) */
283 static const char *const label_fan_speed[] = {
284 	"Fan 1 speed",
285 	"Fan 2 speed",
286 	"Fan 3 speed",
287 	"Fan 4 speed",
288 	"Fan 5 speed",
289 	"Fan 6 speed",
290 	"Fan 7 speed",
291 	"Fan 8 speed"
292 };
293 
294 static const char *const label_fan_power[] = {
295 	"Fan 1 power",
296 	"Fan 2 power",
297 	"Fan 3 power",
298 	"Fan 4 power",
299 	"Fan 5 power",
300 	"Fan 6 power",
301 	"Fan 7 power",
302 	"Fan 8 power"
303 };
304 
305 static const char *const label_fan_voltage[] = {
306 	"Fan 1 voltage",
307 	"Fan 2 voltage",
308 	"Fan 3 voltage",
309 	"Fan 4 voltage",
310 	"Fan 5 voltage",
311 	"Fan 6 voltage",
312 	"Fan 7 voltage",
313 	"Fan 8 voltage"
314 };
315 
316 static const char *const label_fan_current[] = {
317 	"Fan 1 current",
318 	"Fan 2 current",
319 	"Fan 3 current",
320 	"Fan 4 current",
321 	"Fan 5 current",
322 	"Fan 6 current",
323 	"Fan 7 current",
324 	"Fan 8 current"
325 };
326 
327 /* Labels for Quadro fan speeds */
328 static const char *const label_quadro_speeds[] = {
329 	"Fan 1 speed",
330 	"Fan 2 speed",
331 	"Fan 3 speed",
332 	"Fan 4 speed",
333 	"Flow speed [dL/h]"
334 };
335 
336 /* Labels for Aquaero fan speeds */
337 static const char *const label_aquaero_speeds[] = {
338 	"Fan 1 speed",
339 	"Fan 2 speed",
340 	"Fan 3 speed",
341 	"Fan 4 speed",
342 	"Flow sensor 1 [dL/h]",
343 	"Flow sensor 2 [dL/h]"
344 };
345 
346 /* Labels for High Flow Next */
347 static const char *const label_highflownext_temp_sensors[] = {
348 	"Coolant temp",
349 	"External sensor"
350 };
351 
352 static const char *const label_highflownext_fan_speed[] = {
353 	"Flow [dL/h]",
354 	"Water quality [%]",
355 	"Conductivity [nS/cm]",
356 };
357 
358 static const char *const label_highflownext_power[] = {
359 	"Dissipated power",
360 };
361 
362 static const char *const label_highflownext_voltage[] = {
363 	"+5V voltage",
364 	"+5V USB voltage"
365 };
366 
367 /* Labels for Aquastream Ultimate */
368 static const char *const label_aquastreamult_temp[] = {
369 	"Coolant temp",
370 	"External temp"
371 };
372 
373 static const char *const label_aquastreamult_speeds[] = {
374 	"Fan speed",
375 	"Pump speed",
376 	"Pressure [mbar]",
377 	"Flow speed [dL/h]"
378 };
379 
380 static const char *const label_aquastreamult_power[] = {
381 	"Fan power",
382 	"Pump power"
383 };
384 
385 static const char *const label_aquastreamult_voltages[] = {
386 	"Fan voltage",
387 	"Pump voltage"
388 };
389 
390 static const char *const label_aquastreamult_current[] = {
391 	"Fan current",
392 	"Pump current"
393 };
394 
395 /* Labels for Poweradjust 3 */
396 static const char *const label_poweradjust3_temp_sensors[] = {
397 	"External sensor"
398 };
399 
400 struct aqc_fan_structure_offsets {
401 	u8 voltage;
402 	u8 curr;
403 	u8 power;
404 	u8 speed;
405 };
406 
407 /* Fan structure offsets for Aquaero */
408 static struct aqc_fan_structure_offsets aqc_aquaero_fan_structure = {
409 	.voltage = AQUAERO_FAN_VOLTAGE_OFFSET,
410 	.curr = AQUAERO_FAN_CURRENT_OFFSET,
411 	.power = AQUAERO_FAN_POWER_OFFSET,
412 	.speed = AQUAERO_FAN_SPEED_OFFSET
413 };
414 
415 /* Fan structure offsets for Aquastream Ultimate */
416 static struct aqc_fan_structure_offsets aqc_aquastreamult_fan_structure = {
417 	.voltage = AQUASTREAMULT_FAN_VOLTAGE_OFFSET,
418 	.curr = AQUASTREAMULT_FAN_CURRENT_OFFSET,
419 	.power = AQUASTREAMULT_FAN_POWER_OFFSET,
420 	.speed = AQUASTREAMULT_FAN_SPEED_OFFSET
421 };
422 
423 /* Fan structure offsets for all devices except those above */
424 static struct aqc_fan_structure_offsets aqc_general_fan_structure = {
425 	.voltage = AQC_FAN_VOLTAGE_OFFSET,
426 	.curr = AQC_FAN_CURRENT_OFFSET,
427 	.power = AQC_FAN_POWER_OFFSET,
428 	.speed = AQC_FAN_SPEED_OFFSET
429 };
430 
431 struct aqc_data {
432 	struct hid_device *hdev;
433 	struct device *hwmon_dev;
434 	struct dentry *debugfs;
435 	struct mutex mutex;	/* Used for locking access when reading and writing PWM values */
436 	enum kinds kind;
437 	const char *name;
438 
439 	int status_report_id;	/* Used for legacy devices, report is stored in buffer */
440 
441 	int buffer_size;
442 	u8 *buffer;
443 	int checksum_start;
444 	int checksum_length;
445 	int checksum_offset;
446 
447 	int num_fans;
448 	u16 *fan_sensor_offsets;
449 	u16 *fan_ctrl_offsets;
450 	int num_temp_sensors;
451 	int temp_sensor_start_offset;
452 	int num_virtual_temp_sensors;
453 	int virtual_temp_sensor_start_offset;
454 	int num_calc_virt_temp_sensors;
455 	int calc_virt_temp_sensor_start_offset;
456 	u16 temp_ctrl_offset;
457 	u16 power_cycle_count_offset;
458 	int num_flow_sensors;
459 	u8 flow_sensors_start_offset;
460 	u8 flow_pulses_ctrl_offset;
461 	struct aqc_fan_structure_offsets *fan_structure;
462 
463 	/* General info, same across all devices */
464 	u8 serial_number_start_offset;
465 	u32 serial_number[2];
466 	u8 firmware_version_offset;
467 	u16 firmware_version;
468 
469 	/* How many times the device was powered on, if available */
470 	u32 power_cycles;
471 
472 	/* Sensor values */
473 	s32 temp_input[20];	/* Max 4 physical and 16 virtual or 8 physical and 12 virtual */
474 	u16 speed_input[8];
475 	u32 power_input[8];
476 	u16 voltage_input[8];
477 	u16 current_input[8];
478 
479 	/* Label values */
480 	const char *const *temp_label;
481 	const char *const *virtual_temp_label;
482 	const char *const *calc_virt_temp_label;	/* For Aquaero */
483 	const char *const *speed_label;
484 	const char *const *power_label;
485 	const char *const *voltage_label;
486 	const char *const *current_label;
487 
488 	unsigned long updated;
489 };
490 
491 /* Converts from centi-percent */
492 static int aqc_percent_to_pwm(u16 val)
493 {
494 	return DIV_ROUND_CLOSEST(val * 255, 100 * 100);
495 }
496 
497 /* Converts to centi-percent */
498 static int aqc_pwm_to_percent(long val)
499 {
500 	if (val < 0 || val > 255)
501 		return -EINVAL;
502 
503 	return DIV_ROUND_CLOSEST(val * 100 * 100, 255);
504 }
505 
506 /* Expects the mutex to be locked */
507 static int aqc_get_ctrl_data(struct aqc_data *priv)
508 {
509 	int ret;
510 
511 	memset(priv->buffer, 0x00, priv->buffer_size);
512 	ret = hid_hw_raw_request(priv->hdev, CTRL_REPORT_ID, priv->buffer, priv->buffer_size,
513 				 HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
514 	if (ret < 0)
515 		ret = -ENODATA;
516 
517 	return ret;
518 }
519 
520 /* Expects the mutex to be locked */
521 static int aqc_send_ctrl_data(struct aqc_data *priv)
522 {
523 	int ret;
524 	u16 checksum;
525 
526 	/* Init and xorout value for CRC-16/USB is 0xffff */
527 	checksum = crc16(0xffff, priv->buffer + priv->checksum_start, priv->checksum_length);
528 	checksum ^= 0xffff;
529 
530 	/* Place the new checksum at the end of the report */
531 	put_unaligned_be16(checksum, priv->buffer + priv->checksum_offset);
532 
533 	/* Send the patched up report back to the device */
534 	ret = hid_hw_raw_request(priv->hdev, CTRL_REPORT_ID, priv->buffer, priv->buffer_size,
535 				 HID_FEATURE_REPORT, HID_REQ_SET_REPORT);
536 	if (ret < 0)
537 		return ret;
538 
539 	/* The official software sends this report after every change, so do it here as well */
540 	ret = hid_hw_raw_request(priv->hdev, SECONDARY_CTRL_REPORT_ID, secondary_ctrl_report,
541 				 SECONDARY_CTRL_REPORT_SIZE, HID_FEATURE_REPORT,
542 				 HID_REQ_SET_REPORT);
543 	return ret;
544 }
545 
546 /* Refreshes the control buffer and stores value at offset in val */
547 static int aqc_get_ctrl_val(struct aqc_data *priv, int offset, long *val)
548 {
549 	int ret;
550 
551 	mutex_lock(&priv->mutex);
552 
553 	ret = aqc_get_ctrl_data(priv);
554 	if (ret < 0)
555 		goto unlock_and_return;
556 
557 	*val = (s16)get_unaligned_be16(priv->buffer + offset);
558 
559 unlock_and_return:
560 	mutex_unlock(&priv->mutex);
561 	return ret;
562 }
563 
564 static int aqc_set_ctrl_val(struct aqc_data *priv, int offset, long val)
565 {
566 	int ret;
567 
568 	mutex_lock(&priv->mutex);
569 
570 	ret = aqc_get_ctrl_data(priv);
571 	if (ret < 0)
572 		goto unlock_and_return;
573 
574 	put_unaligned_be16((s16)val, priv->buffer + offset);
575 
576 	ret = aqc_send_ctrl_data(priv);
577 
578 unlock_and_return:
579 	mutex_unlock(&priv->mutex);
580 	return ret;
581 }
582 
583 static umode_t aqc_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
584 {
585 	const struct aqc_data *priv = data;
586 
587 	switch (type) {
588 	case hwmon_temp:
589 		if (channel < priv->num_temp_sensors) {
590 			switch (attr) {
591 			case hwmon_temp_label:
592 			case hwmon_temp_input:
593 				return 0444;
594 			case hwmon_temp_offset:
595 				if (priv->temp_ctrl_offset != 0)
596 					return 0644;
597 				break;
598 			default:
599 				break;
600 			}
601 		}
602 
603 		if (channel <
604 		    priv->num_temp_sensors + priv->num_virtual_temp_sensors +
605 		    priv->num_calc_virt_temp_sensors)
606 			switch (attr) {
607 			case hwmon_temp_label:
608 			case hwmon_temp_input:
609 				return 0444;
610 			default:
611 				break;
612 			}
613 		break;
614 	case hwmon_pwm:
615 		if (priv->fan_ctrl_offsets && channel < priv->num_fans) {
616 			switch (attr) {
617 			case hwmon_pwm_input:
618 				return 0644;
619 			default:
620 				break;
621 			}
622 		}
623 		break;
624 	case hwmon_fan:
625 		switch (attr) {
626 		case hwmon_fan_input:
627 		case hwmon_fan_label:
628 			switch (priv->kind) {
629 			case aquastreamult:
630 				/*
631 				 * Special case to support pump RPM, fan RPM,
632 				 * pressure and flow sensor
633 				 */
634 				if (channel < 4)
635 					return 0444;
636 				break;
637 			case highflownext:
638 				/* Special case to support flow sensor, water quality
639 				 * and conductivity
640 				 */
641 				if (channel < 3)
642 					return 0444;
643 				break;
644 			case aquaero:
645 			case quadro:
646 				/* Special case to support flow sensors */
647 				if (channel < priv->num_fans + priv->num_flow_sensors)
648 					return 0444;
649 				break;
650 			default:
651 				if (channel < priv->num_fans)
652 					return 0444;
653 				break;
654 			}
655 			break;
656 		case hwmon_fan_pulses:
657 			/* Special case for Quadro flow sensor */
658 			if (priv->kind == quadro && channel == priv->num_fans)
659 				return 0644;
660 			break;
661 		default:
662 			break;
663 		}
664 		break;
665 	case hwmon_power:
666 		switch (priv->kind) {
667 		case aquastreamult:
668 			/* Special case to support pump and fan power */
669 			if (channel < 2)
670 				return 0444;
671 			break;
672 		case highflownext:
673 			/* Special case to support one power sensor */
674 			if (channel == 0)
675 				return 0444;
676 			break;
677 		default:
678 			if (channel < priv->num_fans)
679 				return 0444;
680 			break;
681 		}
682 		break;
683 	case hwmon_curr:
684 		switch (priv->kind) {
685 		case aquastreamult:
686 			/* Special case to support pump and fan current */
687 			if (channel < 2)
688 				return 0444;
689 			break;
690 		default:
691 			if (channel < priv->num_fans)
692 				return 0444;
693 			break;
694 		}
695 		break;
696 	case hwmon_in:
697 		switch (priv->kind) {
698 		case d5next:
699 			/* Special case to support +5V and +12V voltage sensors */
700 			if (channel < priv->num_fans + 2)
701 				return 0444;
702 			break;
703 		case aquastreamult:
704 		case highflownext:
705 			/* Special case to support two voltage sensors */
706 			if (channel < 2)
707 				return 0444;
708 			break;
709 		default:
710 			if (channel < priv->num_fans)
711 				return 0444;
712 			break;
713 		}
714 		break;
715 	default:
716 		break;
717 	}
718 
719 	return 0;
720 }
721 
722 /* Read device sensors by manually requesting the sensor report (legacy way) */
723 static int aqc_legacy_read(struct aqc_data *priv)
724 {
725 	int ret, i, sensor_value;
726 
727 	mutex_lock(&priv->mutex);
728 
729 	memset(priv->buffer, 0x00, priv->buffer_size);
730 	ret = hid_hw_raw_request(priv->hdev, priv->status_report_id, priv->buffer,
731 				 priv->buffer_size, HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
732 	if (ret < 0)
733 		goto unlock_and_return;
734 
735 	/* Temperature sensor readings */
736 	for (i = 0; i < priv->num_temp_sensors; i++) {
737 		sensor_value = get_unaligned_le16(priv->buffer + priv->temp_sensor_start_offset +
738 						  i * AQC_SENSOR_SIZE);
739 		priv->temp_input[i] = sensor_value * 10;
740 	}
741 
742 	priv->updated = jiffies;
743 
744 unlock_and_return:
745 	mutex_unlock(&priv->mutex);
746 	return ret;
747 }
748 
749 static int aqc_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
750 		    int channel, long *val)
751 {
752 	int ret;
753 	struct aqc_data *priv = dev_get_drvdata(dev);
754 
755 	if (time_after(jiffies, priv->updated + STATUS_UPDATE_INTERVAL)) {
756 		if (priv->status_report_id != 0) {
757 			/* Legacy devices require manual reads */
758 			ret = aqc_legacy_read(priv);
759 			if (ret < 0)
760 				return -ENODATA;
761 		} else {
762 			return -ENODATA;
763 		}
764 	}
765 
766 	switch (type) {
767 	case hwmon_temp:
768 		switch (attr) {
769 		case hwmon_temp_input:
770 			if (priv->temp_input[channel] == -ENODATA)
771 				return -ENODATA;
772 
773 			*val = priv->temp_input[channel];
774 			break;
775 		case hwmon_temp_offset:
776 			ret =
777 			    aqc_get_ctrl_val(priv, priv->temp_ctrl_offset +
778 					     channel * AQC_SENSOR_SIZE, val);
779 			if (ret < 0)
780 				return ret;
781 
782 			*val *= 10;
783 			break;
784 		default:
785 			break;
786 		}
787 		break;
788 	case hwmon_fan:
789 		switch (attr) {
790 		case hwmon_fan_input:
791 			*val = priv->speed_input[channel];
792 			break;
793 		case hwmon_fan_pulses:
794 			ret = aqc_get_ctrl_val(priv, priv->flow_pulses_ctrl_offset, val);
795 			if (ret < 0)
796 				return ret;
797 			break;
798 		default:
799 			break;
800 		}
801 		break;
802 	case hwmon_power:
803 		*val = priv->power_input[channel];
804 		break;
805 	case hwmon_pwm:
806 		if (priv->fan_ctrl_offsets) {
807 			ret = aqc_get_ctrl_val(priv, priv->fan_ctrl_offsets[channel], val);
808 			if (ret < 0)
809 				return ret;
810 
811 			*val = aqc_percent_to_pwm(ret);
812 		}
813 		break;
814 	case hwmon_in:
815 		*val = priv->voltage_input[channel];
816 		break;
817 	case hwmon_curr:
818 		*val = priv->current_input[channel];
819 		break;
820 	default:
821 		return -EOPNOTSUPP;
822 	}
823 
824 	return 0;
825 }
826 
827 static int aqc_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
828 			   int channel, const char **str)
829 {
830 	struct aqc_data *priv = dev_get_drvdata(dev);
831 
832 	/* Number of sensors that are not calculated */
833 	int num_non_calc_sensors = priv->num_temp_sensors + priv->num_virtual_temp_sensors;
834 
835 	switch (type) {
836 	case hwmon_temp:
837 		if (channel < priv->num_temp_sensors) {
838 			*str = priv->temp_label[channel];
839 		} else {
840 			if (priv->kind == aquaero && channel >= num_non_calc_sensors)
841 				*str =
842 				    priv->calc_virt_temp_label[channel - num_non_calc_sensors];
843 			else
844 				*str = priv->virtual_temp_label[channel - priv->num_temp_sensors];
845 		}
846 		break;
847 	case hwmon_fan:
848 		*str = priv->speed_label[channel];
849 		break;
850 	case hwmon_power:
851 		*str = priv->power_label[channel];
852 		break;
853 	case hwmon_in:
854 		*str = priv->voltage_label[channel];
855 		break;
856 	case hwmon_curr:
857 		*str = priv->current_label[channel];
858 		break;
859 	default:
860 		return -EOPNOTSUPP;
861 	}
862 
863 	return 0;
864 }
865 
866 static int aqc_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel,
867 		     long val)
868 {
869 	int ret, pwm_value;
870 	struct aqc_data *priv = dev_get_drvdata(dev);
871 
872 	switch (type) {
873 	case hwmon_temp:
874 		switch (attr) {
875 		case hwmon_temp_offset:
876 			/* Limit temp offset to +/- 15K as in the official software */
877 			val = clamp_val(val, -15000, 15000) / 10;
878 			ret =
879 			    aqc_set_ctrl_val(priv, priv->temp_ctrl_offset +
880 					     channel * AQC_SENSOR_SIZE, val);
881 			if (ret < 0)
882 				return ret;
883 			break;
884 		default:
885 			return -EOPNOTSUPP;
886 		}
887 		break;
888 	case hwmon_fan:
889 		switch (attr) {
890 		case hwmon_fan_pulses:
891 			val = clamp_val(val, 10, 1000);
892 			ret = aqc_set_ctrl_val(priv, priv->flow_pulses_ctrl_offset, val);
893 			if (ret < 0)
894 				return ret;
895 			break;
896 		default:
897 			break;
898 		}
899 		break;
900 	case hwmon_pwm:
901 		switch (attr) {
902 		case hwmon_pwm_input:
903 			if (priv->fan_ctrl_offsets) {
904 				pwm_value = aqc_pwm_to_percent(val);
905 				if (pwm_value < 0)
906 					return pwm_value;
907 
908 				ret = aqc_set_ctrl_val(priv, priv->fan_ctrl_offsets[channel],
909 						       pwm_value);
910 				if (ret < 0)
911 					return ret;
912 			}
913 			break;
914 		default:
915 			break;
916 		}
917 		break;
918 	default:
919 		return -EOPNOTSUPP;
920 	}
921 
922 	return 0;
923 }
924 
925 static const struct hwmon_ops aqc_hwmon_ops = {
926 	.is_visible = aqc_is_visible,
927 	.read = aqc_read,
928 	.read_string = aqc_read_string,
929 	.write = aqc_write
930 };
931 
932 static const struct hwmon_channel_info *aqc_info[] = {
933 	HWMON_CHANNEL_INFO(temp,
934 			   HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_OFFSET,
935 			   HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_OFFSET,
936 			   HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_OFFSET,
937 			   HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_OFFSET,
938 			   HWMON_T_INPUT | HWMON_T_LABEL,
939 			   HWMON_T_INPUT | HWMON_T_LABEL,
940 			   HWMON_T_INPUT | HWMON_T_LABEL,
941 			   HWMON_T_INPUT | HWMON_T_LABEL,
942 			   HWMON_T_INPUT | HWMON_T_LABEL,
943 			   HWMON_T_INPUT | HWMON_T_LABEL,
944 			   HWMON_T_INPUT | HWMON_T_LABEL,
945 			   HWMON_T_INPUT | HWMON_T_LABEL,
946 			   HWMON_T_INPUT | HWMON_T_LABEL,
947 			   HWMON_T_INPUT | HWMON_T_LABEL,
948 			   HWMON_T_INPUT | HWMON_T_LABEL,
949 			   HWMON_T_INPUT | HWMON_T_LABEL,
950 			   HWMON_T_INPUT | HWMON_T_LABEL,
951 			   HWMON_T_INPUT | HWMON_T_LABEL,
952 			   HWMON_T_INPUT | HWMON_T_LABEL,
953 			   HWMON_T_INPUT | HWMON_T_LABEL),
954 	HWMON_CHANNEL_INFO(fan,
955 			   HWMON_F_INPUT | HWMON_F_LABEL,
956 			   HWMON_F_INPUT | HWMON_F_LABEL,
957 			   HWMON_F_INPUT | HWMON_F_LABEL,
958 			   HWMON_F_INPUT | HWMON_F_LABEL,
959 			   HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_PULSES,
960 			   HWMON_F_INPUT | HWMON_F_LABEL,
961 			   HWMON_F_INPUT | HWMON_F_LABEL,
962 			   HWMON_F_INPUT | HWMON_F_LABEL),
963 	HWMON_CHANNEL_INFO(power,
964 			   HWMON_P_INPUT | HWMON_P_LABEL,
965 			   HWMON_P_INPUT | HWMON_P_LABEL,
966 			   HWMON_P_INPUT | HWMON_P_LABEL,
967 			   HWMON_P_INPUT | HWMON_P_LABEL,
968 			   HWMON_P_INPUT | HWMON_P_LABEL,
969 			   HWMON_P_INPUT | HWMON_P_LABEL,
970 			   HWMON_P_INPUT | HWMON_P_LABEL,
971 			   HWMON_P_INPUT | HWMON_P_LABEL),
972 	HWMON_CHANNEL_INFO(pwm,
973 			   HWMON_PWM_INPUT,
974 			   HWMON_PWM_INPUT,
975 			   HWMON_PWM_INPUT,
976 			   HWMON_PWM_INPUT,
977 			   HWMON_PWM_INPUT,
978 			   HWMON_PWM_INPUT,
979 			   HWMON_PWM_INPUT,
980 			   HWMON_PWM_INPUT),
981 	HWMON_CHANNEL_INFO(in,
982 			   HWMON_I_INPUT | HWMON_I_LABEL,
983 			   HWMON_I_INPUT | HWMON_I_LABEL,
984 			   HWMON_I_INPUT | HWMON_I_LABEL,
985 			   HWMON_I_INPUT | HWMON_I_LABEL,
986 			   HWMON_I_INPUT | HWMON_I_LABEL,
987 			   HWMON_I_INPUT | HWMON_I_LABEL,
988 			   HWMON_I_INPUT | HWMON_I_LABEL,
989 			   HWMON_I_INPUT | HWMON_I_LABEL),
990 	HWMON_CHANNEL_INFO(curr,
991 			   HWMON_C_INPUT | HWMON_C_LABEL,
992 			   HWMON_C_INPUT | HWMON_C_LABEL,
993 			   HWMON_C_INPUT | HWMON_C_LABEL,
994 			   HWMON_C_INPUT | HWMON_C_LABEL,
995 			   HWMON_C_INPUT | HWMON_C_LABEL,
996 			   HWMON_C_INPUT | HWMON_C_LABEL,
997 			   HWMON_C_INPUT | HWMON_C_LABEL,
998 			   HWMON_C_INPUT | HWMON_C_LABEL),
999 	NULL
1000 };
1001 
1002 static const struct hwmon_chip_info aqc_chip_info = {
1003 	.ops = &aqc_hwmon_ops,
1004 	.info = aqc_info,
1005 };
1006 
1007 static int aqc_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
1008 {
1009 	int i, j, sensor_value;
1010 	struct aqc_data *priv;
1011 
1012 	if (report->id != STATUS_REPORT_ID)
1013 		return 0;
1014 
1015 	priv = hid_get_drvdata(hdev);
1016 
1017 	/* Info provided with every report */
1018 	priv->serial_number[0] = get_unaligned_be16(data + priv->serial_number_start_offset);
1019 	priv->serial_number[1] = get_unaligned_be16(data + priv->serial_number_start_offset +
1020 						    SERIAL_PART_OFFSET);
1021 	priv->firmware_version = get_unaligned_be16(data + priv->firmware_version_offset);
1022 
1023 	/* Physical temperature sensor readings */
1024 	for (i = 0; i < priv->num_temp_sensors; i++) {
1025 		sensor_value = get_unaligned_be16(data +
1026 						  priv->temp_sensor_start_offset +
1027 						  i * AQC_SENSOR_SIZE);
1028 		if (sensor_value == AQC_TEMP_SENSOR_DISCONNECTED)
1029 			priv->temp_input[i] = -ENODATA;
1030 		else
1031 			priv->temp_input[i] = sensor_value * 10;
1032 	}
1033 
1034 	/* Virtual temperature sensor readings */
1035 	for (j = 0; j < priv->num_virtual_temp_sensors; j++) {
1036 		sensor_value = get_unaligned_be16(data +
1037 						  priv->virtual_temp_sensor_start_offset +
1038 						  j * AQC_SENSOR_SIZE);
1039 		if (sensor_value == AQC_TEMP_SENSOR_DISCONNECTED)
1040 			priv->temp_input[i] = -ENODATA;
1041 		else
1042 			priv->temp_input[i] = sensor_value * 10;
1043 		i++;
1044 	}
1045 
1046 	/* Fan speed and related readings */
1047 	for (i = 0; i < priv->num_fans; i++) {
1048 		priv->speed_input[i] =
1049 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
1050 				       priv->fan_structure->speed);
1051 		priv->power_input[i] =
1052 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
1053 				       priv->fan_structure->power) * 10000;
1054 		priv->voltage_input[i] =
1055 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
1056 				       priv->fan_structure->voltage) * 10;
1057 		priv->current_input[i] =
1058 		    get_unaligned_be16(data + priv->fan_sensor_offsets[i] +
1059 				       priv->fan_structure->curr);
1060 	}
1061 
1062 	/* Flow sensor readings */
1063 	for (j = 0; j < priv->num_flow_sensors; j++) {
1064 		priv->speed_input[i] = get_unaligned_be16(data + priv->flow_sensors_start_offset +
1065 							  j * AQC_SENSOR_SIZE);
1066 		i++;
1067 	}
1068 
1069 	if (priv->power_cycle_count_offset != 0)
1070 		priv->power_cycles = get_unaligned_be32(data + priv->power_cycle_count_offset);
1071 
1072 	/* Special-case sensor readings */
1073 	switch (priv->kind) {
1074 	case aquaero:
1075 		/* Read calculated virtual temp sensors */
1076 		i = priv->num_temp_sensors + priv->num_virtual_temp_sensors;
1077 		for (j = 0; j < priv->num_calc_virt_temp_sensors; j++) {
1078 			sensor_value = get_unaligned_be16(data +
1079 					priv->calc_virt_temp_sensor_start_offset +
1080 					j * AQC_SENSOR_SIZE);
1081 			if (sensor_value == AQC_TEMP_SENSOR_DISCONNECTED)
1082 				priv->temp_input[i] = -ENODATA;
1083 			else
1084 				priv->temp_input[i] = sensor_value * 10;
1085 			i++;
1086 		}
1087 		break;
1088 	case aquastreamult:
1089 		priv->speed_input[1] = get_unaligned_be16(data + AQUASTREAMULT_PUMP_OFFSET);
1090 		priv->speed_input[2] = get_unaligned_be16(data + AQUASTREAMULT_PRESSURE_OFFSET);
1091 		priv->speed_input[3] = get_unaligned_be16(data + AQUASTREAMULT_FLOW_SENSOR_OFFSET);
1092 
1093 		priv->power_input[1] = get_unaligned_be16(data + AQUASTREAMULT_PUMP_POWER) * 10000;
1094 
1095 		priv->voltage_input[1] = get_unaligned_be16(data + AQUASTREAMULT_PUMP_VOLTAGE) * 10;
1096 
1097 		priv->current_input[1] = get_unaligned_be16(data + AQUASTREAMULT_PUMP_CURRENT);
1098 		break;
1099 	case d5next:
1100 		priv->voltage_input[2] = get_unaligned_be16(data + D5NEXT_5V_VOLTAGE) * 10;
1101 		priv->voltage_input[3] = get_unaligned_be16(data + D5NEXT_12V_VOLTAGE) * 10;
1102 		break;
1103 	case highflownext:
1104 		/* If external temp sensor is not connected, its power reading is also N/A */
1105 		if (priv->temp_input[1] == -ENODATA)
1106 			priv->power_input[0] = -ENODATA;
1107 		else
1108 			priv->power_input[0] =
1109 			    get_unaligned_be16(data + HIGHFLOWNEXT_POWER) * 1000000;
1110 
1111 		priv->voltage_input[0] = get_unaligned_be16(data + HIGHFLOWNEXT_5V_VOLTAGE) * 10;
1112 		priv->voltage_input[1] =
1113 		    get_unaligned_be16(data + HIGHFLOWNEXT_5V_VOLTAGE_USB) * 10;
1114 
1115 		priv->speed_input[1] = get_unaligned_be16(data + HIGHFLOWNEXT_WATER_QUALITY);
1116 		priv->speed_input[2] = get_unaligned_be16(data + HIGHFLOWNEXT_CONDUCTIVITY);
1117 		break;
1118 	default:
1119 		break;
1120 	}
1121 
1122 	priv->updated = jiffies;
1123 
1124 	return 0;
1125 }
1126 
1127 #ifdef CONFIG_DEBUG_FS
1128 
1129 static int serial_number_show(struct seq_file *seqf, void *unused)
1130 {
1131 	struct aqc_data *priv = seqf->private;
1132 
1133 	seq_printf(seqf, "%05u-%05u\n", priv->serial_number[0], priv->serial_number[1]);
1134 
1135 	return 0;
1136 }
1137 DEFINE_SHOW_ATTRIBUTE(serial_number);
1138 
1139 static int firmware_version_show(struct seq_file *seqf, void *unused)
1140 {
1141 	struct aqc_data *priv = seqf->private;
1142 
1143 	seq_printf(seqf, "%u\n", priv->firmware_version);
1144 
1145 	return 0;
1146 }
1147 DEFINE_SHOW_ATTRIBUTE(firmware_version);
1148 
1149 static int power_cycles_show(struct seq_file *seqf, void *unused)
1150 {
1151 	struct aqc_data *priv = seqf->private;
1152 
1153 	seq_printf(seqf, "%u\n", priv->power_cycles);
1154 
1155 	return 0;
1156 }
1157 DEFINE_SHOW_ATTRIBUTE(power_cycles);
1158 
1159 static void aqc_debugfs_init(struct aqc_data *priv)
1160 {
1161 	char name[64];
1162 
1163 	scnprintf(name, sizeof(name), "%s_%s-%s", "aquacomputer", priv->name,
1164 		  dev_name(&priv->hdev->dev));
1165 
1166 	priv->debugfs = debugfs_create_dir(name, NULL);
1167 
1168 	if (priv->serial_number_start_offset != 0)
1169 		debugfs_create_file("serial_number", 0444, priv->debugfs, priv,
1170 				    &serial_number_fops);
1171 	if (priv->firmware_version_offset != 0)
1172 		debugfs_create_file("firmware_version", 0444, priv->debugfs, priv,
1173 				    &firmware_version_fops);
1174 	if (priv->power_cycle_count_offset != 0)
1175 		debugfs_create_file("power_cycles", 0444, priv->debugfs, priv, &power_cycles_fops);
1176 }
1177 
1178 #else
1179 
1180 static void aqc_debugfs_init(struct aqc_data *priv)
1181 {
1182 }
1183 
1184 #endif
1185 
1186 static int aqc_probe(struct hid_device *hdev, const struct hid_device_id *id)
1187 {
1188 	struct aqc_data *priv;
1189 	int ret;
1190 
1191 	priv = devm_kzalloc(&hdev->dev, sizeof(*priv), GFP_KERNEL);
1192 	if (!priv)
1193 		return -ENOMEM;
1194 
1195 	priv->hdev = hdev;
1196 	hid_set_drvdata(hdev, priv);
1197 
1198 	priv->updated = jiffies - STATUS_UPDATE_INTERVAL;
1199 
1200 	ret = hid_parse(hdev);
1201 	if (ret)
1202 		return ret;
1203 
1204 	ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
1205 	if (ret)
1206 		return ret;
1207 
1208 	ret = hid_hw_open(hdev);
1209 	if (ret)
1210 		goto fail_and_stop;
1211 
1212 	switch (hdev->product) {
1213 	case USB_PRODUCT_ID_AQUAERO:
1214 		/*
1215 		 * Aquaero presents itself as three HID devices under the same product ID:
1216 		 * "aquaero keyboard/mouse", "aquaero System Control" and "aquaero Device",
1217 		 * which is the one we want to communicate with. Unlike most other Aquacomputer
1218 		 * devices, Aquaero does not return meaningful data when explicitly requested
1219 		 * using GET_FEATURE_REPORT.
1220 		 *
1221 		 * The difference between "aquaero Device" and the other two is in the collections
1222 		 * they present. The two other devices have the type of the second element in
1223 		 * their respective collections set to 1, while the real device has it set to 0.
1224 		 */
1225 		if (hdev->collection[1].type != 0) {
1226 			ret = -ENODEV;
1227 			goto fail_and_close;
1228 		}
1229 
1230 		priv->kind = aquaero;
1231 
1232 		priv->num_fans = AQUAERO_NUM_FANS;
1233 		priv->fan_sensor_offsets = aquaero_sensor_fan_offsets;
1234 
1235 		priv->num_temp_sensors = AQUAERO_NUM_SENSORS;
1236 		priv->temp_sensor_start_offset = AQUAERO_SENSOR_START;
1237 		priv->num_virtual_temp_sensors = AQUAERO_NUM_VIRTUAL_SENSORS;
1238 		priv->virtual_temp_sensor_start_offset = AQUAERO_VIRTUAL_SENSOR_START;
1239 		priv->num_calc_virt_temp_sensors = AQUAERO_NUM_CALC_VIRTUAL_SENSORS;
1240 		priv->calc_virt_temp_sensor_start_offset = AQUAERO_CALC_VIRTUAL_SENSOR_START;
1241 		priv->num_flow_sensors = AQUAERO_NUM_FLOW_SENSORS;
1242 		priv->flow_sensors_start_offset = AQUAERO_FLOW_SENSORS_START;
1243 
1244 		priv->temp_label = label_temp_sensors;
1245 		priv->virtual_temp_label = label_virtual_temp_sensors;
1246 		priv->calc_virt_temp_label = label_aquaero_calc_temp_sensors;
1247 		priv->speed_label = label_aquaero_speeds;
1248 		priv->power_label = label_fan_power;
1249 		priv->voltage_label = label_fan_voltage;
1250 		priv->current_label = label_fan_current;
1251 		break;
1252 	case USB_PRODUCT_ID_D5NEXT:
1253 		priv->kind = d5next;
1254 
1255 		priv->num_fans = D5NEXT_NUM_FANS;
1256 		priv->fan_sensor_offsets = d5next_sensor_fan_offsets;
1257 		priv->fan_ctrl_offsets = d5next_ctrl_fan_offsets;
1258 
1259 		priv->num_temp_sensors = D5NEXT_NUM_SENSORS;
1260 		priv->temp_sensor_start_offset = D5NEXT_COOLANT_TEMP;
1261 		priv->num_virtual_temp_sensors = D5NEXT_NUM_VIRTUAL_SENSORS;
1262 		priv->virtual_temp_sensor_start_offset = D5NEXT_VIRTUAL_SENSORS_START;
1263 		priv->temp_ctrl_offset = D5NEXT_TEMP_CTRL_OFFSET;
1264 
1265 		priv->buffer_size = D5NEXT_CTRL_REPORT_SIZE;
1266 
1267 		priv->power_cycle_count_offset = D5NEXT_POWER_CYCLES;
1268 
1269 		priv->temp_label = label_d5next_temp;
1270 		priv->virtual_temp_label = label_virtual_temp_sensors;
1271 		priv->speed_label = label_d5next_speeds;
1272 		priv->power_label = label_d5next_power;
1273 		priv->voltage_label = label_d5next_voltages;
1274 		priv->current_label = label_d5next_current;
1275 		break;
1276 	case USB_PRODUCT_ID_FARBWERK:
1277 		priv->kind = farbwerk;
1278 
1279 		priv->num_fans = 0;
1280 
1281 		priv->num_temp_sensors = FARBWERK_NUM_SENSORS;
1282 		priv->temp_sensor_start_offset = FARBWERK_SENSOR_START;
1283 
1284 		priv->temp_label = label_temp_sensors;
1285 		break;
1286 	case USB_PRODUCT_ID_FARBWERK360:
1287 		priv->kind = farbwerk360;
1288 
1289 		priv->num_fans = 0;
1290 
1291 		priv->num_temp_sensors = FARBWERK360_NUM_SENSORS;
1292 		priv->temp_sensor_start_offset = FARBWERK360_SENSOR_START;
1293 		priv->num_virtual_temp_sensors = FARBWERK360_NUM_VIRTUAL_SENSORS;
1294 		priv->virtual_temp_sensor_start_offset = FARBWERK360_VIRTUAL_SENSORS_START;
1295 		priv->temp_ctrl_offset = FARBWERK360_TEMP_CTRL_OFFSET;
1296 
1297 		priv->buffer_size = FARBWERK360_CTRL_REPORT_SIZE;
1298 
1299 		priv->temp_label = label_temp_sensors;
1300 		priv->virtual_temp_label = label_virtual_temp_sensors;
1301 		break;
1302 	case USB_PRODUCT_ID_OCTO:
1303 		priv->kind = octo;
1304 
1305 		priv->num_fans = OCTO_NUM_FANS;
1306 		priv->fan_sensor_offsets = octo_sensor_fan_offsets;
1307 		priv->fan_ctrl_offsets = octo_ctrl_fan_offsets;
1308 
1309 		priv->num_temp_sensors = OCTO_NUM_SENSORS;
1310 		priv->temp_sensor_start_offset = OCTO_SENSOR_START;
1311 		priv->num_virtual_temp_sensors = OCTO_NUM_VIRTUAL_SENSORS;
1312 		priv->virtual_temp_sensor_start_offset = OCTO_VIRTUAL_SENSORS_START;
1313 		priv->temp_ctrl_offset = OCTO_TEMP_CTRL_OFFSET;
1314 
1315 		priv->buffer_size = OCTO_CTRL_REPORT_SIZE;
1316 
1317 		priv->power_cycle_count_offset = OCTO_POWER_CYCLES;
1318 
1319 		priv->temp_label = label_temp_sensors;
1320 		priv->virtual_temp_label = label_virtual_temp_sensors;
1321 		priv->speed_label = label_fan_speed;
1322 		priv->power_label = label_fan_power;
1323 		priv->voltage_label = label_fan_voltage;
1324 		priv->current_label = label_fan_current;
1325 		break;
1326 	case USB_PRODUCT_ID_QUADRO:
1327 		priv->kind = quadro;
1328 
1329 		priv->num_fans = QUADRO_NUM_FANS;
1330 		priv->fan_sensor_offsets = quadro_sensor_fan_offsets;
1331 		priv->fan_ctrl_offsets = quadro_ctrl_fan_offsets;
1332 
1333 		priv->num_temp_sensors = QUADRO_NUM_SENSORS;
1334 		priv->temp_sensor_start_offset = QUADRO_SENSOR_START;
1335 		priv->num_virtual_temp_sensors = QUADRO_NUM_VIRTUAL_SENSORS;
1336 		priv->virtual_temp_sensor_start_offset = QUADRO_VIRTUAL_SENSORS_START;
1337 		priv->num_flow_sensors = QUADRO_NUM_FLOW_SENSORS;
1338 		priv->flow_sensors_start_offset = QUADRO_FLOW_SENSOR_OFFSET;
1339 
1340 		priv->temp_ctrl_offset = QUADRO_TEMP_CTRL_OFFSET;
1341 
1342 		priv->buffer_size = QUADRO_CTRL_REPORT_SIZE;
1343 
1344 		priv->flow_pulses_ctrl_offset = QUADRO_FLOW_PULSES_CTRL_OFFSET;
1345 		priv->power_cycle_count_offset = QUADRO_POWER_CYCLES;
1346 
1347 		priv->temp_label = label_temp_sensors;
1348 		priv->virtual_temp_label = label_virtual_temp_sensors;
1349 		priv->speed_label = label_quadro_speeds;
1350 		priv->power_label = label_fan_power;
1351 		priv->voltage_label = label_fan_voltage;
1352 		priv->current_label = label_fan_current;
1353 		break;
1354 	case USB_PRODUCT_ID_HIGHFLOWNEXT:
1355 		priv->kind = highflownext;
1356 
1357 		priv->num_fans = 0;
1358 
1359 		priv->num_temp_sensors = HIGHFLOWNEXT_NUM_SENSORS;
1360 		priv->temp_sensor_start_offset = HIGHFLOWNEXT_SENSOR_START;
1361 		priv->num_flow_sensors = HIGHFLOWNEXT_NUM_FLOW_SENSORS;
1362 		priv->flow_sensors_start_offset = HIGHFLOWNEXT_FLOW;
1363 
1364 		priv->power_cycle_count_offset = QUADRO_POWER_CYCLES;
1365 
1366 		priv->temp_label = label_highflownext_temp_sensors;
1367 		priv->speed_label = label_highflownext_fan_speed;
1368 		priv->power_label = label_highflownext_power;
1369 		priv->voltage_label = label_highflownext_voltage;
1370 		break;
1371 	case USB_PRODUCT_ID_AQUASTREAMULT:
1372 		priv->kind = aquastreamult;
1373 
1374 		priv->num_fans = AQUASTREAMULT_NUM_FANS;
1375 		priv->fan_sensor_offsets = aquastreamult_sensor_fan_offsets;
1376 
1377 		priv->num_temp_sensors = AQUASTREAMULT_NUM_SENSORS;
1378 		priv->temp_sensor_start_offset = AQUASTREAMULT_SENSOR_START;
1379 
1380 		priv->temp_label = label_aquastreamult_temp;
1381 		priv->speed_label = label_aquastreamult_speeds;
1382 		priv->power_label = label_aquastreamult_power;
1383 		priv->voltage_label = label_aquastreamult_voltages;
1384 		priv->current_label = label_aquastreamult_current;
1385 		break;
1386 	case USB_PRODUCT_ID_POWERADJUST3:
1387 		priv->kind = poweradjust3;
1388 
1389 		priv->num_fans = 0;
1390 
1391 		priv->num_temp_sensors = POWERADJUST3_NUM_SENSORS;
1392 		priv->temp_sensor_start_offset = POWERADJUST3_SENSOR_START;
1393 		priv->buffer_size = POWERADJUST3_SENSOR_REPORT_SIZE;
1394 
1395 		priv->temp_label = label_poweradjust3_temp_sensors;
1396 		break;
1397 	default:
1398 		break;
1399 	}
1400 
1401 	switch (priv->kind) {
1402 	case aquaero:
1403 		priv->serial_number_start_offset = AQUAERO_SERIAL_START;
1404 		priv->firmware_version_offset = AQUAERO_FIRMWARE_VERSION;
1405 
1406 		priv->fan_structure = &aqc_aquaero_fan_structure;
1407 		break;
1408 	case poweradjust3:
1409 		priv->status_report_id = POWERADJUST3_STATUS_REPORT_ID;
1410 		break;
1411 	default:
1412 		priv->serial_number_start_offset = AQC_SERIAL_START;
1413 		priv->firmware_version_offset = AQC_FIRMWARE_VERSION;
1414 
1415 		if (priv->kind == aquastreamult)
1416 			priv->fan_structure = &aqc_aquastreamult_fan_structure;
1417 		else
1418 			priv->fan_structure = &aqc_general_fan_structure;
1419 		break;
1420 	}
1421 
1422 	if (priv->buffer_size != 0) {
1423 		priv->checksum_start = 0x01;
1424 		priv->checksum_length = priv->buffer_size - 3;
1425 		priv->checksum_offset = priv->buffer_size - 2;
1426 	}
1427 
1428 	priv->name = aqc_device_names[priv->kind];
1429 
1430 	priv->buffer = devm_kzalloc(&hdev->dev, priv->buffer_size, GFP_KERNEL);
1431 	if (!priv->buffer) {
1432 		ret = -ENOMEM;
1433 		goto fail_and_close;
1434 	}
1435 
1436 	mutex_init(&priv->mutex);
1437 
1438 	priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, priv->name, priv,
1439 							  &aqc_chip_info, NULL);
1440 
1441 	if (IS_ERR(priv->hwmon_dev)) {
1442 		ret = PTR_ERR(priv->hwmon_dev);
1443 		goto fail_and_close;
1444 	}
1445 
1446 	aqc_debugfs_init(priv);
1447 
1448 	return 0;
1449 
1450 fail_and_close:
1451 	hid_hw_close(hdev);
1452 fail_and_stop:
1453 	hid_hw_stop(hdev);
1454 	return ret;
1455 }
1456 
1457 static void aqc_remove(struct hid_device *hdev)
1458 {
1459 	struct aqc_data *priv = hid_get_drvdata(hdev);
1460 
1461 	debugfs_remove_recursive(priv->debugfs);
1462 	hwmon_device_unregister(priv->hwmon_dev);
1463 
1464 	hid_hw_close(hdev);
1465 	hid_hw_stop(hdev);
1466 }
1467 
1468 static const struct hid_device_id aqc_table[] = {
1469 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_AQUAERO) },
1470 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_D5NEXT) },
1471 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_FARBWERK) },
1472 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_FARBWERK360) },
1473 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_OCTO) },
1474 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_QUADRO) },
1475 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_HIGHFLOWNEXT) },
1476 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_AQUASTREAMULT) },
1477 	{ HID_USB_DEVICE(USB_VENDOR_ID_AQUACOMPUTER, USB_PRODUCT_ID_POWERADJUST3) },
1478 	{ }
1479 };
1480 
1481 MODULE_DEVICE_TABLE(hid, aqc_table);
1482 
1483 static struct hid_driver aqc_driver = {
1484 	.name = DRIVER_NAME,
1485 	.id_table = aqc_table,
1486 	.probe = aqc_probe,
1487 	.remove = aqc_remove,
1488 	.raw_event = aqc_raw_event,
1489 };
1490 
1491 static int __init aqc_init(void)
1492 {
1493 	return hid_register_driver(&aqc_driver);
1494 }
1495 
1496 static void __exit aqc_exit(void)
1497 {
1498 	hid_unregister_driver(&aqc_driver);
1499 }
1500 
1501 /* Request to initialize after the HID bus to ensure it's not being loaded before */
1502 late_initcall(aqc_init);
1503 module_exit(aqc_exit);
1504 
1505 MODULE_LICENSE("GPL");
1506 MODULE_AUTHOR("Aleksa Savic <savicaleksa83@gmail.com>");
1507 MODULE_AUTHOR("Jack Doan <me@jackdoan.com>");
1508 MODULE_DESCRIPTION("Hwmon driver for Aquacomputer devices");
1509