battery.c 25 KB

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