commands.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: LGPL-2.1+
  2. // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
  3. #define _GNU_SOURCE
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <limits.h>
  9. #include <thermal.h>
  10. #include "thermal_nl.h"
  11. static struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
  12. /* Thermal zone */
  13. [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
  14. [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
  15. [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
  16. [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
  17. [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
  18. [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
  19. [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
  20. [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
  21. [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
  22. [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
  23. [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING },
  24. /* Governor(s) */
  25. [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
  26. [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING },
  27. /* Cooling devices */
  28. [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
  29. [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
  30. [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
  31. [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
  32. [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING },
  33. /* Thresholds */
  34. [THERMAL_GENL_ATTR_THRESHOLD] = { .type = NLA_NESTED },
  35. [THERMAL_GENL_ATTR_THRESHOLD_TEMP] = { .type = NLA_U32 },
  36. [THERMAL_GENL_ATTR_THRESHOLD_DIRECTION] = { .type = NLA_U32 },
  37. };
  38. static int parse_tz_get(struct genl_info *info, struct thermal_zone **tz)
  39. {
  40. struct nlattr *attr;
  41. struct thermal_zone *__tz = NULL;
  42. size_t size = 0;
  43. int rem;
  44. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ], rem) {
  45. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_ID) {
  46. size++;
  47. __tz = realloc(__tz, sizeof(*__tz) * (size + 2));
  48. if (!__tz)
  49. return THERMAL_ERROR;
  50. __tz[size - 1].id = nla_get_u32(attr);
  51. }
  52. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_NAME)
  53. nla_strlcpy(__tz[size - 1].name, attr,
  54. THERMAL_NAME_LENGTH);
  55. }
  56. if (__tz)
  57. __tz[size].id = -1;
  58. *tz = __tz;
  59. return THERMAL_SUCCESS;
  60. }
  61. static int parse_cdev_get(struct genl_info *info, struct thermal_cdev **cdev)
  62. {
  63. struct nlattr *attr;
  64. struct thermal_cdev *__cdev = NULL;
  65. size_t size = 0;
  66. int rem;
  67. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_CDEV], rem) {
  68. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_ID) {
  69. size++;
  70. __cdev = realloc(__cdev, sizeof(*__cdev) * (size + 2));
  71. if (!__cdev)
  72. return THERMAL_ERROR;
  73. __cdev[size - 1].id = nla_get_u32(attr);
  74. }
  75. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_NAME) {
  76. nla_strlcpy(__cdev[size - 1].name, attr,
  77. THERMAL_NAME_LENGTH);
  78. }
  79. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_CUR_STATE)
  80. __cdev[size - 1].cur_state = nla_get_u32(attr);
  81. if (nla_type(attr) == THERMAL_GENL_ATTR_CDEV_MAX_STATE)
  82. __cdev[size - 1].max_state = nla_get_u32(attr);
  83. }
  84. if (__cdev)
  85. __cdev[size].id = -1;
  86. *cdev = __cdev;
  87. return THERMAL_SUCCESS;
  88. }
  89. static int parse_tz_get_trip(struct genl_info *info, struct thermal_zone *tz)
  90. {
  91. struct nlattr *attr;
  92. struct thermal_trip *__tt = NULL;
  93. size_t size = 0;
  94. int rem;
  95. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_TZ_TRIP], rem) {
  96. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_ID) {
  97. size++;
  98. __tt = realloc(__tt, sizeof(*__tt) * (size + 2));
  99. if (!__tt)
  100. return THERMAL_ERROR;
  101. __tt[size - 1].id = nla_get_u32(attr);
  102. }
  103. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TYPE)
  104. __tt[size - 1].type = nla_get_u32(attr);
  105. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_TEMP)
  106. __tt[size - 1].temp = nla_get_u32(attr);
  107. if (nla_type(attr) == THERMAL_GENL_ATTR_TZ_TRIP_HYST)
  108. __tt[size - 1].hyst = nla_get_u32(attr);
  109. }
  110. if (__tt)
  111. __tt[size].id = -1;
  112. tz->trip = __tt;
  113. return THERMAL_SUCCESS;
  114. }
  115. static int parse_tz_get_temp(struct genl_info *info, struct thermal_zone *tz)
  116. {
  117. int id = -1;
  118. if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
  119. id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  120. if (tz->id != id)
  121. return THERMAL_ERROR;
  122. if (info->attrs[THERMAL_GENL_ATTR_TZ_TEMP])
  123. tz->temp = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_TEMP]);
  124. return THERMAL_SUCCESS;
  125. }
  126. static int parse_tz_get_gov(struct genl_info *info, struct thermal_zone *tz)
  127. {
  128. int id = -1;
  129. if (info->attrs[THERMAL_GENL_ATTR_TZ_ID])
  130. id = nla_get_u32(info->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  131. if (tz->id != id)
  132. return THERMAL_ERROR;
  133. if (info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME]) {
  134. nla_strlcpy(tz->governor,
  135. info->attrs[THERMAL_GENL_ATTR_TZ_GOV_NAME],
  136. THERMAL_NAME_LENGTH);
  137. }
  138. return THERMAL_SUCCESS;
  139. }
  140. static int parse_threshold_get(struct genl_info *info, struct thermal_zone *tz)
  141. {
  142. struct nlattr *attr;
  143. struct thermal_threshold *__tt = NULL;
  144. size_t size = 0;
  145. int rem;
  146. /*
  147. * The size contains the size of the array and we want to
  148. * access the last element, size - 1.
  149. *
  150. * The variable size is initialized to zero but it will be
  151. * then incremented by the first if() statement. The message
  152. * attributes are ordered, so the first if() statement will be
  153. * always called before the second one. If it happens that is
  154. * not the case, then it is a kernel bug.
  155. */
  156. nla_for_each_nested(attr, info->attrs[THERMAL_GENL_ATTR_THRESHOLD], rem) {
  157. if (nla_type(attr) == THERMAL_GENL_ATTR_THRESHOLD_TEMP) {
  158. size++;
  159. __tt = realloc(__tt, sizeof(*__tt) * (size + 2));
  160. if (!__tt)
  161. return THERMAL_ERROR;
  162. __tt[size - 1].temperature = nla_get_u32(attr);
  163. }
  164. if (nla_type(attr) == THERMAL_GENL_ATTR_THRESHOLD_DIRECTION)
  165. __tt[size - 1].direction = nla_get_u32(attr);
  166. }
  167. if (__tt)
  168. __tt[size].temperature = INT_MAX;
  169. tz->thresholds = __tt;
  170. return THERMAL_SUCCESS;
  171. }
  172. static int handle_netlink(struct nl_cache_ops *unused,
  173. struct genl_cmd *cmd,
  174. struct genl_info *info, void *arg)
  175. {
  176. int ret;
  177. switch (cmd->c_id) {
  178. case THERMAL_GENL_CMD_TZ_GET_ID:
  179. ret = parse_tz_get(info, arg);
  180. break;
  181. case THERMAL_GENL_CMD_CDEV_GET:
  182. ret = parse_cdev_get(info, arg);
  183. break;
  184. case THERMAL_GENL_CMD_TZ_GET_TEMP:
  185. ret = parse_tz_get_temp(info, arg);
  186. break;
  187. case THERMAL_GENL_CMD_TZ_GET_TRIP:
  188. ret = parse_tz_get_trip(info, arg);
  189. break;
  190. case THERMAL_GENL_CMD_TZ_GET_GOV:
  191. ret = parse_tz_get_gov(info, arg);
  192. break;
  193. case THERMAL_GENL_CMD_THRESHOLD_GET:
  194. ret = parse_threshold_get(info, arg);
  195. break;
  196. default:
  197. return THERMAL_ERROR;
  198. }
  199. return ret;
  200. }
  201. static struct genl_cmd thermal_cmds[] = {
  202. {
  203. .c_id = THERMAL_GENL_CMD_TZ_GET_ID,
  204. .c_name = (char *)"List thermal zones",
  205. .c_msg_parser = handle_netlink,
  206. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  207. .c_attr_policy = thermal_genl_policy,
  208. },
  209. {
  210. .c_id = THERMAL_GENL_CMD_TZ_GET_GOV,
  211. .c_name = (char *)"Get governor",
  212. .c_msg_parser = handle_netlink,
  213. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  214. .c_attr_policy = thermal_genl_policy,
  215. },
  216. {
  217. .c_id = THERMAL_GENL_CMD_TZ_GET_TEMP,
  218. .c_name = (char *)"Get thermal zone temperature",
  219. .c_msg_parser = handle_netlink,
  220. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  221. .c_attr_policy = thermal_genl_policy,
  222. },
  223. {
  224. .c_id = THERMAL_GENL_CMD_TZ_GET_TRIP,
  225. .c_name = (char *)"Get thermal zone trip points",
  226. .c_msg_parser = handle_netlink,
  227. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  228. .c_attr_policy = thermal_genl_policy,
  229. },
  230. {
  231. .c_id = THERMAL_GENL_CMD_CDEV_GET,
  232. .c_name = (char *)"Get cooling devices",
  233. .c_msg_parser = handle_netlink,
  234. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  235. .c_attr_policy = thermal_genl_policy,
  236. },
  237. {
  238. .c_id = THERMAL_GENL_CMD_THRESHOLD_GET,
  239. .c_name = (char *)"Get thresholds list",
  240. .c_msg_parser = handle_netlink,
  241. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  242. .c_attr_policy = thermal_genl_policy,
  243. },
  244. {
  245. .c_id = THERMAL_GENL_CMD_THRESHOLD_ADD,
  246. .c_name = (char *)"Add a threshold",
  247. .c_msg_parser = handle_netlink,
  248. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  249. .c_attr_policy = thermal_genl_policy,
  250. },
  251. {
  252. .c_id = THERMAL_GENL_CMD_THRESHOLD_DELETE,
  253. .c_name = (char *)"Delete a threshold",
  254. .c_msg_parser = handle_netlink,
  255. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  256. .c_attr_policy = thermal_genl_policy,
  257. },
  258. {
  259. .c_id = THERMAL_GENL_CMD_THRESHOLD_FLUSH,
  260. .c_name = (char *)"Flush the thresholds",
  261. .c_msg_parser = handle_netlink,
  262. .c_maxattr = THERMAL_GENL_ATTR_MAX,
  263. .c_attr_policy = thermal_genl_policy,
  264. },
  265. };
  266. static struct genl_ops thermal_cmd_ops = {
  267. .o_name = (char *)"thermal",
  268. .o_cmds = thermal_cmds,
  269. .o_ncmds = ARRAY_SIZE(thermal_cmds),
  270. };
  271. struct cmd_param {
  272. int tz_id;
  273. int temp;
  274. int direction;
  275. };
  276. typedef int (*cmd_cb_t)(struct nl_msg *, struct cmd_param *);
  277. static int thermal_genl_tz_id_encode(struct nl_msg *msg, struct cmd_param *p)
  278. {
  279. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
  280. return -1;
  281. return 0;
  282. }
  283. static int thermal_genl_threshold_encode(struct nl_msg *msg, struct cmd_param *p)
  284. {
  285. if (thermal_genl_tz_id_encode(msg, p))
  286. return -1;
  287. if (nla_put_u32(msg, THERMAL_GENL_ATTR_THRESHOLD_TEMP, p->temp))
  288. return -1;
  289. if (nla_put_u32(msg, THERMAL_GENL_ATTR_THRESHOLD_DIRECTION, p->direction))
  290. return -1;
  291. return 0;
  292. }
  293. static thermal_error_t thermal_genl_auto(struct thermal_handler *th, cmd_cb_t cmd_cb,
  294. struct cmd_param *param,
  295. int cmd, int flags, void *arg)
  296. {
  297. thermal_error_t ret = THERMAL_ERROR;
  298. struct nl_msg *msg;
  299. void *hdr;
  300. msg = nlmsg_alloc();
  301. if (!msg)
  302. return THERMAL_ERROR;
  303. hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, thermal_cmd_ops.o_id,
  304. 0, flags, cmd, THERMAL_GENL_VERSION);
  305. if (!hdr)
  306. goto out;
  307. if (cmd_cb && cmd_cb(msg, param))
  308. goto out;
  309. if (nl_send_msg(th->sk_cmd, th->cb_cmd, msg, genl_handle_msg, arg))
  310. goto out;
  311. ret = THERMAL_SUCCESS;
  312. out:
  313. nlmsg_free(msg);
  314. return ret;
  315. }
  316. thermal_error_t thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz)
  317. {
  318. return thermal_genl_auto(th, NULL, NULL, THERMAL_GENL_CMD_TZ_GET_ID,
  319. NLM_F_DUMP | NLM_F_ACK, tz);
  320. }
  321. thermal_error_t thermal_cmd_get_cdev(struct thermal_handler *th, struct thermal_cdev **tc)
  322. {
  323. return thermal_genl_auto(th, NULL, NULL, THERMAL_GENL_CMD_CDEV_GET,
  324. NLM_F_DUMP | NLM_F_ACK, tc);
  325. }
  326. thermal_error_t thermal_cmd_get_trip(struct thermal_handler *th, struct thermal_zone *tz)
  327. {
  328. struct cmd_param p = { .tz_id = tz->id };
  329. return thermal_genl_auto(th, thermal_genl_tz_id_encode, &p,
  330. THERMAL_GENL_CMD_TZ_GET_TRIP, 0, tz);
  331. }
  332. thermal_error_t thermal_cmd_get_governor(struct thermal_handler *th, struct thermal_zone *tz)
  333. {
  334. struct cmd_param p = { .tz_id = tz->id };
  335. return thermal_genl_auto(th, thermal_genl_tz_id_encode, &p,
  336. THERMAL_GENL_CMD_TZ_GET_GOV, 0, tz);
  337. }
  338. thermal_error_t thermal_cmd_get_temp(struct thermal_handler *th, struct thermal_zone *tz)
  339. {
  340. struct cmd_param p = { .tz_id = tz->id };
  341. return thermal_genl_auto(th, thermal_genl_tz_id_encode, &p,
  342. THERMAL_GENL_CMD_TZ_GET_TEMP, 0, tz);
  343. }
  344. thermal_error_t thermal_cmd_threshold_get(struct thermal_handler *th,
  345. struct thermal_zone *tz)
  346. {
  347. struct cmd_param p = { .tz_id = tz->id };
  348. return thermal_genl_auto(th, thermal_genl_tz_id_encode, &p,
  349. THERMAL_GENL_CMD_THRESHOLD_GET, 0, tz);
  350. }
  351. thermal_error_t thermal_cmd_threshold_add(struct thermal_handler *th,
  352. struct thermal_zone *tz,
  353. int temperature,
  354. int direction)
  355. {
  356. struct cmd_param p = { .tz_id = tz->id, .temp = temperature, .direction = direction };
  357. return thermal_genl_auto(th, thermal_genl_threshold_encode, &p,
  358. THERMAL_GENL_CMD_THRESHOLD_ADD, 0, tz);
  359. }
  360. thermal_error_t thermal_cmd_threshold_delete(struct thermal_handler *th,
  361. struct thermal_zone *tz,
  362. int temperature,
  363. int direction)
  364. {
  365. struct cmd_param p = { .tz_id = tz->id, .temp = temperature, .direction = direction };
  366. return thermal_genl_auto(th, thermal_genl_threshold_encode, &p,
  367. THERMAL_GENL_CMD_THRESHOLD_DELETE, 0, tz);
  368. }
  369. thermal_error_t thermal_cmd_threshold_flush(struct thermal_handler *th,
  370. struct thermal_zone *tz)
  371. {
  372. struct cmd_param p = { .tz_id = tz->id };
  373. return thermal_genl_auto(th, thermal_genl_tz_id_encode, &p,
  374. THERMAL_GENL_CMD_THRESHOLD_FLUSH, 0, tz);
  375. }
  376. thermal_error_t thermal_cmd_exit(struct thermal_handler *th)
  377. {
  378. if (genl_unregister_family(&thermal_cmd_ops))
  379. return THERMAL_ERROR;
  380. nl_thermal_disconnect(th->sk_cmd, th->cb_cmd);
  381. return THERMAL_SUCCESS;
  382. }
  383. thermal_error_t thermal_cmd_init(struct thermal_handler *th)
  384. {
  385. int ret;
  386. int family;
  387. if (nl_thermal_connect(&th->sk_cmd, &th->cb_cmd))
  388. return THERMAL_ERROR;
  389. ret = genl_register_family(&thermal_cmd_ops);
  390. if (ret)
  391. return THERMAL_ERROR;
  392. ret = genl_ops_resolve(th->sk_cmd, &thermal_cmd_ops);
  393. if (ret)
  394. return THERMAL_ERROR;
  395. family = genl_ctrl_resolve(th->sk_cmd, "nlctrl");
  396. if (family != GENL_ID_CTRL)
  397. return THERMAL_ERROR;
  398. return THERMAL_SUCCESS;
  399. }