1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * corsair-cpro.c - Linux driver for Corsair Commander Pro
4 * Copyright (C) 2020 Marius Zachmann <mail@mariuszachmann.de>
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/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>
24 #define USB_VENDOR_ID_CORSAIR 0x1b1c
25 #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
26 #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
28 #define OUT_BUFFER_SIZE 63
29 #define IN_BUFFER_SIZE 16
30 #define LABEL_LENGTH 11
31 #define REQ_TIMEOUT 300
33 #define CTL_GET_FW_VER 0x02 /* returns the firmware version in bytes 1-3 */
34 #define CTL_GET_BL_VER 0x06 /* returns the bootloader version in bytes 1-2 */
35 #define CTL_GET_TMP_CNCT 0x10 /*
36 * returns in bytes 1-4 for each temp sensor:
40 #define CTL_GET_TMP 0x11 /*
41 * send: byte 1 is channel, rest zero
42 * rcv: returns temp for channel in centi-degree celsius
44 * returns 0x11 in byte 0 if no sensor is connected
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
51 #define CTL_GET_FAN_CNCT 0x20 /*
52 * returns in bytes 1-6 for each fan:
57 #define CTL_GET_FAN_RPM 0x21 /*
58 * send: byte 1 is channel, rest zero
59 * rcv: returns rpm in bytes 1,2
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
67 #define CTL_SET_FAN_FPWM 0x23 /*
69 * send: byte 1 is fan number
70 * send: byte 2 is percentage from 0 - 100
72 #define CTL_SET_FAN_TARGET 0x24 /*
74 * send: byte 1 is fan number
75 * send: byte 2-3 is target
76 * device accepts all values from 0x00 - 0xFFFF
80 #define NUM_TEMP_SENSORS 4
83 struct hid_device
*hdev
;
84 struct device
*hwmon_dev
;
85 struct dentry
*debugfs
;
86 /* For reinitializing the completion below */
87 spinlock_t wait_input_report_lock
;
88 struct completion wait_input_report
;
89 struct mutex mutex
; /* whenever buffer is used, lock before send_usb_cmd */
93 DECLARE_BITMAP(temp_cnct
, NUM_TEMP_SENSORS
);
94 DECLARE_BITMAP(fan_cnct
, NUM_FANS
);
95 char fan_label
[6][LABEL_LENGTH
];
100 /* converts response error in buffer to errno */
101 static int ccp_get_errno(struct ccp_device
*ccp
)
103 switch (ccp
->buffer
[0]) {
104 case 0x00: /* success */
106 case 0x01: /* called invalid command */
108 case 0x10: /* called GET_VOLT / GET_TMP with invalid arguments */
110 case 0x11: /* requested temps of disconnected sensors */
111 case 0x12: /* requested pwm of not pwm controlled channels */
114 hid_dbg(ccp
->hdev
, "unknown device response error: %d", ccp
->buffer
[0]);
119 /* send command, check for error in response, response in ccp->buffer */
120 static int send_usb_cmd(struct ccp_device
*ccp
, u8 command
, u8 byte1
, u8 byte2
, u8 byte3
)
125 memset(ccp
->cmd_buffer
, 0x00, OUT_BUFFER_SIZE
);
126 ccp
->cmd_buffer
[0] = command
;
127 ccp
->cmd_buffer
[1] = byte1
;
128 ccp
->cmd_buffer
[2] = byte2
;
129 ccp
->cmd_buffer
[3] = byte3
;
132 * Disable raw event parsing for a moment to safely reinitialize the
133 * completion. Reinit is done because hidraw could have triggered
134 * the raw event parsing and marked the ccp->wait_input_report
135 * completion as done.
137 spin_lock_bh(&ccp
->wait_input_report_lock
);
138 reinit_completion(&ccp
->wait_input_report
);
139 spin_unlock_bh(&ccp
->wait_input_report_lock
);
141 ret
= hid_hw_output_report(ccp
->hdev
, ccp
->cmd_buffer
, OUT_BUFFER_SIZE
);
145 t
= wait_for_completion_timeout(&ccp
->wait_input_report
, msecs_to_jiffies(REQ_TIMEOUT
));
149 return ccp_get_errno(ccp
);
152 static int ccp_raw_event(struct hid_device
*hdev
, struct hid_report
*report
, u8
*data
, int size
)
154 struct ccp_device
*ccp
= hid_get_drvdata(hdev
);
156 /* only copy buffer when requested */
157 spin_lock(&ccp
->wait_input_report_lock
);
158 if (!completion_done(&ccp
->wait_input_report
)) {
159 memcpy(ccp
->buffer
, data
, min(IN_BUFFER_SIZE
, size
));
160 complete_all(&ccp
->wait_input_report
);
162 spin_unlock(&ccp
->wait_input_report_lock
);
167 /* requests and returns single data values depending on channel */
168 static int get_data(struct ccp_device
*ccp
, int command
, int channel
, bool two_byte_data
)
172 mutex_lock(&ccp
->mutex
);
174 ret
= send_usb_cmd(ccp
, command
, channel
, 0, 0);
178 ret
= ccp
->buffer
[1];
180 ret
= (ret
<< 8) + ccp
->buffer
[2];
183 mutex_unlock(&ccp
->mutex
);
187 static int set_pwm(struct ccp_device
*ccp
, int channel
, long val
)
191 if (val
< 0 || val
> 255)
194 /* The Corsair Commander Pro uses values from 0-100 */
195 val
= DIV_ROUND_CLOSEST(val
* 100, 255);
197 mutex_lock(&ccp
->mutex
);
199 ret
= send_usb_cmd(ccp
, CTL_SET_FAN_FPWM
, channel
, val
, 0);
201 ccp
->target
[channel
] = -ENODATA
;
203 mutex_unlock(&ccp
->mutex
);
207 static int set_target(struct ccp_device
*ccp
, int channel
, long val
)
211 val
= clamp_val(val
, 0, 0xFFFF);
212 ccp
->target
[channel
] = val
;
214 mutex_lock(&ccp
->mutex
);
215 ret
= send_usb_cmd(ccp
, CTL_SET_FAN_TARGET
, channel
, val
>> 8, val
);
217 mutex_unlock(&ccp
->mutex
);
221 static int ccp_read_string(struct device
*dev
, enum hwmon_sensor_types type
,
222 u32 attr
, int channel
, const char **str
)
224 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
229 case hwmon_fan_label
:
230 *str
= ccp
->fan_label
[channel
];
243 static int ccp_read(struct device
*dev
, enum hwmon_sensor_types type
,
244 u32 attr
, int channel
, long *val
)
246 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
252 case hwmon_temp_input
:
253 ret
= get_data(ccp
, CTL_GET_TMP
, channel
, true);
264 case hwmon_fan_input
:
265 ret
= get_data(ccp
, CTL_GET_FAN_RPM
, channel
, true);
270 case hwmon_fan_target
:
271 /* how to read target values from the device is unknown */
272 /* driver returns last set value or 0 */
273 if (ccp
->target
[channel
] < 0)
275 *val
= ccp
->target
[channel
];
283 case hwmon_pwm_input
:
284 ret
= get_data(ccp
, CTL_GET_FAN_PWM
, channel
, false);
287 *val
= DIV_ROUND_CLOSEST(ret
* 255, 100);
296 ret
= get_data(ccp
, CTL_GET_VOLT
, channel
, true);
312 static int ccp_write(struct device
*dev
, enum hwmon_sensor_types type
,
313 u32 attr
, int channel
, long val
)
315 struct ccp_device
*ccp
= dev_get_drvdata(dev
);
320 case hwmon_pwm_input
:
321 return set_pwm(ccp
, channel
, val
);
328 case hwmon_fan_target
:
329 return set_target(ccp
, channel
, val
);
341 static umode_t
ccp_is_visible(const void *data
, enum hwmon_sensor_types type
,
342 u32 attr
, int channel
)
344 const struct ccp_device
*ccp
= data
;
348 if (!test_bit(channel
, ccp
->temp_cnct
))
352 case hwmon_temp_input
:
354 case hwmon_temp_label
:
361 if (!test_bit(channel
, ccp
->fan_cnct
))
365 case hwmon_fan_input
:
367 case hwmon_fan_label
:
369 case hwmon_fan_target
:
376 if (!test_bit(channel
, ccp
->fan_cnct
))
380 case hwmon_pwm_input
:
401 static const struct hwmon_ops ccp_hwmon_ops
= {
402 .is_visible
= ccp_is_visible
,
404 .read_string
= ccp_read_string
,
408 static const struct hwmon_channel_info
* const ccp_info
[] = {
409 HWMON_CHANNEL_INFO(chip
,
410 HWMON_C_REGISTER_TZ
),
411 HWMON_CHANNEL_INFO(temp
,
417 HWMON_CHANNEL_INFO(fan
,
418 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
419 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
420 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
421 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
422 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
423 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
425 HWMON_CHANNEL_INFO(pwm
,
433 HWMON_CHANNEL_INFO(in
,
441 static const struct hwmon_chip_info ccp_chip_info
= {
442 .ops
= &ccp_hwmon_ops
,
446 /* read fan connection status and set labels */
447 static int get_fan_cnct(struct ccp_device
*ccp
)
453 ret
= send_usb_cmd(ccp
, CTL_GET_FAN_CNCT
, 0, 0, 0);
457 for (channel
= 0; channel
< NUM_FANS
; channel
++) {
458 mode
= ccp
->buffer
[channel
+ 1];
462 set_bit(channel
, ccp
->fan_cnct
);
463 ccp
->target
[channel
] = -ENODATA
;
467 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
468 "fan%d 3pin", channel
+ 1);
471 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
472 "fan%d 4pin", channel
+ 1);
475 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
476 "fan%d other", channel
+ 1);
484 /* read temp sensor connection status */
485 static int get_temp_cnct(struct ccp_device
*ccp
)
491 ret
= send_usb_cmd(ccp
, CTL_GET_TMP_CNCT
, 0, 0, 0);
495 for (channel
= 0; channel
< NUM_TEMP_SENSORS
; channel
++) {
496 mode
= ccp
->buffer
[channel
+ 1];
500 set_bit(channel
, ccp
->temp_cnct
);
506 /* read firmware version */
507 static int get_fw_version(struct ccp_device
*ccp
)
511 ret
= send_usb_cmd(ccp
, CTL_GET_FW_VER
, 0, 0, 0);
513 hid_notice(ccp
->hdev
, "Failed to read firmware version.\n");
516 ccp
->firmware_ver
[0] = ccp
->buffer
[1];
517 ccp
->firmware_ver
[1] = ccp
->buffer
[2];
518 ccp
->firmware_ver
[2] = ccp
->buffer
[3];
523 /* read bootloader version */
524 static int get_bl_version(struct ccp_device
*ccp
)
528 ret
= send_usb_cmd(ccp
, CTL_GET_BL_VER
, 0, 0, 0);
530 hid_notice(ccp
->hdev
, "Failed to read bootloader version.\n");
533 ccp
->bootloader_ver
[0] = ccp
->buffer
[1];
534 ccp
->bootloader_ver
[1] = ccp
->buffer
[2];
539 static int firmware_show(struct seq_file
*seqf
, void *unused
)
541 struct ccp_device
*ccp
= seqf
->private;
543 seq_printf(seqf
, "%d.%d.%d\n",
544 ccp
->firmware_ver
[0],
545 ccp
->firmware_ver
[1],
546 ccp
->firmware_ver
[2]);
550 DEFINE_SHOW_ATTRIBUTE(firmware
);
552 static int bootloader_show(struct seq_file
*seqf
, void *unused
)
554 struct ccp_device
*ccp
= seqf
->private;
556 seq_printf(seqf
, "%d.%d\n",
557 ccp
->bootloader_ver
[0],
558 ccp
->bootloader_ver
[1]);
562 DEFINE_SHOW_ATTRIBUTE(bootloader
);
564 static void ccp_debugfs_init(struct ccp_device
*ccp
)
569 scnprintf(name
, sizeof(name
), "corsaircpro-%s", dev_name(&ccp
->hdev
->dev
));
570 ccp
->debugfs
= debugfs_create_dir(name
, NULL
);
572 ret
= get_fw_version(ccp
);
574 debugfs_create_file("firmware_version", 0444,
575 ccp
->debugfs
, ccp
, &firmware_fops
);
577 ret
= get_bl_version(ccp
);
579 debugfs_create_file("bootloader_version", 0444,
580 ccp
->debugfs
, ccp
, &bootloader_fops
);
583 static int ccp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
585 struct ccp_device
*ccp
;
588 ccp
= devm_kzalloc(&hdev
->dev
, sizeof(*ccp
), GFP_KERNEL
);
592 ccp
->cmd_buffer
= devm_kmalloc(&hdev
->dev
, OUT_BUFFER_SIZE
, GFP_KERNEL
);
593 if (!ccp
->cmd_buffer
)
596 ccp
->buffer
= devm_kmalloc(&hdev
->dev
, IN_BUFFER_SIZE
, GFP_KERNEL
);
600 ret
= hid_parse(hdev
);
604 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
608 ret
= hid_hw_open(hdev
);
613 hid_set_drvdata(hdev
, ccp
);
615 mutex_init(&ccp
->mutex
);
616 spin_lock_init(&ccp
->wait_input_report_lock
);
617 init_completion(&ccp
->wait_input_report
);
619 hid_device_io_start(hdev
);
621 /* temp and fan connection status only updates when device is powered on */
622 ret
= get_temp_cnct(ccp
);
626 ret
= get_fan_cnct(ccp
);
630 ccp_debugfs_init(ccp
);
632 ccp
->hwmon_dev
= hwmon_device_register_with_info(&hdev
->dev
, "corsaircpro",
633 ccp
, &ccp_chip_info
, NULL
);
634 if (IS_ERR(ccp
->hwmon_dev
)) {
635 ret
= PTR_ERR(ccp
->hwmon_dev
);
648 static void ccp_remove(struct hid_device
*hdev
)
650 struct ccp_device
*ccp
= hid_get_drvdata(hdev
);
652 debugfs_remove_recursive(ccp
->debugfs
);
653 hwmon_device_unregister(ccp
->hwmon_dev
);
658 static const struct hid_device_id ccp_devices
[] = {
659 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO
) },
660 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_PRODUCT_ID_CORSAIR_1000D
) },
664 static struct hid_driver ccp_driver
= {
665 .name
= "corsair-cpro",
666 .id_table
= ccp_devices
,
668 .remove
= ccp_remove
,
669 .raw_event
= ccp_raw_event
,
672 MODULE_DEVICE_TABLE(hid
, ccp_devices
);
673 MODULE_DESCRIPTION("Corsair Commander Pro controller driver");
674 MODULE_LICENSE("GPL");
676 static int __init
ccp_init(void)
678 return hid_register_driver(&ccp_driver
);
681 static void __exit
ccp_exit(void)
683 hid_unregister_driver(&ccp_driver
);
687 * When compiling this driver as built-in, hwmon initcalls will get called before the
688 * hid driver and this driver would fail to register. late_initcall solves this.
690 late_initcall(ccp_init
);
691 module_exit(ccp_exit
);