battery.c 25 KB

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