corsair-psu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * corsair-psu.c - Linux driver for Corsair power supplies with HID sensors interface
  4. * Copyright (C) 2020 Wilken Gottwalt <[email protected]>
  5. */
  6. #include <linux/completion.h>
  7. #include <linux/debugfs.h>
  8. #include <linux/errno.h>
  9. #include <linux/hid.h>
  10. #include <linux/hwmon.h>
  11. #include <linux/hwmon-sysfs.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/types.h>
  18. /*
  19. * Corsair protocol for PSUs
  20. *
  21. * message size = 64 bytes (request and response, little endian)
  22. * request:
  23. * [length][command][param0][param1][paramX]...
  24. * reply:
  25. * [echo of length][echo of command][data0][data1][dataX]...
  26. *
  27. * - commands are byte sized opcodes
  28. * - length is the sum of all bytes of the commands/params
  29. * - the micro-controller of most of these PSUs support concatenation in the request and reply,
  30. * but it is better to not rely on this (it is also hard to parse)
  31. * - the driver uses raw events to be accessible from userspace (though this is not really
  32. * supported, it is just there for convenience, may be removed in the future)
  33. * - a reply always start with the length and command in the same order the request used it
  34. * - length of the reply data is specific to the command used
  35. * - some of the commands work on a rail and can be switched to a specific rail (0 = 12v,
  36. * 1 = 5v, 2 = 3.3v)
  37. * - the format of the init command 0xFE is swapped length/command bytes
  38. * - parameter bytes amount and values are specific to the command (rail setting is the only
  39. * for now that uses non-zero values)
  40. * - there are much more commands, especially for configuring the device, but they are not
  41. * supported because a wrong command/length can lockup the micro-controller
  42. * - the driver supports debugfs for values not fitting into the hwmon class
  43. * - not every device class (HXi, RMi or AXi) supports all commands
  44. * - it is a pure sensors reading driver (will not support configuring)
  45. */
  46. #define DRIVER_NAME "corsair-psu"
  47. #define REPLY_SIZE 16 /* max length of a reply to a single command */
  48. #define CMD_BUFFER_SIZE 64
  49. #define CMD_TIMEOUT_MS 250
  50. #define SECONDS_PER_HOUR (60 * 60)
  51. #define SECONDS_PER_DAY (SECONDS_PER_HOUR * 24)
  52. #define RAIL_COUNT 3 /* 3v3 + 5v + 12v */
  53. #define TEMP_COUNT 2
  54. #define OCP_MULTI_RAIL 0x02
  55. #define PSU_CMD_SELECT_RAIL 0x00 /* expects length 2 */
  56. #define PSU_CMD_RAIL_VOLTS_HCRIT 0x40 /* the rest of the commands expect length 3 */
  57. #define PSU_CMD_RAIL_VOLTS_LCRIT 0x44
  58. #define PSU_CMD_RAIL_AMPS_HCRIT 0x46
  59. #define PSU_CMD_TEMP_HCRIT 0x4F
  60. #define PSU_CMD_IN_VOLTS 0x88
  61. #define PSU_CMD_IN_AMPS 0x89
  62. #define PSU_CMD_RAIL_VOLTS 0x8B
  63. #define PSU_CMD_RAIL_AMPS 0x8C
  64. #define PSU_CMD_TEMP0 0x8D
  65. #define PSU_CMD_TEMP1 0x8E
  66. #define PSU_CMD_FAN 0x90
  67. #define PSU_CMD_RAIL_WATTS 0x96
  68. #define PSU_CMD_VEND_STR 0x99
  69. #define PSU_CMD_PROD_STR 0x9A
  70. #define PSU_CMD_TOTAL_UPTIME 0xD1
  71. #define PSU_CMD_UPTIME 0xD2
  72. #define PSU_CMD_OCPMODE 0xD8
  73. #define PSU_CMD_TOTAL_WATTS 0xEE
  74. #define PSU_CMD_INIT 0xFE
  75. #define L_IN_VOLTS "v_in"
  76. #define L_OUT_VOLTS_12V "v_out +12v"
  77. #define L_OUT_VOLTS_5V "v_out +5v"
  78. #define L_OUT_VOLTS_3_3V "v_out +3.3v"
  79. #define L_IN_AMPS "curr in"
  80. #define L_AMPS_12V "curr +12v"
  81. #define L_AMPS_5V "curr +5v"
  82. #define L_AMPS_3_3V "curr +3.3v"
  83. #define L_FAN "psu fan"
  84. #define L_TEMP0 "vrm temp"
  85. #define L_TEMP1 "case temp"
  86. #define L_WATTS "power total"
  87. #define L_WATTS_12V "power +12v"
  88. #define L_WATTS_5V "power +5v"
  89. #define L_WATTS_3_3V "power +3.3v"
  90. static const char *const label_watts[] = {
  91. L_WATTS,
  92. L_WATTS_12V,
  93. L_WATTS_5V,
  94. L_WATTS_3_3V
  95. };
  96. static const char *const label_volts[] = {
  97. L_IN_VOLTS,
  98. L_OUT_VOLTS_12V,
  99. L_OUT_VOLTS_5V,
  100. L_OUT_VOLTS_3_3V
  101. };
  102. static const char *const label_amps[] = {
  103. L_IN_AMPS,
  104. L_AMPS_12V,
  105. L_AMPS_5V,
  106. L_AMPS_3_3V
  107. };
  108. struct corsairpsu_data {
  109. struct hid_device *hdev;
  110. struct device *hwmon_dev;
  111. struct dentry *debugfs;
  112. struct completion wait_completion;
  113. struct mutex lock; /* for locking access to cmd_buffer */
  114. u8 *cmd_buffer;
  115. char vendor[REPLY_SIZE];
  116. char product[REPLY_SIZE];
  117. long temp_crit[TEMP_COUNT];
  118. long in_crit[RAIL_COUNT];
  119. long in_lcrit[RAIL_COUNT];
  120. long curr_crit[RAIL_COUNT];
  121. u8 temp_crit_support;
  122. u8 in_crit_support;
  123. u8 in_lcrit_support;
  124. u8 curr_crit_support;
  125. bool in_curr_cmd_support; /* not all commands are supported on every PSU */
  126. };
  127. /* some values are SMBus LINEAR11 data which need a conversion */
  128. static int corsairpsu_linear11_to_int(const u16 val, const int scale)
  129. {
  130. const int exp = ((s16)val) >> 11;
  131. const int mant = (((s16)(val & 0x7ff)) << 5) >> 5;
  132. const int result = mant * scale;
  133. return (exp >= 0) ? (result << exp) : (result >> -exp);
  134. }
  135. static int corsairpsu_usb_cmd(struct corsairpsu_data *priv, u8 p0, u8 p1, u8 p2, void *data)
  136. {
  137. unsigned long time;
  138. int ret;
  139. memset(priv->cmd_buffer, 0, CMD_BUFFER_SIZE);
  140. priv->cmd_buffer[0] = p0;
  141. priv->cmd_buffer[1] = p1;
  142. priv->cmd_buffer[2] = p2;
  143. reinit_completion(&priv->wait_completion);
  144. ret = hid_hw_output_report(priv->hdev, priv->cmd_buffer, CMD_BUFFER_SIZE);
  145. if (ret < 0)
  146. return ret;
  147. time = wait_for_completion_timeout(&priv->wait_completion,
  148. msecs_to_jiffies(CMD_TIMEOUT_MS));
  149. if (!time)
  150. return -ETIMEDOUT;
  151. /*
  152. * at the start of the reply is an echo of the send command/length in the same order it
  153. * was send, not every command is supported on every device class, if a command is not
  154. * supported, the length value in the reply is okay, but the command value is set to 0
  155. */
  156. if (p0 != priv->cmd_buffer[0] || p1 != priv->cmd_buffer[1])
  157. return -EOPNOTSUPP;
  158. if (data)
  159. memcpy(data, priv->cmd_buffer + 2, REPLY_SIZE);
  160. return 0;
  161. }
  162. static int corsairpsu_init(struct corsairpsu_data *priv)
  163. {
  164. /*
  165. * PSU_CMD_INIT uses swapped length/command and expects 2 parameter bytes, this command
  166. * actually generates a reply, but we don't need it
  167. */
  168. return corsairpsu_usb_cmd(priv, PSU_CMD_INIT, 3, 0, NULL);
  169. }
  170. static int corsairpsu_fwinfo(struct corsairpsu_data *priv)
  171. {
  172. int ret;
  173. ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_VEND_STR, 0, priv->vendor);
  174. if (ret < 0)
  175. return ret;
  176. ret = corsairpsu_usb_cmd(priv, 3, PSU_CMD_PROD_STR, 0, priv->product);
  177. if (ret < 0)
  178. return ret;
  179. return 0;
  180. }
  181. static int corsairpsu_request(struct corsairpsu_data *priv, u8 cmd, u8 rail, void *data)
  182. {
  183. int ret;
  184. mutex_lock(&priv->lock);
  185. switch (cmd) {
  186. case PSU_CMD_RAIL_VOLTS_HCRIT:
  187. case PSU_CMD_RAIL_VOLTS_LCRIT:
  188. case PSU_CMD_RAIL_AMPS_HCRIT:
  189. case PSU_CMD_RAIL_VOLTS:
  190. case PSU_CMD_RAIL_AMPS:
  191. case PSU_CMD_RAIL_WATTS:
  192. ret = corsairpsu_usb_cmd(priv, 2, PSU_CMD_SELECT_RAIL, rail, NULL);
  193. if (ret < 0)
  194. goto cmd_fail;
  195. break;
  196. default:
  197. break;
  198. }
  199. ret = corsairpsu_usb_cmd(priv, 3, cmd, 0, data);
  200. cmd_fail:
  201. mutex_unlock(&priv->lock);
  202. return ret;
  203. }
  204. static int corsairpsu_get_value(struct corsairpsu_data *priv, u8 cmd, u8 rail, long *val)
  205. {
  206. u8 data[REPLY_SIZE];
  207. long tmp;
  208. int ret;
  209. ret = corsairpsu_request(priv, cmd, rail, data);
  210. if (ret < 0)
  211. return ret;
  212. /*
  213. * the biggest value here comes from the uptime command and to exceed MAXINT total uptime
  214. * needs to be about 68 years, the rest are u16 values and the biggest value coming out of
  215. * the LINEAR11 conversion are the watts values which are about 1200 for the strongest psu
  216. * supported (HX1200i)
  217. */
  218. tmp = ((long)data[3] << 24) + (data[2] << 16) + (data[1] << 8) + data[0];
  219. switch (cmd) {
  220. case PSU_CMD_RAIL_VOLTS_HCRIT:
  221. case PSU_CMD_RAIL_VOLTS_LCRIT:
  222. case PSU_CMD_RAIL_AMPS_HCRIT:
  223. case PSU_CMD_TEMP_HCRIT:
  224. case PSU_CMD_IN_VOLTS:
  225. case PSU_CMD_IN_AMPS:
  226. case PSU_CMD_RAIL_VOLTS:
  227. case PSU_CMD_RAIL_AMPS:
  228. case PSU_CMD_TEMP0:
  229. case PSU_CMD_TEMP1:
  230. *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1000);
  231. break;
  232. case PSU_CMD_FAN:
  233. *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1);
  234. break;
  235. case PSU_CMD_RAIL_WATTS:
  236. case PSU_CMD_TOTAL_WATTS:
  237. *val = corsairpsu_linear11_to_int(tmp & 0xFFFF, 1000000);
  238. break;
  239. case PSU_CMD_TOTAL_UPTIME:
  240. case PSU_CMD_UPTIME:
  241. case PSU_CMD_OCPMODE:
  242. *val = tmp;
  243. break;
  244. default:
  245. ret = -EOPNOTSUPP;
  246. break;
  247. }
  248. return ret;
  249. }
  250. static void corsairpsu_get_criticals(struct corsairpsu_data *priv)
  251. {
  252. long tmp;
  253. int rail;
  254. for (rail = 0; rail < TEMP_COUNT; ++rail) {
  255. if (!corsairpsu_get_value(priv, PSU_CMD_TEMP_HCRIT, rail, &tmp)) {
  256. priv->temp_crit_support |= BIT(rail);
  257. priv->temp_crit[rail] = tmp;
  258. }
  259. }
  260. for (rail = 0; rail < RAIL_COUNT; ++rail) {
  261. if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS_HCRIT, rail, &tmp)) {
  262. priv->in_crit_support |= BIT(rail);
  263. priv->in_crit[rail] = tmp;
  264. }
  265. if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS_LCRIT, rail, &tmp)) {
  266. priv->in_lcrit_support |= BIT(rail);
  267. priv->in_lcrit[rail] = tmp;
  268. }
  269. if (!corsairpsu_get_value(priv, PSU_CMD_RAIL_AMPS_HCRIT, rail, &tmp)) {
  270. priv->curr_crit_support |= BIT(rail);
  271. priv->curr_crit[rail] = tmp;
  272. }
  273. }
  274. }
  275. static void corsairpsu_check_cmd_support(struct corsairpsu_data *priv)
  276. {
  277. long tmp;
  278. priv->in_curr_cmd_support = !corsairpsu_get_value(priv, PSU_CMD_IN_AMPS, 0, &tmp);
  279. }
  280. static umode_t corsairpsu_hwmon_temp_is_visible(const struct corsairpsu_data *priv, u32 attr,
  281. int channel)
  282. {
  283. umode_t res = 0444;
  284. switch (attr) {
  285. case hwmon_temp_input:
  286. case hwmon_temp_label:
  287. case hwmon_temp_crit:
  288. if (channel > 0 && !(priv->temp_crit_support & BIT(channel - 1)))
  289. res = 0;
  290. break;
  291. default:
  292. break;
  293. }
  294. return res;
  295. }
  296. static umode_t corsairpsu_hwmon_fan_is_visible(const struct corsairpsu_data *priv, u32 attr,
  297. int channel)
  298. {
  299. switch (attr) {
  300. case hwmon_fan_input:
  301. case hwmon_fan_label:
  302. return 0444;
  303. default:
  304. return 0;
  305. }
  306. }
  307. static umode_t corsairpsu_hwmon_power_is_visible(const struct corsairpsu_data *priv, u32 attr,
  308. int channel)
  309. {
  310. switch (attr) {
  311. case hwmon_power_input:
  312. case hwmon_power_label:
  313. return 0444;
  314. default:
  315. return 0;
  316. }
  317. }
  318. static umode_t corsairpsu_hwmon_in_is_visible(const struct corsairpsu_data *priv, u32 attr,
  319. int channel)
  320. {
  321. umode_t res = 0444;
  322. switch (attr) {
  323. case hwmon_in_input:
  324. case hwmon_in_label:
  325. case hwmon_in_crit:
  326. if (channel > 0 && !(priv->in_crit_support & BIT(channel - 1)))
  327. res = 0;
  328. break;
  329. case hwmon_in_lcrit:
  330. if (channel > 0 && !(priv->in_lcrit_support & BIT(channel - 1)))
  331. res = 0;
  332. break;
  333. default:
  334. break;
  335. }
  336. return res;
  337. }
  338. static umode_t corsairpsu_hwmon_curr_is_visible(const struct corsairpsu_data *priv, u32 attr,
  339. int channel)
  340. {
  341. umode_t res = 0444;
  342. switch (attr) {
  343. case hwmon_curr_input:
  344. if (channel == 0 && !priv->in_curr_cmd_support)
  345. res = 0;
  346. break;
  347. case hwmon_curr_label:
  348. case hwmon_curr_crit:
  349. if (channel > 0 && !(priv->curr_crit_support & BIT(channel - 1)))
  350. res = 0;
  351. break;
  352. default:
  353. break;
  354. }
  355. return res;
  356. }
  357. static umode_t corsairpsu_hwmon_ops_is_visible(const void *data, enum hwmon_sensor_types type,
  358. u32 attr, int channel)
  359. {
  360. const struct corsairpsu_data *priv = data;
  361. switch (type) {
  362. case hwmon_temp:
  363. return corsairpsu_hwmon_temp_is_visible(priv, attr, channel);
  364. case hwmon_fan:
  365. return corsairpsu_hwmon_fan_is_visible(priv, attr, channel);
  366. case hwmon_power:
  367. return corsairpsu_hwmon_power_is_visible(priv, attr, channel);
  368. case hwmon_in:
  369. return corsairpsu_hwmon_in_is_visible(priv, attr, channel);
  370. case hwmon_curr:
  371. return corsairpsu_hwmon_curr_is_visible(priv, attr, channel);
  372. default:
  373. return 0;
  374. }
  375. }
  376. static int corsairpsu_hwmon_temp_read(struct corsairpsu_data *priv, u32 attr, int channel,
  377. long *val)
  378. {
  379. int err = -EOPNOTSUPP;
  380. switch (attr) {
  381. case hwmon_temp_input:
  382. return corsairpsu_get_value(priv, channel ? PSU_CMD_TEMP1 : PSU_CMD_TEMP0,
  383. channel, val);
  384. case hwmon_temp_crit:
  385. *val = priv->temp_crit[channel];
  386. err = 0;
  387. break;
  388. default:
  389. break;
  390. }
  391. return err;
  392. }
  393. static int corsairpsu_hwmon_power_read(struct corsairpsu_data *priv, u32 attr, int channel,
  394. long *val)
  395. {
  396. if (attr == hwmon_power_input) {
  397. switch (channel) {
  398. case 0:
  399. return corsairpsu_get_value(priv, PSU_CMD_TOTAL_WATTS, 0, val);
  400. case 1 ... 3:
  401. return corsairpsu_get_value(priv, PSU_CMD_RAIL_WATTS, channel - 1, val);
  402. default:
  403. break;
  404. }
  405. }
  406. return -EOPNOTSUPP;
  407. }
  408. static int corsairpsu_hwmon_in_read(struct corsairpsu_data *priv, u32 attr, int channel, long *val)
  409. {
  410. int err = -EOPNOTSUPP;
  411. switch (attr) {
  412. case hwmon_in_input:
  413. switch (channel) {
  414. case 0:
  415. return corsairpsu_get_value(priv, PSU_CMD_IN_VOLTS, 0, val);
  416. case 1 ... 3:
  417. return corsairpsu_get_value(priv, PSU_CMD_RAIL_VOLTS, channel - 1, val);
  418. default:
  419. break;
  420. }
  421. break;
  422. case hwmon_in_crit:
  423. *val = priv->in_crit[channel - 1];
  424. err = 0;
  425. break;
  426. case hwmon_in_lcrit:
  427. *val = priv->in_lcrit[channel - 1];
  428. err = 0;
  429. break;
  430. }
  431. return err;
  432. }
  433. static int corsairpsu_hwmon_curr_read(struct corsairpsu_data *priv, u32 attr, int channel,
  434. long *val)
  435. {
  436. int err = -EOPNOTSUPP;
  437. switch (attr) {
  438. case hwmon_curr_input:
  439. switch (channel) {
  440. case 0:
  441. return corsairpsu_get_value(priv, PSU_CMD_IN_AMPS, 0, val);
  442. case 1 ... 3:
  443. return corsairpsu_get_value(priv, PSU_CMD_RAIL_AMPS, channel - 1, val);
  444. default:
  445. break;
  446. }
  447. break;
  448. case hwmon_curr_crit:
  449. *val = priv->curr_crit[channel - 1];
  450. err = 0;
  451. break;
  452. default:
  453. break;
  454. }
  455. return err;
  456. }
  457. static int corsairpsu_hwmon_ops_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  458. int channel, long *val)
  459. {
  460. struct corsairpsu_data *priv = dev_get_drvdata(dev);
  461. switch (type) {
  462. case hwmon_temp:
  463. return corsairpsu_hwmon_temp_read(priv, attr, channel, val);
  464. case hwmon_fan:
  465. if (attr == hwmon_fan_input)
  466. return corsairpsu_get_value(priv, PSU_CMD_FAN, 0, val);
  467. return -EOPNOTSUPP;
  468. case hwmon_power:
  469. return corsairpsu_hwmon_power_read(priv, attr, channel, val);
  470. case hwmon_in:
  471. return corsairpsu_hwmon_in_read(priv, attr, channel, val);
  472. case hwmon_curr:
  473. return corsairpsu_hwmon_curr_read(priv, attr, channel, val);
  474. default:
  475. return -EOPNOTSUPP;
  476. }
  477. }
  478. static int corsairpsu_hwmon_ops_read_string(struct device *dev, enum hwmon_sensor_types type,
  479. u32 attr, int channel, const char **str)
  480. {
  481. if (type == hwmon_temp && attr == hwmon_temp_label) {
  482. *str = channel ? L_TEMP1 : L_TEMP0;
  483. return 0;
  484. } else if (type == hwmon_fan && attr == hwmon_fan_label) {
  485. *str = L_FAN;
  486. return 0;
  487. } else if (type == hwmon_power && attr == hwmon_power_label && channel < 4) {
  488. *str = label_watts[channel];
  489. return 0;
  490. } else if (type == hwmon_in && attr == hwmon_in_label && channel < 4) {
  491. *str = label_volts[channel];
  492. return 0;
  493. } else if (type == hwmon_curr && attr == hwmon_curr_label && channel < 4) {
  494. *str = label_amps[channel];
  495. return 0;
  496. }
  497. return -EOPNOTSUPP;
  498. }
  499. static const struct hwmon_ops corsairpsu_hwmon_ops = {
  500. .is_visible = corsairpsu_hwmon_ops_is_visible,
  501. .read = corsairpsu_hwmon_ops_read,
  502. .read_string = corsairpsu_hwmon_ops_read_string,
  503. };
  504. static const struct hwmon_channel_info *corsairpsu_info[] = {
  505. HWMON_CHANNEL_INFO(chip,
  506. HWMON_C_REGISTER_TZ),
  507. HWMON_CHANNEL_INFO(temp,
  508. HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_CRIT,
  509. HWMON_T_INPUT | HWMON_T_LABEL | HWMON_T_CRIT),
  510. HWMON_CHANNEL_INFO(fan,
  511. HWMON_F_INPUT | HWMON_F_LABEL),
  512. HWMON_CHANNEL_INFO(power,
  513. HWMON_P_INPUT | HWMON_P_LABEL,
  514. HWMON_P_INPUT | HWMON_P_LABEL,
  515. HWMON_P_INPUT | HWMON_P_LABEL,
  516. HWMON_P_INPUT | HWMON_P_LABEL),
  517. HWMON_CHANNEL_INFO(in,
  518. HWMON_I_INPUT | HWMON_I_LABEL,
  519. HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT,
  520. HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT,
  521. HWMON_I_INPUT | HWMON_I_LABEL | HWMON_I_LCRIT | HWMON_I_CRIT),
  522. HWMON_CHANNEL_INFO(curr,
  523. HWMON_C_INPUT | HWMON_C_LABEL,
  524. HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT,
  525. HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT,
  526. HWMON_C_INPUT | HWMON_C_LABEL | HWMON_C_CRIT),
  527. NULL
  528. };
  529. static const struct hwmon_chip_info corsairpsu_chip_info = {
  530. .ops = &corsairpsu_hwmon_ops,
  531. .info = corsairpsu_info,
  532. };
  533. #ifdef CONFIG_DEBUG_FS
  534. static void print_uptime(struct seq_file *seqf, u8 cmd)
  535. {
  536. struct corsairpsu_data *priv = seqf->private;
  537. long val;
  538. int ret;
  539. ret = corsairpsu_get_value(priv, cmd, 0, &val);
  540. if (ret < 0) {
  541. seq_puts(seqf, "N/A\n");
  542. return;
  543. }
  544. if (val > SECONDS_PER_DAY) {
  545. seq_printf(seqf, "%ld day(s), %02ld:%02ld:%02ld\n", val / SECONDS_PER_DAY,
  546. val % SECONDS_PER_DAY / SECONDS_PER_HOUR, val % SECONDS_PER_HOUR / 60,
  547. val % 60);
  548. return;
  549. }
  550. seq_printf(seqf, "%02ld:%02ld:%02ld\n", val % SECONDS_PER_DAY / SECONDS_PER_HOUR,
  551. val % SECONDS_PER_HOUR / 60, val % 60);
  552. }
  553. static int uptime_show(struct seq_file *seqf, void *unused)
  554. {
  555. print_uptime(seqf, PSU_CMD_UPTIME);
  556. return 0;
  557. }
  558. DEFINE_SHOW_ATTRIBUTE(uptime);
  559. static int uptime_total_show(struct seq_file *seqf, void *unused)
  560. {
  561. print_uptime(seqf, PSU_CMD_TOTAL_UPTIME);
  562. return 0;
  563. }
  564. DEFINE_SHOW_ATTRIBUTE(uptime_total);
  565. static int vendor_show(struct seq_file *seqf, void *unused)
  566. {
  567. struct corsairpsu_data *priv = seqf->private;
  568. seq_printf(seqf, "%s\n", priv->vendor);
  569. return 0;
  570. }
  571. DEFINE_SHOW_ATTRIBUTE(vendor);
  572. static int product_show(struct seq_file *seqf, void *unused)
  573. {
  574. struct corsairpsu_data *priv = seqf->private;
  575. seq_printf(seqf, "%s\n", priv->product);
  576. return 0;
  577. }
  578. DEFINE_SHOW_ATTRIBUTE(product);
  579. static int ocpmode_show(struct seq_file *seqf, void *unused)
  580. {
  581. struct corsairpsu_data *priv = seqf->private;
  582. long val;
  583. int ret;
  584. /*
  585. * The rail mode is switchable on the fly. The RAW interface can be used for this. But it
  586. * will not be included here, because I consider it somewhat dangerous for the health of the
  587. * PSU. The returned value can be a bogus one, if the PSU is in the process of switching and
  588. * getting of the value itself can also fail during this. Because of this every other value
  589. * than OCP_MULTI_RAIL can be considered as "single rail".
  590. */
  591. ret = corsairpsu_get_value(priv, PSU_CMD_OCPMODE, 0, &val);
  592. if (ret < 0)
  593. seq_puts(seqf, "N/A\n");
  594. else
  595. seq_printf(seqf, "%s\n", (val == OCP_MULTI_RAIL) ? "multi rail" : "single rail");
  596. return 0;
  597. }
  598. DEFINE_SHOW_ATTRIBUTE(ocpmode);
  599. static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
  600. {
  601. char name[32];
  602. scnprintf(name, sizeof(name), "%s-%s", DRIVER_NAME, dev_name(&priv->hdev->dev));
  603. priv->debugfs = debugfs_create_dir(name, NULL);
  604. debugfs_create_file("uptime", 0444, priv->debugfs, priv, &uptime_fops);
  605. debugfs_create_file("uptime_total", 0444, priv->debugfs, priv, &uptime_total_fops);
  606. debugfs_create_file("vendor", 0444, priv->debugfs, priv, &vendor_fops);
  607. debugfs_create_file("product", 0444, priv->debugfs, priv, &product_fops);
  608. debugfs_create_file("ocpmode", 0444, priv->debugfs, priv, &ocpmode_fops);
  609. }
  610. #else
  611. static void corsairpsu_debugfs_init(struct corsairpsu_data *priv)
  612. {
  613. }
  614. #endif
  615. static int corsairpsu_probe(struct hid_device *hdev, const struct hid_device_id *id)
  616. {
  617. struct corsairpsu_data *priv;
  618. int ret;
  619. priv = devm_kzalloc(&hdev->dev, sizeof(struct corsairpsu_data), GFP_KERNEL);
  620. if (!priv)
  621. return -ENOMEM;
  622. priv->cmd_buffer = devm_kmalloc(&hdev->dev, CMD_BUFFER_SIZE, GFP_KERNEL);
  623. if (!priv->cmd_buffer)
  624. return -ENOMEM;
  625. ret = hid_parse(hdev);
  626. if (ret)
  627. return ret;
  628. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  629. if (ret)
  630. return ret;
  631. ret = hid_hw_open(hdev);
  632. if (ret)
  633. goto fail_and_stop;
  634. priv->hdev = hdev;
  635. hid_set_drvdata(hdev, priv);
  636. mutex_init(&priv->lock);
  637. init_completion(&priv->wait_completion);
  638. hid_device_io_start(hdev);
  639. ret = corsairpsu_init(priv);
  640. if (ret < 0) {
  641. dev_err(&hdev->dev, "unable to initialize device (%d)\n", ret);
  642. goto fail_and_stop;
  643. }
  644. ret = corsairpsu_fwinfo(priv);
  645. if (ret < 0) {
  646. dev_err(&hdev->dev, "unable to query firmware (%d)\n", ret);
  647. goto fail_and_stop;
  648. }
  649. corsairpsu_get_criticals(priv);
  650. corsairpsu_check_cmd_support(priv);
  651. priv->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsairpsu", priv,
  652. &corsairpsu_chip_info, NULL);
  653. if (IS_ERR(priv->hwmon_dev)) {
  654. ret = PTR_ERR(priv->hwmon_dev);
  655. goto fail_and_close;
  656. }
  657. corsairpsu_debugfs_init(priv);
  658. return 0;
  659. fail_and_close:
  660. hid_hw_close(hdev);
  661. fail_and_stop:
  662. hid_hw_stop(hdev);
  663. return ret;
  664. }
  665. static void corsairpsu_remove(struct hid_device *hdev)
  666. {
  667. struct corsairpsu_data *priv = hid_get_drvdata(hdev);
  668. debugfs_remove_recursive(priv->debugfs);
  669. hwmon_device_unregister(priv->hwmon_dev);
  670. hid_hw_close(hdev);
  671. hid_hw_stop(hdev);
  672. }
  673. static int corsairpsu_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data,
  674. int size)
  675. {
  676. struct corsairpsu_data *priv = hid_get_drvdata(hdev);
  677. if (completion_done(&priv->wait_completion))
  678. return 0;
  679. memcpy(priv->cmd_buffer, data, min(CMD_BUFFER_SIZE, size));
  680. complete(&priv->wait_completion);
  681. return 0;
  682. }
  683. #ifdef CONFIG_PM
  684. static int corsairpsu_resume(struct hid_device *hdev)
  685. {
  686. struct corsairpsu_data *priv = hid_get_drvdata(hdev);
  687. /* some PSUs turn off the microcontroller during standby, so a reinit is required */
  688. return corsairpsu_init(priv);
  689. }
  690. #endif
  691. static const struct hid_device_id corsairpsu_idtable[] = {
  692. { HID_USB_DEVICE(0x1b1c, 0x1c03) }, /* Corsair HX550i */
  693. { HID_USB_DEVICE(0x1b1c, 0x1c04) }, /* Corsair HX650i */
  694. { HID_USB_DEVICE(0x1b1c, 0x1c05) }, /* Corsair HX750i */
  695. { HID_USB_DEVICE(0x1b1c, 0x1c06) }, /* Corsair HX850i */
  696. { HID_USB_DEVICE(0x1b1c, 0x1c07) }, /* Corsair HX1000i revision 1 */
  697. { HID_USB_DEVICE(0x1b1c, 0x1c08) }, /* Corsair HX1200i */
  698. { HID_USB_DEVICE(0x1b1c, 0x1c09) }, /* Corsair RM550i */
  699. { HID_USB_DEVICE(0x1b1c, 0x1c0a) }, /* Corsair RM650i */
  700. { HID_USB_DEVICE(0x1b1c, 0x1c0b) }, /* Corsair RM750i */
  701. { HID_USB_DEVICE(0x1b1c, 0x1c0c) }, /* Corsair RM850i */
  702. { HID_USB_DEVICE(0x1b1c, 0x1c0d) }, /* Corsair RM1000i */
  703. { HID_USB_DEVICE(0x1b1c, 0x1c1e) }, /* Corsair HX1000i revision 2 */
  704. { HID_USB_DEVICE(0x1b1c, 0x1c1f) }, /* Corsair HX1500i */
  705. { },
  706. };
  707. MODULE_DEVICE_TABLE(hid, corsairpsu_idtable);
  708. static struct hid_driver corsairpsu_driver = {
  709. .name = DRIVER_NAME,
  710. .id_table = corsairpsu_idtable,
  711. .probe = corsairpsu_probe,
  712. .remove = corsairpsu_remove,
  713. .raw_event = corsairpsu_raw_event,
  714. #ifdef CONFIG_PM
  715. .resume = corsairpsu_resume,
  716. .reset_resume = corsairpsu_resume,
  717. #endif
  718. };
  719. module_hid_driver(corsairpsu_driver);
  720. MODULE_LICENSE("GPL");
  721. MODULE_AUTHOR("Wilken Gottwalt <[email protected]>");
  722. MODULE_DESCRIPTION("Linux driver for Corsair power supplies with HID sensors interface");