thermal_netlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 Linaro Limited
  4. *
  5. * Author: Daniel Lezcano <daniel.lezcano@linaro.org>
  6. *
  7. * Generic netlink for thermal management framework
  8. */
  9. #include <linux/module.h>
  10. #include <linux/notifier.h>
  11. #include <linux/kernel.h>
  12. #include <net/genetlink.h>
  13. #include <uapi/linux/thermal.h>
  14. #include "thermal_core.h"
  15. static const struct genl_multicast_group thermal_genl_mcgrps[] = {
  16. [THERMAL_GENL_SAMPLING_GROUP] = { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
  17. [THERMAL_GENL_EVENT_GROUP] = { .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_genl_family;
  68. static BLOCKING_NOTIFIER_HEAD(thermal_genl_chain);
  69. static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
  70. {
  71. return genl_has_listeners(&thermal_genl_family, &init_net, group);
  72. }
  73. /************************** Sampling encoding *******************************/
  74. int thermal_genl_sampling_temp(int id, int temp)
  75. {
  76. struct sk_buff *skb;
  77. void *hdr;
  78. if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
  79. return 0;
  80. skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  81. if (!skb)
  82. return -ENOMEM;
  83. hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0,
  84. THERMAL_GENL_SAMPLING_TEMP);
  85. if (!hdr)
  86. goto out_free;
  87. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
  88. goto out_cancel;
  89. if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  90. goto out_cancel;
  91. genlmsg_end(skb, hdr);
  92. genlmsg_multicast(&thermal_genl_family, skb, 0, THERMAL_GENL_SAMPLING_GROUP, GFP_KERNEL);
  93. return 0;
  94. out_cancel:
  95. genlmsg_cancel(skb, hdr);
  96. out_free:
  97. nlmsg_free(skb);
  98. return -EMSGSIZE;
  99. }
  100. /**************************** Event encoding *********************************/
  101. static int thermal_genl_event_tz_create(struct param *p)
  102. {
  103. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  104. nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
  105. return -EMSGSIZE;
  106. return 0;
  107. }
  108. static int thermal_genl_event_tz(struct param *p)
  109. {
  110. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
  111. return -EMSGSIZE;
  112. return 0;
  113. }
  114. static int thermal_genl_event_tz_trip_up(struct param *p)
  115. {
  116. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  117. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  118. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
  119. return -EMSGSIZE;
  120. return 0;
  121. }
  122. static int thermal_genl_event_tz_trip_change(struct param *p)
  123. {
  124. if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
  125. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
  126. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
  127. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
  128. nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
  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. static cb_t event_cb[] = {
  201. [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
  202. [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
  203. [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
  204. [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
  205. [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
  206. [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
  207. [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
  208. [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
  209. [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
  210. [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
  211. [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
  212. [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
  213. };
  214. /*
  215. * Generic netlink event encoding
  216. */
  217. static int thermal_genl_send_event(enum thermal_genl_event event,
  218. struct param *p)
  219. {
  220. struct sk_buff *msg;
  221. int ret = -EMSGSIZE;
  222. void *hdr;
  223. if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
  224. return 0;
  225. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  226. if (!msg)
  227. return -ENOMEM;
  228. p->msg = msg;
  229. hdr = genlmsg_put(msg, 0, 0, &thermal_genl_family, 0, event);
  230. if (!hdr)
  231. goto out_free_msg;
  232. ret = event_cb[event](p);
  233. if (ret)
  234. goto out_cancel_msg;
  235. genlmsg_end(msg, hdr);
  236. genlmsg_multicast(&thermal_genl_family, msg, 0, THERMAL_GENL_EVENT_GROUP, GFP_KERNEL);
  237. return 0;
  238. out_cancel_msg:
  239. genlmsg_cancel(msg, hdr);
  240. out_free_msg:
  241. nlmsg_free(msg);
  242. return ret;
  243. }
  244. int thermal_notify_tz_create(const struct thermal_zone_device *tz)
  245. {
  246. struct param p = { .tz_id = tz->id, .name = tz->type };
  247. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
  248. }
  249. int thermal_notify_tz_delete(const struct thermal_zone_device *tz)
  250. {
  251. struct param p = { .tz_id = tz->id };
  252. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
  253. }
  254. int thermal_notify_tz_enable(const struct thermal_zone_device *tz)
  255. {
  256. struct param p = { .tz_id = tz->id };
  257. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
  258. }
  259. int thermal_notify_tz_disable(const struct thermal_zone_device *tz)
  260. {
  261. struct param p = { .tz_id = tz->id };
  262. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
  263. }
  264. int thermal_notify_tz_trip_down(const struct thermal_zone_device *tz,
  265. const struct thermal_trip *trip)
  266. {
  267. struct param p = { .tz_id = tz->id,
  268. .trip_id = thermal_zone_trip_id(tz, trip),
  269. .temp = tz->temperature };
  270. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
  271. }
  272. int thermal_notify_tz_trip_up(const struct thermal_zone_device *tz,
  273. const struct thermal_trip *trip)
  274. {
  275. struct param p = { .tz_id = tz->id,
  276. .trip_id = thermal_zone_trip_id(tz, trip),
  277. .temp = tz->temperature };
  278. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
  279. }
  280. int thermal_notify_tz_trip_change(const struct thermal_zone_device *tz,
  281. const struct thermal_trip *trip)
  282. {
  283. struct param p = { .tz_id = tz->id,
  284. .trip_id = thermal_zone_trip_id(tz, trip),
  285. .trip_type = trip->type,
  286. .trip_temp = trip->temperature,
  287. .trip_hyst = trip->hysteresis };
  288. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
  289. }
  290. int thermal_notify_cdev_state_update(const struct thermal_cooling_device *cdev,
  291. int state)
  292. {
  293. struct param p = { .cdev_id = cdev->id, .cdev_state = state };
  294. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
  295. }
  296. int thermal_notify_cdev_add(const struct thermal_cooling_device *cdev)
  297. {
  298. struct param p = { .cdev_id = cdev->id, .name = cdev->type,
  299. .cdev_max_state = cdev->max_state };
  300. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
  301. }
  302. int thermal_notify_cdev_delete(const struct thermal_cooling_device *cdev)
  303. {
  304. struct param p = { .cdev_id = cdev->id };
  305. return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
  306. }
  307. int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,
  308. const char *name)
  309. {
  310. struct param p = { .tz_id = tz->id, .name = name };
  311. return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
  312. }
  313. int thermal_genl_cpu_capability_event(int count,
  314. struct thermal_genl_cpu_caps *caps)
  315. {
  316. struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
  317. return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
  318. }
  319. EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
  320. /*************************** Command encoding ********************************/
  321. static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
  322. void *data)
  323. {
  324. struct sk_buff *msg = data;
  325. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
  326. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
  327. return -EMSGSIZE;
  328. return 0;
  329. }
  330. static int thermal_genl_cmd_tz_get_id(struct param *p)
  331. {
  332. struct sk_buff *msg = p->msg;
  333. struct nlattr *start_tz;
  334. int ret;
  335. start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
  336. if (!start_tz)
  337. return -EMSGSIZE;
  338. ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
  339. if (ret)
  340. goto out_cancel_nest;
  341. nla_nest_end(msg, start_tz);
  342. return 0;
  343. out_cancel_nest:
  344. nla_nest_cancel(msg, start_tz);
  345. return ret;
  346. }
  347. static int thermal_genl_cmd_tz_get_trip(struct param *p)
  348. {
  349. struct sk_buff *msg = p->msg;
  350. const struct thermal_trip_desc *td;
  351. struct nlattr *start_trip;
  352. int id;
  353. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  354. return -EINVAL;
  355. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  356. CLASS(thermal_zone_get_by_id, tz)(id);
  357. if (!tz)
  358. return -EINVAL;
  359. start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
  360. if (!start_trip)
  361. return -EMSGSIZE;
  362. mutex_lock(&tz->lock);
  363. for_each_trip_desc(tz, td) {
  364. const struct thermal_trip *trip = &td->trip;
  365. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID,
  366. thermal_zone_trip_id(tz, trip)) ||
  367. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
  368. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
  369. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
  370. goto out_cancel_nest;
  371. }
  372. mutex_unlock(&tz->lock);
  373. nla_nest_end(msg, start_trip);
  374. return 0;
  375. out_cancel_nest:
  376. mutex_unlock(&tz->lock);
  377. return -EMSGSIZE;
  378. }
  379. static int thermal_genl_cmd_tz_get_temp(struct param *p)
  380. {
  381. struct sk_buff *msg = p->msg;
  382. int temp, ret, id;
  383. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  384. return -EINVAL;
  385. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  386. CLASS(thermal_zone_get_by_id, tz)(id);
  387. if (!tz)
  388. return -EINVAL;
  389. ret = thermal_zone_get_temp(tz, &temp);
  390. if (ret)
  391. return ret;
  392. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  393. nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
  394. return -EMSGSIZE;
  395. return 0;
  396. }
  397. static int thermal_genl_cmd_tz_get_gov(struct param *p)
  398. {
  399. struct sk_buff *msg = p->msg;
  400. int id, ret = 0;
  401. if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
  402. return -EINVAL;
  403. id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
  404. CLASS(thermal_zone_get_by_id, tz)(id);
  405. if (!tz)
  406. return -EINVAL;
  407. mutex_lock(&tz->lock);
  408. if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
  409. nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
  410. tz->governor->name))
  411. ret = -EMSGSIZE;
  412. mutex_unlock(&tz->lock);
  413. return ret;
  414. }
  415. static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
  416. void *data)
  417. {
  418. struct sk_buff *msg = data;
  419. if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
  420. return -EMSGSIZE;
  421. if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
  422. return -EMSGSIZE;
  423. return 0;
  424. }
  425. static int thermal_genl_cmd_cdev_get(struct param *p)
  426. {
  427. struct sk_buff *msg = p->msg;
  428. struct nlattr *start_cdev;
  429. int ret;
  430. start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
  431. if (!start_cdev)
  432. return -EMSGSIZE;
  433. ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
  434. if (ret)
  435. goto out_cancel_nest;
  436. nla_nest_end(msg, start_cdev);
  437. return 0;
  438. out_cancel_nest:
  439. nla_nest_cancel(msg, start_cdev);
  440. return ret;
  441. }
  442. static cb_t cmd_cb[] = {
  443. [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
  444. [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
  445. [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
  446. [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
  447. [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
  448. };
  449. static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
  450. struct netlink_callback *cb)
  451. {
  452. struct param p = { .msg = skb };
  453. const struct genl_dumpit_info *info = genl_dumpit_info(cb);
  454. int cmd = info->op.cmd;
  455. int ret;
  456. void *hdr;
  457. hdr = genlmsg_put(skb, 0, 0, &thermal_genl_family, 0, cmd);
  458. if (!hdr)
  459. return -EMSGSIZE;
  460. ret = cmd_cb[cmd](&p);
  461. if (ret)
  462. goto out_cancel_msg;
  463. genlmsg_end(skb, hdr);
  464. return 0;
  465. out_cancel_msg:
  466. genlmsg_cancel(skb, hdr);
  467. return ret;
  468. }
  469. static int thermal_genl_cmd_doit(struct sk_buff *skb,
  470. struct genl_info *info)
  471. {
  472. struct param p = { .attrs = info->attrs };
  473. struct sk_buff *msg;
  474. void *hdr;
  475. int cmd = info->genlhdr->cmd;
  476. int ret = -EMSGSIZE;
  477. msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  478. if (!msg)
  479. return -ENOMEM;
  480. p.msg = msg;
  481. hdr = genlmsg_put_reply(msg, info, &thermal_genl_family, 0, cmd);
  482. if (!hdr)
  483. goto out_free_msg;
  484. ret = cmd_cb[cmd](&p);
  485. if (ret)
  486. goto out_cancel_msg;
  487. genlmsg_end(msg, hdr);
  488. return genlmsg_reply(msg, info);
  489. out_cancel_msg:
  490. genlmsg_cancel(msg, hdr);
  491. out_free_msg:
  492. nlmsg_free(msg);
  493. return ret;
  494. }
  495. static int thermal_genl_bind(int mcgrp)
  496. {
  497. struct thermal_genl_notify n = { .mcgrp = mcgrp };
  498. if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
  499. return -EINVAL;
  500. blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_BIND, &n);
  501. return 0;
  502. }
  503. static void thermal_genl_unbind(int mcgrp)
  504. {
  505. struct thermal_genl_notify n = { .mcgrp = mcgrp };
  506. if (WARN_ON_ONCE(mcgrp > THERMAL_GENL_MAX_GROUP))
  507. return;
  508. blocking_notifier_call_chain(&thermal_genl_chain, THERMAL_NOTIFY_UNBIND, &n);
  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_genl_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. .bind = thermal_genl_bind,
  544. .unbind = thermal_genl_unbind,
  545. .small_ops = thermal_genl_ops,
  546. .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
  547. .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
  548. .mcgrps = thermal_genl_mcgrps,
  549. .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
  550. };
  551. int thermal_genl_register_notifier(struct notifier_block *nb)
  552. {
  553. return blocking_notifier_chain_register(&thermal_genl_chain, nb);
  554. }
  555. int thermal_genl_unregister_notifier(struct notifier_block *nb)
  556. {
  557. return blocking_notifier_chain_unregister(&thermal_genl_chain, nb);
  558. }
  559. int __init thermal_netlink_init(void)
  560. {
  561. return genl_register_family(&thermal_genl_family);
  562. }
  563. void __init thermal_netlink_exit(void)
  564. {
  565. genl_unregister_family(&thermal_genl_family);
  566. }