ac.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. acpi_handle handle;
  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. ACPI_FUNCTION_TRACE("acpi_ac_get_state");
  77. if (!ac)
  78. return_VALUE(-EINVAL);
  79. status = acpi_evaluate_integer(ac->handle, "_PSR", NULL, &ac->state);
  80. if (ACPI_FAILURE(status)) {
  81. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  82. "Error reading AC Adapter state\n"));
  83. ac->state = ACPI_AC_STATUS_UNKNOWN;
  84. return_VALUE(-ENODEV);
  85. }
  86. return_VALUE(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. ACPI_FUNCTION_TRACE("acpi_ac_seq_show");
  96. if (!ac)
  97. return_VALUE(0);
  98. if (acpi_ac_get_state(ac)) {
  99. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  100. return_VALUE(0);
  101. }
  102. seq_puts(seq, "state: ");
  103. switch (ac->state) {
  104. case ACPI_AC_STATUS_OFFLINE:
  105. seq_puts(seq, "off-line\n");
  106. break;
  107. case ACPI_AC_STATUS_ONLINE:
  108. seq_puts(seq, "on-line\n");
  109. break;
  110. default:
  111. seq_puts(seq, "unknown\n");
  112. break;
  113. }
  114. return_VALUE(0);
  115. }
  116. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  117. {
  118. return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
  119. }
  120. static int acpi_ac_add_fs(struct acpi_device *device)
  121. {
  122. struct proc_dir_entry *entry = NULL;
  123. ACPI_FUNCTION_TRACE("acpi_ac_add_fs");
  124. if (!acpi_device_dir(device)) {
  125. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  126. acpi_ac_dir);
  127. if (!acpi_device_dir(device))
  128. return_VALUE(-ENODEV);
  129. acpi_device_dir(device)->owner = THIS_MODULE;
  130. }
  131. /* 'state' [R] */
  132. entry = create_proc_entry(ACPI_AC_FILE_STATE,
  133. S_IRUGO, acpi_device_dir(device));
  134. if (!entry)
  135. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  136. "Unable to create '%s' fs entry\n",
  137. ACPI_AC_FILE_STATE));
  138. else {
  139. entry->proc_fops = &acpi_ac_fops;
  140. entry->data = acpi_driver_data(device);
  141. entry->owner = THIS_MODULE;
  142. }
  143. return_VALUE(0);
  144. }
  145. static int acpi_ac_remove_fs(struct acpi_device *device)
  146. {
  147. ACPI_FUNCTION_TRACE("acpi_ac_remove_fs");
  148. if (acpi_device_dir(device)) {
  149. remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
  150. remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
  151. acpi_device_dir(device) = NULL;
  152. }
  153. return_VALUE(0);
  154. }
  155. /* --------------------------------------------------------------------------
  156. Driver Model
  157. -------------------------------------------------------------------------- */
  158. static void acpi_ac_notify(acpi_handle handle, u32 event, void *data)
  159. {
  160. struct acpi_ac *ac = (struct acpi_ac *)data;
  161. struct acpi_device *device = NULL;
  162. ACPI_FUNCTION_TRACE("acpi_ac_notify");
  163. if (!ac)
  164. return_VOID;
  165. if (acpi_bus_get_device(ac->handle, &device))
  166. return_VOID;
  167. switch (event) {
  168. case ACPI_AC_NOTIFY_STATUS:
  169. acpi_ac_get_state(ac);
  170. acpi_bus_generate_event(device, event, (u32) ac->state);
  171. break;
  172. default:
  173. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  174. "Unsupported event [0x%x]\n", event));
  175. break;
  176. }
  177. return_VOID;
  178. }
  179. static int acpi_ac_add(struct acpi_device *device)
  180. {
  181. int result = 0;
  182. acpi_status status = AE_OK;
  183. struct acpi_ac *ac = NULL;
  184. ACPI_FUNCTION_TRACE("acpi_ac_add");
  185. if (!device)
  186. return_VALUE(-EINVAL);
  187. ac = kmalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  188. if (!ac)
  189. return_VALUE(-ENOMEM);
  190. memset(ac, 0, sizeof(struct acpi_ac));
  191. ac->handle = device->handle;
  192. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  193. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  194. acpi_driver_data(device) = ac;
  195. result = acpi_ac_get_state(ac);
  196. if (result)
  197. goto end;
  198. result = acpi_ac_add_fs(device);
  199. if (result)
  200. goto end;
  201. status = acpi_install_notify_handler(ac->handle,
  202. ACPI_DEVICE_NOTIFY, acpi_ac_notify,
  203. ac);
  204. if (ACPI_FAILURE(status)) {
  205. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  206. "Error installing notify handler\n"));
  207. result = -ENODEV;
  208. goto end;
  209. }
  210. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  211. acpi_device_name(device), acpi_device_bid(device),
  212. ac->state ? "on-line" : "off-line");
  213. end:
  214. if (result) {
  215. acpi_ac_remove_fs(device);
  216. kfree(ac);
  217. }
  218. return_VALUE(result);
  219. }
  220. static int acpi_ac_remove(struct acpi_device *device, int type)
  221. {
  222. acpi_status status = AE_OK;
  223. struct acpi_ac *ac = NULL;
  224. ACPI_FUNCTION_TRACE("acpi_ac_remove");
  225. if (!device || !acpi_driver_data(device))
  226. return_VALUE(-EINVAL);
  227. ac = (struct acpi_ac *)acpi_driver_data(device);
  228. status = acpi_remove_notify_handler(ac->handle,
  229. ACPI_DEVICE_NOTIFY, acpi_ac_notify);
  230. if (ACPI_FAILURE(status))
  231. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  232. "Error removing notify handler\n"));
  233. acpi_ac_remove_fs(device);
  234. kfree(ac);
  235. return_VALUE(0);
  236. }
  237. static int __init acpi_ac_init(void)
  238. {
  239. int result = 0;
  240. ACPI_FUNCTION_TRACE("acpi_ac_init");
  241. acpi_ac_dir = proc_mkdir(ACPI_AC_CLASS, acpi_root_dir);
  242. if (!acpi_ac_dir)
  243. return_VALUE(-ENODEV);
  244. acpi_ac_dir->owner = THIS_MODULE;
  245. result = acpi_bus_register_driver(&acpi_ac_driver);
  246. if (result < 0) {
  247. remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
  248. return_VALUE(-ENODEV);
  249. }
  250. return_VALUE(0);
  251. }
  252. static void __exit acpi_ac_exit(void)
  253. {
  254. ACPI_FUNCTION_TRACE("acpi_ac_exit");
  255. acpi_bus_unregister_driver(&acpi_ac_driver);
  256. remove_proc_entry(ACPI_AC_CLASS, acpi_root_dir);
  257. return_VOID;
  258. }
  259. module_init(acpi_ac_init);
  260. module_exit(acpi_ac_exit);