diff options
author | John Stultz <john.stultz@linaro.org> | 2011-04-25 23:25:24 -0400 |
---|---|---|
committer | Anton Vorontsov <cbouatmailru@gmail.com> | 2011-05-20 11:25:08 -0400 |
commit | f17ef9b2dade4d7afb605465ef1a926fdfc1a4cc (patch) | |
tree | 0642149c33ba45fe48160c36a4a0ccc6f9366af4 /drivers/power | |
parent | 73c244a8a5541e3a1a1997be671b019a220a66e8 (diff) |
power: Make test_power driver more dynamic.
In 2008 Masashi YOKOTA <yokota@pylone.jp> created the virtual
battery driver found here:
http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2
It found use out of tree, but was never merged upstream.
Since then the test_power driver has been merged, which provides
very similar functionality.
This patch extends the test_power driver to be more dynamic
at runtime, by merging portions of the Virtual Battery Driver
by Masashi YOKOTA.
With this patch, I can tweak the values in:
/sys/module/test_power/parameters/* and watch the behavior of
the gnome power managment daemon or other battery UI software.
CC: Anton Vorontsov <cbouatmailru@gmail.com>
CC: Akihiro MAEDA <sola.1980.a@gmail.com>
CC: Masashi YOKOTA <yokota@pylone.jp>
Signed-off-by: John Stultz <john.stultz@linaro.org>
Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
Diffstat (limited to 'drivers/power')
-rw-r--r-- | drivers/power/test_power.c | 276 |
1 files changed, 266 insertions, 10 deletions
diff --git a/drivers/power/test_power.c b/drivers/power/test_power.c index 0cd9f67d33e5..b527c93bf2f3 100644 --- a/drivers/power/test_power.c +++ b/drivers/power/test_power.c | |||
@@ -3,6 +3,12 @@ | |||
3 | * | 3 | * |
4 | * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com> | 4 | * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com> |
5 | * | 5 | * |
6 | * Dynamic module parameter code from the Virtual Battery Driver | ||
7 | * Copyright (C) 2008 Pylone, Inc. | ||
8 | * By: Masashi YOKOTA <yokota@pylone.jp> | ||
9 | * Originally found here: | ||
10 | * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2 | ||
11 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | 12 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License version 2 as | 13 | * it under the terms of the GNU General Public License version 2 as |
8 | * published by the Free Software Foundation. | 14 | * published by the Free Software Foundation. |
@@ -15,8 +21,12 @@ | |||
15 | #include <linux/delay.h> | 21 | #include <linux/delay.h> |
16 | #include <linux/vermagic.h> | 22 | #include <linux/vermagic.h> |
17 | 23 | ||
18 | static int test_power_ac_online = 1; | 24 | static int ac_online = 1; |
19 | static int test_power_battery_status = POWER_SUPPLY_STATUS_CHARGING; | 25 | static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
26 | static int battery_health = POWER_SUPPLY_HEALTH_GOOD; | ||
27 | static int battery_present = 1; /* true */ | ||
28 | static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION; | ||
29 | static int battery_capacity = 50; | ||
20 | 30 | ||
21 | static int test_power_get_ac_property(struct power_supply *psy, | 31 | static int test_power_get_ac_property(struct power_supply *psy, |
22 | enum power_supply_property psp, | 32 | enum power_supply_property psp, |
@@ -24,7 +34,7 @@ static int test_power_get_ac_property(struct power_supply *psy, | |||
24 | { | 34 | { |
25 | switch (psp) { | 35 | switch (psp) { |
26 | case POWER_SUPPLY_PROP_ONLINE: | 36 | case POWER_SUPPLY_PROP_ONLINE: |
27 | val->intval = test_power_ac_online; | 37 | val->intval = ac_online; |
28 | break; | 38 | break; |
29 | default: | 39 | default: |
30 | return -EINVAL; | 40 | return -EINVAL; |
@@ -47,22 +57,30 @@ static int test_power_get_battery_property(struct power_supply *psy, | |||
47 | val->strval = UTS_RELEASE; | 57 | val->strval = UTS_RELEASE; |
48 | break; | 58 | break; |
49 | case POWER_SUPPLY_PROP_STATUS: | 59 | case POWER_SUPPLY_PROP_STATUS: |
50 | val->intval = test_power_battery_status; | 60 | val->intval = battery_status; |
51 | break; | 61 | break; |
52 | case POWER_SUPPLY_PROP_CHARGE_TYPE: | 62 | case POWER_SUPPLY_PROP_CHARGE_TYPE: |
53 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; | 63 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; |
54 | break; | 64 | break; |
55 | case POWER_SUPPLY_PROP_HEALTH: | 65 | case POWER_SUPPLY_PROP_HEALTH: |
56 | val->intval = POWER_SUPPLY_HEALTH_GOOD; | 66 | val->intval = battery_health; |
67 | break; | ||
68 | case POWER_SUPPLY_PROP_PRESENT: | ||
69 | val->intval = battery_present; | ||
57 | break; | 70 | break; |
58 | case POWER_SUPPLY_PROP_TECHNOLOGY: | 71 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
59 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; | 72 | val->intval = battery_technology; |
60 | break; | 73 | break; |
61 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: | 74 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: |
62 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | 75 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; |
63 | break; | 76 | break; |
64 | case POWER_SUPPLY_PROP_CAPACITY: | 77 | case POWER_SUPPLY_PROP_CAPACITY: |
65 | val->intval = 50; | 78 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
79 | val->intval = battery_capacity; | ||
80 | break; | ||
81 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: | ||
82 | case POWER_SUPPLY_PROP_CHARGE_FULL: | ||
83 | val->intval = 100; | ||
66 | break; | 84 | break; |
67 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | 85 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
68 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: | 86 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
@@ -84,9 +102,11 @@ static enum power_supply_property test_power_battery_props[] = { | |||
84 | POWER_SUPPLY_PROP_STATUS, | 102 | POWER_SUPPLY_PROP_STATUS, |
85 | POWER_SUPPLY_PROP_CHARGE_TYPE, | 103 | POWER_SUPPLY_PROP_CHARGE_TYPE, |
86 | POWER_SUPPLY_PROP_HEALTH, | 104 | POWER_SUPPLY_PROP_HEALTH, |
105 | POWER_SUPPLY_PROP_PRESENT, | ||
87 | POWER_SUPPLY_PROP_TECHNOLOGY, | 106 | POWER_SUPPLY_PROP_TECHNOLOGY, |
107 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, | ||
88 | POWER_SUPPLY_PROP_CHARGE_FULL, | 108 | POWER_SUPPLY_PROP_CHARGE_FULL, |
89 | POWER_SUPPLY_PROP_CHARGE_EMPTY, | 109 | POWER_SUPPLY_PROP_CHARGE_NOW, |
90 | POWER_SUPPLY_PROP_CAPACITY, | 110 | POWER_SUPPLY_PROP_CAPACITY, |
91 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, | 111 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, |
92 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, | 112 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
@@ -118,6 +138,7 @@ static struct power_supply test_power_supplies[] = { | |||
118 | }, | 138 | }, |
119 | }; | 139 | }; |
120 | 140 | ||
141 | |||
121 | static int __init test_power_init(void) | 142 | static int __init test_power_init(void) |
122 | { | 143 | { |
123 | int i; | 144 | int i; |
@@ -145,8 +166,8 @@ static void __exit test_power_exit(void) | |||
145 | int i; | 166 | int i; |
146 | 167 | ||
147 | /* Let's see how we handle changes... */ | 168 | /* Let's see how we handle changes... */ |
148 | test_power_ac_online = 0; | 169 | ac_online = 0; |
149 | test_power_battery_status = POWER_SUPPLY_STATUS_DISCHARGING; | 170 | battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
150 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) | 171 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
151 | power_supply_changed(&test_power_supplies[i]); | 172 | power_supply_changed(&test_power_supplies[i]); |
152 | pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n", | 173 | pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n", |
@@ -158,6 +179,241 @@ static void __exit test_power_exit(void) | |||
158 | } | 179 | } |
159 | module_exit(test_power_exit); | 180 | module_exit(test_power_exit); |
160 | 181 | ||
182 | |||
183 | |||
184 | #define MAX_KEYLENGTH 256 | ||
185 | struct battery_property_map { | ||
186 | int value; | ||
187 | char const *key; | ||
188 | }; | ||
189 | |||
190 | static struct battery_property_map map_ac_online[] = { | ||
191 | { 0, "on" }, | ||
192 | { 1, "off" }, | ||
193 | { -1, NULL }, | ||
194 | }; | ||
195 | |||
196 | static struct battery_property_map map_status[] = { | ||
197 | { POWER_SUPPLY_STATUS_CHARGING, "charging" }, | ||
198 | { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" }, | ||
199 | { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" }, | ||
200 | { POWER_SUPPLY_STATUS_FULL, "full" }, | ||
201 | { -1, NULL }, | ||
202 | }; | ||
203 | |||
204 | static struct battery_property_map map_health[] = { | ||
205 | { POWER_SUPPLY_HEALTH_GOOD, "good" }, | ||
206 | { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" }, | ||
207 | { POWER_SUPPLY_HEALTH_DEAD, "dead" }, | ||
208 | { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" }, | ||
209 | { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" }, | ||
210 | { -1, NULL }, | ||
211 | }; | ||
212 | |||
213 | static struct battery_property_map map_present[] = { | ||
214 | { 0, "false" }, | ||
215 | { 1, "true" }, | ||
216 | { -1, NULL }, | ||
217 | }; | ||
218 | |||
219 | static struct battery_property_map map_technology[] = { | ||
220 | { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" }, | ||
221 | { POWER_SUPPLY_TECHNOLOGY_LION, "LION" }, | ||
222 | { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" }, | ||
223 | { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" }, | ||
224 | { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" }, | ||
225 | { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" }, | ||
226 | { -1, NULL }, | ||
227 | }; | ||
228 | |||
229 | |||
230 | static int map_get_value(struct battery_property_map *map, const char *key, | ||
231 | int def_val) | ||
232 | { | ||
233 | char buf[MAX_KEYLENGTH]; | ||
234 | int cr; | ||
235 | |||
236 | strncpy(buf, key, MAX_KEYLENGTH); | ||
237 | buf[MAX_KEYLENGTH-1] = '\0'; | ||
238 | |||
239 | cr = strnlen(buf, MAX_KEYLENGTH) - 1; | ||
240 | if (buf[cr] == '\n') | ||
241 | buf[cr] = '\0'; | ||
242 | |||
243 | while (map->key) { | ||
244 | if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0) | ||
245 | return map->value; | ||
246 | map++; | ||
247 | } | ||
248 | |||
249 | return def_val; | ||
250 | } | ||
251 | |||
252 | |||
253 | static const char *map_get_key(struct battery_property_map *map, int value, | ||
254 | const char *def_key) | ||
255 | { | ||
256 | while (map->key) { | ||
257 | if (map->value == value) | ||
258 | return map->key; | ||
259 | map++; | ||
260 | } | ||
261 | |||
262 | return def_key; | ||
263 | } | ||
264 | |||
265 | static int param_set_ac_online(const char *key, const struct kernel_param *kp) | ||
266 | { | ||
267 | ac_online = map_get_value(map_ac_online, key, ac_online); | ||
268 | power_supply_changed(&test_power_supplies[0]); | ||
269 | return 0; | ||
270 | } | ||
271 | |||
272 | static int param_get_ac_online(char *buffer, const struct kernel_param *kp) | ||
273 | { | ||
274 | strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown")); | ||
275 | return strlen(buffer); | ||
276 | } | ||
277 | |||
278 | static int param_set_battery_status(const char *key, | ||
279 | const struct kernel_param *kp) | ||
280 | { | ||
281 | battery_status = map_get_value(map_status, key, battery_status); | ||
282 | power_supply_changed(&test_power_supplies[1]); | ||
283 | return 0; | ||
284 | } | ||
285 | |||
286 | static int param_get_battery_status(char *buffer, const struct kernel_param *kp) | ||
287 | { | ||
288 | strcpy(buffer, map_get_key(map_status, battery_status, "unknown")); | ||
289 | return strlen(buffer); | ||
290 | } | ||
291 | |||
292 | static int param_set_battery_health(const char *key, | ||
293 | const struct kernel_param *kp) | ||
294 | { | ||
295 | battery_health = map_get_value(map_health, key, battery_health); | ||
296 | power_supply_changed(&test_power_supplies[1]); | ||
297 | return 0; | ||
298 | } | ||
299 | |||
300 | static int param_get_battery_health(char *buffer, const struct kernel_param *kp) | ||
301 | { | ||
302 | strcpy(buffer, map_get_key(map_health, battery_health, "unknown")); | ||
303 | return strlen(buffer); | ||
304 | } | ||
305 | |||
306 | static int param_set_battery_present(const char *key, | ||
307 | const struct kernel_param *kp) | ||
308 | { | ||
309 | battery_present = map_get_value(map_present, key, battery_present); | ||
310 | power_supply_changed(&test_power_supplies[0]); | ||
311 | return 0; | ||
312 | } | ||
313 | |||
314 | static int param_get_battery_present(char *buffer, | ||
315 | const struct kernel_param *kp) | ||
316 | { | ||
317 | strcpy(buffer, map_get_key(map_present, battery_present, "unknown")); | ||
318 | return strlen(buffer); | ||
319 | } | ||
320 | |||
321 | static int param_set_battery_technology(const char *key, | ||
322 | const struct kernel_param *kp) | ||
323 | { | ||
324 | battery_technology = map_get_value(map_technology, key, | ||
325 | battery_technology); | ||
326 | power_supply_changed(&test_power_supplies[1]); | ||
327 | return 0; | ||
328 | } | ||
329 | |||
330 | static int param_get_battery_technology(char *buffer, | ||
331 | const struct kernel_param *kp) | ||
332 | { | ||
333 | strcpy(buffer, | ||
334 | map_get_key(map_technology, battery_technology, "unknown")); | ||
335 | return strlen(buffer); | ||
336 | } | ||
337 | |||
338 | static int param_set_battery_capacity(const char *key, | ||
339 | const struct kernel_param *kp) | ||
340 | { | ||
341 | int tmp; | ||
342 | |||
343 | if (1 != sscanf(key, "%d", &tmp)) | ||
344 | return -EINVAL; | ||
345 | |||
346 | battery_capacity = tmp; | ||
347 | power_supply_changed(&test_power_supplies[1]); | ||
348 | return 0; | ||
349 | } | ||
350 | |||
351 | #define param_get_battery_capacity param_get_int | ||
352 | |||
353 | |||
354 | |||
355 | static struct kernel_param_ops param_ops_ac_online = { | ||
356 | .set = param_set_ac_online, | ||
357 | .get = param_get_ac_online, | ||
358 | }; | ||
359 | |||
360 | static struct kernel_param_ops param_ops_battery_status = { | ||
361 | .set = param_set_battery_status, | ||
362 | .get = param_get_battery_status, | ||
363 | }; | ||
364 | |||
365 | static struct kernel_param_ops param_ops_battery_present = { | ||
366 | .set = param_set_battery_present, | ||
367 | .get = param_get_battery_present, | ||
368 | }; | ||
369 | |||
370 | static struct kernel_param_ops param_ops_battery_technology = { | ||
371 | .set = param_set_battery_technology, | ||
372 | .get = param_get_battery_technology, | ||
373 | }; | ||
374 | |||
375 | static struct kernel_param_ops param_ops_battery_health = { | ||
376 | .set = param_set_battery_health, | ||
377 | .get = param_get_battery_health, | ||
378 | }; | ||
379 | |||
380 | static struct kernel_param_ops param_ops_battery_capacity = { | ||
381 | .set = param_set_battery_capacity, | ||
382 | .get = param_get_battery_capacity, | ||
383 | }; | ||
384 | |||
385 | |||
386 | #define param_check_ac_online(name, p) __param_check(name, p, void); | ||
387 | #define param_check_battery_status(name, p) __param_check(name, p, void); | ||
388 | #define param_check_battery_present(name, p) __param_check(name, p, void); | ||
389 | #define param_check_battery_technology(name, p) __param_check(name, p, void); | ||
390 | #define param_check_battery_health(name, p) __param_check(name, p, void); | ||
391 | #define param_check_battery_capacity(name, p) __param_check(name, p, void); | ||
392 | |||
393 | |||
394 | module_param(ac_online, ac_online, 0644); | ||
395 | MODULE_PARM_DESC(ac_online, "AC charging state <on|off>"); | ||
396 | |||
397 | module_param(battery_status, battery_status, 0644); | ||
398 | MODULE_PARM_DESC(battery_status, | ||
399 | "battery status <charging|discharging|not-charging|full>"); | ||
400 | |||
401 | module_param(battery_present, battery_present, 0644); | ||
402 | MODULE_PARM_DESC(battery_present, | ||
403 | "battery presence state <good|overheat|dead|overvoltage|failure>"); | ||
404 | |||
405 | module_param(battery_technology, battery_technology, 0644); | ||
406 | MODULE_PARM_DESC(battery_technology, | ||
407 | "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>"); | ||
408 | |||
409 | module_param(battery_health, battery_health, 0644); | ||
410 | MODULE_PARM_DESC(battery_health, | ||
411 | "battery health state <good|overheat|dead|overvoltage|failure>"); | ||
412 | |||
413 | module_param(battery_capacity, battery_capacity, 0644); | ||
414 | MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)"); | ||
415 | |||
416 | |||
161 | MODULE_DESCRIPTION("Power supply driver for testing"); | 417 | MODULE_DESCRIPTION("Power supply driver for testing"); |
162 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); | 418 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); |
163 | MODULE_LICENSE("GPL"); | 419 | MODULE_LICENSE("GPL"); |