Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 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) |
Jing Xiangfeng | 48b4585 | 2020-09-29 16:26:52 +0800 | [diff] [blame] | 81 | goto out_free; |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 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); |
Jing Xiangfeng | 48b4585 | 2020-09-29 16:26:52 +0800 | [diff] [blame] | 96 | out_free: |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 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 | return -EMSGSIZE; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | */ |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | return -EMSGSIZE; |
| 415 | |
| 416 | mutex_lock(&tz->lock); |
| 417 | |
| 418 | for (i = 0; i < tz->trips; i++) { |
| 419 | |
| 420 | enum thermal_trip_type type; |
| 421 | int temp, hyst; |
| 422 | |
| 423 | tz->ops->get_trip_type(tz, i, &type); |
| 424 | tz->ops->get_trip_temp(tz, i, &temp); |
| 425 | tz->ops->get_trip_hyst(tz, i, &hyst); |
| 426 | |
| 427 | if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_ID, i) || |
| 428 | nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TYPE, type) || |
| 429 | nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_TEMP, temp) || |
| 430 | nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TRIP_HYST, hyst)) |
| 431 | goto out_cancel_nest; |
| 432 | } |
| 433 | |
| 434 | mutex_unlock(&tz->lock); |
| 435 | |
| 436 | nla_nest_end(msg, start_trip); |
| 437 | |
| 438 | return 0; |
| 439 | |
| 440 | out_cancel_nest: |
| 441 | mutex_unlock(&tz->lock); |
| 442 | |
| 443 | return -EMSGSIZE; |
| 444 | } |
| 445 | |
| 446 | static int thermal_genl_cmd_tz_get_temp(struct param *p) |
| 447 | { |
| 448 | struct sk_buff *msg = p->msg; |
| 449 | struct thermal_zone_device *tz; |
| 450 | int temp, ret, 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 | ret = thermal_zone_get_temp(tz, &temp); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | |
| 465 | if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) || |
| 466 | nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_TEMP, temp)) |
| 467 | return -EMSGSIZE; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int thermal_genl_cmd_tz_get_gov(struct param *p) |
| 473 | { |
| 474 | struct sk_buff *msg = p->msg; |
| 475 | struct thermal_zone_device *tz; |
| 476 | int id, ret = 0; |
| 477 | |
| 478 | if (!p->attrs[THERMAL_GENL_ATTR_TZ_ID]) |
| 479 | return -EINVAL; |
| 480 | |
| 481 | id = nla_get_u32(p->attrs[THERMAL_GENL_ATTR_TZ_ID]); |
| 482 | |
| 483 | tz = thermal_zone_get_by_id(id); |
| 484 | if (!tz) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | mutex_lock(&tz->lock); |
| 488 | |
| 489 | if (nla_put_u32(msg, THERMAL_GENL_ATTR_TZ_ID, id) || |
| 490 | nla_put_string(msg, THERMAL_GENL_ATTR_TZ_GOV_NAME, |
| 491 | tz->governor->name)) |
| 492 | ret = -EMSGSIZE; |
| 493 | |
| 494 | mutex_unlock(&tz->lock); |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | static int __thermal_genl_cmd_cdev_get(struct thermal_cooling_device *cdev, |
| 500 | void *data) |
| 501 | { |
| 502 | struct sk_buff *msg = data; |
| 503 | |
| 504 | if (nla_put_u32(msg, THERMAL_GENL_ATTR_CDEV_ID, cdev->id)) |
| 505 | return -EMSGSIZE; |
| 506 | |
| 507 | if (nla_put_string(msg, THERMAL_GENL_ATTR_CDEV_NAME, cdev->type)) |
| 508 | return -EMSGSIZE; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int thermal_genl_cmd_cdev_get(struct param *p) |
| 514 | { |
| 515 | struct sk_buff *msg = p->msg; |
| 516 | struct nlattr *start_cdev; |
| 517 | int ret; |
| 518 | |
| 519 | start_cdev = nla_nest_start(msg, THERMAL_GENL_ATTR_CDEV); |
| 520 | if (!start_cdev) |
| 521 | return -EMSGSIZE; |
| 522 | |
| 523 | ret = for_each_thermal_cooling_device(__thermal_genl_cmd_cdev_get, msg); |
| 524 | if (ret) |
| 525 | goto out_cancel_nest; |
| 526 | |
| 527 | nla_nest_end(msg, start_cdev); |
| 528 | |
| 529 | return 0; |
| 530 | out_cancel_nest: |
| 531 | nla_nest_cancel(msg, start_cdev); |
| 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | static cb_t cmd_cb[] = { |
| 537 | [THERMAL_GENL_CMD_TZ_GET_ID] = thermal_genl_cmd_tz_get_id, |
| 538 | [THERMAL_GENL_CMD_TZ_GET_TRIP] = thermal_genl_cmd_tz_get_trip, |
| 539 | [THERMAL_GENL_CMD_TZ_GET_TEMP] = thermal_genl_cmd_tz_get_temp, |
| 540 | [THERMAL_GENL_CMD_TZ_GET_GOV] = thermal_genl_cmd_tz_get_gov, |
| 541 | [THERMAL_GENL_CMD_CDEV_GET] = thermal_genl_cmd_cdev_get, |
| 542 | }; |
| 543 | |
| 544 | static int thermal_genl_cmd_dumpit(struct sk_buff *skb, |
| 545 | struct netlink_callback *cb) |
| 546 | { |
| 547 | struct param p = { .msg = skb }; |
| 548 | const struct genl_dumpit_info *info = genl_dumpit_info(cb); |
Jakub Kicinski | 0b588afd | 2020-10-02 14:49:53 -0700 | [diff] [blame] | 549 | int cmd = info->op.cmd; |
Colin Ian King | 52674f5 | 2020-07-06 15:07:47 +0100 | [diff] [blame] | 550 | int ret; |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 551 | void *hdr; |
| 552 | |
| 553 | hdr = genlmsg_put(skb, 0, 0, &thermal_gnl_family, 0, cmd); |
| 554 | if (!hdr) |
| 555 | return -EMSGSIZE; |
| 556 | |
| 557 | ret = cmd_cb[cmd](&p); |
| 558 | if (ret) |
| 559 | goto out_cancel_msg; |
| 560 | |
| 561 | genlmsg_end(skb, hdr); |
| 562 | |
| 563 | return 0; |
| 564 | |
| 565 | out_cancel_msg: |
| 566 | genlmsg_cancel(skb, hdr); |
| 567 | |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | static int thermal_genl_cmd_doit(struct sk_buff *skb, |
| 572 | struct genl_info *info) |
| 573 | { |
| 574 | struct param p = { .attrs = info->attrs }; |
| 575 | struct sk_buff *msg; |
| 576 | void *hdr; |
| 577 | int cmd = info->genlhdr->cmd; |
| 578 | int ret = -EMSGSIZE; |
| 579 | |
| 580 | msg = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
| 581 | if (!msg) |
| 582 | return -ENOMEM; |
| 583 | p.msg = msg; |
| 584 | |
| 585 | hdr = genlmsg_put_reply(msg, info, &thermal_gnl_family, 0, cmd); |
| 586 | if (!hdr) |
| 587 | goto out_free_msg; |
| 588 | |
| 589 | ret = cmd_cb[cmd](&p); |
| 590 | if (ret) |
| 591 | goto out_cancel_msg; |
| 592 | |
| 593 | genlmsg_end(msg, hdr); |
| 594 | |
| 595 | return genlmsg_reply(msg, info); |
| 596 | |
| 597 | out_cancel_msg: |
| 598 | genlmsg_cancel(msg, hdr); |
| 599 | out_free_msg: |
| 600 | nlmsg_free(msg); |
| 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 605 | static const struct genl_small_ops thermal_genl_ops[] = { |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 606 | { |
| 607 | .cmd = THERMAL_GENL_CMD_TZ_GET_ID, |
| 608 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 609 | .dumpit = thermal_genl_cmd_dumpit, |
| 610 | }, |
| 611 | { |
| 612 | .cmd = THERMAL_GENL_CMD_TZ_GET_TRIP, |
| 613 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 614 | .doit = thermal_genl_cmd_doit, |
| 615 | }, |
| 616 | { |
| 617 | .cmd = THERMAL_GENL_CMD_TZ_GET_TEMP, |
| 618 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 619 | .doit = thermal_genl_cmd_doit, |
| 620 | }, |
| 621 | { |
| 622 | .cmd = THERMAL_GENL_CMD_TZ_GET_GOV, |
| 623 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 624 | .doit = thermal_genl_cmd_doit, |
| 625 | }, |
| 626 | { |
| 627 | .cmd = THERMAL_GENL_CMD_CDEV_GET, |
| 628 | .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, |
| 629 | .dumpit = thermal_genl_cmd_dumpit, |
| 630 | }, |
| 631 | }; |
| 632 | |
| 633 | static struct genl_family thermal_gnl_family __ro_after_init = { |
| 634 | .hdrsize = 0, |
| 635 | .name = THERMAL_GENL_FAMILY_NAME, |
| 636 | .version = THERMAL_GENL_VERSION, |
| 637 | .maxattr = THERMAL_GENL_ATTR_MAX, |
| 638 | .policy = thermal_genl_policy, |
Jakub Kicinski | 66a9b92 | 2020-10-02 14:49:54 -0700 | [diff] [blame] | 639 | .small_ops = thermal_genl_ops, |
| 640 | .n_small_ops = ARRAY_SIZE(thermal_genl_ops), |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 641 | .mcgrps = thermal_genl_mcgrps, |
| 642 | .n_mcgrps = ARRAY_SIZE(thermal_genl_mcgrps), |
| 643 | }; |
| 644 | |
Daniel Lezcano | d2a89b5 | 2020-07-17 18:42:16 +0200 | [diff] [blame] | 645 | int __init thermal_netlink_init(void) |
Daniel Lezcano | 1ce50e7 | 2020-07-06 12:55:37 +0200 | [diff] [blame] | 646 | { |
| 647 | return genl_register_family(&thermal_gnl_family); |
| 648 | } |