ac.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. #include <linux/dmi.h>
  31. #include <linux/delay.h>
  32. #ifdef CONFIG_ACPI_PROCFS_POWER
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #endif
  36. #include <linux/platform_device.h>
  37. #include <linux/power_supply.h>
  38. #include <linux/acpi.h>
  39. #include "battery.h"
  40. #define PREFIX "ACPI: "
  41. #define ACPI_AC_CLASS "ac_adapter"
  42. #define ACPI_AC_DEVICE_NAME "AC Adapter"
  43. #define ACPI_AC_FILE_STATE "state"
  44. #define ACPI_AC_NOTIFY_STATUS 0x80
  45. #define ACPI_AC_STATUS_OFFLINE 0x00
  46. #define ACPI_AC_STATUS_ONLINE 0x01
  47. #define ACPI_AC_STATUS_UNKNOWN 0xFF
  48. #define _COMPONENT ACPI_AC_COMPONENT
  49. ACPI_MODULE_NAME("ac");
  50. MODULE_AUTHOR("Paul Diefenbaugh");
  51. MODULE_DESCRIPTION("ACPI AC Adapter Driver");
  52. MODULE_LICENSE("GPL");
  53. static int acpi_ac_add(struct acpi_device *device);
  54. static int acpi_ac_remove(struct acpi_device *device);
  55. static void acpi_ac_notify(struct acpi_device *device, u32 event);
  56. static const struct acpi_device_id ac_device_ids[] = {
  57. {"ACPI0003", 0},
  58. {"", 0},
  59. };
  60. MODULE_DEVICE_TABLE(acpi, ac_device_ids);
  61. #ifdef CONFIG_PM_SLEEP
  62. static int acpi_ac_resume(struct device *dev);
  63. #endif
  64. static SIMPLE_DEV_PM_OPS(acpi_ac_pm, NULL, acpi_ac_resume);
  65. #ifdef CONFIG_ACPI_PROCFS_POWER
  66. extern struct proc_dir_entry *acpi_lock_ac_dir(void);
  67. extern void *acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
  68. static int acpi_ac_open_fs(struct inode *inode, struct file *file);
  69. #endif
  70. static int ac_sleep_before_get_state_ms;
  71. static struct acpi_driver acpi_ac_driver = {
  72. .name = "ac",
  73. .class = ACPI_AC_CLASS,
  74. .ids = ac_device_ids,
  75. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  76. .ops = {
  77. .add = acpi_ac_add,
  78. .remove = acpi_ac_remove,
  79. .notify = acpi_ac_notify,
  80. },
  81. .drv.pm = &acpi_ac_pm,
  82. };
  83. struct acpi_ac {
  84. struct power_supply charger;
  85. struct acpi_device * device;
  86. unsigned long long state;
  87. struct notifier_block battery_nb;
  88. };
  89. #define to_acpi_ac(x) container_of(x, struct acpi_ac, charger)
  90. #ifdef CONFIG_ACPI_PROCFS_POWER
  91. static const struct file_operations acpi_ac_fops = {
  92. .owner = THIS_MODULE,
  93. .open = acpi_ac_open_fs,
  94. .read = seq_read,
  95. .llseek = seq_lseek,
  96. .release = single_release,
  97. };
  98. #endif
  99. /* --------------------------------------------------------------------------
  100. AC Adapter Management
  101. -------------------------------------------------------------------------- */
  102. static int acpi_ac_get_state(struct acpi_ac *ac)
  103. {
  104. acpi_status status = AE_OK;
  105. if (!ac)
  106. return -EINVAL;
  107. status = acpi_evaluate_integer(ac->device->handle, "_PSR", NULL,
  108. &ac->state);
  109. if (ACPI_FAILURE(status)) {
  110. ACPI_EXCEPTION((AE_INFO, status,
  111. "Error reading AC Adapter state"));
  112. ac->state = ACPI_AC_STATUS_UNKNOWN;
  113. return -ENODEV;
  114. }
  115. return 0;
  116. }
  117. /* --------------------------------------------------------------------------
  118. sysfs I/F
  119. -------------------------------------------------------------------------- */
  120. static int get_ac_property(struct power_supply *psy,
  121. enum power_supply_property psp,
  122. union power_supply_propval *val)
  123. {
  124. struct acpi_ac *ac = to_acpi_ac(psy);
  125. if (!ac)
  126. return -ENODEV;
  127. if (acpi_ac_get_state(ac))
  128. return -ENODEV;
  129. switch (psp) {
  130. case POWER_SUPPLY_PROP_ONLINE:
  131. val->intval = ac->state;
  132. break;
  133. default:
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static enum power_supply_property ac_props[] = {
  139. POWER_SUPPLY_PROP_ONLINE,
  140. };
  141. #ifdef CONFIG_ACPI_PROCFS_POWER
  142. /* --------------------------------------------------------------------------
  143. FS Interface (/proc)
  144. -------------------------------------------------------------------------- */
  145. static struct proc_dir_entry *acpi_ac_dir;
  146. static int acpi_ac_seq_show(struct seq_file *seq, void *offset)
  147. {
  148. struct acpi_ac *ac = seq->private;
  149. if (!ac)
  150. return 0;
  151. if (acpi_ac_get_state(ac)) {
  152. seq_puts(seq, "ERROR: Unable to read AC Adapter state\n");
  153. return 0;
  154. }
  155. seq_puts(seq, "state: ");
  156. switch (ac->state) {
  157. case ACPI_AC_STATUS_OFFLINE:
  158. seq_puts(seq, "off-line\n");
  159. break;
  160. case ACPI_AC_STATUS_ONLINE:
  161. seq_puts(seq, "on-line\n");
  162. break;
  163. default:
  164. seq_puts(seq, "unknown\n");
  165. break;
  166. }
  167. return 0;
  168. }
  169. static int acpi_ac_open_fs(struct inode *inode, struct file *file)
  170. {
  171. return single_open(file, acpi_ac_seq_show, PDE_DATA(inode));
  172. }
  173. static int acpi_ac_add_fs(struct acpi_ac *ac)
  174. {
  175. struct proc_dir_entry *entry = NULL;
  176. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for AC is loaded,"
  177. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  178. if (!acpi_device_dir(ac->device)) {
  179. acpi_device_dir(ac->device) =
  180. proc_mkdir(acpi_device_bid(ac->device), acpi_ac_dir);
  181. if (!acpi_device_dir(ac->device))
  182. return -ENODEV;
  183. }
  184. /* 'state' [R] */
  185. entry = proc_create_data(ACPI_AC_FILE_STATE,
  186. S_IRUGO, acpi_device_dir(ac->device),
  187. &acpi_ac_fops, ac);
  188. if (!entry)
  189. return -ENODEV;
  190. return 0;
  191. }
  192. static int acpi_ac_remove_fs(struct acpi_ac *ac)
  193. {
  194. if (acpi_device_dir(ac->device)) {
  195. remove_proc_entry(ACPI_AC_FILE_STATE,
  196. acpi_device_dir(ac->device));
  197. remove_proc_entry(acpi_device_bid(ac->device), acpi_ac_dir);
  198. acpi_device_dir(ac->device) = NULL;
  199. }
  200. return 0;
  201. }
  202. #endif
  203. /* --------------------------------------------------------------------------
  204. Driver Model
  205. -------------------------------------------------------------------------- */
  206. static void acpi_ac_notify(struct acpi_device *device, u32 event)
  207. {
  208. struct acpi_ac *ac = acpi_driver_data(device);
  209. if (!ac)
  210. return;
  211. switch (event) {
  212. default:
  213. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  214. "Unsupported event [0x%x]\n", event));
  215. case ACPI_AC_NOTIFY_STATUS:
  216. case ACPI_NOTIFY_BUS_CHECK:
  217. case ACPI_NOTIFY_DEVICE_CHECK:
  218. /*
  219. * A buggy BIOS may notify AC first and then sleep for
  220. * a specific time before doing actual operations in the
  221. * EC event handler (_Qxx). This will cause the AC state
  222. * reported by the ACPI event to be incorrect, so wait for a
  223. * specific time for the EC event handler to make progress.
  224. */
  225. if (ac_sleep_before_get_state_ms > 0)
  226. msleep(ac_sleep_before_get_state_ms);
  227. acpi_ac_get_state(ac);
  228. acpi_bus_generate_netlink_event(device->pnp.device_class,
  229. dev_name(&device->dev), event,
  230. (u32) ac->state);
  231. acpi_notifier_call_chain(device, event, (u32) ac->state);
  232. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  233. }
  234. return;
  235. }
  236. static int acpi_ac_battery_notify(struct notifier_block *nb,
  237. unsigned long action, void *data)
  238. {
  239. struct acpi_ac *ac = container_of(nb, struct acpi_ac, battery_nb);
  240. struct acpi_bus_event *event = (struct acpi_bus_event *)data;
  241. /*
  242. * On HP Pavilion dv6-6179er AC status notifications aren't triggered
  243. * when adapter is plugged/unplugged. However, battery status
  244. * notifcations are triggered when battery starts charging or
  245. * discharging. Re-reading AC status triggers lost AC notifications,
  246. * if AC status has changed.
  247. */
  248. if (strcmp(event->device_class, ACPI_BATTERY_CLASS) == 0 &&
  249. event->type == ACPI_BATTERY_NOTIFY_STATUS)
  250. acpi_ac_get_state(ac);
  251. return NOTIFY_OK;
  252. }
  253. static int thinkpad_e530_quirk(const struct dmi_system_id *d)
  254. {
  255. ac_sleep_before_get_state_ms = 1000;
  256. return 0;
  257. }
  258. static struct dmi_system_id ac_dmi_table[] = {
  259. {
  260. .callback = thinkpad_e530_quirk,
  261. .ident = "thinkpad e530",
  262. .matches = {
  263. DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
  264. DMI_MATCH(DMI_PRODUCT_NAME, "32597CG"),
  265. },
  266. },
  267. {},
  268. };
  269. static int acpi_ac_add(struct acpi_device *device)
  270. {
  271. int result = 0;
  272. struct acpi_ac *ac = NULL;
  273. if (!device)
  274. return -EINVAL;
  275. ac = kzalloc(sizeof(struct acpi_ac), GFP_KERNEL);
  276. if (!ac)
  277. return -ENOMEM;
  278. ac->device = device;
  279. strcpy(acpi_device_name(device), ACPI_AC_DEVICE_NAME);
  280. strcpy(acpi_device_class(device), ACPI_AC_CLASS);
  281. device->driver_data = ac;
  282. result = acpi_ac_get_state(ac);
  283. if (result)
  284. goto end;
  285. ac->charger.name = acpi_device_bid(device);
  286. #ifdef CONFIG_ACPI_PROCFS_POWER
  287. result = acpi_ac_add_fs(ac);
  288. if (result)
  289. goto end;
  290. #endif
  291. ac->charger.type = POWER_SUPPLY_TYPE_MAINS;
  292. ac->charger.properties = ac_props;
  293. ac->charger.num_properties = ARRAY_SIZE(ac_props);
  294. ac->charger.get_property = get_ac_property;
  295. result = power_supply_register(&ac->device->dev, &ac->charger);
  296. if (result)
  297. goto end;
  298. printk(KERN_INFO PREFIX "%s [%s] (%s)\n",
  299. acpi_device_name(device), acpi_device_bid(device),
  300. ac->state ? "on-line" : "off-line");
  301. ac->battery_nb.notifier_call = acpi_ac_battery_notify;
  302. register_acpi_notifier(&ac->battery_nb);
  303. end:
  304. if (result) {
  305. #ifdef CONFIG_ACPI_PROCFS_POWER
  306. acpi_ac_remove_fs(ac);
  307. #endif
  308. kfree(ac);
  309. }
  310. dmi_check_system(ac_dmi_table);
  311. return result;
  312. }
  313. #ifdef CONFIG_PM_SLEEP
  314. static int acpi_ac_resume(struct device *dev)
  315. {
  316. struct acpi_ac *ac;
  317. unsigned old_state;
  318. if (!dev)
  319. return -EINVAL;
  320. ac = acpi_driver_data(to_acpi_device(dev));
  321. if (!ac)
  322. return -EINVAL;
  323. old_state = ac->state;
  324. if (acpi_ac_get_state(ac))
  325. return 0;
  326. if (old_state != ac->state)
  327. kobject_uevent(&ac->charger.dev->kobj, KOBJ_CHANGE);
  328. return 0;
  329. }
  330. #else
  331. #define acpi_ac_resume NULL
  332. #endif
  333. static int acpi_ac_remove(struct acpi_device *device)
  334. {
  335. struct acpi_ac *ac = NULL;
  336. if (!device || !acpi_driver_data(device))
  337. return -EINVAL;
  338. ac = acpi_driver_data(device);
  339. if (ac->charger.dev)
  340. power_supply_unregister(&ac->charger);
  341. unregister_acpi_notifier(&ac->battery_nb);
  342. #ifdef CONFIG_ACPI_PROCFS_POWER
  343. acpi_ac_remove_fs(ac);
  344. #endif
  345. kfree(ac);
  346. return 0;
  347. }
  348. static int __init acpi_ac_init(void)
  349. {
  350. int result;
  351. if (acpi_disabled)
  352. return -ENODEV;
  353. #ifdef CONFIG_ACPI_PROCFS_POWER
  354. acpi_ac_dir = acpi_lock_ac_dir();
  355. if (!acpi_ac_dir)
  356. return -ENODEV;
  357. #endif
  358. result = acpi_bus_register_driver(&acpi_ac_driver);
  359. if (result < 0) {
  360. #ifdef CONFIG_ACPI_PROCFS_POWER
  361. acpi_unlock_ac_dir(acpi_ac_dir);
  362. #endif
  363. return -ENODEV;
  364. }
  365. return 0;
  366. }
  367. static void __exit acpi_ac_exit(void)
  368. {
  369. acpi_bus_unregister_driver(&acpi_ac_driver);
  370. #ifdef CONFIG_ACPI_PROCFS_POWER
  371. acpi_unlock_ac_dir(acpi_ac_dir);
  372. #endif
  373. }
  374. module_init(acpi_ac_init);
  375. module_exit(acpi_ac_exit);