power.c 16 KB

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