0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <linux/bitops.h>
0012 #include <linux/completion.h>
0013 #include <linux/hid.h>
0014 #include <linux/hwmon.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/slab.h>
0019 #include <linux/types.h>
0020
0021 #define USB_VENDOR_ID_CORSAIR 0x1b1c
0022 #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
0023 #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
0024
0025 #define OUT_BUFFER_SIZE 63
0026 #define IN_BUFFER_SIZE 16
0027 #define LABEL_LENGTH 11
0028 #define REQ_TIMEOUT 300
0029
0030 #define CTL_GET_TMP_CNCT 0x10
0031
0032
0033
0034
0035 #define CTL_GET_TMP 0x11
0036
0037
0038
0039
0040
0041 #define CTL_GET_VOLT 0x12
0042
0043
0044
0045
0046 #define CTL_GET_FAN_CNCT 0x20
0047
0048
0049
0050
0051
0052 #define CTL_GET_FAN_RPM 0x21
0053
0054
0055
0056 #define CTL_GET_FAN_PWM 0x22
0057
0058
0059
0060
0061
0062 #define CTL_SET_FAN_FPWM 0x23
0063
0064
0065
0066
0067 #define CTL_SET_FAN_TARGET 0x24
0068
0069
0070
0071
0072
0073
0074 #define NUM_FANS 6
0075 #define NUM_TEMP_SENSORS 4
0076
0077 struct ccp_device {
0078 struct hid_device *hdev;
0079 struct device *hwmon_dev;
0080 struct completion wait_input_report;
0081 struct mutex mutex;
0082 u8 *buffer;
0083 int target[6];
0084 DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
0085 DECLARE_BITMAP(fan_cnct, NUM_FANS);
0086 char fan_label[6][LABEL_LENGTH];
0087 };
0088
0089
0090 static int ccp_get_errno(struct ccp_device *ccp)
0091 {
0092 switch (ccp->buffer[0]) {
0093 case 0x00:
0094 return 0;
0095 case 0x01:
0096 return -EOPNOTSUPP;
0097 case 0x10:
0098 return -EINVAL;
0099 case 0x11:
0100 case 0x12:
0101 return -ENODATA;
0102 default:
0103 hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
0104 return -EIO;
0105 }
0106 }
0107
0108
0109 static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
0110 {
0111 unsigned long t;
0112 int ret;
0113
0114 memset(ccp->buffer, 0x00, OUT_BUFFER_SIZE);
0115 ccp->buffer[0] = command;
0116 ccp->buffer[1] = byte1;
0117 ccp->buffer[2] = byte2;
0118 ccp->buffer[3] = byte3;
0119
0120 reinit_completion(&ccp->wait_input_report);
0121
0122 ret = hid_hw_output_report(ccp->hdev, ccp->buffer, OUT_BUFFER_SIZE);
0123 if (ret < 0)
0124 return ret;
0125
0126 t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
0127 if (!t)
0128 return -ETIMEDOUT;
0129
0130 return ccp_get_errno(ccp);
0131 }
0132
0133 static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
0134 {
0135 struct ccp_device *ccp = hid_get_drvdata(hdev);
0136
0137
0138 if (completion_done(&ccp->wait_input_report))
0139 return 0;
0140
0141 memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
0142 complete(&ccp->wait_input_report);
0143
0144 return 0;
0145 }
0146
0147
0148 static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
0149 {
0150 int ret;
0151
0152 mutex_lock(&ccp->mutex);
0153
0154 ret = send_usb_cmd(ccp, command, channel, 0, 0);
0155 if (ret)
0156 goto out_unlock;
0157
0158 ret = ccp->buffer[1];
0159 if (two_byte_data)
0160 ret = (ret << 8) + ccp->buffer[2];
0161
0162 out_unlock:
0163 mutex_unlock(&ccp->mutex);
0164 return ret;
0165 }
0166
0167 static int set_pwm(struct ccp_device *ccp, int channel, long val)
0168 {
0169 int ret;
0170
0171 if (val < 0 || val > 255)
0172 return -EINVAL;
0173
0174
0175 val = DIV_ROUND_CLOSEST(val * 100, 255);
0176
0177 mutex_lock(&ccp->mutex);
0178
0179 ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
0180 if (!ret)
0181 ccp->target[channel] = -ENODATA;
0182
0183 mutex_unlock(&ccp->mutex);
0184 return ret;
0185 }
0186
0187 static int set_target(struct ccp_device *ccp, int channel, long val)
0188 {
0189 int ret;
0190
0191 val = clamp_val(val, 0, 0xFFFF);
0192 ccp->target[channel] = val;
0193
0194 mutex_lock(&ccp->mutex);
0195 ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
0196
0197 mutex_unlock(&ccp->mutex);
0198 return ret;
0199 }
0200
0201 static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
0202 u32 attr, int channel, const char **str)
0203 {
0204 struct ccp_device *ccp = dev_get_drvdata(dev);
0205
0206 switch (type) {
0207 case hwmon_fan:
0208 switch (attr) {
0209 case hwmon_fan_label:
0210 *str = ccp->fan_label[channel];
0211 return 0;
0212 default:
0213 break;
0214 }
0215 break;
0216 default:
0217 break;
0218 }
0219
0220 return -EOPNOTSUPP;
0221 }
0222
0223 static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
0224 u32 attr, int channel, long *val)
0225 {
0226 struct ccp_device *ccp = dev_get_drvdata(dev);
0227 int ret;
0228
0229 switch (type) {
0230 case hwmon_temp:
0231 switch (attr) {
0232 case hwmon_temp_input:
0233 ret = get_data(ccp, CTL_GET_TMP, channel, true);
0234 if (ret < 0)
0235 return ret;
0236 *val = ret * 10;
0237 return 0;
0238 default:
0239 break;
0240 }
0241 break;
0242 case hwmon_fan:
0243 switch (attr) {
0244 case hwmon_fan_input:
0245 ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
0246 if (ret < 0)
0247 return ret;
0248 *val = ret;
0249 return 0;
0250 case hwmon_fan_target:
0251
0252
0253 if (ccp->target[channel] < 0)
0254 return -ENODATA;
0255 *val = ccp->target[channel];
0256 return 0;
0257 default:
0258 break;
0259 }
0260 break;
0261 case hwmon_pwm:
0262 switch (attr) {
0263 case hwmon_pwm_input:
0264 ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
0265 if (ret < 0)
0266 return ret;
0267 *val = DIV_ROUND_CLOSEST(ret * 255, 100);
0268 return 0;
0269 default:
0270 break;
0271 }
0272 break;
0273 case hwmon_in:
0274 switch (attr) {
0275 case hwmon_in_input:
0276 ret = get_data(ccp, CTL_GET_VOLT, channel, true);
0277 if (ret < 0)
0278 return ret;
0279 *val = ret;
0280 return 0;
0281 default:
0282 break;
0283 }
0284 break;
0285 default:
0286 break;
0287 }
0288
0289 return -EOPNOTSUPP;
0290 };
0291
0292 static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
0293 u32 attr, int channel, long val)
0294 {
0295 struct ccp_device *ccp = dev_get_drvdata(dev);
0296
0297 switch (type) {
0298 case hwmon_pwm:
0299 switch (attr) {
0300 case hwmon_pwm_input:
0301 return set_pwm(ccp, channel, val);
0302 default:
0303 break;
0304 }
0305 break;
0306 case hwmon_fan:
0307 switch (attr) {
0308 case hwmon_fan_target:
0309 return set_target(ccp, channel, val);
0310 default:
0311 break;
0312 }
0313 break;
0314 default:
0315 break;
0316 }
0317
0318 return -EOPNOTSUPP;
0319 };
0320
0321 static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
0322 u32 attr, int channel)
0323 {
0324 const struct ccp_device *ccp = data;
0325
0326 switch (type) {
0327 case hwmon_temp:
0328 if (!test_bit(channel, ccp->temp_cnct))
0329 break;
0330
0331 switch (attr) {
0332 case hwmon_temp_input:
0333 return 0444;
0334 case hwmon_temp_label:
0335 return 0444;
0336 default:
0337 break;
0338 }
0339 break;
0340 case hwmon_fan:
0341 if (!test_bit(channel, ccp->fan_cnct))
0342 break;
0343
0344 switch (attr) {
0345 case hwmon_fan_input:
0346 return 0444;
0347 case hwmon_fan_label:
0348 return 0444;
0349 case hwmon_fan_target:
0350 return 0644;
0351 default:
0352 break;
0353 }
0354 break;
0355 case hwmon_pwm:
0356 if (!test_bit(channel, ccp->fan_cnct))
0357 break;
0358
0359 switch (attr) {
0360 case hwmon_pwm_input:
0361 return 0644;
0362 default:
0363 break;
0364 }
0365 break;
0366 case hwmon_in:
0367 switch (attr) {
0368 case hwmon_in_input:
0369 return 0444;
0370 default:
0371 break;
0372 }
0373 break;
0374 default:
0375 break;
0376 }
0377
0378 return 0;
0379 };
0380
0381 static const struct hwmon_ops ccp_hwmon_ops = {
0382 .is_visible = ccp_is_visible,
0383 .read = ccp_read,
0384 .read_string = ccp_read_string,
0385 .write = ccp_write,
0386 };
0387
0388 static const struct hwmon_channel_info *ccp_info[] = {
0389 HWMON_CHANNEL_INFO(chip,
0390 HWMON_C_REGISTER_TZ),
0391 HWMON_CHANNEL_INFO(temp,
0392 HWMON_T_INPUT,
0393 HWMON_T_INPUT,
0394 HWMON_T_INPUT,
0395 HWMON_T_INPUT
0396 ),
0397 HWMON_CHANNEL_INFO(fan,
0398 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
0399 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
0400 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
0401 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
0402 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
0403 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
0404 ),
0405 HWMON_CHANNEL_INFO(pwm,
0406 HWMON_PWM_INPUT,
0407 HWMON_PWM_INPUT,
0408 HWMON_PWM_INPUT,
0409 HWMON_PWM_INPUT,
0410 HWMON_PWM_INPUT,
0411 HWMON_PWM_INPUT
0412 ),
0413 HWMON_CHANNEL_INFO(in,
0414 HWMON_I_INPUT,
0415 HWMON_I_INPUT,
0416 HWMON_I_INPUT
0417 ),
0418 NULL
0419 };
0420
0421 static const struct hwmon_chip_info ccp_chip_info = {
0422 .ops = &ccp_hwmon_ops,
0423 .info = ccp_info,
0424 };
0425
0426
0427 static int get_fan_cnct(struct ccp_device *ccp)
0428 {
0429 int channel;
0430 int mode;
0431 int ret;
0432
0433 ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
0434 if (ret)
0435 return ret;
0436
0437 for (channel = 0; channel < NUM_FANS; channel++) {
0438 mode = ccp->buffer[channel + 1];
0439 if (mode == 0)
0440 continue;
0441
0442 set_bit(channel, ccp->fan_cnct);
0443 ccp->target[channel] = -ENODATA;
0444
0445 switch (mode) {
0446 case 1:
0447 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
0448 "fan%d 3pin", channel + 1);
0449 break;
0450 case 2:
0451 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
0452 "fan%d 4pin", channel + 1);
0453 break;
0454 default:
0455 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
0456 "fan%d other", channel + 1);
0457 break;
0458 }
0459 }
0460
0461 return 0;
0462 }
0463
0464
0465 static int get_temp_cnct(struct ccp_device *ccp)
0466 {
0467 int channel;
0468 int mode;
0469 int ret;
0470
0471 ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
0472 if (ret)
0473 return ret;
0474
0475 for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
0476 mode = ccp->buffer[channel + 1];
0477 if (mode == 0)
0478 continue;
0479
0480 set_bit(channel, ccp->temp_cnct);
0481 }
0482
0483 return 0;
0484 }
0485
0486 static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
0487 {
0488 struct ccp_device *ccp;
0489 int ret;
0490
0491 ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
0492 if (!ccp)
0493 return -ENOMEM;
0494
0495 ccp->buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
0496 if (!ccp->buffer)
0497 return -ENOMEM;
0498
0499 ret = hid_parse(hdev);
0500 if (ret)
0501 return ret;
0502
0503 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
0504 if (ret)
0505 return ret;
0506
0507 ret = hid_hw_open(hdev);
0508 if (ret)
0509 goto out_hw_stop;
0510
0511 ccp->hdev = hdev;
0512 hid_set_drvdata(hdev, ccp);
0513 mutex_init(&ccp->mutex);
0514 init_completion(&ccp->wait_input_report);
0515
0516 hid_device_io_start(hdev);
0517
0518
0519 ret = get_temp_cnct(ccp);
0520 if (ret)
0521 goto out_hw_close;
0522
0523 ret = get_fan_cnct(ccp);
0524 if (ret)
0525 goto out_hw_close;
0526 ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
0527 ccp, &ccp_chip_info, 0);
0528 if (IS_ERR(ccp->hwmon_dev)) {
0529 ret = PTR_ERR(ccp->hwmon_dev);
0530 goto out_hw_close;
0531 }
0532
0533 return 0;
0534
0535 out_hw_close:
0536 hid_hw_close(hdev);
0537 out_hw_stop:
0538 hid_hw_stop(hdev);
0539 return ret;
0540 }
0541
0542 static void ccp_remove(struct hid_device *hdev)
0543 {
0544 struct ccp_device *ccp = hid_get_drvdata(hdev);
0545
0546 hwmon_device_unregister(ccp->hwmon_dev);
0547 hid_hw_close(hdev);
0548 hid_hw_stop(hdev);
0549 }
0550
0551 static const struct hid_device_id ccp_devices[] = {
0552 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
0553 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
0554 { }
0555 };
0556
0557 static struct hid_driver ccp_driver = {
0558 .name = "corsair-cpro",
0559 .id_table = ccp_devices,
0560 .probe = ccp_probe,
0561 .remove = ccp_remove,
0562 .raw_event = ccp_raw_event,
0563 };
0564
0565 MODULE_DEVICE_TABLE(hid, ccp_devices);
0566 MODULE_LICENSE("GPL");
0567
0568 static int __init ccp_init(void)
0569 {
0570 return hid_register_driver(&ccp_driver);
0571 }
0572
0573 static void __exit ccp_exit(void)
0574 {
0575 hid_unregister_driver(&ccp_driver);
0576 }
0577
0578
0579
0580
0581
0582 late_initcall(ccp_init);
0583 module_exit(ccp_exit);