ac.c 7.4 KB

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