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 starts 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 * one for now that uses non-zero values)
42 * - the driver supports debugfs for values not fitting into the hwmon class
43 * - not every device class (HXi or RMi) supports all commands
44 * - if configured wrong the PSU resets or shuts down, often before actually hitting the
45 * reported critical temperature
46 * - new models like HX1500i Series 2023 have changes in the reported vendor and product
47 * strings, both are slightly longer now, report vendor and product in one string and are
51 #define DRIVER_NAME "corsair-psu"
53 #define REPLY_SIZE 24 /* max length of a reply to a single command */
54 #define CMD_BUFFER_SIZE 64
55 #define CMD_TIMEOUT_MS 250
56 #define SECONDS_PER_HOUR (60 * 60)
57 #define SECONDS_PER_DAY (SECONDS_PER_HOUR * 24)
58 #define RAIL_COUNT 3 /* 3v3 + 5v + 12v */
60 #define OCP_MULTI_RAIL 0x02
62 #define PSU_CMD_SELECT_RAIL 0x00 /* expects length 2 */
63 #define PSU_CMD_FAN_PWM 0x3B /* the rest of the commands expect length 3 */
64 #define PSU_CMD_RAIL_VOLTS_HCRIT 0x40
65 #define PSU_CMD_RAIL_VOLTS_LCRIT 0x44
66 #define PSU_CMD_RAIL_AMPS_HCRIT 0x46
67 #define PSU_CMD_TEMP_HCRIT 0x4F
68 #define PSU_CMD_IN_VOLTS 0x88
69 #define PSU_CMD_IN_AMPS 0x89
70 #define PSU_CMD_RAIL_VOLTS 0x8B
71 #define PSU_CMD_RAIL_AMPS 0x8C
72 #define PSU_CMD_TEMP0 0x8D
73 #define PSU_CMD_TEMP1 0x8E
74 #define PSU_CMD_FAN 0x90
75 #define PSU_CMD_RAIL_WATTS 0x96
76 #define PSU_CMD_VEND_STR 0x99
77 #define PSU_CMD_PROD_STR 0x9A
78 #define PSU_CMD_TOTAL_UPTIME 0xD1
79 #define PSU_CMD_UPTIME 0xD2
80 #define PSU_CMD_OCPMODE 0xD8
81 #define PSU_CMD_TOTAL_WATTS 0xEE
82 #define PSU_CMD_FAN_PWM_ENABLE 0xF0
83 #define PSU_CMD_INIT 0xFE
85 #define L_IN_VOLTS "v_in"
86 #define L_OUT_VOLTS_12V "v_out +12v"
87 #define L_OUT_VOLTS_5V "v_out +5v"
88 #define L_OUT_VOLTS_3_3V "v_out +3.3v"
89 #define L_IN_AMPS "curr in"
90 #define L_AMPS_12V "curr +12v"
91 #define L_AMPS_5V "curr +5v"
92 #define L_AMPS_3_3V "curr +3.3v"
93 #define L_FAN "psu fan"
94 #define L_TEMP0 "vrm temp"
95 #define L_TEMP1 "case temp"
96 #define L_WATTS "power total"
97 #define L_WATTS_12V "power +12v"
98 #define L_WATTS_5V "power +5v"
99 #define L_WATTS_3_3V "power +3.3v"
101 static const char *const label_watts
[] = {
108 static const char *const label_volts
[] = {
115 static const char *const label_amps
[] = {
122 struct corsairpsu_data
{
123 struct hid_device
*hdev
;
124 struct device
*hwmon_dev
;
125 struct dentry
*debugfs
;
126 struct completion wait_completion
;
127 struct mutex lock
; /* for locking access to cmd_buffer */
129 char vendor
[REPLY_SIZE
];
130 char product
[REPLY_SIZE
];
131 long temp_crit
[TEMP_COUNT
];
132 long in_crit
[RAIL_COUNT
];
133 long in_lcrit
[RAIL_COUNT
];
134 long curr_crit
[RAIL_COUNT
];
135 u8 temp_crit_support
;
138 u8 curr_crit_support
;
139 bool in_curr_cmd_support
; /* not all commands are supported on every PSU */
142 /* some values are SMBus LINEAR11 data which need a conversion */
143 static int corsairpsu_linear11_to_int(const u16 val
, const int scale
)
145 const int exp
= ((s16
)val
) >> 11;
146 const int mant
= (((s16
)(val
& 0x7ff)) << 5) >> 5;
147 const int result
= mant
* scale
;
149 return (exp
>= 0) ? (result
<< exp
) : (result
>> -exp
);
152 /* the micro-controller uses percentage values to control pwm */
153 static int corsairpsu_dutycycle_to_pwm(const long dutycycle
)
155 const int result
= (256 << 16) / 100;
157 return (result
* dutycycle
) >> 16;
160 static int corsairpsu_usb_cmd(struct corsairpsu_data
*priv
, u8 p0
, u8 p1
, u8 p2
, void *data
)
165 memset(priv
->cmd_buffer
, 0, CMD_BUFFER_SIZE
);
166 priv
->cmd_buffer
[0] = p0
;
167 priv
->cmd_buffer
[1] = p1
;
168 priv
->cmd_buffer
[2] = p2
;
170 reinit_completion(&priv
->wait_completion
);
172 ret
= hid_hw_output_report(priv
->hdev
, priv
->cmd_buffer
, CMD_BUFFER_SIZE
);
176 time
= wait_for_completion_timeout(&priv
->wait_completion
,
177 msecs_to_jiffies(CMD_TIMEOUT_MS
));
182 * at the start of the reply is an echo of the send command/length in the same order it
183 * was send, not every command is supported on every device class, if a command is not
184 * supported, the length value in the reply is okay, but the command value is set to 0
186 if (p0
!= priv
->cmd_buffer
[0] || p1
!= priv
->cmd_buffer
[1])
190 memcpy(data
, priv
->cmd_buffer
+ 2, REPLY_SIZE
);
195 static int corsairpsu_init(struct corsairpsu_data
*priv
)
198 * PSU_CMD_INIT uses swapped length/command and expects 2 parameter bytes, this command
199 * actually generates a reply, but we don't need it
201 return corsairpsu_usb_cmd(priv
, PSU_CMD_INIT
, 3, 0, NULL
);
204 static int corsairpsu_fwinfo(struct corsairpsu_data
*priv
)
208 ret
= corsairpsu_usb_cmd(priv
, 3, PSU_CMD_VEND_STR
, 0, priv
->vendor
);
212 ret
= corsairpsu_usb_cmd(priv
, 3, PSU_CMD_PROD_STR
, 0, priv
->product
);
219 static int corsairpsu_request(struct corsairpsu_data
*priv
, u8 cmd
, u8 rail
, void *data
)
223 mutex_lock(&priv
->lock
);
225 case PSU_CMD_RAIL_VOLTS_HCRIT
:
226 case PSU_CMD_RAIL_VOLTS_LCRIT
:
227 case PSU_CMD_RAIL_AMPS_HCRIT
:
228 case PSU_CMD_RAIL_VOLTS
:
229 case PSU_CMD_RAIL_AMPS
:
230 case PSU_CMD_RAIL_WATTS
:
231 ret
= corsairpsu_usb_cmd(priv
, 2, PSU_CMD_SELECT_RAIL
, rail
, NULL
);
239 ret
= corsairpsu_usb_cmd(priv
, 3, cmd
, 0, data
);
242 mutex_unlock(&priv
->lock
);
246 static int corsairpsu_get_value(struct corsairpsu_data
*priv
, u8 cmd
, u8 rail
, long *val
)
252 ret
= corsairpsu_request(priv
, cmd
, rail
, data
);
257 * the biggest value here comes from the uptime command and to exceed MAXINT total uptime
258 * needs to be about 68 years, the rest are u16 values and the biggest value coming out of
259 * the LINEAR11 conversion are the watts values which are about 1500 for the strongest psu
260 * supported (HX1500i)
262 tmp
= ((long)data
[3] << 24) + (data
[2] << 16) + (data
[1] << 8) + data
[0];
264 case PSU_CMD_RAIL_VOLTS_HCRIT
:
265 case PSU_CMD_RAIL_VOLTS_LCRIT
:
266 case PSU_CMD_RAIL_AMPS_HCRIT
:
267 case PSU_CMD_TEMP_HCRIT
:
268 case PSU_CMD_IN_VOLTS
:
269 case PSU_CMD_IN_AMPS
:
270 case PSU_CMD_RAIL_VOLTS
:
271 case PSU_CMD_RAIL_AMPS
:
274 *val
= corsairpsu_linear11_to_int(tmp
& 0xFFFF, 1000);
277 *val
= corsairpsu_linear11_to_int(tmp
& 0xFFFF, 1);
279 case PSU_CMD_FAN_PWM_ENABLE
:
280 *val
= corsairpsu_linear11_to_int(tmp
& 0xFFFF, 1);
282 * 0 = automatic mode, means the micro-controller controls the fan using a plan
283 * which can be modified, but changing this plan is not supported by this
284 * driver, the matching PWM mode is automatic fan speed control = PWM 2
285 * 1 = fixed mode, fan runs at a fixed speed represented by a percentage
286 * value 0-100, this matches the PWM manual fan speed control = PWM 1
287 * technically there is no PWM no fan speed control mode, it would be a combination
293 case PSU_CMD_FAN_PWM
:
294 *val
= corsairpsu_linear11_to_int(tmp
& 0xFFFF, 1);
295 *val
= corsairpsu_dutycycle_to_pwm(*val
);
297 case PSU_CMD_RAIL_WATTS
:
298 case PSU_CMD_TOTAL_WATTS
:
299 *val
= corsairpsu_linear11_to_int(tmp
& 0xFFFF, 1000000);
301 case PSU_CMD_TOTAL_UPTIME
:
303 case PSU_CMD_OCPMODE
:
314 static void corsairpsu_get_criticals(struct corsairpsu_data
*priv
)
319 for (rail
= 0; rail
< TEMP_COUNT
; ++rail
) {
320 if (!corsairpsu_get_value(priv
, PSU_CMD_TEMP_HCRIT
, rail
, &tmp
)) {
321 priv
->temp_crit_support
|= BIT(rail
);
322 priv
->temp_crit
[rail
] = tmp
;
326 for (rail
= 0; rail
< RAIL_COUNT
; ++rail
) {
327 if (!corsairpsu_get_value(priv
, PSU_CMD_RAIL_VOLTS_HCRIT
, rail
, &tmp
)) {
328 priv
->in_crit_support
|= BIT(rail
);
329 priv
->in_crit
[rail
] = tmp
;
332 if (!corsairpsu_get_value(priv
, PSU_CMD_RAIL_VOLTS_LCRIT
, rail
, &tmp
)) {
333 priv
->in_lcrit_support
|= BIT(rail
);
334 priv
->in_lcrit
[rail
] = tmp
;
337 if (!corsairpsu_get_value(priv
, PSU_CMD_RAIL_AMPS_HCRIT
, rail
, &tmp
)) {
338 priv
->curr_crit_support
|= BIT(rail
);
339 priv
->curr_crit
[rail
] = tmp
;
344 static void corsairpsu_check_cmd_support(struct corsairpsu_data
*priv
)
348 priv
->in_curr_cmd_support
= !corsairpsu_get_value(priv
, PSU_CMD_IN_AMPS
, 0, &tmp
);
351 static umode_t
corsairpsu_hwmon_temp_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
357 case hwmon_temp_input
:
358 case hwmon_temp_label
:
359 case hwmon_temp_crit
:
360 if (channel
> 0 && !(priv
->temp_crit_support
& BIT(channel
- 1)))
370 static umode_t
corsairpsu_hwmon_fan_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
374 case hwmon_fan_input
:
375 case hwmon_fan_label
:
382 static umode_t
corsairpsu_hwmon_pwm_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
386 case hwmon_pwm_input
:
387 case hwmon_pwm_enable
:
394 static umode_t
corsairpsu_hwmon_power_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
398 case hwmon_power_input
:
399 case hwmon_power_label
:
406 static umode_t
corsairpsu_hwmon_in_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
415 if (channel
> 0 && !(priv
->in_crit_support
& BIT(channel
- 1)))
419 if (channel
> 0 && !(priv
->in_lcrit_support
& BIT(channel
- 1)))
429 static umode_t
corsairpsu_hwmon_curr_is_visible(const struct corsairpsu_data
*priv
, u32 attr
,
435 case hwmon_curr_input
:
436 if (channel
== 0 && !priv
->in_curr_cmd_support
)
439 case hwmon_curr_label
:
440 case hwmon_curr_crit
:
441 if (channel
> 0 && !(priv
->curr_crit_support
& BIT(channel
- 1)))
451 static umode_t
corsairpsu_hwmon_ops_is_visible(const void *data
, enum hwmon_sensor_types type
,
452 u32 attr
, int channel
)
454 const struct corsairpsu_data
*priv
= data
;
458 return corsairpsu_hwmon_temp_is_visible(priv
, attr
, channel
);
460 return corsairpsu_hwmon_fan_is_visible(priv
, attr
, channel
);
462 return corsairpsu_hwmon_pwm_is_visible(priv
, attr
, channel
);
464 return corsairpsu_hwmon_power_is_visible(priv
, attr
, channel
);
466 return corsairpsu_hwmon_in_is_visible(priv
, attr
, channel
);
468 return corsairpsu_hwmon_curr_is_visible(priv
, attr
, channel
);
474 static int corsairpsu_hwmon_temp_read(struct corsairpsu_data
*priv
, u32 attr
, int channel
,
477 int err
= -EOPNOTSUPP
;
480 case hwmon_temp_input
:
481 return corsairpsu_get_value(priv
, channel
? PSU_CMD_TEMP1
: PSU_CMD_TEMP0
,
483 case hwmon_temp_crit
:
484 *val
= priv
->temp_crit
[channel
];
494 static int corsairpsu_hwmon_pwm_read(struct corsairpsu_data
*priv
, u32 attr
, int channel
, long *val
)
497 case hwmon_pwm_input
:
498 return corsairpsu_get_value(priv
, PSU_CMD_FAN_PWM
, 0, val
);
499 case hwmon_pwm_enable
:
500 return corsairpsu_get_value(priv
, PSU_CMD_FAN_PWM_ENABLE
, 0, val
);
508 static int corsairpsu_hwmon_power_read(struct corsairpsu_data
*priv
, u32 attr
, int channel
,
511 if (attr
== hwmon_power_input
) {
514 return corsairpsu_get_value(priv
, PSU_CMD_TOTAL_WATTS
, 0, val
);
516 return corsairpsu_get_value(priv
, PSU_CMD_RAIL_WATTS
, channel
- 1, val
);
525 static int corsairpsu_hwmon_in_read(struct corsairpsu_data
*priv
, u32 attr
, int channel
, long *val
)
527 int err
= -EOPNOTSUPP
;
533 return corsairpsu_get_value(priv
, PSU_CMD_IN_VOLTS
, 0, val
);
535 return corsairpsu_get_value(priv
, PSU_CMD_RAIL_VOLTS
, channel
- 1, val
);
541 *val
= priv
->in_crit
[channel
- 1];
545 *val
= priv
->in_lcrit
[channel
- 1];
553 static int corsairpsu_hwmon_curr_read(struct corsairpsu_data
*priv
, u32 attr
, int channel
,
556 int err
= -EOPNOTSUPP
;
559 case hwmon_curr_input
:
562 return corsairpsu_get_value(priv
, PSU_CMD_IN_AMPS
, 0, val
);
564 return corsairpsu_get_value(priv
, PSU_CMD_RAIL_AMPS
, channel
- 1, val
);
569 case hwmon_curr_crit
:
570 *val
= priv
->curr_crit
[channel
- 1];
580 static int corsairpsu_hwmon_ops_read(struct device
*dev
, enum hwmon_sensor_types type
, u32 attr
,
581 int channel
, long *val
)
583 struct corsairpsu_data
*priv
= dev_get_drvdata(dev
);
587 return corsairpsu_hwmon_temp_read(priv
, attr
, channel
, val
);
589 if (attr
== hwmon_fan_input
)
590 return corsairpsu_get_value(priv
, PSU_CMD_FAN
, 0, val
);
593 return corsairpsu_hwmon_pwm_read(priv
, attr
, channel
, val
);
595 return corsairpsu_hwmon_power_read(priv
, attr
, channel
, val
);
597 return corsairpsu_hwmon_in_read(priv
, attr
, channel
, val
);
599 return corsairpsu_hwmon_curr_read(priv
, attr
, channel
, val
);
605 static int corsairpsu_hwmon_ops_read_string(struct device
*dev
, enum hwmon_sensor_types type
,
606 u32 attr
, int channel
, const char **str
)
608 if (type
== hwmon_temp
&& attr
== hwmon_temp_label
) {
609 *str
= channel
? L_TEMP1
: L_TEMP0
;
611 } else if (type
== hwmon_fan
&& attr
== hwmon_fan_label
) {
614 } else if (type
== hwmon_power
&& attr
== hwmon_power_label
&& channel
< 4) {
615 *str
= label_watts
[channel
];
617 } else if (type
== hwmon_in
&& attr
== hwmon_in_label
&& channel
< 4) {
618 *str
= label_volts
[channel
];
620 } else if (type
== hwmon_curr
&& attr
== hwmon_curr_label
&& channel
< 4) {
621 *str
= label_amps
[channel
];
628 static const struct hwmon_ops corsairpsu_hwmon_ops
= {
629 .is_visible
= corsairpsu_hwmon_ops_is_visible
,
630 .read
= corsairpsu_hwmon_ops_read
,
631 .read_string
= corsairpsu_hwmon_ops_read_string
,
634 static const struct hwmon_channel_info
*const corsairpsu_info
[] = {
635 HWMON_CHANNEL_INFO(chip
,
636 HWMON_C_REGISTER_TZ
),
637 HWMON_CHANNEL_INFO(temp
,
638 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
,
639 HWMON_T_INPUT
| HWMON_T_LABEL
| HWMON_T_CRIT
),
640 HWMON_CHANNEL_INFO(fan
,
641 HWMON_F_INPUT
| HWMON_F_LABEL
),
642 HWMON_CHANNEL_INFO(pwm
,
643 HWMON_PWM_INPUT
| HWMON_PWM_ENABLE
),
644 HWMON_CHANNEL_INFO(power
,
645 HWMON_P_INPUT
| HWMON_P_LABEL
,
646 HWMON_P_INPUT
| HWMON_P_LABEL
,
647 HWMON_P_INPUT
| HWMON_P_LABEL
,
648 HWMON_P_INPUT
| HWMON_P_LABEL
),
649 HWMON_CHANNEL_INFO(in
,
650 HWMON_I_INPUT
| HWMON_I_LABEL
,
651 HWMON_I_INPUT
| HWMON_I_LABEL
| HWMON_I_LCRIT
| HWMON_I_CRIT
,
652 HWMON_I_INPUT
| HWMON_I_LABEL
| HWMON_I_LCRIT
| HWMON_I_CRIT
,
653 HWMON_I_INPUT
| HWMON_I_LABEL
| HWMON_I_LCRIT
| HWMON_I_CRIT
),
654 HWMON_CHANNEL_INFO(curr
,
655 HWMON_C_INPUT
| HWMON_C_LABEL
,
656 HWMON_C_INPUT
| HWMON_C_LABEL
| HWMON_C_CRIT
,
657 HWMON_C_INPUT
| HWMON_C_LABEL
| HWMON_C_CRIT
,
658 HWMON_C_INPUT
| HWMON_C_LABEL
| HWMON_C_CRIT
),
662 static const struct hwmon_chip_info corsairpsu_chip_info
= {
663 .ops
= &corsairpsu_hwmon_ops
,
664 .info
= corsairpsu_info
,
667 #ifdef CONFIG_DEBUG_FS
669 static void print_uptime(struct seq_file
*seqf
, u8 cmd
)
671 struct corsairpsu_data
*priv
= seqf
->private;
675 ret
= corsairpsu_get_value(priv
, cmd
, 0, &val
);
677 seq_puts(seqf
, "N/A\n");
681 if (val
> SECONDS_PER_DAY
) {
682 seq_printf(seqf
, "%ld day(s), %02ld:%02ld:%02ld\n", val
/ SECONDS_PER_DAY
,
683 val
% SECONDS_PER_DAY
/ SECONDS_PER_HOUR
, val
% SECONDS_PER_HOUR
/ 60,
688 seq_printf(seqf
, "%02ld:%02ld:%02ld\n", val
% SECONDS_PER_DAY
/ SECONDS_PER_HOUR
,
689 val
% SECONDS_PER_HOUR
/ 60, val
% 60);
692 static int uptime_show(struct seq_file
*seqf
, void *unused
)
694 print_uptime(seqf
, PSU_CMD_UPTIME
);
698 DEFINE_SHOW_ATTRIBUTE(uptime
);
700 static int uptime_total_show(struct seq_file
*seqf
, void *unused
)
702 print_uptime(seqf
, PSU_CMD_TOTAL_UPTIME
);
706 DEFINE_SHOW_ATTRIBUTE(uptime_total
);
708 static int vendor_show(struct seq_file
*seqf
, void *unused
)
710 struct corsairpsu_data
*priv
= seqf
->private;
712 seq_printf(seqf
, "%s\n", priv
->vendor
);
716 DEFINE_SHOW_ATTRIBUTE(vendor
);
718 static int product_show(struct seq_file
*seqf
, void *unused
)
720 struct corsairpsu_data
*priv
= seqf
->private;
722 seq_printf(seqf
, "%s\n", priv
->product
);
726 DEFINE_SHOW_ATTRIBUTE(product
);
728 static int ocpmode_show(struct seq_file
*seqf
, void *unused
)
730 struct corsairpsu_data
*priv
= seqf
->private;
735 * The rail mode is switchable on the fly. The RAW interface can be used for this. But it
736 * will not be included here, because I consider it somewhat dangerous for the health of the
737 * PSU. The returned value can be a bogus one, if the PSU is in the process of switching and
738 * getting of the value itself can also fail during this. Because of this every other value
739 * than OCP_MULTI_RAIL can be considered as "single rail".
741 ret
= corsairpsu_get_value(priv
, PSU_CMD_OCPMODE
, 0, &val
);
743 seq_puts(seqf
, "N/A\n");
745 seq_printf(seqf
, "%s\n", (val
== OCP_MULTI_RAIL
) ? "multi rail" : "single rail");
749 DEFINE_SHOW_ATTRIBUTE(ocpmode
);
751 static void corsairpsu_debugfs_init(struct corsairpsu_data
*priv
)
755 scnprintf(name
, sizeof(name
), "%s-%s", DRIVER_NAME
, dev_name(&priv
->hdev
->dev
));
757 priv
->debugfs
= debugfs_create_dir(name
, NULL
);
758 debugfs_create_file("uptime", 0444, priv
->debugfs
, priv
, &uptime_fops
);
759 debugfs_create_file("uptime_total", 0444, priv
->debugfs
, priv
, &uptime_total_fops
);
760 debugfs_create_file("vendor", 0444, priv
->debugfs
, priv
, &vendor_fops
);
761 debugfs_create_file("product", 0444, priv
->debugfs
, priv
, &product_fops
);
762 debugfs_create_file("ocpmode", 0444, priv
->debugfs
, priv
, &ocpmode_fops
);
767 static void corsairpsu_debugfs_init(struct corsairpsu_data
*priv
)
773 static int corsairpsu_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
775 struct corsairpsu_data
*priv
;
778 priv
= devm_kzalloc(&hdev
->dev
, sizeof(struct corsairpsu_data
), GFP_KERNEL
);
782 priv
->cmd_buffer
= devm_kmalloc(&hdev
->dev
, CMD_BUFFER_SIZE
, GFP_KERNEL
);
783 if (!priv
->cmd_buffer
)
786 ret
= hid_parse(hdev
);
790 ret
= hid_hw_start(hdev
, HID_CONNECT_HIDRAW
);
794 ret
= hid_hw_open(hdev
);
799 hid_set_drvdata(hdev
, priv
);
800 mutex_init(&priv
->lock
);
801 init_completion(&priv
->wait_completion
);
803 hid_device_io_start(hdev
);
805 ret
= corsairpsu_init(priv
);
807 dev_err(&hdev
->dev
, "unable to initialize device (%d)\n", ret
);
811 ret
= corsairpsu_fwinfo(priv
);
813 dev_err(&hdev
->dev
, "unable to query firmware (%d)\n", ret
);
817 corsairpsu_get_criticals(priv
);
818 corsairpsu_check_cmd_support(priv
);
820 priv
->hwmon_dev
= hwmon_device_register_with_info(&hdev
->dev
, "corsairpsu", priv
,
821 &corsairpsu_chip_info
, NULL
);
823 if (IS_ERR(priv
->hwmon_dev
)) {
824 ret
= PTR_ERR(priv
->hwmon_dev
);
828 corsairpsu_debugfs_init(priv
);
839 static void corsairpsu_remove(struct hid_device
*hdev
)
841 struct corsairpsu_data
*priv
= hid_get_drvdata(hdev
);
843 debugfs_remove_recursive(priv
->debugfs
);
844 hwmon_device_unregister(priv
->hwmon_dev
);
849 static int corsairpsu_raw_event(struct hid_device
*hdev
, struct hid_report
*report
, u8
*data
,
852 struct corsairpsu_data
*priv
= hid_get_drvdata(hdev
);
854 if (completion_done(&priv
->wait_completion
))
857 memcpy(priv
->cmd_buffer
, data
, min(CMD_BUFFER_SIZE
, size
));
858 complete(&priv
->wait_completion
);
864 static int corsairpsu_resume(struct hid_device
*hdev
)
866 struct corsairpsu_data
*priv
= hid_get_drvdata(hdev
);
868 /* some PSUs turn off the microcontroller during standby, so a reinit is required */
869 return corsairpsu_init(priv
);
873 static const struct hid_device_id corsairpsu_idtable
[] = {
874 { HID_USB_DEVICE(0x1b1c, 0x1c03) }, /* Corsair HX550i */
875 { HID_USB_DEVICE(0x1b1c, 0x1c04) }, /* Corsair HX650i */
876 { HID_USB_DEVICE(0x1b1c, 0x1c05) }, /* Corsair HX750i */
877 { HID_USB_DEVICE(0x1b1c, 0x1c06) }, /* Corsair HX850i */
878 { HID_USB_DEVICE(0x1b1c, 0x1c07) }, /* Corsair HX1000i Legacy */
879 { HID_USB_DEVICE(0x1b1c, 0x1c08) }, /* Corsair HX1200i Legacy */
880 { HID_USB_DEVICE(0x1b1c, 0x1c09) }, /* Corsair RM550i */
881 { HID_USB_DEVICE(0x1b1c, 0x1c0a) }, /* Corsair RM650i */
882 { HID_USB_DEVICE(0x1b1c, 0x1c0b) }, /* Corsair RM750i */
883 { HID_USB_DEVICE(0x1b1c, 0x1c0c) }, /* Corsair RM850i */
884 { HID_USB_DEVICE(0x1b1c, 0x1c0d) }, /* Corsair RM1000i */
885 { HID_USB_DEVICE(0x1b1c, 0x1c1e) }, /* Corsair HX1000i Series 2023 */
886 { HID_USB_DEVICE(0x1b1c, 0x1c1f) }, /* Corsair HX1500i Legacy and Series 2023 */
887 { HID_USB_DEVICE(0x1b1c, 0x1c23) }, /* Corsair HX1200i Series 2023 */
890 MODULE_DEVICE_TABLE(hid
, corsairpsu_idtable
);
892 static struct hid_driver corsairpsu_driver
= {
894 .id_table
= corsairpsu_idtable
,
895 .probe
= corsairpsu_probe
,
896 .remove
= corsairpsu_remove
,
897 .raw_event
= corsairpsu_raw_event
,
899 .resume
= corsairpsu_resume
,
900 .reset_resume
= corsairpsu_resume
,
904 static int __init
corsair_init(void)
906 return hid_register_driver(&corsairpsu_driver
);
909 static void __exit
corsair_exit(void)
911 hid_unregister_driver(&corsairpsu_driver
);
915 * With module_init() the driver would load before the HID bus when
916 * built-in, so use late_initcall() instead.
918 late_initcall(corsair_init
);
919 module_exit(corsair_exit
);
921 MODULE_LICENSE("GPL");
922 MODULE_AUTHOR("Wilken Gottwalt <wilken.gottwalt@posteo.net>");
923 MODULE_DESCRIPTION("Linux driver for Corsair power supplies with HID sensors interface");