]> git.ipfire.org Git - thirdparty/linux.git/blob - drivers/thermal/thermal_netlink.c
46b0156a63191ea8eed5e71ec224b6cdafef4aff
[thirdparty/linux.git] / drivers / thermal / thermal_netlink.c
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/kernel.h>
11 #include <net/genetlink.h>
12 #include <uapi/linux/thermal.h>
13
14 #include "thermal_core.h"
15
16 enum thermal_genl_multicast_groups {
17 THERMAL_GENL_SAMPLING_GROUP = 0,
18 THERMAL_GENL_EVENT_GROUP = 1,
19 };
20
21 static const struct genl_multicast_group thermal_genl_mcgrps[] = {
22 [THERMAL_GENL_SAMPLING_GROUP] = { .name = THERMAL_GENL_SAMPLING_GROUP_NAME, },
23 [THERMAL_GENL_EVENT_GROUP] = { .name = THERMAL_GENL_EVENT_GROUP_NAME, },
24 };
25
26 static const struct nla_policy thermal_genl_policy[THERMAL_GENL_ATTR_MAX + 1] = {
27 /* Thermal zone */
28 [THERMAL_GENL_ATTR_TZ] = { .type = NLA_NESTED },
29 [THERMAL_GENL_ATTR_TZ_ID] = { .type = NLA_U32 },
30 [THERMAL_GENL_ATTR_TZ_TEMP] = { .type = NLA_U32 },
31 [THERMAL_GENL_ATTR_TZ_TRIP] = { .type = NLA_NESTED },
32 [THERMAL_GENL_ATTR_TZ_TRIP_ID] = { .type = NLA_U32 },
33 [THERMAL_GENL_ATTR_TZ_TRIP_TEMP] = { .type = NLA_U32 },
34 [THERMAL_GENL_ATTR_TZ_TRIP_TYPE] = { .type = NLA_U32 },
35 [THERMAL_GENL_ATTR_TZ_TRIP_HYST] = { .type = NLA_U32 },
36 [THERMAL_GENL_ATTR_TZ_MODE] = { .type = NLA_U32 },
37 [THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT] = { .type = NLA_U32 },
38 [THERMAL_GENL_ATTR_TZ_NAME] = { .type = NLA_STRING,
39 .len = THERMAL_NAME_LENGTH },
40 /* Governor(s) */
41 [THERMAL_GENL_ATTR_TZ_GOV] = { .type = NLA_NESTED },
42 [THERMAL_GENL_ATTR_TZ_GOV_NAME] = { .type = NLA_STRING,
43 .len = THERMAL_NAME_LENGTH },
44 /* Cooling devices */
45 [THERMAL_GENL_ATTR_CDEV] = { .type = NLA_NESTED },
46 [THERMAL_GENL_ATTR_CDEV_ID] = { .type = NLA_U32 },
47 [THERMAL_GENL_ATTR_CDEV_CUR_STATE] = { .type = NLA_U32 },
48 [THERMAL_GENL_ATTR_CDEV_MAX_STATE] = { .type = NLA_U32 },
49 [THERMAL_GENL_ATTR_CDEV_NAME] = { .type = NLA_STRING,
50 .len = THERMAL_NAME_LENGTH },
51 /* CPU capabilities */
52 [THERMAL_GENL_ATTR_CPU_CAPABILITY] = { .type = NLA_NESTED },
53 [THERMAL_GENL_ATTR_CPU_CAPABILITY_ID] = { .type = NLA_U32 },
54 [THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE] = { .type = NLA_U32 },
55 [THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY] = { .type = NLA_U32 },
56 };
57
58 struct param {
59 struct nlattr **attrs;
60 struct sk_buff *msg;
61 const char *name;
62 int tz_id;
63 int cdev_id;
64 int trip_id;
65 int trip_temp;
66 int trip_type;
67 int trip_hyst;
68 int temp;
69 int cdev_state;
70 int cdev_max_state;
71 struct thermal_genl_cpu_caps *cpu_capabilities;
72 int cpu_capabilities_count;
73 };
74
75 typedef int (*cb_t)(struct param *);
76
77 static struct genl_family thermal_gnl_family;
78
79 static int thermal_group_has_listeners(enum thermal_genl_multicast_groups group)
80 {
81 return genl_has_listeners(&thermal_gnl_family, &init_net, group);
82 }
83
84 /************************** Sampling encoding *******************************/
85
86 int thermal_genl_sampling_temp(int id, int temp)
87 {
88 struct sk_buff *skb;
89 void *hdr;
90
91 if (!thermal_group_has_listeners(THERMAL_GENL_SAMPLING_GROUP))
92 return 0;
93
94 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
95 if (!skb)
96 return -ENOMEM;
97
98 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0,
99 THERMAL_GENL_SAMPLING_TEMP);
100 if (!hdr)
101 goto out_free;
102
103 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_ID, id))
104 goto out_cancel;
105
106 if (nla_put_u32(skb, THERMAL_GENL_ATTR_TZ_TEMP, temp))
107 goto out_cancel;
108
109 genlmsg_end(skb, hdr);
110
111 genlmsg_multicast(&thermal_gnl_family, skb, 0, THERMAL_GENL_SAMPLING_GROUP, GFP_KERNEL);
112
113 return 0;
114 out_cancel:
115 genlmsg_cancel(skb, hdr);
116 out_free:
117 nlmsg_free(skb);
118
119 return -EMSGSIZE;
120 }
121
122 /**************************** Event encoding *********************************/
123
124 static int thermal_genl_event_tz_create(struct param *p)
125 {
126 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
127 nla_put_string(p->msg, THERMAL_GENL_ATTR_TZ_NAME, p->name))
128 return -EMSGSIZE;
129
130 return 0;
131 }
132
133 static int thermal_genl_event_tz(struct param *p)
134 {
135 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id))
136 return -EMSGSIZE;
137
138 return 0;
139 }
140
141 static int thermal_genl_event_tz_trip_up(struct param *p)
142 {
143 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
144 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
145 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
146 return -EMSGSIZE;
147
148 return 0;
149 }
150
151 static int thermal_genl_event_tz_trip_change(struct param *p)
152 {
153 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
154 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, p->trip_id) ||
155 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, p->trip_type) ||
156 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, p->trip_temp) ||
157 nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, p->trip_hyst))
158 return -EMSGSIZE;
159
160 return 0;
161 }
162
163 static int thermal_genl_event_cdev_add(struct param *p)
164 {
165 if (nla_put_string(p->msg, THERMAL_GENL_ATTR_CDEV_NAME,
166 p->name) ||
167 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
168 p->cdev_id) ||
169 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_MAX_STATE,
170 p->cdev_max_state))
171 return -EMSGSIZE;
172
173 return 0;
174 }
175
176 static int thermal_genl_event_cdev_delete(struct param *p)
177 {
178 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID, p->cdev_id))
179 return -EMSGSIZE;
180
181 return 0;
182 }
183
184 static int thermal_genl_event_cdev_state_update(struct param *p)
185 {
186 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_ID,
187 p->cdev_id) ||
188 nla_put_u32(p->msg, THERMAL_GENL_ATTR_CDEV_CUR_STATE,
189 p->cdev_state))
190 return -EMSGSIZE;
191
192 return 0;
193 }
194
195 static int thermal_genl_event_gov_change(struct param *p)
196 {
197 if (nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_ID, p->tz_id) ||
198 nla_put_string(p->msg, THERMAL_GENL_ATTR_GOV_NAME, p->name))
199 return -EMSGSIZE;
200
201 return 0;
202 }
203
204 static int thermal_genl_event_cpu_capability_change(struct param *p)
205 {
206 struct thermal_genl_cpu_caps *cpu_cap = p->cpu_capabilities;
207 struct sk_buff *msg = p->msg;
208 struct nlattr *start_cap;
209 int i;
210
211 start_cap = nla_nest_start(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY);
212 if (!start_cap)
213 return -EMSGSIZE;
214
215 for (i = 0; i < p->cpu_capabilities_count; ++i) {
216 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_ID,
217 cpu_cap->cpu))
218 goto out_cancel_nest;
219
220 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_PERFORMANCE,
221 cpu_cap->performance))
222 goto out_cancel_nest;
223
224 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CPU_CAPABILITY_EFFICIENCY,
225 cpu_cap->efficiency))
226 goto out_cancel_nest;
227
228 ++cpu_cap;
229 }
230
231 nla_nest_end(msg, start_cap);
232
233 return 0;
234 out_cancel_nest:
235 nla_nest_cancel(msg, start_cap);
236
237 return -EMSGSIZE;
238 }
239
240 int thermal_genl_event_tz_delete(struct param *p)
241 __attribute__((alias("thermal_genl_event_tz")));
242
243 int thermal_genl_event_tz_enable(struct param *p)
244 __attribute__((alias("thermal_genl_event_tz")));
245
246 int thermal_genl_event_tz_disable(struct param *p)
247 __attribute__((alias("thermal_genl_event_tz")));
248
249 int thermal_genl_event_tz_trip_down(struct param *p)
250 __attribute__((alias("thermal_genl_event_tz_trip_up")));
251
252 static cb_t event_cb[] = {
253 [THERMAL_GENL_EVENT_TZ_CREATE] = thermal_genl_event_tz_create,
254 [THERMAL_GENL_EVENT_TZ_DELETE] = thermal_genl_event_tz_delete,
255 [THERMAL_GENL_EVENT_TZ_ENABLE] = thermal_genl_event_tz_enable,
256 [THERMAL_GENL_EVENT_TZ_DISABLE] = thermal_genl_event_tz_disable,
257 [THERMAL_GENL_EVENT_TZ_TRIP_UP] = thermal_genl_event_tz_trip_up,
258 [THERMAL_GENL_EVENT_TZ_TRIP_DOWN] = thermal_genl_event_tz_trip_down,
259 [THERMAL_GENL_EVENT_TZ_TRIP_CHANGE] = thermal_genl_event_tz_trip_change,
260 [THERMAL_GENL_EVENT_CDEV_ADD] = thermal_genl_event_cdev_add,
261 [THERMAL_GENL_EVENT_CDEV_DELETE] = thermal_genl_event_cdev_delete,
262 [THERMAL_GENL_EVENT_CDEV_STATE_UPDATE] = thermal_genl_event_cdev_state_update,
263 [THERMAL_GENL_EVENT_TZ_GOV_CHANGE] = thermal_genl_event_gov_change,
264 [THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE] = thermal_genl_event_cpu_capability_change,
265 };
266
267 /*
268 * Generic netlink event encoding
269 */
270 static int thermal_genl_send_event(enum thermal_genl_event event,
271 struct param *p)
272 {
273 struct sk_buff *msg;
274 int ret = -EMSGSIZE;
275 void *hdr;
276
277 if (!thermal_group_has_listeners(THERMAL_GENL_EVENT_GROUP))
278 return 0;
279
280 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
281 if (!msg)
282 return -ENOMEM;
283 p->msg = msg;
284
285 hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
286 if (!hdr)
287 goto out_free_msg;
288
289 ret = event_cb[event](p);
290 if (ret)
291 goto out_cancel_msg;
292
293 genlmsg_end(msg, hdr);
294
295 genlmsg_multicast(&thermal_gnl_family, msg, 0, THERMAL_GENL_EVENT_GROUP, GFP_KERNEL);
296
297 return 0;
298
299 out_cancel_msg:
300 genlmsg_cancel(msg, hdr);
301 out_free_msg:
302 nlmsg_free(msg);
303
304 return ret;
305 }
306
307 int thermal_notify_tz_create(const struct thermal_zone_device *tz)
308 {
309 struct param p = { .tz_id = tz->id, .name = tz->type };
310
311 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
312 }
313
314 int thermal_notify_tz_delete(const struct thermal_zone_device *tz)
315 {
316 struct param p = { .tz_id = tz->id };
317
318 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
319 }
320
321 int thermal_notify_tz_enable(const struct thermal_zone_device *tz)
322 {
323 struct param p = { .tz_id = tz->id };
324
325 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
326 }
327
328 int thermal_notify_tz_disable(const struct thermal_zone_device *tz)
329 {
330 struct param p = { .tz_id = tz->id };
331
332 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
333 }
334
335 int thermal_notify_tz_trip_down(const struct thermal_zone_device *tz,
336 const struct thermal_trip *trip)
337 {
338 struct param p = { .tz_id = tz->id,
339 .trip_id = thermal_zone_trip_id(tz, trip),
340 .temp = tz->temperature };
341
342 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
343 }
344
345 int thermal_notify_tz_trip_up(const struct thermal_zone_device *tz,
346 const struct thermal_trip *trip)
347 {
348 struct param p = { .tz_id = tz->id,
349 .trip_id = thermal_zone_trip_id(tz, trip),
350 .temp = tz->temperature };
351
352 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
353 }
354
355 int thermal_notify_tz_trip_change(const struct thermal_zone_device *tz,
356 const struct thermal_trip *trip)
357 {
358 struct param p = { .tz_id = tz->id,
359 .trip_id = thermal_zone_trip_id(tz, trip),
360 .trip_type = trip->type,
361 .trip_temp = trip->temperature,
362 .trip_hyst = trip->hysteresis };
363
364 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
365 }
366
367 int thermal_notify_cdev_state_update(int cdev_id, int cdev_state)
368 {
369 struct param p = { .cdev_id = cdev_id, .cdev_state = cdev_state };
370
371 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
372 }
373
374 int thermal_notify_cdev_add(int cdev_id, const char *name, int cdev_max_state)
375 {
376 struct param p = { .cdev_id = cdev_id, .name = name,
377 .cdev_max_state = cdev_max_state };
378
379 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
380 }
381
382 int thermal_notify_cdev_delete(int cdev_id)
383 {
384 struct param p = { .cdev_id = cdev_id };
385
386 return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
387 }
388
389 int thermal_notify_tz_gov_change(const struct thermal_zone_device *tz,
390 const char *name)
391 {
392 struct param p = { .tz_id = tz->id, .name = name };
393
394 return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
395 }
396
397 int thermal_genl_cpu_capability_event(int count,
398 struct thermal_genl_cpu_caps *caps)
399 {
400 struct param p = { .cpu_capabilities_count = count, .cpu_capabilities = caps };
401
402 return thermal_genl_send_event(THERMAL_GENL_EVENT_CPU_CAPABILITY_CHANGE, &p);
403 }
404 EXPORT_SYMBOL_GPL(thermal_genl_cpu_capability_event);
405
406 /*************************** Command encoding ********************************/
407
408 static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
409 void *data)
410 {
411 struct sk_buff *msg = data;
412
413 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
414 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
415 return -EMSGSIZE;
416
417 return 0;
418 }
419
420 static int thermal_genl_cmd_tz_get_id(struct param *p)
421 {
422 struct sk_buff *msg = p->msg;
423 struct nlattr *start_tz;
424 int ret;
425
426 start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
427 if (!start_tz)
428 return -EMSGSIZE;
429
430 ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
431 if (ret)
432 goto out_cancel_nest;
433
434 nla_nest_end(msg, start_tz);
435
436 return 0;
437
438 out_cancel_nest:
439 nla_nest_cancel(msg, start_tz);
440
441 return ret;
442 }
443
444 static int thermal_genl_cmd_tz_get_trip(struct param *p)
445 {
446 struct sk_buff *msg = p->msg;
447 const struct thermal_trip *trip;
448 struct thermal_zone_device *tz;
449 struct nlattr *start_trip;
450 int id;
451
452 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
453 return -EINVAL;
454
455 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
456
457 tz = thermal_zone_get_by_id(id);
458 if (!tz)
459 return -EINVAL;
460
461 start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
462 if (!start_trip)
463 return -EMSGSIZE;
464
465 mutex_lock(&tz->lock);
466
467 for_each_trip(tz, trip) {
468 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID,
469 thermal_zone_trip_id(tz, trip)) ||
470 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, trip->type) ||
471 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, trip->temperature) ||
472 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, trip->hysteresis))
473 goto out_cancel_nest;
474 }
475
476 mutex_unlock(&tz->lock);
477
478 nla_nest_end(msg, start_trip);
479
480 return 0;
481
482 out_cancel_nest:
483 mutex_unlock(&tz->lock);
484
485 return -EMSGSIZE;
486 }
487
488 static int thermal_genl_cmd_tz_get_temp(struct param *p)
489 {
490 struct sk_buff *msg = p->msg;
491 struct thermal_zone_device *tz;
492 int temp, ret, id;
493
494 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
495 return -EINVAL;
496
497 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
498
499 tz = thermal_zone_get_by_id(id);
500 if (!tz)
501 return -EINVAL;
502
503 ret = thermal_zone_get_temp(tz, &temp);
504 if (ret)
505 return ret;
506
507 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
508 nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
509 return -EMSGSIZE;
510
511 return 0;
512 }
513
514 static int thermal_genl_cmd_tz_get_gov(struct param *p)
515 {
516 struct sk_buff *msg = p->msg;
517 struct thermal_zone_device *tz;
518 int id, ret = 0;
519
520 if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
521 return -EINVAL;
522
523 id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
524
525 tz = thermal_zone_get_by_id(id);
526 if (!tz)
527 return -EINVAL;
528
529 mutex_lock(&tz->lock);
530
531 if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
532 nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
533 tz->governor->name))
534 ret = -EMSGSIZE;
535
536 mutex_unlock(&tz->lock);
537
538 return ret;
539 }
540
541 static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
542 void *data)
543 {
544 struct sk_buff *msg = data;
545
546 if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
547 return -EMSGSIZE;
548
549 if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
550 return -EMSGSIZE;
551
552 return 0;
553 }
554
555 static int thermal_genl_cmd_cdev_get(struct param *p)
556 {
557 struct sk_buff *msg = p->msg;
558 struct nlattr *start_cdev;
559 int ret;
560
561 start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
562 if (!start_cdev)
563 return -EMSGSIZE;
564
565 ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
566 if (ret)
567 goto out_cancel_nest;
568
569 nla_nest_end(msg, start_cdev);
570
571 return 0;
572 out_cancel_nest:
573 nla_nest_cancel(msg, start_cdev);
574
575 return ret;
576 }
577
578 static cb_t cmd_cb[] = {
579 [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id,
580 [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip,
581 [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp,
582 [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov,
583 [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get,
584 };
585
586 static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
587 struct netlink_callback *cb)
588 {
589 struct param p = { .msg = skb };
590 const struct genl_dumpit_info *info = genl_dumpit_info(cb);
591 int cmd = info->op.cmd;
592 int ret;
593 void *hdr;
594
595 hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
596 if (!hdr)
597 return -EMSGSIZE;
598
599 ret = cmd_cb[cmd](&p);
600 if (ret)
601 goto out_cancel_msg;
602
603 genlmsg_end(skb, hdr);
604
605 return 0;
606
607 out_cancel_msg:
608 genlmsg_cancel(skb, hdr);
609
610 return ret;
611 }
612
613 static int thermal_genl_cmd_doit(struct sk_buff *skb,
614 struct genl_info *info)
615 {
616 struct param p = { .attrs = info->attrs };
617 struct sk_buff *msg;
618 void *hdr;
619 int cmd = info->genlhdr->cmd;
620 int ret = -EMSGSIZE;
621
622 msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
623 if (!msg)
624 return -ENOMEM;
625 p.msg = msg;
626
627 hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);
628 if (!hdr)
629 goto out_free_msg;
630
631 ret = cmd_cb[cmd](&p);
632 if (ret)
633 goto out_cancel_msg;
634
635 genlmsg_end(msg, hdr);
636
637 return genlmsg_reply(msg, info);
638
639 out_cancel_msg:
640 genlmsg_cancel(msg, hdr);
641 out_free_msg:
642 nlmsg_free(msg);
643
644 return ret;
645 }
646
647 static const struct genl_small_ops thermal_genl_ops[] = {
648 {
649 .cmd = THERMAL_GENL_CMD_TZ_GET_ID,
650 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
651 .dumpit = thermal_genl_cmd_dumpit,
652 },
653 {
654 .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
655 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
656 .doit = thermal_genl_cmd_doit,
657 },
658 {
659 .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
660 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
661 .doit = thermal_genl_cmd_doit,
662 },
663 {
664 .cmd = THERMAL_GENL_CMD_TZ_GET_GOV,
665 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
666 .doit = thermal_genl_cmd_doit,
667 },
668 {
669 .cmd = THERMAL_GENL_CMD_CDEV_GET,
670 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
671 .dumpit = thermal_genl_cmd_dumpit,
672 },
673 };
674
675 static struct genl_family thermal_gnl_family __ro_after_init = {
676 .hdrsize = 0,
677 .name = THERMAL_GENL_FAMILY_NAME,
678 .version = THERMAL_GENL_VERSION,
679 .maxattr = THERMAL_GENL_ATTR_MAX,
680 .policy = thermal_genl_policy,
681 .small_ops = thermal_genl_ops,
682 .n_small_ops = ARRAY_SIZE(thermal_genl_ops),
683 .resv_start_op = THERMAL_GENL_CMD_CDEV_GET + 1,
684 .mcgrps = thermal_genl_mcgrps,
685 .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps),
686 };
687
688 int __init thermal_netlink_init(void)
689 {
690 return genl_register_family(&thermal_gnl_family);
691 }
692
693 void __init thermal_netlink_exit(void)
694 {
695 genl_unregister_family(&thermal_gnl_family);
696 }