battery.c 26 KB

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