battery.c 30 KB

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