battery.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. mutex_lock(&battery->lock);
  511. if (!battery->bat.dev) {
  512. mutex_unlock(&battery->lock);
  513. return;
  514. }
  515. device_remove_file(battery->bat.dev, &alarm_attr);
  516. power_supply_unregister(&battery->bat);
  517. battery->bat.dev = NULL;
  518. mutex_unlock(&battery->lock);
  519. }
  520. /*
  521. * According to the ACPI spec, some kinds of primary batteries can
  522. * report percentage battery remaining capacity directly to OS.
  523. * In this case, it reports the Last Full Charged Capacity == 100
  524. * and BatteryPresentRate == 0xFFFFFFFF.
  525. *
  526. * Now we found some battery reports percentage remaining capacity
  527. * even if it's rechargeable.
  528. * https://bugzilla.kernel.org/show_bug.cgi?id=15979
  529. *
  530. * Handle this correctly so that they won't break userspace.
  531. */
  532. static void acpi_battery_quirks(struct acpi_battery *battery)
  533. {
  534. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
  535. return ;
  536. if (battery->full_charge_capacity == 100 &&
  537. battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
  538. battery->capacity_now >=0 && battery->capacity_now <= 100) {
  539. set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
  540. battery->full_charge_capacity = battery->design_capacity;
  541. battery->capacity_now = (battery->capacity_now *
  542. battery->full_charge_capacity) / 100;
  543. }
  544. }
  545. static int acpi_battery_update(struct acpi_battery *battery)
  546. {
  547. int result, old_present = acpi_battery_present(battery);
  548. result = acpi_battery_get_status(battery);
  549. if (result)
  550. return result;
  551. if (!acpi_battery_present(battery)) {
  552. sysfs_remove_battery(battery);
  553. battery->update_time = 0;
  554. return 0;
  555. }
  556. if (!battery->update_time ||
  557. old_present != acpi_battery_present(battery)) {
  558. result = acpi_battery_get_info(battery);
  559. if (result)
  560. return result;
  561. acpi_battery_init_alarm(battery);
  562. }
  563. if (!battery->bat.dev) {
  564. result = sysfs_add_battery(battery);
  565. if (result)
  566. return result;
  567. }
  568. result = acpi_battery_get_state(battery);
  569. acpi_battery_quirks(battery);
  570. return result;
  571. }
  572. static void acpi_battery_refresh(struct acpi_battery *battery)
  573. {
  574. if (!battery->bat.dev)
  575. return;
  576. acpi_battery_get_info(battery);
  577. /* The battery may have changed its reporting units. */
  578. sysfs_remove_battery(battery);
  579. sysfs_add_battery(battery);
  580. }
  581. /* --------------------------------------------------------------------------
  582. FS Interface (/proc)
  583. -------------------------------------------------------------------------- */
  584. #ifdef CONFIG_ACPI_PROCFS_POWER
  585. static struct proc_dir_entry *acpi_battery_dir;
  586. static int acpi_battery_print_info(struct seq_file *seq, int result)
  587. {
  588. struct acpi_battery *battery = seq->private;
  589. if (result)
  590. goto end;
  591. seq_printf(seq, "present: %s\n",
  592. acpi_battery_present(battery)?"yes":"no");
  593. if (!acpi_battery_present(battery))
  594. goto end;
  595. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  596. seq_printf(seq, "design capacity: unknown\n");
  597. else
  598. seq_printf(seq, "design capacity: %d %sh\n",
  599. battery->design_capacity,
  600. acpi_battery_units(battery));
  601. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  602. seq_printf(seq, "last full capacity: unknown\n");
  603. else
  604. seq_printf(seq, "last full capacity: %d %sh\n",
  605. battery->full_charge_capacity,
  606. acpi_battery_units(battery));
  607. seq_printf(seq, "battery technology: %srechargeable\n",
  608. (!battery->technology)?"non-":"");
  609. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  610. seq_printf(seq, "design voltage: unknown\n");
  611. else
  612. seq_printf(seq, "design voltage: %d mV\n",
  613. battery->design_voltage);
  614. seq_printf(seq, "design capacity warning: %d %sh\n",
  615. battery->design_capacity_warning,
  616. acpi_battery_units(battery));
  617. seq_printf(seq, "design capacity low: %d %sh\n",
  618. battery->design_capacity_low,
  619. acpi_battery_units(battery));
  620. seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
  621. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  622. battery->capacity_granularity_1,
  623. acpi_battery_units(battery));
  624. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  625. battery->capacity_granularity_2,
  626. acpi_battery_units(battery));
  627. seq_printf(seq, "model number: %s\n", battery->model_number);
  628. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  629. seq_printf(seq, "battery type: %s\n", battery->type);
  630. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  631. end:
  632. if (result)
  633. seq_printf(seq, "ERROR: Unable to read battery info\n");
  634. return result;
  635. }
  636. static int acpi_battery_print_state(struct seq_file *seq, int result)
  637. {
  638. struct acpi_battery *battery = seq->private;
  639. if (result)
  640. goto end;
  641. seq_printf(seq, "present: %s\n",
  642. acpi_battery_present(battery)?"yes":"no");
  643. if (!acpi_battery_present(battery))
  644. goto end;
  645. seq_printf(seq, "capacity state: %s\n",
  646. (battery->state & 0x04)?"critical":"ok");
  647. if ((battery->state & 0x01) && (battery->state & 0x02))
  648. seq_printf(seq,
  649. "charging state: charging/discharging\n");
  650. else if (battery->state & 0x01)
  651. seq_printf(seq, "charging state: discharging\n");
  652. else if (battery->state & 0x02)
  653. seq_printf(seq, "charging state: charging\n");
  654. else
  655. seq_printf(seq, "charging state: charged\n");
  656. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  657. seq_printf(seq, "present rate: unknown\n");
  658. else
  659. seq_printf(seq, "present rate: %d %s\n",
  660. battery->rate_now, acpi_battery_units(battery));
  661. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  662. seq_printf(seq, "remaining capacity: unknown\n");
  663. else
  664. seq_printf(seq, "remaining capacity: %d %sh\n",
  665. battery->capacity_now, acpi_battery_units(battery));
  666. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  667. seq_printf(seq, "present voltage: unknown\n");
  668. else
  669. seq_printf(seq, "present voltage: %d mV\n",
  670. battery->voltage_now);
  671. end:
  672. if (result)
  673. seq_printf(seq, "ERROR: Unable to read battery state\n");
  674. return result;
  675. }
  676. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  677. {
  678. struct acpi_battery *battery = seq->private;
  679. if (result)
  680. goto end;
  681. if (!acpi_battery_present(battery)) {
  682. seq_printf(seq, "present: no\n");
  683. goto end;
  684. }
  685. seq_printf(seq, "alarm: ");
  686. if (!battery->alarm)
  687. seq_printf(seq, "unsupported\n");
  688. else
  689. seq_printf(seq, "%u %sh\n", battery->alarm,
  690. acpi_battery_units(battery));
  691. end:
  692. if (result)
  693. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  694. return result;
  695. }
  696. static ssize_t acpi_battery_write_alarm(struct file *file,
  697. const char __user * buffer,
  698. size_t count, loff_t * ppos)
  699. {
  700. int result = 0;
  701. char alarm_string[12] = { '\0' };
  702. struct seq_file *m = file->private_data;
  703. struct acpi_battery *battery = m->private;
  704. if (!battery || (count > sizeof(alarm_string) - 1))
  705. return -EINVAL;
  706. if (!acpi_battery_present(battery)) {
  707. result = -ENODEV;
  708. goto end;
  709. }
  710. if (copy_from_user(alarm_string, buffer, count)) {
  711. result = -EFAULT;
  712. goto end;
  713. }
  714. alarm_string[count] = '\0';
  715. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  716. result = acpi_battery_set_alarm(battery);
  717. end:
  718. if (!result)
  719. return count;
  720. return result;
  721. }
  722. typedef int(*print_func)(struct seq_file *seq, int result);
  723. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  724. acpi_battery_print_info,
  725. acpi_battery_print_state,
  726. acpi_battery_print_alarm,
  727. };
  728. static int acpi_battery_read(int fid, struct seq_file *seq)
  729. {
  730. struct acpi_battery *battery = seq->private;
  731. int result = acpi_battery_update(battery);
  732. return acpi_print_funcs[fid](seq, result);
  733. }
  734. #define DECLARE_FILE_FUNCTIONS(_name) \
  735. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  736. { \
  737. return acpi_battery_read(_name##_tag, seq); \
  738. } \
  739. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  740. { \
  741. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  742. }
  743. DECLARE_FILE_FUNCTIONS(info);
  744. DECLARE_FILE_FUNCTIONS(state);
  745. DECLARE_FILE_FUNCTIONS(alarm);
  746. #undef DECLARE_FILE_FUNCTIONS
  747. #define FILE_DESCRIPTION_RO(_name) \
  748. { \
  749. .name = __stringify(_name), \
  750. .mode = S_IRUGO, \
  751. .ops = { \
  752. .open = acpi_battery_##_name##_open_fs, \
  753. .read = seq_read, \
  754. .llseek = seq_lseek, \
  755. .release = single_release, \
  756. .owner = THIS_MODULE, \
  757. }, \
  758. }
  759. #define FILE_DESCRIPTION_RW(_name) \
  760. { \
  761. .name = __stringify(_name), \
  762. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  763. .ops = { \
  764. .open = acpi_battery_##_name##_open_fs, \
  765. .read = seq_read, \
  766. .llseek = seq_lseek, \
  767. .write = acpi_battery_write_##_name, \
  768. .release = single_release, \
  769. .owner = THIS_MODULE, \
  770. }, \
  771. }
  772. static struct battery_file {
  773. struct file_operations ops;
  774. mode_t mode;
  775. const char *name;
  776. } acpi_battery_file[] = {
  777. FILE_DESCRIPTION_RO(info),
  778. FILE_DESCRIPTION_RO(state),
  779. FILE_DESCRIPTION_RW(alarm),
  780. };
  781. #undef FILE_DESCRIPTION_RO
  782. #undef FILE_DESCRIPTION_RW
  783. static int acpi_battery_add_fs(struct acpi_device *device)
  784. {
  785. struct proc_dir_entry *entry = NULL;
  786. int i;
  787. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
  788. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  789. if (!acpi_device_dir(device)) {
  790. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  791. acpi_battery_dir);
  792. if (!acpi_device_dir(device))
  793. return -ENODEV;
  794. }
  795. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  796. entry = proc_create_data(acpi_battery_file[i].name,
  797. acpi_battery_file[i].mode,
  798. acpi_device_dir(device),
  799. &acpi_battery_file[i].ops,
  800. acpi_driver_data(device));
  801. if (!entry)
  802. return -ENODEV;
  803. }
  804. return 0;
  805. }
  806. static void acpi_battery_remove_fs(struct acpi_device *device)
  807. {
  808. int i;
  809. if (!acpi_device_dir(device))
  810. return;
  811. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  812. remove_proc_entry(acpi_battery_file[i].name,
  813. acpi_device_dir(device));
  814. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  815. acpi_device_dir(device) = NULL;
  816. }
  817. #endif
  818. /* --------------------------------------------------------------------------
  819. Driver Interface
  820. -------------------------------------------------------------------------- */
  821. static void acpi_battery_notify(struct acpi_device *device, u32 event)
  822. {
  823. struct acpi_battery *battery = acpi_driver_data(device);
  824. struct device *old;
  825. if (!battery)
  826. return;
  827. old = battery->bat.dev;
  828. if (event == ACPI_BATTERY_NOTIFY_INFO)
  829. acpi_battery_refresh(battery);
  830. acpi_battery_update(battery);
  831. acpi_bus_generate_proc_event(device, event,
  832. acpi_battery_present(battery));
  833. acpi_bus_generate_netlink_event(device->pnp.device_class,
  834. dev_name(&device->dev), event,
  835. acpi_battery_present(battery));
  836. /* acpi_battery_update could remove power_supply object */
  837. if (old && battery->bat.dev)
  838. power_supply_changed(&battery->bat);
  839. }
  840. static int battery_notify(struct notifier_block *nb,
  841. unsigned long mode, void *_unused)
  842. {
  843. struct acpi_battery *battery = container_of(nb, struct acpi_battery,
  844. pm_nb);
  845. switch (mode) {
  846. case PM_POST_HIBERNATION:
  847. case PM_POST_SUSPEND:
  848. if (battery->bat.dev) {
  849. sysfs_remove_battery(battery);
  850. sysfs_add_battery(battery);
  851. }
  852. break;
  853. }
  854. return 0;
  855. }
  856. static int acpi_battery_add(struct acpi_device *device)
  857. {
  858. int result = 0;
  859. struct acpi_battery *battery = NULL;
  860. acpi_handle handle;
  861. if (!device)
  862. return -EINVAL;
  863. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  864. if (!battery)
  865. return -ENOMEM;
  866. battery->device = device;
  867. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  868. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  869. device->driver_data = battery;
  870. mutex_init(&battery->lock);
  871. if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
  872. "_BIX", &handle)))
  873. set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  874. result = acpi_battery_update(battery);
  875. if (result)
  876. goto fail;
  877. #ifdef CONFIG_ACPI_PROCFS_POWER
  878. result = acpi_battery_add_fs(device);
  879. #endif
  880. if (result) {
  881. #ifdef CONFIG_ACPI_PROCFS_POWER
  882. acpi_battery_remove_fs(device);
  883. #endif
  884. goto fail;
  885. }
  886. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  887. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  888. device->status.battery_present ? "present" : "absent");
  889. battery->pm_nb.notifier_call = battery_notify;
  890. register_pm_notifier(&battery->pm_nb);
  891. return result;
  892. fail:
  893. sysfs_remove_battery(battery);
  894. mutex_destroy(&battery->lock);
  895. kfree(battery);
  896. return result;
  897. }
  898. static int acpi_battery_remove(struct acpi_device *device, int type)
  899. {
  900. struct acpi_battery *battery = NULL;
  901. if (!device || !acpi_driver_data(device))
  902. return -EINVAL;
  903. battery = acpi_driver_data(device);
  904. unregister_pm_notifier(&battery->pm_nb);
  905. #ifdef CONFIG_ACPI_PROCFS_POWER
  906. acpi_battery_remove_fs(device);
  907. #endif
  908. sysfs_remove_battery(battery);
  909. mutex_destroy(&battery->lock);
  910. kfree(battery);
  911. return 0;
  912. }
  913. /* this is needed to learn about changes made in suspended state */
  914. static int acpi_battery_resume(struct acpi_device *device)
  915. {
  916. struct acpi_battery *battery;
  917. if (!device)
  918. return -EINVAL;
  919. battery = acpi_driver_data(device);
  920. battery->update_time = 0;
  921. acpi_battery_update(battery);
  922. return 0;
  923. }
  924. static struct acpi_driver acpi_battery_driver = {
  925. .name = "battery",
  926. .class = ACPI_BATTERY_CLASS,
  927. .ids = battery_device_ids,
  928. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  929. .ops = {
  930. .add = acpi_battery_add,
  931. .resume = acpi_battery_resume,
  932. .remove = acpi_battery_remove,
  933. .notify = acpi_battery_notify,
  934. },
  935. };
  936. static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
  937. {
  938. if (acpi_disabled)
  939. return;
  940. #ifdef CONFIG_ACPI_PROCFS_POWER
  941. acpi_battery_dir = acpi_lock_battery_dir();
  942. if (!acpi_battery_dir)
  943. return;
  944. #endif
  945. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  946. #ifdef CONFIG_ACPI_PROCFS_POWER
  947. acpi_unlock_battery_dir(acpi_battery_dir);
  948. #endif
  949. return;
  950. }
  951. return;
  952. }
  953. static int __init acpi_battery_init(void)
  954. {
  955. async_schedule(acpi_battery_init_async, NULL);
  956. return 0;
  957. }
  958. static void __exit acpi_battery_exit(void)
  959. {
  960. acpi_bus_unregister_driver(&acpi_battery_driver);
  961. #ifdef CONFIG_ACPI_PROCFS_POWER
  962. acpi_unlock_battery_dir(acpi_battery_dir);
  963. #endif
  964. }
  965. module_init(acpi_battery_init);
  966. module_exit(acpi_battery_exit);