battery.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 current_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. val->intval = battery->current_now * 1000;
  172. break;
  173. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  174. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  175. val->intval = battery->design_capacity * 1000;
  176. break;
  177. case POWER_SUPPLY_PROP_CHARGE_FULL:
  178. case POWER_SUPPLY_PROP_ENERGY_FULL:
  179. val->intval = battery->full_charge_capacity * 1000;
  180. break;
  181. case POWER_SUPPLY_PROP_CHARGE_NOW:
  182. case POWER_SUPPLY_PROP_ENERGY_NOW:
  183. val->intval = battery->capacity_now * 1000;
  184. break;
  185. case POWER_SUPPLY_PROP_MODEL_NAME:
  186. val->strval = battery->model_number;
  187. break;
  188. case POWER_SUPPLY_PROP_MANUFACTURER:
  189. val->strval = battery->oem_info;
  190. break;
  191. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  192. val->strval = battery->serial_number;
  193. break;
  194. default:
  195. return -EINVAL;
  196. }
  197. return 0;
  198. }
  199. static enum power_supply_property charge_battery_props[] = {
  200. POWER_SUPPLY_PROP_STATUS,
  201. POWER_SUPPLY_PROP_PRESENT,
  202. POWER_SUPPLY_PROP_TECHNOLOGY,
  203. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  204. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  205. POWER_SUPPLY_PROP_CURRENT_NOW,
  206. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  207. POWER_SUPPLY_PROP_CHARGE_FULL,
  208. POWER_SUPPLY_PROP_CHARGE_NOW,
  209. POWER_SUPPLY_PROP_MODEL_NAME,
  210. POWER_SUPPLY_PROP_MANUFACTURER,
  211. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  212. };
  213. static enum power_supply_property energy_battery_props[] = {
  214. POWER_SUPPLY_PROP_STATUS,
  215. POWER_SUPPLY_PROP_PRESENT,
  216. POWER_SUPPLY_PROP_TECHNOLOGY,
  217. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  218. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  219. POWER_SUPPLY_PROP_CURRENT_NOW,
  220. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  221. POWER_SUPPLY_PROP_ENERGY_FULL,
  222. POWER_SUPPLY_PROP_ENERGY_NOW,
  223. POWER_SUPPLY_PROP_MODEL_NAME,
  224. POWER_SUPPLY_PROP_MANUFACTURER,
  225. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  226. };
  227. #endif
  228. #ifdef CONFIG_ACPI_PROCFS_POWER
  229. inline char *acpi_battery_units(struct acpi_battery *battery)
  230. {
  231. return (battery->power_unit)?"mA":"mW";
  232. }
  233. #endif
  234. /* --------------------------------------------------------------------------
  235. Battery Management
  236. -------------------------------------------------------------------------- */
  237. struct acpi_offsets {
  238. size_t offset; /* offset inside struct acpi_sbs_battery */
  239. u8 mode; /* int or string? */
  240. };
  241. static struct acpi_offsets state_offsets[] = {
  242. {offsetof(struct acpi_battery, state), 0},
  243. {offsetof(struct acpi_battery, current_now), 0},
  244. {offsetof(struct acpi_battery, capacity_now), 0},
  245. {offsetof(struct acpi_battery, voltage_now), 0},
  246. };
  247. static struct acpi_offsets info_offsets[] = {
  248. {offsetof(struct acpi_battery, power_unit), 0},
  249. {offsetof(struct acpi_battery, design_capacity), 0},
  250. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  251. {offsetof(struct acpi_battery, technology), 0},
  252. {offsetof(struct acpi_battery, design_voltage), 0},
  253. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  254. {offsetof(struct acpi_battery, design_capacity_low), 0},
  255. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  256. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  257. {offsetof(struct acpi_battery, model_number), 1},
  258. {offsetof(struct acpi_battery, serial_number), 1},
  259. {offsetof(struct acpi_battery, type), 1},
  260. {offsetof(struct acpi_battery, oem_info), 1},
  261. };
  262. static int extract_package(struct acpi_battery *battery,
  263. union acpi_object *package,
  264. struct acpi_offsets *offsets, int num)
  265. {
  266. int i;
  267. union acpi_object *element;
  268. if (package->type != ACPI_TYPE_PACKAGE)
  269. return -EFAULT;
  270. for (i = 0; i < num; ++i) {
  271. if (package->package.count <= i)
  272. return -EFAULT;
  273. element = &package->package.elements[i];
  274. if (offsets[i].mode) {
  275. u8 *ptr = (u8 *)battery + offsets[i].offset;
  276. if (element->type == ACPI_TYPE_STRING ||
  277. element->type == ACPI_TYPE_BUFFER)
  278. strncpy(ptr, element->string.pointer, 32);
  279. else if (element->type == ACPI_TYPE_INTEGER) {
  280. strncpy(ptr, (u8 *)&element->integer.value,
  281. sizeof(acpi_integer));
  282. ptr[sizeof(acpi_integer)] = 0;
  283. } else
  284. *ptr = 0; /* don't have value */
  285. } else {
  286. int *x = (int *)((u8 *)battery + offsets[i].offset);
  287. *x = (element->type == ACPI_TYPE_INTEGER) ?
  288. element->integer.value : -1;
  289. }
  290. }
  291. return 0;
  292. }
  293. static int acpi_battery_get_status(struct acpi_battery *battery)
  294. {
  295. if (acpi_bus_get_status(battery->device)) {
  296. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  297. return -ENODEV;
  298. }
  299. return 0;
  300. }
  301. static int acpi_battery_get_info(struct acpi_battery *battery)
  302. {
  303. int result = -EFAULT;
  304. acpi_status status = 0;
  305. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  306. if (!acpi_battery_present(battery))
  307. return 0;
  308. mutex_lock(&battery->lock);
  309. status = acpi_evaluate_object(battery->device->handle, "_BIF",
  310. NULL, &buffer);
  311. mutex_unlock(&battery->lock);
  312. if (ACPI_FAILURE(status)) {
  313. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
  314. return -ENODEV;
  315. }
  316. result = extract_package(battery, buffer.pointer,
  317. info_offsets, ARRAY_SIZE(info_offsets));
  318. kfree(buffer.pointer);
  319. return result;
  320. }
  321. static int acpi_battery_get_state(struct acpi_battery *battery)
  322. {
  323. int result = 0;
  324. acpi_status status = 0;
  325. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  326. if (!acpi_battery_present(battery))
  327. return 0;
  328. if (battery->update_time &&
  329. time_before(jiffies, battery->update_time +
  330. msecs_to_jiffies(cache_time)))
  331. return 0;
  332. mutex_lock(&battery->lock);
  333. status = acpi_evaluate_object(battery->device->handle, "_BST",
  334. NULL, &buffer);
  335. mutex_unlock(&battery->lock);
  336. if (ACPI_FAILURE(status)) {
  337. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  338. return -ENODEV;
  339. }
  340. result = extract_package(battery, buffer.pointer,
  341. state_offsets, ARRAY_SIZE(state_offsets));
  342. battery->update_time = jiffies;
  343. kfree(buffer.pointer);
  344. return result;
  345. }
  346. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  347. {
  348. acpi_status status = 0;
  349. union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
  350. struct acpi_object_list arg_list = { 1, &arg0 };
  351. if (!acpi_battery_present(battery)|| !battery->alarm_present)
  352. return -ENODEV;
  353. arg0.integer.value = battery->alarm;
  354. mutex_lock(&battery->lock);
  355. status = acpi_evaluate_object(battery->device->handle, "_BTP",
  356. &arg_list, NULL);
  357. mutex_unlock(&battery->lock);
  358. if (ACPI_FAILURE(status))
  359. return -ENODEV;
  360. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  361. return 0;
  362. }
  363. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  364. {
  365. acpi_status status = AE_OK;
  366. acpi_handle handle = NULL;
  367. /* See if alarms are supported, and if so, set default */
  368. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  369. if (ACPI_FAILURE(status)) {
  370. battery->alarm_present = 0;
  371. return 0;
  372. }
  373. battery->alarm_present = 1;
  374. if (!battery->alarm)
  375. battery->alarm = battery->design_capacity_warning;
  376. return acpi_battery_set_alarm(battery);
  377. }
  378. #ifdef CONFIG_ACPI_SYSFS_POWER
  379. static ssize_t acpi_battery_alarm_show(struct device *dev,
  380. struct device_attribute *attr,
  381. char *buf)
  382. {
  383. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  384. return sprintf(buf, "%d\n", battery->alarm * 1000);
  385. }
  386. static ssize_t acpi_battery_alarm_store(struct device *dev,
  387. struct device_attribute *attr,
  388. const char *buf, size_t count)
  389. {
  390. unsigned long x;
  391. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  392. if (sscanf(buf, "%ld\n", &x) == 1)
  393. battery->alarm = x/1000;
  394. if (acpi_battery_present(battery))
  395. acpi_battery_set_alarm(battery);
  396. return count;
  397. }
  398. static struct device_attribute alarm_attr = {
  399. .attr = {.name = "alarm", .mode = 0644},
  400. .show = acpi_battery_alarm_show,
  401. .store = acpi_battery_alarm_store,
  402. };
  403. static int sysfs_add_battery(struct acpi_battery *battery)
  404. {
  405. int result;
  406. if (battery->power_unit) {
  407. battery->bat.properties = charge_battery_props;
  408. battery->bat.num_properties =
  409. ARRAY_SIZE(charge_battery_props);
  410. } else {
  411. battery->bat.properties = energy_battery_props;
  412. battery->bat.num_properties =
  413. ARRAY_SIZE(energy_battery_props);
  414. }
  415. battery->bat.name = acpi_device_bid(battery->device);
  416. battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  417. battery->bat.get_property = acpi_battery_get_property;
  418. result = power_supply_register(&battery->device->dev, &battery->bat);
  419. if (result)
  420. return result;
  421. return device_create_file(battery->bat.dev, &alarm_attr);
  422. }
  423. static void sysfs_remove_battery(struct acpi_battery *battery)
  424. {
  425. if (!battery->bat.dev)
  426. return;
  427. device_remove_file(battery->bat.dev, &alarm_attr);
  428. power_supply_unregister(&battery->bat);
  429. battery->bat.dev = NULL;
  430. }
  431. #endif
  432. static int acpi_battery_update(struct acpi_battery *battery)
  433. {
  434. int result, old_present = acpi_battery_present(battery);
  435. result = acpi_battery_get_status(battery);
  436. if (result)
  437. return result;
  438. #ifdef CONFIG_ACPI_SYSFS_POWER
  439. if (!acpi_battery_present(battery)) {
  440. sysfs_remove_battery(battery);
  441. battery->update_time = 0;
  442. return 0;
  443. }
  444. #endif
  445. if (!battery->update_time ||
  446. old_present != acpi_battery_present(battery)) {
  447. result = acpi_battery_get_info(battery);
  448. if (result)
  449. return result;
  450. acpi_battery_init_alarm(battery);
  451. }
  452. #ifdef CONFIG_ACPI_SYSFS_POWER
  453. if (!battery->bat.dev)
  454. sysfs_add_battery(battery);
  455. #endif
  456. return acpi_battery_get_state(battery);
  457. }
  458. /* --------------------------------------------------------------------------
  459. FS Interface (/proc)
  460. -------------------------------------------------------------------------- */
  461. #ifdef CONFIG_ACPI_PROCFS_POWER
  462. static struct proc_dir_entry *acpi_battery_dir;
  463. static int acpi_battery_print_info(struct seq_file *seq, int result)
  464. {
  465. struct acpi_battery *battery = seq->private;
  466. if (result)
  467. goto end;
  468. seq_printf(seq, "present: %s\n",
  469. acpi_battery_present(battery)?"yes":"no");
  470. if (!acpi_battery_present(battery))
  471. goto end;
  472. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  473. seq_printf(seq, "design capacity: unknown\n");
  474. else
  475. seq_printf(seq, "design capacity: %d %sh\n",
  476. battery->design_capacity,
  477. acpi_battery_units(battery));
  478. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  479. seq_printf(seq, "last full capacity: unknown\n");
  480. else
  481. seq_printf(seq, "last full capacity: %d %sh\n",
  482. battery->full_charge_capacity,
  483. acpi_battery_units(battery));
  484. seq_printf(seq, "battery technology: %srechargeable\n",
  485. (!battery->technology)?"non-":"");
  486. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  487. seq_printf(seq, "design voltage: unknown\n");
  488. else
  489. seq_printf(seq, "design voltage: %d mV\n",
  490. battery->design_voltage);
  491. seq_printf(seq, "design capacity warning: %d %sh\n",
  492. battery->design_capacity_warning,
  493. acpi_battery_units(battery));
  494. seq_printf(seq, "design capacity low: %d %sh\n",
  495. battery->design_capacity_low,
  496. acpi_battery_units(battery));
  497. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  498. battery->capacity_granularity_1,
  499. acpi_battery_units(battery));
  500. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  501. battery->capacity_granularity_2,
  502. acpi_battery_units(battery));
  503. seq_printf(seq, "model number: %s\n", battery->model_number);
  504. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  505. seq_printf(seq, "battery type: %s\n", battery->type);
  506. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  507. end:
  508. if (result)
  509. seq_printf(seq, "ERROR: Unable to read battery info\n");
  510. return result;
  511. }
  512. static int acpi_battery_print_state(struct seq_file *seq, int result)
  513. {
  514. struct acpi_battery *battery = seq->private;
  515. if (result)
  516. goto end;
  517. seq_printf(seq, "present: %s\n",
  518. acpi_battery_present(battery)?"yes":"no");
  519. if (!acpi_battery_present(battery))
  520. goto end;
  521. seq_printf(seq, "capacity state: %s\n",
  522. (battery->state & 0x04)?"critical":"ok");
  523. if ((battery->state & 0x01) && (battery->state & 0x02))
  524. seq_printf(seq,
  525. "charging state: charging/discharging\n");
  526. else if (battery->state & 0x01)
  527. seq_printf(seq, "charging state: discharging\n");
  528. else if (battery->state & 0x02)
  529. seq_printf(seq, "charging state: charging\n");
  530. else
  531. seq_printf(seq, "charging state: charged\n");
  532. if (battery->current_now == ACPI_BATTERY_VALUE_UNKNOWN)
  533. seq_printf(seq, "present rate: unknown\n");
  534. else
  535. seq_printf(seq, "present rate: %d %s\n",
  536. battery->current_now, acpi_battery_units(battery));
  537. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  538. seq_printf(seq, "remaining capacity: unknown\n");
  539. else
  540. seq_printf(seq, "remaining capacity: %d %sh\n",
  541. battery->capacity_now, acpi_battery_units(battery));
  542. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  543. seq_printf(seq, "present voltage: unknown\n");
  544. else
  545. seq_printf(seq, "present voltage: %d mV\n",
  546. battery->voltage_now);
  547. end:
  548. if (result)
  549. seq_printf(seq, "ERROR: Unable to read battery state\n");
  550. return result;
  551. }
  552. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  553. {
  554. struct acpi_battery *battery = seq->private;
  555. if (result)
  556. goto end;
  557. if (!acpi_battery_present(battery)) {
  558. seq_printf(seq, "present: no\n");
  559. goto end;
  560. }
  561. seq_printf(seq, "alarm: ");
  562. if (!battery->alarm)
  563. seq_printf(seq, "unsupported\n");
  564. else
  565. seq_printf(seq, "%u %sh\n", battery->alarm,
  566. acpi_battery_units(battery));
  567. end:
  568. if (result)
  569. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  570. return result;
  571. }
  572. static ssize_t acpi_battery_write_alarm(struct file *file,
  573. const char __user * buffer,
  574. size_t count, loff_t * ppos)
  575. {
  576. int result = 0;
  577. char alarm_string[12] = { '\0' };
  578. struct seq_file *m = file->private_data;
  579. struct acpi_battery *battery = m->private;
  580. if (!battery || (count > sizeof(alarm_string) - 1))
  581. return -EINVAL;
  582. if (!acpi_battery_present(battery)) {
  583. result = -ENODEV;
  584. goto end;
  585. }
  586. if (copy_from_user(alarm_string, buffer, count)) {
  587. result = -EFAULT;
  588. goto end;
  589. }
  590. alarm_string[count] = '\0';
  591. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  592. result = acpi_battery_set_alarm(battery);
  593. end:
  594. if (!result)
  595. return count;
  596. return result;
  597. }
  598. typedef int(*print_func)(struct seq_file *seq, int result);
  599. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  600. acpi_battery_print_info,
  601. acpi_battery_print_state,
  602. acpi_battery_print_alarm,
  603. };
  604. static int acpi_battery_read(int fid, struct seq_file *seq)
  605. {
  606. struct acpi_battery *battery = seq->private;
  607. int result = acpi_battery_update(battery);
  608. return acpi_print_funcs[fid](seq, result);
  609. }
  610. #define DECLARE_FILE_FUNCTIONS(_name) \
  611. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  612. { \
  613. return acpi_battery_read(_name##_tag, seq); \
  614. } \
  615. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  616. { \
  617. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  618. }
  619. DECLARE_FILE_FUNCTIONS(info);
  620. DECLARE_FILE_FUNCTIONS(state);
  621. DECLARE_FILE_FUNCTIONS(alarm);
  622. #undef DECLARE_FILE_FUNCTIONS
  623. #define FILE_DESCRIPTION_RO(_name) \
  624. { \
  625. .name = __stringify(_name), \
  626. .mode = S_IRUGO, \
  627. .ops = { \
  628. .open = acpi_battery_##_name##_open_fs, \
  629. .read = seq_read, \
  630. .llseek = seq_lseek, \
  631. .release = single_release, \
  632. .owner = THIS_MODULE, \
  633. }, \
  634. }
  635. #define FILE_DESCRIPTION_RW(_name) \
  636. { \
  637. .name = __stringify(_name), \
  638. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  639. .ops = { \
  640. .open = acpi_battery_##_name##_open_fs, \
  641. .read = seq_read, \
  642. .llseek = seq_lseek, \
  643. .write = acpi_battery_write_##_name, \
  644. .release = single_release, \
  645. .owner = THIS_MODULE, \
  646. }, \
  647. }
  648. static struct battery_file {
  649. struct file_operations ops;
  650. mode_t mode;
  651. char *name;
  652. } acpi_battery_file[] = {
  653. FILE_DESCRIPTION_RO(info),
  654. FILE_DESCRIPTION_RO(state),
  655. FILE_DESCRIPTION_RW(alarm),
  656. };
  657. #undef FILE_DESCRIPTION_RO
  658. #undef FILE_DESCRIPTION_RW
  659. static int acpi_battery_add_fs(struct acpi_device *device)
  660. {
  661. struct proc_dir_entry *entry = NULL;
  662. int i;
  663. if (!acpi_device_dir(device)) {
  664. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  665. acpi_battery_dir);
  666. if (!acpi_device_dir(device))
  667. return -ENODEV;
  668. acpi_device_dir(device)->owner = THIS_MODULE;
  669. }
  670. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  671. entry = proc_create_data(acpi_battery_file[i].name,
  672. acpi_battery_file[i].mode,
  673. acpi_device_dir(device),
  674. &acpi_battery_file[i].ops,
  675. acpi_driver_data(device));
  676. if (!entry)
  677. return -ENODEV;
  678. }
  679. return 0;
  680. }
  681. static void acpi_battery_remove_fs(struct acpi_device *device)
  682. {
  683. int i;
  684. if (!acpi_device_dir(device))
  685. return;
  686. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  687. remove_proc_entry(acpi_battery_file[i].name,
  688. acpi_device_dir(device));
  689. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  690. acpi_device_dir(device) = NULL;
  691. }
  692. #endif
  693. /* --------------------------------------------------------------------------
  694. Driver Interface
  695. -------------------------------------------------------------------------- */
  696. static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
  697. {
  698. struct acpi_battery *battery = data;
  699. struct acpi_device *device;
  700. if (!battery)
  701. return;
  702. device = battery->device;
  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. acpi_status status = 0;
  719. struct acpi_battery *battery = NULL;
  720. if (!device)
  721. return -EINVAL;
  722. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  723. if (!battery)
  724. return -ENOMEM;
  725. battery->device = device;
  726. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  727. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  728. device->driver_data = battery;
  729. mutex_init(&battery->lock);
  730. acpi_battery_update(battery);
  731. #ifdef CONFIG_ACPI_PROCFS_POWER
  732. result = acpi_battery_add_fs(device);
  733. if (result)
  734. goto end;
  735. #endif
  736. status = acpi_install_notify_handler(device->handle,
  737. ACPI_ALL_NOTIFY,
  738. acpi_battery_notify, battery);
  739. if (ACPI_FAILURE(status)) {
  740. ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
  741. result = -ENODEV;
  742. goto end;
  743. }
  744. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  745. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  746. device->status.battery_present ? "present" : "absent");
  747. end:
  748. if (result) {
  749. #ifdef CONFIG_ACPI_PROCFS_POWER
  750. acpi_battery_remove_fs(device);
  751. #endif
  752. kfree(battery);
  753. }
  754. return result;
  755. }
  756. static int acpi_battery_remove(struct acpi_device *device, int type)
  757. {
  758. acpi_status status = 0;
  759. struct acpi_battery *battery = NULL;
  760. if (!device || !acpi_driver_data(device))
  761. return -EINVAL;
  762. battery = acpi_driver_data(device);
  763. status = acpi_remove_notify_handler(device->handle,
  764. ACPI_ALL_NOTIFY,
  765. acpi_battery_notify);
  766. #ifdef CONFIG_ACPI_PROCFS_POWER
  767. acpi_battery_remove_fs(device);
  768. #endif
  769. #ifdef CONFIG_ACPI_SYSFS_POWER
  770. sysfs_remove_battery(battery);
  771. #endif
  772. mutex_destroy(&battery->lock);
  773. kfree(battery);
  774. return 0;
  775. }
  776. /* this is needed to learn about changes made in suspended state */
  777. static int acpi_battery_resume(struct acpi_device *device)
  778. {
  779. struct acpi_battery *battery;
  780. if (!device)
  781. return -EINVAL;
  782. battery = acpi_driver_data(device);
  783. battery->update_time = 0;
  784. acpi_battery_update(battery);
  785. return 0;
  786. }
  787. static struct acpi_driver acpi_battery_driver = {
  788. .name = "battery",
  789. .class = ACPI_BATTERY_CLASS,
  790. .ids = battery_device_ids,
  791. .ops = {
  792. .add = acpi_battery_add,
  793. .resume = acpi_battery_resume,
  794. .remove = acpi_battery_remove,
  795. },
  796. };
  797. static int __init acpi_battery_init(void)
  798. {
  799. if (acpi_disabled)
  800. return -ENODEV;
  801. #ifdef CONFIG_ACPI_PROCFS_POWER
  802. acpi_battery_dir = acpi_lock_battery_dir();
  803. if (!acpi_battery_dir)
  804. return -ENODEV;
  805. #endif
  806. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  807. #ifdef CONFIG_ACPI_PROCFS_POWER
  808. acpi_unlock_battery_dir(acpi_battery_dir);
  809. #endif
  810. return -ENODEV;
  811. }
  812. return 0;
  813. }
  814. static void __exit acpi_battery_exit(void)
  815. {
  816. acpi_bus_unregister_driver(&acpi_battery_driver);
  817. #ifdef CONFIG_ACPI_PROCFS_POWER
  818. acpi_unlock_battery_dir(acpi_battery_dir);
  819. #endif
  820. }
  821. module_init(acpi_battery_init);
  822. module_exit(acpi_battery_exit);