power.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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/slab.h>
  41. #include <acpi/acpi_bus.h>
  42. #include <acpi/acpi_drivers.h>
  43. #include "sleep.h"
  44. #define PREFIX "ACPI: "
  45. #define _COMPONENT ACPI_POWER_COMPONENT
  46. ACPI_MODULE_NAME("power");
  47. #define ACPI_POWER_CLASS "power_resource"
  48. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  49. #define ACPI_POWER_FILE_INFO "info"
  50. #define ACPI_POWER_FILE_STATUS "state"
  51. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  52. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  53. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  54. static int acpi_power_add(struct acpi_device *device);
  55. static int acpi_power_remove(struct acpi_device *device, int type);
  56. static int acpi_power_resume(struct acpi_device *device);
  57. static const struct acpi_device_id power_device_ids[] = {
  58. {ACPI_POWER_HID, 0},
  59. {"", 0},
  60. };
  61. MODULE_DEVICE_TABLE(acpi, power_device_ids);
  62. static struct acpi_driver acpi_power_driver = {
  63. .name = "power",
  64. .class = ACPI_POWER_CLASS,
  65. .ids = power_device_ids,
  66. .ops = {
  67. .add = acpi_power_add,
  68. .remove = acpi_power_remove,
  69. .resume = acpi_power_resume,
  70. },
  71. };
  72. struct acpi_power_resource {
  73. struct acpi_device * device;
  74. acpi_bus_id name;
  75. u32 system_level;
  76. u32 order;
  77. unsigned int ref_count;
  78. struct mutex resource_lock;
  79. };
  80. static struct list_head acpi_power_resource_list;
  81. /* --------------------------------------------------------------------------
  82. Power Resource Management
  83. -------------------------------------------------------------------------- */
  84. static int
  85. acpi_power_get_context(acpi_handle handle,
  86. struct acpi_power_resource **resource)
  87. {
  88. int result = 0;
  89. struct acpi_device *device = NULL;
  90. if (!resource)
  91. return -ENODEV;
  92. result = acpi_bus_get_device(handle, &device);
  93. if (result) {
  94. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  95. return result;
  96. }
  97. *resource = acpi_driver_data(device);
  98. if (!*resource)
  99. return -ENODEV;
  100. return 0;
  101. }
  102. static int acpi_power_get_state(acpi_handle handle, int *state)
  103. {
  104. acpi_status status = AE_OK;
  105. unsigned long long sta = 0;
  106. char node_name[5];
  107. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  108. if (!handle || !state)
  109. return -EINVAL;
  110. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  111. if (ACPI_FAILURE(status))
  112. return -ENODEV;
  113. *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
  114. ACPI_POWER_RESOURCE_STATE_OFF;
  115. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  116. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  117. node_name,
  118. *state ? "on" : "off"));
  119. return 0;
  120. }
  121. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  122. {
  123. int cur_state;
  124. int 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. struct acpi_power_resource *resource;
  130. acpi_handle handle = list->handles[i];
  131. int result;
  132. result = acpi_power_get_context(handle, &resource);
  133. if (result)
  134. return result;
  135. mutex_lock(&resource->resource_lock);
  136. result = acpi_power_get_state(handle, &cur_state);
  137. mutex_unlock(&resource->resource_lock);
  138. if (result)
  139. return result;
  140. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  141. break;
  142. }
  143. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  144. cur_state ? "on" : "off"));
  145. *state = cur_state;
  146. return 0;
  147. }
  148. static int __acpi_power_on(struct acpi_power_resource *resource)
  149. {
  150. acpi_status status = AE_OK;
  151. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  152. if (ACPI_FAILURE(status))
  153. return -ENODEV;
  154. /* Update the power resource's _device_ power state */
  155. resource->device->power.state = ACPI_STATE_D0;
  156. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
  157. resource->name));
  158. return 0;
  159. }
  160. static int acpi_power_on(acpi_handle handle)
  161. {
  162. int result = 0;
  163. struct acpi_power_resource *resource = NULL;
  164. result = acpi_power_get_context(handle, &resource);
  165. if (result)
  166. return result;
  167. mutex_lock(&resource->resource_lock);
  168. if (resource->ref_count++) {
  169. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  170. "Power resource [%s] already on",
  171. resource->name));
  172. } else {
  173. result = __acpi_power_on(resource);
  174. if (result)
  175. resource->ref_count--;
  176. }
  177. mutex_unlock(&resource->resource_lock);
  178. return result;
  179. }
  180. static int acpi_power_off(acpi_handle handle)
  181. {
  182. int result = 0;
  183. acpi_status status = AE_OK;
  184. struct acpi_power_resource *resource = NULL;
  185. result = acpi_power_get_context(handle, &resource);
  186. if (result)
  187. return result;
  188. mutex_lock(&resource->resource_lock);
  189. if (!resource->ref_count) {
  190. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  191. "Power resource [%s] already off",
  192. resource->name));
  193. goto unlock;
  194. }
  195. if (--resource->ref_count) {
  196. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  197. "Power resource [%s] still in use\n",
  198. resource->name));
  199. goto unlock;
  200. }
  201. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  202. if (ACPI_FAILURE(status)) {
  203. result = -ENODEV;
  204. } else {
  205. /* Update the power resource's _device_ power state */
  206. resource->device->power.state = ACPI_STATE_D3;
  207. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  208. "Power resource [%s] turned off\n",
  209. resource->name));
  210. }
  211. unlock:
  212. mutex_unlock(&resource->resource_lock);
  213. return result;
  214. }
  215. static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
  216. {
  217. int i;
  218. for (i = num_res - 1; i >= 0 ; i--)
  219. acpi_power_off(list->handles[i]);
  220. }
  221. static void acpi_power_off_list(struct acpi_handle_list *list)
  222. {
  223. __acpi_power_off_list(list, list->count);
  224. }
  225. static int acpi_power_on_list(struct acpi_handle_list *list)
  226. {
  227. int result = 0;
  228. int i;
  229. for (i = 0; i < list->count; i++) {
  230. result = acpi_power_on(list->handles[i]);
  231. if (result) {
  232. __acpi_power_off_list(list, i);
  233. break;
  234. }
  235. }
  236. return result;
  237. }
  238. /**
  239. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  240. * ACPI 3.0) _PSW (Power State Wake)
  241. * @dev: Device to handle.
  242. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  243. * @sleep_state: Target sleep state of the system.
  244. * @dev_state: Target power state of the device.
  245. *
  246. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  247. * State Wake) for the device, if present. On failure reset the device's
  248. * wakeup.flags.valid flag.
  249. *
  250. * RETURN VALUE:
  251. * 0 if either _DSW or _PSW has been successfully executed
  252. * 0 if neither _DSW nor _PSW has been found
  253. * -ENODEV if the execution of either _DSW or _PSW has failed
  254. */
  255. int acpi_device_sleep_wake(struct acpi_device *dev,
  256. int enable, int sleep_state, int dev_state)
  257. {
  258. union acpi_object in_arg[3];
  259. struct acpi_object_list arg_list = { 3, in_arg };
  260. acpi_status status = AE_OK;
  261. /*
  262. * Try to execute _DSW first.
  263. *
  264. * Three agruments are needed for the _DSW object:
  265. * Argument 0: enable/disable the wake capabilities
  266. * Argument 1: target system state
  267. * Argument 2: target device state
  268. * When _DSW object is called to disable the wake capabilities, maybe
  269. * the first argument is filled. The values of the other two agruments
  270. * are meaningless.
  271. */
  272. in_arg[0].type = ACPI_TYPE_INTEGER;
  273. in_arg[0].integer.value = enable;
  274. in_arg[1].type = ACPI_TYPE_INTEGER;
  275. in_arg[1].integer.value = sleep_state;
  276. in_arg[2].type = ACPI_TYPE_INTEGER;
  277. in_arg[2].integer.value = dev_state;
  278. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  279. if (ACPI_SUCCESS(status)) {
  280. return 0;
  281. } else if (status != AE_NOT_FOUND) {
  282. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  283. dev->wakeup.flags.valid = 0;
  284. return -ENODEV;
  285. }
  286. /* Execute _PSW */
  287. arg_list.count = 1;
  288. in_arg[0].integer.value = enable;
  289. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  290. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  291. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  292. dev->wakeup.flags.valid = 0;
  293. return -ENODEV;
  294. }
  295. return 0;
  296. }
  297. /*
  298. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  299. * 1. Power on the power resources required for the wakeup device
  300. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  301. * State Wake) for the device, if present
  302. */
  303. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  304. {
  305. int i, err = 0;
  306. if (!dev || !dev->wakeup.flags.valid)
  307. return -EINVAL;
  308. mutex_lock(&acpi_device_lock);
  309. if (dev->wakeup.prepare_count++)
  310. goto out;
  311. /* Open power resource */
  312. for (i = 0; i < dev->wakeup.resources.count; i++) {
  313. int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
  314. if (ret) {
  315. printk(KERN_ERR PREFIX "Transition power state\n");
  316. dev->wakeup.flags.valid = 0;
  317. err = -ENODEV;
  318. goto err_out;
  319. }
  320. }
  321. /*
  322. * Passing 3 as the third argument below means the device may be placed
  323. * in arbitrary power state afterwards.
  324. */
  325. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  326. err_out:
  327. if (err)
  328. dev->wakeup.prepare_count = 0;
  329. out:
  330. mutex_unlock(&acpi_device_lock);
  331. return err;
  332. }
  333. /*
  334. * Shutdown a wakeup device, counterpart of above method
  335. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  336. * State Wake) for the device, if present
  337. * 2. Shutdown down the power resources
  338. */
  339. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  340. {
  341. int i, err = 0;
  342. if (!dev || !dev->wakeup.flags.valid)
  343. return -EINVAL;
  344. mutex_lock(&acpi_device_lock);
  345. if (--dev->wakeup.prepare_count > 0)
  346. goto out;
  347. /*
  348. * Executing the code below even if prepare_count is already zero when
  349. * the function is called may be useful, for example for initialisation.
  350. */
  351. if (dev->wakeup.prepare_count < 0)
  352. dev->wakeup.prepare_count = 0;
  353. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  354. if (err)
  355. goto out;
  356. /* Close power resource */
  357. for (i = 0; i < dev->wakeup.resources.count; i++) {
  358. int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
  359. if (ret) {
  360. printk(KERN_ERR PREFIX "Transition power state\n");
  361. dev->wakeup.flags.valid = 0;
  362. err = -ENODEV;
  363. goto out;
  364. }
  365. }
  366. out:
  367. mutex_unlock(&acpi_device_lock);
  368. return err;
  369. }
  370. /* --------------------------------------------------------------------------
  371. Device Power Management
  372. -------------------------------------------------------------------------- */
  373. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  374. {
  375. int result = 0;
  376. struct acpi_handle_list *list = NULL;
  377. int list_state = 0;
  378. int i = 0;
  379. if (!device || !state)
  380. return -EINVAL;
  381. /*
  382. * We know a device's inferred power state when all the resources
  383. * required for a given D-state are 'on'.
  384. */
  385. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  386. list = &device->power.states[i].resources;
  387. if (list->count < 1)
  388. continue;
  389. result = acpi_power_get_list_state(list, &list_state);
  390. if (result)
  391. return result;
  392. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  393. *state = i;
  394. return 0;
  395. }
  396. }
  397. *state = ACPI_STATE_D3;
  398. return 0;
  399. }
  400. int acpi_power_on_resources(struct acpi_device *device, int state)
  401. {
  402. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
  403. return -EINVAL;
  404. return acpi_power_on_list(&device->power.states[state].resources);
  405. }
  406. int acpi_power_transition(struct acpi_device *device, int state)
  407. {
  408. int result;
  409. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  410. return -EINVAL;
  411. if (device->power.state == state)
  412. return 0;
  413. if ((device->power.state < ACPI_STATE_D0)
  414. || (device->power.state > ACPI_STATE_D3))
  415. return -ENODEV;
  416. /* TBD: Resources must be ordered. */
  417. /*
  418. * First we reference all power resources required in the target list
  419. * (e.g. so the device doesn't lose power while transitioning). Then,
  420. * we dereference all power resources used in the current list.
  421. */
  422. result = acpi_power_on_list(&device->power.states[state].resources);
  423. if (!result)
  424. acpi_power_off_list(
  425. &device->power.states[device->power.state].resources);
  426. /* We shouldn't change the state unless the above operations succeed. */
  427. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  428. return result;
  429. }
  430. /* --------------------------------------------------------------------------
  431. Driver Interface
  432. -------------------------------------------------------------------------- */
  433. static int acpi_power_add(struct acpi_device *device)
  434. {
  435. int result = 0, state;
  436. acpi_status status = AE_OK;
  437. struct acpi_power_resource *resource = NULL;
  438. union acpi_object acpi_object;
  439. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  440. if (!device)
  441. return -EINVAL;
  442. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  443. if (!resource)
  444. return -ENOMEM;
  445. resource->device = device;
  446. mutex_init(&resource->resource_lock);
  447. strcpy(resource->name, device->pnp.bus_id);
  448. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  449. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  450. device->driver_data = resource;
  451. /* Evalute the object to get the system level and resource order. */
  452. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  453. if (ACPI_FAILURE(status)) {
  454. result = -ENODEV;
  455. goto end;
  456. }
  457. resource->system_level = acpi_object.power_resource.system_level;
  458. resource->order = acpi_object.power_resource.resource_order;
  459. result = acpi_power_get_state(device->handle, &state);
  460. if (result)
  461. goto end;
  462. switch (state) {
  463. case ACPI_POWER_RESOURCE_STATE_ON:
  464. device->power.state = ACPI_STATE_D0;
  465. break;
  466. case ACPI_POWER_RESOURCE_STATE_OFF:
  467. device->power.state = ACPI_STATE_D3;
  468. break;
  469. default:
  470. device->power.state = ACPI_STATE_UNKNOWN;
  471. break;
  472. }
  473. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  474. acpi_device_bid(device), state ? "on" : "off");
  475. end:
  476. if (result)
  477. kfree(resource);
  478. return result;
  479. }
  480. static int acpi_power_remove(struct acpi_device *device, int type)
  481. {
  482. struct acpi_power_resource *resource;
  483. if (!device)
  484. return -EINVAL;
  485. resource = acpi_driver_data(device);
  486. if (!resource)
  487. return -EINVAL;
  488. kfree(resource);
  489. return 0;
  490. }
  491. static int acpi_power_resume(struct acpi_device *device)
  492. {
  493. int result = 0, state;
  494. struct acpi_power_resource *resource;
  495. if (!device)
  496. return -EINVAL;
  497. resource = acpi_driver_data(device);
  498. if (!resource)
  499. return -EINVAL;
  500. mutex_lock(&resource->resource_lock);
  501. result = acpi_power_get_state(device->handle, &state);
  502. if (result)
  503. goto unlock;
  504. if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count)
  505. result = __acpi_power_on(resource);
  506. unlock:
  507. mutex_unlock(&resource->resource_lock);
  508. return result;
  509. }
  510. int __init acpi_power_init(void)
  511. {
  512. INIT_LIST_HEAD(&acpi_power_resource_list);
  513. return acpi_bus_register_driver(&acpi_power_driver);
  514. }