battery.c 31 KB

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