simple cli for tuxedo laptop N150ZU

fancontrol.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* simple cli fan control for tuxedo laptop N150ZU */
  2. #include <cstdint>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include <unistd.h>
  6. #include <sys/io.h>
  7. #define EC_COMMAND_PORT 0x66
  8. #define EC_DATA_PORT 0x62
  9. #define IBF 1
  10. #define OBF 0
  11. #define EC_SC_READ_CMD 0x80
  12. #define TEMP 0x9E
  13. /**
  14. * Set IO port input/output permissions
  15. * On success, zero is returned. On error, -1 is returned, and errno is
  16. * set appropriately.
  17. *
  18. * @return doesn't matter, we don't ever care
  19. */
  20. int EcInit() {
  21. if (ioperm(EC_DATA_PORT, 1, 1) != 0) {
  22. return EXIT_FAILURE;
  23. }
  24. if (ioperm(EC_COMMAND_PORT, 1, 1) != 0) {
  25. return EXIT_FAILURE;
  26. }
  27. return EXIT_SUCCESS;
  28. }
  29. /**
  30. * Wait on EC
  31. *
  32. * @param port for waiting
  33. * @param flag to be captured
  34. * @param value to be set
  35. *
  36. * @return same as in EcInit()
  37. */
  38. static int EcIoWait(const uint8_t port, const uint8_t flag, const char value) {
  39. uint8_t data = inb(port);
  40. int i = 0;
  41. while ((((data >> flag) & 0x1) != value) && (i++ < 100)) {
  42. data = inb(port);
  43. }
  44. if (i >= 100) {
  45. return EXIT_FAILURE;
  46. }
  47. return EXIT_SUCCESS;
  48. }
  49. /**
  50. * Read the Port informations
  51. *
  52. * @param port for waiting
  53. *
  54. * @return the Data
  55. */
  56. static uint8_t EcIoRead(const uint8_t port) {
  57. EcIoWait(EC_COMMAND_PORT, IBF, 0);
  58. outb(EC_SC_READ_CMD, EC_COMMAND_PORT);
  59. EcIoWait(EC_COMMAND_PORT, IBF, 0);
  60. outb(port, EC_DATA_PORT);
  61. EcIoWait(EC_COMMAND_PORT, OBF, 1);
  62. uint8_t value = inb(EC_DATA_PORT);
  63. return value;
  64. }
  65. /**
  66. * Flush the EC
  67. */
  68. static void EcFlush() {
  69. while ((inb(EC_COMMAND_PORT) & 0x1) == 0x1) {
  70. inb(EC_DATA_PORT);
  71. }
  72. }
  73. /**
  74. * Read a byte from EC
  75. *
  76. * @return the Data
  77. */
  78. static int ReadByte() {
  79. int i = 1000000;
  80. while ((inb(EC_COMMAND_PORT) & 1) == 0 && i > 0) {
  81. i -= 1;
  82. }
  83. if (i == 0) {
  84. return 0;
  85. } else {
  86. return inb(EC_DATA_PORT);
  87. }
  88. }
  89. /**
  90. * Send a command to the ec
  91. *
  92. * @param command to send
  93. */
  94. static void SendCommand(uint8_t command) {
  95. int tt = 0;
  96. while ((inb(EC_COMMAND_PORT) & 2)) {
  97. tt++;
  98. if (tt > 30000) {
  99. break;
  100. }
  101. }
  102. outb(command, EC_COMMAND_PORT);
  103. }
  104. /**
  105. * Write data to ec
  106. *
  107. * @param data to write
  108. */
  109. static void WriteData(uint8_t data) {
  110. while ((inb(EC_COMMAND_PORT) & 2));
  111. outb(data, EC_DATA_PORT);
  112. }
  113. /**
  114. * Read and return the Raw Fan duty of fan
  115. *
  116. * @param index of the fan
  117. *
  118. * @return raw fan duty value (1-255)
  119. */
  120. uint8_t RawFanDuty(uint8_t index) {
  121. EcInit();
  122. EcFlush();
  123. SendCommand(TEMP);
  124. WriteData(index);
  125. ReadByte();
  126. ReadByte();
  127. uint8_t value = ReadByte();
  128. return value;
  129. }
  130. /**
  131. * Get the clean Fan Duty in percent
  132. *
  133. * @param index of the fan
  134. *
  135. * @return fan duty value (0-100)
  136. */
  137. uint8_t GetFanDuty(uint8_t index) {
  138. float fan_duty_perc = 0;
  139. uint8_t value = RawFanDuty(index);
  140. fan_duty_perc = ((float) value / (float) 255) * 100;
  141. return ((uint8_t) (fan_duty_perc + 0.5));
  142. }
  143. /**
  144. * Read and returns the remote(?) temp of the fan
  145. *
  146. * @param index of the fan
  147. *
  148. * @return the remote(?) temp
  149. */
  150. uint8_t GetRemoteTemp(uint8_t index) {
  151. EcInit();
  152. EcFlush();
  153. SendCommand(TEMP);
  154. WriteData(index);
  155. uint8_t value = ReadByte();
  156. return value;
  157. }
  158. /**
  159. * Read and returns the local(?) temp of the fan
  160. *
  161. * @param index of the fan
  162. *
  163. * @return the local(?) temp
  164. */
  165. uint8_t GetLocalTemp(uint8_t index) {
  166. EcInit();
  167. EcFlush();
  168. SendCommand(TEMP);
  169. WriteData(index);
  170. ReadByte();
  171. uint8_t value = ReadByte();
  172. return value;
  173. }
  174. /**
  175. * Calculate the fan rpms
  176. *
  177. * @param raw_rpm_high The raw high rpm value of the fan
  178. * @param raw_rpm_low The raw low rpm value of the fan
  179. */
  180. static int calculate_fan_rpms(int raw_rpm_high, int raw_rpm_low) {
  181. int raw_rpm = (raw_rpm_high << 8) + raw_rpm_low;
  182. return raw_rpm > 0 ? (2156220 / raw_rpm) : 0;
  183. }
  184. /**
  185. *
  186. *
  187. * @param index of the fan 1-3
  188. * @param fanDuty power 1 - 255
  189. * @return
  190. */
  191. bool SetFanDuty(uint8_t index, uint8_t fanDuty) {
  192. if (fanDuty < 1 || fanDuty > 255) {
  193. printf("Wrong fan duty to write: 0 < %d < 256\n", fanDuty);
  194. return false;
  195. }
  196. EcInit();
  197. SendCommand(0x99);
  198. switch (index) {
  199. case 1:
  200. WriteData(0x01);
  201. break;
  202. case 2:
  203. WriteData(0x02);
  204. break;
  205. case 3:
  206. WriteData(0x03);
  207. break;
  208. default:
  209. printf("Wrong fan: 0 < %d < 4\n", index);
  210. return false;
  211. }
  212. WriteData(fanDuty);
  213. return true;
  214. }
  215. /**
  216. * Set the fan duty on auto mode
  217. *
  218. * @param info the nodejs CallbackInfo
  219. */
  220. void SetAutoFanDuty(uint8_t index) {
  221. EcInit();
  222. SendCommand(0x99);
  223. WriteData(0xff);
  224. switch (index) {
  225. case 1:
  226. WriteData(0x01);
  227. break;
  228. case 2:
  229. WriteData(0x02);
  230. break;
  231. case 3:
  232. WriteData(0x03);
  233. break;
  234. case 5:
  235. WriteData(0xff);
  236. WriteData(0xff);
  237. break;
  238. default:
  239. printf("Wrong fan: 0 < %d < 6\n", index);
  240. break;
  241. }
  242. }
  243. void printUsage(char *argv0) {
  244. fprintf(stderr, "Usage: %s [-rq] [-f fan] [duty]\n", argv0);
  245. fprintf(stderr,
  246. "\nCommands:\n"
  247. " -r read temperature\n"
  248. );
  249. fprintf(stderr,
  250. "\nOptions:\n"
  251. " -q silence output\n"
  252. " -f # fan 1-3 (default 1, N150ZU has 1)\n"
  253. " duty 1-255\n"
  254. );
  255. fprintf(stderr,
  256. "\nExamples:\n"
  257. " %1$s -r # reads temperature\n"
  258. " %1$s -q 64 # silently sets fan 1 to 64/255\n",
  259. argv0
  260. );
  261. }
  262. /**
  263. * main
  264. *
  265. * @param argc
  266. * @param argv
  267. * @return
  268. */
  269. int main(int argc, char *argv[]) {
  270. int8_t opt;
  271. uint8_t fan, duty, quiet, temp;
  272. if (argc < 2) {
  273. printUsage(argv[0]);
  274. exit(EXIT_FAILURE);
  275. }
  276. temp = 0;
  277. quiet = 0;
  278. fan = 1;
  279. while ((opt = getopt(argc, argv, "rqf:")) != -1) {
  280. switch (opt) {
  281. case 'r':
  282. temp = 1;
  283. break;
  284. case 'q':
  285. quiet = 1;
  286. break;
  287. case 'f':
  288. fan = atoi(optarg);
  289. break;
  290. default:
  291. printUsage(argv[0]);
  292. exit(EXIT_FAILURE);
  293. }
  294. }
  295. if (temp) {
  296. temp = GetLocalTemp(fan);
  297. printf("%d", temp);
  298. exit(EXIT_SUCCESS);
  299. }
  300. duty = atoi(argv[optind]);
  301. if (!quiet) {
  302. printf("setting fan=%d duty=%d\n", fan, duty);
  303. }
  304. SetFanDuty(fan, duty);
  305. exit(EXIT_SUCCESS);
  306. }