blob: aad83490dd16715cac26e1a8e49b09a1b83b2a8e [file] [log] [blame]
Amit Arorae9e16b02010-08-03 10:15:20 +05301#include <getopt.h>
2
3#include "powerdebug.h"
4
5
6int numregulators;
7
8int init_regulator_ds(void)
9{
10 DIR *regdir;
11 struct dirent *item;
12
13 regdir = opendir("/sys/class/regulator");
14 if (!regdir)
15 return(1);
16 while((item = readdir(regdir))) {
17 if (strncmp(item->d_name, "regulator", 9))
18 continue;
19
20 numregulators++;
21 }
22 closedir(regdir);
23
24 regulators_info = (struct regulator_info *)malloc(numregulators*
25 sizeof(struct regulator_info));
26 if (!regulators_info) {
Amit Arora85fd4952010-08-05 14:04:50 +053027 fprintf(stderr, "init_regulator_ds: Not enough memory to ");
28 fprintf(stderr, "read information for %d regulators!\n",
Amit Arorae9e16b02010-08-03 10:15:20 +053029 numregulators);
30 return(1);
31 }
32
33 return(0);
34}
35
36int read_and_print_sensor_info(int verbose)
37{
38 DIR *dir, *subdir;
Amit Arora1c037892010-08-05 13:46:10 +053039 int len, found = 0;
Amit Arorae9e16b02010-08-03 10:15:20 +053040 char filename[PATH_MAX], devpath[PATH_MAX];
41 char device[PATH_MAX];
42 struct dirent *item, *subitem;
43
44/*
45 1. readdir /sys/class/hwmon
46 2. foreach subdir in above, do following
47 a) check if its virtual or physical (check for device)
48 b) for each *type* (in, temp, pwm, fan, curr, power, energy)
49 i> search for "_" (strrchr... check for last)
50 ii> check the *item* depending on type
51 i.e for "in" type items=min, max, input, label
52 for "fan" type items=min,max,input,div,target,label
53 for "pwm" type items=enable, mode, freq
54 for "temp" type items=type, max, min,input,crit,offset,label,lowest,highest,
55 for "curr" type items=max,min,input
56 for "power" type items=average,average_interval,average_min/max, average_highest/lowest, input, input_highest/lowest, accuracy,alarm, cap, cap_min/max
57 for "energy" type items=input
58 for "intrusion" type items=alarm,beep
59*/
60
61 sprintf(filename, "%s", "/sys/class/hwmon");
62
63 dir = opendir(filename);
64 if (!dir)
65 return errno;
66
67 while ((item = readdir(dir))) {
68 if (item->d_name[0] == '.') /* skip the hidden files */
69 continue;
70
Amit Arora1c037892010-08-05 13:46:10 +053071 found = 1;
72
Amit Arorae9e16b02010-08-03 10:15:20 +053073 sprintf(filename, "/sys/class/hwmon/%s", item->d_name);
74 sprintf(devpath, "%s/device", filename);
75
76 len = readlink(devpath, device, PATH_MAX - 1);
77
78 if (len < 0)
79 strcpy(devpath, filename);
80 else
81 device[len] = '\0';
82
83 subdir = opendir(devpath);
84
85 printf("\nSensor Information for %s :\n", item->d_name);
86 fflush(stdin);
87 while ((subitem = readdir(subdir))) {
88 if (subitem->d_name[0] == '.') /* skip hidden files */
89 continue;
90
91 if(!strncmp(subitem->d_name, "in", 2))
92 get_sensor_info(devpath, subitem->d_name, "in",
93 verbose);
94 else if (!strncmp(subitem->d_name, "temp", 4))
95 get_sensor_info(devpath, subitem->d_name,
96 "temp", verbose);
97 else if (!strncmp(subitem->d_name, "fan", 4))
98 get_sensor_info(devpath, subitem->d_name,
99 "fan", verbose);
100 else if (!strncmp(subitem->d_name, "pwm", 4))
101 get_sensor_info(devpath, subitem->d_name,
102 "pwm", verbose);
103
104 }
105
106 closedir(subdir);
107 }
108 closedir(dir);
109
Amit Arora1c037892010-08-05 13:46:10 +0530110 if(!found && verbose) {
111 printf("Could not find sensor information!");
112 printf(" Looks like /sys/class/hwmon is empty.\n");
113 }
114
Amit Arorae9e16b02010-08-03 10:15:20 +0530115 return 0;
116}
117
118
119int read_regulator_info(int verbose)
120{
121 FILE *file = NULL;
122 DIR *regdir, *dir;
123 int len, count = 0, ret = 0;
124 char line[1024], filename[1024], *fptr;
125 struct dirent *item, *ritem;
126
127 regdir = opendir("/sys/class/regulator");
128 if (!regdir)
129 return(1);
130 while((item = readdir(regdir))) {
131 if (strlen(item->d_name) < 3)
132 continue;
133
134 if (strncmp(item->d_name, "regulator", 9))
135 continue;
136
Amit Arora85fd4952010-08-05 14:04:50 +0530137 len = sprintf(filename, "/sys/class/regulator/%s",
138 item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530139
140 dir = opendir(filename);
Amit Arora85fd4952010-08-05 14:04:50 +0530141 if (!dir)
Amit Arora7ef178c2010-08-03 15:57:21 +0530142 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530143
144 count++;
145 if (count > numregulators) {
146 ret = 1;
Amit Arora85fd4952010-08-05 14:04:50 +0530147 goto exit;
Amit Arorae9e16b02010-08-03 10:15:20 +0530148 }
149
Amit Arorafccac912010-08-03 16:15:09 +0530150 strcpy(regulators_info[count-1].name, item->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530151 while((ritem = readdir(dir))) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530152 if (strlen(ritem->d_name) < 3)
153 continue;
154
155 sprintf(filename + len, "/%s", ritem->d_name);
Amit Arorae9e16b02010-08-03 10:15:20 +0530156
157 file = fopen(filename, "r");
158 if (!file)
159 continue;
160
161 memset(line, 0, 1024);
162 fptr = fgets(line, 1024, file);
163 fclose(file);
Amit Arora85fd4952010-08-05 14:04:50 +0530164 if (!fptr)
Amit Arora7ef178c2010-08-03 15:57:21 +0530165 continue;
Amit Arorae9e16b02010-08-03 10:15:20 +0530166
167 if (!strcmp(ritem->d_name, "name"))
168 strcpy(regulators_info[count-1].name, fptr);
169 if (!strcmp(ritem->d_name, "state"))
170 strcpy(regulators_info[count-1].state, fptr);
171 if (!strcmp(ritem->d_name, "status"))
172 strcpy(regulators_info[count-1].status, fptr);
173
174 /* Read following _only_ if verbose option specified */
175 if(!verbose)
176 continue;
177
178 if (!strcmp(ritem->d_name, "type"))
179 strcpy(regulators_info[count-1].type, fptr);
180 if (!strcmp(ritem->d_name, "opmode"))
181 strcpy(regulators_info[count-1].opmode, fptr);
182
183 if (!strcmp(ritem->d_name, "microvolts"))
184 regulators_info[count-1].microvolts = atoi(fptr);
Amit Arora83faf0e2010-08-05 12:27:42 +0530185 if (!strcmp(ritem->d_name, "min_microvolts"))
186 regulators_info[count-1].min_microvolts = atoi(fptr);
187 if (!strcmp(ritem->d_name, "max_microvolts"))
188 regulators_info[count-1].max_microvolts = atoi(fptr);
189
Amit Arorae9e16b02010-08-03 10:15:20 +0530190 if (!strcmp(ritem->d_name, "microamps"))
191 regulators_info[count-1].microamps = atoi(fptr);
Amit Arora83faf0e2010-08-05 12:27:42 +0530192 if (!strcmp(ritem->d_name, "min_microamps"))
193 regulators_info[count-1].min_microamps = atoi(fptr);
194 if (!strcmp(ritem->d_name, "max_microamps"))
195 regulators_info[count-1].max_microamps = atoi(fptr);
196 if (!strcmp(ritem->d_name, "requested_microamps"))
197 regulators_info[count-1].requested_microamps = atoi(fptr);
198
Amit Arorae9e16b02010-08-03 10:15:20 +0530199 if (!strcmp(ritem->d_name, "num_users"))
200 regulators_info[count-1].num_users = atoi(fptr);
Amit Arorae9e16b02010-08-03 10:15:20 +0530201 }
Amit Arora85fd4952010-08-05 14:04:50 +0530202exit:
Amit Arorae9e16b02010-08-03 10:15:20 +0530203 closedir(dir);
204 if (ret)
205 break;
206 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530207 closedir(regdir);
208
209 return ret;
210}
211
212
213int main(int argc, char **argv)
214{
215 int c;
216 int regulators = 0, sensors = 0, verbose = 0;
217
Amit Arorafefe8bf2010-08-05 13:31:20 +0530218 /*
219 * Options:
220 * -r, --regulator : regulator
221 * -s, --sensor : sensors
222 * -v, --verbose : verbose
223 * -V, --version : version
224 * -h, --help : help
225 * no option / default : show usage!
Amit Arorae9e16b02010-08-03 10:15:20 +0530226 */
227
Amit Arorafefe8bf2010-08-05 13:31:20 +0530228 while (1) {
229 int optindex = 0;
230 static struct option long_options[] = {
231 {"regulator", 0, 0, 'r'},
232 {"sensor", 0, 0, 's'},
233 {"verbose", 0, 0, 'v'},
234 {"version", 0, 0, 'V'},
235 {"help", 0, 0, 'h'},
236 {0, 0, 0, 0}
237 };
238
239 c = getopt_long(argc, argv, "rsvVh", long_options, &optindex);
240 if (c == -1)
241 break;
242
243 switch (c) {
244 case 'r':
245 regulators = 1;
246 break;
247 case 's':
248 sensors = 1;
249 break;
250 case 'v':
251 verbose = 1;
252 break;
253 case 'V':
254 version();
255 break;
256 case 'h':
257 usage(argv);
258 break;
259 case '?':
260 fprintf (stderr, "%s: Unknown option %c'.\n",
261 argv[0], optopt);
262 exit(1);
263 default:
264 usage(argv);
265 break;
266 }
Amit Arorae9e16b02010-08-03 10:15:20 +0530267 }
268
Amit Arorafefe8bf2010-08-05 13:31:20 +0530269
270 /* Need atleast one option specified */
Amit Arorae9e16b02010-08-03 10:15:20 +0530271 if (!regulators && !sensors) {
Amit Arorae9e16b02010-08-03 10:15:20 +0530272 usage(argv);
273 }
274
275 init_regulator_ds();
276
277 if (regulators) {
278 read_regulator_info(verbose);
279 print_regulator_info(verbose);
280 }
281
282 if (sensors) {
283 read_and_print_sensor_info(verbose);
284 }
285
Amit Arora85fd4952010-08-05 14:04:50 +0530286 exit(0);
Amit Arorae9e16b02010-08-03 10:15:20 +0530287}