1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * corsair-psu.c - Linux driver for Corsair power supplies with HID sensors interface
4 * Copyright (C) 2020 Wilken Gottwalt <wilken.gottwalt@posteo.net>
7 #include <linux/completion.h>
8 #include <linux/debugfs.h>
9 #include <linux/errno.h>
10 #include <linux/hid.h>
11 #include <linux/hwmon.h>
12 #include <linux/hwmon-sysfs.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
21 * Corsair protocol for PSUs
23 * message size = 64 bytes (request and response, little endian)
25 * [length][command][param0][param1][paramX]...
27 * [echo of length][echo of command][data0][data1][dataX]...
29 * - commands are byte sized opcodes
30 * - length is the sum of all bytes of the commands/params
31 * - the micro-controller of most of these PSUs support concatenation in the request and reply,
32 * but it is better to not rely on this (it is also hard to parse)
33 * - the driver uses raw events to be accessible from userspace (though this is not really
34 * supported, it is just there for convenience, may be removed in the future)
35 * - a reply always start with the length and command in the same order the request used it
36 * - length of the reply data is specific to the command used
37 * - some of the commands work on a rail and can be switched to a specific rail (0 = 12v,
39 * - the format of the init command 0xFE is swapped length/command bytes
40 * - parameter bytes amount and values are specific to the command (rail setting is the only
41 * for now that uses non-zero values)
42 * - there are much more commands, especially for configuring the device, but they are not
43 * supported because a wrong command/length can lockup the micro-controller
44 * - the driver supports debugfs for values not fitting into the hwmon class
45 * - not every device class (HXi, RMi or AXi) supports all commands
46 * - it is a pure sensors reading driver (will not support configuring)
49 #define DRIVER_NAME "corsair-psu"
51 #define REPLY_SIZE 16 /* max length of a reply to a single command */
52 #define CMD_BUFFER_SIZE 64
53 #define CMD_TIMEOUT_MS 250
54 #define SECONDS_PER_HOUR (60 * 60)
55 #define SECONDS_PER_DAY (SECONDS_PER_HOUR * 24)
57 #define PSU_CMD_SELECT_RAIL 0x00 /* expects length 2 */
58 #define PSU_CMD_IN_VOLTS 0x88 /* the rest of the commands expect length 3 */
59 #define PSU_CMD_IN_AMPS 0x89
60 #define PSU_CMD_RAIL_OUT_VOLTS 0x8B
61 #define PSU_CMD_RAIL_AMPS 0x8C
62 #define PSU_CMD_TEMP0 0x8D
63 #define PSU_CMD_TEMP1 0x8E
64 #define PSU_CMD_FAN 0x90
65 #define PSU_CMD_RAIL_WATTS 0x96
66 #define PSU_CMD_VEND_STR 0x99
67 #define PSU_CMD_PROD_STR 0x9A
68 #define PSU_CMD_TOTAL_WATTS 0xEE
69 #define PSU_CMD_TOTAL_UPTIME 0xD1
70 #define PSU_CMD_UPTIME 0xD2
71 #define PSU_CMD_INIT 0xFE
73 #define L_IN_VOLTS "v_in"
74 #define L_OUT_VOLTS_12V "v_out +12v"
75 #define L_OUT_VOLTS_5V "v_out +5v"
76 #define L_OUT_VOLTS_3_3V "v_out +3.3v"
77 #define L_IN_AMPS "curr in"
78 #define L_AMPS_12V "curr +12v"
79 #define L_AMPS_5V "curr +5v"
80 #define L_AMPS_3_3V "curr +3.3v"
81 #define L_FAN "psu fan"
82 #define L_TEMP0 "vrm temp"
83 #define L_TEMP1 "case temp"
84 #define L_WATTS "power total"
85 #define L_WATTS_12V "power +12v"
86 #define L_WATTS_5V "power +5v"
87 #define L_WATTS_3_3V "power +3.3v"
89 static const char *const label_watts[] = {
96 static const char *const label_volts[] = {
103 static const char *const label_amps[] = {
110 struct corsairpsu_data {
111 struct hid_device *hdev;
112 struct device *hwmon_dev;
113 struct dentry *debugfs;
114 struct completion wait_completion;
115 struct mutex lock; /* for locking access to cmd_buffer */
117 char vendor[REPLY_SIZE];
118 char product[REPLY_SIZE];
121 /* some values are SMBus LINEAR11 data which need a conversion */
122 static int corsairpsu_linear11_to_int(const int val)
124 int exp = (val & 0xFFFF) >> 0x0B;
125 int mant = val & 0x7FF;
132 if ((mant & 0x01) == 1)
135 for (i = 0; i < -exp; ++i)
138 for (i = 0; i < exp; ++i)
145 static int corsairpsu_usb_cmd(struct corsairpsu_data *priv, u8 p0, u8 p1, u8 p2, void *data)
150 memset(priv->cmd_buffer, 0, CMD_BUFFER_SIZE);
151 priv->cmd_buffer[0] = p0;
152 priv->cmd_buffer[1] = p1;
153 priv->cmd_buffer[2] = p2;
155 reinit_completion(&priv->wait_completion);
157 ret = hid_hw_output_report(priv->hdev, priv->cmd_buffer, CMD_BUFFER_SIZE);
161 time = wait_for_completion_timeout(&priv->wait_completion,
162 msecs_to_jiffies(CMD_TIMEOUT_MS));
167 * at the start of the reply is an echo of the send command/length in the same order it
168 * was send, not every command is supported on every device class, if a command is not
169 * supported, the length value in the reply is okay, but the command value is set to 0
171 if (p0 != priv->cmd_buffer[0] || p1 != priv->cmd_buffer[1])
175 memcpy(data, priv->cmd_buffer + 2, REPLY_SIZE);
180 static int corsairpsu_init(struct corsairpsu_data *priv)
183 * PSU_CMD_INIT uses swapped length/command and expects 2 parameter bytes, this command
184 * actually generates a reply, but we don't need it
186 return corsairpsu_usb_cmd(priv, PSU_CMD_INIT, 3, 0, NULL);
189 static int corsairpsu_fwinfo(struct corsairpsu_data *priv)
193 ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_VEND_STR, 0, priv->vendor);
197 ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_PROD_STR, 0, priv->product);
204 static int corsairpsu_request(struct corsairpsu_data *priv, u8 cmd, u8 rail, void *data)
208 mutex_lock(&priv->lock);
210 case PSU_CMD_RAIL_OUT_VOLTS:
211 case PSU_CMD_RAIL_AMPS:
212 case PSU_CMD_RAIL_WATTS:
213 ret = corsairpsu_usb_cmd(priv, 2, PSU_CMD_SELECT_RAIL, rail, NULL);
221 ret = corsairpsu_usb_cmd(priv, 3, cmd, 0, data);
224 mutex_unlock(&priv->lock);
228 static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, long *val)
234 ret = corsairpsu_request(priv, cmd, rail, data);
239 * the biggest value here comes from the uptime command and to exceed MAXINT total uptime
240 * needs to be about 68 years, the rest are u16 values and the biggest value coming out of
241 * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
242 * supported (HX1200i)
244 tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
246 case PSU_CMD_IN_VOLTS:
247 case PSU_CMD_IN_AMPS:
248 case PSU_CMD_RAIL_OUT_VOLTS:
249 case PSU_CMD_RAIL_AMPS:
252 *val = corsairpsu_linear11_to_int(tmp & 0xFFFF) * 1000;
255 *val = corsairpsu_linear11_to_int(tmp & 0xFFFF);
257 case PSU_CMD_RAIL_WATTS:
258 case PSU_CMD_TOTAL_WATTS:
259 *val = corsairpsu_linear11_to_int(tmp & 0xFFFF) * 1000000;
261 case PSU_CMD_TOTAL_UPTIME:
273 static umode_t corsairpsu_hwmon_ops_is_visible(const void *data, enum hwmon_sensor_types type,
274 u32 attr, int channel)
276 if (type == hwmon_temp && (attr == hwmon_temp_input || attr == hwmon_temp_label))
278 else if (type == hwmon_fan && (attr == hwmon_fan_input || attr == hwmon_fan_label))
280 else if (type == hwmon_power && (attr == hwmon_power_input || attr == hwmon_power_label))
282 else if (type == hwmon_in && (attr == hwmon_in_input || attr == hwmon_in_label))
284 else if (type == hwmon_curr && (attr == hwmon_curr_input || attr == hwmon_curr_label))
290 static int corsairpsu_hwmon_ops_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
291 int channel, long *val)
293 struct corsairpsu_data *priv = dev_get_drvdata(dev);
296 if (type == hwmon_temp && attr == hwmon_temp_input && channel < 2) {
297 ret = corsairpsu_get_value(priv, channel ? PSU_CMD_TEMP1 : PSU_CMD_TEMP0, channel,
299 } else if (type == hwmon_fan && attr == hwmon_fan_input) {
300 ret = corsairpsu_get_value(priv, PSU_CMD_FAN, 0, val);
301 } else if (type == hwmon_power && attr == hwmon_power_input) {
304 ret = corsairpsu_get_value(priv, PSU_CMD_TOTAL_WATTS, 0, val);
307 ret = corsairpsu_get_value(priv, PSU_CMD_RAIL_WATTS, channel - 1, val);
312 } else if (type == hwmon_in && attr == hwmon_in_input) {
315 ret = corsairpsu_get_value(priv, PSU_CMD_IN_VOLTS, 0, val);
318 ret = corsairpsu_get_value(priv, PSU_CMD_RAIL_OUT_VOLTS, channel - 1, val);
323 } else if (type == hwmon_curr && attr == hwmon_curr_input) {
326 ret = corsairpsu_get_value(priv, PSU_CMD_IN_AMPS, 0, val);
329 ret = corsairpsu_get_value(priv, PSU_CMD_RAIL_AMPS, channel - 1, val);
344 static int corsairpsu_hwmon_ops_read_string(struct device *dev, enum hwmon_sensor_types type,
345 u32 attr, int channel, const char **str)
347 if (type == hwmon_temp && attr == hwmon_temp_label) {
348 *str = channel ? L_TEMP1 : L_TEMP0;
350 } else if (type == hwmon_fan && attr == hwmon_fan_label) {
353 } else if (type == hwmon_power && attr == hwmon_power_label && channel < 4) {
354 *str = label_watts[channel];
356 } else if (type == hwmon_in && attr == hwmon_in_label && channel < 4) {
357 *str = label_volts[channel];
359 } else if (type == hwmon_curr && attr == hwmon_curr_label && channel < 4) {
360 *str = label_amps[channel];
367 static const struct hwmon_ops corsairpsu_hwmon_ops = {
368 .is_visible = corsairpsu_hwmon_ops_is_visible,
369 .read = corsairpsu_hwmon_ops_read,
370 .read_string = corsairpsu_hwmon_ops_read_string,
373 static const struct hwmon_channel_info *corsairpsu_info[] = {
374 HWMON_CHANNEL_INFO(chip,
375 HWMON_C_REGISTER_TZ),
376 HWMON_CHANNEL_INFO(temp,
377 HWMON_T_INPUT | HWMON_T_LABEL,
378 HWMON_T_INPUT | HWMON_T_LABEL),
379 HWMON_CHANNEL_INFO(fan,
380 HWMON_F_INPUT | HWMON_F_LABEL),
381 HWMON_CHANNEL_INFO(power,
382 HWMON_P_INPUT | HWMON_P_LABEL,
383 HWMON_P_INPUT | HWMON_P_LABEL,
384 HWMON_P_INPUT | HWMON_P_LABEL,
385 HWMON_P_INPUT | HWMON_P_LABEL),
386 HWMON_CHANNEL_INFO(in,
387 HWMON_I_INPUT | HWMON_I_LABEL,
388 HWMON_I_INPUT | HWMON_I_LABEL,
389 HWMON_I_INPUT | HWMON_I_LABEL,
390 HWMON_I_INPUT | HWMON_I_LABEL),
391 HWMON_CHANNEL_INFO(curr,
392 HWMON_C_INPUT | HWMON_C_LABEL,
393 HWMON_C_INPUT | HWMON_C_LABEL,
394 HWMON_C_INPUT | HWMON_C_LABEL,
395 HWMON_C_INPUT | HWMON_C_LABEL),
399 static const struct hwmon_chip_info corsairpsu_chip_info = {
400 .ops = &corsairpsu_hwmon_ops,
401 .info = corsairpsu_info,
404 #ifdef CONFIG_DEBUG_FS
406 static void print_uptime(struct seq_file *seqf, u8 cmd)
408 struct corsairpsu_data *priv = seqf->private;
412 ret = corsairpsu_get_value(priv, cmd, 0, &val);
414 seq_puts(seqf, "N/A\n");
418 if (val > SECONDS_PER_DAY) {
419 seq_printf(seqf, "%ld day(s), %02ld:%02ld:%02ld\n", val / SECONDS_PER_DAY,
420 val % SECONDS_PER_DAY / SECONDS_PER_HOUR, val % SECONDS_PER_HOUR / 60,
425 seq_printf(seqf, "%02ld:%02ld:%02ld\n", val % SECONDS_PER_DAY / SECONDS_PER_HOUR,
426 val % SECONDS_PER_HOUR / 60, val % 60);
429 static int uptime_show(struct seq_file *seqf, void *unused)
431 print_uptime(seqf, PSU_CMD_UPTIME);
435 DEFINE_SHOW_ATTRIBUTE(uptime);
437 static int uptime_total_show(struct seq_file *seqf, void *unused)
439 print_uptime(seqf, PSU_CMD_TOTAL_UPTIME);
443 DEFINE_SHOW_ATTRIBUTE(uptime_total);
445 static int vendor_show(struct seq_file *seqf, void *unused)
447 struct corsairpsu_data *priv = seqf->private;
449 seq_printf(seqf, "%s\n", priv->vendor);
453 DEFINE_SHOW_ATTRIBUTE(vendor);
455 static int product_show(struct seq_file *seqf, void *unused)
457 struct corsairpsu_data *priv = seqf->private;
459 seq_printf(seqf, "%s\n", priv->product);
463 DEFINE_SHOW_ATTRIBUTE(product);
465 static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
469 scnprintf(name, sizeof(name), "%s-%s", DRIVER_NAME, dev_name(&priv->hdev->dev));
471 priv->debugfs = debugfs_create_dir(name, NULL);
472 debugfs_create_file("uptime", 0444, priv->debugfs, priv, &uptime_fops);
473 debugfs_create_file("uptime_total", 0444, priv->debugfs, priv, &uptime_total_fops);
474 debugfs_create_file("vendor", 0444, priv->debugfs, priv, &vendor_fops);
475 debugfs_create_file("product", 0444, priv->debugfs, priv, &product_fops);
480 static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
486 static int corsairpsu_probe(struct hid_device *hdev, const struct hid_device_id *id)
488 struct corsairpsu_data *priv;
491 priv = devm_kzalloc(&hdev->dev, sizeof(struct corsairpsu_data), GFP_KERNEL);
495 priv->cmd_buffer = devm_kmalloc(&hdev->dev, CMD_BUFFER_SIZE, GFP_KERNEL);
496 if (!priv->cmd_buffer)
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, priv);
513 mutex_init(&priv->lock);
514 init_completion(&priv->wait_completion);
516 hid_device_io_start(hdev);
518 ret = corsairpsu_init(priv);
520 dev_err(&hdev->dev, "unable to initialize device (%d)\n", ret);
524 ret = corsairpsu_fwinfo(priv);
526 dev_err(&hdev->dev, "unable to query firmware (%d)\n", ret);
530 priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsairpsu", priv,
531 &corsairpsu_chip_info, 0);
533 if (IS_ERR(priv->hwmon_dev)) {
534 ret = PTR_ERR(priv->hwmon_dev);
538 corsairpsu_debugfs_init(priv);
549 static void corsairpsu_remove(struct hid_device *hdev)
551 struct corsairpsu_data *priv = hid_get_drvdata(hdev);
553 debugfs_remove_recursive(priv->debugfs);
554 hwmon_device_unregister(priv->hwmon_dev);
559 static int corsairpsu_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
562 struct corsairpsu_data *priv = hid_get_drvdata(hdev);
564 if (completion_done(&priv->wait_completion))
567 memcpy(priv->cmd_buffer, data, min(CMD_BUFFER_SIZE, size));
568 complete(&priv->wait_completion);
573 static const struct hid_device_id corsairpsu_idtable[] = {
574 { HID_USB_DEVICE(0x1b1c, 0x1c03) }, /* Corsair HX550i */
575 { HID_USB_DEVICE(0x1b1c, 0x1c04) }, /* Corsair HX650i */
576 { HID_USB_DEVICE(0x1b1c, 0x1c05) }, /* Corsair HX750i */
577 { HID_USB_DEVICE(0x1b1c, 0x1c06) }, /* Corsair HX850i */
578 { HID_USB_DEVICE(0x1b1c, 0x1c07) }, /* Corsair HX1000i */
579 { HID_USB_DEVICE(0x1b1c, 0x1c08) }, /* Corsair HX1200i */
580 { HID_USB_DEVICE(0x1b1c, 0x1c09) }, /* Corsair RM550i */
581 { HID_USB_DEVICE(0x1b1c, 0x1c0a) }, /* Corsair RM650i */
582 { HID_USB_DEVICE(0x1b1c, 0x1c0b) }, /* Corsair RM750i */
583 { HID_USB_DEVICE(0x1b1c, 0x1c0c) }, /* Corsair RM850i */
584 { HID_USB_DEVICE(0x1b1c, 0x1c0d) }, /* Corsair RM1000i */
587 MODULE_DEVICE_TABLE(hid, corsairpsu_idtable);
589 static struct hid_driver corsairpsu_driver = {
591 .id_table = corsairpsu_idtable,
592 .probe = corsairpsu_probe,
593 .remove = corsairpsu_remove,
594 .raw_event = corsairpsu_raw_event,
596 module_hid_driver(corsairpsu_driver);
598 MODULE_LICENSE("GPL");
599 MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
600 MODULE_DESCRIPTION("Linux driver for Corsair power supplies with HID sensors interface");