ac.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * acpi_ac.c - ACPI AC Adapter Driver ($Revision: 27 $)
  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 <acpi/acpi_bus.h>
  32. #include <acpi/acpi_drivers.h>
  33. #define ACPI_AC_COMPONENT 0x00020000
  34. #define ACPI_AC_CLASS "ac_adapter"
  35. #define ACPI_AC_HID "ACPI0003"
  36. #define ACPI_AC_DRIVER_NAME "ACPI AC Adapter Driver"
  37. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  38. #define ACPI_AC_FILE_STATE "state"
  39. #define ACPI_AC_NOTIFY_STATUS 0x80
  40. #define ACPI_AC_STATUS_OFFLINE 0x00
  41. #define ACPI_AC_STATUS_ONLINE 0x01
  42. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  43. #define _COMPONENT ACPI_AC_COMPONENT
  44. ACPI_MODULE_NAME("acpi_ac")
  45. MODULE_AUTHOR("Paul Diefenbaugh");
  46. MODULE_DESCRIPTION(ACPI_AC_DRIVER_NAME);
  47. MODULE_LICENSE("GPL");
  48. extern struct proc_dir_entry *acpi_lock_ac_dir(void);
  49. extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
  50. static int acpi_ac_add(struct acpi_device *device);
  51. static int acpi_ac_remove(struct acpi_device *device, int type);
  52. static int acpi_ac_open_fs(struct inode *inode, struct file *file);
  53. static struct acpi_driver acpi_ac_driver = {
  54. .name = ACPI_AC_DRIVER_NAME,
  55. .class = ACPI_AC_CLASS,
  56. .ids = ACPI_AC_HID,
  57. .ops = {
  58. .add = acpi_ac_add,
  59. .remove = acpi_ac_remove,
  60. },
  61. };
  62. struct acpi_ac {
  63. struct acpi_device * device;
  64. unsigned long state;
  65. };
  66. static const struct file_operations acpi_ac_fops = {
  67. .open = acpi_ac_open_fs,
  68. .read = seq_read,
  69. .llseek = seq_lseek,
  70. .release = single_release,
  71. };
  72. /* --------------------------------------------------------------------------
  73. AC Adapter Management
  74. -------------------------------------------------------------------------- */
  75. static int acpi_ac_get_state(struct acpi_ac *ac)
  76. {
  77. acpi_status status = AE_OK;
  78. if (!ac)
  79. return -EINVAL;
  80. status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
  81. if (ACPI_FAILURE(status)) {
  82. ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
  83. ac->state = ACPI_AC_STATUS_UNKNOWN;
  84. return -ENODEV;
  85. }
  86. return 0;
  87. }
  88. /* --------------------------------------------------------------------------
  89. FS Interface (/proc)
  90. -------------------------------------------------------------------------- */
  91. static struct proc_dir_entry *acpi_ac_dir;
  92. static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
  93. {
  94. struct acpi_ac *ac = (struct acpi_ac *)seq->private;
  95. if (!ac)
  96. return 0;
  97. if (acpi_ac_get_state(ac)) {
  98. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  99. return 0;
  100. }
  101. seq_puts(seq, "state: ");
  102. switch (ac->state) {
  103. case ACPI_AC_STATUS_OFFLINE:
  104. seq_puts(seq, "off-line\n");
  105. break;
  106. case ACPI_AC_STATUS_ONLINE:
  107. seq_puts(seq, "on-line\n");
  108. break;
  109. default:
  110. seq_puts(seq, "unknown\n");
  111. break;
  112. }
  113. return 0;
  114. }
  115. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  116. {
  117. return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
  118. }
  119. static int acpi_ac_add_fs(struct acpi_device *device)
  120. {
  121. struct proc_dir_entry *entry = NULL;
  122. if (!acpi_device_dir(device)) {
  123. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  124. acpi_ac_dir);
  125. if (!acpi_device_dir(device))
  126. return -ENODEV;
  127. acpi_device_dir(device)->owner = THIS_MODULE;
  128. }
  129. /* 'state' [R] */
  130. entry = create_proc_entry(ACPI_AC_FILE_STATE,
  131. S_IRUGO, acpi_device_dir(device));
  132. if (!entry)
  133. return -ENODEV;
  134. else {
  135. entry->proc_fops = &acpi_ac_fops;
  136. entry->data = acpi_driver_data(device);
  137. entry->owner = THIS_MODULE;
  138. }
  139. return 0;
  140. }
  141. static int acpi_ac_remove_fs(struct acpi_device *device)
  142. {
  143. if (acpi_device_dir(device)) {
  144. remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
  145. remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
  146. acpi_device_dir(device) = NULL;
  147. }
  148. return 0;
  149. }
  150. /* --------------------------------------------------------------------------
  151. Driver Model
  152. -------------------------------------------------------------------------- */
  153. static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
  154. {
  155. struct acpi_ac *ac = (struct acpi_ac *)data;
  156. struct acpi_device *device = NULL;
  157. if (!ac)
  158. return;
  159. device = ac->device;
  160. switch (event) {
  161. case ACPI_AC_NOTIFY_STATUS:
  162. case ACPI_NOTIFY_BUS_CHECK:
  163. case ACPI_NOTIFY_DEVICE_CHECK:
  164. acpi_ac_get_state(ac);
  165. acpi_bus_generate_event(device, event, (u32) ac->state);
  166. break;
  167. default:
  168. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  169. "Unsupported event [0x%x]\n", event));
  170. break;
  171. }
  172. return;
  173. }
  174. static int acpi_ac_add(struct acpi_device *device)
  175. {
  176. int result = 0;
  177. acpi_status status = AE_OK;
  178. struct acpi_ac *ac = NULL;
  179. if (!device)
  180. return -EINVAL;
  181. ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  182. if (!ac)
  183. return -ENOMEM;
  184. memset(ac, 0, sizeof(struct acpi_ac));
  185. ac->device = device;
  186. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  187. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  188. acpi_driver_data(device) = ac;
  189. result = acpi_ac_get_state(ac);
  190. if (result)
  191. goto end;
  192. result = acpi_ac_add_fs(device);
  193. if (result)
  194. goto end;
  195. status = acpi_install_notify_handler(device->handle,
  196. ACPI_ALL_NOTIFY, acpi_ac_notify,
  197. ac);
  198. if (ACPI_FAILURE(status)) {
  199. result = -ENODEV;
  200. goto end;
  201. }
  202. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  203. acpi_device_name(device), acpi_device_bid(device),
  204. ac->state ? "on-line" : "off-line");
  205. end:
  206. if (result) {
  207. acpi_ac_remove_fs(device);
  208. kfree(ac);
  209. }
  210. return result;
  211. }
  212. static int acpi_ac_remove(struct acpi_device *device, int type)
  213. {
  214. acpi_status status = AE_OK;
  215. struct acpi_ac *ac = NULL;
  216. if (!device || !acpi_driver_data(device))
  217. return -EINVAL;
  218. ac = (struct acpi_ac *)acpi_driver_data(device);
  219. status = acpi_remove_notify_handler(device->handle,
  220. ACPI_ALL_NOTIFY, acpi_ac_notify);
  221. acpi_ac_remove_fs(device);
  222. kfree(ac);
  223. return 0;
  224. }
  225. static int __init acpi_ac_init(void)
  226. {
  227. int result;
  228. if (acpi_disabled)
  229. return -ENODEV;
  230. acpi_ac_dir = acpi_lock_ac_dir();
  231. if (!acpi_ac_dir)
  232. return -ENODEV;
  233. result = acpi_bus_register_driver(&acpi_ac_driver);
  234. if (result < 0) {
  235. acpi_unlock_ac_dir(acpi_ac_dir);
  236. return -ENODEV;
  237. }
  238. return 0;
  239. }
  240. static void __exit acpi_ac_exit(void)
  241. {
  242. acpi_bus_unregister_driver(&acpi_ac_driver);
  243. acpi_unlock_ac_dir(acpi_ac_dir);
  244. return;
  245. }
  246. module_init(acpi_ac_init);
  247. module_exit(acpi_ac_exit);