ac.c 9.4 KB

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