battery.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. /*
  2. * battery.c - ACPI Battery Driver (Revision: 2.0)
  3. *
  4. * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
  6. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  7. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  24. *
  25. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/async.h>
  33. #include <linux/dmi.h>
  34. #include <linux/slab.h>
  35. #ifdef CONFIG_ACPI_PROCFS_POWER
  36. #include <linux/proc_fs.h>
  37. #include <linux/seq_file.h>
  38. #include <asm/uaccess.h>
  39. #endif
  40. #include <acpi/acpi_bus.h>
  41. #include <acpi/acpi_drivers.h>
  42. #include <linux/power_supply.h>
  43. #define PREFIX "ACPI: "
  44. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  45. #define ACPI_BATTERY_CLASS "battery"
  46. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  47. #define ACPI_BATTERY_NOTIFY_STATUS 0x80
  48. #define ACPI_BATTERY_NOTIFY_INFO 0x81
  49. #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
  50. #define _COMPONENT ACPI_BATTERY_COMPONENT
  51. ACPI_MODULE_NAME("battery");
  52. MODULE_AUTHOR("Paul Diefenbaugh");
  53. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  54. MODULE_DESCRIPTION("ACPI Battery Driver");
  55. MODULE_LICENSE("GPL");
  56. static unsigned int cache_time = 1000;
  57. module_param(cache_time, uint, 0644);
  58. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  59. #ifdef CONFIG_ACPI_PROCFS_POWER
  60. extern struct proc_dir_entry *acpi_lock_battery_dir(void);
  61. extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
  62. enum acpi_battery_files {
  63. info_tag = 0,
  64. state_tag,
  65. alarm_tag,
  66. ACPI_BATTERY_NUMFILES,
  67. };
  68. #endif
  69. static const struct acpi_device_id battery_device_ids[] = {
  70. {"PNP0C0A", 0},
  71. {"", 0},
  72. };
  73. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  74. enum {
  75. ACPI_BATTERY_ALARM_PRESENT,
  76. ACPI_BATTERY_XINFO_PRESENT,
  77. /* For buggy DSDTs that report negative 16-bit values for either
  78. * charging or discharging current and/or report 0 as 65536
  79. * due to bad math.
  80. */
  81. ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
  82. };
  83. struct acpi_battery {
  84. struct mutex lock;
  85. struct power_supply bat;
  86. struct acpi_device *device;
  87. unsigned long update_time;
  88. int rate_now;
  89. int capacity_now;
  90. int voltage_now;
  91. int design_capacity;
  92. int full_charge_capacity;
  93. int technology;
  94. int design_voltage;
  95. int design_capacity_warning;
  96. int design_capacity_low;
  97. int cycle_count;
  98. int measurement_accuracy;
  99. int max_sampling_time;
  100. int min_sampling_time;
  101. int max_averaging_interval;
  102. int min_averaging_interval;
  103. int capacity_granularity_1;
  104. int capacity_granularity_2;
  105. int alarm;
  106. char model_number[32];
  107. char serial_number[32];
  108. char type[32];
  109. char oem_info[32];
  110. int state;
  111. int power_unit;
  112. unsigned long flags;
  113. };
  114. #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
  115. inline int acpi_battery_present(struct acpi_battery *battery)
  116. {
  117. return battery->device->status.battery_present;
  118. }
  119. static int acpi_battery_technology(struct acpi_battery *battery)
  120. {
  121. if (!strcasecmp("NiCd", battery->type))
  122. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  123. if (!strcasecmp("NiMH", battery->type))
  124. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  125. if (!strcasecmp("LION", battery->type))
  126. return POWER_SUPPLY_TECHNOLOGY_LION;
  127. if (!strncasecmp("LI-ION", battery->type, 6))
  128. return POWER_SUPPLY_TECHNOLOGY_LION;
  129. if (!strcasecmp("LiP", battery->type))
  130. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  131. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  132. }
  133. static int acpi_battery_get_state(struct acpi_battery *battery);
  134. static int acpi_battery_is_charged(struct acpi_battery *battery)
  135. {
  136. /* either charging or discharging */
  137. if (battery->state != 0)
  138. return 0;
  139. /* battery not reporting charge */
  140. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
  141. battery->capacity_now == 0)
  142. return 0;
  143. /* good batteries update full_charge as the batteries degrade */
  144. if (battery->full_charge_capacity == battery->capacity_now)
  145. return 1;
  146. /* fallback to using design values for broken batteries */
  147. if (battery->design_capacity == battery->capacity_now)
  148. return 1;
  149. /* we don't do any sort of metric based on percentages */
  150. return 0;
  151. }
  152. static int acpi_battery_get_property(struct power_supply *psy,
  153. enum power_supply_property psp,
  154. union power_supply_propval *val)
  155. {
  156. struct acpi_battery *battery = to_acpi_battery(psy);
  157. if (acpi_battery_present(battery)) {
  158. /* run battery update only if it is present */
  159. acpi_battery_get_state(battery);
  160. } else if (psp != POWER_SUPPLY_PROP_PRESENT)
  161. return -ENODEV;
  162. switch (psp) {
  163. case POWER_SUPPLY_PROP_STATUS:
  164. if (battery->state & 0x01)
  165. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  166. else if (battery->state & 0x02)
  167. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  168. else if (acpi_battery_is_charged(battery))
  169. val->intval = POWER_SUPPLY_STATUS_FULL;
  170. else
  171. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  172. break;
  173. case POWER_SUPPLY_PROP_PRESENT:
  174. val->intval = acpi_battery_present(battery);
  175. break;
  176. case POWER_SUPPLY_PROP_TECHNOLOGY:
  177. val->intval = acpi_battery_technology(battery);
  178. break;
  179. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  180. val->intval = battery->cycle_count;
  181. break;
  182. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  183. val->intval = battery->design_voltage * 1000;
  184. break;
  185. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  186. val->intval = battery->voltage_now * 1000;
  187. break;
  188. case POWER_SUPPLY_PROP_CURRENT_NOW:
  189. case POWER_SUPPLY_PROP_POWER_NOW:
  190. val->intval = battery->rate_now * 1000;
  191. break;
  192. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  193. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  194. val->intval = battery->design_capacity * 1000;
  195. break;
  196. case POWER_SUPPLY_PROP_CHARGE_FULL:
  197. case POWER_SUPPLY_PROP_ENERGY_FULL:
  198. val->intval = battery->full_charge_capacity * 1000;
  199. break;
  200. case POWER_SUPPLY_PROP_CHARGE_NOW:
  201. case POWER_SUPPLY_PROP_ENERGY_NOW:
  202. val->intval = battery->capacity_now * 1000;
  203. break;
  204. case POWER_SUPPLY_PROP_MODEL_NAME:
  205. val->strval = battery->model_number;
  206. break;
  207. case POWER_SUPPLY_PROP_MANUFACTURER:
  208. val->strval = battery->oem_info;
  209. break;
  210. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  211. val->strval = battery->serial_number;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. return 0;
  217. }
  218. static enum power_supply_property charge_battery_props[] = {
  219. POWER_SUPPLY_PROP_STATUS,
  220. POWER_SUPPLY_PROP_PRESENT,
  221. POWER_SUPPLY_PROP_TECHNOLOGY,
  222. POWER_SUPPLY_PROP_CYCLE_COUNT,
  223. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  224. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  225. POWER_SUPPLY_PROP_CURRENT_NOW,
  226. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  227. POWER_SUPPLY_PROP_CHARGE_FULL,
  228. POWER_SUPPLY_PROP_CHARGE_NOW,
  229. POWER_SUPPLY_PROP_MODEL_NAME,
  230. POWER_SUPPLY_PROP_MANUFACTURER,
  231. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  232. };
  233. static enum power_supply_property energy_battery_props[] = {
  234. POWER_SUPPLY_PROP_STATUS,
  235. POWER_SUPPLY_PROP_PRESENT,
  236. POWER_SUPPLY_PROP_TECHNOLOGY,
  237. POWER_SUPPLY_PROP_CYCLE_COUNT,
  238. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  239. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  240. POWER_SUPPLY_PROP_POWER_NOW,
  241. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  242. POWER_SUPPLY_PROP_ENERGY_FULL,
  243. POWER_SUPPLY_PROP_ENERGY_NOW,
  244. POWER_SUPPLY_PROP_MODEL_NAME,
  245. POWER_SUPPLY_PROP_MANUFACTURER,
  246. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  247. };
  248. #ifdef CONFIG_ACPI_PROCFS_POWER
  249. inline char *acpi_battery_units(struct acpi_battery *battery)
  250. {
  251. return (battery->power_unit)?"mA":"mW";
  252. }
  253. #endif
  254. /* --------------------------------------------------------------------------
  255. Battery Management
  256. -------------------------------------------------------------------------- */
  257. struct acpi_offsets {
  258. size_t offset; /* offset inside struct acpi_sbs_battery */
  259. u8 mode; /* int or string? */
  260. };
  261. static struct acpi_offsets state_offsets[] = {
  262. {offsetof(struct acpi_battery, state), 0},
  263. {offsetof(struct acpi_battery, rate_now), 0},
  264. {offsetof(struct acpi_battery, capacity_now), 0},
  265. {offsetof(struct acpi_battery, voltage_now), 0},
  266. };
  267. static struct acpi_offsets info_offsets[] = {
  268. {offsetof(struct acpi_battery, power_unit), 0},
  269. {offsetof(struct acpi_battery, design_capacity), 0},
  270. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  271. {offsetof(struct acpi_battery, technology), 0},
  272. {offsetof(struct acpi_battery, design_voltage), 0},
  273. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  274. {offsetof(struct acpi_battery, design_capacity_low), 0},
  275. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  276. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  277. {offsetof(struct acpi_battery, model_number), 1},
  278. {offsetof(struct acpi_battery, serial_number), 1},
  279. {offsetof(struct acpi_battery, type), 1},
  280. {offsetof(struct acpi_battery, oem_info), 1},
  281. };
  282. static struct acpi_offsets extended_info_offsets[] = {
  283. {offsetof(struct acpi_battery, power_unit), 0},
  284. {offsetof(struct acpi_battery, design_capacity), 0},
  285. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  286. {offsetof(struct acpi_battery, technology), 0},
  287. {offsetof(struct acpi_battery, design_voltage), 0},
  288. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  289. {offsetof(struct acpi_battery, design_capacity_low), 0},
  290. {offsetof(struct acpi_battery, cycle_count), 0},
  291. {offsetof(struct acpi_battery, measurement_accuracy), 0},
  292. {offsetof(struct acpi_battery, max_sampling_time), 0},
  293. {offsetof(struct acpi_battery, min_sampling_time), 0},
  294. {offsetof(struct acpi_battery, max_averaging_interval), 0},
  295. {offsetof(struct acpi_battery, min_averaging_interval), 0},
  296. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  297. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  298. {offsetof(struct acpi_battery, model_number), 1},
  299. {offsetof(struct acpi_battery, serial_number), 1},
  300. {offsetof(struct acpi_battery, type), 1},
  301. {offsetof(struct acpi_battery, oem_info), 1},
  302. };
  303. static int extract_package(struct acpi_battery *battery,
  304. union acpi_object *package,
  305. struct acpi_offsets *offsets, int num)
  306. {
  307. int i;
  308. union acpi_object *element;
  309. if (package->type != ACPI_TYPE_PACKAGE)
  310. return -EFAULT;
  311. for (i = 0; i < num; ++i) {
  312. if (package->package.count <= i)
  313. return -EFAULT;
  314. element = &package->package.elements[i];
  315. if (offsets[i].mode) {
  316. u8 *ptr = (u8 *)battery + offsets[i].offset;
  317. if (element->type == ACPI_TYPE_STRING ||
  318. element->type == ACPI_TYPE_BUFFER)
  319. strncpy(ptr, element->string.pointer, 32);
  320. else if (element->type == ACPI_TYPE_INTEGER) {
  321. strncpy(ptr, (u8 *)&element->integer.value,
  322. sizeof(u64));
  323. ptr[sizeof(u64)] = 0;
  324. } else
  325. *ptr = 0; /* don't have value */
  326. } else {
  327. int *x = (int *)((u8 *)battery + offsets[i].offset);
  328. *x = (element->type == ACPI_TYPE_INTEGER) ?
  329. element->integer.value : -1;
  330. }
  331. }
  332. return 0;
  333. }
  334. static int acpi_battery_get_status(struct acpi_battery *battery)
  335. {
  336. if (acpi_bus_get_status(battery->device)) {
  337. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  338. return -ENODEV;
  339. }
  340. return 0;
  341. }
  342. static int acpi_battery_get_info(struct acpi_battery *battery)
  343. {
  344. int result = -EFAULT;
  345. acpi_status status = 0;
  346. char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
  347. "_BIX" : "_BIF";
  348. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  349. if (!acpi_battery_present(battery))
  350. return 0;
  351. mutex_lock(&battery->lock);
  352. status = acpi_evaluate_object(battery->device->handle, name,
  353. NULL, &buffer);
  354. mutex_unlock(&battery->lock);
  355. if (ACPI_FAILURE(status)) {
  356. ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
  357. return -ENODEV;
  358. }
  359. if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
  360. result = extract_package(battery, buffer.pointer,
  361. extended_info_offsets,
  362. ARRAY_SIZE(extended_info_offsets));
  363. else
  364. result = extract_package(battery, buffer.pointer,
  365. info_offsets, ARRAY_SIZE(info_offsets));
  366. kfree(buffer.pointer);
  367. return result;
  368. }
  369. static int acpi_battery_get_state(struct acpi_battery *battery)
  370. {
  371. int result = 0;
  372. acpi_status status = 0;
  373. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  374. if (!acpi_battery_present(battery))
  375. return 0;
  376. if (battery->update_time &&
  377. time_before(jiffies, battery->update_time +
  378. msecs_to_jiffies(cache_time)))
  379. return 0;
  380. mutex_lock(&battery->lock);
  381. status = acpi_evaluate_object(battery->device->handle, "_BST",
  382. NULL, &buffer);
  383. mutex_unlock(&battery->lock);
  384. if (ACPI_FAILURE(status)) {
  385. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  386. return -ENODEV;
  387. }
  388. result = extract_package(battery, buffer.pointer,
  389. state_offsets, ARRAY_SIZE(state_offsets));
  390. battery->update_time = jiffies;
  391. kfree(buffer.pointer);
  392. if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
  393. battery->rate_now != -1)
  394. battery->rate_now = abs((s16)battery->rate_now);
  395. return result;
  396. }
  397. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  398. {
  399. acpi_status status = 0;
  400. union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
  401. struct acpi_object_list arg_list = { 1, &arg0 };
  402. if (!acpi_battery_present(battery) ||
  403. !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
  404. return -ENODEV;
  405. arg0.integer.value = battery->alarm;
  406. mutex_lock(&battery->lock);
  407. status = acpi_evaluate_object(battery->device->handle, "_BTP",
  408. &arg_list, NULL);
  409. mutex_unlock(&battery->lock);
  410. if (ACPI_FAILURE(status))
  411. return -ENODEV;
  412. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  413. return 0;
  414. }
  415. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  416. {
  417. acpi_status status = AE_OK;
  418. acpi_handle handle = NULL;
  419. /* See if alarms are supported, and if so, set default */
  420. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  421. if (ACPI_FAILURE(status)) {
  422. clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  423. return 0;
  424. }
  425. set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  426. if (!battery->alarm)
  427. battery->alarm = battery->design_capacity_warning;
  428. return acpi_battery_set_alarm(battery);
  429. }
  430. static ssize_t acpi_battery_alarm_show(struct device *dev,
  431. struct device_attribute *attr,
  432. char *buf)
  433. {
  434. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  435. return sprintf(buf, "%d\n", battery->alarm * 1000);
  436. }
  437. static ssize_t acpi_battery_alarm_store(struct device *dev,
  438. struct device_attribute *attr,
  439. const char *buf, size_t count)
  440. {
  441. unsigned long x;
  442. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  443. if (sscanf(buf, "%ld\n", &x) == 1)
  444. battery->alarm = x/1000;
  445. if (acpi_battery_present(battery))
  446. acpi_battery_set_alarm(battery);
  447. return count;
  448. }
  449. static struct device_attribute alarm_attr = {
  450. .attr = {.name = "alarm", .mode = 0644},
  451. .show = acpi_battery_alarm_show,
  452. .store = acpi_battery_alarm_store,
  453. };
  454. static int sysfs_add_battery(struct acpi_battery *battery)
  455. {
  456. int result;
  457. if (battery->power_unit) {
  458. battery->bat.properties = charge_battery_props;
  459. battery->bat.num_properties =
  460. ARRAY_SIZE(charge_battery_props);
  461. } else {
  462. battery->bat.properties = energy_battery_props;
  463. battery->bat.num_properties =
  464. ARRAY_SIZE(energy_battery_props);
  465. }
  466. battery->bat.name = acpi_device_bid(battery->device);
  467. battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  468. battery->bat.get_property = acpi_battery_get_property;
  469. result = power_supply_register(&battery->device->dev, &battery->bat);
  470. if (result)
  471. return result;
  472. return device_create_file(battery->bat.dev, &alarm_attr);
  473. }
  474. static void sysfs_remove_battery(struct acpi_battery *battery)
  475. {
  476. if (!battery->bat.dev)
  477. return;
  478. device_remove_file(battery->bat.dev, &alarm_attr);
  479. power_supply_unregister(&battery->bat);
  480. battery->bat.dev = NULL;
  481. }
  482. static void acpi_battery_quirks(struct acpi_battery *battery)
  483. {
  484. if (dmi_name_in_vendors("Acer") && battery->power_unit) {
  485. set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
  486. }
  487. }
  488. static int acpi_battery_update(struct acpi_battery *battery)
  489. {
  490. int result, old_present = acpi_battery_present(battery);
  491. result = acpi_battery_get_status(battery);
  492. if (result)
  493. return result;
  494. if (!acpi_battery_present(battery)) {
  495. sysfs_remove_battery(battery);
  496. battery->update_time = 0;
  497. return 0;
  498. }
  499. if (!battery->update_time ||
  500. old_present != acpi_battery_present(battery)) {
  501. result = acpi_battery_get_info(battery);
  502. if (result)
  503. return result;
  504. acpi_battery_quirks(battery);
  505. acpi_battery_init_alarm(battery);
  506. }
  507. if (!battery->bat.dev)
  508. sysfs_add_battery(battery);
  509. return acpi_battery_get_state(battery);
  510. }
  511. /* --------------------------------------------------------------------------
  512. FS Interface (/proc)
  513. -------------------------------------------------------------------------- */
  514. #ifdef CONFIG_ACPI_PROCFS_POWER
  515. static struct proc_dir_entry *acpi_battery_dir;
  516. static int acpi_battery_print_info(struct seq_file *seq, int result)
  517. {
  518. struct acpi_battery *battery = seq->private;
  519. if (result)
  520. goto end;
  521. seq_printf(seq, "present: %s\n",
  522. acpi_battery_present(battery)?"yes":"no");
  523. if (!acpi_battery_present(battery))
  524. goto end;
  525. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  526. seq_printf(seq, "design capacity: unknown\n");
  527. else
  528. seq_printf(seq, "design capacity: %d %sh\n",
  529. battery->design_capacity,
  530. acpi_battery_units(battery));
  531. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  532. seq_printf(seq, "last full capacity: unknown\n");
  533. else
  534. seq_printf(seq, "last full capacity: %d %sh\n",
  535. battery->full_charge_capacity,
  536. acpi_battery_units(battery));
  537. seq_printf(seq, "battery technology: %srechargeable\n",
  538. (!battery->technology)?"non-":"");
  539. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  540. seq_printf(seq, "design voltage: unknown\n");
  541. else
  542. seq_printf(seq, "design voltage: %d mV\n",
  543. battery->design_voltage);
  544. seq_printf(seq, "design capacity warning: %d %sh\n",
  545. battery->design_capacity_warning,
  546. acpi_battery_units(battery));
  547. seq_printf(seq, "design capacity low: %d %sh\n",
  548. battery->design_capacity_low,
  549. acpi_battery_units(battery));
  550. seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
  551. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  552. battery->capacity_granularity_1,
  553. acpi_battery_units(battery));
  554. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  555. battery->capacity_granularity_2,
  556. acpi_battery_units(battery));
  557. seq_printf(seq, "model number: %s\n", battery->model_number);
  558. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  559. seq_printf(seq, "battery type: %s\n", battery->type);
  560. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  561. end:
  562. if (result)
  563. seq_printf(seq, "ERROR: Unable to read battery info\n");
  564. return result;
  565. }
  566. static int acpi_battery_print_state(struct seq_file *seq, int result)
  567. {
  568. struct acpi_battery *battery = seq->private;
  569. if (result)
  570. goto end;
  571. seq_printf(seq, "present: %s\n",
  572. acpi_battery_present(battery)?"yes":"no");
  573. if (!acpi_battery_present(battery))
  574. goto end;
  575. seq_printf(seq, "capacity state: %s\n",
  576. (battery->state & 0x04)?"critical":"ok");
  577. if ((battery->state & 0x01) && (battery->state & 0x02))
  578. seq_printf(seq,
  579. "charging state: charging/discharging\n");
  580. else if (battery->state & 0x01)
  581. seq_printf(seq, "charging state: discharging\n");
  582. else if (battery->state & 0x02)
  583. seq_printf(seq, "charging state: charging\n");
  584. else
  585. seq_printf(seq, "charging state: charged\n");
  586. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  587. seq_printf(seq, "present rate: unknown\n");
  588. else
  589. seq_printf(seq, "present rate: %d %s\n",
  590. battery->rate_now, acpi_battery_units(battery));
  591. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  592. seq_printf(seq, "remaining capacity: unknown\n");
  593. else
  594. seq_printf(seq, "remaining capacity: %d %sh\n",
  595. battery->capacity_now, acpi_battery_units(battery));
  596. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  597. seq_printf(seq, "present voltage: unknown\n");
  598. else
  599. seq_printf(seq, "present voltage: %d mV\n",
  600. battery->voltage_now);
  601. end:
  602. if (result)
  603. seq_printf(seq, "ERROR: Unable to read battery state\n");
  604. return result;
  605. }
  606. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  607. {
  608. struct acpi_battery *battery = seq->private;
  609. if (result)
  610. goto end;
  611. if (!acpi_battery_present(battery)) {
  612. seq_printf(seq, "present: no\n");
  613. goto end;
  614. }
  615. seq_printf(seq, "alarm: ");
  616. if (!battery->alarm)
  617. seq_printf(seq, "unsupported\n");
  618. else
  619. seq_printf(seq, "%u %sh\n", battery->alarm,
  620. acpi_battery_units(battery));
  621. end:
  622. if (result)
  623. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  624. return result;
  625. }
  626. static ssize_t acpi_battery_write_alarm(struct file *file,
  627. const char __user * buffer,
  628. size_t count, loff_t * ppos)
  629. {
  630. int result = 0;
  631. char alarm_string[12] = { '\0' };
  632. struct seq_file *m = file->private_data;
  633. struct acpi_battery *battery = m->private;
  634. if (!battery || (count > sizeof(alarm_string) - 1))
  635. return -EINVAL;
  636. if (!acpi_battery_present(battery)) {
  637. result = -ENODEV;
  638. goto end;
  639. }
  640. if (copy_from_user(alarm_string, buffer, count)) {
  641. result = -EFAULT;
  642. goto end;
  643. }
  644. alarm_string[count] = '\0';
  645. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  646. result = acpi_battery_set_alarm(battery);
  647. end:
  648. if (!result)
  649. return count;
  650. return result;
  651. }
  652. typedef int(*print_func)(struct seq_file *seq, int result);
  653. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  654. acpi_battery_print_info,
  655. acpi_battery_print_state,
  656. acpi_battery_print_alarm,
  657. };
  658. static int acpi_battery_read(int fid, struct seq_file *seq)
  659. {
  660. struct acpi_battery *battery = seq->private;
  661. int result = acpi_battery_update(battery);
  662. return acpi_print_funcs[fid](seq, result);
  663. }
  664. #define DECLARE_FILE_FUNCTIONS(_name) \
  665. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  666. { \
  667. return acpi_battery_read(_name##_tag, seq); \
  668. } \
  669. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  670. { \
  671. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  672. }
  673. DECLARE_FILE_FUNCTIONS(info);
  674. DECLARE_FILE_FUNCTIONS(state);
  675. DECLARE_FILE_FUNCTIONS(alarm);
  676. #undef DECLARE_FILE_FUNCTIONS
  677. #define FILE_DESCRIPTION_RO(_name) \
  678. { \
  679. .name = __stringify(_name), \
  680. .mode = S_IRUGO, \
  681. .ops = { \
  682. .open = acpi_battery_##_name##_open_fs, \
  683. .read = seq_read, \
  684. .llseek = seq_lseek, \
  685. .release = single_release, \
  686. .owner = THIS_MODULE, \
  687. }, \
  688. }
  689. #define FILE_DESCRIPTION_RW(_name) \
  690. { \
  691. .name = __stringify(_name), \
  692. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  693. .ops = { \
  694. .open = acpi_battery_##_name##_open_fs, \
  695. .read = seq_read, \
  696. .llseek = seq_lseek, \
  697. .write = acpi_battery_write_##_name, \
  698. .release = single_release, \
  699. .owner = THIS_MODULE, \
  700. }, \
  701. }
  702. static struct battery_file {
  703. struct file_operations ops;
  704. mode_t mode;
  705. const char *name;
  706. } acpi_battery_file[] = {
  707. FILE_DESCRIPTION_RO(info),
  708. FILE_DESCRIPTION_RO(state),
  709. FILE_DESCRIPTION_RW(alarm),
  710. };
  711. #undef FILE_DESCRIPTION_RO
  712. #undef FILE_DESCRIPTION_RW
  713. static int acpi_battery_add_fs(struct acpi_device *device)
  714. {
  715. struct proc_dir_entry *entry = NULL;
  716. int i;
  717. if (!acpi_device_dir(device)) {
  718. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  719. acpi_battery_dir);
  720. if (!acpi_device_dir(device))
  721. return -ENODEV;
  722. }
  723. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  724. entry = proc_create_data(acpi_battery_file[i].name,
  725. acpi_battery_file[i].mode,
  726. acpi_device_dir(device),
  727. &acpi_battery_file[i].ops,
  728. acpi_driver_data(device));
  729. if (!entry)
  730. return -ENODEV;
  731. }
  732. return 0;
  733. }
  734. static void acpi_battery_remove_fs(struct acpi_device *device)
  735. {
  736. int i;
  737. if (!acpi_device_dir(device))
  738. return;
  739. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  740. remove_proc_entry(acpi_battery_file[i].name,
  741. acpi_device_dir(device));
  742. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  743. acpi_device_dir(device) = NULL;
  744. }
  745. #endif
  746. /* --------------------------------------------------------------------------
  747. Driver Interface
  748. -------------------------------------------------------------------------- */
  749. static void acpi_battery_notify(struct acpi_device *device, u32 event)
  750. {
  751. struct acpi_battery *battery = acpi_driver_data(device);
  752. struct device *old;
  753. if (!battery)
  754. return;
  755. old = battery->bat.dev;
  756. acpi_battery_update(battery);
  757. acpi_bus_generate_proc_event(device, event,
  758. acpi_battery_present(battery));
  759. acpi_bus_generate_netlink_event(device->pnp.device_class,
  760. dev_name(&device->dev), event,
  761. acpi_battery_present(battery));
  762. /* acpi_battery_update could remove power_supply object */
  763. if (old && battery->bat.dev)
  764. power_supply_changed(&battery->bat);
  765. }
  766. static int acpi_battery_add(struct acpi_device *device)
  767. {
  768. int result = 0;
  769. struct acpi_battery *battery = NULL;
  770. acpi_handle handle;
  771. if (!device)
  772. return -EINVAL;
  773. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  774. if (!battery)
  775. return -ENOMEM;
  776. battery->device = device;
  777. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  778. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  779. device->driver_data = battery;
  780. mutex_init(&battery->lock);
  781. if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
  782. "_BIX", &handle)))
  783. set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  784. acpi_battery_update(battery);
  785. #ifdef CONFIG_ACPI_PROCFS_POWER
  786. result = acpi_battery_add_fs(device);
  787. #endif
  788. if (!result) {
  789. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  790. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  791. device->status.battery_present ? "present" : "absent");
  792. } else {
  793. #ifdef CONFIG_ACPI_PROCFS_POWER
  794. acpi_battery_remove_fs(device);
  795. #endif
  796. kfree(battery);
  797. }
  798. return result;
  799. }
  800. static int acpi_battery_remove(struct acpi_device *device, int type)
  801. {
  802. struct acpi_battery *battery = NULL;
  803. if (!device || !acpi_driver_data(device))
  804. return -EINVAL;
  805. battery = acpi_driver_data(device);
  806. #ifdef CONFIG_ACPI_PROCFS_POWER
  807. acpi_battery_remove_fs(device);
  808. #endif
  809. sysfs_remove_battery(battery);
  810. mutex_destroy(&battery->lock);
  811. kfree(battery);
  812. return 0;
  813. }
  814. /* this is needed to learn about changes made in suspended state */
  815. static int acpi_battery_resume(struct acpi_device *device)
  816. {
  817. struct acpi_battery *battery;
  818. if (!device)
  819. return -EINVAL;
  820. battery = acpi_driver_data(device);
  821. battery->update_time = 0;
  822. acpi_battery_update(battery);
  823. return 0;
  824. }
  825. static struct acpi_driver acpi_battery_driver = {
  826. .name = "battery",
  827. .class = ACPI_BATTERY_CLASS,
  828. .ids = battery_device_ids,
  829. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  830. .ops = {
  831. .add = acpi_battery_add,
  832. .resume = acpi_battery_resume,
  833. .remove = acpi_battery_remove,
  834. .notify = acpi_battery_notify,
  835. },
  836. };
  837. static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
  838. {
  839. if (acpi_disabled)
  840. return;
  841. #ifdef CONFIG_ACPI_PROCFS_POWER
  842. acpi_battery_dir = acpi_lock_battery_dir();
  843. if (!acpi_battery_dir)
  844. return;
  845. #endif
  846. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  847. #ifdef CONFIG_ACPI_PROCFS_POWER
  848. acpi_unlock_battery_dir(acpi_battery_dir);
  849. #endif
  850. return;
  851. }
  852. return;
  853. }
  854. static int __init acpi_battery_init(void)
  855. {
  856. async_schedule(acpi_battery_init_async, NULL);
  857. return 0;
  858. }
  859. static void __exit acpi_battery_exit(void)
  860. {
  861. acpi_bus_unregister_driver(&acpi_battery_driver);
  862. #ifdef CONFIG_ACPI_PROCFS_POWER
  863. acpi_unlock_battery_dir(acpi_battery_dir);
  864. #endif
  865. }
  866. module_init(acpi_battery_init);
  867. module_exit(acpi_battery_exit);