1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * corsair-cpro.c - Linux driver for Corsair Commander Pro
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.
11 #include <linux/bitops.h>
12 #include <linux/completion.h>
13 #include <linux/hid.h>
14 #include <linux/hwmon.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
21 #define USB_VENDOR_ID_CORSAIR 0x1b1c
22 #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
23 #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
25 #define OUT_BUFFER_SIZE 63
26 #define IN_BUFFER_SIZE 16
27 #define LABEL_LENGTH 11
28 #define REQ_TIMEOUT 300
30 #define CTL_GET_TMP_CNCT 0x10 /*
31 * returns in bytes 1-4 for each temp sensor:
35 #define CTL_GET_TMP 0x11 /*
36 * send: byte 1 is channel, rest zero
37 * rcv: returns temp for channel in centi-degree celsius
39 * returns 0x11 in byte 0 if no sensor is connected
41 #define CTL_GET_VOLT 0x12 /*
42 * send: byte 1 is rail number: 0 = 12v, 1 = 5v, 2 = 3.3v
43 * rcv: returns millivolt in bytes 1,2
44 * returns error 0x10 if request is invalid
46 #define CTL_GET_FAN_CNCT 0x20 /*
47 * returns in bytes 1-6 for each fan:
52 #define CTL_GET_FAN_RPM 0x21 /*
53 * send: byte 1 is channel, rest zero
54 * rcv: returns rpm in bytes 1,2
56 #define CTL_GET_FAN_PWM 0x22 /*
57 * send: byte 1 is channel, rest zero
58 * rcv: returns pwm in byte 1 if it was set
59 * returns error 0x12 if fan is controlled via
60 * fan_target or fan curve
62 #define CTL_SET_FAN_FPWM 0x23 /*
64 * send: byte 1 is fan number
65 * send: byte 2 is percentage from 0 - 100
67 #define CTL_SET_FAN_TARGET 0x24 /*
69 * send: byte 1 is fan number
70 * send: byte 2-3 is target
71 * device accepts all values from 0x00 - 0xFFFF
75 #define NUM_TEMP_SENSORS 4
78 struct hid_device *hdev;
79 struct device *hwmon_dev;
80 struct completion wait_input_report;
81 struct mutex mutex; /* whenever buffer is used, lock before send_usb_cmd */
84 DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
85 DECLARE_BITMAP(fan_cnct, NUM_FANS);
86 char fan_label[6][LABEL_LENGTH];
89 /* converts response error in buffer to errno */
90 static int ccp_get_errno(struct ccp_device *ccp)
92 switch (ccp->buffer[0]) {
93 case 0x00: /* success */
95 case 0x01: /* called invalid command */
97 case 0x10: /* called GET_VOLT / GET_TMP with invalid arguments */
99 case 0x11: /* requested temps of disconnected sensors */
100 case 0x12: /* requested pwm of not pwm controlled channels */
103 hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
108 /* send command, check for error in response, response in ccp->buffer */
109 static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
114 memset(ccp->buffer, 0x00, OUT_BUFFER_SIZE);
115 ccp->buffer[0] = command;
116 ccp->buffer[1] = byte1;
117 ccp->buffer[2] = byte2;
118 ccp->buffer[3] = byte3;
120 reinit_completion(&ccp->wait_input_report);
122 ret = hid_hw_output_report(ccp->hdev, ccp->buffer, OUT_BUFFER_SIZE);
126 t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
130 return ccp_get_errno(ccp);
133 static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
135 struct ccp_device *ccp = hid_get_drvdata(hdev);
137 /* only copy buffer when requested */
138 if (completion_done(&ccp->wait_input_report))
141 memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
142 complete(&ccp->wait_input_report);
147 /* requests and returns single data values depending on channel */
148 static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
152 mutex_lock(&ccp->mutex);
154 ret = send_usb_cmd(ccp, command, channel, 0, 0);
158 ret = ccp->buffer[1];
160 ret = (ret << 8) + ccp->buffer[2];
163 mutex_unlock(&ccp->mutex);
167 static int set_pwm(struct ccp_device *ccp, int channel, long val)
171 if (val < 0 || val > 255)
174 /* The Corsair Commander Pro uses values from 0-100 */
175 val = DIV_ROUND_CLOSEST(val * 100, 255);
177 mutex_lock(&ccp->mutex);
179 ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
181 ccp->target[channel] = -ENODATA;
183 mutex_unlock(&ccp->mutex);
187 static int set_target(struct ccp_device *ccp, int channel, long val)
191 val = clamp_val(val, 0, 0xFFFF);
192 ccp->target[channel] = val;
194 mutex_lock(&ccp->mutex);
195 ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
197 mutex_unlock(&ccp->mutex);
201 static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
202 u32 attr, int channel, const char **str)
204 struct ccp_device *ccp = dev_get_drvdata(dev);
209 case hwmon_fan_label:
210 *str = ccp->fan_label[channel];
223 static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
224 u32 attr, int channel, long *val)
226 struct ccp_device *ccp = dev_get_drvdata(dev);
232 case hwmon_temp_input:
233 ret = get_data(ccp, CTL_GET_TMP, channel, true);
244 case hwmon_fan_input:
245 ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
250 case hwmon_fan_target:
251 /* how to read target values from the device is unknown */
252 /* driver returns last set value or 0 */
253 if (ccp->target[channel] < 0)
255 *val = ccp->target[channel];
263 case hwmon_pwm_input:
264 ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
267 *val = DIV_ROUND_CLOSEST(ret * 255, 100);
276 ret = get_data(ccp, CTL_GET_VOLT, channel, true);
292 static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
293 u32 attr, int channel, long val)
295 struct ccp_device *ccp = dev_get_drvdata(dev);
300 case hwmon_pwm_input:
301 return set_pwm(ccp, channel, val);
308 case hwmon_fan_target:
309 return set_target(ccp, channel, val);
321 static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
322 u32 attr, int channel)
324 const struct ccp_device *ccp = data;
328 if (!test_bit(channel, ccp->temp_cnct))
332 case hwmon_temp_input:
334 case hwmon_temp_label:
341 if (!test_bit(channel, ccp->fan_cnct))
345 case hwmon_fan_input:
347 case hwmon_fan_label:
349 case hwmon_fan_target:
356 if (!test_bit(channel, ccp->fan_cnct))
360 case hwmon_pwm_input:
381 static const struct hwmon_ops ccp_hwmon_ops = {
382 .is_visible = ccp_is_visible,
384 .read_string = ccp_read_string,
388 static const struct hwmon_channel_info *ccp_info[] = {
389 HWMON_CHANNEL_INFO(chip,
390 HWMON_C_REGISTER_TZ),
391 HWMON_CHANNEL_INFO(temp,
397 HWMON_CHANNEL_INFO(fan,
398 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
399 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
400 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
401 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
402 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
403 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
405 HWMON_CHANNEL_INFO(pwm,
413 HWMON_CHANNEL_INFO(in,
421 static const struct hwmon_chip_info ccp_chip_info = {
422 .ops = &ccp_hwmon_ops,
426 /* read fan connection status and set labels */
427 static int get_fan_cnct(struct ccp_device *ccp)
433 ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
437 for (channel = 0; channel < NUM_FANS; channel++) {
438 mode = ccp->buffer[channel + 1];
442 set_bit(channel, ccp->fan_cnct);
443 ccp->target[channel] = -ENODATA;
447 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
448 "fan%d 3pin", channel + 1);
451 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
452 "fan%d 4pin", channel + 1);
455 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
456 "fan%d other", channel + 1);
464 /* read temp sensor connection status */
465 static int get_temp_cnct(struct ccp_device *ccp)
471 ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
475 for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
476 mode = ccp->buffer[channel + 1];
480 set_bit(channel, ccp->temp_cnct);
486 static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
488 struct ccp_device *ccp;
491 ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
495 ccp->buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
499 ret = hid_parse(hdev);
503 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
507 ret = hid_hw_open(hdev);
512 hid_set_drvdata(hdev, ccp);
513 mutex_init(&ccp->mutex);
514 init_completion(&ccp->wait_input_report);
516 hid_device_io_start(hdev);
518 /* temp and fan connection status only updates when device is powered on */
519 ret = get_temp_cnct(ccp);
523 ret = get_fan_cnct(ccp);
526 ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
527 ccp, &ccp_chip_info, 0);
528 if (IS_ERR(ccp->hwmon_dev)) {
529 ret = PTR_ERR(ccp->hwmon_dev);
542 static void ccp_remove(struct hid_device *hdev)
544 struct ccp_device *ccp = hid_get_drvdata(hdev);
546 hwmon_device_unregister(ccp->hwmon_dev);
551 static const struct hid_device_id ccp_devices[] = {
552 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
553 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
557 static struct hid_driver ccp_driver = {
558 .name = "corsair-cpro",
559 .id_table = ccp_devices,
561 .remove = ccp_remove,
562 .raw_event = ccp_raw_event,
565 MODULE_DEVICE_TABLE(hid, ccp_devices);
566 MODULE_LICENSE("GPL");
568 static int __init ccp_init(void)
570 return hid_register_driver(&ccp_driver);
573 static void __exit ccp_exit(void)
575 hid_unregister_driver(&ccp_driver);
579 * When compiling this driver as built-in, hwmon initcalls will get called before the
580 * hid driver and this driver would fail to register. late_initcall solves this.
582 late_initcall(ccp_init);
583 module_exit(ccp_exit);