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/spinlock.h>
20 #include <linux/types.h>
22 #define USB_VENDOR_ID_CORSAIR 0x1b1c
23 #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
24 #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
26 #define OUT_BUFFER_SIZE 63
27 #define IN_BUFFER_SIZE 16
28 #define LABEL_LENGTH 11
29 #define REQ_TIMEOUT 300
31 #define CTL_GET_TMP_CNCT 0x10 /*
32 * returns in bytes 1-4 for each temp sensor:
36 #define CTL_GET_TMP 0x11 /*
37 * send: byte 1 is channel, rest zero
38 * rcv: returns temp for channel in centi-degree celsius
40 * returns 0x11 in byte 0 if no sensor is connected
42 #define CTL_GET_VOLT 0x12 /*
43 * send: byte 1 is rail number: 0 = 12v, 1 = 5v, 2 = 3.3v
44 * rcv: returns millivolt in bytes 1,2
45 * returns error 0x10 if request is invalid
47 #define CTL_GET_FAN_CNCT 0x20 /*
48 * returns in bytes 1-6 for each fan:
53 #define CTL_GET_FAN_RPM 0x21 /*
54 * send: byte 1 is channel, rest zero
55 * rcv: returns rpm in bytes 1,2
57 #define CTL_GET_FAN_PWM 0x22 /*
58 * send: byte 1 is channel, rest zero
59 * rcv: returns pwm in byte 1 if it was set
60 * returns error 0x12 if fan is controlled via
61 * fan_target or fan curve
63 #define CTL_SET_FAN_FPWM 0x23 /*
65 * send: byte 1 is fan number
66 * send: byte 2 is percentage from 0 - 100
68 #define CTL_SET_FAN_TARGET 0x24 /*
70 * send: byte 1 is fan number
71 * send: byte 2-3 is target
72 * device accepts all values from 0x00 - 0xFFFF
76 #define NUM_TEMP_SENSORS 4
79 struct hid_device *hdev;
80 struct device *hwmon_dev;
81 /* For reinitializing the completion below */
82 spinlock_t wait_input_report_lock;
83 struct completion wait_input_report;
84 struct mutex mutex; /* whenever buffer is used, lock before send_usb_cmd */
88 DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
89 DECLARE_BITMAP(fan_cnct, NUM_FANS);
90 char fan_label[6][LABEL_LENGTH];
93 /* converts response error in buffer to errno */
94 static int ccp_get_errno(struct ccp_device *ccp)
96 switch (ccp->buffer[0]) {
97 case 0x00: /* success */
99 case 0x01: /* called invalid command */
101 case 0x10: /* called GET_VOLT / GET_TMP with invalid arguments */
103 case 0x11: /* requested temps of disconnected sensors */
104 case 0x12: /* requested pwm of not pwm controlled channels */
107 hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
112 /* send command, check for error in response, response in ccp->buffer */
113 static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
118 memset(ccp->cmd_buffer, 0x00, OUT_BUFFER_SIZE);
119 ccp->cmd_buffer[0] = command;
120 ccp->cmd_buffer[1] = byte1;
121 ccp->cmd_buffer[2] = byte2;
122 ccp->cmd_buffer[3] = byte3;
125 * Disable raw event parsing for a moment to safely reinitialize the
126 * completion. Reinit is done because hidraw could have triggered
127 * the raw event parsing and marked the ccp->wait_input_report
128 * completion as done.
130 spin_lock_bh(&ccp->wait_input_report_lock);
131 reinit_completion(&ccp->wait_input_report);
132 spin_unlock_bh(&ccp->wait_input_report_lock);
134 ret = hid_hw_output_report(ccp->hdev, ccp->cmd_buffer, OUT_BUFFER_SIZE);
138 t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
142 return ccp_get_errno(ccp);
145 static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
147 struct ccp_device *ccp = hid_get_drvdata(hdev);
149 /* only copy buffer when requested */
150 spin_lock(&ccp->wait_input_report_lock);
151 if (!completion_done(&ccp->wait_input_report)) {
152 memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
153 complete_all(&ccp->wait_input_report);
155 spin_unlock(&ccp->wait_input_report_lock);
160 /* requests and returns single data values depending on channel */
161 static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
165 mutex_lock(&ccp->mutex);
167 ret = send_usb_cmd(ccp, command, channel, 0, 0);
171 ret = ccp->buffer[1];
173 ret = (ret << 8) + ccp->buffer[2];
176 mutex_unlock(&ccp->mutex);
180 static int set_pwm(struct ccp_device *ccp, int channel, long val)
184 if (val < 0 || val > 255)
187 /* The Corsair Commander Pro uses values from 0-100 */
188 val = DIV_ROUND_CLOSEST(val * 100, 255);
190 mutex_lock(&ccp->mutex);
192 ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
194 ccp->target[channel] = -ENODATA;
196 mutex_unlock(&ccp->mutex);
200 static int set_target(struct ccp_device *ccp, int channel, long val)
204 val = clamp_val(val, 0, 0xFFFF);
205 ccp->target[channel] = val;
207 mutex_lock(&ccp->mutex);
208 ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
210 mutex_unlock(&ccp->mutex);
214 static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
215 u32 attr, int channel, const char **str)
217 struct ccp_device *ccp = dev_get_drvdata(dev);
222 case hwmon_fan_label:
223 *str = ccp->fan_label[channel];
236 static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
237 u32 attr, int channel, long *val)
239 struct ccp_device *ccp = dev_get_drvdata(dev);
245 case hwmon_temp_input:
246 ret = get_data(ccp, CTL_GET_TMP, channel, true);
257 case hwmon_fan_input:
258 ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
263 case hwmon_fan_target:
264 /* how to read target values from the device is unknown */
265 /* driver returns last set value or 0 */
266 if (ccp->target[channel] < 0)
268 *val = ccp->target[channel];
276 case hwmon_pwm_input:
277 ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
280 *val = DIV_ROUND_CLOSEST(ret * 255, 100);
289 ret = get_data(ccp, CTL_GET_VOLT, channel, true);
305 static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
306 u32 attr, int channel, long val)
308 struct ccp_device *ccp = dev_get_drvdata(dev);
313 case hwmon_pwm_input:
314 return set_pwm(ccp, channel, val);
321 case hwmon_fan_target:
322 return set_target(ccp, channel, val);
333 static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
334 u32 attr, int channel)
336 const struct ccp_device *ccp = data;
340 if (!test_bit(channel, ccp->temp_cnct))
344 case hwmon_temp_input:
346 case hwmon_temp_label:
353 if (!test_bit(channel, ccp->fan_cnct))
357 case hwmon_fan_input:
359 case hwmon_fan_label:
361 case hwmon_fan_target:
368 if (!test_bit(channel, ccp->fan_cnct))
372 case hwmon_pwm_input:
393 static const struct hwmon_ops ccp_hwmon_ops = {
394 .is_visible = ccp_is_visible,
396 .read_string = ccp_read_string,
400 static const struct hwmon_channel_info *ccp_info[] = {
401 HWMON_CHANNEL_INFO(chip,
402 HWMON_C_REGISTER_TZ),
403 HWMON_CHANNEL_INFO(temp,
409 HWMON_CHANNEL_INFO(fan,
410 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
411 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
412 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
413 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
414 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
415 HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
417 HWMON_CHANNEL_INFO(pwm,
425 HWMON_CHANNEL_INFO(in,
433 static const struct hwmon_chip_info ccp_chip_info = {
434 .ops = &ccp_hwmon_ops,
438 /* read fan connection status and set labels */
439 static int get_fan_cnct(struct ccp_device *ccp)
445 ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
449 for (channel = 0; channel < NUM_FANS; channel++) {
450 mode = ccp->buffer[channel + 1];
454 set_bit(channel, ccp->fan_cnct);
455 ccp->target[channel] = -ENODATA;
459 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
460 "fan%d 3pin", channel + 1);
463 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
464 "fan%d 4pin", channel + 1);
467 scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
468 "fan%d other", channel + 1);
476 /* read temp sensor connection status */
477 static int get_temp_cnct(struct ccp_device *ccp)
483 ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
487 for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
488 mode = ccp->buffer[channel + 1];
492 set_bit(channel, ccp->temp_cnct);
498 static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
500 struct ccp_device *ccp;
503 ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
507 ccp->cmd_buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
508 if (!ccp->cmd_buffer)
511 ccp->buffer = devm_kmalloc(&hdev->dev, IN_BUFFER_SIZE, GFP_KERNEL);
515 ret = hid_parse(hdev);
519 ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
523 ret = hid_hw_open(hdev);
528 hid_set_drvdata(hdev, ccp);
530 mutex_init(&ccp->mutex);
531 spin_lock_init(&ccp->wait_input_report_lock);
532 init_completion(&ccp->wait_input_report);
534 hid_device_io_start(hdev);
536 /* temp and fan connection status only updates when device is powered on */
537 ret = get_temp_cnct(ccp);
541 ret = get_fan_cnct(ccp);
544 ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
545 ccp, &ccp_chip_info, 0);
546 if (IS_ERR(ccp->hwmon_dev)) {
547 ret = PTR_ERR(ccp->hwmon_dev);
560 static void ccp_remove(struct hid_device *hdev)
562 struct ccp_device *ccp = hid_get_drvdata(hdev);
564 hwmon_device_unregister(ccp->hwmon_dev);
569 static const struct hid_device_id ccp_devices[] = {
570 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
571 { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
575 static struct hid_driver ccp_driver = {
576 .name = "corsair-cpro",
577 .id_table = ccp_devices,
579 .remove = ccp_remove,
580 .raw_event = ccp_raw_event,
583 MODULE_DEVICE_TABLE(hid, ccp_devices);
584 MODULE_LICENSE("GPL");
586 static int __init ccp_init(void)
588 return hid_register_driver(&ccp_driver);
591 static void __exit ccp_exit(void)
593 hid_unregister_driver(&ccp_driver);
597 * When compiling this driver as built-in, hwmon initcalls will get called before the
598 * hid driver and this driver would fail to register. late_initcall solves this.
600 late_initcall(ccp_init);
601 module_exit(ccp_exit);