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 
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 
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 
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 
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 	    nla_put_u32(p->msg, THERMAL_GENL_ATTR_TZ_TEMP, p->temp))
126 		return -EMSGSIZE;
127 
128 	return 0;
129 }
130 
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 
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 
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 
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 
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 
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  */
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 
234 	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
235 	if (!msg)
236 		return -ENOMEM;
237 	p->msg = msg;
238 
239 	hdr = genlmsg_put(msg, 0, 0, &thermal_gnl_family, 0, event);
240 	if (!hdr)
241 		goto out_free_msg;
242 
243 	ret = event_cb[event](p);
244 	if (ret)
245 		goto out_cancel_msg;
246 
247 	genlmsg_end(msg, hdr);
248 
249 	genlmsg_multicast(&thermal_gnl_family, msg, 0, 1, GFP_KERNEL);
250 
251 	return 0;
252 
253 out_cancel_msg:
254 	genlmsg_cancel(msg, hdr);
255 out_free_msg:
256 	nlmsg_free(msg);
257 
258 	return ret;
259 }
260 
261 int thermal_notify_tz_create(int tz_id, const char *name)
262 {
263 	struct param p = { .tz_id = tz_id, .name = name };
264 
265 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_CREATE, &p);
266 }
267 
268 int thermal_notify_tz_delete(int tz_id)
269 {
270 	struct param p = { .tz_id = tz_id };
271 
272 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DELETE, &p);
273 }
274 
275 int thermal_notify_tz_enable(int tz_id)
276 {
277 	struct param p = { .tz_id = tz_id };
278 
279 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_ENABLE, &p);
280 }
281 
282 int thermal_notify_tz_disable(int tz_id)
283 {
284 	struct param p = { .tz_id = tz_id };
285 
286 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_DISABLE, &p);
287 }
288 
289 int thermal_notify_tz_trip_down(int tz_id, int trip_id, int temp)
290 {
291 	struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
292 
293 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DOWN, &p);
294 }
295 
296 int thermal_notify_tz_trip_up(int tz_id, int trip_id, int temp)
297 {
298 	struct param p = { .tz_id = tz_id, .trip_id = trip_id, .temp = temp };
299 
300 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_UP, &p);
301 }
302 
303 int thermal_notify_tz_trip_add(int tz_id, int trip_id, int trip_type,
304 			       int trip_temp, int trip_hyst)
305 {
306 	struct param p = { .tz_id = tz_id, .trip_id = trip_id,
307 			   .trip_type = trip_type, .trip_temp = trip_temp,
308 			   .trip_hyst = trip_hyst };
309 
310 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_ADD, &p);
311 }
312 
313 int thermal_notify_tz_trip_delete(int tz_id, int trip_id)
314 {
315 	struct param p = { .tz_id = tz_id, .trip_id = trip_id };
316 
317 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_DELETE, &p);
318 }
319 
320 int thermal_notify_tz_trip_change(int tz_id, int trip_id, int trip_type,
321 				  int trip_temp, int trip_hyst)
322 {
323 	struct param p = { .tz_id = tz_id, .trip_id = trip_id,
324 			   .trip_type = trip_type, .trip_temp = trip_temp,
325 			   .trip_hyst = trip_hyst };
326 
327 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_TRIP_CHANGE, &p);
328 }
329 
330 int thermal_notify_cdev_state_update(int cdev_id, int cdev_state)
331 {
332 	struct param p = { .cdev_id = cdev_id, .cdev_state = cdev_state };
333 
334 	return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_STATE_UPDATE, &p);
335 }
336 
337 int thermal_notify_cdev_add(int cdev_id, const char *name, int cdev_max_state)
338 {
339 	struct param p = { .cdev_id = cdev_id, .name = name,
340 			   .cdev_max_state = cdev_max_state };
341 
342 	return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_ADD, &p);
343 }
344 
345 int thermal_notify_cdev_delete(int cdev_id)
346 {
347 	struct param p = { .cdev_id = cdev_id };
348 
349 	return thermal_genl_send_event(THERMAL_GENL_EVENT_CDEV_DELETE, &p);
350 }
351 
352 int thermal_notify_tz_gov_change(int tz_id, const char *name)
353 {
354 	struct param p = { .tz_id = tz_id, .name = name };
355 
356 	return thermal_genl_send_event(THERMAL_GENL_EVENT_TZ_GOV_CHANGE, &p);
357 }
358 
359 /*************************** Command encoding ********************************/
360 
361 static int __thermal_genl_cmd_tz_get_id(struct thermal_zone_device *tz,
362 					void *data)
363 {
364 	struct sk_buff *msg = data;
365 
366 	if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, tz->id) ||
367 	    nla_put_string(msg, THERMAL_GENL_ATTR_TZ_NAME, tz->type))
368 		return -EMSGSIZE;
369 
370 	return 0;
371 }
372 
373 static int thermal_genl_cmd_tz_get_id(struct param *p)
374 {
375 	struct sk_buff *msg = p->msg;
376 	struct nlattr *start_tz;
377 	int ret;
378 
379 	start_tz = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ);
380 	if (!start_tz)
381 		return -EMSGSIZE;
382 
383 	ret = for_each_thermal_zone(__thermal_genl_cmd_tz_get_id, msg);
384 	if (ret)
385 		goto out_cancel_nest;
386 
387 	nla_nest_end(msg, start_tz);
388 
389 	return 0;
390 
391 out_cancel_nest:
392 	nla_nest_cancel(msg, start_tz);
393 
394 	return ret;
395 }
396 
397 static int thermal_genl_cmd_tz_get_trip(struct param *p)
398 {
399 	struct sk_buff *msg = p->msg;
400 	struct thermal_zone_device *tz;
401 	struct nlattr *start_trip;
402 	int i, id;
403 
404 	if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
405 		return -EINVAL;
406 
407 	id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
408 
409 	tz = thermal_zone_get_by_id(id);
410 	if (!tz)
411 		return -EINVAL;
412 
413 	start_trip = nla_nest_start(msg, THERMAL_GENL_ATTR_TZ_TRIP);
414 	if (!start_trip)
415 		return -EMSGSIZE;
416 
417 	mutex_lock(&tz->lock);
418 
419 	for (i = 0; i < tz->trips; i++) {
420 
421 		enum thermal_trip_type type;
422 		int temp, hyst = 0;
423 
424 		tz->ops->get_trip_type(tz, i, &type);
425 		tz->ops->get_trip_temp(tz, i, &temp);
426 		if (tz->ops->get_trip_hyst)
427 			tz->ops->get_trip_hyst(tz, i, &hyst);
428 
429 		if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) ||
430 		    nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, type) ||
431 		    nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, temp) ||
432 		    nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, hyst))
433 			goto out_cancel_nest;
434 	}
435 
436 	mutex_unlock(&tz->lock);
437 
438 	nla_nest_end(msg, start_trip);
439 
440 	return 0;
441 
442 out_cancel_nest:
443 	mutex_unlock(&tz->lock);
444 
445 	return -EMSGSIZE;
446 }
447 
448 static int thermal_genl_cmd_tz_get_temp(struct param *p)
449 {
450 	struct sk_buff *msg = p->msg;
451 	struct thermal_zone_device *tz;
452 	int temp, ret, id;
453 
454 	if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
455 		return -EINVAL;
456 
457 	id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
458 
459 	tz = thermal_zone_get_by_id(id);
460 	if (!tz)
461 		return -EINVAL;
462 
463 	ret = thermal_zone_get_temp(tz, &temp);
464 	if (ret)
465 		return ret;
466 
467 	if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
468 	    nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp))
469 		return -EMSGSIZE;
470 
471 	return 0;
472 }
473 
474 static int thermal_genl_cmd_tz_get_gov(struct param *p)
475 {
476 	struct sk_buff *msg = p->msg;
477 	struct thermal_zone_device *tz;
478 	int id, ret = 0;
479 
480 	if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID])
481 		return -EINVAL;
482 
483 	id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]);
484 
485 	tz = thermal_zone_get_by_id(id);
486 	if (!tz)
487 		return -EINVAL;
488 
489 	mutex_lock(&tz->lock);
490 
491 	if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) ||
492 	    nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME,
493 			   tz->governor->name))
494 		ret = -EMSGSIZE;
495 
496 	mutex_unlock(&tz->lock);
497 
498 	return ret;
499 }
500 
501 static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev,
502 				       void *data)
503 {
504 	struct sk_buff *msg = data;
505 
506 	if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id))
507 		return -EMSGSIZE;
508 
509 	if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type))
510 		return -EMSGSIZE;
511 
512 	return 0;
513 }
514 
515 static int thermal_genl_cmd_cdev_get(struct param *p)
516 {
517 	struct sk_buff *msg = p->msg;
518 	struct nlattr *start_cdev;
519 	int ret;
520 
521 	start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV);
522 	if (!start_cdev)
523 		return -EMSGSIZE;
524 
525 	ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg);
526 	if (ret)
527 		goto out_cancel_nest;
528 
529 	nla_nest_end(msg, start_cdev);
530 
531 	return 0;
532 out_cancel_nest:
533 	nla_nest_cancel(msg, start_cdev);
534 
535 	return ret;
536 }
537 
538 static cb_t cmd_cb[] = {
539 	[THERMAL_GENL_CMD_TZ_GET_ID]	= thermal_genl_cmd_tz_get_id,
540 	[THERMAL_GENL_CMD_TZ_GET_TRIP]	= thermal_genl_cmd_tz_get_trip,
541 	[THERMAL_GENL_CMD_TZ_GET_TEMP]	= thermal_genl_cmd_tz_get_temp,
542 	[THERMAL_GENL_CMD_TZ_GET_GOV]	= thermal_genl_cmd_tz_get_gov,
543 	[THERMAL_GENL_CMD_CDEV_GET]	= thermal_genl_cmd_cdev_get,
544 };
545 
546 static int thermal_genl_cmd_dumpit(struct sk_buff *skb,
547 				   struct netlink_callback *cb)
548 {
549 	struct param p = { .msg = skb };
550 	const struct genl_dumpit_info *info = genl_dumpit_info(cb);
551 	int cmd = info->op.cmd;
552 	int ret;
553 	void *hdr;
554 
555 	hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd);
556 	if (!hdr)
557 		return -EMSGSIZE;
558 
559 	ret = cmd_cb[cmd](&p);
560 	if (ret)
561 		goto out_cancel_msg;
562 
563 	genlmsg_end(skb, hdr);
564 
565 	return 0;
566 
567 out_cancel_msg:
568 	genlmsg_cancel(skb, hdr);
569 
570 	return ret;
571 }
572 
573 static int thermal_genl_cmd_doit(struct sk_buff *skb,
574 				 struct genl_info *info)
575 {
576 	struct param p = { .attrs = info->attrs };
577 	struct sk_buff *msg;
578 	void *hdr;
579 	int cmd = info->genlhdr->cmd;
580 	int ret = -EMSGSIZE;
581 
582 	msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
583 	if (!msg)
584 		return -ENOMEM;
585 	p.msg = msg;
586 
587 	hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd);
588 	if (!hdr)
589 		goto out_free_msg;
590 
591 	ret = cmd_cb[cmd](&p);
592 	if (ret)
593 		goto out_cancel_msg;
594 
595 	genlmsg_end(msg, hdr);
596 
597 	return genlmsg_reply(msg, info);
598 
599 out_cancel_msg:
600 	genlmsg_cancel(msg, hdr);
601 out_free_msg:
602 	nlmsg_free(msg);
603 
604 	return ret;
605 }
606 
607 static const struct genl_small_ops thermal_genl_ops[] = {
608 	{
609 		.cmd = THERMAL_GENL_CMD_TZ_GET_ID,
610 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
611 		.dumpit = thermal_genl_cmd_dumpit,
612 	},
613 	{
614 		.cmd = THERMAL_GENL_CMD_TZ_GET_TRIP,
615 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
616 		.doit = thermal_genl_cmd_doit,
617 	},
618 	{
619 		.cmd = THERMAL_GENL_CMD_TZ_GET_TEMP,
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_GOV,
625 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
626 		.doit = thermal_genl_cmd_doit,
627 	},
628 	{
629 		.cmd = THERMAL_GENL_CMD_CDEV_GET,
630 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
631 		.dumpit = thermal_genl_cmd_dumpit,
632 	},
633 };
634 
635 static struct genl_family thermal_gnl_family __ro_after_init = {
636 	.hdrsize	= 0,
637 	.name		= THERMAL_GENL_FAMILY_NAME,
638 	.version	= THERMAL_GENL_VERSION,
639 	.maxattr	= THERMAL_GENL_ATTR_MAX,
640 	.policy		= thermal_genl_policy,
641 	.small_ops	= thermal_genl_ops,
642 	.n_small_ops	= ARRAY_SIZE(thermal_genl_ops),
643 	.mcgrps		= thermal_genl_mcgrps,
644 	.n_mcgrps	= ARRAY_SIZE(thermal_genl_mcgrps),
645 };
646 
647 int __init thermal_netlink_init(void)
648 {
649 	return genl_register_family(&thermal_gnl_family);
650 }
651