~ubuntu-branches/ubuntu/saucy/cpufreqd/saucy-updates

« back to all changes in this revision

Viewing changes to src/cpufreqd_pmu.c

  • Committer: Bazaar Package Importer
  • Author(s): Mattia Dongili
  • Date: 2006-12-17 17:13:57 UTC
  • mfrom: (3.1.5 feisty)
  • Revision ID: james.westby@ubuntu.com-20061217171357-atrpxy6jqdq846jm
Tags: 2.2.1-2
Provide a more conservative config files, users owning non ondemand
capable CPU have complained. (Closes: #400580)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2003,2004  Rene Rebe <rene@rocklinux.org>
 
3
 *                2005       Mattia Dongili <malattia@linux.it>
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 *  Based upon the acpi version:
 
20
 *    Copyright (C) 2002-2005  Mattia Dongili<malattia@linux.it>
 
21
 *                             George Staikos <staikos@0wned.org>
 
22
 * 
 
23
 *  2005-09-11
 
24
 *  Ported to cpufreqd-2 new plugin interface by Mattia Dongili
 
25
 */
 
26
 
 
27
#include <ctype.h>
 
28
#include <errno.h>
 
29
#include <stdio.h>
 
30
#include <stdlib.h>
 
31
#include <string.h>
 
32
#include "cpufreqd_plugin.h"
 
33
 
 
34
#ifndef DEBUG_NO_PMU
 
35
#  define PMU_INFO_FILE         "/proc/pmu/info"
 
36
#  define PMU_BATTERY_FILE      "/proc/pmu/battery_0"
 
37
#else
 
38
#  define PMU_INFO_FILE         "/home/mattia/devel/cpufreqd/pmu/info"
 
39
#  define PMU_BATTERY_FILE      "/home/mattia/devel/cpufreqd/pmu/battery_0"
 
40
#endif
 
41
#define PLUGGED   1
 
42
#define UNPLUGGED 0
 
43
 
 
44
struct battery_interval {
 
45
        int min, max;
 
46
};
 
47
 
 
48
static char tag[255];
 
49
static char val[255];
 
50
static char version[100];
 
51
static unsigned int battery_present;
 
52
static int battery_percent;
 
53
static unsigned int ac;
 
54
 
 
55
static int tokenize (FILE *fp, char *t, char *v) {
 
56
        char str[255];
 
57
        char *s1, *s2;
 
58
 
 
59
        t[0] = v[0] = '\0';
 
60
 
 
61
        if (fgets(str, 255, fp) == NULL)
 
62
                return EOF;
 
63
 
 
64
        if ((s1 = strtok(str, ":")) == NULL)
 
65
                return 0;
 
66
 
 
67
        s2 = s1 + strlen (s1) - 1;
 
68
        /* remove trailing spaces */
 
69
        for ( ; s1 != s2 ; --s2) {
 
70
                if (!isspace(*s2)) {
 
71
                        break;
 
72
                } else {
 
73
                        *s2 = '\0';
 
74
                }
 
75
        }
 
76
 
 
77
        strncpy (t, s1, 255);
 
78
        t[254] = '\0';
 
79
 
 
80
        if ((s1 = strtok(NULL, ":")) == NULL)
 
81
                return 0;
 
82
 
 
83
        for ( ; *s1 != 0 ; ++s1) {
 
84
                if (!isspace(*s1)) {
 
85
                        break;
 
86
                }
 
87
        }
 
88
        s2 = s1 + strlen (s1) - 1;
 
89
        /* remove trailing spaces */
 
90
        for ( ; s1 != s2 ; --s2) {
 
91
                if (!isspace(*s2)) {
 
92
                        break;
 
93
                } else {
 
94
                        *s2 = '\0';
 
95
                }
 
96
        }
 
97
 
 
98
        strncpy (v, s1, 255);
 
99
        v[254] = '\0';
 
100
 
 
101
        return 1;
 
102
}
 
103
 
 
104
/* check if PMU is available
 
105
 */
 
106
static int pmu_init(void) {
 
107
 
 
108
        FILE *fp;
 
109
 
 
110
        fp = fopen(PMU_INFO_FILE, "r");
 
111
        if (!fp) {
 
112
                clog(LOG_ERR, "%s: %s\n", PMU_INFO_FILE, strerror(errno));
 
113
                return -1;
 
114
        }
 
115
 
 
116
        while (tokenize(fp, tag, val) != EOF) {
 
117
                if (strcmp(tag, "PMU driver version") == 0) {
 
118
                        sprintf(version, "%s - ", val);
 
119
                } 
 
120
                else if (strcmp(tag, "PMU firmware version") == 0) {
 
121
                        strncat(version, val, 100-strlen(version));
 
122
                }
 
123
        } 
 
124
        fclose(fp);
 
125
 
 
126
        clog(LOG_NOTICE, "PMU driver/firmware version %s\n", version);
 
127
 
 
128
        return 0;
 
129
}
 
130
 
 
131
static int pmu_update(void) {
 
132
 
 
133
        FILE *fp;
 
134
 
 
135
        float bat_charge = .0;
 
136
        float bat_max_charge = .0;
 
137
 
 
138
        /** /proc/pmu/info **/
 
139
        fp = fopen(PMU_INFO_FILE, "r");
 
140
        if (!fp) {
 
141
                clog(LOG_ERR, "%s: %s\n", PMU_INFO_FILE, strerror(errno));
 
142
                return -1;
 
143
        }
 
144
 
 
145
        while (tokenize(fp, tag, val) != EOF) {
 
146
                if (strcmp(tag, "AC Power") == 0) {
 
147
                        ac = atoi(val); 
 
148
                }
 
149
                else if (strcmp(tag, "Battery count") == 0) {
 
150
                        battery_present = atoi(val); 
 
151
                }
 
152
        }
 
153
        fclose(fp);
 
154
 
 
155
        /** /proc/pmu/battery_0 **/
 
156
        fp = fopen(PMU_BATTERY_FILE, "r");
 
157
        if (!fp) {
 
158
                clog(LOG_ERR, "%s: %s\n", PMU_BATTERY_FILE, strerror(errno));
 
159
                return -1;
 
160
        }
 
161
 
 
162
        while (tokenize(fp, tag, val) != EOF) {
 
163
                if (strcmp(tag, "charge") == 0) {
 
164
                        bat_charge = atof(val); 
 
165
                }
 
166
                else if (strcmp(tag, "max_charge") == 0) {
 
167
                        bat_max_charge = atof(val);
 
168
                }
 
169
        }
 
170
        fclose(fp);
 
171
 
 
172
        battery_percent = 100 * (bat_charge / bat_max_charge);
 
173
 
 
174
        clog(LOG_INFO, "battery %s - %d - %s\n",
 
175
                        battery_present ? "present" : "absent", 
 
176
                        battery_percent, 
 
177
                        ac ? "on-line" : "off-line");
 
178
        return 0;
 
179
}
 
180
 
 
181
/*
 
182
 *  parse the 'ac' keywork
 
183
 */
 
184
static int pmu_ac_parse(const char *ev, void **obj) {
 
185
        unsigned int *ret = malloc(sizeof(int));
 
186
        if (ret == NULL) {
 
187
                clog(LOG_ERR, "couldn't make enough room for ac_status (%s)\n",
 
188
                                strerror(errno));
 
189
                return -1;
 
190
        }
 
191
        *ret = 0;
 
192
        clog(LOG_DEBUG, "called with %s\n", ev);
 
193
 
 
194
        if (strncmp(ev, "on", 2) == 0) {
 
195
                *ret = PLUGGED;
 
196
        } else if (strncmp(ev, "off", 3) == 0) {
 
197
                *ret = UNPLUGGED;
 
198
        } else {
 
199
                clog(LOG_ERR, "couldn't parse %s\n", ev);
 
200
                free(ret);
 
201
                return -1;
 
202
        }
 
203
        clog(LOG_INFO, "parsed %s\n", *ret==PLUGGED ? "on" : "off");
 
204
 
 
205
        *obj = ret;
 
206
        return 0;
 
207
}
 
208
 
 
209
/*
 
210
 *  evaluate the 'ac' keywork
 
211
 */
 
212
static int pmu_ac_evaluate(const void *s) {
 
213
        const unsigned int *ac_parm = (const unsigned int *)s;
 
214
 
 
215
        clog(LOG_DEBUG, "called %s [%s]\n",
 
216
                        *ac_parm==PLUGGED ? "on" : "off", ac==PLUGGED ? "on" : "off");
 
217
 
 
218
        return (*ac_parm == ac) ? MATCH : DONT_MATCH;
 
219
}
 
220
 
 
221
/*
 
222
 *  parse the 'battery' keywork
 
223
 */
 
224
static int pmu_bat_parse(const char *ev, void **obj) {
 
225
        struct battery_interval *ret = malloc(sizeof(struct battery_interval));
 
226
        if (ret == NULL) {
 
227
                clog(LOG_ERR, "couldn't make enough room for battery_interval (%s)\n",
 
228
                                strerror(errno));
 
229
                return -1;
 
230
        }
 
231
        ret->min = ret->max = 0;
 
232
        clog(LOG_DEBUG, "called with %s\n", ev);
 
233
 
 
234
        if (sscanf(ev, "%d-%d", &(ret->min), &(ret->max)) != 2) {
 
235
                clog(LOG_ERR, "wrong parameter %s\n", ev);
 
236
                free(ret);
 
237
                return -1;
 
238
        }
 
239
 
 
240
        clog(LOG_INFO, "parsed %d-%d\n", ret->min, ret->max);
 
241
 
 
242
        *obj = ret;
 
243
        return 0;
 
244
}
 
245
 
 
246
/*
 
247
 *  evaluate the 'battery' keywork
 
248
 */
 
249
static int pmu_bat_evaluate(const void *s) {
 
250
        const struct battery_interval *bi = (const struct battery_interval *)s;
 
251
 
 
252
        clog(LOG_DEBUG, "called %d-%d [%d]\n", bi->min, bi->max, battery_percent);
 
253
 
 
254
        return (battery_percent>=bi->min && battery_percent<=bi->max) ? MATCH : DONT_MATCH;
 
255
}
 
256
 
 
257
static struct cpufreqd_keyword kw[] = {
 
258
        { .word = "ac",       .parse = &pmu_ac_parse,  .evaluate = &pmu_ac_evaluate  },
 
259
        { .word = "battery_interval",  .parse = &pmu_bat_parse, .evaluate = &pmu_bat_evaluate },
 
260
        { .word = NULL,       .parse = NULL,           .evaluate = NULL              }
 
261
};
 
262
 
 
263
static struct cpufreqd_plugin pmu = {
 
264
        .plugin_name      = "pmu_plugin",       /* plugin_name */
 
265
        .keywords         = kw,                 /* config_keywords */
 
266
        .plugin_init      = &pmu_init,          /* plugin_init */
 
267
        .plugin_update    = &pmu_update         /* plugin_update */
 
268
};
 
269
 
 
270
struct cpufreqd_plugin *create_plugin (void) {
 
271
        return &pmu;
 
272
}