nzxt-smart2.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Reverse-engineered NZXT RGB & Fan Controller/Smart Device v2 driver.
  4. *
  5. * Copyright (c) 2021 Aleksandr Mezin
  6. */
  7. #include <linux/hid.h>
  8. #include <linux/hwmon.h>
  9. #include <linux/math.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/wait.h>
  14. #include <asm/byteorder.h>
  15. #include <asm/unaligned.h>
  16. /*
  17. * The device has only 3 fan channels/connectors. But all HID reports have
  18. * space reserved for up to 8 channels.
  19. */
  20. #define FAN_CHANNELS 3
  21. #define FAN_CHANNELS_MAX 8
  22. #define UPDATE_INTERVAL_DEFAULT_MS 1000
  23. /* These strings match labels on the device exactly */
  24. static const char *const fan_label[] = {
  25. "FAN 1",
  26. "FAN 2",
  27. "FAN 3",
  28. };
  29. static const char *const curr_label[] = {
  30. "FAN 1 Current",
  31. "FAN 2 Current",
  32. "FAN 3 Current",
  33. };
  34. static const char *const in_label[] = {
  35. "FAN 1 Voltage",
  36. "FAN 2 Voltage",
  37. "FAN 3 Voltage",
  38. };
  39. enum {
  40. INPUT_REPORT_ID_FAN_CONFIG = 0x61,
  41. INPUT_REPORT_ID_FAN_STATUS = 0x67,
  42. };
  43. enum {
  44. FAN_STATUS_REPORT_SPEED = 0x02,
  45. FAN_STATUS_REPORT_VOLTAGE = 0x04,
  46. };
  47. enum {
  48. FAN_TYPE_NONE = 0,
  49. FAN_TYPE_DC = 1,
  50. FAN_TYPE_PWM = 2,
  51. };
  52. struct unknown_static_data {
  53. /*
  54. * Some configuration data? Stays the same after fan speed changes,
  55. * changes in fan configuration, reboots and driver reloads.
  56. *
  57. * The same data in multiple report types.
  58. *
  59. * Byte 12 seems to be the number of fan channels, but I am not sure.
  60. */
  61. u8 unknown1[14];
  62. } __packed;
  63. /*
  64. * The device sends this input report in response to "detect fans" command:
  65. * a 2-byte output report { 0x60, 0x03 }.
  66. */
  67. struct fan_config_report {
  68. /* report_id should be INPUT_REPORT_ID_FAN_CONFIG = 0x61 */
  69. u8 report_id;
  70. /* Always 0x03 */
  71. u8 magic;
  72. struct unknown_static_data unknown_data;
  73. /* Fan type as detected by the device. See FAN_TYPE_* enum. */
  74. u8 fan_type[FAN_CHANNELS_MAX];
  75. } __packed;
  76. /*
  77. * The device sends these reports at a fixed interval (update interval) -
  78. * one report with type = FAN_STATUS_REPORT_SPEED, and one report with type =
  79. * FAN_STATUS_REPORT_VOLTAGE per update interval.
  80. */
  81. struct fan_status_report {
  82. /* report_id should be INPUT_REPORT_ID_STATUS = 0x67 */
  83. u8 report_id;
  84. /* FAN_STATUS_REPORT_SPEED = 0x02 or FAN_STATUS_REPORT_VOLTAGE = 0x04 */
  85. u8 type;
  86. struct unknown_static_data unknown_data;
  87. /* Fan type as detected by the device. See FAN_TYPE_* enum. */
  88. u8 fan_type[FAN_CHANNELS_MAX];
  89. union {
  90. /* When type == FAN_STATUS_REPORT_SPEED */
  91. struct {
  92. /*
  93. * Fan speed, in RPM. Zero for channels without fans
  94. * connected.
  95. */
  96. __le16 fan_rpm[FAN_CHANNELS_MAX];
  97. /*
  98. * Fan duty cycle, in percent. Non-zero even for
  99. * channels without fans connected.
  100. */
  101. u8 duty_percent[FAN_CHANNELS_MAX];
  102. /*
  103. * Exactly the same values as duty_percent[], non-zero
  104. * for disconnected fans too.
  105. */
  106. u8 duty_percent_dup[FAN_CHANNELS_MAX];
  107. /* "Case Noise" in db */
  108. u8 noise_db;
  109. } __packed fan_speed;
  110. /* When type == FAN_STATUS_REPORT_VOLTAGE */
  111. struct {
  112. /*
  113. * Voltage, in millivolts. Non-zero even when fan is
  114. * not connected.
  115. */
  116. __le16 fan_in[FAN_CHANNELS_MAX];
  117. /*
  118. * Current, in milliamperes. Near-zero when
  119. * disconnected.
  120. */
  121. __le16 fan_current[FAN_CHANNELS_MAX];
  122. } __packed fan_voltage;
  123. } __packed;
  124. } __packed;
  125. #define OUTPUT_REPORT_SIZE 64
  126. enum {
  127. OUTPUT_REPORT_ID_INIT_COMMAND = 0x60,
  128. OUTPUT_REPORT_ID_SET_FAN_SPEED = 0x62,
  129. };
  130. enum {
  131. INIT_COMMAND_SET_UPDATE_INTERVAL = 0x02,
  132. INIT_COMMAND_DETECT_FANS = 0x03,
  133. };
  134. /*
  135. * This output report sets pwm duty cycle/target fan speed for one or more
  136. * channels.
  137. */
  138. struct set_fan_speed_report {
  139. /* report_id should be OUTPUT_REPORT_ID_SET_FAN_SPEED = 0x62 */
  140. u8 report_id;
  141. /* Should be 0x01 */
  142. u8 magic;
  143. /* To change fan speed on i-th channel, set i-th bit here */
  144. u8 channel_bit_mask;
  145. /*
  146. * Fan duty cycle/target speed in percent. For voltage-controlled fans,
  147. * the minimal voltage (duty_percent = 1) is about 9V.
  148. * Setting duty_percent to 0 (if the channel is selected in
  149. * channel_bit_mask) turns off the fan completely (regardless of the
  150. * control mode).
  151. */
  152. u8 duty_percent[FAN_CHANNELS_MAX];
  153. } __packed;
  154. struct drvdata {
  155. struct hid_device *hid;
  156. struct device *hwmon;
  157. u8 fan_duty_percent[FAN_CHANNELS];
  158. u16 fan_rpm[FAN_CHANNELS];
  159. bool pwm_status_received;
  160. u16 fan_in[FAN_CHANNELS];
  161. u16 fan_curr[FAN_CHANNELS];
  162. bool voltage_status_received;
  163. u8 fan_type[FAN_CHANNELS];
  164. bool fan_config_received;
  165. /*
  166. * wq is used to wait for *_received flags to become true.
  167. * All accesses to *_received flags and fan_* arrays are performed with
  168. * wq.lock held.
  169. */
  170. wait_queue_head_t wq;
  171. /*
  172. * mutex is used to:
  173. * 1) Prevent concurrent conflicting changes to update interval and pwm
  174. * values (after sending an output hid report, the corresponding field
  175. * in drvdata must be updated, and only then new output reports can be
  176. * sent).
  177. * 2) Synchronize access to output_buffer (well, the buffer is here,
  178. * because synchronization is necessary anyway - so why not get rid of
  179. * a kmalloc?).
  180. */
  181. struct mutex mutex;
  182. long update_interval;
  183. u8 output_buffer[OUTPUT_REPORT_SIZE];
  184. };
  185. static long scale_pwm_value(long val, long orig_max, long new_max)
  186. {
  187. if (val <= 0)
  188. return 0;
  189. /*
  190. * Positive values should not become zero: 0 completely turns off the
  191. * fan.
  192. */
  193. return max(1L, DIV_ROUND_CLOSEST(min(val, orig_max) * new_max, orig_max));
  194. }
  195. static void handle_fan_config_report(struct drvdata *drvdata, void *data, int size)
  196. {
  197. struct fan_config_report *report = data;
  198. int i;
  199. if (size < sizeof(struct fan_config_report))
  200. return;
  201. if (report->magic != 0x03)
  202. return;
  203. spin_lock(&drvdata->wq.lock);
  204. for (i = 0; i < FAN_CHANNELS; i++)
  205. drvdata->fan_type[i] = report->fan_type[i];
  206. drvdata->fan_config_received = true;
  207. wake_up_all_locked(&drvdata->wq);
  208. spin_unlock(&drvdata->wq.lock);
  209. }
  210. static void handle_fan_status_report(struct drvdata *drvdata, void *data, int size)
  211. {
  212. struct fan_status_report *report = data;
  213. int i;
  214. if (size < sizeof(struct fan_status_report))
  215. return;
  216. spin_lock(&drvdata->wq.lock);
  217. /*
  218. * The device sends INPUT_REPORT_ID_FAN_CONFIG = 0x61 report in response
  219. * to "detect fans" command. Only accept other data after getting 0x61,
  220. * to make sure that fan detection is complete. In particular, fan
  221. * detection resets pwm values.
  222. */
  223. if (!drvdata->fan_config_received) {
  224. spin_unlock(&drvdata->wq.lock);
  225. return;
  226. }
  227. for (i = 0; i < FAN_CHANNELS; i++) {
  228. if (drvdata->fan_type[i] == report->fan_type[i])
  229. continue;
  230. /*
  231. * This should not happen (if my expectations about the device
  232. * are correct).
  233. *
  234. * Even if the userspace sends fan detect command through
  235. * hidraw, fan config report should arrive first.
  236. */
  237. hid_warn_once(drvdata->hid,
  238. "Fan %d type changed unexpectedly from %d to %d",
  239. i, drvdata->fan_type[i], report->fan_type[i]);
  240. drvdata->fan_type[i] = report->fan_type[i];
  241. }
  242. switch (report->type) {
  243. case FAN_STATUS_REPORT_SPEED:
  244. for (i = 0; i < FAN_CHANNELS; i++) {
  245. drvdata->fan_rpm[i] =
  246. get_unaligned_le16(&report->fan_speed.fan_rpm[i]);
  247. drvdata->fan_duty_percent[i] =
  248. report->fan_speed.duty_percent[i];
  249. }
  250. drvdata->pwm_status_received = true;
  251. wake_up_all_locked(&drvdata->wq);
  252. break;
  253. case FAN_STATUS_REPORT_VOLTAGE:
  254. for (i = 0; i < FAN_CHANNELS; i++) {
  255. drvdata->fan_in[i] =
  256. get_unaligned_le16(&report->fan_voltage.fan_in[i]);
  257. drvdata->fan_curr[i] =
  258. get_unaligned_le16(&report->fan_voltage.fan_current[i]);
  259. }
  260. drvdata->voltage_status_received = true;
  261. wake_up_all_locked(&drvdata->wq);
  262. break;
  263. }
  264. spin_unlock(&drvdata->wq.lock);
  265. }
  266. static umode_t nzxt_smart2_hwmon_is_visible(const void *data,
  267. enum hwmon_sensor_types type,
  268. u32 attr, int channel)
  269. {
  270. switch (type) {
  271. case hwmon_pwm:
  272. switch (attr) {
  273. case hwmon_pwm_input:
  274. case hwmon_pwm_enable:
  275. return 0644;
  276. default:
  277. return 0444;
  278. }
  279. case hwmon_chip:
  280. switch (attr) {
  281. case hwmon_chip_update_interval:
  282. return 0644;
  283. default:
  284. return 0444;
  285. }
  286. default:
  287. return 0444;
  288. }
  289. }
  290. static int nzxt_smart2_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
  291. u32 attr, int channel, long *val)
  292. {
  293. struct drvdata *drvdata = dev_get_drvdata(dev);
  294. int res = -EINVAL;
  295. if (type == hwmon_chip) {
  296. switch (attr) {
  297. case hwmon_chip_update_interval:
  298. *val = drvdata->update_interval;
  299. return 0;
  300. default:
  301. return -EINVAL;
  302. }
  303. }
  304. spin_lock_irq(&drvdata->wq.lock);
  305. switch (type) {
  306. case hwmon_pwm:
  307. /*
  308. * fancontrol:
  309. * 1) remembers pwm* values when it starts
  310. * 2) needs pwm*_enable to be 1 on controlled fans
  311. * So make sure we have correct data before allowing pwm* reads.
  312. * Returning errors for pwm of fan speed read can even cause
  313. * fancontrol to shut down. So the wait is unavoidable.
  314. */
  315. switch (attr) {
  316. case hwmon_pwm_enable:
  317. res = wait_event_interruptible_locked_irq(drvdata->wq,
  318. drvdata->fan_config_received);
  319. if (res)
  320. goto unlock;
  321. *val = drvdata->fan_type[channel] != FAN_TYPE_NONE;
  322. break;
  323. case hwmon_pwm_mode:
  324. res = wait_event_interruptible_locked_irq(drvdata->wq,
  325. drvdata->fan_config_received);
  326. if (res)
  327. goto unlock;
  328. *val = drvdata->fan_type[channel] == FAN_TYPE_PWM;
  329. break;
  330. case hwmon_pwm_input:
  331. res = wait_event_interruptible_locked_irq(drvdata->wq,
  332. drvdata->pwm_status_received);
  333. if (res)
  334. goto unlock;
  335. *val = scale_pwm_value(drvdata->fan_duty_percent[channel],
  336. 100, 255);
  337. break;
  338. }
  339. break;
  340. case hwmon_fan:
  341. /*
  342. * It's not strictly necessary to wait for *_received in the
  343. * remaining cases (fancontrol doesn't care about them). But I'm
  344. * doing it to have consistent behavior.
  345. */
  346. if (attr == hwmon_fan_input) {
  347. res = wait_event_interruptible_locked_irq(drvdata->wq,
  348. drvdata->pwm_status_received);
  349. if (res)
  350. goto unlock;
  351. *val = drvdata->fan_rpm[channel];
  352. }
  353. break;
  354. case hwmon_in:
  355. if (attr == hwmon_in_input) {
  356. res = wait_event_interruptible_locked_irq(drvdata->wq,
  357. drvdata->voltage_status_received);
  358. if (res)
  359. goto unlock;
  360. *val = drvdata->fan_in[channel];
  361. }
  362. break;
  363. case hwmon_curr:
  364. if (attr == hwmon_curr_input) {
  365. res = wait_event_interruptible_locked_irq(drvdata->wq,
  366. drvdata->voltage_status_received);
  367. if (res)
  368. goto unlock;
  369. *val = drvdata->fan_curr[channel];
  370. }
  371. break;
  372. default:
  373. break;
  374. }
  375. unlock:
  376. spin_unlock_irq(&drvdata->wq.lock);
  377. return res;
  378. }
  379. static int send_output_report(struct drvdata *drvdata, const void *data,
  380. size_t data_size)
  381. {
  382. int ret;
  383. if (data_size > sizeof(drvdata->output_buffer))
  384. return -EINVAL;
  385. memcpy(drvdata->output_buffer, data, data_size);
  386. if (data_size < sizeof(drvdata->output_buffer))
  387. memset(drvdata->output_buffer + data_size, 0,
  388. sizeof(drvdata->output_buffer) - data_size);
  389. ret = hid_hw_output_report(drvdata->hid, drvdata->output_buffer,
  390. sizeof(drvdata->output_buffer));
  391. return ret < 0 ? ret : 0;
  392. }
  393. static int set_pwm(struct drvdata *drvdata, int channel, long val)
  394. {
  395. int ret;
  396. u8 duty_percent = scale_pwm_value(val, 255, 100);
  397. struct set_fan_speed_report report = {
  398. .report_id = OUTPUT_REPORT_ID_SET_FAN_SPEED,
  399. .magic = 1,
  400. .channel_bit_mask = 1 << channel
  401. };
  402. ret = mutex_lock_interruptible(&drvdata->mutex);
  403. if (ret)
  404. return ret;
  405. report.duty_percent[channel] = duty_percent;
  406. ret = send_output_report(drvdata, &report, sizeof(report));
  407. if (ret)
  408. goto unlock;
  409. /*
  410. * pwmconfig and fancontrol scripts expect pwm writes to take effect
  411. * immediately (i. e. read from pwm* sysfs should return the value
  412. * written into it). The device seems to always accept pwm values - even
  413. * when there is no fan connected - so update pwm status without waiting
  414. * for a report, to make pwmconfig and fancontrol happy. Worst case -
  415. * if the device didn't accept new pwm value for some reason (never seen
  416. * this in practice) - it will be reported incorrectly only until next
  417. * update. This avoids "fan stuck" messages from pwmconfig, and
  418. * fancontrol setting fan speed to 100% during shutdown.
  419. */
  420. spin_lock_bh(&drvdata->wq.lock);
  421. drvdata->fan_duty_percent[channel] = duty_percent;
  422. spin_unlock_bh(&drvdata->wq.lock);
  423. unlock:
  424. mutex_unlock(&drvdata->mutex);
  425. return ret;
  426. }
  427. /*
  428. * Workaround for fancontrol/pwmconfig trying to write to pwm*_enable even if it
  429. * already is 1 and read-only. Otherwise, fancontrol won't restore pwm on
  430. * shutdown properly.
  431. */
  432. static int set_pwm_enable(struct drvdata *drvdata, int channel, long val)
  433. {
  434. long expected_val;
  435. int res;
  436. spin_lock_irq(&drvdata->wq.lock);
  437. res = wait_event_interruptible_locked_irq(drvdata->wq,
  438. drvdata->fan_config_received);
  439. if (res) {
  440. spin_unlock_irq(&drvdata->wq.lock);
  441. return res;
  442. }
  443. expected_val = drvdata->fan_type[channel] != FAN_TYPE_NONE;
  444. spin_unlock_irq(&drvdata->wq.lock);
  445. return (val == expected_val) ? 0 : -EOPNOTSUPP;
  446. }
  447. /*
  448. * Control byte | Actual update interval in seconds
  449. * 0xff | 65.5
  450. * 0xf7 | 63.46
  451. * 0x7f | 32.74
  452. * 0x3f | 16.36
  453. * 0x1f | 8.17
  454. * 0x0f | 4.07
  455. * 0x07 | 2.02
  456. * 0x03 | 1.00
  457. * 0x02 | 0.744
  458. * 0x01 | 0.488
  459. * 0x00 | 0.25
  460. */
  461. static u8 update_interval_to_control_byte(long interval)
  462. {
  463. if (interval <= 250)
  464. return 0;
  465. return clamp_val(1 + DIV_ROUND_CLOSEST(interval - 488, 256), 0, 255);
  466. }
  467. static long control_byte_to_update_interval(u8 control_byte)
  468. {
  469. if (control_byte == 0)
  470. return 250;
  471. return 488 + (control_byte - 1) * 256;
  472. }
  473. static int set_update_interval(struct drvdata *drvdata, long val)
  474. {
  475. u8 control = update_interval_to_control_byte(val);
  476. u8 report[] = {
  477. OUTPUT_REPORT_ID_INIT_COMMAND,
  478. INIT_COMMAND_SET_UPDATE_INTERVAL,
  479. 0x01,
  480. 0xe8,
  481. control,
  482. 0x01,
  483. 0xe8,
  484. control,
  485. };
  486. int ret;
  487. ret = send_output_report(drvdata, report, sizeof(report));
  488. if (ret)
  489. return ret;
  490. drvdata->update_interval = control_byte_to_update_interval(control);
  491. return 0;
  492. }
  493. static int init_device(struct drvdata *drvdata, long update_interval)
  494. {
  495. int ret;
  496. static const u8 detect_fans_report[] = {
  497. OUTPUT_REPORT_ID_INIT_COMMAND,
  498. INIT_COMMAND_DETECT_FANS,
  499. };
  500. ret = send_output_report(drvdata, detect_fans_report,
  501. sizeof(detect_fans_report));
  502. if (ret)
  503. return ret;
  504. return set_update_interval(drvdata, update_interval);
  505. }
  506. static int nzxt_smart2_hwmon_write(struct device *dev,
  507. enum hwmon_sensor_types type, u32 attr,
  508. int channel, long val)
  509. {
  510. struct drvdata *drvdata = dev_get_drvdata(dev);
  511. int ret;
  512. switch (type) {
  513. case hwmon_pwm:
  514. switch (attr) {
  515. case hwmon_pwm_enable:
  516. return set_pwm_enable(drvdata, channel, val);
  517. case hwmon_pwm_input:
  518. return set_pwm(drvdata, channel, val);
  519. default:
  520. return -EINVAL;
  521. }
  522. case hwmon_chip:
  523. switch (attr) {
  524. case hwmon_chip_update_interval:
  525. ret = mutex_lock_interruptible(&drvdata->mutex);
  526. if (ret)
  527. return ret;
  528. ret = set_update_interval(drvdata, val);
  529. mutex_unlock(&drvdata->mutex);
  530. return ret;
  531. default:
  532. return -EINVAL;
  533. }
  534. default:
  535. return -EINVAL;
  536. }
  537. }
  538. static int nzxt_smart2_hwmon_read_string(struct device *dev,
  539. enum hwmon_sensor_types type, u32 attr,
  540. int channel, const char **str)
  541. {
  542. switch (type) {
  543. case hwmon_fan:
  544. *str = fan_label[channel];
  545. return 0;
  546. case hwmon_curr:
  547. *str = curr_label[channel];
  548. return 0;
  549. case hwmon_in:
  550. *str = in_label[channel];
  551. return 0;
  552. default:
  553. return -EINVAL;
  554. }
  555. }
  556. static const struct hwmon_ops nzxt_smart2_hwmon_ops = {
  557. .is_visible = nzxt_smart2_hwmon_is_visible,
  558. .read = nzxt_smart2_hwmon_read,
  559. .read_string = nzxt_smart2_hwmon_read_string,
  560. .write = nzxt_smart2_hwmon_write,
  561. };
  562. static const struct hwmon_channel_info *nzxt_smart2_channel_info[] = {
  563. HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_LABEL,
  564. HWMON_F_INPUT | HWMON_F_LABEL,
  565. HWMON_F_INPUT | HWMON_F_LABEL),
  566. HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE,
  567. HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE,
  568. HWMON_PWM_INPUT | HWMON_PWM_MODE | HWMON_PWM_ENABLE),
  569. HWMON_CHANNEL_INFO(in, HWMON_I_INPUT | HWMON_I_LABEL,
  570. HWMON_I_INPUT | HWMON_I_LABEL,
  571. HWMON_I_INPUT | HWMON_I_LABEL),
  572. HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT | HWMON_C_LABEL,
  573. HWMON_C_INPUT | HWMON_C_LABEL,
  574. HWMON_C_INPUT | HWMON_C_LABEL),
  575. HWMON_CHANNEL_INFO(chip, HWMON_C_UPDATE_INTERVAL),
  576. NULL
  577. };
  578. static const struct hwmon_chip_info nzxt_smart2_chip_info = {
  579. .ops = &nzxt_smart2_hwmon_ops,
  580. .info = nzxt_smart2_channel_info,
  581. };
  582. static int nzxt_smart2_hid_raw_event(struct hid_device *hdev,
  583. struct hid_report *report, u8 *data, int size)
  584. {
  585. struct drvdata *drvdata = hid_get_drvdata(hdev);
  586. u8 report_id = *data;
  587. switch (report_id) {
  588. case INPUT_REPORT_ID_FAN_CONFIG:
  589. handle_fan_config_report(drvdata, data, size);
  590. break;
  591. case INPUT_REPORT_ID_FAN_STATUS:
  592. handle_fan_status_report(drvdata, data, size);
  593. break;
  594. }
  595. return 0;
  596. }
  597. static int __maybe_unused nzxt_smart2_hid_reset_resume(struct hid_device *hdev)
  598. {
  599. struct drvdata *drvdata = hid_get_drvdata(hdev);
  600. /*
  601. * Userspace is still frozen (so no concurrent sysfs attribute access
  602. * is possible), but raw_event can already be called concurrently.
  603. */
  604. spin_lock_bh(&drvdata->wq.lock);
  605. drvdata->fan_config_received = false;
  606. drvdata->pwm_status_received = false;
  607. drvdata->voltage_status_received = false;
  608. spin_unlock_bh(&drvdata->wq.lock);
  609. return init_device(drvdata, drvdata->update_interval);
  610. }
  611. static int nzxt_smart2_hid_probe(struct hid_device *hdev,
  612. const struct hid_device_id *id)
  613. {
  614. struct drvdata *drvdata;
  615. int ret;
  616. drvdata = devm_kzalloc(&hdev->dev, sizeof(struct drvdata), GFP_KERNEL);
  617. if (!drvdata)
  618. return -ENOMEM;
  619. drvdata->hid = hdev;
  620. hid_set_drvdata(hdev, drvdata);
  621. init_waitqueue_head(&drvdata->wq);
  622. mutex_init(&drvdata->mutex);
  623. devm_add_action(&hdev->dev, (void (*)(void *))mutex_destroy,
  624. &drvdata->mutex);
  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 out_hw_stop;
  634. hid_device_io_start(hdev);
  635. init_device(drvdata, UPDATE_INTERVAL_DEFAULT_MS);
  636. drvdata->hwmon =
  637. hwmon_device_register_with_info(&hdev->dev, "nzxtsmart2", drvdata,
  638. &nzxt_smart2_chip_info, NULL);
  639. if (IS_ERR(drvdata->hwmon)) {
  640. ret = PTR_ERR(drvdata->hwmon);
  641. goto out_hw_close;
  642. }
  643. return 0;
  644. out_hw_close:
  645. hid_hw_close(hdev);
  646. out_hw_stop:
  647. hid_hw_stop(hdev);
  648. return ret;
  649. }
  650. static void nzxt_smart2_hid_remove(struct hid_device *hdev)
  651. {
  652. struct drvdata *drvdata = hid_get_drvdata(hdev);
  653. hwmon_device_unregister(drvdata->hwmon);
  654. hid_hw_close(hdev);
  655. hid_hw_stop(hdev);
  656. }
  657. static const struct hid_device_id nzxt_smart2_hid_id_table[] = {
  658. { HID_USB_DEVICE(0x1e71, 0x2006) }, /* NZXT Smart Device V2 */
  659. { HID_USB_DEVICE(0x1e71, 0x200d) }, /* NZXT Smart Device V2 */
  660. { HID_USB_DEVICE(0x1e71, 0x200f) }, /* NZXT Smart Device V2 */
  661. { HID_USB_DEVICE(0x1e71, 0x2009) }, /* NZXT RGB & Fan Controller */
  662. { HID_USB_DEVICE(0x1e71, 0x200e) }, /* NZXT RGB & Fan Controller */
  663. { HID_USB_DEVICE(0x1e71, 0x2010) }, /* NZXT RGB & Fan Controller */
  664. { HID_USB_DEVICE(0x1e71, 0x2011) }, /* NZXT RGB & Fan Controller (6 RGB) */
  665. { HID_USB_DEVICE(0x1e71, 0x2019) }, /* NZXT RGB & Fan Controller (6 RGB) */
  666. {},
  667. };
  668. static struct hid_driver nzxt_smart2_hid_driver = {
  669. .name = "nzxt-smart2",
  670. .id_table = nzxt_smart2_hid_id_table,
  671. .probe = nzxt_smart2_hid_probe,
  672. .remove = nzxt_smart2_hid_remove,
  673. .raw_event = nzxt_smart2_hid_raw_event,
  674. #ifdef CONFIG_PM
  675. .reset_resume = nzxt_smart2_hid_reset_resume,
  676. #endif
  677. };
  678. static int __init nzxt_smart2_init(void)
  679. {
  680. return hid_register_driver(&nzxt_smart2_hid_driver);
  681. }
  682. static void __exit nzxt_smart2_exit(void)
  683. {
  684. hid_unregister_driver(&nzxt_smart2_hid_driver);
  685. }
  686. MODULE_DEVICE_TABLE(hid, nzxt_smart2_hid_id_table);
  687. MODULE_AUTHOR("Aleksandr Mezin <[email protected]>");
  688. MODULE_DESCRIPTION("Driver for NZXT RGB & Fan Controller/Smart Device V2");
  689. MODULE_LICENSE("GPL");
  690. /*
  691. * With module_init()/module_hid_driver() and the driver built into the kernel:
  692. *
  693. * Driver 'nzxt_smart2' was unable to register with bus_type 'hid' because the
  694. * bus was not initialized.
  695. */
  696. late_initcall(nzxt_smart2_init);
  697. module_exit(nzxt_smart2_exit);