PageRenderTime 148ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/acpi/ac.c

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