battery.c 25 KB

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