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