battery.c 24 KB

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