power.c 16 KB

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