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