battery.c 28 KB

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