battery.c 24 KB

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