ac.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 void acpi_ac_notify(struct acpi_device *device, u32 event);
  58. static const struct acpi_device_id ac_device_ids[] = {
  59. {"ACPI0003", 0},
  60. {"", 0},
  61. };
  62. MODULE_DEVICE_TABLE(acpi, ac_device_ids);
  63. #ifdef CONFIG_PM_SLEEP
  64. static int acpi_ac_resume(struct device *dev);
  65. #endif
  66. static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
  67. static struct acpi_driver acpi_ac_driver = {
  68. .name = "ac",
  69. .class = ACPI_AC_CLASS,
  70. .ids = ac_device_ids,
  71. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  72. .ops = {
  73. .add = acpi_ac_add,
  74. .remove = acpi_ac_remove,
  75. .notify = acpi_ac_notify,
  76. },
  77. .drv.pm = &acpi_ac_pm,
  78. };
  79. struct acpi_ac {
  80. struct power_supply charger;
  81. struct acpi_device * device;
  82. unsigned long long state;
  83. };
  84. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  85. #ifdef CONFIG_ACPI_PROCFS_POWER
  86. static const struct file_operations acpi_ac_fops = {
  87. .owner = THIS_MODULE,
  88. .open = acpi_ac_open_fs,
  89. .read = seq_read,
  90. .llseek = seq_lseek,
  91. .release = single_release,
  92. };
  93. #endif
  94. /* --------------------------------------------------------------------------
  95. AC Adapter Management
  96. -------------------------------------------------------------------------- */
  97. static int acpi_ac_get_state(struct acpi_ac *ac)
  98. {
  99. acpi_status status = AE_OK;
  100. if (!ac)
  101. return -EINVAL;
  102. status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL, &ac->state);
  103. if (ACPI_FAILURE(status)) {
  104. ACPI_EXCEPTION((AE_INFO, status, "Error reading AC Adapter state"));
  105. ac->state = ACPI_AC_STATUS_UNKNOWN;
  106. return -ENODEV;
  107. }
  108. return 0;
  109. }
  110. /* --------------------------------------------------------------------------
  111. sysfs I/F
  112. -------------------------------------------------------------------------- */
  113. static int get_ac_property(struct power_supply *psy,
  114. enum power_supply_property psp,
  115. union power_supply_propval *val)
  116. {
  117. struct acpi_ac *ac = to_acpi_ac(psy);
  118. if (!ac)
  119. return -ENODEV;
  120. if (acpi_ac_get_state(ac))
  121. return -ENODEV;
  122. switch (psp) {
  123. case POWER_SUPPLY_PROP_ONLINE:
  124. val->intval = ac->state;
  125. break;
  126. default:
  127. return -EINVAL;
  128. }
  129. return 0;
  130. }
  131. static enum power_supply_property ac_props[] = {
  132. POWER_SUPPLY_PROP_ONLINE,
  133. };
  134. #ifdef CONFIG_ACPI_PROCFS_POWER
  135. /* --------------------------------------------------------------------------
  136. FS Interface (/proc)
  137. -------------------------------------------------------------------------- */
  138. static struct proc_dir_entry *acpi_ac_dir;
  139. static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
  140. {
  141. struct acpi_ac *ac = seq->private;
  142. if (!ac)
  143. return 0;
  144. if (acpi_ac_get_state(ac)) {
  145. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  146. return 0;
  147. }
  148. seq_puts(seq, "state: ");
  149. switch (ac->state) {
  150. case ACPI_AC_STATUS_OFFLINE:
  151. seq_puts(seq, "off-line\n");
  152. break;
  153. case ACPI_AC_STATUS_ONLINE:
  154. seq_puts(seq, "on-line\n");
  155. break;
  156. default:
  157. seq_puts(seq, "unknown\n");
  158. break;
  159. }
  160. return 0;
  161. }
  162. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  163. {
  164. return single_open(file, acpi_ac_seq_show, PDE(inode)->data);
  165. }
  166. static int acpi_ac_add_fs(struct acpi_device *device)
  167. {
  168. struct proc_dir_entry *entry = NULL;
  169. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
  170. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  171. if (!acpi_device_dir(device)) {
  172. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  173. acpi_ac_dir);
  174. if (!acpi_device_dir(device))
  175. return -ENODEV;
  176. }
  177. /* 'state' [R] */
  178. entry = proc_create_data(ACPI_AC_FILE_STATE,
  179. S_IRUGO, acpi_device_dir(device),
  180. &acpi_ac_fops, acpi_driver_data(device));
  181. if (!entry)
  182. return -ENODEV;
  183. return 0;
  184. }
  185. static int acpi_ac_remove_fs(struct acpi_device *device)
  186. {
  187. if (acpi_device_dir(device)) {
  188. remove_proc_entry(ACPI_AC_FILE_STATE, acpi_device_dir(device));
  189. remove_proc_entry(acpi_device_bid(device), acpi_ac_dir);
  190. acpi_device_dir(device) = NULL;
  191. }
  192. return 0;
  193. }
  194. #endif
  195. /* --------------------------------------------------------------------------
  196. Driver Model
  197. -------------------------------------------------------------------------- */
  198. static void acpi_ac_notify(struct acpi_device *device, u32 event)
  199. {
  200. struct acpi_ac *ac = acpi_driver_data(device);
  201. if (!ac)
  202. return;
  203. switch (event) {
  204. default:
  205. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  206. "Unsupported event [0x%x]\n", event));
  207. case ACPI_AC_NOTIFY_STATUS:
  208. case ACPI_NOTIFY_BUS_CHECK:
  209. case ACPI_NOTIFY_DEVICE_CHECK:
  210. acpi_ac_get_state(ac);
  211. acpi_bus_generate_proc_event(device, event, (u32) ac->state);
  212. acpi_bus_generate_netlink_event(device->pnp.device_class,
  213. dev_name(&device->dev), event,
  214. (u32) ac->state);
  215. acpi_notifier_call_chain(device, event, (u32) ac->state);
  216. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  217. }
  218. return;
  219. }
  220. static int acpi_ac_add(struct acpi_device *device)
  221. {
  222. int result = 0;
  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. device->driver_data = 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. ac->charger.name = acpi_device_bid(device);
  242. ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
  243. ac->charger.properties = ac_props;
  244. ac->charger.num_properties = ARRAY_SIZE(ac_props);
  245. ac->charger.get_property = get_ac_property;
  246. result = power_supply_register(&ac->device->dev, &ac->charger);
  247. if (result)
  248. goto end;
  249. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  250. acpi_device_name(device), acpi_device_bid(device),
  251. ac->state ? "on-line" : "off-line");
  252. end:
  253. if (result) {
  254. #ifdef CONFIG_ACPI_PROCFS_POWER
  255. acpi_ac_remove_fs(device);
  256. #endif
  257. kfree(ac);
  258. }
  259. return result;
  260. }
  261. #ifdef CONFIG_PM_SLEEP
  262. static int acpi_ac_resume(struct device *dev)
  263. {
  264. struct acpi_ac *ac;
  265. unsigned old_state;
  266. if (!dev)
  267. return -EINVAL;
  268. ac = acpi_driver_data(to_acpi_device(dev));
  269. if (!ac)
  270. return -EINVAL;
  271. old_state = ac->state;
  272. if (acpi_ac_get_state(ac))
  273. return 0;
  274. if (old_state != ac->state)
  275. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  276. return 0;
  277. }
  278. #endif
  279. static int acpi_ac_remove(struct acpi_device *device, int type)
  280. {
  281. struct acpi_ac *ac = NULL;
  282. if (!device || !acpi_driver_data(device))
  283. return -EINVAL;
  284. ac = acpi_driver_data(device);
  285. if (ac->charger.dev)
  286. power_supply_unregister(&ac->charger);
  287. #ifdef CONFIG_ACPI_PROCFS_POWER
  288. acpi_ac_remove_fs(device);
  289. #endif
  290. kfree(ac);
  291. return 0;
  292. }
  293. static int __init acpi_ac_init(void)
  294. {
  295. int result;
  296. if (acpi_disabled)
  297. return -ENODEV;
  298. #ifdef CONFIG_ACPI_PROCFS_POWER
  299. acpi_ac_dir = acpi_lock_ac_dir();
  300. if (!acpi_ac_dir)
  301. return -ENODEV;
  302. #endif
  303. result = acpi_bus_register_driver(&acpi_ac_driver);
  304. if (result < 0) {
  305. #ifdef CONFIG_ACPI_PROCFS_POWER
  306. acpi_unlock_ac_dir(acpi_ac_dir);
  307. #endif
  308. return -ENODEV;
  309. }
  310. return 0;
  311. }
  312. static void __exit acpi_ac_exit(void)
  313. {
  314. acpi_bus_unregister_driver(&acpi_ac_driver);
  315. #ifdef CONFIG_ACPI_PROCFS_POWER
  316. acpi_unlock_ac_dir(acpi_ac_dir);
  317. #endif
  318. return;
  319. }
  320. module_init(acpi_ac_init);
  321. module_exit(acpi_ac_exit);