battery.c 24 KB

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