power.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * acpi_power.c - ACPI Bus Power Management ($Revision: 39 $)
  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. /*
  26. * ACPI power-managed devices may be controlled in two ways:
  27. * 1. via "Device Specific (D-State) Control"
  28. * 2. via "Power Resource Control".
  29. * This module is used to manage devices relying on Power Resource Control.
  30. *
  31. * An ACPI "power resource object" describes a software controllable power
  32. * plane, clock plane, or other resource used by a power managed device.
  33. * A device may rely on multiple power resources, and a power resource
  34. * may be shared by multiple devices.
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/types.h>
  40. #include <linux/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #define _COMPONENT ACPI_POWER_COMPONENT
  45. ACPI_MODULE_NAME("acpi_power")
  46. #define ACPI_POWER_COMPONENT 0x00800000
  47. #define ACPI_POWER_CLASS "power_resource"
  48. #define ACPI_POWER_DRIVER_NAME "ACPI Power Resource Driver"
  49. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  50. #define ACPI_POWER_FILE_INFO "info"
  51. #define ACPI_POWER_FILE_STATUS "state"
  52. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  53. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  54. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  55. static int acpi_power_add(struct acpi_device *device);
  56. static int acpi_power_remove(struct acpi_device *device, int type);
  57. static int acpi_power_open_fs(struct inode *inode, struct file *file);
  58. static struct acpi_driver acpi_power_driver = {
  59. .name = ACPI_POWER_DRIVER_NAME,
  60. .class = ACPI_POWER_CLASS,
  61. .ids = ACPI_POWER_HID,
  62. .ops = {
  63. .add = acpi_power_add,
  64. .remove = acpi_power_remove,
  65. },
  66. };
  67. struct acpi_power_resource {
  68. struct acpi_device * device;
  69. acpi_bus_id name;
  70. u32 system_level;
  71. u32 order;
  72. int state;
  73. int references;
  74. };
  75. static struct list_head acpi_power_resource_list;
  76. static const struct file_operations acpi_power_fops = {
  77. .open = acpi_power_open_fs,
  78. .read = seq_read,
  79. .llseek = seq_lseek,
  80. .release = single_release,
  81. };
  82. /* --------------------------------------------------------------------------
  83. Power Resource Management
  84. -------------------------------------------------------------------------- */
  85. static int
  86. acpi_power_get_context(acpi_handle handle,
  87. struct acpi_power_resource **resource)
  88. {
  89. int result = 0;
  90. struct acpi_device *device = NULL;
  91. if (!resource)
  92. return -ENODEV;
  93. result = acpi_bus_get_device(handle, &device);
  94. if (result) {
  95. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  96. return result;
  97. }
  98. *resource = (struct acpi_power_resource *)acpi_driver_data(device);
  99. if (!resource)
  100. return -ENODEV;
  101. return 0;
  102. }
  103. static int acpi_power_get_state(struct acpi_power_resource *resource)
  104. {
  105. acpi_status status = AE_OK;
  106. unsigned long sta = 0;
  107. if (!resource)
  108. return -EINVAL;
  109. status = acpi_evaluate_integer(resource->device->handle, "_STA", NULL, &sta);
  110. if (ACPI_FAILURE(status))
  111. return -ENODEV;
  112. if (sta & 0x01)
  113. resource->state = ACPI_POWER_RESOURCE_STATE_ON;
  114. else
  115. resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
  116. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  117. resource->name, resource->state ? "on" : "off"));
  118. return 0;
  119. }
  120. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  121. {
  122. int result = 0;
  123. struct acpi_power_resource *resource = NULL;
  124. u32 i = 0;
  125. if (!list || !state)
  126. return -EINVAL;
  127. /* The state of the list is 'on' IFF all resources are 'on'. */
  128. for (i = 0; i < list->count; i++) {
  129. result = acpi_power_get_context(list->handles[i], &resource);
  130. if (result)
  131. return result;
  132. result = acpi_power_get_state(resource);
  133. if (result)
  134. return result;
  135. *state = resource->state;
  136. if (*state != ACPI_POWER_RESOURCE_STATE_ON)
  137. break;
  138. }
  139. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  140. *state ? "on" : "off"));
  141. return result;
  142. }
  143. static int acpi_power_on(acpi_handle handle)
  144. {
  145. int result = 0;
  146. acpi_status status = AE_OK;
  147. struct acpi_device *device = NULL;
  148. struct acpi_power_resource *resource = NULL;
  149. result = acpi_power_get_context(handle, &resource);
  150. if (result)
  151. return result;
  152. resource->references++;
  153. if ((resource->references > 1)
  154. || (resource->state == ACPI_POWER_RESOURCE_STATE_ON)) {
  155. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
  156. resource->name));
  157. return 0;
  158. }
  159. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  160. if (ACPI_FAILURE(status))
  161. return -ENODEV;
  162. result = acpi_power_get_state(resource);
  163. if (result)
  164. return result;
  165. if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
  166. return -ENOEXEC;
  167. /* Update the power resource's _device_ power state */
  168. device = resource->device;
  169. resource->device->power.state = ACPI_STATE_D0;
  170. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
  171. resource->name));
  172. return 0;
  173. }
  174. static int acpi_power_off_device(acpi_handle handle)
  175. {
  176. int result = 0;
  177. acpi_status status = AE_OK;
  178. struct acpi_device *device = NULL;
  179. struct acpi_power_resource *resource = NULL;
  180. result = acpi_power_get_context(handle, &resource);
  181. if (result)
  182. return result;
  183. if (resource->references)
  184. resource->references--;
  185. if (resource->references) {
  186. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  187. "Resource [%s] is still in use, dereferencing\n",
  188. device->pnp.bus_id));
  189. return 0;
  190. }
  191. if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
  192. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
  193. device->pnp.bus_id));
  194. return 0;
  195. }
  196. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  197. if (ACPI_FAILURE(status))
  198. return -ENODEV;
  199. result = acpi_power_get_state(resource);
  200. if (result)
  201. return result;
  202. if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
  203. return -ENOEXEC;
  204. /* Update the power resource's _device_ power state */
  205. device = resource->device;
  206. device->power.state = ACPI_STATE_D3;
  207. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
  208. resource->name));
  209. return 0;
  210. }
  211. /*
  212. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  213. * 1. Power on the power resources required for the wakeup device
  214. * 2. Enable _PSW (power state wake) for the device if present
  215. */
  216. int acpi_enable_wakeup_device_power(struct acpi_device *dev)
  217. {
  218. union acpi_object arg = { ACPI_TYPE_INTEGER };
  219. struct acpi_object_list arg_list = { 1, &arg };
  220. acpi_status status = AE_OK;
  221. int i;
  222. int ret = 0;
  223. if (!dev || !dev->wakeup.flags.valid)
  224. return -1;
  225. arg.integer.value = 1;
  226. /* Open power resource */
  227. for (i = 0; i < dev->wakeup.resources.count; i++) {
  228. ret = acpi_power_on(dev->wakeup.resources.handles[i]);
  229. if (ret) {
  230. printk(KERN_ERR PREFIX "Transition power state\n");
  231. dev->wakeup.flags.valid = 0;
  232. return -1;
  233. }
  234. }
  235. /* Execute PSW */
  236. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  237. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  238. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  239. dev->wakeup.flags.valid = 0;
  240. ret = -1;
  241. }
  242. return ret;
  243. }
  244. /*
  245. * Shutdown a wakeup device, counterpart of above method
  246. * 1. Disable _PSW (power state wake)
  247. * 2. Shutdown down the power resources
  248. */
  249. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  250. {
  251. union acpi_object arg = { ACPI_TYPE_INTEGER };
  252. struct acpi_object_list arg_list = { 1, &arg };
  253. acpi_status status = AE_OK;
  254. int i;
  255. int ret = 0;
  256. if (!dev || !dev->wakeup.flags.valid)
  257. return -1;
  258. arg.integer.value = 0;
  259. /* Execute PSW */
  260. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  261. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  262. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  263. dev->wakeup.flags.valid = 0;
  264. return -1;
  265. }
  266. /* Close power resource */
  267. for (i = 0; i < dev->wakeup.resources.count; i++) {
  268. ret = acpi_power_off_device(dev->wakeup.resources.handles[i]);
  269. if (ret) {
  270. printk(KERN_ERR PREFIX "Transition power state\n");
  271. dev->wakeup.flags.valid = 0;
  272. return -1;
  273. }
  274. }
  275. return ret;
  276. }
  277. /* --------------------------------------------------------------------------
  278. Device Power Management
  279. -------------------------------------------------------------------------- */
  280. int acpi_power_get_inferred_state(struct acpi_device *device)
  281. {
  282. int result = 0;
  283. struct acpi_handle_list *list = NULL;
  284. int list_state = 0;
  285. int i = 0;
  286. if (!device)
  287. return -EINVAL;
  288. device->power.state = ACPI_STATE_UNKNOWN;
  289. /*
  290. * We know a device's inferred power state when all the resources
  291. * required for a given D-state are 'on'.
  292. */
  293. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  294. list = &device->power.states[i].resources;
  295. if (list->count < 1)
  296. continue;
  297. result = acpi_power_get_list_state(list, &list_state);
  298. if (result)
  299. return result;
  300. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  301. device->power.state = i;
  302. return 0;
  303. }
  304. }
  305. device->power.state = ACPI_STATE_D3;
  306. return 0;
  307. }
  308. int acpi_power_transition(struct acpi_device *device, int state)
  309. {
  310. int result = 0;
  311. struct acpi_handle_list *cl = NULL; /* Current Resources */
  312. struct acpi_handle_list *tl = NULL; /* Target Resources */
  313. int i = 0;
  314. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  315. return -EINVAL;
  316. if ((device->power.state < ACPI_STATE_D0)
  317. || (device->power.state > ACPI_STATE_D3))
  318. return -ENODEV;
  319. cl = &device->power.states[device->power.state].resources;
  320. tl = &device->power.states[state].resources;
  321. device->power.state = ACPI_STATE_UNKNOWN;
  322. if (!cl->count && !tl->count) {
  323. result = -ENODEV;
  324. goto end;
  325. }
  326. /* TBD: Resources must be ordered. */
  327. /*
  328. * First we reference all power resources required in the target list
  329. * (e.g. so the device doesn't lose power while transitioning).
  330. */
  331. for (i = 0; i < tl->count; i++) {
  332. result = acpi_power_on(tl->handles[i]);
  333. if (result)
  334. goto end;
  335. }
  336. /*
  337. * Then we dereference all power resources used in the current list.
  338. */
  339. for (i = 0; i < cl->count; i++) {
  340. result = acpi_power_off_device(cl->handles[i]);
  341. if (result)
  342. goto end;
  343. }
  344. /* We shouldn't change the state till all above operations succeed */
  345. device->power.state = state;
  346. end:
  347. if (result)
  348. printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
  349. device->pnp.bus_id, state);
  350. return result;
  351. }
  352. /* --------------------------------------------------------------------------
  353. FS Interface (/proc)
  354. -------------------------------------------------------------------------- */
  355. static struct proc_dir_entry *acpi_power_dir;
  356. static int acpi_power_seq_show(struct seq_file *seq, void *offset)
  357. {
  358. struct acpi_power_resource *resource = NULL;
  359. resource = (struct acpi_power_resource *)seq->private;
  360. if (!resource)
  361. goto end;
  362. seq_puts(seq, "state: ");
  363. switch (resource->state) {
  364. case ACPI_POWER_RESOURCE_STATE_ON:
  365. seq_puts(seq, "on\n");
  366. break;
  367. case ACPI_POWER_RESOURCE_STATE_OFF:
  368. seq_puts(seq, "off\n");
  369. break;
  370. default:
  371. seq_puts(seq, "unknown\n");
  372. break;
  373. }
  374. seq_printf(seq, "system level: S%d\n"
  375. "order: %d\n"
  376. "reference count: %d\n",
  377. resource->system_level,
  378. resource->order, resource->references);
  379. end:
  380. return 0;
  381. }
  382. static int acpi_power_open_fs(struct inode *inode, struct file *file)
  383. {
  384. return single_open(file, acpi_power_seq_show, PDE(inode)->data);
  385. }
  386. static int acpi_power_add_fs(struct acpi_device *device)
  387. {
  388. struct proc_dir_entry *entry = NULL;
  389. if (!device)
  390. return -EINVAL;
  391. if (!acpi_device_dir(device)) {
  392. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  393. acpi_power_dir);
  394. if (!acpi_device_dir(device))
  395. return -ENODEV;
  396. }
  397. /* 'status' [R] */
  398. entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
  399. S_IRUGO, acpi_device_dir(device));
  400. if (!entry)
  401. return -EIO;
  402. else {
  403. entry->proc_fops = &acpi_power_fops;
  404. entry->data = acpi_driver_data(device);
  405. }
  406. return 0;
  407. }
  408. static int acpi_power_remove_fs(struct acpi_device *device)
  409. {
  410. if (acpi_device_dir(device)) {
  411. remove_proc_entry(ACPI_POWER_FILE_STATUS,
  412. acpi_device_dir(device));
  413. remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
  414. acpi_device_dir(device) = NULL;
  415. }
  416. return 0;
  417. }
  418. /* --------------------------------------------------------------------------
  419. Driver Interface
  420. -------------------------------------------------------------------------- */
  421. static int acpi_power_add(struct acpi_device *device)
  422. {
  423. int result = 0;
  424. acpi_status status = AE_OK;
  425. struct acpi_power_resource *resource = NULL;
  426. union acpi_object acpi_object;
  427. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  428. if (!device)
  429. return -EINVAL;
  430. resource = kmalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  431. if (!resource)
  432. return -ENOMEM;
  433. memset(resource, 0, sizeof(struct acpi_power_resource));
  434. resource->device = device;
  435. strcpy(resource->name, device->pnp.bus_id);
  436. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  437. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  438. acpi_driver_data(device) = resource;
  439. /* Evalute the object to get the system level and resource order. */
  440. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  441. if (ACPI_FAILURE(status)) {
  442. result = -ENODEV;
  443. goto end;
  444. }
  445. resource->system_level = acpi_object.power_resource.system_level;
  446. resource->order = acpi_object.power_resource.resource_order;
  447. result = acpi_power_get_state(resource);
  448. if (result)
  449. goto end;
  450. switch (resource->state) {
  451. case ACPI_POWER_RESOURCE_STATE_ON:
  452. device->power.state = ACPI_STATE_D0;
  453. break;
  454. case ACPI_POWER_RESOURCE_STATE_OFF:
  455. device->power.state = ACPI_STATE_D3;
  456. break;
  457. default:
  458. device->power.state = ACPI_STATE_UNKNOWN;
  459. break;
  460. }
  461. result = acpi_power_add_fs(device);
  462. if (result)
  463. goto end;
  464. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  465. acpi_device_bid(device), resource->state ? "on" : "off");
  466. end:
  467. if (result)
  468. kfree(resource);
  469. return result;
  470. }
  471. static int acpi_power_remove(struct acpi_device *device, int type)
  472. {
  473. struct acpi_power_resource *resource = NULL;
  474. if (!device || !acpi_driver_data(device))
  475. return -EINVAL;
  476. resource = (struct acpi_power_resource *)acpi_driver_data(device);
  477. acpi_power_remove_fs(device);
  478. kfree(resource);
  479. return 0;
  480. }
  481. static int __init acpi_power_init(void)
  482. {
  483. int result = 0;
  484. if (acpi_disabled)
  485. return 0;
  486. INIT_LIST_HEAD(&acpi_power_resource_list);
  487. acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
  488. if (!acpi_power_dir)
  489. return -ENODEV;
  490. result = acpi_bus_register_driver(&acpi_power_driver);
  491. if (result < 0) {
  492. remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
  493. return -ENODEV;
  494. }
  495. return 0;
  496. }
  497. subsys_initcall(acpi_power_init);