battery.c 28 KB

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