thermal_netlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <[email protected]>
  6. *
  7. * Generic netlink for thermal management framework
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <net/genetlink.h>
  12. #include <trace/hooks/thermal.h>
  13. #include <uapi/linux/thermal.h>
  14. #include "thermal_core.h"
  15. static const struct genl_multicast_group thermal_genl_mcgrps[] = {
  16. { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
  17. { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
  18. };
  19. static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
  20. /* Thermal zone */
  21. [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
  22. [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
  23. [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
  24. [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
  25. [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
  26. [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
  27. [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
  28. [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
  29. [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
  30. [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
  31. [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
  32. .len = THERMAL_NAME_LENGTH },
  33. /* Governor(s) */
  34. [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
  35. [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
  36. .len = THERMAL_NAME_LENGTH },
  37. /* Cooling devices */
  38. [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
  39. [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
  40. [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
  41. [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
  42. [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
  43. .len = THERMAL_NAME_LENGTH },
  44. /* CPU capabilities */
  45. [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
  46. [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
  47. [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
  48. [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
  49. };
  50. struct param {
  51. struct nlattr **attrs;
  52. struct sk_buff *msg;
  53. const char *name;
  54. int tz_id;
  55. int cdev_id;
  56. int trip_id;
  57. int trip_temp;
  58. int trip_type;
  59. int trip_hyst;
  60. int temp;
  61. int cdev_state;
  62. int cdev_max_state;
  63. struct thermal_genl_cpu_caps *cpu_capabilities;
  64. int cpu_capabilities_count;
  65. };
  66. typedef int (*cb_t)(struct param *);
  67. static struct genl_family thermal_gnl_family;
  68. /************************** Sampling encoding *******************************/
  69. int thermal_genl_sampling_temp(int id, int temp)
  70. {
  71. struct sk_buff *skb;
  72. void *hdr;
  73. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  74. if (!skb)
  75. return -ENOMEM;
  76. hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0,
  77. THERMAL_GENL_SAMPLING_TEMP);
  78. if (!hdr)
  79. goto out_free;
  80. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
  81. goto out_cancel;
  82. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  83. goto out_cancel;
  84. genlmsg_end(skb, hdr);
  85. genlmsg_multicast(&thermal_gnl_family, skb, 0, 0, GFP_KERNEL);
  86. return 0;
  87. out_cancel:
  88. genlmsg_cancel(skb, hdr);
  89. out_free:
  90. nlmsg_free(skb);
  91. return -EMSGSIZE;
  92. }
  93. /**************************** Event encoding *********************************/
  94. static int thermal_genl_event_tz_create(struct param *p)
  95. {
  96. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  97. nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
  98. return -EMSGSIZE;
  99. return 0;
  100. }
  101. static int thermal_genl_event_tz(struct param *p)
  102. {
  103. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
  104. return -EMSGSIZE;
  105. return 0;
  106. }
  107. static int thermal_genl_event_tz_trip_up(struct param *p)
  108. {
  109. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  110. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  111. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
  112. return -EMSGSIZE;
  113. return 0;
  114. }
  115. static int thermal_genl_event_tz_trip_add(struct param *p)
  116. {
  117. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  118. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  119. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
  120. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
  121. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
  122. return -EMSGSIZE;
  123. return 0;
  124. }
  125. static int thermal_genl_event_tz_trip_delete(struct param *p)
  126. {
  127. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  128. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id))
  129. return -EMSGSIZE;
  130. return 0;
  131. }
  132. static int thermal_genl_event_cdev_add(struct param *p)
  133. {
  134. if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
  135. p->name) ||
  136. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
  137. p->cdev_id) ||
  138. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
  139. p->cdev_max_state))
  140. return -EMSGSIZE;
  141. return 0;
  142. }
  143. static int thermal_genl_event_cdev_delete(struct param *p)
  144. {
  145. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
  146. return -EMSGSIZE;
  147. return 0;
  148. }
  149. static int thermal_genl_event_cdev_state_update(struct param *p)
  150. {
  151. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
  152. p->cdev_id) ||
  153. nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
  154. p->cdev_state))
  155. return -EMSGSIZE;
  156. return 0;
  157. }
  158. static int thermal_genl_event_gov_change(struct param *p)
  159. {
  160. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  161. nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
  162. return -EMSGSIZE;
  163. return 0;
  164. }
  165. static int thermal_genl_event_cpu_capability_change(struct param *p)
  166. {
  167. struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
  168. struct sk_buff *msg = p->msg;
  169. struct nlattr *start_cap;
  170. int i;
  171. start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
  172. if (!start_cap)
  173. return -EMSGSIZE;
  174. for (i = 0; i < p->cpu_capabilities_count; ++i) {
  175. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
  176. cpu_cap->cpu))
  177. goto out_cancel_nest;
  178. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
  179. cpu_cap->performance))
  180. goto out_cancel_nest;
  181. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
  182. cpu_cap->efficiency))
  183. goto out_cancel_nest;
  184. ++cpu_cap;
  185. }
  186. nla_nest_end(msg, start_cap);
  187. return 0;
  188. out_cancel_nest:
  189. nla_nest_cancel(msg, start_cap);
  190. return -EMSGSIZE;
  191. }
  192. int thermal_genl_event_tz_delete(struct param *p)
  193. __attribute__((alias("thermal_genl_event_tz")));
  194. int thermal_genl_event_tz_enable(struct param *p)
  195. __attribute__((alias("thermal_genl_event_tz")));
  196. int thermal_genl_event_tz_disable(struct param *p)
  197. __attribute__((alias("thermal_genl_event_tz")));
  198. int thermal_genl_event_tz_trip_down(struct param *p)
  199. __attribute__((alias("thermal_genl_event_tz_trip_up")));
  200. int thermal_genl_event_tz_trip_change(struct param *p)
  201. __attribute__((alias("thermal_genl_event_tz_trip_add")));
  202. static cb_t event_cb[] = {
  203. [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
  204. [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
  205. [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
  206. [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
  207. [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
  208. [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
  209. [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
  210. [THERMAL_GENL_EVENT_TZ_TRIP_ADD] = thermal_genl_event_tz_trip_add,
  211. [THERMAL_GENL_EVENT_TZ_TRIP_DELETE] = thermal_genl_event_tz_trip_delete,
  212. [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
  213. [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
  214. [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
  215. [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
  216. [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
  217. };
  218. /*
  219. * Generic netlink event encoding
  220. */
  221. static int thermal_genl_send_event(enum thermal_genl_event event,
  222. struct param *p)
  223. {
  224. struct sk_buff *msg;
  225. int ret = -EMSGSIZE;
  226. void *hdr;
  227. int enable_thermal_genl = 1;
  228. trace_android_vh_enable_thermal_genl_check(event, p->tz_id, &enable_thermal_genl);
  229. if (!enable_thermal_genl)
  230. return 0;
  231. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  232. if (!msg)
  233. return -ENOMEM;
  234. p->msg = msg;
  235. hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
  236. if (!hdr)
  237. goto out_free_msg;
  238. ret = event_cb[event](p);
  239. if (ret)
  240. goto out_cancel_msg;
  241. genlmsg_end(msg, hdr);
  242. genlmsg_multicast(&thermal_gnl_family, msg, 0, 1, GFP_KERNEL);
  243. return 0;
  244. out_cancel_msg:
  245. genlmsg_cancel(msg, hdr);
  246. out_free_msg:
  247. nlmsg_free(msg);
  248. return ret;
  249. }
  250. int thermal_notify_tz_create(int tz_id, const char *name)
  251. {
  252. struct param p = { .tz_id = tz_id, .name = name };
  253. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
  254. }
  255. int thermal_notify_tz_delete(int tz_id)
  256. {
  257. struct param p = { .tz_id = tz_id };
  258. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
  259. }
  260. int thermal_notify_tz_enable(int tz_id)
  261. {
  262. struct param p = { .tz_id = tz_id };
  263. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
  264. }
  265. int thermal_notify_tz_disable(int tz_id)
  266. {
  267. struct param p = { .tz_id = tz_id };
  268. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
  269. }
  270. int thermal_notify_tz_trip_down(int tz_id, int trip_id, int temp)
  271. {
  272. struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
  273. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
  274. }
  275. int thermal_notify_tz_trip_up(int tz_id, int trip_id, int temp)
  276. {
  277. struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
  278. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
  279. }
  280. int thermal_notify_tz_trip_add(int tz_id, int trip_id, int trip_type,
  281. int trip_temp, int trip_hyst)
  282. {
  283. struct param p = { .tz_id = tz_id, .trip_id = trip_id,
  284. .trip_type = trip_type, .trip_temp = trip_temp,
  285. .trip_hyst = trip_hyst };
  286. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_ADD, &p);
  287. }
  288. int thermal_notify_tz_trip_delete(int tz_id, int trip_id)
  289. {
  290. struct param p = { .tz_id = tz_id, .trip_id = trip_id };
  291. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DELETE, &p);
  292. }
  293. int thermal_notify_tz_trip_change(int tz_id, int trip_id, int trip_type,
  294. int trip_temp, int trip_hyst)
  295. {
  296. struct param p = { .tz_id = tz_id, .trip_id = trip_id,
  297. .trip_type = trip_type, .trip_temp = trip_temp,
  298. .trip_hyst = trip_hyst };
  299. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
  300. }
  301. int thermal_notify_cdev_state_update(int cdev_id, int cdev_state)
  302. {
  303. struct param p = { .cdev_id = cdev_id, .cdev_state = cdev_state };
  304. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
  305. }
  306. int thermal_notify_cdev_add(int cdev_id, const char *name, int cdev_max_state)
  307. {
  308. struct param p = { .cdev_id = cdev_id, .name = name,
  309. .cdev_max_state = cdev_max_state };
  310. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
  311. }
  312. int thermal_notify_cdev_delete(int cdev_id)
  313. {
  314. struct param p = { .cdev_id = cdev_id };
  315. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
  316. }
  317. int thermal_notify_tz_gov_change(int tz_id, const char *name)
  318. {
  319. struct param p = { .tz_id = tz_id, .name = name };
  320. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
  321. }
  322. int thermal_genl_cpu_capability_event(int count,
  323. struct thermal_genl_cpu_caps *caps)
  324. {
  325. struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
  326. return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
  327. }
  328. EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
  329. /*************************** Command encoding ********************************/
  330. static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
  331. void *data)
  332. {
  333. struct sk_buff *msg = data;
  334. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
  335. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
  336. return -EMSGSIZE;
  337. return 0;
  338. }
  339. static int thermal_genl_cmd_tz_get_id(struct param *p)
  340. {
  341. struct sk_buff *msg = p->msg;
  342. struct nlattr *start_tz;
  343. int ret;
  344. start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
  345. if (!start_tz)
  346. return -EMSGSIZE;
  347. ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
  348. if (ret)
  349. goto out_cancel_nest;
  350. nla_nest_end(msg, start_tz);
  351. return 0;
  352. out_cancel_nest:
  353. nla_nest_cancel(msg, start_tz);
  354. return ret;
  355. }
  356. static int thermal_genl_cmd_tz_get_trip(struct param *p)
  357. {
  358. struct sk_buff *msg = p->msg;
  359. struct thermal_zone_device *tz;
  360. struct nlattr *start_trip;
  361. int i, id;
  362. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  363. return -EINVAL;
  364. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  365. tz = thermal_zone_get_by_id(id);
  366. if (!tz)
  367. return -EINVAL;
  368. start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
  369. if (!start_trip)
  370. return -EMSGSIZE;
  371. mutex_lock(&tz->lock);
  372. for (i = 0; i < tz->num_trips; i++) {
  373. enum thermal_trip_type type;
  374. int temp, hyst = 0;
  375. tz->ops->get_trip_type(tz, i, &type);
  376. tz->ops->get_trip_temp(tz, i, &temp);
  377. if (tz->ops->get_trip_hyst)
  378. tz->ops->get_trip_hyst(tz, i, &hyst);
  379. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) ||
  380. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, type) ||
  381. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, temp) ||
  382. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, hyst))
  383. goto out_cancel_nest;
  384. }
  385. mutex_unlock(&tz->lock);
  386. nla_nest_end(msg, start_trip);
  387. return 0;
  388. out_cancel_nest:
  389. mutex_unlock(&tz->lock);
  390. return -EMSGSIZE;
  391. }
  392. static int thermal_genl_cmd_tz_get_temp(struct param *p)
  393. {
  394. struct sk_buff *msg = p->msg;
  395. struct thermal_zone_device *tz;
  396. int temp, ret, id;
  397. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  398. return -EINVAL;
  399. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  400. tz = thermal_zone_get_by_id(id);
  401. if (!tz)
  402. return -EINVAL;
  403. ret = thermal_zone_get_temp(tz, &temp);
  404. if (ret)
  405. return ret;
  406. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  407. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  408. return -EMSGSIZE;
  409. return 0;
  410. }
  411. static int thermal_genl_cmd_tz_get_gov(struct param *p)
  412. {
  413. struct sk_buff *msg = p->msg;
  414. struct thermal_zone_device *tz;
  415. int id, ret = 0;
  416. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  417. return -EINVAL;
  418. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  419. tz = thermal_zone_get_by_id(id);
  420. if (!tz)
  421. return -EINVAL;
  422. mutex_lock(&tz->lock);
  423. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  424. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
  425. tz->governor->name))
  426. ret = -EMSGSIZE;
  427. mutex_unlock(&tz->lock);
  428. return ret;
  429. }
  430. static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
  431. void *data)
  432. {
  433. struct sk_buff *msg = data;
  434. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
  435. return -EMSGSIZE;
  436. if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
  437. return -EMSGSIZE;
  438. return 0;
  439. }
  440. static int thermal_genl_cmd_cdev_get(struct param *p)
  441. {
  442. struct sk_buff *msg = p->msg;
  443. struct nlattr *start_cdev;
  444. int ret;
  445. start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
  446. if (!start_cdev)
  447. return -EMSGSIZE;
  448. ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
  449. if (ret)
  450. goto out_cancel_nest;
  451. nla_nest_end(msg, start_cdev);
  452. return 0;
  453. out_cancel_nest:
  454. nla_nest_cancel(msg, start_cdev);
  455. return ret;
  456. }
  457. static cb_t cmd_cb[] = {
  458. [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
  459. [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
  460. [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
  461. [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
  462. [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
  463. };
  464. static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
  465. struct netlink_callback *cb)
  466. {
  467. struct param p = { .msg = skb };
  468. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  469. int cmd = info->op.cmd;
  470. int ret;
  471. void *hdr;
  472. hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
  473. if (!hdr)
  474. return -EMSGSIZE;
  475. ret = cmd_cb[cmd](&p);
  476. if (ret)
  477. goto out_cancel_msg;
  478. genlmsg_end(skb, hdr);
  479. return 0;
  480. out_cancel_msg:
  481. genlmsg_cancel(skb, hdr);
  482. return ret;
  483. }
  484. static int thermal_genl_cmd_doit(struct sk_buff *skb,
  485. struct genl_info *info)
  486. {
  487. struct param p = { .attrs = info->attrs };
  488. struct sk_buff *msg;
  489. void *hdr;
  490. int cmd = info->genlhdr->cmd;
  491. int ret = -EMSGSIZE;
  492. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  493. if (!msg)
  494. return -ENOMEM;
  495. p.msg = msg;
  496. hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);
  497. if (!hdr)
  498. goto out_free_msg;
  499. ret = cmd_cb[cmd](&p);
  500. if (ret)
  501. goto out_cancel_msg;
  502. genlmsg_end(msg, hdr);
  503. return genlmsg_reply(msg, info);
  504. out_cancel_msg:
  505. genlmsg_cancel(msg, hdr);
  506. out_free_msg:
  507. nlmsg_free(msg);
  508. return ret;
  509. }
  510. static const struct genl_small_ops thermal_genl_ops[] = {
  511. {
  512. .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
  513. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  514. .dumpit = thermal_genl_cmd_dumpit,
  515. },
  516. {
  517. .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
  518. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  519. .doit = thermal_genl_cmd_doit,
  520. },
  521. {
  522. .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
  523. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  524. .doit = thermal_genl_cmd_doit,
  525. },
  526. {
  527. .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
  528. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  529. .doit = thermal_genl_cmd_doit,
  530. },
  531. {
  532. .cmd = THERMAL_GENL_CMD_CDEV_GET,
  533. .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
  534. .dumpit = thermal_genl_cmd_dumpit,
  535. },
  536. };
  537. static struct genl_family thermal_gnl_family __ro_after_init = {
  538. .hdrsize = 0,
  539. .name = THERMAL_GENL_FAMILY_NAME,
  540. .version = THERMAL_GENL_VERSION,
  541. .maxattr = THERMAL_GENL_ATTR_MAX,
  542. .policy = thermal_genl_policy,
  543. .small_ops = thermal_genl_ops,
  544. .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
  545. .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
  546. .mcgrps = thermal_genl_mcgrps,
  547. .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
  548. };
  549. int __init thermal_netlink_init(void)
  550. {
  551. return genl_register_family(&thermal_gnl_family);
  552. }