battery.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <asm/uaccess.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  35. #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
  36. #define ACPI_BATTERY_FORMAT_BST "NNNN"
  37. #define ACPI_BATTERY_COMPONENT 0x00040000
  38. #define ACPI_BATTERY_CLASS "battery"
  39. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  40. #define ACPI_BATTERY_NOTIFY_STATUS 0x80
  41. #define ACPI_BATTERY_NOTIFY_INFO 0x81
  42. #define ACPI_BATTERY_UNITS_WATTS "mW"
  43. #define ACPI_BATTERY_UNITS_AMPS "mA"
  44. #define _COMPONENT ACPI_BATTERY_COMPONENT
  45. #define ACPI_BATTERY_UPDATE_TIME 0
  46. #define ACPI_BATTERY_NONE_UPDATE 0
  47. #define ACPI_BATTERY_EASY_UPDATE 1
  48. #define ACPI_BATTERY_INIT_UPDATE 2
  49. ACPI_MODULE_NAME("battery");
  50. MODULE_AUTHOR("Paul Diefenbaugh");
  51. MODULE_DESCRIPTION("ACPI Battery Driver");
  52. MODULE_LICENSE("GPL");
  53. static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
  54. /* 0 - every time, > 0 - by update_time */
  55. module_param(update_time, uint, 0644);
  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. static int acpi_battery_add(struct acpi_device *device);
  59. static int acpi_battery_remove(struct acpi_device *device, int type);
  60. static int acpi_battery_resume(struct acpi_device *device);
  61. static const struct acpi_device_id battery_device_ids[] = {
  62. {"PNP0C0A", 0},
  63. {"", 0},
  64. };
  65. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  66. static struct acpi_driver acpi_battery_driver = {
  67. .name = "battery",
  68. .class = ACPI_BATTERY_CLASS,
  69. .ids = battery_device_ids,
  70. .ops = {
  71. .add = acpi_battery_add,
  72. .resume = acpi_battery_resume,
  73. .remove = acpi_battery_remove,
  74. },
  75. };
  76. struct acpi_battery_state {
  77. acpi_integer state;
  78. acpi_integer present_rate;
  79. acpi_integer remaining_capacity;
  80. acpi_integer present_voltage;
  81. };
  82. struct acpi_battery_info {
  83. acpi_integer power_unit;
  84. acpi_integer design_capacity;
  85. acpi_integer last_full_capacity;
  86. acpi_integer battery_technology;
  87. acpi_integer design_voltage;
  88. acpi_integer design_capacity_warning;
  89. acpi_integer design_capacity_low;
  90. acpi_integer battery_capacity_granularity_1;
  91. acpi_integer battery_capacity_granularity_2;
  92. acpi_string model_number;
  93. acpi_string serial_number;
  94. acpi_string battery_type;
  95. acpi_string oem_info;
  96. };
  97. enum acpi_battery_files{
  98. ACPI_BATTERY_INFO = 0,
  99. ACPI_BATTERY_STATE,
  100. ACPI_BATTERY_ALARM,
  101. ACPI_BATTERY_NUMFILES,
  102. };
  103. struct acpi_battery_flags {
  104. u8 battery_present_prev;
  105. u8 alarm_present;
  106. u8 init_update;
  107. u8 update[ACPI_BATTERY_NUMFILES];
  108. u8 power_unit;
  109. };
  110. struct acpi_battery {
  111. struct mutex mutex;
  112. struct acpi_device *device;
  113. struct acpi_battery_flags flags;
  114. struct acpi_buffer bif_data;
  115. struct acpi_buffer bst_data;
  116. unsigned long alarm;
  117. unsigned long update_time[ACPI_BATTERY_NUMFILES];
  118. };
  119. inline int acpi_battery_present(struct acpi_battery *battery)
  120. {
  121. return battery->device->status.battery_present;
  122. }
  123. inline char *acpi_battery_power_units(struct acpi_battery *battery)
  124. {
  125. if (battery->flags.power_unit)
  126. return ACPI_BATTERY_UNITS_AMPS;
  127. else
  128. return ACPI_BATTERY_UNITS_WATTS;
  129. }
  130. inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
  131. {
  132. return battery->device->handle;
  133. }
  134. /* --------------------------------------------------------------------------
  135. Battery Management
  136. -------------------------------------------------------------------------- */
  137. static void acpi_battery_check_result(struct acpi_battery *battery, int result)
  138. {
  139. if (!battery)
  140. return;
  141. if (result) {
  142. battery->flags.init_update = 1;
  143. }
  144. }
  145. static int acpi_battery_extract_package(struct acpi_battery *battery,
  146. union acpi_object *package,
  147. struct acpi_buffer *format,
  148. struct acpi_buffer *data,
  149. char *package_name)
  150. {
  151. acpi_status status = AE_OK;
  152. struct acpi_buffer data_null = { 0, NULL };
  153. status = acpi_extract_package(package, format, &data_null);
  154. if (status != AE_BUFFER_OVERFLOW) {
  155. ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
  156. package_name));
  157. return -ENODEV;
  158. }
  159. if (data_null.length != data->length) {
  160. kfree(data->pointer);
  161. data->pointer = kzalloc(data_null.length, GFP_KERNEL);
  162. if (!data->pointer) {
  163. ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
  164. return -ENOMEM;
  165. }
  166. data->length = data_null.length;
  167. }
  168. status = acpi_extract_package(package, format, data);
  169. if (ACPI_FAILURE(status)) {
  170. ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
  171. package_name));
  172. return -ENODEV;
  173. }
  174. return 0;
  175. }
  176. static int acpi_battery_get_status(struct acpi_battery *battery)
  177. {
  178. int result = 0;
  179. result = acpi_bus_get_status(battery->device);
  180. if (result) {
  181. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  182. return -ENODEV;
  183. }
  184. return result;
  185. }
  186. static int acpi_battery_get_info(struct acpi_battery *battery)
  187. {
  188. int result = 0;
  189. acpi_status status = 0;
  190. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  191. struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
  192. ACPI_BATTERY_FORMAT_BIF
  193. };
  194. union acpi_object *package = NULL;
  195. struct acpi_buffer *data = NULL;
  196. struct acpi_battery_info *bif = NULL;
  197. battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
  198. if (!acpi_battery_present(battery))
  199. return 0;
  200. /* Evaluate _BIF */
  201. status =
  202. acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL,
  203. &buffer);
  204. if (ACPI_FAILURE(status)) {
  205. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
  206. return -ENODEV;
  207. }
  208. package = buffer.pointer;
  209. data = &battery->bif_data;
  210. /* Extract Package Data */
  211. result =
  212. acpi_battery_extract_package(battery, package, &format, data,
  213. "_BIF");
  214. if (result)
  215. goto end;
  216. end:
  217. kfree(buffer.pointer);
  218. if (!result) {
  219. bif = data->pointer;
  220. battery->flags.power_unit = bif->power_unit;
  221. }
  222. return result;
  223. }
  224. static int acpi_battery_get_state(struct acpi_battery *battery)
  225. {
  226. int result = 0;
  227. acpi_status status = 0;
  228. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  229. struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
  230. ACPI_BATTERY_FORMAT_BST
  231. };
  232. union acpi_object *package = NULL;
  233. struct acpi_buffer *data = NULL;
  234. battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
  235. if (!acpi_battery_present(battery))
  236. return 0;
  237. /* Evaluate _BST */
  238. status =
  239. acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL,
  240. &buffer);
  241. if (ACPI_FAILURE(status)) {
  242. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  243. return -ENODEV;
  244. }
  245. package = buffer.pointer;
  246. data = &battery->bst_data;
  247. /* Extract Package Data */
  248. result =
  249. acpi_battery_extract_package(battery, package, &format, data,
  250. "_BST");
  251. if (result)
  252. goto end;
  253. end:
  254. kfree(buffer.pointer);
  255. return result;
  256. }
  257. static int acpi_battery_get_alarm(struct acpi_battery *battery)
  258. {
  259. battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
  260. return 0;
  261. }
  262. static int acpi_battery_set_alarm(struct acpi_battery *battery,
  263. unsigned long alarm)
  264. {
  265. acpi_status status = 0;
  266. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  267. struct acpi_object_list arg_list = { 1, &arg0 };
  268. battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
  269. if (!acpi_battery_present(battery))
  270. return -ENODEV;
  271. if (!battery->flags.alarm_present)
  272. return -ENODEV;
  273. arg0.integer.value = alarm;
  274. status =
  275. acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
  276. &arg_list, NULL);
  277. if (ACPI_FAILURE(status))
  278. return -ENODEV;
  279. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
  280. battery->alarm = alarm;
  281. return 0;
  282. }
  283. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  284. {
  285. int result = 0;
  286. acpi_status status = AE_OK;
  287. acpi_handle handle = NULL;
  288. struct acpi_battery_info *bif = battery->bif_data.pointer;
  289. unsigned long alarm = battery->alarm;
  290. /* See if alarms are supported, and if so, set default */
  291. status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
  292. if (ACPI_SUCCESS(status)) {
  293. battery->flags.alarm_present = 1;
  294. if (!alarm && bif) {
  295. alarm = bif->design_capacity_warning;
  296. }
  297. result = acpi_battery_set_alarm(battery, alarm);
  298. if (result)
  299. goto end;
  300. } else {
  301. battery->flags.alarm_present = 0;
  302. }
  303. end:
  304. return result;
  305. }
  306. static int acpi_battery_init_update(struct acpi_battery *battery)
  307. {
  308. int result = 0;
  309. result = acpi_battery_get_status(battery);
  310. if (result)
  311. return result;
  312. battery->flags.battery_present_prev = acpi_battery_present(battery);
  313. if (acpi_battery_present(battery)) {
  314. result = acpi_battery_get_info(battery);
  315. if (result)
  316. return result;
  317. result = acpi_battery_get_state(battery);
  318. if (result)
  319. return result;
  320. acpi_battery_init_alarm(battery);
  321. }
  322. return result;
  323. }
  324. static int acpi_battery_update(struct acpi_battery *battery,
  325. int update, int *update_result_ptr)
  326. {
  327. int result = 0;
  328. int update_result = ACPI_BATTERY_NONE_UPDATE;
  329. if (!acpi_battery_present(battery)) {
  330. update = 1;
  331. }
  332. if (battery->flags.init_update) {
  333. result = acpi_battery_init_update(battery);
  334. if (result)
  335. goto end;
  336. update_result = ACPI_BATTERY_INIT_UPDATE;
  337. } else if (update) {
  338. result = acpi_battery_get_status(battery);
  339. if (result)
  340. goto end;
  341. if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
  342. || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
  343. result = acpi_battery_init_update(battery);
  344. if (result)
  345. goto end;
  346. update_result = ACPI_BATTERY_INIT_UPDATE;
  347. } else {
  348. update_result = ACPI_BATTERY_EASY_UPDATE;
  349. }
  350. }
  351. end:
  352. battery->flags.init_update = (result != 0);
  353. *update_result_ptr = update_result;
  354. return result;
  355. }
  356. static void acpi_battery_notify_update(struct acpi_battery *battery)
  357. {
  358. acpi_battery_get_status(battery);
  359. if (battery->flags.init_update) {
  360. return;
  361. }
  362. if ((!battery->flags.battery_present_prev &
  363. acpi_battery_present(battery)) ||
  364. (battery->flags.battery_present_prev &
  365. !acpi_battery_present(battery))) {
  366. battery->flags.init_update = 1;
  367. } else {
  368. battery->flags.update[ACPI_BATTERY_INFO] = 1;
  369. battery->flags.update[ACPI_BATTERY_STATE] = 1;
  370. battery->flags.update[ACPI_BATTERY_ALARM] = 1;
  371. }
  372. }
  373. /* --------------------------------------------------------------------------
  374. FS Interface (/proc)
  375. -------------------------------------------------------------------------- */
  376. static struct proc_dir_entry *acpi_battery_dir;
  377. static int acpi_battery_print_info(struct seq_file *seq, int result)
  378. {
  379. struct acpi_battery *battery = seq->private;
  380. struct acpi_battery_info *bif = NULL;
  381. char *units = "?";
  382. if (result)
  383. goto end;
  384. if (acpi_battery_present(battery))
  385. seq_printf(seq, "present: yes\n");
  386. else {
  387. seq_printf(seq, "present: no\n");
  388. goto end;
  389. }
  390. bif = battery->bif_data.pointer;
  391. if (!bif) {
  392. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
  393. result = -ENODEV;
  394. goto end;
  395. }
  396. /* Battery Units */
  397. units = acpi_battery_power_units(battery);
  398. if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  399. seq_printf(seq, "design capacity: unknown\n");
  400. else
  401. seq_printf(seq, "design capacity: %d %sh\n",
  402. (u32) bif->design_capacity, units);
  403. if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  404. seq_printf(seq, "last full capacity: unknown\n");
  405. else
  406. seq_printf(seq, "last full capacity: %d %sh\n",
  407. (u32) bif->last_full_capacity, units);
  408. switch ((u32) bif->battery_technology) {
  409. case 0:
  410. seq_printf(seq, "battery technology: non-rechargeable\n");
  411. break;
  412. case 1:
  413. seq_printf(seq, "battery technology: rechargeable\n");
  414. break;
  415. default:
  416. seq_printf(seq, "battery technology: unknown\n");
  417. break;
  418. }
  419. if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  420. seq_printf(seq, "design voltage: unknown\n");
  421. else
  422. seq_printf(seq, "design voltage: %d mV\n",
  423. (u32) bif->design_voltage);
  424. seq_printf(seq, "design capacity warning: %d %sh\n",
  425. (u32) bif->design_capacity_warning, units);
  426. seq_printf(seq, "design capacity low: %d %sh\n",
  427. (u32) bif->design_capacity_low, units);
  428. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  429. (u32) bif->battery_capacity_granularity_1, units);
  430. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  431. (u32) bif->battery_capacity_granularity_2, units);
  432. seq_printf(seq, "model number: %s\n", bif->model_number);
  433. seq_printf(seq, "serial number: %s\n", bif->serial_number);
  434. seq_printf(seq, "battery type: %s\n", bif->battery_type);
  435. seq_printf(seq, "OEM info: %s\n", bif->oem_info);
  436. end:
  437. if (result)
  438. seq_printf(seq, "ERROR: Unable to read battery info\n");
  439. return result;
  440. }
  441. static int acpi_battery_print_state(struct seq_file *seq, int result)
  442. {
  443. struct acpi_battery *battery = seq->private;
  444. struct acpi_battery_state *bst = NULL;
  445. char *units = "?";
  446. if (result)
  447. goto end;
  448. if (acpi_battery_present(battery))
  449. seq_printf(seq, "present: yes\n");
  450. else {
  451. seq_printf(seq, "present: no\n");
  452. goto end;
  453. }
  454. bst = battery->bst_data.pointer;
  455. if (!bst) {
  456. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
  457. result = -ENODEV;
  458. goto end;
  459. }
  460. /* Battery Units */
  461. units = acpi_battery_power_units(battery);
  462. if (!(bst->state & 0x04))
  463. seq_printf(seq, "capacity state: ok\n");
  464. else
  465. seq_printf(seq, "capacity state: critical\n");
  466. if ((bst->state & 0x01) && (bst->state & 0x02)) {
  467. seq_printf(seq,
  468. "charging state: charging/discharging\n");
  469. } else if (bst->state & 0x01)
  470. seq_printf(seq, "charging state: discharging\n");
  471. else if (bst->state & 0x02)
  472. seq_printf(seq, "charging state: charging\n");
  473. else {
  474. seq_printf(seq, "charging state: charged\n");
  475. }
  476. if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
  477. seq_printf(seq, "present rate: unknown\n");
  478. else
  479. seq_printf(seq, "present rate: %d %s\n",
  480. (u32) bst->present_rate, units);
  481. if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  482. seq_printf(seq, "remaining capacity: unknown\n");
  483. else
  484. seq_printf(seq, "remaining capacity: %d %sh\n",
  485. (u32) bst->remaining_capacity, units);
  486. if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  487. seq_printf(seq, "present voltage: unknown\n");
  488. else
  489. seq_printf(seq, "present voltage: %d mV\n",
  490. (u32) bst->present_voltage);
  491. end:
  492. if (result) {
  493. seq_printf(seq, "ERROR: Unable to read battery state\n");
  494. }
  495. return result;
  496. }
  497. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  498. {
  499. struct acpi_battery *battery = seq->private;
  500. char *units = "?";
  501. if (result)
  502. goto end;
  503. if (!acpi_battery_present(battery)) {
  504. seq_printf(seq, "present: no\n");
  505. goto end;
  506. }
  507. /* Battery Units */
  508. units = acpi_battery_power_units(battery);
  509. seq_printf(seq, "alarm: ");
  510. if (!battery->alarm)
  511. seq_printf(seq, "unsupported\n");
  512. else
  513. seq_printf(seq, "%lu %sh\n", battery->alarm, units);
  514. end:
  515. if (result)
  516. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  517. return result;
  518. }
  519. static ssize_t
  520. acpi_battery_write_alarm(struct file *file,
  521. const char __user * buffer,
  522. size_t count, loff_t * ppos)
  523. {
  524. int result = 0;
  525. char alarm_string[12] = { '\0' };
  526. struct seq_file *m = file->private_data;
  527. struct acpi_battery *battery = m->private;
  528. int update_result = ACPI_BATTERY_NONE_UPDATE;
  529. if (!battery || (count > sizeof(alarm_string) - 1))
  530. return -EINVAL;
  531. mutex_lock(&battery->mutex);
  532. result = acpi_battery_update(battery, 1, &update_result);
  533. if (result) {
  534. result = -ENODEV;
  535. goto end;
  536. }
  537. if (!acpi_battery_present(battery)) {
  538. result = -ENODEV;
  539. goto end;
  540. }
  541. if (copy_from_user(alarm_string, buffer, count)) {
  542. result = -EFAULT;
  543. goto end;
  544. }
  545. alarm_string[count] = '\0';
  546. result = acpi_battery_set_alarm(battery,
  547. simple_strtoul(alarm_string, NULL, 0));
  548. if (result)
  549. goto end;
  550. end:
  551. acpi_battery_check_result(battery, result);
  552. if (!result)
  553. result = count;
  554. mutex_unlock(&battery->mutex);
  555. return result;
  556. }
  557. typedef int(*print_func)(struct seq_file *seq, int result);
  558. typedef int(*get_func)(struct acpi_battery *battery);
  559. static struct acpi_read_mux {
  560. print_func print;
  561. get_func get;
  562. } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
  563. {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
  564. {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
  565. {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
  566. };
  567. static int acpi_battery_read(int fid, struct seq_file *seq)
  568. {
  569. struct acpi_battery *battery = seq->private;
  570. int result = 0;
  571. int update_result = ACPI_BATTERY_NONE_UPDATE;
  572. int update = 0;
  573. mutex_lock(&battery->mutex);
  574. update = (get_seconds() - battery->update_time[fid] >= update_time);
  575. update = (update | battery->flags.update[fid]);
  576. result = acpi_battery_update(battery, update, &update_result);
  577. if (result)
  578. goto end;
  579. if (update_result == ACPI_BATTERY_EASY_UPDATE) {
  580. result = acpi_read_funcs[fid].get(battery);
  581. if (result)
  582. goto end;
  583. }
  584. end:
  585. result = acpi_read_funcs[fid].print(seq, result);
  586. acpi_battery_check_result(battery, result);
  587. battery->flags.update[fid] = result;
  588. mutex_unlock(&battery->mutex);
  589. return result;
  590. }
  591. static int acpi_battery_read_info(struct seq_file *seq, void *offset)
  592. {
  593. return acpi_battery_read(ACPI_BATTERY_INFO, seq);
  594. }
  595. static int acpi_battery_read_state(struct seq_file *seq, void *offset)
  596. {
  597. return acpi_battery_read(ACPI_BATTERY_STATE, seq);
  598. }
  599. static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
  600. {
  601. return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
  602. }
  603. static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
  604. {
  605. return single_open(file, acpi_battery_read_info, PDE(inode)->data);
  606. }
  607. static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
  608. {
  609. return single_open(file, acpi_battery_read_state, PDE(inode)->data);
  610. }
  611. static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
  612. {
  613. return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
  614. }
  615. static struct battery_file {
  616. struct file_operations ops;
  617. mode_t mode;
  618. char *name;
  619. } acpi_battery_file[] = {
  620. {
  621. .name = "info",
  622. .mode = S_IRUGO,
  623. .ops = {
  624. .open = acpi_battery_info_open_fs,
  625. .read = seq_read,
  626. .llseek = seq_lseek,
  627. .release = single_release,
  628. .owner = THIS_MODULE,
  629. },
  630. },
  631. {
  632. .name = "state",
  633. .mode = S_IRUGO,
  634. .ops = {
  635. .open = acpi_battery_state_open_fs,
  636. .read = seq_read,
  637. .llseek = seq_lseek,
  638. .release = single_release,
  639. .owner = THIS_MODULE,
  640. },
  641. },
  642. {
  643. .name = "alarm",
  644. .mode = S_IFREG | S_IRUGO | S_IWUSR,
  645. .ops = {
  646. .open = acpi_battery_alarm_open_fs,
  647. .read = seq_read,
  648. .write = acpi_battery_write_alarm,
  649. .llseek = seq_lseek,
  650. .release = single_release,
  651. .owner = THIS_MODULE,
  652. },
  653. },
  654. };
  655. static int acpi_battery_add_fs(struct acpi_device *device)
  656. {
  657. struct proc_dir_entry *entry = NULL;
  658. int i;
  659. if (!acpi_device_dir(device)) {
  660. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  661. acpi_battery_dir);
  662. if (!acpi_device_dir(device))
  663. return -ENODEV;
  664. acpi_device_dir(device)->owner = THIS_MODULE;
  665. }
  666. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  667. entry = create_proc_entry(acpi_battery_file[i].name,
  668. acpi_battery_file[i].mode, acpi_device_dir(device));
  669. if (!entry)
  670. return -ENODEV;
  671. else {
  672. entry->proc_fops = &acpi_battery_file[i].ops;
  673. entry->data = acpi_driver_data(device);
  674. entry->owner = THIS_MODULE;
  675. }
  676. }
  677. return 0;
  678. }
  679. static int acpi_battery_remove_fs(struct acpi_device *device)
  680. {
  681. int i;
  682. if (acpi_device_dir(device)) {
  683. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  684. remove_proc_entry(acpi_battery_file[i].name,
  685. acpi_device_dir(device));
  686. }
  687. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  688. acpi_device_dir(device) = NULL;
  689. }
  690. return 0;
  691. }
  692. /* --------------------------------------------------------------------------
  693. Driver Interface
  694. -------------------------------------------------------------------------- */
  695. static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
  696. {
  697. struct acpi_battery *battery = data;
  698. struct acpi_device *device = NULL;
  699. if (!battery)
  700. return;
  701. device = battery->device;
  702. switch (event) {
  703. case ACPI_BATTERY_NOTIFY_STATUS:
  704. case ACPI_BATTERY_NOTIFY_INFO:
  705. case ACPI_NOTIFY_BUS_CHECK:
  706. case ACPI_NOTIFY_DEVICE_CHECK:
  707. device = battery->device;
  708. acpi_battery_notify_update(battery);
  709. acpi_bus_generate_proc_event(device, event,
  710. acpi_battery_present(battery));
  711. acpi_bus_generate_netlink_event(device->pnp.device_class,
  712. device->dev.bus_id, event,
  713. acpi_battery_present(battery));
  714. break;
  715. default:
  716. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  717. "Unsupported event [0x%x]\n", event));
  718. break;
  719. }
  720. return;
  721. }
  722. static int acpi_battery_add(struct acpi_device *device)
  723. {
  724. int result = 0;
  725. acpi_status status = 0;
  726. struct acpi_battery *battery = NULL;
  727. if (!device)
  728. return -EINVAL;
  729. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  730. if (!battery)
  731. return -ENOMEM;
  732. mutex_init(&battery->mutex);
  733. mutex_lock(&battery->mutex);
  734. battery->device = device;
  735. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  736. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  737. acpi_driver_data(device) = battery;
  738. result = acpi_battery_get_status(battery);
  739. if (result)
  740. goto end;
  741. battery->flags.init_update = 1;
  742. result = acpi_battery_add_fs(device);
  743. if (result)
  744. goto end;
  745. status = acpi_install_notify_handler(device->handle,
  746. ACPI_ALL_NOTIFY,
  747. acpi_battery_notify, battery);
  748. if (ACPI_FAILURE(status)) {
  749. ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
  750. result = -ENODEV;
  751. goto end;
  752. }
  753. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  754. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  755. device->status.battery_present ? "present" : "absent");
  756. end:
  757. if (result) {
  758. acpi_battery_remove_fs(device);
  759. kfree(battery);
  760. }
  761. mutex_unlock(&battery->mutex);
  762. return result;
  763. }
  764. static int acpi_battery_remove(struct acpi_device *device, int type)
  765. {
  766. acpi_status status = 0;
  767. struct acpi_battery *battery = NULL;
  768. if (!device || !acpi_driver_data(device))
  769. return -EINVAL;
  770. battery = acpi_driver_data(device);
  771. mutex_lock(&battery->mutex);
  772. status = acpi_remove_notify_handler(device->handle,
  773. ACPI_ALL_NOTIFY,
  774. acpi_battery_notify);
  775. acpi_battery_remove_fs(device);
  776. kfree(battery->bif_data.pointer);
  777. kfree(battery->bst_data.pointer);
  778. mutex_unlock(&battery->mutex);
  779. mutex_destroy(&battery->mutex);
  780. kfree(battery);
  781. return 0;
  782. }
  783. /* this is needed to learn about changes made in suspended state */
  784. static int acpi_battery_resume(struct acpi_device *device)
  785. {
  786. struct acpi_battery *battery;
  787. if (!device)
  788. return -EINVAL;
  789. battery = device->driver_data;
  790. battery->flags.init_update = 1;
  791. return 0;
  792. }
  793. static int __init acpi_battery_init(void)
  794. {
  795. int result;
  796. if (acpi_disabled)
  797. return -ENODEV;
  798. acpi_battery_dir = acpi_lock_battery_dir();
  799. if (!acpi_battery_dir)
  800. return -ENODEV;
  801. result = acpi_bus_register_driver(&acpi_battery_driver);
  802. if (result < 0) {
  803. acpi_unlock_battery_dir(acpi_battery_dir);
  804. return -ENODEV;
  805. }
  806. return 0;
  807. }
  808. static void __exit acpi_battery_exit(void)
  809. {
  810. acpi_bus_unregister_driver(&acpi_battery_driver);
  811. acpi_unlock_battery_dir(acpi_battery_dir);
  812. return;
  813. }
  814. module_init(acpi_battery_init);
  815. module_exit(acpi_battery_exit);