battery.c 26 KB

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