| 1 | |
| 2 | |
| 3 | * corsair-cpro.c - Linux driver for Corsair Commander Pro
|
| 4 | * Copyright (C) 2020 Marius Zachmann <[email protected]>
|
| 5 | *
|
| 6 | * This driver uses hid reports to communicate with the device to allow hidraw userspace drivers
|
| 7 | * still being used. The device does not use report ids. When using hidraw and this driver
|
| 8 | * simultaniously, reports could be switched.
|
| 9 |
|
| 10 |
|
| 11 | #include <linux/bitops.h>
|
| 12 | #include <linux/completion.h>
|
| 13 | #include <linux/debugfs.h>
|
| 14 | #include <linux/hid.h>
|
| 15 | #include <linux/hwmon.h>
|
| 16 | #include <linux/kernel.h>
|
| 17 | #include <linux/module.h>
|
| 18 | #include <linux/mutex.h>
|
| 19 | #include <linux/seq_file.h>
|
| 20 | #include <linux/slab.h>
|
| 21 | #include <linux/spinlock.h>
|
| 22 | #include <linux/types.h>
|
| 23 |
|
| 24 | #define USB_VENDOR_ID_CORSAIR 0x1b1c
|
| 25 | #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
|
| 26 | #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
|
| 27 |
|
| 28 | #define OUT_BUFFER_SIZE 63
|
| 29 | #define IN_BUFFER_SIZE 16
|
| 30 | #define LABEL_LENGTH 11
|
| 31 | #define REQ_TIMEOUT 300
|
| 32 |
|
| 33 | #define CTL_GET_FW_VER 0x02
|
| 34 | #define CTL_GET_BL_VER 0x06
|
| 35 | #define CTL_GET_TMP_CNCT 0x10 |
| 36 | * returns in bytes 1-4 for each temp sensor:
|
| 37 | * 0 not connected
|
| 38 | * 1 connected
|
| 39 |
|
| 40 | #define CTL_GET_TMP 0x11 |
| 41 | * send: byte 1 is channel, rest zero
|
| 42 | * rcv: returns temp for channel in centi-degree celsius
|
| 43 | * in bytes 1 and 2 as a two's complement value
|
| 44 | * returns 0x11 in byte 0 if no sensor is connected
|
| 45 |
|
| 46 | #define CTL_GET_VOLT 0x12 |
| 47 | * send: byte 1 is rail number: 0 = 12v, 1 = 5v, 2 = 3.3v
|
| 48 | * rcv: returns millivolt in bytes 1,2
|
| 49 | * returns error 0x10 if request is invalid
|
| 50 |
|
| 51 | #define CTL_GET_FAN_CNCT 0x20 |
| 52 | * returns in bytes 1-6 for each fan:
|
| 53 | * 0 not connected
|
| 54 | * 1 3pin
|
| 55 | * 2 4pin
|
| 56 |
|
| 57 | #define CTL_GET_FAN_RPM 0x21 |
| 58 | * send: byte 1 is channel, rest zero
|
| 59 | * rcv: returns rpm in bytes 1,2
|
| 60 |
|
| 61 | #define CTL_GET_FAN_PWM 0x22 |
| 62 | * send: byte 1 is channel, rest zero
|
| 63 | * rcv: returns pwm in byte 1 if it was set
|
| 64 | * returns error 0x12 if fan is controlled via
|
| 65 | * fan_target or fan curve
|
| 66 |
|
| 67 | #define CTL_SET_FAN_FPWM 0x23 |
| 68 | * set fixed pwm
|
| 69 | * send: byte 1 is fan number
|
| 70 | * send: byte 2 is percentage from 0 - 100
|
| 71 |
|
| 72 | #define CTL_SET_FAN_TARGET 0x24 |
| 73 | * set target rpm
|
| 74 | * send: byte 1 is fan number
|
| 75 | * send: byte 2-3 is target
|
| 76 | * device accepts all values from 0x00 - 0xFFFF
|
| 77 |
|
| 78 |
|
| 79 | #define NUM_FANS 6
|
| 80 | #define NUM_TEMP_SENSORS 4
|
| 81 |
|
| 82 | struct ccp_device {
|
| 83 | struct hid_device *hdev;
|
| 84 | struct device *hwmon_dev;
|
| 85 | struct dentry *debugfs;
|
| 86 |
|
| 87 | spinlock_t wait_input_report_lock;
|
| 88 | struct completion wait_input_report;
|
| 89 | struct mutex mutex;
|
| 90 | u8 *cmd_buffer;
|
| 91 | u8 *buffer;
|
| 92 | int buffer_recv_size;
|
| 93 | int target[NUM_FANS];
|
| 94 | DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
|
| 95 | DECLARE_BITMAP(fan_cnct, NUM_FANS);
|
| 96 | char fan_label[NUM_FANS][LABEL_LENGTH];
|
| 97 | u8 firmware_ver[3];
|
| 98 | u8 bootloader_ver[2];
|
| 99 | };
|
| 100 |
|
| 101 |
|
| 102 | static int ccp_get_errno(struct ccp_device *ccp)
|
| 103 | {
|
| 104 | switch (ccp->buffer[0]) {
|
| 105 | case 0x00:
|
| 106 | return 0;
|
| 107 | case 0x01:
|
| 108 | return -EOPNOTSUPP;
|
| 109 | case 0x10:
|
| 110 | return -EINVAL;
|
| 111 | case 0x11:
|
| 112 | case 0x12:
|
| 113 | return -ENODATA;
|
| 114 | default:
|
| 115 | hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
|
| 116 | return -EIO;
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 |
|
| 121 | static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
|
| 122 | {
|
| 123 | unsigned long t;
|
| 124 | int ret;
|
| 125 |
|
| 126 | memset(ccp->cmd_buffer, 0x00, OUT_BUFFER_SIZE);
|
| 127 | ccp->cmd_buffer[0] = command;
|
| 128 | ccp->cmd_buffer[1] = byte1;
|
| 129 | ccp->cmd_buffer[2] = byte2;
|
| 130 | ccp->cmd_buffer[3] = byte3;
|
| 131 |
|
| 132 | |
| 133 | * Disable raw event parsing for a moment to safely reinitialize the
|
| 134 | * completion. Reinit is done because hidraw could have triggered
|
| 135 | * the raw event parsing and marked the ccp->wait_input_report
|
| 136 | * completion as done.
|
| 137 |
|
| 138 | spin_lock_bh(&ccp->wait_input_report_lock);
|
| 139 | reinit_completion(&ccp->wait_input_report);
|
| 140 | spin_unlock_bh(&ccp->wait_input_report_lock);
|
| 141 |
|
| 142 | ret = hid_hw_output_report(ccp->hdev, ccp->cmd_buffer, OUT_BUFFER_SIZE);
|
| 143 | if (ret < 0)
|
| 144 | return ret;
|
| 145 |
|
| 146 | t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
|
| 147 | if (!t)
|
| 148 | return -ETIMEDOUT;
|
| 149 |
|
| 150 | if (ccp->buffer_recv_size != IN_BUFFER_SIZE)
|
| 151 | return -EPROTO;
|
| 152 |
|
| 153 | return ccp_get_errno(ccp);
|
| 154 | }
|
| 155 |
|
| 156 | static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
|
| 157 | {
|
| 158 | struct ccp_device *ccp = hid_get_drvdata(hdev);
|
| 159 |
|
| 160 |
|
| 161 | spin_lock(&ccp->wait_input_report_lock);
|
| 162 | if (!completion_done(&ccp->wait_input_report)) {
|
| 163 | memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
|
| 164 | ccp->buffer_recv_size = size;
|
| 165 | complete_all(&ccp->wait_input_report);
|
| 166 | }
|
| 167 | spin_unlock(&ccp->wait_input_report_lock);
|
| 168 |
|
| 169 | return 0;
|
| 170 | }
|
| 171 |
|
| 172 |
|
| 173 | static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
|
| 174 | {
|
| 175 | int ret;
|
| 176 |
|
| 177 | mutex_lock(&ccp->mutex);
|
| 178 |
|
| 179 | ret = send_usb_cmd(ccp, command, channel, 0, 0);
|
| 180 | if (ret)
|
| 181 | goto out_unlock;
|
| 182 |
|
| 183 | ret = ccp->buffer[1];
|
| 184 | if (two_byte_data)
|
| 185 | ret = (ret << 8) + ccp->buffer[2];
|
| 186 |
|
| 187 | out_unlock:
|
| 188 | mutex_unlock(&ccp->mutex);
|
| 189 | return ret;
|
| 190 | }
|
| 191 |
|
| 192 | static int set_pwm(struct ccp_device *ccp, int channel, long val)
|
| 193 | {
|
| 194 | int ret;
|
| 195 |
|
| 196 | if (val < 0 || val > 255)
|
| 197 | return -EINVAL;
|
| 198 |
|
| 199 |
|
| 200 | val = DIV_ROUND_CLOSEST(val * 100, 255);
|
| 201 |
|
| 202 | mutex_lock(&ccp->mutex);
|
| 203 |
|
| 204 | ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
|
| 205 | if (!ret)
|
| 206 | ccp->target[channel] = -ENODATA;
|
| 207 |
|
| 208 | mutex_unlock(&ccp->mutex);
|
| 209 | return ret;
|
| 210 | }
|
| 211 |
|
| 212 | static int set_target(struct ccp_device *ccp, int channel, long val)
|
| 213 | {
|
| 214 | int ret;
|
| 215 |
|
| 216 | val = clamp_val(val, 0, 0xFFFF);
|
| 217 | ccp->target[channel] = val;
|
| 218 |
|
| 219 | mutex_lock(&ccp->mutex);
|
| 220 | ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
|
| 221 |
|
| 222 | mutex_unlock(&ccp->mutex);
|
| 223 | return ret;
|
| 224 | }
|
| 225 |
|
| 226 | static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
|
| 227 | u32 attr, int channel, const char **str)
|
| 228 | {
|
| 229 | struct ccp_device *ccp = dev_get_drvdata(dev);
|
| 230 |
|
| 231 | switch (type) {
|
| 232 | case hwmon_fan:
|
| 233 | switch (attr) {
|
| 234 | case hwmon_fan_label:
|
| 235 | *str = ccp->fan_label[channel];
|
| 236 | return 0;
|
| 237 | default:
|
| 238 | break;
|
| 239 | }
|
| 240 | break;
|
| 241 | default:
|
| 242 | break;
|
| 243 | }
|
| 244 |
|
| 245 | return -EOPNOTSUPP;
|
| 246 | }
|
| 247 |
|
| 248 | static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
|
| 249 | u32 attr, int channel, long *val)
|
| 250 | {
|
| 251 | struct ccp_device *ccp = dev_get_drvdata(dev);
|
| 252 | int ret;
|
| 253 |
|
| 254 | switch (type) {
|
| 255 | case hwmon_temp:
|
| 256 | switch (attr) {
|
| 257 | case hwmon_temp_input:
|
| 258 | ret = get_data(ccp, CTL_GET_TMP, channel, true);
|
| 259 | if (ret < 0)
|
| 260 | return ret;
|
| 261 | *val = (s16)ret * 10;
|
| 262 | return 0;
|
| 263 | default:
|
| 264 | break;
|
| 265 | }
|
| 266 | break;
|
| 267 | case hwmon_fan:
|
| 268 | switch (attr) {
|
| 269 | case hwmon_fan_input:
|
| 270 | ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
|
| 271 | if (ret < 0)
|
| 272 | return ret;
|
| 273 | *val = ret;
|
| 274 | return 0;
|
| 275 | case hwmon_fan_target:
|
| 276 |
|
| 277 |
|
| 278 | if (ccp->target[channel] < 0)
|
| 279 | return -ENODATA;
|
| 280 | *val = ccp->target[channel];
|
| 281 | return 0;
|
| 282 | default:
|
| 283 | break;
|
| 284 | }
|
| 285 | break;
|
| 286 | case hwmon_pwm:
|
| 287 | switch (attr) {
|
| 288 | case hwmon_pwm_input:
|
| 289 | ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
|
| 290 | if (ret < 0)
|
| 291 | return ret;
|
| 292 | *val = DIV_ROUND_CLOSEST(ret * 255, 100);
|
| 293 | return 0;
|
| 294 | default:
|
| 295 | break;
|
| 296 | }
|
| 297 | break;
|
| 298 | case hwmon_in:
|
| 299 | switch (attr) {
|
| 300 | case hwmon_in_input:
|
| 301 | ret = get_data(ccp, CTL_GET_VOLT, channel, true);
|
| 302 | if (ret < 0)
|
| 303 | return ret;
|
| 304 | *val = ret;
|
| 305 | return 0;
|
| 306 | default:
|
| 307 | break;
|
| 308 | }
|
| 309 | break;
|
| 310 | default:
|
| 311 | break;
|
| 312 | }
|
| 313 |
|
| 314 | return -EOPNOTSUPP;
|
| 315 | };
|
| 316 |
|
| 317 | static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
|
| 318 | u32 attr, int channel, long val)
|
| 319 | {
|
| 320 | struct ccp_device *ccp = dev_get_drvdata(dev);
|
| 321 |
|
| 322 | switch (type) {
|
| 323 | case hwmon_pwm:
|
| 324 | switch (attr) {
|
| 325 | case hwmon_pwm_input:
|
| 326 | return set_pwm(ccp, channel, val);
|
| 327 | default:
|
| 328 | break;
|
| 329 | }
|
| 330 | break;
|
| 331 | case hwmon_fan:
|
| 332 | switch (attr) {
|
| 333 | case hwmon_fan_target:
|
| 334 | return set_target(ccp, channel, val);
|
| 335 | default:
|
| 336 | break;
|
| 337 | }
|
| 338 | break;
|
| 339 | default:
|
| 340 | break;
|
| 341 | }
|
| 342 |
|
| 343 | return -EOPNOTSUPP;
|
| 344 | };
|
| 345 |
|
| 346 | static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
|
| 347 | u32 attr, int channel)
|
| 348 | {
|
| 349 | const struct ccp_device *ccp = data;
|
| 350 |
|
| 351 | switch (type) {
|
| 352 | case hwmon_temp:
|
| 353 | if (!test_bit(channel, ccp->temp_cnct))
|
| 354 | break;
|
| 355 |
|
| 356 | switch (attr) {
|
| 357 | case hwmon_temp_input:
|
| 358 | return 0444;
|
| 359 | case hwmon_temp_label:
|
| 360 | return 0444;
|
| 361 | default:
|
| 362 | break;
|
| 363 | }
|
| 364 | break;
|
| 365 | case hwmon_fan:
|
| 366 | if (!test_bit(channel, ccp->fan_cnct))
|
| 367 | break;
|
| 368 |
|
| 369 | switch (attr) {
|
| 370 | case hwmon_fan_input:
|
| 371 | return 0444;
|
| 372 | case hwmon_fan_label:
|
| 373 | return 0444;
|
| 374 | case hwmon_fan_target:
|
| 375 | return 0644;
|
| 376 | default:
|
| 377 | break;
|
| 378 | }
|
| 379 | break;
|
| 380 | case hwmon_pwm:
|
| 381 | if (!test_bit(channel, ccp->fan_cnct))
|
| 382 | break;
|
| 383 |
|
| 384 | switch (attr) {
|
| 385 | case hwmon_pwm_input:
|
| 386 | return 0644;
|
| 387 | default:
|
| 388 | break;
|
| 389 | }
|
| 390 | break;
|
| 391 | case hwmon_in:
|
| 392 | switch (attr) {
|
| 393 | case hwmon_in_input:
|
| 394 | return 0444;
|
| 395 | default:
|
| 396 | break;
|
| 397 | }
|
| 398 | break;
|
| 399 | default:
|
| 400 | break;
|
| 401 | }
|
| 402 |
|
| 403 | return 0;
|
| 404 | };
|
| 405 |
|
| 406 | static const struct hwmon_ops ccp_hwmon_ops = {
|
| 407 | .is_visible = ccp_is_visible,
|
| 408 | .read = ccp_read,
|
| 409 | .read_string = ccp_read_string,
|
| 410 | .write = ccp_write,
|
| 411 | };
|
| 412 |
|
| 413 | static const struct hwmon_channel_info * const ccp_info[] = {
|
| 414 | HWMON_CHANNEL_INFO(chip,
|
| 415 | HWMON_C_REGISTER_TZ),
|
| 416 | HWMON_CHANNEL_INFO(temp,
|
| 417 | HWMON_T_INPUT,
|
| 418 | HWMON_T_INPUT,
|
| 419 | HWMON_T_INPUT,
|
| 420 | HWMON_T_INPUT
|
| 421 | ),
|
| 422 | HWMON_CHANNEL_INFO(fan,
|
| 423 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
|
| 424 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
|
| 425 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
|
| 426 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
|
| 427 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
|
| 428 | HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
|
| 429 | ),
|
| 430 | HWMON_CHANNEL_INFO(pwm,
|
| 431 | HWMON_PWM_INPUT,
|
| 432 | HWMON_PWM_INPUT,
|
| 433 | HWMON_PWM_INPUT,
|
| 434 | HWMON_PWM_INPUT,
|
| 435 | HWMON_PWM_INPUT,
|
| 436 | HWMON_PWM_INPUT
|
| 437 | ),
|
| 438 | HWMON_CHANNEL_INFO(in,
|
| 439 | HWMON_I_INPUT,
|
| 440 | HWMON_I_INPUT,
|
| 441 | HWMON_I_INPUT
|
| 442 | ),
|
| 443 | NULL
|
| 444 | };
|
| 445 |
|
| 446 | static const struct hwmon_chip_info ccp_chip_info = {
|
| 447 | .ops = &ccp_hwmon_ops,
|
| 448 | .info = ccp_info,
|
| 449 | };
|
| 450 |
|
| 451 |
|
| 452 | static int get_fan_cnct(struct ccp_device *ccp)
|
| 453 | {
|
| 454 | int channel;
|
| 455 | int mode;
|
| 456 | int ret;
|
| 457 |
|
| 458 | ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
|
| 459 | if (ret)
|
| 460 | return ret;
|
| 461 |
|
| 462 | for (channel = 0; channel < NUM_FANS; channel++) {
|
| 463 | mode = ccp->buffer[channel + 1];
|
| 464 | if (mode == 0)
|
| 465 | continue;
|
| 466 |
|
| 467 | set_bit(channel, ccp->fan_cnct);
|
| 468 | ccp->target[channel] = -ENODATA;
|
| 469 |
|
| 470 | switch (mode) {
|
| 471 | case 1:
|
| 472 | scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
|
| 473 | "fan%d 3pin", channel + 1);
|
| 474 | break;
|
| 475 | case 2:
|
| 476 | scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
|
| 477 | "fan%d 4pin", channel + 1);
|
| 478 | break;
|
| 479 | default:
|
| 480 | scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
|
| 481 | "fan%d other", channel + 1);
|
| 482 | break;
|
| 483 | }
|
| 484 | }
|
| 485 |
|
| 486 | return 0;
|
| 487 | }
|
| 488 |
|
| 489 |
|
| 490 | static int get_temp_cnct(struct ccp_device *ccp)
|
| 491 | {
|
| 492 | int channel;
|
| 493 | int mode;
|
| 494 | int ret;
|
| 495 |
|
| 496 | ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
|
| 497 | if (ret)
|
| 498 | return ret;
|
| 499 |
|
| 500 | for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
|
| 501 | mode = ccp->buffer[channel + 1];
|
| 502 | if (mode == 0)
|
| 503 | continue;
|
| 504 |
|
| 505 | set_bit(channel, ccp->temp_cnct);
|
| 506 | }
|
| 507 |
|
| 508 | return 0;
|
| 509 | }
|
| 510 |
|
| 511 |
|
| 512 | static int get_fw_version(struct ccp_device *ccp)
|
| 513 | {
|
| 514 | int ret;
|
| 515 |
|
| 516 | ret = send_usb_cmd(ccp, CTL_GET_FW_VER, 0, 0, 0);
|
| 517 | if (ret) {
|
| 518 | hid_notice(ccp->hdev, "Failed to read firmware version.\n");
|
| 519 | return ret;
|
| 520 | }
|
| 521 | ccp->firmware_ver[0] = ccp->buffer[1];
|
| 522 | ccp->firmware_ver[1] = ccp->buffer[2];
|
| 523 | ccp->firmware_ver[2] = ccp->buffer[3];
|
| 524 |
|
| 525 | return 0;
|
| 526 | }
|
| 527 |
|
| 528 |
|
| 529 | static int get_bl_version(struct ccp_device *ccp)
|
| 530 | {
|
| 531 | int ret;
|
| 532 |
|
| 533 | ret = send_usb_cmd(ccp, CTL_GET_BL_VER, 0, 0, 0);
|
| 534 | if (ret) {
|
| 535 | hid_notice(ccp->hdev, "Failed to read bootloader version.\n");
|
| 536 | return ret;
|
| 537 | }
|
| 538 | ccp->bootloader_ver[0] = ccp->buffer[1];
|
| 539 | ccp->bootloader_ver[1] = ccp->buffer[2];
|
| 540 |
|
| 541 | return 0;
|
| 542 | }
|
| 543 |
|
| 544 | static int firmware_show(struct seq_file *seqf, void *unused)
|
| 545 | {
|
| 546 | struct ccp_device *ccp = seqf->private;
|
| 547 |
|
| 548 | seq_printf(seqf, "%d.%d.%d\n",
|
| 549 | ccp->firmware_ver[0],
|
| 550 | ccp->firmware_ver[1],
|
| 551 | ccp->firmware_ver[2]);
|
| 552 |
|
| 553 | return 0;
|
| 554 | }
|
| 555 | DEFINE_SHOW_ATTRIBUTE(firmware);
|
| 556 |
|
| 557 | static int bootloader_show(struct seq_file *seqf, void *unused)
|
| 558 | {
|
| 559 | struct ccp_device *ccp = seqf->private;
|
| 560 |
|
| 561 | seq_printf(seqf, "%d.%d\n",
|
| 562 | ccp->bootloader_ver[0],
|
| 563 | ccp->bootloader_ver[1]);
|
| 564 |
|
| 565 | return 0;
|
| 566 | }
|
| 567 | DEFINE_SHOW_ATTRIBUTE(bootloader);
|
| 568 |
|
| 569 | static void ccp_debugfs_init(struct ccp_device *ccp, bool fw_valid, bool bl_valid)
|
| 570 | {
|
| 571 | char name[32];
|
| 572 |
|
| 573 | scnprintf(name, sizeof(name), "corsaircpro-%s", dev_name(&ccp->hdev->dev));
|
| 574 | ccp->debugfs = debugfs_create_dir(name, NULL);
|
| 575 |
|
| 576 | if (fw_valid)
|
| 577 | debugfs_create_file("firmware_version", 0444,
|
| 578 | ccp->debugfs, ccp, &firmware_fops);
|
| 579 |
|
| 580 | if (bl_valid)
|
| 581 | debugfs_create_file("bootloader_version", 0444,
|
| 582 | ccp->debugfs, ccp, &bootloader_fops);
|
| 583 | }
|
| 584 |
|
| 585 | static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
|
| 586 | {
|
| 587 | struct ccp_device *ccp;
|
| 588 | bool fw_valid, bl_valid;
|
| 589 | int ret;
|
| 590 |
|
| 591 | ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
|
| 592 | if (!ccp)
|
| 593 | return -ENOMEM;
|
| 594 |
|
| 595 | ccp->cmd_buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
|
| 596 | if (!ccp->cmd_buffer)
|
| 597 | return -ENOMEM;
|
| 598 |
|
| 599 | ccp->buffer = devm_kmalloc(&hdev->dev, IN_BUFFER_SIZE, GFP_KERNEL);
|
| 600 | if (!ccp->buffer)
|
| 601 | return -ENOMEM;
|
| 602 |
|
| 603 | ret = hid_parse(hdev);
|
| 604 | if (ret)
|
| 605 | return ret;
|
| 606 |
|
| 607 | ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
|
| 608 | if (ret)
|
| 609 | return ret;
|
| 610 |
|
| 611 | ret = hid_hw_open(hdev);
|
| 612 | if (ret)
|
| 613 | goto out_hw_stop;
|
| 614 |
|
| 615 | ccp->hdev = hdev;
|
| 616 | hid_set_drvdata(hdev, ccp);
|
| 617 |
|
| 618 | mutex_init(&ccp->mutex);
|
| 619 | spin_lock_init(&ccp->wait_input_report_lock);
|
| 620 | init_completion(&ccp->wait_input_report);
|
| 621 |
|
| 622 | hid_device_io_start(hdev);
|
| 623 |
|
| 624 |
|
| 625 | ret = get_temp_cnct(ccp);
|
| 626 | if (ret)
|
| 627 | goto out_hw_close;
|
| 628 |
|
| 629 | ret = get_fan_cnct(ccp);
|
| 630 | if (ret)
|
| 631 | goto out_hw_close;
|
| 632 |
|
| 633 | |
| 634 | * Query the versions before registering the hwmon device: they send
|
| 635 | * USB commands without holding ccp->mutex, which is only safe while
|
| 636 | * nothing else can call send_usb_cmd().
|
| 637 |
|
| 638 | fw_valid = !get_fw_version(ccp);
|
| 639 | bl_valid = !get_bl_version(ccp);
|
| 640 |
|
| 641 | ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
|
| 642 | ccp, &ccp_chip_info, NULL);
|
| 643 | if (IS_ERR(ccp->hwmon_dev)) {
|
| 644 | ret = PTR_ERR(ccp->hwmon_dev);
|
| 645 | goto out_debugfs_remove;
|
| 646 | }
|
| 647 |
|
| 648 | ccp_debugfs_init(ccp, fw_valid, bl_valid);
|
| 649 |
|
| 650 | return 0;
|
| 651 |
|
| 652 | out_debugfs_remove:
|
| 653 | debugfs_remove_recursive(ccp->debugfs);
|
| 654 | out_hw_close:
|
| 655 | hid_hw_close(hdev);
|
| 656 | hid_device_io_stop(hdev);
|
| 657 | out_hw_stop:
|
| 658 | hid_hw_stop(hdev);
|
| 659 | return ret;
|
| 660 | }
|
| 661 |
|
| 662 | static void ccp_remove(struct hid_device *hdev)
|
| 663 | {
|
| 664 | struct ccp_device *ccp = hid_get_drvdata(hdev);
|
| 665 |
|
| 666 | debugfs_remove_recursive(ccp->debugfs);
|
| 667 | hwmon_device_unregister(ccp->hwmon_dev);
|
| 668 | hid_hw_close(hdev);
|
| 669 | hid_hw_stop(hdev);
|
| 670 | }
|
| 671 |
|
| 672 | static const struct hid_device_id ccp_devices[] = {
|
| 673 | { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
|
| 674 | { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
|
| 675 | { }
|
| 676 | };
|
| 677 |
|
| 678 | static struct hid_driver ccp_driver = {
|
| 679 | .name = "corsair-cpro",
|
| 680 | .id_table = ccp_devices,
|
| 681 | .probe = ccp_probe,
|
| 682 | .remove = ccp_remove,
|
| 683 | .raw_event = ccp_raw_event,
|
| 684 | };
|
| 685 |
|
| 686 | MODULE_DEVICE_TABLE(hid, ccp_devices);
|
| 687 | MODULE_DESCRIPTION("Corsair Commander Pro controller driver");
|
| 688 | MODULE_LICENSE("GPL");
|
| 689 |
|
| 690 | static int __init ccp_init(void)
|
| 691 | {
|
| 692 | return hid_register_driver(&ccp_driver);
|
| 693 | }
|
| 694 |
|
| 695 | static void __exit ccp_exit(void)
|
| 696 | {
|
| 697 | hid_unregister_driver(&ccp_driver);
|
| 698 | }
|
| 699 |
|
| 700 | |
| 701 | * When compiling this driver as built-in, hwmon initcalls will get called before the
|
| 702 | * hid driver and this driver would fail to register. late_initcall solves this.
|
| 703 |
|
| 704 | late_initcall(ccp_init);
|
| 705 | module_exit(ccp_exit);
|