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
|
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <string.h>
#include <kstat.h>
#include <errno.h>
#include "powertop.h"
#define mW2W(value) ((value) / 1000)
typedef struct battery_state {
uint32_t exist;
uint32_t power_unit;
uint32_t bst_state;
double present_rate;
double remain_cap;
double last_cap;
} battery_state_t;
static char *kstat_batt_mod[3] = {NULL, "battery", "acpi_drv"};
static uint_t kstat_batt_idx;
static battery_state_t battery_state;
static int pt_battery_stat_snapshot(void);
/*
* Checks if the kstat module for battery information is present and
* whether it's called 'battery' or 'acpi_drv'
*/
void
pt_battery_mod_lookup(void)
{
kstat_ctl_t *kc = kstat_open();
if (kstat_lookup(kc, kstat_batt_mod[1], 0, NULL))
kstat_batt_idx = 1;
else
if (kstat_lookup(kc, kstat_batt_mod[2], 0, NULL))
kstat_batt_idx = 2;
else
kstat_batt_idx = 0;
(void) kstat_close(kc);
}
void
pt_battery_print(void)
{
int err;
(void) memset(&battery_state, 0, sizeof (battery_state_t));
/*
* The return value of pt_battery_stat_snapshot() can be used for
* debug or to show/hide the acpi power line. We currently don't
* make the distinction of a system that runs only on AC and one
* that runs on battery but has no kstat battery info.
*
* We still display the estimate power usage for systems
* running on AC with a fully charged battery because some
* batteries may still consume power.
*
* If pt_battery_mod_lookup() didn't find a kstat battery module, don't
* bother trying to take the snapshot
*/
if (kstat_batt_idx > 0) {
if ((err = pt_battery_stat_snapshot()) < 0)
pt_error("battery kstat not found (%d)\n", err);
}
pt_display_acpi_power(battery_state.exist, battery_state.present_rate,
battery_state.remain_cap, battery_state.last_cap,
battery_state.bst_state);
}
static int
pt_battery_stat_snapshot(void)
{
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
kc = kstat_open();
/*
* power unit:
* 0 - Capacity information is reported in [mWh] and
* charge/discharge rate information in [mW]
* 1 - Capacity information is reported in [mAh] and
* charge/discharge rate information in [mA].
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BIF0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bif_unit");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.power_unit = knp->value.ui32;
/*
* Present rate:
* the power or current being supplied or accepted
* through the battery's terminal
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_rate");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
if (knp->value.ui32 == 0xFFFFFFFF)
battery_state.present_rate = 0;
else {
battery_state.exist = 1;
battery_state.present_rate = mW2W((double)(knp->value.ui32));
}
/*
* Last Full charge capacity:
* Predicted battery capacity when fully charged.
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BIF0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bif_last_cap");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.last_cap = mW2W((double)(knp->value.ui32));
/*
* Remaining capacity:
* the estimated remaining battery capacity
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_rem_cap");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.remain_cap = mW2W((double)(knp->value.ui32));
/*
* Battery State:
* Bit0 - 1 : discharging
* Bit1 - 1 : charging
* Bit2 - 1 : critical energy state
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_state");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.bst_state = knp->value.ui32;
(void) kstat_close(kc);
return (0);
}
|