aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power
diff options
context:
space:
mode:
Diffstat (limited to 'tools/power')
-rw-r--r--tools/power/cpupower/lib/cpufreq.c72
-rw-r--r--tools/power/cpupower/lib/cpufreq.h54
-rw-r--r--tools/power/cpupower/lib/sysfs.c221
-rw-r--r--tools/power/cpupower/lib/sysfs.h34
4 files changed, 209 insertions, 172 deletions
diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
index ae7d8c57b447..d961101d1cea 100644
--- a/tools/power/cpupower/lib/cpufreq.c
+++ b/tools/power/cpupower/lib/cpufreq.c
@@ -42,21 +42,25 @@ int cpufreq_get_hardware_limits(unsigned int cpu,
42 return sysfs_get_freq_hardware_limits(cpu, min, max); 42 return sysfs_get_freq_hardware_limits(cpu, min, max);
43} 43}
44 44
45char * cpufreq_get_driver(unsigned int cpu) { 45char *cpufreq_get_driver(unsigned int cpu)
46{
46 return sysfs_get_freq_driver(cpu); 47 return sysfs_get_freq_driver(cpu);
47} 48}
48 49
49void cpufreq_put_driver(char * ptr) { 50void cpufreq_put_driver(char *ptr)
51{
50 if (!ptr) 52 if (!ptr)
51 return; 53 return;
52 free(ptr); 54 free(ptr);
53} 55}
54 56
55struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu) { 57struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu)
58{
56 return sysfs_get_freq_policy(cpu); 59 return sysfs_get_freq_policy(cpu);
57} 60}
58 61
59void cpufreq_put_policy(struct cpufreq_policy *policy) { 62void cpufreq_put_policy(struct cpufreq_policy *policy)
63{
60 if ((!policy) || (!policy->governor)) 64 if ((!policy) || (!policy->governor))
61 return; 65 return;
62 66
@@ -65,11 +69,14 @@ void cpufreq_put_policy(struct cpufreq_policy *policy) {
65 free(policy); 69 free(policy);
66} 70}
67 71
68struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu) { 72struct cpufreq_available_governors *cpufreq_get_available_governors(unsigned
73 int cpu)
74{
69 return sysfs_get_freq_available_governors(cpu); 75 return sysfs_get_freq_available_governors(cpu);
70} 76}
71 77
72void cpufreq_put_available_governors(struct cpufreq_available_governors *any) { 78void cpufreq_put_available_governors(struct cpufreq_available_governors *any)
79{
73 struct cpufreq_available_governors *tmp, *next; 80 struct cpufreq_available_governors *tmp, *next;
74 81
75 if (!any) 82 if (!any)
@@ -86,11 +93,14 @@ void cpufreq_put_available_governors(struct cpufreq_available_governors *any) {
86} 93}
87 94
88 95
89struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu) { 96struct cpufreq_available_frequencies
97*cpufreq_get_available_frequencies(unsigned int cpu)
98{
90 return sysfs_get_available_frequencies(cpu); 99 return sysfs_get_available_frequencies(cpu);
91} 100}
92 101
93void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any) { 102void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies
103 *any) {
94 struct cpufreq_available_frequencies *tmp, *next; 104 struct cpufreq_available_frequencies *tmp, *next;
95 105
96 if (!any) 106 if (!any)
@@ -105,11 +115,13 @@ void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any
105} 115}
106 116
107 117
108struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu) { 118struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned int cpu)
119{
109 return sysfs_get_freq_affected_cpus(cpu); 120 return sysfs_get_freq_affected_cpus(cpu);
110} 121}
111 122
112void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) { 123void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any)
124{
113 struct cpufreq_affected_cpus *tmp, *next; 125 struct cpufreq_affected_cpus *tmp, *next;
114 126
115 if (!any) 127 if (!any)
@@ -124,16 +136,19 @@ void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) {
124} 136}
125 137
126 138
127struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu) { 139struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned int cpu)
140{
128 return sysfs_get_freq_related_cpus(cpu); 141 return sysfs_get_freq_related_cpus(cpu);
129} 142}
130 143
131void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any) { 144void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any)
145{
132 cpufreq_put_affected_cpus(any); 146 cpufreq_put_affected_cpus(any);
133} 147}
134 148
135 149
136int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) { 150int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy)
151{
137 if (!policy || !(policy->governor)) 152 if (!policy || !(policy->governor))
138 return -EINVAL; 153 return -EINVAL;
139 154
@@ -141,35 +156,39 @@ int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) {
141} 156}
142 157
143 158
144int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq) { 159int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq)
160{
145 return sysfs_modify_freq_policy_min(cpu, min_freq); 161 return sysfs_modify_freq_policy_min(cpu, min_freq);
146} 162}
147 163
148 164
149int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq) { 165int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq)
166{
150 return sysfs_modify_freq_policy_max(cpu, max_freq); 167 return sysfs_modify_freq_policy_max(cpu, max_freq);
151} 168}
152 169
153 170
154int cpufreq_modify_policy_governor(unsigned int cpu, char *governor) { 171int cpufreq_modify_policy_governor(unsigned int cpu, char *governor)
172{
155 if ((!governor) || (strlen(governor) > 19)) 173 if ((!governor) || (strlen(governor) > 19))
156 return -EINVAL; 174 return -EINVAL;
157 175
158 return sysfs_modify_freq_policy_governor(cpu, governor); 176 return sysfs_modify_freq_policy_governor(cpu, governor);
159} 177}
160 178
161int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency) { 179int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency)
180{
162 return sysfs_set_frequency(cpu, target_frequency); 181 return sysfs_set_frequency(cpu, target_frequency);
163} 182}
164 183
165struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { 184struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
166 struct cpufreq_stats *ret; 185 unsigned long long *total_time)
167 186{
168 ret = sysfs_get_freq_stats(cpu, total_time); 187 return sysfs_get_freq_stats(cpu, total_time);
169 return (ret);
170} 188}
171 189
172void cpufreq_put_stats(struct cpufreq_stats *any) { 190void cpufreq_put_stats(struct cpufreq_stats *any)
191{
173 struct cpufreq_stats *tmp, *next; 192 struct cpufreq_stats *tmp, *next;
174 193
175 if (!any) 194 if (!any)
@@ -183,8 +202,7 @@ void cpufreq_put_stats(struct cpufreq_stats *any) {
183 } 202 }
184} 203}
185 204
186unsigned long cpufreq_get_transitions(unsigned int cpu) { 205unsigned long cpufreq_get_transitions(unsigned int cpu)
187 unsigned long ret = sysfs_get_freq_transitions(cpu); 206{
188 207 return sysfs_get_freq_transitions(cpu);
189 return (ret);
190} 208}
diff --git a/tools/power/cpupower/lib/cpufreq.h b/tools/power/cpupower/lib/cpufreq.h
index 03be906581b5..3aae8e7a0839 100644
--- a/tools/power/cpupower/lib/cpufreq.h
+++ b/tools/power/cpupower/lib/cpufreq.h
@@ -93,9 +93,9 @@ extern unsigned long cpufreq_get_transition_latency(unsigned int cpu);
93 * considerations by cpufreq policy notifiers in the kernel. 93 * considerations by cpufreq policy notifiers in the kernel.
94 */ 94 */
95 95
96extern int cpufreq_get_hardware_limits(unsigned int cpu, 96extern int cpufreq_get_hardware_limits(unsigned int cpu,
97 unsigned long *min, 97 unsigned long *min,
98 unsigned long *max); 98 unsigned long *max);
99 99
100 100
101/* determine CPUfreq driver used 101/* determine CPUfreq driver used
@@ -104,9 +104,9 @@ extern int cpufreq_get_hardware_limits(unsigned int cpu,
104 * to avoid memory leakage, please. 104 * to avoid memory leakage, please.
105 */ 105 */
106 106
107extern char * cpufreq_get_driver(unsigned int cpu); 107extern char *cpufreq_get_driver(unsigned int cpu);
108 108
109extern void cpufreq_put_driver(char * ptr); 109extern void cpufreq_put_driver(char *ptr);
110 110
111 111
112/* determine CPUfreq policy currently used 112/* determine CPUfreq policy currently used
@@ -116,7 +116,7 @@ extern void cpufreq_put_driver(char * ptr);
116 */ 116 */
117 117
118 118
119extern struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu); 119extern struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu);
120 120
121extern void cpufreq_put_policy(struct cpufreq_policy *policy); 121extern void cpufreq_put_policy(struct cpufreq_policy *policy);
122 122
@@ -129,41 +129,47 @@ extern void cpufreq_put_policy(struct cpufreq_policy *policy);
129 */ 129 */
130 130
131 131
132extern struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu); 132extern struct cpufreq_available_governors
133*cpufreq_get_available_governors(unsigned int cpu);
133 134
134extern void cpufreq_put_available_governors(struct cpufreq_available_governors *first); 135extern void cpufreq_put_available_governors(
136 struct cpufreq_available_governors *first);
135 137
136 138
137/* determine CPU frequency states available 139/* determine CPU frequency states available
138 * 140 *
139 * only present on _some_ ->target() cpufreq drivers. For information purposes 141 * Only present on _some_ ->target() cpufreq drivers. For information purposes
140 * only. Please free allocated memory by calling cpufreq_put_available_frequencies 142 * only. Please free allocated memory by calling
141 * after use. 143 * cpufreq_put_available_frequencies after use.
142 */ 144 */
143 145
144extern struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu); 146extern struct cpufreq_available_frequencies
147*cpufreq_get_available_frequencies(unsigned int cpu);
145 148
146extern void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *first); 149extern void cpufreq_put_available_frequencies(
150 struct cpufreq_available_frequencies *first);
147 151
148 152
149/* determine affected CPUs 153/* determine affected CPUs
150 * 154 *
151 * Remember to call cpufreq_put_affected_cpus when no longer needed 155 * Remember to call cpufreq_put_affected_cpus when no longer needed
152 * to avoid memory leakage, please. 156 * to avoid memory leakage, please.
153 */ 157 */
154 158
155extern struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu); 159extern struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned
160 int cpu);
156 161
157extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first); 162extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first);
158 163
159 164
160/* determine related CPUs 165/* determine related CPUs
161 * 166 *
162 * Remember to call cpufreq_put_related_cpus when no longer needed 167 * Remember to call cpufreq_put_related_cpus when no longer needed
163 * to avoid memory leakage, please. 168 * to avoid memory leakage, please.
164 */ 169 */
165 170
166extern struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu); 171extern struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned
172 int cpu);
167 173
168extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first); 174extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first);
169 175
@@ -173,15 +179,16 @@ extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first);
173 * This is not available in all kernel versions or configurations. 179 * This is not available in all kernel versions or configurations.
174 */ 180 */
175 181
176extern struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time); 182extern struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
183 unsigned long long *total_time);
177 184
178extern void cpufreq_put_stats(struct cpufreq_stats *stats); 185extern void cpufreq_put_stats(struct cpufreq_stats *stats);
179 186
180extern unsigned long cpufreq_get_transitions(unsigned int cpu); 187extern unsigned long cpufreq_get_transitions(unsigned int cpu);
181 188
182 189
183/* set new cpufreq policy 190/* set new cpufreq policy
184 * 191 *
185 * Tries to set the passed policy as new policy as close as possible, 192 * Tries to set the passed policy as new policy as close as possible,
186 * but results may differ depending e.g. on governors being available. 193 * but results may differ depending e.g. on governors being available.
187 */ 194 */
@@ -189,7 +196,7 @@ extern unsigned long cpufreq_get_transitions(unsigned int cpu);
189extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy); 196extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy);
190 197
191 198
192/* modify a policy by only changing min/max freq or governor 199/* modify a policy by only changing min/max freq or governor
193 * 200 *
194 * Does not check whether result is what was intended. 201 * Does not check whether result is what was intended.
195 */ 202 */
@@ -202,11 +209,12 @@ extern int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);
202/* set a specific frequency 209/* set a specific frequency
203 * 210 *
204 * Does only work if userspace governor can be used and no external 211 * Does only work if userspace governor can be used and no external
205 * interference (other calls to this function or to set/modify_policy) 212 * interference (other calls to this function or to set/modify_policy)
206 * occurs. Also does not work on ->range() cpufreq drivers. 213 * occurs. Also does not work on ->range() cpufreq drivers.
207 */ 214 */
208 215
209extern int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency); 216extern int cpufreq_set_frequency(unsigned int cpu,
217 unsigned long target_frequency);
210 218
211#ifdef __cplusplus 219#ifdef __cplusplus
212} 220}
diff --git a/tools/power/cpupower/lib/sysfs.c b/tools/power/cpupower/lib/sysfs.c
index c9b061fe87b1..9a35456ba6b2 100644
--- a/tools/power/cpupower/lib/sysfs.c
+++ b/tools/power/cpupower/lib/sysfs.c
@@ -24,14 +24,14 @@
24static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) 24static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
25{ 25{
26 int fd; 26 int fd;
27 size_t numread; 27 ssize_t numread;
28 28
29 if ( ( fd = open(path, O_RDONLY) ) == -1 ) 29 fd = open(path, O_RDONLY);
30 if (fd == -1)
30 return 0; 31 return 0;
31 32
32 numread = read(fd, buf, buflen - 1); 33 numread = read(fd, buf, buflen - 1);
33 if ( numread < 1 ) 34 if (numread < 1) {
34 {
35 close(fd); 35 close(fd);
36 return 0; 36 return 0;
37 } 37 }
@@ -39,7 +39,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
39 buf[numread] = '\0'; 39 buf[numread] = '\0';
40 close(fd); 40 close(fd);
41 41
42 return numread; 42 return (unsigned int) numread;
43} 43}
44 44
45 45
@@ -65,24 +65,24 @@ static unsigned int sysfs_cpufreq_write_file(unsigned int cpu,
65{ 65{
66 char path[SYSFS_PATH_MAX]; 66 char path[SYSFS_PATH_MAX];
67 int fd; 67 int fd;
68 size_t numwrite; 68 ssize_t numwrite;
69 69
70 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", 70 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s",
71 cpu, fname); 71 cpu, fname);
72 72
73 if ( ( fd = open(path, O_WRONLY) ) == -1 ) 73 fd = open(path, O_WRONLY);
74 if (fd == -1)
74 return 0; 75 return 0;
75 76
76 numwrite = write(fd, value, len); 77 numwrite = write(fd, value, len);
77 if ( numwrite < 1 ) 78 if (numwrite < 1) {
78 {
79 close(fd); 79 close(fd);
80 return 0; 80 return 0;
81 } 81 }
82 82
83 close(fd); 83 close(fd);
84 84
85 return numwrite; 85 return (unsigned int) numwrite;
86} 86}
87 87
88/* read access to files which contain one numeric value */ 88/* read access to files which contain one numeric value */
@@ -114,21 +114,23 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
114static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu, 114static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
115 enum cpufreq_value which) 115 enum cpufreq_value which)
116{ 116{
117 unsigned long value; 117 unsigned long value;
118 unsigned int len; 118 unsigned int len;
119 char linebuf[MAX_LINE_LEN]; 119 char linebuf[MAX_LINE_LEN];
120 char *endp; 120 char *endp;
121 121
122 if ( which >= MAX_CPUFREQ_VALUE_READ_FILES ) 122 if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
123 return 0; 123 return 0;
124 124
125 if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], 125 len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
126 linebuf, sizeof(linebuf))) == 0 ) 126 linebuf, sizeof(linebuf));
127
128 if (len == 0)
127 return 0; 129 return 0;
128 130
129 value = strtoul(linebuf, &endp, 0); 131 value = strtoul(linebuf, &endp, 0);
130 132
131 if ( endp == linebuf || errno == ERANGE ) 133 if (endp == linebuf || errno == ERANGE)
132 return 0; 134 return 0;
133 135
134 return value; 136 return value;
@@ -148,7 +150,7 @@ static const char *cpufreq_string_files[MAX_CPUFREQ_STRING_FILES] = {
148}; 150};
149 151
150 152
151static char * sysfs_cpufreq_get_one_string(unsigned int cpu, 153static char *sysfs_cpufreq_get_one_string(unsigned int cpu,
152 enum cpufreq_string which) 154 enum cpufreq_string which)
153{ 155{
154 char linebuf[MAX_LINE_LEN]; 156 char linebuf[MAX_LINE_LEN];
@@ -158,11 +160,13 @@ static char * sysfs_cpufreq_get_one_string(unsigned int cpu,
158 if (which >= MAX_CPUFREQ_STRING_FILES) 160 if (which >= MAX_CPUFREQ_STRING_FILES)
159 return NULL; 161 return NULL;
160 162
161 if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], 163 len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which],
162 linebuf, sizeof(linebuf))) == 0 ) 164 linebuf, sizeof(linebuf));
165 if (len == 0)
163 return NULL; 166 return NULL;
164 167
165 if ( ( result = strdup(linebuf) ) == NULL ) 168 result = strdup(linebuf);
169 if (result == NULL)
166 return NULL; 170 return NULL;
167 171
168 if (result[strlen(result) - 1] == '\n') 172 if (result[strlen(result) - 1] == '\n')
@@ -195,8 +199,8 @@ static int sysfs_cpufreq_write_one_value(unsigned int cpu,
195 if (which >= MAX_CPUFREQ_WRITE_FILES) 199 if (which >= MAX_CPUFREQ_WRITE_FILES)
196 return 0; 200 return 0;
197 201
198 if ( sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which], 202 if (sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which],
199 new_value, len) != len ) 203 new_value, len) != len)
200 return -ENODEV; 204 return -ENODEV;
201 205
202 return 0; 206 return 0;
@@ -235,11 +239,13 @@ int sysfs_get_freq_hardware_limits(unsigned int cpu,
235 return 0; 239 return 0;
236} 240}
237 241
238char * sysfs_get_freq_driver(unsigned int cpu) { 242char *sysfs_get_freq_driver(unsigned int cpu)
243{
239 return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER); 244 return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER);
240} 245}
241 246
242struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu) { 247struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu)
248{
243 struct cpufreq_policy *policy; 249 struct cpufreq_policy *policy;
244 250
245 policy = malloc(sizeof(struct cpufreq_policy)); 251 policy = malloc(sizeof(struct cpufreq_policy));
@@ -270,27 +276,24 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
270 unsigned int pos, i; 276 unsigned int pos, i;
271 unsigned int len; 277 unsigned int len;
272 278
273 if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", 279 len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors",
274 linebuf, sizeof(linebuf))) == 0 ) 280 linebuf, sizeof(linebuf));
275 { 281 if (len == 0)
276 return NULL; 282 return NULL;
277 }
278 283
279 pos = 0; 284 pos = 0;
280 for ( i = 0; i < len; i++ ) 285 for (i = 0; i < len; i++) {
281 { 286 if (linebuf[i] == ' ' || linebuf[i] == '\n') {
282 if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) 287 if (i - pos < 2)
283 {
284 if ( i - pos < 2 )
285 continue; 288 continue;
286 if ( current ) { 289 if (current) {
287 current->next = malloc(sizeof *current ); 290 current->next = malloc(sizeof(*current));
288 if ( ! current->next ) 291 if (!current->next)
289 goto error_out; 292 goto error_out;
290 current = current->next; 293 current = current->next;
291 } else { 294 } else {
292 first = malloc( sizeof *first ); 295 first = malloc(sizeof(*first));
293 if ( ! first ) 296 if (!first)
294 goto error_out; 297 goto error_out;
295 current = first; 298 current = first;
296 } 299 }
@@ -298,10 +301,10 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
298 current->next = NULL; 301 current->next = NULL;
299 302
300 current->governor = malloc(i - pos + 1); 303 current->governor = malloc(i - pos + 1);
301 if ( ! current->governor ) 304 if (!current->governor)
302 goto error_out; 305 goto error_out;
303 306
304 memcpy( current->governor, linebuf + pos, i - pos); 307 memcpy(current->governor, linebuf + pos, i - pos);
305 current->governor[i - pos] = '\0'; 308 current->governor[i - pos] = '\0';
306 pos = i + 1; 309 pos = i + 1;
307 } 310 }
@@ -310,11 +313,11 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
310 return first; 313 return first;
311 314
312 error_out: 315 error_out:
313 while ( first ) { 316 while (first) {
314 current = first->next; 317 current = first->next;
315 if ( first->governor ) 318 if (first->governor)
316 free( first->governor ); 319 free(first->governor);
317 free( first ); 320 free(first);
318 first = current; 321 first = current;
319 } 322 }
320 return NULL; 323 return NULL;
@@ -330,30 +333,26 @@ sysfs_get_available_frequencies(unsigned int cpu) {
330 unsigned int pos, i; 333 unsigned int pos, i;
331 unsigned int len; 334 unsigned int len;
332 335
333 if ( ( len = sysfs_cpufreq_read_file(cpu, 336 len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies",
334 "scaling_available_frequencies", 337 linebuf, sizeof(linebuf));
335 linebuf, sizeof(linebuf))) == 0 ) 338 if (len == 0)
336 {
337 return NULL; 339 return NULL;
338 }
339 340
340 pos = 0; 341 pos = 0;
341 for ( i = 0; i < len; i++ ) 342 for (i = 0; i < len; i++) {
342 { 343 if (linebuf[i] == ' ' || linebuf[i] == '\n') {
343 if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) 344 if (i - pos < 2)
344 {
345 if ( i - pos < 2 )
346 continue; 345 continue;
347 if ( i - pos >= SYSFS_PATH_MAX ) 346 if (i - pos >= SYSFS_PATH_MAX)
348 goto error_out; 347 goto error_out;
349 if ( current ) { 348 if (current) {
350 current->next = malloc(sizeof *current ); 349 current->next = malloc(sizeof(*current));
351 if ( ! current->next ) 350 if (!current->next)
352 goto error_out; 351 goto error_out;
353 current = current->next; 352 current = current->next;
354 } else { 353 } else {
355 first = malloc(sizeof *first ); 354 first = malloc(sizeof(*first));
356 if ( ! first ) 355 if (!first)
357 goto error_out; 356 goto error_out;
358 current = first; 357 current = first;
359 } 358 }
@@ -362,7 +361,7 @@ sysfs_get_available_frequencies(unsigned int cpu) {
362 361
363 memcpy(one_value, linebuf + pos, i - pos); 362 memcpy(one_value, linebuf + pos, i - pos);
364 one_value[i - pos] = '\0'; 363 one_value[i - pos] = '\0';
365 if ( sscanf(one_value, "%lu", &current->frequency) != 1 ) 364 if (sscanf(one_value, "%lu", &current->frequency) != 1)
366 goto error_out; 365 goto error_out;
367 366
368 pos = i + 1; 367 pos = i + 1;
@@ -372,7 +371,7 @@ sysfs_get_available_frequencies(unsigned int cpu) {
372 return first; 371 return first;
373 372
374 error_out: 373 error_out:
375 while ( first ) { 374 while (first) {
376 current = first->next; 375 current = first->next;
377 free(first); 376 free(first);
378 first = current; 377 first = current;
@@ -380,8 +379,9 @@ sysfs_get_available_frequencies(unsigned int cpu) {
380 return NULL; 379 return NULL;
381} 380}
382 381
383static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, 382static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu,
384 const char *file) { 383 const char *file)
384{
385 struct cpufreq_affected_cpus *first = NULL; 385 struct cpufreq_affected_cpus *first = NULL;
386 struct cpufreq_affected_cpus *current = NULL; 386 struct cpufreq_affected_cpus *current = NULL;
387 char one_value[SYSFS_PATH_MAX]; 387 char one_value[SYSFS_PATH_MAX];
@@ -389,29 +389,25 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu,
389 unsigned int pos, i; 389 unsigned int pos, i;
390 unsigned int len; 390 unsigned int len;
391 391
392 if ( ( len = sysfs_cpufreq_read_file(cpu, file, linebuf, 392 len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf));
393 sizeof(linebuf))) == 0 ) 393 if (len == 0)
394 {
395 return NULL; 394 return NULL;
396 }
397 395
398 pos = 0; 396 pos = 0;
399 for ( i = 0; i < len; i++ ) 397 for (i = 0; i < len; i++) {
400 { 398 if (i == len || linebuf[i] == ' ' || linebuf[i] == '\n') {
401 if ( i == len || linebuf[i] == ' ' || linebuf[i] == '\n' ) 399 if (i - pos < 1)
402 {
403 if ( i - pos < 1 )
404 continue; 400 continue;
405 if ( i - pos >= SYSFS_PATH_MAX ) 401 if (i - pos >= SYSFS_PATH_MAX)
406 goto error_out; 402 goto error_out;
407 if ( current ) { 403 if (current) {
408 current->next = malloc(sizeof *current); 404 current->next = malloc(sizeof(*current));
409 if ( ! current->next ) 405 if (!current->next)
410 goto error_out; 406 goto error_out;
411 current = current->next; 407 current = current->next;
412 } else { 408 } else {
413 first = malloc(sizeof *first); 409 first = malloc(sizeof(*first));
414 if ( ! first ) 410 if (!first)
415 goto error_out; 411 goto error_out;
416 current = first; 412 current = first;
417 } 413 }
@@ -421,7 +417,7 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu,
421 memcpy(one_value, linebuf + pos, i - pos); 417 memcpy(one_value, linebuf + pos, i - pos);
422 one_value[i - pos] = '\0'; 418 one_value[i - pos] = '\0';
423 419
424 if ( sscanf(one_value, "%u", &current->cpu) != 1 ) 420 if (sscanf(one_value, "%u", &current->cpu) != 1)
425 goto error_out; 421 goto error_out;
426 422
427 pos = i + 1; 423 pos = i + 1;
@@ -439,15 +435,18 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu,
439 return NULL; 435 return NULL;
440} 436}
441 437
442struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu) { 438struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(unsigned int cpu)
439{
443 return sysfs_get_cpu_list(cpu, "affected_cpus"); 440 return sysfs_get_cpu_list(cpu, "affected_cpus");
444} 441}
445 442
446struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu) { 443struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(unsigned int cpu)
444{
447 return sysfs_get_cpu_list(cpu, "related_cpus"); 445 return sysfs_get_cpu_list(cpu, "related_cpus");
448} 446}
449 447
450struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time) { 448struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
449 unsigned long long *total_time) {
451 struct cpufreq_stats *first = NULL; 450 struct cpufreq_stats *first = NULL;
452 struct cpufreq_stats *current = NULL; 451 struct cpufreq_stats *current = NULL;
453 char one_value[SYSFS_PATH_MAX]; 452 char one_value[SYSFS_PATH_MAX];
@@ -455,28 +454,27 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long
455 unsigned int pos, i; 454 unsigned int pos, i;
456 unsigned int len; 455 unsigned int len;
457 456
458 if ( ( len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", 457 len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state",
459 linebuf, sizeof(linebuf))) == 0 ) 458 linebuf, sizeof(linebuf));
459 if (len == 0)
460 return NULL; 460 return NULL;
461 461
462 *total_time = 0; 462 *total_time = 0;
463 pos = 0; 463 pos = 0;
464 for ( i = 0; i < len; i++ ) 464 for (i = 0; i < len; i++) {
465 { 465 if (i == strlen(linebuf) || linebuf[i] == '\n') {
466 if ( i == strlen(linebuf) || linebuf[i] == '\n' ) 466 if (i - pos < 2)
467 {
468 if ( i - pos < 2 )
469 continue; 467 continue;
470 if ( (i - pos) >= SYSFS_PATH_MAX ) 468 if ((i - pos) >= SYSFS_PATH_MAX)
471 goto error_out; 469 goto error_out;
472 if ( current ) { 470 if (current) {
473 current->next = malloc(sizeof *current ); 471 current->next = malloc(sizeof(*current));
474 if ( ! current->next ) 472 if (!current->next)
475 goto error_out; 473 goto error_out;
476 current = current->next; 474 current = current->next;
477 } else { 475 } else {
478 first = malloc(sizeof *first ); 476 first = malloc(sizeof(*first));
479 if ( ! first ) 477 if (!first)
480 goto error_out; 478 goto error_out;
481 current = first; 479 current = first;
482 } 480 }
@@ -485,7 +483,9 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long
485 483
486 memcpy(one_value, linebuf + pos, i - pos); 484 memcpy(one_value, linebuf + pos, i - pos);
487 one_value[i - pos] = '\0'; 485 one_value[i - pos] = '\0';
488 if ( sscanf(one_value, "%lu %llu", &current->frequency, &current->time_in_state) != 2 ) 486 if (sscanf(one_value, "%lu %llu",
487 &current->frequency,
488 &current->time_in_state) != 2)
489 goto error_out; 489 goto error_out;
490 490
491 *total_time = *total_time + current->time_in_state; 491 *total_time = *total_time + current->time_in_state;
@@ -496,7 +496,7 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long
496 return first; 496 return first;
497 497
498 error_out: 498 error_out:
499 while ( first ) { 499 while (first) {
500 current = first->next; 500 current = first->next;
501 free(first); 501 free(first);
502 first = current; 502 first = current;
@@ -511,29 +511,29 @@ unsigned long sysfs_get_freq_transitions(unsigned int cpu)
511 511
512static int verify_gov(char *new_gov, char *passed_gov) 512static int verify_gov(char *new_gov, char *passed_gov)
513{ 513{
514 unsigned int i, j=0; 514 unsigned int i, j = 0;
515 515
516 if (!passed_gov || (strlen(passed_gov) > 19)) 516 if (!passed_gov || (strlen(passed_gov) > 19))
517 return -EINVAL; 517 return -EINVAL;
518 518
519 strncpy(new_gov, passed_gov, 20); 519 strncpy(new_gov, passed_gov, 20);
520 for (i=0;i<20;i++) { 520 for (i = 0; i < 20; i++) {
521 if (j) { 521 if (j) {
522 new_gov[i] = '\0'; 522 new_gov[i] = '\0';
523 continue; 523 continue;
524 } 524 }
525 if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) { 525 if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z'))
526 continue; 526 continue;
527 } 527
528 if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) { 528 if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z'))
529 continue; 529 continue;
530 } 530
531 if (new_gov[i] == '-') { 531 if (new_gov[i] == '-')
532 continue; 532 continue;
533 } 533
534 if (new_gov[i] == '_') { 534 if (new_gov[i] == '_')
535 continue; 535 continue;
536 } 536
537 if (new_gov[i] == '\0') { 537 if (new_gov[i] == '\0') {
538 j = 1; 538 j = 1;
539 continue; 539 continue;
@@ -627,7 +627,8 @@ int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy)
627 gov, strlen(gov)); 627 gov, strlen(gov));
628} 628}
629 629
630int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) { 630int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency)
631{
631 struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu); 632 struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu);
632 char userspace_gov[] = "userspace"; 633 char userspace_gov[] = "userspace";
633 char freq[SYSFS_PATH_MAX]; 634 char freq[SYSFS_PATH_MAX];
@@ -640,7 +641,7 @@ int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) {
640 ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov); 641 ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov);
641 if (ret) { 642 if (ret) {
642 cpufreq_put_policy(pol); 643 cpufreq_put_policy(pol);
643 return (ret); 644 return ret;
644 } 645 }
645 } 646 }
646 647
@@ -662,7 +663,7 @@ int sysfs_cpu_exists(unsigned int cpu)
662 663
663 snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu); 664 snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu);
664 665
665 if ( stat(file, &statbuf) != 0 ) 666 if (stat(file, &statbuf) != 0)
666 return -ENOSYS; 667 return -ENOSYS;
667 668
668 return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS; 669 return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS;
diff --git a/tools/power/cpupower/lib/sysfs.h b/tools/power/cpupower/lib/sysfs.h
index c29e5575be8a..c76a5e0af501 100644
--- a/tools/power/cpupower/lib/sysfs.h
+++ b/tools/power/cpupower/lib/sysfs.h
@@ -5,17 +5,27 @@ extern unsigned int sysfs_cpu_exists(unsigned int cpu);
5extern unsigned long sysfs_get_freq_kernel(unsigned int cpu); 5extern unsigned long sysfs_get_freq_kernel(unsigned int cpu);
6extern unsigned long sysfs_get_freq_hardware(unsigned int cpu); 6extern unsigned long sysfs_get_freq_hardware(unsigned int cpu);
7extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu); 7extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu);
8extern int sysfs_get_freq_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max); 8extern int sysfs_get_freq_hardware_limits(unsigned int cpu,
9extern char * sysfs_get_freq_driver(unsigned int cpu); 9 unsigned long *min, unsigned long *max);
10extern struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu); 10extern char *sysfs_get_freq_driver(unsigned int cpu);
11extern struct cpufreq_available_governors * sysfs_get_freq_available_governors(unsigned int cpu); 11extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu);
12extern struct cpufreq_available_frequencies * sysfs_get_available_frequencies(unsigned int cpu); 12extern struct cpufreq_available_governors *sysfs_get_freq_available_governors(
13extern struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu); 13 unsigned int cpu);
14extern struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu); 14extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies(
15extern struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time); 15 unsigned int cpu);
16extern struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(
17 unsigned int cpu);
18extern struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(
19 unsigned int cpu);
20extern struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
21 unsigned long long *total_time);
16extern unsigned long sysfs_get_freq_transitions(unsigned int cpu); 22extern unsigned long sysfs_get_freq_transitions(unsigned int cpu);
17extern int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 23extern int sysfs_set_freq_policy(unsigned int cpu,
18extern int sysfs_modify_freq_policy_min(unsigned int cpu, unsigned long min_freq); 24 struct cpufreq_policy *policy);
19extern int sysfs_modify_freq_policy_max(unsigned int cpu, unsigned long max_freq); 25extern int sysfs_modify_freq_policy_min(unsigned int cpu,
26 unsigned long min_freq);
27extern int sysfs_modify_freq_policy_max(unsigned int cpu,
28 unsigned long max_freq);
20extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor); 29extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor);
21extern int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency); 30extern int sysfs_set_frequency(unsigned int cpu,
31 unsigned long target_frequency);