ac.c 9.2 KB

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