ac.c 8.3 KB

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