File: cpufreq.c

package info (click to toggle)
cpufrequtils 007-1%2Bsqueeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 608 kB
  • ctags: 433
  • sloc: ansic: 3,421; sh: 393; makefile: 289
file content (255 lines) | stat: -rw-r--r-- 5,265 bytes parent folder | download | duplicates (4)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 *  (C) 2004-2009  Dominik Brodowski <linux@dominikbrodowski.de>
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 */


#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "cpufreq.h"
#include "interfaces.h"

int cpufreq_cpu_exists(unsigned int cpu)
{
	int ret = sysfs_cpu_exists(cpu);
	if (ret == -ENOSYS)
		ret = proc_cpu_exists(cpu);
	return (ret);
}

unsigned long cpufreq_get_freq_kernel(unsigned int cpu)
{
	unsigned long ret = sysfs_get_freq_kernel(cpu);
	if (!ret)
		ret = proc_get_freq_kernel(cpu);
	return (ret);
}

unsigned long cpufreq_get_freq_hardware(unsigned int cpu)
{
	unsigned long ret = sysfs_get_freq_hardware(cpu);
	if (!ret)
		ret = proc_get_freq_hardware(cpu);
	return (ret);
}

unsigned long cpufreq_get_transition_latency(unsigned int cpu)
{
	unsigned long ret = sysfs_get_transition_latency(cpu);
	if (!ret)
		ret = proc_get_transition_latency(cpu);
	return (ret);
}

int cpufreq_get_hardware_limits(unsigned int cpu,
				unsigned long *min,
				unsigned long *max)
{
	int ret;
	if ((!min) || (!max))
		return -EINVAL;
	ret = sysfs_get_hardware_limits(cpu, min, max);
	if (ret)
		ret = proc_get_hardware_limits(cpu, min, max);
	return (ret);
}

char * cpufreq_get_driver(unsigned int cpu) {
	char * ret;
	ret = sysfs_get_driver(cpu);
	if (!ret) {
		ret = proc_get_driver(cpu);
	}
	return (ret);
}

void cpufreq_put_driver(char * ptr) {
	if (!ptr)
		return;
	free(ptr);
}

struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu) {
	struct cpufreq_policy * ret;
	ret = sysfs_get_policy(cpu);
	if (!ret)
		ret = proc_get_policy(cpu);
	return (ret);
}

void cpufreq_put_policy(struct cpufreq_policy *policy) {
	if ((!policy) || (!policy->governor))
		return;

	free(policy->governor);
	policy->governor = NULL;
	free(policy);
}

struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu) {
	struct cpufreq_available_governors *ret;
	ret = sysfs_get_available_governors(cpu);
	if (!ret)
		ret = proc_get_available_governors(cpu);
	return (ret);
}

void cpufreq_put_available_governors(struct cpufreq_available_governors *any) {
	struct cpufreq_available_governors *tmp, *next;

	if (!any)
		return;

	tmp = any->first;
	while (tmp) {
		next = tmp->next;
		if (tmp->governor)
			free(tmp->governor);
		free(tmp);
		tmp = next;
	}
}


struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu) {
	struct cpufreq_available_frequencies * ret;
	ret = sysfs_get_available_frequencies(cpu);
	if (!ret)
		ret = proc_get_available_frequencies(cpu);
	return (ret);
}

void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any) {
	struct cpufreq_available_frequencies *tmp, *next;

	if (!any)
		return;

	tmp = any->first;
	while (tmp) {
		next = tmp->next;
		free(tmp);
		tmp = next;
	}
}


struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu) {
	struct cpufreq_affected_cpus * ret;
	ret = sysfs_get_affected_cpus(cpu);
	if (!ret)
		ret = proc_get_affected_cpus(cpu);
	return (ret);
}

void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) {
	struct cpufreq_affected_cpus *tmp, *next;

	if (!any)
		return;

	tmp = any->first;
	while (tmp) {
		next = tmp->next;
		free(tmp);
		tmp = next;
	}
}


struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu) {
	struct cpufreq_affected_cpus * ret;
	ret = sysfs_get_related_cpus(cpu);
	if (!ret)
		ret = proc_get_related_cpus(cpu);
	return (ret);
}

void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any) {
	cpufreq_put_affected_cpus(any);
}


int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) {
	int ret;
	if (!policy || !(policy->governor))
		return -EINVAL;

	ret = sysfs_set_policy(cpu, policy);
	if (ret)
		ret = proc_set_policy(cpu, policy);
	return (ret);
}


int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq) {
	int ret;

	ret = sysfs_modify_policy_min(cpu, min_freq);
	if (ret)
		ret = proc_modify_policy_min(cpu, min_freq);
	return (ret);
}


int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq) {
	int ret;

	ret = sysfs_modify_policy_max(cpu, max_freq);
	if (ret)
		ret = proc_modify_policy_max(cpu, max_freq);
	return (ret);
}


int cpufreq_modify_policy_governor(unsigned int cpu, char *governor) {
	int ret;

	if ((!governor) || (strlen(governor) > 19))
		return -EINVAL;

	ret = sysfs_modify_policy_governor(cpu, governor);
	if (ret)
		ret = proc_modify_policy_governor(cpu, governor);
	return (ret);
}

int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency) {
	int ret;

	ret = sysfs_set_frequency(cpu, target_frequency);
	if (ret)
		ret = proc_set_frequency(cpu, target_frequency);
	return (ret);
}

struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) {
	struct cpufreq_stats *ret;

	ret = sysfs_get_stats(cpu, total_time);
	return (ret);
}

void cpufreq_put_stats(struct cpufreq_stats *any) {
	struct cpufreq_stats *tmp, *next;

	if (!any)
		return;

	tmp = any->first;
	while (tmp) {
		next = tmp->next;
		free(tmp);
		tmp = next;
	}
}

unsigned long cpufreq_get_transitions(unsigned int cpu) {
	unsigned long ret = sysfs_get_transitions(cpu);

	return (ret);
}