battery.c 28 KB

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