power.c 16 KB

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