battery.c 25 KB

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