battery.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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 acpi_device *device;
  112. struct acpi_battery_flags flags;
  113. struct acpi_buffer bif_data;
  114. struct acpi_buffer bst_data;
  115. struct mutex lock;
  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. mutex_lock(&battery->lock);
  202. status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF",
  203. NULL, &buffer);
  204. mutex_unlock(&battery->lock);
  205. if (ACPI_FAILURE(status)) {
  206. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
  207. return -ENODEV;
  208. }
  209. package = buffer.pointer;
  210. data = &battery->bif_data;
  211. /* Extract Package Data */
  212. result =
  213. acpi_battery_extract_package(battery, package, &format, data,
  214. "_BIF");
  215. if (result)
  216. goto end;
  217. end:
  218. kfree(buffer.pointer);
  219. if (!result) {
  220. bif = data->pointer;
  221. battery->flags.power_unit = bif->power_unit;
  222. }
  223. return result;
  224. }
  225. static int acpi_battery_get_state(struct acpi_battery *battery)
  226. {
  227. int result = 0;
  228. acpi_status status = 0;
  229. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  230. struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
  231. ACPI_BATTERY_FORMAT_BST
  232. };
  233. union acpi_object *package = NULL;
  234. struct acpi_buffer *data = NULL;
  235. battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
  236. if (!acpi_battery_present(battery))
  237. return 0;
  238. /* Evaluate _BST */
  239. mutex_lock(&battery->lock);
  240. status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST",
  241. NULL, &buffer);
  242. mutex_unlock(&battery->lock);
  243. if (ACPI_FAILURE(status)) {
  244. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  245. return -ENODEV;
  246. }
  247. package = buffer.pointer;
  248. data = &battery->bst_data;
  249. /* Extract Package Data */
  250. result =
  251. acpi_battery_extract_package(battery, package, &format, data,
  252. "_BST");
  253. if (result)
  254. goto end;
  255. end:
  256. kfree(buffer.pointer);
  257. return result;
  258. }
  259. static int acpi_battery_get_alarm(struct acpi_battery *battery)
  260. {
  261. battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
  262. return 0;
  263. }
  264. static int acpi_battery_set_alarm(struct acpi_battery *battery,
  265. unsigned long alarm)
  266. {
  267. acpi_status status = 0;
  268. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  269. struct acpi_object_list arg_list = { 1, &arg0 };
  270. battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
  271. if (!acpi_battery_present(battery))
  272. return -ENODEV;
  273. if (!battery->flags.alarm_present)
  274. return -ENODEV;
  275. arg0.integer.value = alarm;
  276. mutex_lock(&battery->lock);
  277. status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
  278. &arg_list, NULL);
  279. mutex_unlock(&battery->lock);
  280. if (ACPI_FAILURE(status))
  281. return -ENODEV;
  282. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
  283. battery->alarm = alarm;
  284. return 0;
  285. }
  286. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  287. {
  288. int result = 0;
  289. acpi_status status = AE_OK;
  290. acpi_handle handle = NULL;
  291. struct acpi_battery_info *bif = battery->bif_data.pointer;
  292. unsigned long alarm = battery->alarm;
  293. /* See if alarms are supported, and if so, set default */
  294. status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
  295. if (ACPI_SUCCESS(status)) {
  296. battery->flags.alarm_present = 1;
  297. if (!alarm && bif) {
  298. alarm = bif->design_capacity_warning;
  299. }
  300. result = acpi_battery_set_alarm(battery, alarm);
  301. if (result)
  302. goto end;
  303. } else {
  304. battery->flags.alarm_present = 0;
  305. }
  306. end:
  307. return result;
  308. }
  309. static int acpi_battery_init_update(struct acpi_battery *battery)
  310. {
  311. int result = 0;
  312. result = acpi_battery_get_status(battery);
  313. if (result)
  314. return result;
  315. battery->flags.battery_present_prev = acpi_battery_present(battery);
  316. if (acpi_battery_present(battery)) {
  317. result = acpi_battery_get_info(battery);
  318. if (result)
  319. return result;
  320. result = acpi_battery_get_state(battery);
  321. if (result)
  322. return result;
  323. acpi_battery_init_alarm(battery);
  324. }
  325. return result;
  326. }
  327. static int acpi_battery_update(struct acpi_battery *battery,
  328. int update, int *update_result_ptr)
  329. {
  330. int result = 0;
  331. int update_result = ACPI_BATTERY_NONE_UPDATE;
  332. if (!acpi_battery_present(battery)) {
  333. update = 1;
  334. }
  335. if (battery->flags.init_update) {
  336. result = acpi_battery_init_update(battery);
  337. if (result)
  338. goto end;
  339. update_result = ACPI_BATTERY_INIT_UPDATE;
  340. } else if (update) {
  341. result = acpi_battery_get_status(battery);
  342. if (result)
  343. goto end;
  344. if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
  345. || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
  346. result = acpi_battery_init_update(battery);
  347. if (result)
  348. goto end;
  349. update_result = ACPI_BATTERY_INIT_UPDATE;
  350. } else {
  351. update_result = ACPI_BATTERY_EASY_UPDATE;
  352. }
  353. }
  354. end:
  355. battery->flags.init_update = (result != 0);
  356. *update_result_ptr = update_result;
  357. return result;
  358. }
  359. static void acpi_battery_notify_update(struct acpi_battery *battery)
  360. {
  361. acpi_battery_get_status(battery);
  362. if (battery->flags.init_update) {
  363. return;
  364. }
  365. if ((!battery->flags.battery_present_prev &
  366. acpi_battery_present(battery)) ||
  367. (battery->flags.battery_present_prev &
  368. !acpi_battery_present(battery))) {
  369. battery->flags.init_update = 1;
  370. } else {
  371. battery->flags.update[ACPI_BATTERY_INFO] = 1;
  372. battery->flags.update[ACPI_BATTERY_STATE] = 1;
  373. battery->flags.update[ACPI_BATTERY_ALARM] = 1;
  374. }
  375. }
  376. /* --------------------------------------------------------------------------
  377. FS Interface (/proc)
  378. -------------------------------------------------------------------------- */
  379. static struct proc_dir_entry *acpi_battery_dir;
  380. static int acpi_battery_print_info(struct seq_file *seq, int result)
  381. {
  382. struct acpi_battery *battery = seq->private;
  383. struct acpi_battery_info *bif = NULL;
  384. char *units = "?";
  385. if (result)
  386. goto end;
  387. if (acpi_battery_present(battery))
  388. seq_printf(seq, "present: yes\n");
  389. else {
  390. seq_printf(seq, "present: no\n");
  391. goto end;
  392. }
  393. bif = battery->bif_data.pointer;
  394. if (!bif) {
  395. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
  396. result = -ENODEV;
  397. goto end;
  398. }
  399. /* Battery Units */
  400. units = acpi_battery_power_units(battery);
  401. if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  402. seq_printf(seq, "design capacity: unknown\n");
  403. else
  404. seq_printf(seq, "design capacity: %d %sh\n",
  405. (u32) bif->design_capacity, units);
  406. if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  407. seq_printf(seq, "last full capacity: unknown\n");
  408. else
  409. seq_printf(seq, "last full capacity: %d %sh\n",
  410. (u32) bif->last_full_capacity, units);
  411. switch ((u32) bif->battery_technology) {
  412. case 0:
  413. seq_printf(seq, "battery technology: non-rechargeable\n");
  414. break;
  415. case 1:
  416. seq_printf(seq, "battery technology: rechargeable\n");
  417. break;
  418. default:
  419. seq_printf(seq, "battery technology: unknown\n");
  420. break;
  421. }
  422. if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  423. seq_printf(seq, "design voltage: unknown\n");
  424. else
  425. seq_printf(seq, "design voltage: %d mV\n",
  426. (u32) bif->design_voltage);
  427. seq_printf(seq, "design capacity warning: %d %sh\n",
  428. (u32) bif->design_capacity_warning, units);
  429. seq_printf(seq, "design capacity low: %d %sh\n",
  430. (u32) bif->design_capacity_low, units);
  431. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  432. (u32) bif->battery_capacity_granularity_1, units);
  433. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  434. (u32) bif->battery_capacity_granularity_2, units);
  435. seq_printf(seq, "model number: %s\n", bif->model_number);
  436. seq_printf(seq, "serial number: %s\n", bif->serial_number);
  437. seq_printf(seq, "battery type: %s\n", bif->battery_type);
  438. seq_printf(seq, "OEM info: %s\n", bif->oem_info);
  439. end:
  440. if (result)
  441. seq_printf(seq, "ERROR: Unable to read battery info\n");
  442. return result;
  443. }
  444. static int acpi_battery_print_state(struct seq_file *seq, int result)
  445. {
  446. struct acpi_battery *battery = seq->private;
  447. struct acpi_battery_state *bst = NULL;
  448. char *units = "?";
  449. if (result)
  450. goto end;
  451. if (acpi_battery_present(battery))
  452. seq_printf(seq, "present: yes\n");
  453. else {
  454. seq_printf(seq, "present: no\n");
  455. goto end;
  456. }
  457. bst = battery->bst_data.pointer;
  458. if (!bst) {
  459. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
  460. result = -ENODEV;
  461. goto end;
  462. }
  463. /* Battery Units */
  464. units = acpi_battery_power_units(battery);
  465. if (!(bst->state & 0x04))
  466. seq_printf(seq, "capacity state: ok\n");
  467. else
  468. seq_printf(seq, "capacity state: critical\n");
  469. if ((bst->state & 0x01) && (bst->state & 0x02)) {
  470. seq_printf(seq,
  471. "charging state: charging/discharging\n");
  472. } else if (bst->state & 0x01)
  473. seq_printf(seq, "charging state: discharging\n");
  474. else if (bst->state & 0x02)
  475. seq_printf(seq, "charging state: charging\n");
  476. else {
  477. seq_printf(seq, "charging state: charged\n");
  478. }
  479. if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
  480. seq_printf(seq, "present rate: unknown\n");
  481. else
  482. seq_printf(seq, "present rate: %d %s\n",
  483. (u32) bst->present_rate, units);
  484. if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  485. seq_printf(seq, "remaining capacity: unknown\n");
  486. else
  487. seq_printf(seq, "remaining capacity: %d %sh\n",
  488. (u32) bst->remaining_capacity, units);
  489. if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  490. seq_printf(seq, "present voltage: unknown\n");
  491. else
  492. seq_printf(seq, "present voltage: %d mV\n",
  493. (u32) bst->present_voltage);
  494. end:
  495. if (result) {
  496. seq_printf(seq, "ERROR: Unable to read battery state\n");
  497. }
  498. return result;
  499. }
  500. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  501. {
  502. struct acpi_battery *battery = seq->private;
  503. char *units = "?";
  504. if (result)
  505. goto end;
  506. if (!acpi_battery_present(battery)) {
  507. seq_printf(seq, "present: no\n");
  508. goto end;
  509. }
  510. /* Battery Units */
  511. units = acpi_battery_power_units(battery);
  512. seq_printf(seq, "alarm: ");
  513. if (!battery->alarm)
  514. seq_printf(seq, "unsupported\n");
  515. else
  516. seq_printf(seq, "%lu %sh\n", battery->alarm, units);
  517. end:
  518. if (result)
  519. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  520. return result;
  521. }
  522. static ssize_t
  523. acpi_battery_write_alarm(struct file *file,
  524. const char __user * buffer,
  525. size_t count, loff_t * ppos)
  526. {
  527. int result = 0;
  528. char alarm_string[12] = { '\0' };
  529. struct seq_file *m = file->private_data;
  530. struct acpi_battery *battery = m->private;
  531. int update_result = ACPI_BATTERY_NONE_UPDATE;
  532. if (!battery || (count > sizeof(alarm_string) - 1))
  533. return -EINVAL;
  534. result = acpi_battery_update(battery, 1, &update_result);
  535. if (result) {
  536. result = -ENODEV;
  537. goto end;
  538. }
  539. if (!acpi_battery_present(battery)) {
  540. result = -ENODEV;
  541. goto end;
  542. }
  543. if (copy_from_user(alarm_string, buffer, count)) {
  544. result = -EFAULT;
  545. goto end;
  546. }
  547. alarm_string[count] = '\0';
  548. result = acpi_battery_set_alarm(battery,
  549. simple_strtoul(alarm_string, NULL, 0));
  550. if (result)
  551. goto end;
  552. end:
  553. acpi_battery_check_result(battery, result);
  554. if (!result)
  555. return count;
  556. return result;
  557. }
  558. typedef int(*print_func)(struct seq_file *seq, int result);
  559. typedef int(*get_func)(struct acpi_battery *battery);
  560. static struct acpi_read_mux {
  561. print_func print;
  562. get_func get;
  563. } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
  564. {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
  565. {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
  566. {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
  567. };
  568. static int acpi_battery_read(int fid, struct seq_file *seq)
  569. {
  570. struct acpi_battery *battery = seq->private;
  571. int result = 0;
  572. int update_result = ACPI_BATTERY_NONE_UPDATE;
  573. int update = 0;
  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. return result;
  589. }
  590. static int acpi_battery_read_info(struct seq_file *seq, void *offset)
  591. {
  592. return acpi_battery_read(ACPI_BATTERY_INFO, seq);
  593. }
  594. static int acpi_battery_read_state(struct seq_file *seq, void *offset)
  595. {
  596. return acpi_battery_read(ACPI_BATTERY_STATE, seq);
  597. }
  598. static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
  599. {
  600. return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
  601. }
  602. static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
  603. {
  604. return single_open(file, acpi_battery_read_info, PDE(inode)->data);
  605. }
  606. static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
  607. {
  608. return single_open(file, acpi_battery_read_state, PDE(inode)->data);
  609. }
  610. static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
  611. {
  612. return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
  613. }
  614. static struct battery_file {
  615. struct file_operations ops;
  616. mode_t mode;
  617. char *name;
  618. } acpi_battery_file[] = {
  619. {
  620. .name = "info",
  621. .mode = S_IRUGO,
  622. .ops = {
  623. .open = acpi_battery_info_open_fs,
  624. .read = seq_read,
  625. .llseek = seq_lseek,
  626. .release = single_release,
  627. .owner = THIS_MODULE,
  628. },
  629. },
  630. {
  631. .name = "state",
  632. .mode = S_IRUGO,
  633. .ops = {
  634. .open = acpi_battery_state_open_fs,
  635. .read = seq_read,
  636. .llseek = seq_lseek,
  637. .release = single_release,
  638. .owner = THIS_MODULE,
  639. },
  640. },
  641. {
  642. .name = "alarm",
  643. .mode = S_IFREG | S_IRUGO | S_IWUSR,
  644. .ops = {
  645. .open = acpi_battery_alarm_open_fs,
  646. .read = seq_read,
  647. .write = acpi_battery_write_alarm,
  648. .llseek = seq_lseek,
  649. .release = single_release,
  650. .owner = THIS_MODULE,
  651. },
  652. },
  653. };
  654. static int acpi_battery_add_fs(struct acpi_device *device)
  655. {
  656. struct proc_dir_entry *entry = NULL;
  657. int i;
  658. if (!acpi_device_dir(device)) {
  659. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  660. acpi_battery_dir);
  661. if (!acpi_device_dir(device))
  662. return -ENODEV;
  663. acpi_device_dir(device)->owner = THIS_MODULE;
  664. }
  665. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  666. entry = create_proc_entry(acpi_battery_file[i].name,
  667. acpi_battery_file[i].mode, acpi_device_dir(device));
  668. if (!entry)
  669. return -ENODEV;
  670. else {
  671. entry->proc_fops = &acpi_battery_file[i].ops;
  672. entry->data = acpi_driver_data(device);
  673. entry->owner = THIS_MODULE;
  674. }
  675. }
  676. return 0;
  677. }
  678. static int acpi_battery_remove_fs(struct acpi_device *device)
  679. {
  680. int i;
  681. if (acpi_device_dir(device)) {
  682. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  683. remove_proc_entry(acpi_battery_file[i].name,
  684. acpi_device_dir(device));
  685. }
  686. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  687. acpi_device_dir(device) = NULL;
  688. }
  689. return 0;
  690. }
  691. /* --------------------------------------------------------------------------
  692. Driver Interface
  693. -------------------------------------------------------------------------- */
  694. static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
  695. {
  696. struct acpi_battery *battery = data;
  697. struct acpi_device *device = NULL;
  698. if (!battery)
  699. return;
  700. device = battery->device;
  701. switch (event) {
  702. case ACPI_BATTERY_NOTIFY_STATUS:
  703. case ACPI_BATTERY_NOTIFY_INFO:
  704. case ACPI_NOTIFY_BUS_CHECK:
  705. case ACPI_NOTIFY_DEVICE_CHECK:
  706. device = battery->device;
  707. acpi_battery_notify_update(battery);
  708. acpi_bus_generate_event(device, event,
  709. acpi_battery_present(battery));
  710. break;
  711. default:
  712. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  713. "Unsupported event [0x%x]\n", event));
  714. break;
  715. }
  716. return;
  717. }
  718. static int acpi_battery_add(struct acpi_device *device)
  719. {
  720. int result = 0;
  721. acpi_status status = 0;
  722. struct acpi_battery *battery = NULL;
  723. if (!device)
  724. return -EINVAL;
  725. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  726. if (!battery)
  727. return -ENOMEM;
  728. mutex_init(&battery->lock);
  729. battery->device = device;
  730. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  731. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  732. acpi_driver_data(device) = battery;
  733. result = acpi_battery_get_status(battery);
  734. if (result)
  735. goto end;
  736. battery->flags.init_update = 1;
  737. result = acpi_battery_add_fs(device);
  738. if (result)
  739. goto end;
  740. status = acpi_install_notify_handler(device->handle,
  741. ACPI_ALL_NOTIFY,
  742. acpi_battery_notify, battery);
  743. if (ACPI_FAILURE(status)) {
  744. ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
  745. result = -ENODEV;
  746. goto end;
  747. }
  748. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  749. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  750. device->status.battery_present ? "present" : "absent");
  751. end:
  752. if (result) {
  753. acpi_battery_remove_fs(device);
  754. kfree(battery);
  755. }
  756. return result;
  757. }
  758. static int acpi_battery_remove(struct acpi_device *device, int type)
  759. {
  760. acpi_status status = 0;
  761. struct acpi_battery *battery = NULL;
  762. if (!device || !acpi_driver_data(device))
  763. return -EINVAL;
  764. battery = acpi_driver_data(device);
  765. status = acpi_remove_notify_handler(device->handle,
  766. ACPI_ALL_NOTIFY,
  767. acpi_battery_notify);
  768. acpi_battery_remove_fs(device);
  769. kfree(battery->bif_data.pointer);
  770. kfree(battery->bst_data.pointer);
  771. mutex_destroy(&battery->lock);
  772. kfree(battery);
  773. return 0;
  774. }
  775. /* this is needed to learn about changes made in suspended state */
  776. static int acpi_battery_resume(struct acpi_device *device)
  777. {
  778. struct acpi_battery *battery;
  779. if (!device)
  780. return -EINVAL;
  781. battery = device->driver_data;
  782. battery->flags.init_update = 1;
  783. return 0;
  784. }
  785. static int __init acpi_battery_init(void)
  786. {
  787. int result;
  788. if (acpi_disabled)
  789. return -ENODEV;
  790. acpi_battery_dir = acpi_lock_battery_dir();
  791. if (!acpi_battery_dir)
  792. return -ENODEV;
  793. result = acpi_bus_register_driver(&acpi_battery_driver);
  794. if (result < 0) {
  795. acpi_unlock_battery_dir(acpi_battery_dir);
  796. return -ENODEV;
  797. }
  798. return 0;
  799. }
  800. static void __exit acpi_battery_exit(void)
  801. {
  802. acpi_bus_unregister_driver(&acpi_battery_driver);
  803. acpi_unlock_battery_dir(acpi_battery_dir);
  804. return;
  805. }
  806. module_init(acpi_battery_init);
  807. module_exit(acpi_battery_exit);