battery.c 30 KB

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