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