ac.c 7.2 KB

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