ac.c 10 KB

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