corsair-cpro.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * corsair-cpro.c - Linux driver for Corsair Commander Pro
  4. * Copyright (C) 2020 Marius Zachmann <[email protected]>
  5. *
  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.
  9. */
  10. #include <linux/bitops.h>
  11. #include <linux/completion.h>
  12. #include <linux/hid.h>
  13. #include <linux/hwmon.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>
  19. #define USB_VENDOR_ID_CORSAIR 0x1b1c
  20. #define USB_PRODUCT_ID_CORSAIR_COMMANDERPRO 0x0c10
  21. #define USB_PRODUCT_ID_CORSAIR_1000D 0x1d00
  22. #define OUT_BUFFER_SIZE 63
  23. #define IN_BUFFER_SIZE 16
  24. #define LABEL_LENGTH 11
  25. #define REQ_TIMEOUT 300
  26. #define CTL_GET_TMP_CNCT 0x10 /*
  27. * returns in bytes 1-4 for each temp sensor:
  28. * 0 not connected
  29. * 1 connected
  30. */
  31. #define CTL_GET_TMP 0x11 /*
  32. * send: byte 1 is channel, rest zero
  33. * rcv: returns temp for channel in centi-degree celsius
  34. * in bytes 1 and 2
  35. * returns 0x11 in byte 0 if no sensor is connected
  36. */
  37. #define CTL_GET_VOLT 0x12 /*
  38. * send: byte 1 is rail number: 0 = 12v, 1 = 5v, 2 = 3.3v
  39. * rcv: returns millivolt in bytes 1,2
  40. * returns error 0x10 if request is invalid
  41. */
  42. #define CTL_GET_FAN_CNCT 0x20 /*
  43. * returns in bytes 1-6 for each fan:
  44. * 0 not connected
  45. * 1 3pin
  46. * 2 4pin
  47. */
  48. #define CTL_GET_FAN_RPM 0x21 /*
  49. * send: byte 1 is channel, rest zero
  50. * rcv: returns rpm in bytes 1,2
  51. */
  52. #define CTL_GET_FAN_PWM 0x22 /*
  53. * send: byte 1 is channel, rest zero
  54. * rcv: returns pwm in byte 1 if it was set
  55. * returns error 0x12 if fan is controlled via
  56. * fan_target or fan curve
  57. */
  58. #define CTL_SET_FAN_FPWM 0x23 /*
  59. * set fixed pwm
  60. * send: byte 1 is fan number
  61. * send: byte 2 is percentage from 0 - 100
  62. */
  63. #define CTL_SET_FAN_TARGET 0x24 /*
  64. * set target rpm
  65. * send: byte 1 is fan number
  66. * send: byte 2-3 is target
  67. * device accepts all values from 0x00 - 0xFFFF
  68. */
  69. #define NUM_FANS 6
  70. #define NUM_TEMP_SENSORS 4
  71. struct ccp_device {
  72. struct hid_device *hdev;
  73. struct device *hwmon_dev;
  74. struct completion wait_input_report;
  75. struct mutex mutex; /* whenever buffer is used, lock before send_usb_cmd */
  76. u8 *buffer;
  77. int target[6];
  78. DECLARE_BITMAP(temp_cnct, NUM_TEMP_SENSORS);
  79. DECLARE_BITMAP(fan_cnct, NUM_FANS);
  80. char fan_label[6][LABEL_LENGTH];
  81. };
  82. /* converts response error in buffer to errno */
  83. static int ccp_get_errno(struct ccp_device *ccp)
  84. {
  85. switch (ccp->buffer[0]) {
  86. case 0x00: /* success */
  87. return 0;
  88. case 0x01: /* called invalid command */
  89. return -EOPNOTSUPP;
  90. case 0x10: /* called GET_VOLT / GET_TMP with invalid arguments */
  91. return -EINVAL;
  92. case 0x11: /* requested temps of disconnected sensors */
  93. case 0x12: /* requested pwm of not pwm controlled channels */
  94. return -ENODATA;
  95. default:
  96. hid_dbg(ccp->hdev, "unknown device response error: %d", ccp->buffer[0]);
  97. return -EIO;
  98. }
  99. }
  100. /* send command, check for error in response, response in ccp->buffer */
  101. static int send_usb_cmd(struct ccp_device *ccp, u8 command, u8 byte1, u8 byte2, u8 byte3)
  102. {
  103. unsigned long t;
  104. int ret;
  105. memset(ccp->buffer, 0x00, OUT_BUFFER_SIZE);
  106. ccp->buffer[0] = command;
  107. ccp->buffer[1] = byte1;
  108. ccp->buffer[2] = byte2;
  109. ccp->buffer[3] = byte3;
  110. reinit_completion(&ccp->wait_input_report);
  111. ret = hid_hw_output_report(ccp->hdev, ccp->buffer, OUT_BUFFER_SIZE);
  112. if (ret < 0)
  113. return ret;
  114. t = wait_for_completion_timeout(&ccp->wait_input_report, msecs_to_jiffies(REQ_TIMEOUT));
  115. if (!t)
  116. return -ETIMEDOUT;
  117. return ccp_get_errno(ccp);
  118. }
  119. static int ccp_raw_event(struct hid_device *hdev, struct hid_report *report, u8 *data, int size)
  120. {
  121. struct ccp_device *ccp = hid_get_drvdata(hdev);
  122. /* only copy buffer when requested */
  123. if (completion_done(&ccp->wait_input_report))
  124. return 0;
  125. memcpy(ccp->buffer, data, min(IN_BUFFER_SIZE, size));
  126. complete(&ccp->wait_input_report);
  127. return 0;
  128. }
  129. /* requests and returns single data values depending on channel */
  130. static int get_data(struct ccp_device *ccp, int command, int channel, bool two_byte_data)
  131. {
  132. int ret;
  133. mutex_lock(&ccp->mutex);
  134. ret = send_usb_cmd(ccp, command, channel, 0, 0);
  135. if (ret)
  136. goto out_unlock;
  137. ret = ccp->buffer[1];
  138. if (two_byte_data)
  139. ret = (ret << 8) + ccp->buffer[2];
  140. out_unlock:
  141. mutex_unlock(&ccp->mutex);
  142. return ret;
  143. }
  144. static int set_pwm(struct ccp_device *ccp, int channel, long val)
  145. {
  146. int ret;
  147. if (val < 0 || val > 255)
  148. return -EINVAL;
  149. /* The Corsair Commander Pro uses values from 0-100 */
  150. val = DIV_ROUND_CLOSEST(val * 100, 255);
  151. mutex_lock(&ccp->mutex);
  152. ret = send_usb_cmd(ccp, CTL_SET_FAN_FPWM, channel, val, 0);
  153. if (!ret)
  154. ccp->target[channel] = -ENODATA;
  155. mutex_unlock(&ccp->mutex);
  156. return ret;
  157. }
  158. static int set_target(struct ccp_device *ccp, int channel, long val)
  159. {
  160. int ret;
  161. val = clamp_val(val, 0, 0xFFFF);
  162. ccp->target[channel] = val;
  163. mutex_lock(&ccp->mutex);
  164. ret = send_usb_cmd(ccp, CTL_SET_FAN_TARGET, channel, val >> 8, val);
  165. mutex_unlock(&ccp->mutex);
  166. return ret;
  167. }
  168. static int ccp_read_string(struct device *dev, enum hwmon_sensor_types type,
  169. u32 attr, int channel, const char **str)
  170. {
  171. struct ccp_device *ccp = dev_get_drvdata(dev);
  172. switch (type) {
  173. case hwmon_fan:
  174. switch (attr) {
  175. case hwmon_fan_label:
  176. *str = ccp->fan_label[channel];
  177. return 0;
  178. default:
  179. break;
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. return -EOPNOTSUPP;
  186. }
  187. static int ccp_read(struct device *dev, enum hwmon_sensor_types type,
  188. u32 attr, int channel, long *val)
  189. {
  190. struct ccp_device *ccp = dev_get_drvdata(dev);
  191. int ret;
  192. switch (type) {
  193. case hwmon_temp:
  194. switch (attr) {
  195. case hwmon_temp_input:
  196. ret = get_data(ccp, CTL_GET_TMP, channel, true);
  197. if (ret < 0)
  198. return ret;
  199. *val = ret * 10;
  200. return 0;
  201. default:
  202. break;
  203. }
  204. break;
  205. case hwmon_fan:
  206. switch (attr) {
  207. case hwmon_fan_input:
  208. ret = get_data(ccp, CTL_GET_FAN_RPM, channel, true);
  209. if (ret < 0)
  210. return ret;
  211. *val = ret;
  212. return 0;
  213. case hwmon_fan_target:
  214. /* how to read target values from the device is unknown */
  215. /* driver returns last set value or 0 */
  216. if (ccp->target[channel] < 0)
  217. return -ENODATA;
  218. *val = ccp->target[channel];
  219. return 0;
  220. default:
  221. break;
  222. }
  223. break;
  224. case hwmon_pwm:
  225. switch (attr) {
  226. case hwmon_pwm_input:
  227. ret = get_data(ccp, CTL_GET_FAN_PWM, channel, false);
  228. if (ret < 0)
  229. return ret;
  230. *val = DIV_ROUND_CLOSEST(ret * 255, 100);
  231. return 0;
  232. default:
  233. break;
  234. }
  235. break;
  236. case hwmon_in:
  237. switch (attr) {
  238. case hwmon_in_input:
  239. ret = get_data(ccp, CTL_GET_VOLT, channel, true);
  240. if (ret < 0)
  241. return ret;
  242. *val = ret;
  243. return 0;
  244. default:
  245. break;
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. return -EOPNOTSUPP;
  252. };
  253. static int ccp_write(struct device *dev, enum hwmon_sensor_types type,
  254. u32 attr, int channel, long val)
  255. {
  256. struct ccp_device *ccp = dev_get_drvdata(dev);
  257. switch (type) {
  258. case hwmon_pwm:
  259. switch (attr) {
  260. case hwmon_pwm_input:
  261. return set_pwm(ccp, channel, val);
  262. default:
  263. break;
  264. }
  265. break;
  266. case hwmon_fan:
  267. switch (attr) {
  268. case hwmon_fan_target:
  269. return set_target(ccp, channel, val);
  270. default:
  271. break;
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. return -EOPNOTSUPP;
  278. };
  279. static umode_t ccp_is_visible(const void *data, enum hwmon_sensor_types type,
  280. u32 attr, int channel)
  281. {
  282. const struct ccp_device *ccp = data;
  283. switch (type) {
  284. case hwmon_temp:
  285. if (!test_bit(channel, ccp->temp_cnct))
  286. break;
  287. switch (attr) {
  288. case hwmon_temp_input:
  289. return 0444;
  290. case hwmon_temp_label:
  291. return 0444;
  292. default:
  293. break;
  294. }
  295. break;
  296. case hwmon_fan:
  297. if (!test_bit(channel, ccp->fan_cnct))
  298. break;
  299. switch (attr) {
  300. case hwmon_fan_input:
  301. return 0444;
  302. case hwmon_fan_label:
  303. return 0444;
  304. case hwmon_fan_target:
  305. return 0644;
  306. default:
  307. break;
  308. }
  309. break;
  310. case hwmon_pwm:
  311. if (!test_bit(channel, ccp->fan_cnct))
  312. break;
  313. switch (attr) {
  314. case hwmon_pwm_input:
  315. return 0644;
  316. default:
  317. break;
  318. }
  319. break;
  320. case hwmon_in:
  321. switch (attr) {
  322. case hwmon_in_input:
  323. return 0444;
  324. default:
  325. break;
  326. }
  327. break;
  328. default:
  329. break;
  330. }
  331. return 0;
  332. };
  333. static const struct hwmon_ops ccp_hwmon_ops = {
  334. .is_visible = ccp_is_visible,
  335. .read = ccp_read,
  336. .read_string = ccp_read_string,
  337. .write = ccp_write,
  338. };
  339. static const struct hwmon_channel_info *ccp_info[] = {
  340. HWMON_CHANNEL_INFO(chip,
  341. HWMON_C_REGISTER_TZ),
  342. HWMON_CHANNEL_INFO(temp,
  343. HWMON_T_INPUT,
  344. HWMON_T_INPUT,
  345. HWMON_T_INPUT,
  346. HWMON_T_INPUT
  347. ),
  348. HWMON_CHANNEL_INFO(fan,
  349. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
  350. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
  351. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
  352. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
  353. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET,
  354. HWMON_F_INPUT | HWMON_F_LABEL | HWMON_F_TARGET
  355. ),
  356. HWMON_CHANNEL_INFO(pwm,
  357. HWMON_PWM_INPUT,
  358. HWMON_PWM_INPUT,
  359. HWMON_PWM_INPUT,
  360. HWMON_PWM_INPUT,
  361. HWMON_PWM_INPUT,
  362. HWMON_PWM_INPUT
  363. ),
  364. HWMON_CHANNEL_INFO(in,
  365. HWMON_I_INPUT,
  366. HWMON_I_INPUT,
  367. HWMON_I_INPUT
  368. ),
  369. NULL
  370. };
  371. static const struct hwmon_chip_info ccp_chip_info = {
  372. .ops = &ccp_hwmon_ops,
  373. .info = ccp_info,
  374. };
  375. /* read fan connection status and set labels */
  376. static int get_fan_cnct(struct ccp_device *ccp)
  377. {
  378. int channel;
  379. int mode;
  380. int ret;
  381. ret = send_usb_cmd(ccp, CTL_GET_FAN_CNCT, 0, 0, 0);
  382. if (ret)
  383. return ret;
  384. for (channel = 0; channel < NUM_FANS; channel++) {
  385. mode = ccp->buffer[channel + 1];
  386. if (mode == 0)
  387. continue;
  388. set_bit(channel, ccp->fan_cnct);
  389. ccp->target[channel] = -ENODATA;
  390. switch (mode) {
  391. case 1:
  392. scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
  393. "fan%d 3pin", channel + 1);
  394. break;
  395. case 2:
  396. scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
  397. "fan%d 4pin", channel + 1);
  398. break;
  399. default:
  400. scnprintf(ccp->fan_label[channel], LABEL_LENGTH,
  401. "fan%d other", channel + 1);
  402. break;
  403. }
  404. }
  405. return 0;
  406. }
  407. /* read temp sensor connection status */
  408. static int get_temp_cnct(struct ccp_device *ccp)
  409. {
  410. int channel;
  411. int mode;
  412. int ret;
  413. ret = send_usb_cmd(ccp, CTL_GET_TMP_CNCT, 0, 0, 0);
  414. if (ret)
  415. return ret;
  416. for (channel = 0; channel < NUM_TEMP_SENSORS; channel++) {
  417. mode = ccp->buffer[channel + 1];
  418. if (mode == 0)
  419. continue;
  420. set_bit(channel, ccp->temp_cnct);
  421. }
  422. return 0;
  423. }
  424. static int ccp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  425. {
  426. struct ccp_device *ccp;
  427. int ret;
  428. ccp = devm_kzalloc(&hdev->dev, sizeof(*ccp), GFP_KERNEL);
  429. if (!ccp)
  430. return -ENOMEM;
  431. ccp->buffer = devm_kmalloc(&hdev->dev, OUT_BUFFER_SIZE, GFP_KERNEL);
  432. if (!ccp->buffer)
  433. return -ENOMEM;
  434. ret = hid_parse(hdev);
  435. if (ret)
  436. return ret;
  437. ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW);
  438. if (ret)
  439. return ret;
  440. ret = hid_hw_open(hdev);
  441. if (ret)
  442. goto out_hw_stop;
  443. ccp->hdev = hdev;
  444. hid_set_drvdata(hdev, ccp);
  445. mutex_init(&ccp->mutex);
  446. init_completion(&ccp->wait_input_report);
  447. hid_device_io_start(hdev);
  448. /* temp and fan connection status only updates when device is powered on */
  449. ret = get_temp_cnct(ccp);
  450. if (ret)
  451. goto out_hw_close;
  452. ret = get_fan_cnct(ccp);
  453. if (ret)
  454. goto out_hw_close;
  455. ccp->hwmon_dev = hwmon_device_register_with_info(&hdev->dev, "corsaircpro",
  456. ccp, &ccp_chip_info, 0);
  457. if (IS_ERR(ccp->hwmon_dev)) {
  458. ret = PTR_ERR(ccp->hwmon_dev);
  459. goto out_hw_close;
  460. }
  461. return 0;
  462. out_hw_close:
  463. hid_hw_close(hdev);
  464. out_hw_stop:
  465. hid_hw_stop(hdev);
  466. return ret;
  467. }
  468. static void ccp_remove(struct hid_device *hdev)
  469. {
  470. struct ccp_device *ccp = hid_get_drvdata(hdev);
  471. hwmon_device_unregister(ccp->hwmon_dev);
  472. hid_hw_close(hdev);
  473. hid_hw_stop(hdev);
  474. }
  475. static const struct hid_device_id ccp_devices[] = {
  476. { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_COMMANDERPRO) },
  477. { HID_USB_DEVICE(USB_VENDOR_ID_CORSAIR, USB_PRODUCT_ID_CORSAIR_1000D) },
  478. { }
  479. };
  480. static struct hid_driver ccp_driver = {
  481. .name = "corsair-cpro",
  482. .id_table = ccp_devices,
  483. .probe = ccp_probe,
  484. .remove = ccp_remove,
  485. .raw_event = ccp_raw_event,
  486. };
  487. MODULE_DEVICE_TABLE(hid, ccp_devices);
  488. MODULE_LICENSE("GPL");
  489. static int __init ccp_init(void)
  490. {
  491. return hid_register_driver(&ccp_driver);
  492. }
  493. static void __exit ccp_exit(void)
  494. {
  495. hid_unregister_driver(&ccp_driver);
  496. }
  497. /*
  498. * When compiling this driver as built-in, hwmon initcalls will get called before the
  499. * hid driver and this driver would fail to register. late_initcall solves this.
  500. */
  501. late_initcall(ccp_init);
  502. module_exit(ccp_exit);