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