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