battery.c 25 KB

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