147c4b0deSDaniel Lezcano // SPDX-License-Identifier: LGPL-2.1+
247c4b0deSDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
347c4b0deSDaniel Lezcano #include <errno.h>
447c4b0deSDaniel Lezcano #include <stdio.h>
547c4b0deSDaniel Lezcano #include <stdlib.h>
647c4b0deSDaniel Lezcano #include <unistd.h>
747c4b0deSDaniel Lezcano
847c4b0deSDaniel Lezcano #include <thermal.h>
947c4b0deSDaniel Lezcano #include "thermal_nl.h"
1047c4b0deSDaniel Lezcano
handle_thermal_sample(struct nl_msg * n,void * arg)1147c4b0deSDaniel Lezcano static int handle_thermal_sample(struct nl_msg *n, void *arg)
1247c4b0deSDaniel Lezcano {
1347c4b0deSDaniel Lezcano struct nlmsghdr *nlh = nlmsg_hdr(n);
1447c4b0deSDaniel Lezcano struct genlmsghdr *genlhdr = genlmsg_hdr(nlh);
1547c4b0deSDaniel Lezcano struct nlattr *attrs[THERMAL_GENL_ATTR_MAX + 1];
1647c4b0deSDaniel Lezcano struct thermal_handler_param *thp = arg;
1747c4b0deSDaniel Lezcano struct thermal_handler *th = thp->th;
1847c4b0deSDaniel Lezcano
19*5f2d0b60SEmil Dahl Juhl arg = thp->arg;
20*5f2d0b60SEmil Dahl Juhl
2147c4b0deSDaniel Lezcano genlmsg_parse(nlh, 0, attrs, THERMAL_GENL_ATTR_MAX, NULL);
2247c4b0deSDaniel Lezcano
2347c4b0deSDaniel Lezcano switch (genlhdr->cmd) {
2447c4b0deSDaniel Lezcano
2547c4b0deSDaniel Lezcano case THERMAL_GENL_SAMPLING_TEMP:
2647c4b0deSDaniel Lezcano return th->ops->sampling.tz_temp(
2747c4b0deSDaniel Lezcano nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_ID]),
2847c4b0deSDaniel Lezcano nla_get_u32(attrs[THERMAL_GENL_ATTR_TZ_TEMP]), arg);
2947c4b0deSDaniel Lezcano default:
3047c4b0deSDaniel Lezcano return THERMAL_ERROR;
3147c4b0deSDaniel Lezcano }
3247c4b0deSDaniel Lezcano }
3347c4b0deSDaniel Lezcano
thermal_sampling_handle(struct thermal_handler * th,void * arg)3447c4b0deSDaniel Lezcano thermal_error_t thermal_sampling_handle(struct thermal_handler *th, void *arg)
3547c4b0deSDaniel Lezcano {
3647c4b0deSDaniel Lezcano struct thermal_handler_param thp = { .th = th, .arg = arg };
3747c4b0deSDaniel Lezcano
3847c4b0deSDaniel Lezcano if (!th)
3947c4b0deSDaniel Lezcano return THERMAL_ERROR;
4047c4b0deSDaniel Lezcano
4147c4b0deSDaniel Lezcano if (nl_cb_set(th->cb_sampling, NL_CB_VALID, NL_CB_CUSTOM,
4247c4b0deSDaniel Lezcano handle_thermal_sample, &thp))
4347c4b0deSDaniel Lezcano return THERMAL_ERROR;
4447c4b0deSDaniel Lezcano
4547c4b0deSDaniel Lezcano return nl_recvmsgs(th->sk_sampling, th->cb_sampling);
4647c4b0deSDaniel Lezcano }
4747c4b0deSDaniel Lezcano
thermal_sampling_fd(struct thermal_handler * th)4847c4b0deSDaniel Lezcano int thermal_sampling_fd(struct thermal_handler *th)
4947c4b0deSDaniel Lezcano {
5047c4b0deSDaniel Lezcano if (!th)
5147c4b0deSDaniel Lezcano return -1;
5247c4b0deSDaniel Lezcano
5347c4b0deSDaniel Lezcano return nl_socket_get_fd(th->sk_sampling);
5447c4b0deSDaniel Lezcano }
5547c4b0deSDaniel Lezcano
thermal_sampling_exit(struct thermal_handler * th)5647c4b0deSDaniel Lezcano thermal_error_t thermal_sampling_exit(struct thermal_handler *th)
5747c4b0deSDaniel Lezcano {
5847c4b0deSDaniel Lezcano if (nl_unsubscribe_thermal(th->sk_sampling, th->cb_sampling,
59a29cbd76SVincent Guittot THERMAL_GENL_SAMPLING_GROUP_NAME))
6047c4b0deSDaniel Lezcano return THERMAL_ERROR;
6147c4b0deSDaniel Lezcano
6247c4b0deSDaniel Lezcano nl_thermal_disconnect(th->sk_sampling, th->cb_sampling);
6347c4b0deSDaniel Lezcano
6447c4b0deSDaniel Lezcano return THERMAL_SUCCESS;
6547c4b0deSDaniel Lezcano }
6647c4b0deSDaniel Lezcano
thermal_sampling_init(struct thermal_handler * th)6747c4b0deSDaniel Lezcano thermal_error_t thermal_sampling_init(struct thermal_handler *th)
6847c4b0deSDaniel Lezcano {
6947c4b0deSDaniel Lezcano if (nl_thermal_connect(&th->sk_sampling, &th->cb_sampling))
7047c4b0deSDaniel Lezcano return THERMAL_ERROR;
7147c4b0deSDaniel Lezcano
7247c4b0deSDaniel Lezcano if (nl_subscribe_thermal(th->sk_sampling, th->cb_sampling,
7347c4b0deSDaniel Lezcano THERMAL_GENL_SAMPLING_GROUP_NAME))
7447c4b0deSDaniel Lezcano return THERMAL_ERROR;
7547c4b0deSDaniel Lezcano
7647c4b0deSDaniel Lezcano return THERMAL_SUCCESS;
7747c4b0deSDaniel Lezcano }
78