Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
a1e50fd4 AV |
2 | /* |
3 | * Power supply driver for testing. | |
4 | * | |
5 | * Copyright 2010 Anton Vorontsov <cbouatmailru@gmail.com> | |
6 | * | |
f17ef9b2 JS |
7 | * Dynamic module parameter code from the Virtual Battery Driver |
8 | * Copyright (C) 2008 Pylone, Inc. | |
9 | * By: Masashi YOKOTA <yokota@pylone.jp> | |
10 | * Originally found here: | |
11 | * http://downloads.pylone.jp/src/virtual_battery/virtual_battery-0.0.1.tar.bz2 | |
a1e50fd4 AV |
12 | */ |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/power_supply.h> | |
17 | #include <linux/errno.h> | |
18 | #include <linux/delay.h> | |
1c79031f | 19 | #include <generated/utsrelease.h> |
a1e50fd4 | 20 | |
a30067bb KK |
21 | enum test_power_id { |
22 | TEST_AC, | |
23 | TEST_BATTERY, | |
24 | TEST_USB, | |
25 | TEST_POWER_NUM, | |
26 | }; | |
27 | ||
f17ef9b2 | 28 | static int ac_online = 1; |
73847375 | 29 | static int usb_online = 1; |
f17ef9b2 JS |
30 | static int battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
31 | static int battery_health = POWER_SUPPLY_HEALTH_GOOD; | |
32 | static int battery_present = 1; /* true */ | |
33 | static int battery_technology = POWER_SUPPLY_TECHNOLOGY_LION; | |
34 | static int battery_capacity = 50; | |
85a392d4 | 35 | static int battery_voltage = 3300; |
4b082ac6 | 36 | static int battery_charge_counter = -1000; |
c365ee56 | 37 | static int battery_current = -1600; |
070c1470 TW |
38 | static enum power_supply_charge_behaviour battery_charge_behaviour = |
39 | POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO; | |
a1e50fd4 | 40 | |
9239ebcf AG |
41 | static bool module_initialized; |
42 | ||
a1e50fd4 AV |
43 | static int test_power_get_ac_property(struct power_supply *psy, |
44 | enum power_supply_property psp, | |
45 | union power_supply_propval *val) | |
46 | { | |
47 | switch (psp) { | |
48 | case POWER_SUPPLY_PROP_ONLINE: | |
f17ef9b2 | 49 | val->intval = ac_online; |
a1e50fd4 AV |
50 | break; |
51 | default: | |
52 | return -EINVAL; | |
53 | } | |
54 | return 0; | |
55 | } | |
56 | ||
73847375 DES |
57 | static int test_power_get_usb_property(struct power_supply *psy, |
58 | enum power_supply_property psp, | |
59 | union power_supply_propval *val) | |
60 | { | |
61 | switch (psp) { | |
62 | case POWER_SUPPLY_PROP_ONLINE: | |
63 | val->intval = usb_online; | |
64 | break; | |
65 | default: | |
66 | return -EINVAL; | |
67 | } | |
68 | return 0; | |
69 | } | |
70 | ||
a1e50fd4 AV |
71 | static int test_power_get_battery_property(struct power_supply *psy, |
72 | enum power_supply_property psp, | |
73 | union power_supply_propval *val) | |
74 | { | |
75 | switch (psp) { | |
76 | case POWER_SUPPLY_PROP_MODEL_NAME: | |
77 | val->strval = "Test battery"; | |
78 | break; | |
79 | case POWER_SUPPLY_PROP_MANUFACTURER: | |
80 | val->strval = "Linux"; | |
81 | break; | |
82 | case POWER_SUPPLY_PROP_SERIAL_NUMBER: | |
83 | val->strval = UTS_RELEASE; | |
84 | break; | |
85 | case POWER_SUPPLY_PROP_STATUS: | |
f17ef9b2 | 86 | val->intval = battery_status; |
a1e50fd4 AV |
87 | break; |
88 | case POWER_SUPPLY_PROP_CHARGE_TYPE: | |
89 | val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST; | |
90 | break; | |
91 | case POWER_SUPPLY_PROP_HEALTH: | |
f17ef9b2 JS |
92 | val->intval = battery_health; |
93 | break; | |
94 | case POWER_SUPPLY_PROP_PRESENT: | |
95 | val->intval = battery_present; | |
a1e50fd4 AV |
96 | break; |
97 | case POWER_SUPPLY_PROP_TECHNOLOGY: | |
f17ef9b2 | 98 | val->intval = battery_technology; |
a1e50fd4 AV |
99 | break; |
100 | case POWER_SUPPLY_PROP_CAPACITY_LEVEL: | |
101 | val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; | |
102 | break; | |
103 | case POWER_SUPPLY_PROP_CAPACITY: | |
f17ef9b2 JS |
104 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
105 | val->intval = battery_capacity; | |
106 | break; | |
4b082ac6 | 107 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
108 | val->intval = battery_charge_counter; | |
109 | break; | |
f17ef9b2 JS |
110 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
111 | case POWER_SUPPLY_PROP_CHARGE_FULL: | |
112 | val->intval = 100; | |
a1e50fd4 AV |
113 | break; |
114 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: | |
115 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: | |
116 | val->intval = 3600; | |
117 | break; | |
85a392d4 DES |
118 | case POWER_SUPPLY_PROP_TEMP: |
119 | val->intval = 26; | |
120 | break; | |
121 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: | |
122 | val->intval = battery_voltage; | |
123 | break; | |
69318b39 | 124 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
125 | case POWER_SUPPLY_PROP_CURRENT_NOW: | |
126 | val->intval = battery_current; | |
127 | break; | |
070c1470 TW |
128 | case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: |
129 | val->intval = battery_charge_behaviour; | |
130 | break; | |
a1e50fd4 AV |
131 | default: |
132 | pr_info("%s: some properties deliberately report errors.\n", | |
133 | __func__); | |
134 | return -EINVAL; | |
135 | } | |
136 | return 0; | |
137 | } | |
138 | ||
070c1470 TW |
139 | static int test_power_battery_property_is_writeable(struct power_supply *psy, |
140 | enum power_supply_property psp) | |
141 | { | |
142 | return psp == POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR; | |
143 | } | |
144 | ||
145 | static int test_power_set_battery_property(struct power_supply *psy, | |
146 | enum power_supply_property psp, | |
147 | const union power_supply_propval *val) | |
148 | { | |
149 | switch (psp) { | |
150 | case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR: | |
151 | if (val->intval < 0 || | |
152 | val->intval >= BITS_PER_TYPE(typeof(psy->desc->charge_behaviours)) || | |
153 | !(BIT(val->intval) & psy->desc->charge_behaviours)) { | |
154 | return -EINVAL; | |
155 | } | |
156 | battery_charge_behaviour = val->intval; | |
157 | break; | |
158 | default: | |
159 | return -EINVAL; | |
160 | } | |
161 | return 0; | |
162 | } | |
163 | ||
a1e50fd4 AV |
164 | static enum power_supply_property test_power_ac_props[] = { |
165 | POWER_SUPPLY_PROP_ONLINE, | |
166 | }; | |
167 | ||
168 | static enum power_supply_property test_power_battery_props[] = { | |
169 | POWER_SUPPLY_PROP_STATUS, | |
170 | POWER_SUPPLY_PROP_CHARGE_TYPE, | |
171 | POWER_SUPPLY_PROP_HEALTH, | |
f17ef9b2 | 172 | POWER_SUPPLY_PROP_PRESENT, |
a1e50fd4 | 173 | POWER_SUPPLY_PROP_TECHNOLOGY, |
f17ef9b2 | 174 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
a1e50fd4 | 175 | POWER_SUPPLY_PROP_CHARGE_FULL, |
f17ef9b2 | 176 | POWER_SUPPLY_PROP_CHARGE_NOW, |
4b082ac6 | 177 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
a1e50fd4 AV |
178 | POWER_SUPPLY_PROP_CAPACITY, |
179 | POWER_SUPPLY_PROP_CAPACITY_LEVEL, | |
180 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, | |
181 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, | |
182 | POWER_SUPPLY_PROP_MODEL_NAME, | |
183 | POWER_SUPPLY_PROP_MANUFACTURER, | |
184 | POWER_SUPPLY_PROP_SERIAL_NUMBER, | |
85a392d4 DES |
185 | POWER_SUPPLY_PROP_TEMP, |
186 | POWER_SUPPLY_PROP_VOLTAGE_NOW, | |
69318b39 | 187 | POWER_SUPPLY_PROP_CURRENT_AVG, |
188 | POWER_SUPPLY_PROP_CURRENT_NOW, | |
070c1470 | 189 | POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR, |
a1e50fd4 AV |
190 | }; |
191 | ||
192 | static char *test_power_ac_supplied_to[] = { | |
193 | "test_battery", | |
194 | }; | |
195 | ||
297d716f KK |
196 | static struct power_supply *test_power_supplies[TEST_POWER_NUM]; |
197 | ||
198 | static const struct power_supply_desc test_power_desc[] = { | |
a30067bb | 199 | [TEST_AC] = { |
a1e50fd4 AV |
200 | .name = "test_ac", |
201 | .type = POWER_SUPPLY_TYPE_MAINS, | |
a1e50fd4 AV |
202 | .properties = test_power_ac_props, |
203 | .num_properties = ARRAY_SIZE(test_power_ac_props), | |
204 | .get_property = test_power_get_ac_property, | |
a30067bb KK |
205 | }, |
206 | [TEST_BATTERY] = { | |
a1e50fd4 AV |
207 | .name = "test_battery", |
208 | .type = POWER_SUPPLY_TYPE_BATTERY, | |
209 | .properties = test_power_battery_props, | |
210 | .num_properties = ARRAY_SIZE(test_power_battery_props), | |
211 | .get_property = test_power_get_battery_property, | |
070c1470 TW |
212 | .set_property = test_power_set_battery_property, |
213 | .property_is_writeable = test_power_battery_property_is_writeable, | |
214 | .charge_behaviours = BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO) | |
215 | | BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE) | |
216 | | BIT(POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE), | |
a30067bb KK |
217 | }, |
218 | [TEST_USB] = { | |
73847375 DES |
219 | .name = "test_usb", |
220 | .type = POWER_SUPPLY_TYPE_USB, | |
73847375 DES |
221 | .properties = test_power_ac_props, |
222 | .num_properties = ARRAY_SIZE(test_power_ac_props), | |
223 | .get_property = test_power_get_usb_property, | |
a1e50fd4 AV |
224 | }, |
225 | }; | |
226 | ||
2dc9215d KK |
227 | static const struct power_supply_config test_power_configs[] = { |
228 | { | |
229 | /* test_ac */ | |
230 | .supplied_to = test_power_ac_supplied_to, | |
231 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), | |
232 | }, { | |
233 | /* test_battery */ | |
234 | }, { | |
235 | /* test_usb */ | |
236 | .supplied_to = test_power_ac_supplied_to, | |
237 | .num_supplicants = ARRAY_SIZE(test_power_ac_supplied_to), | |
238 | }, | |
239 | }; | |
f17ef9b2 | 240 | |
a1e50fd4 AV |
241 | static int __init test_power_init(void) |
242 | { | |
243 | int i; | |
244 | int ret; | |
245 | ||
a30067bb | 246 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_supplies)); |
2dc9215d | 247 | BUILD_BUG_ON(TEST_POWER_NUM != ARRAY_SIZE(test_power_configs)); |
a30067bb | 248 | |
a1e50fd4 | 249 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) { |
297d716f KK |
250 | test_power_supplies[i] = power_supply_register(NULL, |
251 | &test_power_desc[i], | |
2dc9215d | 252 | &test_power_configs[i]); |
297d716f | 253 | if (IS_ERR(test_power_supplies[i])) { |
a1e50fd4 | 254 | pr_err("%s: failed to register %s\n", __func__, |
297d716f KK |
255 | test_power_desc[i].name); |
256 | ret = PTR_ERR(test_power_supplies[i]); | |
a1e50fd4 AV |
257 | goto failed; |
258 | } | |
259 | } | |
260 | ||
9239ebcf | 261 | module_initialized = true; |
a1e50fd4 AV |
262 | return 0; |
263 | failed: | |
264 | while (--i >= 0) | |
297d716f | 265 | power_supply_unregister(test_power_supplies[i]); |
a1e50fd4 AV |
266 | return ret; |
267 | } | |
268 | module_init(test_power_init); | |
269 | ||
270 | static void __exit test_power_exit(void) | |
271 | { | |
272 | int i; | |
273 | ||
274 | /* Let's see how we handle changes... */ | |
f17ef9b2 | 275 | ac_online = 0; |
73847375 | 276 | usb_online = 0; |
f17ef9b2 | 277 | battery_status = POWER_SUPPLY_STATUS_DISCHARGING; |
a1e50fd4 | 278 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) |
297d716f | 279 | power_supply_changed(test_power_supplies[i]); |
a1e50fd4 AV |
280 | pr_info("%s: 'changed' event sent, sleeping for 10 seconds...\n", |
281 | __func__); | |
282 | ssleep(10); | |
283 | ||
284 | for (i = 0; i < ARRAY_SIZE(test_power_supplies); i++) | |
297d716f | 285 | power_supply_unregister(test_power_supplies[i]); |
9239ebcf AG |
286 | |
287 | module_initialized = false; | |
a1e50fd4 AV |
288 | } |
289 | module_exit(test_power_exit); | |
290 | ||
f17ef9b2 JS |
291 | |
292 | ||
293 | #define MAX_KEYLENGTH 256 | |
294 | struct battery_property_map { | |
295 | int value; | |
296 | char const *key; | |
297 | }; | |
298 | ||
299 | static struct battery_property_map map_ac_online[] = { | |
9239ebcf AG |
300 | { 0, "off" }, |
301 | { 1, "on" }, | |
f17ef9b2 JS |
302 | { -1, NULL }, |
303 | }; | |
304 | ||
305 | static struct battery_property_map map_status[] = { | |
306 | { POWER_SUPPLY_STATUS_CHARGING, "charging" }, | |
307 | { POWER_SUPPLY_STATUS_DISCHARGING, "discharging" }, | |
308 | { POWER_SUPPLY_STATUS_NOT_CHARGING, "not-charging" }, | |
309 | { POWER_SUPPLY_STATUS_FULL, "full" }, | |
310 | { -1, NULL }, | |
311 | }; | |
312 | ||
313 | static struct battery_property_map map_health[] = { | |
314 | { POWER_SUPPLY_HEALTH_GOOD, "good" }, | |
315 | { POWER_SUPPLY_HEALTH_OVERHEAT, "overheat" }, | |
316 | { POWER_SUPPLY_HEALTH_DEAD, "dead" }, | |
317 | { POWER_SUPPLY_HEALTH_OVERVOLTAGE, "overvoltage" }, | |
318 | { POWER_SUPPLY_HEALTH_UNSPEC_FAILURE, "failure" }, | |
319 | { -1, NULL }, | |
320 | }; | |
321 | ||
322 | static struct battery_property_map map_present[] = { | |
323 | { 0, "false" }, | |
324 | { 1, "true" }, | |
325 | { -1, NULL }, | |
326 | }; | |
327 | ||
328 | static struct battery_property_map map_technology[] = { | |
329 | { POWER_SUPPLY_TECHNOLOGY_NiMH, "NiMH" }, | |
330 | { POWER_SUPPLY_TECHNOLOGY_LION, "LION" }, | |
331 | { POWER_SUPPLY_TECHNOLOGY_LIPO, "LIPO" }, | |
332 | { POWER_SUPPLY_TECHNOLOGY_LiFe, "LiFe" }, | |
333 | { POWER_SUPPLY_TECHNOLOGY_NiCd, "NiCd" }, | |
334 | { POWER_SUPPLY_TECHNOLOGY_LiMn, "LiMn" }, | |
335 | { -1, NULL }, | |
336 | }; | |
337 | ||
338 | ||
339 | static int map_get_value(struct battery_property_map *map, const char *key, | |
340 | int def_val) | |
341 | { | |
342 | char buf[MAX_KEYLENGTH]; | |
343 | int cr; | |
344 | ||
3639dbd7 | 345 | strscpy(buf, key, MAX_KEYLENGTH); |
f17ef9b2 JS |
346 | |
347 | cr = strnlen(buf, MAX_KEYLENGTH) - 1; | |
6b9140f3 SL |
348 | if (cr < 0) |
349 | return def_val; | |
f17ef9b2 JS |
350 | if (buf[cr] == '\n') |
351 | buf[cr] = '\0'; | |
352 | ||
353 | while (map->key) { | |
354 | if (strncasecmp(map->key, buf, MAX_KEYLENGTH) == 0) | |
355 | return map->value; | |
356 | map++; | |
357 | } | |
358 | ||
359 | return def_val; | |
360 | } | |
361 | ||
362 | ||
363 | static const char *map_get_key(struct battery_property_map *map, int value, | |
364 | const char *def_key) | |
365 | { | |
366 | while (map->key) { | |
367 | if (map->value == value) | |
368 | return map->key; | |
369 | map++; | |
370 | } | |
371 | ||
372 | return def_key; | |
373 | } | |
374 | ||
9239ebcf AG |
375 | static inline void signal_power_supply_changed(struct power_supply *psy) |
376 | { | |
377 | if (module_initialized) | |
378 | power_supply_changed(psy); | |
379 | } | |
380 | ||
f17ef9b2 JS |
381 | static int param_set_ac_online(const char *key, const struct kernel_param *kp) |
382 | { | |
383 | ac_online = map_get_value(map_ac_online, key, ac_online); | |
297d716f | 384 | signal_power_supply_changed(test_power_supplies[TEST_AC]); |
f17ef9b2 JS |
385 | return 0; |
386 | } | |
387 | ||
388 | static int param_get_ac_online(char *buffer, const struct kernel_param *kp) | |
389 | { | |
411643e9 HL |
390 | return sprintf(buffer, "%s\n", |
391 | map_get_key(map_ac_online, ac_online, "unknown")); | |
f17ef9b2 JS |
392 | } |
393 | ||
73847375 DES |
394 | static int param_set_usb_online(const char *key, const struct kernel_param *kp) |
395 | { | |
396 | usb_online = map_get_value(map_ac_online, key, usb_online); | |
297d716f | 397 | signal_power_supply_changed(test_power_supplies[TEST_USB]); |
73847375 DES |
398 | return 0; |
399 | } | |
400 | ||
401 | static int param_get_usb_online(char *buffer, const struct kernel_param *kp) | |
402 | { | |
411643e9 HL |
403 | return sprintf(buffer, "%s\n", |
404 | map_get_key(map_ac_online, usb_online, "unknown")); | |
73847375 DES |
405 | } |
406 | ||
f17ef9b2 JS |
407 | static int param_set_battery_status(const char *key, |
408 | const struct kernel_param *kp) | |
409 | { | |
410 | battery_status = map_get_value(map_status, key, battery_status); | |
297d716f | 411 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
f17ef9b2 JS |
412 | return 0; |
413 | } | |
414 | ||
415 | static int param_get_battery_status(char *buffer, const struct kernel_param *kp) | |
416 | { | |
411643e9 HL |
417 | return sprintf(buffer, "%s\n", |
418 | map_get_key(map_ac_online, battery_status, "unknown")); | |
f17ef9b2 JS |
419 | } |
420 | ||
421 | static int param_set_battery_health(const char *key, | |
422 | const struct kernel_param *kp) | |
423 | { | |
424 | battery_health = map_get_value(map_health, key, battery_health); | |
297d716f | 425 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
f17ef9b2 JS |
426 | return 0; |
427 | } | |
428 | ||
429 | static int param_get_battery_health(char *buffer, const struct kernel_param *kp) | |
430 | { | |
411643e9 HL |
431 | return sprintf(buffer, "%s\n", |
432 | map_get_key(map_ac_online, battery_health, "unknown")); | |
f17ef9b2 JS |
433 | } |
434 | ||
435 | static int param_set_battery_present(const char *key, | |
436 | const struct kernel_param *kp) | |
437 | { | |
438 | battery_present = map_get_value(map_present, key, battery_present); | |
297d716f | 439 | signal_power_supply_changed(test_power_supplies[TEST_AC]); |
f17ef9b2 JS |
440 | return 0; |
441 | } | |
442 | ||
443 | static int param_get_battery_present(char *buffer, | |
444 | const struct kernel_param *kp) | |
445 | { | |
411643e9 HL |
446 | return sprintf(buffer, "%s\n", |
447 | map_get_key(map_ac_online, battery_present, "unknown")); | |
f17ef9b2 JS |
448 | } |
449 | ||
450 | static int param_set_battery_technology(const char *key, | |
451 | const struct kernel_param *kp) | |
452 | { | |
453 | battery_technology = map_get_value(map_technology, key, | |
454 | battery_technology); | |
297d716f | 455 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
f17ef9b2 JS |
456 | return 0; |
457 | } | |
458 | ||
459 | static int param_get_battery_technology(char *buffer, | |
460 | const struct kernel_param *kp) | |
461 | { | |
411643e9 HL |
462 | return sprintf(buffer, "%s\n", |
463 | map_get_key(map_ac_online, battery_technology, | |
464 | "unknown")); | |
f17ef9b2 JS |
465 | } |
466 | ||
467 | static int param_set_battery_capacity(const char *key, | |
468 | const struct kernel_param *kp) | |
469 | { | |
470 | int tmp; | |
471 | ||
472 | if (1 != sscanf(key, "%d", &tmp)) | |
473 | return -EINVAL; | |
474 | ||
475 | battery_capacity = tmp; | |
297d716f | 476 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
f17ef9b2 JS |
477 | return 0; |
478 | } | |
479 | ||
480 | #define param_get_battery_capacity param_get_int | |
481 | ||
85a392d4 DES |
482 | static int param_set_battery_voltage(const char *key, |
483 | const struct kernel_param *kp) | |
484 | { | |
485 | int tmp; | |
486 | ||
487 | if (1 != sscanf(key, "%d", &tmp)) | |
488 | return -EINVAL; | |
489 | ||
490 | battery_voltage = tmp; | |
297d716f | 491 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); |
85a392d4 DES |
492 | return 0; |
493 | } | |
494 | ||
495 | #define param_get_battery_voltage param_get_int | |
496 | ||
4b082ac6 | 497 | static int param_set_battery_charge_counter(const char *key, |
498 | const struct kernel_param *kp) | |
499 | { | |
500 | int tmp; | |
501 | ||
502 | if (1 != sscanf(key, "%d", &tmp)) | |
503 | return -EINVAL; | |
504 | ||
505 | battery_charge_counter = tmp; | |
506 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); | |
507 | return 0; | |
508 | } | |
509 | ||
510 | #define param_get_battery_charge_counter param_get_int | |
511 | ||
69318b39 | 512 | static int param_set_battery_current(const char *key, |
513 | const struct kernel_param *kp) | |
514 | { | |
515 | int tmp; | |
516 | ||
517 | if (1 != sscanf(key, "%d", &tmp)) | |
518 | return -EINVAL; | |
519 | ||
520 | battery_current = tmp; | |
521 | signal_power_supply_changed(test_power_supplies[TEST_BATTERY]); | |
522 | return 0; | |
523 | } | |
524 | ||
525 | #define param_get_battery_current param_get_int | |
526 | ||
9c27847d | 527 | static const struct kernel_param_ops param_ops_ac_online = { |
f17ef9b2 JS |
528 | .set = param_set_ac_online, |
529 | .get = param_get_ac_online, | |
530 | }; | |
531 | ||
9c27847d | 532 | static const struct kernel_param_ops param_ops_usb_online = { |
73847375 DES |
533 | .set = param_set_usb_online, |
534 | .get = param_get_usb_online, | |
535 | }; | |
536 | ||
9c27847d | 537 | static const struct kernel_param_ops param_ops_battery_status = { |
f17ef9b2 JS |
538 | .set = param_set_battery_status, |
539 | .get = param_get_battery_status, | |
540 | }; | |
541 | ||
9c27847d | 542 | static const struct kernel_param_ops param_ops_battery_present = { |
f17ef9b2 JS |
543 | .set = param_set_battery_present, |
544 | .get = param_get_battery_present, | |
545 | }; | |
546 | ||
9c27847d | 547 | static const struct kernel_param_ops param_ops_battery_technology = { |
f17ef9b2 JS |
548 | .set = param_set_battery_technology, |
549 | .get = param_get_battery_technology, | |
550 | }; | |
551 | ||
9c27847d | 552 | static const struct kernel_param_ops param_ops_battery_health = { |
f17ef9b2 JS |
553 | .set = param_set_battery_health, |
554 | .get = param_get_battery_health, | |
555 | }; | |
556 | ||
9c27847d | 557 | static const struct kernel_param_ops param_ops_battery_capacity = { |
f17ef9b2 JS |
558 | .set = param_set_battery_capacity, |
559 | .get = param_get_battery_capacity, | |
560 | }; | |
561 | ||
9c27847d | 562 | static const struct kernel_param_ops param_ops_battery_voltage = { |
85a392d4 DES |
563 | .set = param_set_battery_voltage, |
564 | .get = param_get_battery_voltage, | |
565 | }; | |
f17ef9b2 | 566 | |
4b082ac6 | 567 | static const struct kernel_param_ops param_ops_battery_charge_counter = { |
568 | .set = param_set_battery_charge_counter, | |
569 | .get = param_get_battery_charge_counter, | |
570 | }; | |
571 | ||
69318b39 | 572 | static const struct kernel_param_ops param_ops_battery_current = { |
573 | .set = param_set_battery_current, | |
574 | .get = param_get_battery_current, | |
575 | }; | |
576 | ||
f17ef9b2 | 577 | #define param_check_ac_online(name, p) __param_check(name, p, void); |
73847375 | 578 | #define param_check_usb_online(name, p) __param_check(name, p, void); |
f17ef9b2 JS |
579 | #define param_check_battery_status(name, p) __param_check(name, p, void); |
580 | #define param_check_battery_present(name, p) __param_check(name, p, void); | |
581 | #define param_check_battery_technology(name, p) __param_check(name, p, void); | |
582 | #define param_check_battery_health(name, p) __param_check(name, p, void); | |
583 | #define param_check_battery_capacity(name, p) __param_check(name, p, void); | |
85a392d4 | 584 | #define param_check_battery_voltage(name, p) __param_check(name, p, void); |
4b082ac6 | 585 | #define param_check_battery_charge_counter(name, p) __param_check(name, p, void); |
69318b39 | 586 | #define param_check_battery_current(name, p) __param_check(name, p, void); |
f17ef9b2 JS |
587 | |
588 | ||
589 | module_param(ac_online, ac_online, 0644); | |
590 | MODULE_PARM_DESC(ac_online, "AC charging state <on|off>"); | |
591 | ||
73847375 DES |
592 | module_param(usb_online, usb_online, 0644); |
593 | MODULE_PARM_DESC(usb_online, "USB charging state <on|off>"); | |
594 | ||
f17ef9b2 JS |
595 | module_param(battery_status, battery_status, 0644); |
596 | MODULE_PARM_DESC(battery_status, | |
597 | "battery status <charging|discharging|not-charging|full>"); | |
598 | ||
599 | module_param(battery_present, battery_present, 0644); | |
600 | MODULE_PARM_DESC(battery_present, | |
601 | "battery presence state <good|overheat|dead|overvoltage|failure>"); | |
602 | ||
603 | module_param(battery_technology, battery_technology, 0644); | |
604 | MODULE_PARM_DESC(battery_technology, | |
605 | "battery technology <NiMH|LION|LIPO|LiFe|NiCd|LiMn>"); | |
606 | ||
607 | module_param(battery_health, battery_health, 0644); | |
608 | MODULE_PARM_DESC(battery_health, | |
609 | "battery health state <good|overheat|dead|overvoltage|failure>"); | |
610 | ||
611 | module_param(battery_capacity, battery_capacity, 0644); | |
612 | MODULE_PARM_DESC(battery_capacity, "battery capacity (percentage)"); | |
613 | ||
85a392d4 DES |
614 | module_param(battery_voltage, battery_voltage, 0644); |
615 | MODULE_PARM_DESC(battery_voltage, "battery voltage (millivolts)"); | |
f17ef9b2 | 616 | |
4b082ac6 | 617 | module_param(battery_charge_counter, battery_charge_counter, 0644); |
618 | MODULE_PARM_DESC(battery_charge_counter, | |
619 | "battery charge counter (microampere-hours)"); | |
620 | ||
69318b39 | 621 | module_param(battery_current, battery_current, 0644); |
622 | MODULE_PARM_DESC(battery_current, "battery current (milliampere)"); | |
623 | ||
a1e50fd4 AV |
624 | MODULE_DESCRIPTION("Power supply driver for testing"); |
625 | MODULE_AUTHOR("Anton Vorontsov <cbouatmailru@gmail.com>"); | |
626 | MODULE_LICENSE("GPL"); |