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/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
);
320 static umode_t
ccp_is_visible(const void *data
, enum hwmon_sensor_types type
,
321 u32 attr
, int channel
)
323 const struct ccp_device
*ccp
= data
;
327 if (!test_bit(channel
, ccp
->temp_cnct
))
331 case hwmon_temp_input
:
333 case hwmon_temp_label
:
340 if (!test_bit(channel
, ccp
->fan_cnct
))
344 case hwmon_fan_input
:
346 case hwmon_fan_label
:
348 case hwmon_fan_target
:
355 if (!test_bit(channel
, ccp
->fan_cnct
))
359 case hwmon_pwm_input
:
380 static const struct hwmon_ops ccp_hwmon_ops
= {
381 .is_visible
= ccp_is_visible
,
383 .read_string
= ccp_read_string
,
387 static const struct hwmon_channel_info
*ccp_info
[] = {
388 HWMON_CHANNEL_INFO(chip
,
389 HWMON_C_REGISTER_TZ
),
390 HWMON_CHANNEL_INFO(temp
,
396 HWMON_CHANNEL_INFO(fan
,
397 HWMON_F_INPUT
| HWMON_F_LABEL
| HWMON_F_TARGET
,
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
404 HWMON_CHANNEL_INFO(pwm
,
412 HWMON_CHANNEL_INFO(in
,
420 static const struct hwmon_chip_info ccp_chip_info
= {
421 .ops
= &ccp_hwmon_ops
,
425 /* read fan connection status and set labels */
426 static int get_fan_cnct(struct ccp_device
*ccp
)
432 ret
= send_usb_cmd(ccp
, CTL_GET_FAN_CNCT
, 0, 0, 0);
436 for (channel
= 0; channel
< NUM_FANS
; channel
++) {
437 mode
= ccp
->buffer
[channel
+ 1];
441 set_bit(channel
, ccp
->fan_cnct
);
442 ccp
->target
[channel
] = -ENODATA
;
446 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
447 "fan%d 3pin", channel
+ 1);
450 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
451 "fan%d 4pin", channel
+ 1);
454 scnprintf(ccp
->fan_label
[channel
], LABEL_LENGTH
,
455 "fan%d other", channel
+ 1);
463 /* read temp sensor connection status */
464 static int get_temp_cnct(struct ccp_device
*ccp
)
470 ret
= send_usb_cmd(ccp
, CTL_GET_TMP_CNCT
, 0, 0, 0);
474 for (channel
= 0; channel
< NUM_TEMP_SENSORS
; channel
++) {
475 mode
= ccp
->buffer
[channel
+ 1];
479 set_bit(channel
, ccp
->temp_cnct
);
485 static int ccp_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
487 struct ccp_device
*ccp
;
490 ccp
= devm_kzalloc(&hdev
->dev
, sizeof(*ccp
), GFP_KERNEL
);
494 ccp
->buffer
= devm_kmalloc(&hdev
->dev
, OUT_BUFFER_SIZE
, GFP_KERNEL
);
498 ret
= hid_parse(hdev
);
502 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
506 ret
= hid_hw_open(hdev
);
511 hid_set_drvdata(hdev
, ccp
);
512 mutex_init(&ccp
->mutex
);
513 init_completion(&ccp
->wait_input_report
);
515 hid_device_io_start(hdev
);
517 /* temp and fan connection status only updates when device is powered on */
518 ret
= get_temp_cnct(ccp
);
522 ret
= get_fan_cnct(ccp
);
525 ccp
->hwmon_dev
= hwmon_device_register_with_info(&hdev
->dev
, "corsaircpro",
526 ccp
, &ccp_chip_info
, 0);
527 if (IS_ERR(ccp
->hwmon_dev
)) {
528 ret
= PTR_ERR(ccp
->hwmon_dev
);
541 static void ccp_remove(struct hid_device
*hdev
)
543 struct ccp_device
*ccp
= hid_get_drvdata(hdev
);
545 hwmon_device_unregister(ccp
->hwmon_dev
);
550 static const struct hid_device_id ccp_devices
[] = {
551 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO
) },
552 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR
, USB_PRODUCT_ID_CORSAIR_1000D
) },
556 static struct hid_driver ccp_driver
= {
557 .name
= "corsair-cpro",
558 .id_table
= ccp_devices
,
560 .remove
= ccp_remove
,
561 .raw_event
= ccp_raw_event
,
564 MODULE_DEVICE_TABLE(hid
, ccp_devices
);
565 MODULE_LICENSE("GPL");
567 static int __init
ccp_init(void)
569 return hid_register_driver(&ccp_driver
);
572 static void __exit
ccp_exit(void)
574 hid_unregister_driver(&ccp_driver
);
578 * When compiling this driver as built-in, hwmon initcalls will get called before the
579 * hid driver and this driver would fail to register. late_initcall solves this.
581 late_initcall(ccp_init
);
582 module_exit(ccp_exit
);