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