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