summaryrefslogtreecommitdiff
path: root/src/libthermal/thermal.h
blob: e89a3ba3eeb115f72c8cefb1508d9227bf2ecfa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
#ifndef __THERMAL_H
#define __THERMAL_H

// #include <linux/thermal.h>

#include <libnl3/netlink/genl/genl.h>
#include <libnl3/netlink/genl/mngt.h>
#include <libnl3/netlink/genl/ctrl.h>
#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>

#define __maybe_unused		__attribute__((__unused__))

#ifndef THERMAL_NAME_LENGTH
#define THERMAL_NAME_LENGTH 64
#endif

struct thermal_sampling_ops {
	int (*tz_temp)(int tz_id, int temp, void *arg);
};

struct thermal_events_ops {
	int (*tz_create)(const char *name, int tz_id, void *arg);
	int (*tz_delete)(int tz_id, void *arg);
	int (*tz_enable)(int tz_id, void *arg);
	int (*tz_disable)(int tz_id, void *arg);
	int (*trip_high)(int tz_id, int trip_id, int temp, void *arg);
	int (*trip_low)(int tz_id, int trip_id, int temp, void *arg);
	int (*trip_add)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
	int (*trip_change)(int tz_id, int trip_id, int type, int temp, int hyst, void *arg);
	int (*trip_delete)(int tz_id, int trip_id, void *arg);
	int (*cdev_add)(const char *name, int cdev_id, int max_state, void *arg);
	int (*cdev_delete)(int cdev_id, void *arg);
	int (*cdev_update)(int cdev_id, int cur_state, void *arg);
	int (*gov_change)(int tz_id, const char *gov_name, void *arg);
};

struct thermal_ops {
	struct thermal_sampling_ops sampling;
	struct thermal_events_ops events;
};

struct thermal_trip {
	int id;
	int type;
	int temp;
	int hyst;
};

struct thermal_zone {
	int id;
	int temp;
	char name[THERMAL_NAME_LENGTH];
	char governor[THERMAL_NAME_LENGTH];
	struct thermal_trip *trip;
};

struct thermal_cdev {
	int id;
	char name[THERMAL_NAME_LENGTH];
	int max_state;
	int min_state;
	int cur_state;
};

struct thermal_handler {
	int done;
	int error;
	struct thermal_ops *ops;
	struct nl_msg *msg;
	struct nl_sock *sk_event;
	struct nl_sock *sk_sampling;
	struct nl_sock *sk_cmd;
	struct nl_cb *cb_cmd;
	struct nl_cb *cb_event;
	struct nl_cb *cb_sampling;
};

struct thermal_handler_param {
	struct thermal_handler *th;
	void *arg;
};

typedef int (*cb_tz_t)(struct thermal_zone *, void *);

typedef int (*cb_tt_t)(struct thermal_trip *, void *);

typedef int (*cb_tc_t)(struct thermal_cdev *, void *);

int for_each_thermal_zone(struct thermal_zone *tz, cb_tz_t cb, void *arg);

int for_each_thermal_trip(struct thermal_trip *tt, cb_tt_t cb, void *arg);

int for_each_thermal_cdev(struct thermal_cdev *cdev, cb_tc_t cb, void *arg);

struct thermal_zone *thermal_zone_discover(struct thermal_handler *th);

struct thermal_handler *thermal_init(struct thermal_ops *ops);

/*
 * Netlink thermal events
 */
extern int thermal_events_init(struct thermal_handler *th);

extern int thermal_events_handle(struct thermal_handler *th, void *arg);

extern int thermal_events_fd(struct thermal_handler *th);

/*
 * Netlink thermal commands
 */
extern int thermal_cmd_init(struct thermal_handler *th);

extern int thermal_cmd_get_tz(struct thermal_handler *th, struct thermal_zone **tz);

extern int thermal_cmd_get_cdev(struct thermal_handler *th, struct thermal_cdev **tc);

extern int thermal_cmd_get_trip(struct thermal_handler *th, struct thermal_zone *tz);

extern int thermal_cmd_get_governor(struct thermal_handler *th, struct thermal_zone *tz);

extern int thermal_cmd_get_temp(struct thermal_handler *th, struct thermal_zone *tz);

/*
 * Netlink thermal samples
 */
extern int thermal_sampling_init(struct thermal_handler *th);

extern int thermal_sampling_handle(struct thermal_handler *th, void *arg);

extern int thermal_sampling_fd(struct thermal_handler *th);

/*
 * Low level netlink
 */
extern int nl_subscribe_thermal(struct nl_sock *nl_sock, struct nl_cb *nl_cb,
				const char *group);

extern int nl_thermal_connect(struct nl_sock **nl_sock, struct nl_cb **nl_cb);

extern void nl_thermal_disconnect(struct nl_sock *nl_sock, struct nl_cb *nl_cb);

extern int nl_send_msg(struct nl_sock *sock, struct nl_cb *nl_cb, struct nl_msg *msg,
		       int (*rx_handler)(struct nl_msg *, void *),
		       void *data);

#endif /* __THERMAL_H */

/*
 * The netlink notification for thermal is new and the thermal uapi
 * may be not installed yet. Provide the default values, so we can go
 * forward until the linux-headers are available.
 */
#ifndef THERMAL_GENL_VERSION

#warning "Thermal kernel/userspace API is not up-to-date, " \
	"please update the linux-headers"

/* Adding event notification support elements */
#undef THERMAL_GENL_FAMILY_NAME
#undef THERMAL_GENL_VERSION
#define THERMAL_GENL_FAMILY_NAME "thermal"
#define THERMAL_GENL_VERSION 0x01
#define THERMAL_GENL_SAMPLING_GROUP_NAME "sampling"
#define THERMAL_GENL_EVENT_GROUP_NAME "event"

/* Attributes of thermal_genl_family */
enum thermal_genl_attr {
	THERMAL_GENL_ATTR_UNSPEC,
	THERMAL_GENL_ATTR_TZ,
	THERMAL_GENL_ATTR_TZ_ID,
	THERMAL_GENL_ATTR_TZ_TEMP,
	THERMAL_GENL_ATTR_TZ_TRIP,
	THERMAL_GENL_ATTR_TZ_TRIP_ID,
	THERMAL_GENL_ATTR_TZ_TRIP_TYPE,
	THERMAL_GENL_ATTR_TZ_TRIP_TEMP,
	THERMAL_GENL_ATTR_TZ_TRIP_HYST,
	THERMAL_GENL_ATTR_TZ_MODE,
	THERMAL_GENL_ATTR_TZ_NAME,
	THERMAL_GENL_ATTR_TZ_CDEV_WEIGHT,
	THERMAL_GENL_ATTR_TZ_GOV,
	THERMAL_GENL_ATTR_TZ_GOV_NAME,
	THERMAL_GENL_ATTR_CDEV,
	THERMAL_GENL_ATTR_CDEV_ID,
	THERMAL_GENL_ATTR_CDEV_CUR_STATE,
	THERMAL_GENL_ATTR_CDEV_MAX_STATE,
	THERMAL_GENL_ATTR_CDEV_NAME,
	THERMAL_GENL_ATTR_GOV_NAME,

	__THERMAL_GENL_ATTR_MAX,
};
#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)

enum thermal_genl_sampling {
	THERMAL_GENL_SAMPLING_TEMP,
	__THERMAL_GENL_SAMPLING_MAX,
};
#define THERMAL_GENL_SAMPLING_MAX (__THERMAL_GENL_SAMPLING_MAX - 1)

/* Events of thermal_genl_family */
enum thermal_genl_event {
	THERMAL_GENL_EVENT_UNSPEC,
	THERMAL_GENL_EVENT_TZ_CREATE,/* Thermal zone creation */
	THERMAL_GENL_EVENT_TZ_DELETE,/* Thermal zone deletion */
	THERMAL_GENL_EVENT_TZ_DISABLE,/* Thermal zone disabed */
	THERMAL_GENL_EVENT_TZ_ENABLE,/* Thermal zone enabled */
	THERMAL_GENL_EVENT_TZ_TRIP_HIGH,/* Trip point crossed the way up */
	THERMAL_GENL_EVENT_TZ_TRIP_LOW,/* Trip point crossed the way down */
	THERMAL_GENL_EVENT_TZ_TRIP_CHANGE,/* Trip point changed */
	THERMAL_GENL_EVENT_TZ_TRIP_ADD,/* Trip point added */
	THERMAL_GENL_EVENT_TZ_TRIP_DELETE,/* Trip point deleted */
	THERMAL_GENL_EVENT_TZ_CDEV_ADD, /* Cdev bound to the thermal zone */
	THERMAL_GENL_EVENT_TZ_CDEV_DELETE,/* Cdev unbound */
	THERMAL_GENL_EVENT_TZ_CDEV_UPDATE,/* Cdev state updated */
	THERMAL_GENL_EVENT_TZ_GOV_CHANGE,/* Governor policy changed  */
	__THERMAL_GENL_EVENT_MAX,
};
#define THERMAL_GENL_EVENT_MAX (__THERMAL_GENL_EVENT_MAX - 1)

/* Commands supported by the thermal_genl_family */
enum thermal_genl_cmd {
	THERMAL_GENL_CMD_UNSPEC,
	THERMAL_GENL_CMD_TZ_GET,/* List thermal zones id */
	THERMAL_GENL_CMD_TZ_GET_TRIP,/* List of thermal trips */
	THERMAL_GENL_CMD_TZ_GET_TEMP,/* Get the thermal zone temperature */
	THERMAL_GENL_CMD_TZ_GET_GOV,/* Get the thermal zone governor */
	THERMAL_GENL_CMD_TZ_GET_MODE,/* Get the thermal zone mode */
	THERMAL_GENL_CMD_CDEV_GET,/* List of cdev id */
	__THERMAL_GENL_CMD_MAX,
};
#define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)

#endif