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