power_supply: Add driver private data
[linux-2.6-block.git] / drivers / power / test_power.c
CommitLineData
a1e50fd4
AV
1/*
2 * Power supply driver for testing.
3 *
4 * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com>
5 *
f17ef9b2
JS
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 *
a1e50fd4
AV
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/power_supply.h>
20#include <linux/errno.h>
21#include <linux/delay.h>
22#include <linux/vermagic.h>
23
a30067bb
KK
24enum test_power_id {
25 TEST_AC,
26 TEST_BATTERY,
27 TEST_USB,
28 TEST_POWER_NUM,
29};
30
f17ef9b2 31static int ac_online = 1;
73847375 32static int usb_online = 1;
f17ef9b2
JS
33static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
34static int battery_health = POWER_SUPPLY_HEALTH_GOOD;
35static int battery_present = 1; /* true */
36static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION;
37static int battery_capacity = 50;
85a392d4 38static int battery_voltage = 3300;
a1e50fd4 39
9239ebcf
AG
40static bool module_initialized;
41
a1e50fd4
AV
42static int test_power_get_ac_property(struct power_supply *psy,
43 enum power_supply_property psp,
44 union power_supply_propval *val)
45{
46 switch (psp) {
47 case POWER_SUPPLY_PROP_ONLINE:
f17ef9b2 48 val->intval = ac_online;
a1e50fd4
AV
49 break;
50 default:
51 return -EINVAL;
52 }
53 return 0;
54}
55
73847375
DES
56static int test_power_get_usb_property(struct power_supply *psy,
57 enum power_supply_property psp,
58 union power_supply_propval *val)
59{
60 switch (psp) {
61 case POWER_SUPPLY_PROP_ONLINE:
62 val->intval = usb_online;
63 break;
64 default:
65 return -EINVAL;
66 }
67 return 0;
68}
69
a1e50fd4
AV
70static int test_power_get_battery_property(struct power_supply *psy,
71 enum power_supply_property psp,
72 union power_supply_propval *val)
73{
74 switch (psp) {
75 case POWER_SUPPLY_PROP_MODEL_NAME:
76 val->strval = "Test battery";
77 break;
78 case POWER_SUPPLY_PROP_MANUFACTURER:
79 val->strval = "Linux";
80 break;
81 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
82 val->strval = UTS_RELEASE;
83 break;
84 case POWER_SUPPLY_PROP_STATUS:
f17ef9b2 85 val->intval = battery_status;
a1e50fd4
AV
86 break;
87 case POWER_SUPPLY_PROP_CHARGE_TYPE:
88 val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
89 break;
90 case POWER_SUPPLY_PROP_HEALTH:
f17ef9b2
JS
91 val->intval = battery_health;
92 break;
93 case POWER_SUPPLY_PROP_PRESENT:
94 val->intval = battery_present;
a1e50fd4
AV
95 break;
96 case POWER_SUPPLY_PROP_TECHNOLOGY:
f17ef9b2 97 val->intval = battery_technology;
a1e50fd4
AV
98 break;
99 case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
100 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
101 break;
102 case POWER_SUPPLY_PROP_CAPACITY:
f17ef9b2
JS
103 case POWER_SUPPLY_PROP_CHARGE_NOW:
104 val->intval = battery_capacity;
105 break;
106 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
107 case POWER_SUPPLY_PROP_CHARGE_FULL:
108 val->intval = 100;
a1e50fd4
AV
109 break;
110 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
111 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
112 val->intval = 3600;
113 break;
85a392d4
DES
114 case POWER_SUPPLY_PROP_TEMP:
115 val->intval = 26;
116 break;
117 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
118 val->intval = battery_voltage;
119 break;
a1e50fd4
AV
120 default:
121 pr_info("%s: some properties deliberately report errors.\n",
122 __func__);
123 return -EINVAL;
124 }
125 return 0;
126}
127
128static enum power_supply_property test_power_ac_props[] = {
129 POWER_SUPPLY_PROP_ONLINE,
130};
131
132static enum power_supply_property test_power_battery_props[] = {
133 POWER_SUPPLY_PROP_STATUS,
134 POWER_SUPPLY_PROP_CHARGE_TYPE,
135 POWER_SUPPLY_PROP_HEALTH,
f17ef9b2 136 POWER_SUPPLY_PROP_PRESENT,
a1e50fd4 137 POWER_SUPPLY_PROP_TECHNOLOGY,
f17ef9b2 138 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
a1e50fd4 139 POWER_SUPPLY_PROP_CHARGE_FULL,
f17ef9b2 140 POWER_SUPPLY_PROP_CHARGE_NOW,
a1e50fd4
AV
141 POWER_SUPPLY_PROP_CAPACITY,
142 POWER_SUPPLY_PROP_CAPACITY_LEVEL,
143 POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
144 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
145 POWER_SUPPLY_PROP_MODEL_NAME,
146 POWER_SUPPLY_PROP_MANUFACTURER,
147 POWER_SUPPLY_PROP_SERIAL_NUMBER,
85a392d4
DES
148 POWER_SUPPLY_PROP_TEMP,
149 POWER_SUPPLY_PROP_VOLTAGE_NOW,
a1e50fd4
AV
150};
151
152static char *test_power_ac_supplied_to[] = {
153 "test_battery",
154};
155
156static struct power_supply test_power_supplies[] = {
a30067bb 157 [TEST_AC] = {
a1e50fd4
AV
158 .name = "test_ac",
159 .type = POWER_SUPPLY_TYPE_MAINS,
160 .supplied_to = test_power_ac_supplied_to,
161 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
162 .properties = test_power_ac_props,
163 .num_properties = ARRAY_SIZE(test_power_ac_props),
164 .get_property = test_power_get_ac_property,
a30067bb
KK
165 },
166 [TEST_BATTERY] = {
a1e50fd4
AV
167 .name = "test_battery",
168 .type = POWER_SUPPLY_TYPE_BATTERY,
169 .properties = test_power_battery_props,
170 .num_properties = ARRAY_SIZE(test_power_battery_props),
171 .get_property = test_power_get_battery_property,
a30067bb
KK
172 },
173 [TEST_USB] = {
73847375
DES
174 .name = "test_usb",
175 .type = POWER_SUPPLY_TYPE_USB,
176 .supplied_to = test_power_ac_supplied_to,
177 .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to),
178 .properties = test_power_ac_props,
179 .num_properties = ARRAY_SIZE(test_power_ac_props),
180 .get_property = test_power_get_usb_property,
a1e50fd4
AV
181 },
182};
183
f17ef9b2 184
a1e50fd4
AV
185static int __init test_power_init(void)
186{
187 int i;
188 int ret;
189
a30067bb
KK
190 BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies));
191
a1e50fd4
AV
192 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) {
193 ret = power_supply_register(NULL, &test_power_supplies[i]);
194 if (ret) {
195 pr_err("%s: failed to register %s\n", __func__,
196 test_power_supplies[i].name);
197 goto failed;
198 }
199 }
200
9239ebcf 201 module_initialized = true;
a1e50fd4
AV
202 return 0;
203failed:
204 while (--i >= 0)
205 power_supply_unregister(&test_power_supplies[i]);
206 return ret;
207}
208module_init(test_power_init);
209
210static void __exit test_power_exit(void)
211{
212 int i;
213
214 /* Let's see how we handle changes... */
f17ef9b2 215 ac_online = 0;
73847375 216 usb_online = 0;
f17ef9b2 217 battery_status = POWER_SUPPLY_STATUS_DISCHARGING;
a1e50fd4
AV
218 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
219 power_supply_changed(&test_power_supplies[i]);
220 pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n",
221 __func__);
222 ssleep(10);
223
224 for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++)
225 power_supply_unregister(&test_power_supplies[i]);
9239ebcf
AG
226
227 module_initialized = false;
a1e50fd4
AV
228}
229module_exit(test_power_exit);
230
f17ef9b2
JS
231
232
233#define MAX_KEYLENGTH 256
234struct battery_property_map {
235 int value;
236 char const *key;
237};
238
239static struct battery_property_map map_ac_online[] = {
9239ebcf
AG
240 { 0, "off" },
241 { 1, "on" },
f17ef9b2
JS
242 { -1, NULL },
243};
244
245static struct battery_property_map map_status[] = {
246 { POWER_SUPPLY_STATUS_CHARGING, "charging" },
247 { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" },
248 { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" },
249 { POWER_SUPPLY_STATUS_FULL, "full" },
250 { -1, NULL },
251};
252
253static struct battery_property_map map_health[] = {
254 { POWER_SUPPLY_HEALTH_GOOD, "good" },
255 { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" },
256 { POWER_SUPPLY_HEALTH_DEAD, "dead" },
257 { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" },
258 { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" },
259 { -1, NULL },
260};
261
262static struct battery_property_map map_present[] = {
263 { 0, "false" },
264 { 1, "true" },
265 { -1, NULL },
266};
267
268static struct battery_property_map map_technology[] = {
269 { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" },
270 { POWER_SUPPLY_TECHNOLOGY_LION, "LION" },
271 { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" },
272 { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" },
273 { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" },
274 { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" },
275 { -1, NULL },
276};
277
278
279static int map_get_value(struct battery_property_map *map, const char *key,
280 int def_val)
281{
282 char buf[MAX_KEYLENGTH];
283 int cr;
284
285 strncpy(buf, key, MAX_KEYLENGTH);
286 buf[MAX_KEYLENGTH-1] = '\0';
287
288 cr = strnlen(buf, MAX_KEYLENGTH) - 1;
289 if (buf[cr] == '\n')
290 buf[cr] = '\0';
291
292 while (map->key) {
293 if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0)
294 return map->value;
295 map++;
296 }
297
298 return def_val;
299}
300
301
302static const char *map_get_key(struct battery_property_map *map, int value,
303 const char *def_key)
304{
305 while (map->key) {
306 if (map->value == value)
307 return map->key;
308 map++;
309 }
310
311 return def_key;
312}
313
9239ebcf
AG
314static inline void signal_power_supply_changed(struct power_supply *psy)
315{
316 if (module_initialized)
317 power_supply_changed(psy);
318}
319
f17ef9b2
JS
320static int param_set_ac_online(const char *key, const struct kernel_param *kp)
321{
322 ac_online = map_get_value(map_ac_online, key, ac_online);
a30067bb 323 signal_power_supply_changed(&test_power_supplies[TEST_AC]);
f17ef9b2
JS
324 return 0;
325}
326
327static int param_get_ac_online(char *buffer, const struct kernel_param *kp)
328{
329 strcpy(buffer, map_get_key(map_ac_online, ac_online, "unknown"));
330 return strlen(buffer);
331}
332
73847375
DES
333static int param_set_usb_online(const char *key, const struct kernel_param *kp)
334{
335 usb_online = map_get_value(map_ac_online, key, usb_online);
a30067bb 336 signal_power_supply_changed(&test_power_supplies[TEST_USB]);
73847375
DES
337 return 0;
338}
339
340static int param_get_usb_online(char *buffer, const struct kernel_param *kp)
341{
342 strcpy(buffer, map_get_key(map_ac_online, usb_online, "unknown"));
343 return strlen(buffer);
344}
345
f17ef9b2
JS
346static int param_set_battery_status(const char *key,
347 const struct kernel_param *kp)
348{
349 battery_status = map_get_value(map_status, key, battery_status);
a30067bb 350 signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
f17ef9b2
JS
351 return 0;
352}
353
354static int param_get_battery_status(char *buffer, const struct kernel_param *kp)
355{
356 strcpy(buffer, map_get_key(map_status, battery_status, "unknown"));
357 return strlen(buffer);
358}
359
360static int param_set_battery_health(const char *key,
361 const struct kernel_param *kp)
362{
363 battery_health = map_get_value(map_health, key, battery_health);
a30067bb 364 signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
f17ef9b2
JS
365 return 0;
366}
367
368static int param_get_battery_health(char *buffer, const struct kernel_param *kp)
369{
370 strcpy(buffer, map_get_key(map_health, battery_health, "unknown"));
371 return strlen(buffer);
372}
373
374static int param_set_battery_present(const char *key,
375 const struct kernel_param *kp)
376{
377 battery_present = map_get_value(map_present, key, battery_present);
a30067bb 378 signal_power_supply_changed(&test_power_supplies[TEST_AC]);
f17ef9b2
JS
379 return 0;
380}
381
382static int param_get_battery_present(char *buffer,
383 const struct kernel_param *kp)
384{
385 strcpy(buffer, map_get_key(map_present, battery_present, "unknown"));
386 return strlen(buffer);
387}
388
389static int param_set_battery_technology(const char *key,
390 const struct kernel_param *kp)
391{
392 battery_technology = map_get_value(map_technology, key,
393 battery_technology);
a30067bb 394 signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
f17ef9b2
JS
395 return 0;
396}
397
398static int param_get_battery_technology(char *buffer,
399 const struct kernel_param *kp)
400{
401 strcpy(buffer,
402 map_get_key(map_technology, battery_technology, "unknown"));
403 return strlen(buffer);
404}
405
406static int param_set_battery_capacity(const char *key,
407 const struct kernel_param *kp)
408{
409 int tmp;
410
411 if (1 != sscanf(key, "%d", &tmp))
412 return -EINVAL;
413
414 battery_capacity = tmp;
a30067bb 415 signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
f17ef9b2
JS
416 return 0;
417}
418
419#define param_get_battery_capacity param_get_int
420
85a392d4
DES
421static int param_set_battery_voltage(const char *key,
422 const struct kernel_param *kp)
423{
424 int tmp;
425
426 if (1 != sscanf(key, "%d", &tmp))
427 return -EINVAL;
428
429 battery_voltage = tmp;
a30067bb 430 signal_power_supply_changed(&test_power_supplies[TEST_BATTERY]);
85a392d4
DES
431 return 0;
432}
433
434#define param_get_battery_voltage param_get_int
435
f17ef9b2
JS
436static struct kernel_param_ops param_ops_ac_online = {
437 .set = param_set_ac_online,
438 .get = param_get_ac_online,
439};
440
73847375
DES
441static struct kernel_param_ops param_ops_usb_online = {
442 .set = param_set_usb_online,
443 .get = param_get_usb_online,
444};
445
f17ef9b2
JS
446static struct kernel_param_ops param_ops_battery_status = {
447 .set = param_set_battery_status,
448 .get = param_get_battery_status,
449};
450
451static struct kernel_param_ops param_ops_battery_present = {
452 .set = param_set_battery_present,
453 .get = param_get_battery_present,
454};
455
456static struct kernel_param_ops param_ops_battery_technology = {
457 .set = param_set_battery_technology,
458 .get = param_get_battery_technology,
459};
460
461static struct kernel_param_ops param_ops_battery_health = {
462 .set = param_set_battery_health,
463 .get = param_get_battery_health,
464};
465
466static struct kernel_param_ops param_ops_battery_capacity = {
467 .set = param_set_battery_capacity,
468 .get = param_get_battery_capacity,
469};
470
85a392d4
DES
471static struct kernel_param_ops param_ops_battery_voltage = {
472 .set = param_set_battery_voltage,
473 .get = param_get_battery_voltage,
474};
f17ef9b2
JS
475
476#define param_check_ac_online(name, p) __param_check(name, p, void);
73847375 477#define param_check_usb_online(name, p) __param_check(name, p, void);
f17ef9b2
JS
478#define param_check_battery_status(name, p) __param_check(name, p, void);
479#define param_check_battery_present(name, p) __param_check(name, p, void);
480#define param_check_battery_technology(name, p) __param_check(name, p, void);
481#define param_check_battery_health(name, p) __param_check(name, p, void);
482#define param_check_battery_capacity(name, p) __param_check(name, p, void);
85a392d4 483#define param_check_battery_voltage(name, p) __param_check(name, p, void);
f17ef9b2
JS
484
485
486module_param(ac_online, ac_online, 0644);
487MODULE_PARM_DESC(ac_online, "AC charging state <on|off>");
488
73847375
DES
489module_param(usb_online, usb_online, 0644);
490MODULE_PARM_DESC(usb_online, "USB charging state <on|off>");
491
f17ef9b2
JS
492module_param(battery_status, battery_status, 0644);
493MODULE_PARM_DESC(battery_status,
494 "battery status <charging|discharging|not-charging|full>");
495
496module_param(battery_present, battery_present, 0644);
497MODULE_PARM_DESC(battery_present,
498 "battery presence state <good|overheat|dead|overvoltage|failure>");
499
500module_param(battery_technology, battery_technology, 0644);
501MODULE_PARM_DESC(battery_technology,
502 "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>");
503
504module_param(battery_health, battery_health, 0644);
505MODULE_PARM_DESC(battery_health,
506 "battery health state <good|overheat|dead|overvoltage|failure>");
507
508module_param(battery_capacity, battery_capacity, 0644);
509MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)");
510
85a392d4
DES
511module_param(battery_voltage, battery_voltage, 0644);
512MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)");
f17ef9b2 513
a1e50fd4
AV
514MODULE_DESCRIPTION("Power supply driver for testing");
515MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>");
516MODULE_LICENSE("GPL");