ac.c 8.0 KB

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