power.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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 <linux/pm_runtime.h>
  42. #include <linux/sysfs.h>
  43. #include <acpi/acpi_bus.h>
  44. #include <acpi/acpi_drivers.h>
  45. #include "sleep.h"
  46. #include "internal.h"
  47. #define PREFIX "ACPI: "
  48. #define _COMPONENT ACPI_POWER_COMPONENT
  49. ACPI_MODULE_NAME("power");
  50. #define ACPI_POWER_CLASS "power_resource"
  51. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  52. #define ACPI_POWER_FILE_INFO "info"
  53. #define ACPI_POWER_FILE_STATUS "state"
  54. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  55. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  56. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  57. struct acpi_power_resource {
  58. struct acpi_device device;
  59. struct list_head list_node;
  60. char *name;
  61. u32 system_level;
  62. u32 order;
  63. unsigned int ref_count;
  64. bool wakeup_enabled;
  65. struct mutex resource_lock;
  66. };
  67. struct acpi_power_resource_entry {
  68. struct list_head node;
  69. struct acpi_power_resource *resource;
  70. };
  71. static LIST_HEAD(acpi_power_resource_list);
  72. static DEFINE_MUTEX(power_resource_list_lock);
  73. /* --------------------------------------------------------------------------
  74. Power Resource Management
  75. -------------------------------------------------------------------------- */
  76. static inline
  77. struct acpi_power_resource *to_power_resource(struct acpi_device *device)
  78. {
  79. return container_of(device, struct acpi_power_resource, device);
  80. }
  81. static struct acpi_power_resource *acpi_power_get_context(acpi_handle handle)
  82. {
  83. struct acpi_device *device;
  84. if (acpi_bus_get_device(handle, &device))
  85. return NULL;
  86. return to_power_resource(device);
  87. }
  88. static int acpi_power_resources_list_add(acpi_handle handle,
  89. struct list_head *list)
  90. {
  91. struct acpi_power_resource *resource = acpi_power_get_context(handle);
  92. struct acpi_power_resource_entry *entry;
  93. if (!resource || !list)
  94. return -EINVAL;
  95. entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  96. if (!entry)
  97. return -ENOMEM;
  98. entry->resource = resource;
  99. if (!list_empty(list)) {
  100. struct acpi_power_resource_entry *e;
  101. list_for_each_entry(e, list, node)
  102. if (e->resource->order > resource->order) {
  103. list_add_tail(&entry->node, &e->node);
  104. return 0;
  105. }
  106. }
  107. list_add_tail(&entry->node, list);
  108. return 0;
  109. }
  110. void acpi_power_resources_list_free(struct list_head *list)
  111. {
  112. struct acpi_power_resource_entry *entry, *e;
  113. list_for_each_entry_safe(entry, e, list, node) {
  114. list_del(&entry->node);
  115. kfree(entry);
  116. }
  117. }
  118. int acpi_extract_power_resources(union acpi_object *package, unsigned int start,
  119. struct list_head *list)
  120. {
  121. unsigned int i;
  122. int err = 0;
  123. for (i = start; i < package->package.count; i++) {
  124. union acpi_object *element = &package->package.elements[i];
  125. acpi_handle rhandle;
  126. if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
  127. err = -ENODATA;
  128. break;
  129. }
  130. rhandle = element->reference.handle;
  131. if (!rhandle) {
  132. err = -ENODEV;
  133. break;
  134. }
  135. err = acpi_add_power_resource(rhandle);
  136. if (err)
  137. break;
  138. err = acpi_power_resources_list_add(rhandle, list);
  139. if (err)
  140. break;
  141. }
  142. if (err)
  143. acpi_power_resources_list_free(list);
  144. return err;
  145. }
  146. static int acpi_power_get_state(acpi_handle handle, int *state)
  147. {
  148. acpi_status status = AE_OK;
  149. unsigned long long sta = 0;
  150. char node_name[5];
  151. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  152. if (!handle || !state)
  153. return -EINVAL;
  154. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  155. if (ACPI_FAILURE(status))
  156. return -ENODEV;
  157. *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
  158. ACPI_POWER_RESOURCE_STATE_OFF;
  159. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  160. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  161. node_name,
  162. *state ? "on" : "off"));
  163. return 0;
  164. }
  165. static int acpi_power_get_list_state(struct list_head *list, int *state)
  166. {
  167. struct acpi_power_resource_entry *entry;
  168. int cur_state;
  169. if (!list || !state)
  170. return -EINVAL;
  171. /* The state of the list is 'on' IFF all resources are 'on'. */
  172. list_for_each_entry(entry, list, node) {
  173. struct acpi_power_resource *resource = entry->resource;
  174. acpi_handle handle = resource->device.handle;
  175. int result;
  176. mutex_lock(&resource->resource_lock);
  177. result = acpi_power_get_state(handle, &cur_state);
  178. mutex_unlock(&resource->resource_lock);
  179. if (result)
  180. return result;
  181. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  182. break;
  183. }
  184. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  185. cur_state ? "on" : "off"));
  186. *state = cur_state;
  187. return 0;
  188. }
  189. static int __acpi_power_on(struct acpi_power_resource *resource)
  190. {
  191. acpi_status status = AE_OK;
  192. status = acpi_evaluate_object(resource->device.handle, "_ON", NULL, NULL);
  193. if (ACPI_FAILURE(status))
  194. return -ENODEV;
  195. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned on\n",
  196. resource->name));
  197. return 0;
  198. }
  199. static int acpi_power_on_unlocked(struct acpi_power_resource *resource)
  200. {
  201. int result = 0;
  202. if (resource->ref_count++) {
  203. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  204. "Power resource [%s] already on\n",
  205. resource->name));
  206. } else {
  207. result = __acpi_power_on(resource);
  208. if (result)
  209. resource->ref_count--;
  210. }
  211. return result;
  212. }
  213. static int acpi_power_on(struct acpi_power_resource *resource)
  214. {
  215. int result;
  216. mutex_lock(&resource->resource_lock);
  217. result = acpi_power_on_unlocked(resource);
  218. mutex_unlock(&resource->resource_lock);
  219. return result;
  220. }
  221. static int __acpi_power_off(struct acpi_power_resource *resource)
  222. {
  223. acpi_status status;
  224. status = acpi_evaluate_object(resource->device.handle, "_OFF",
  225. NULL, NULL);
  226. if (ACPI_FAILURE(status))
  227. return -ENODEV;
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Power resource [%s] turned off\n",
  229. resource->name));
  230. return 0;
  231. }
  232. static int acpi_power_off_unlocked(struct acpi_power_resource *resource)
  233. {
  234. int result = 0;
  235. if (!resource->ref_count) {
  236. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  237. "Power resource [%s] already off\n",
  238. resource->name));
  239. return 0;
  240. }
  241. if (--resource->ref_count) {
  242. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  243. "Power resource [%s] still in use\n",
  244. resource->name));
  245. } else {
  246. result = __acpi_power_off(resource);
  247. if (result)
  248. resource->ref_count++;
  249. }
  250. return result;
  251. }
  252. static int acpi_power_off(struct acpi_power_resource *resource)
  253. {
  254. int result;
  255. mutex_lock(&resource->resource_lock);
  256. result = acpi_power_off_unlocked(resource);
  257. mutex_unlock(&resource->resource_lock);
  258. return result;
  259. }
  260. static int acpi_power_off_list(struct list_head *list)
  261. {
  262. struct acpi_power_resource_entry *entry;
  263. int result = 0;
  264. list_for_each_entry_reverse(entry, list, node) {
  265. result = acpi_power_off(entry->resource);
  266. if (result)
  267. goto err;
  268. }
  269. return 0;
  270. err:
  271. list_for_each_entry_continue(entry, list, node)
  272. acpi_power_on(entry->resource);
  273. return result;
  274. }
  275. static int acpi_power_on_list(struct list_head *list)
  276. {
  277. struct acpi_power_resource_entry *entry;
  278. int result = 0;
  279. list_for_each_entry(entry, list, node) {
  280. result = acpi_power_on(entry->resource);
  281. if (result)
  282. goto err;
  283. }
  284. return 0;
  285. err:
  286. list_for_each_entry_continue_reverse(entry, list, node)
  287. acpi_power_off(entry->resource);
  288. return result;
  289. }
  290. static struct attribute *attrs[] = {
  291. NULL,
  292. };
  293. static struct attribute_group attr_groups[] = {
  294. [ACPI_STATE_D0] = {
  295. .name = "power_resources_D0",
  296. .attrs = attrs,
  297. },
  298. [ACPI_STATE_D1] = {
  299. .name = "power_resources_D1",
  300. .attrs = attrs,
  301. },
  302. [ACPI_STATE_D2] = {
  303. .name = "power_resources_D2",
  304. .attrs = attrs,
  305. },
  306. [ACPI_STATE_D3_HOT] = {
  307. .name = "power_resources_D3hot",
  308. .attrs = attrs,
  309. },
  310. };
  311. static struct attribute_group wakeup_attr_group = {
  312. .name = "power_resources_wakeup",
  313. .attrs = attrs,
  314. };
  315. static void acpi_power_hide_list(struct acpi_device *adev,
  316. struct list_head *resources,
  317. struct attribute_group *attr_group)
  318. {
  319. struct acpi_power_resource_entry *entry;
  320. if (list_empty(resources))
  321. return;
  322. list_for_each_entry_reverse(entry, resources, node) {
  323. struct acpi_device *res_dev = &entry->resource->device;
  324. sysfs_remove_link_from_group(&adev->dev.kobj,
  325. attr_group->name,
  326. dev_name(&res_dev->dev));
  327. }
  328. sysfs_remove_group(&adev->dev.kobj, attr_group);
  329. }
  330. static void acpi_power_expose_list(struct acpi_device *adev,
  331. struct list_head *resources,
  332. struct attribute_group *attr_group)
  333. {
  334. struct acpi_power_resource_entry *entry;
  335. int ret;
  336. if (list_empty(resources))
  337. return;
  338. ret = sysfs_create_group(&adev->dev.kobj, attr_group);
  339. if (ret)
  340. return;
  341. list_for_each_entry(entry, resources, node) {
  342. struct acpi_device *res_dev = &entry->resource->device;
  343. ret = sysfs_add_link_to_group(&adev->dev.kobj,
  344. attr_group->name,
  345. &res_dev->dev.kobj,
  346. dev_name(&res_dev->dev));
  347. if (ret) {
  348. acpi_power_hide_list(adev, resources, attr_group);
  349. break;
  350. }
  351. }
  352. }
  353. static void acpi_power_expose_hide(struct acpi_device *adev,
  354. struct list_head *resources,
  355. struct attribute_group *attr_group,
  356. bool expose)
  357. {
  358. if (expose)
  359. acpi_power_expose_list(adev, resources, attr_group);
  360. else
  361. acpi_power_hide_list(adev, resources, attr_group);
  362. }
  363. void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
  364. {
  365. int state;
  366. if (adev->wakeup.flags.valid)
  367. acpi_power_expose_hide(adev, &adev->wakeup.resources,
  368. &wakeup_attr_group, add);
  369. if (!adev->power.flags.power_resources)
  370. return;
  371. for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++)
  372. acpi_power_expose_hide(adev,
  373. &adev->power.states[state].resources,
  374. &attr_groups[state], add);
  375. }
  376. int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
  377. {
  378. struct acpi_power_resource_entry *entry;
  379. int system_level = 5;
  380. list_for_each_entry(entry, list, node) {
  381. struct acpi_power_resource *resource = entry->resource;
  382. acpi_handle handle = resource->device.handle;
  383. int result;
  384. int state;
  385. mutex_lock(&resource->resource_lock);
  386. result = acpi_power_get_state(handle, &state);
  387. if (result) {
  388. mutex_unlock(&resource->resource_lock);
  389. return result;
  390. }
  391. if (state == ACPI_POWER_RESOURCE_STATE_ON) {
  392. resource->ref_count++;
  393. resource->wakeup_enabled = true;
  394. }
  395. if (system_level > resource->system_level)
  396. system_level = resource->system_level;
  397. mutex_unlock(&resource->resource_lock);
  398. }
  399. *system_level_p = system_level;
  400. return 0;
  401. }
  402. /* --------------------------------------------------------------------------
  403. Device Power Management
  404. -------------------------------------------------------------------------- */
  405. /**
  406. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  407. * ACPI 3.0) _PSW (Power State Wake)
  408. * @dev: Device to handle.
  409. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  410. * @sleep_state: Target sleep state of the system.
  411. * @dev_state: Target power state of the device.
  412. *
  413. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  414. * State Wake) for the device, if present. On failure reset the device's
  415. * wakeup.flags.valid flag.
  416. *
  417. * RETURN VALUE:
  418. * 0 if either _DSW or _PSW has been successfully executed
  419. * 0 if neither _DSW nor _PSW has been found
  420. * -ENODEV if the execution of either _DSW or _PSW has failed
  421. */
  422. int acpi_device_sleep_wake(struct acpi_device *dev,
  423. int enable, int sleep_state, int dev_state)
  424. {
  425. union acpi_object in_arg[3];
  426. struct acpi_object_list arg_list = { 3, in_arg };
  427. acpi_status status = AE_OK;
  428. /*
  429. * Try to execute _DSW first.
  430. *
  431. * Three agruments are needed for the _DSW object:
  432. * Argument 0: enable/disable the wake capabilities
  433. * Argument 1: target system state
  434. * Argument 2: target device state
  435. * When _DSW object is called to disable the wake capabilities, maybe
  436. * the first argument is filled. The values of the other two agruments
  437. * are meaningless.
  438. */
  439. in_arg[0].type = ACPI_TYPE_INTEGER;
  440. in_arg[0].integer.value = enable;
  441. in_arg[1].type = ACPI_TYPE_INTEGER;
  442. in_arg[1].integer.value = sleep_state;
  443. in_arg[2].type = ACPI_TYPE_INTEGER;
  444. in_arg[2].integer.value = dev_state;
  445. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  446. if (ACPI_SUCCESS(status)) {
  447. return 0;
  448. } else if (status != AE_NOT_FOUND) {
  449. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  450. dev->wakeup.flags.valid = 0;
  451. return -ENODEV;
  452. }
  453. /* Execute _PSW */
  454. status = acpi_execute_simple_method(dev->handle, "_PSW", enable);
  455. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  456. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  457. dev->wakeup.flags.valid = 0;
  458. return -ENODEV;
  459. }
  460. return 0;
  461. }
  462. /*
  463. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  464. * 1. Power on the power resources required for the wakeup device
  465. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  466. * State Wake) for the device, if present
  467. */
  468. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  469. {
  470. struct acpi_power_resource_entry *entry;
  471. int err = 0;
  472. if (!dev || !dev->wakeup.flags.valid)
  473. return -EINVAL;
  474. mutex_lock(&acpi_device_lock);
  475. if (dev->wakeup.prepare_count++)
  476. goto out;
  477. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  478. struct acpi_power_resource *resource = entry->resource;
  479. mutex_lock(&resource->resource_lock);
  480. if (!resource->wakeup_enabled) {
  481. err = acpi_power_on_unlocked(resource);
  482. if (!err)
  483. resource->wakeup_enabled = true;
  484. }
  485. mutex_unlock(&resource->resource_lock);
  486. if (err) {
  487. dev_err(&dev->dev,
  488. "Cannot turn wakeup power resources on\n");
  489. dev->wakeup.flags.valid = 0;
  490. goto out;
  491. }
  492. }
  493. /*
  494. * Passing 3 as the third argument below means the device may be
  495. * put into arbitrary power state afterward.
  496. */
  497. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  498. if (err)
  499. dev->wakeup.prepare_count = 0;
  500. out:
  501. mutex_unlock(&acpi_device_lock);
  502. return err;
  503. }
  504. /*
  505. * Shutdown a wakeup device, counterpart of above method
  506. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  507. * State Wake) for the device, if present
  508. * 2. Shutdown down the power resources
  509. */
  510. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  511. {
  512. struct acpi_power_resource_entry *entry;
  513. int err = 0;
  514. if (!dev || !dev->wakeup.flags.valid)
  515. return -EINVAL;
  516. mutex_lock(&acpi_device_lock);
  517. if (--dev->wakeup.prepare_count > 0)
  518. goto out;
  519. /*
  520. * Executing the code below even if prepare_count is already zero when
  521. * the function is called may be useful, for example for initialisation.
  522. */
  523. if (dev->wakeup.prepare_count < 0)
  524. dev->wakeup.prepare_count = 0;
  525. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  526. if (err)
  527. goto out;
  528. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  529. struct acpi_power_resource *resource = entry->resource;
  530. mutex_lock(&resource->resource_lock);
  531. if (resource->wakeup_enabled) {
  532. err = acpi_power_off_unlocked(resource);
  533. if (!err)
  534. resource->wakeup_enabled = false;
  535. }
  536. mutex_unlock(&resource->resource_lock);
  537. if (err) {
  538. dev_err(&dev->dev,
  539. "Cannot turn wakeup power resources off\n");
  540. dev->wakeup.flags.valid = 0;
  541. break;
  542. }
  543. }
  544. out:
  545. mutex_unlock(&acpi_device_lock);
  546. return err;
  547. }
  548. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  549. {
  550. int result = 0;
  551. int list_state = 0;
  552. int i = 0;
  553. if (!device || !state)
  554. return -EINVAL;
  555. /*
  556. * We know a device's inferred power state when all the resources
  557. * required for a given D-state are 'on'.
  558. */
  559. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  560. struct list_head *list = &device->power.states[i].resources;
  561. if (list_empty(list))
  562. continue;
  563. result = acpi_power_get_list_state(list, &list_state);
  564. if (result)
  565. return result;
  566. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  567. *state = i;
  568. return 0;
  569. }
  570. }
  571. *state = ACPI_STATE_D3_COLD;
  572. return 0;
  573. }
  574. int acpi_power_on_resources(struct acpi_device *device, int state)
  575. {
  576. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
  577. return -EINVAL;
  578. return acpi_power_on_list(&device->power.states[state].resources);
  579. }
  580. int acpi_power_transition(struct acpi_device *device, int state)
  581. {
  582. int result = 0;
  583. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  584. return -EINVAL;
  585. if (device->power.state == state || !device->flags.power_manageable)
  586. return 0;
  587. if ((device->power.state < ACPI_STATE_D0)
  588. || (device->power.state > ACPI_STATE_D3_COLD))
  589. return -ENODEV;
  590. /* TBD: Resources must be ordered. */
  591. /*
  592. * First we reference all power resources required in the target list
  593. * (e.g. so the device doesn't lose power while transitioning). Then,
  594. * we dereference all power resources used in the current list.
  595. */
  596. if (state < ACPI_STATE_D3_COLD)
  597. result = acpi_power_on_list(
  598. &device->power.states[state].resources);
  599. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  600. acpi_power_off_list(
  601. &device->power.states[device->power.state].resources);
  602. /* We shouldn't change the state unless the above operations succeed. */
  603. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  604. return result;
  605. }
  606. static void acpi_release_power_resource(struct device *dev)
  607. {
  608. struct acpi_device *device = to_acpi_device(dev);
  609. struct acpi_power_resource *resource;
  610. resource = container_of(device, struct acpi_power_resource, device);
  611. mutex_lock(&power_resource_list_lock);
  612. list_del(&resource->list_node);
  613. mutex_unlock(&power_resource_list_lock);
  614. acpi_free_pnp_ids(&device->pnp);
  615. kfree(resource);
  616. }
  617. static ssize_t acpi_power_in_use_show(struct device *dev,
  618. struct device_attribute *attr,
  619. char *buf) {
  620. struct acpi_power_resource *resource;
  621. resource = to_power_resource(to_acpi_device(dev));
  622. return sprintf(buf, "%u\n", !!resource->ref_count);
  623. }
  624. static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
  625. static void acpi_power_sysfs_remove(struct acpi_device *device)
  626. {
  627. device_remove_file(&device->dev, &dev_attr_resource_in_use);
  628. }
  629. int acpi_add_power_resource(acpi_handle handle)
  630. {
  631. struct acpi_power_resource *resource;
  632. struct acpi_device *device = NULL;
  633. union acpi_object acpi_object;
  634. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  635. acpi_status status;
  636. int state, result = -ENODEV;
  637. acpi_bus_get_device(handle, &device);
  638. if (device)
  639. return 0;
  640. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  641. if (!resource)
  642. return -ENOMEM;
  643. device = &resource->device;
  644. acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
  645. ACPI_STA_DEFAULT);
  646. mutex_init(&resource->resource_lock);
  647. INIT_LIST_HEAD(&resource->list_node);
  648. resource->name = device->pnp.bus_id;
  649. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  650. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  651. device->power.state = ACPI_STATE_UNKNOWN;
  652. /* Evalute the object to get the system level and resource order. */
  653. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  654. if (ACPI_FAILURE(status))
  655. goto err;
  656. resource->system_level = acpi_object.power_resource.system_level;
  657. resource->order = acpi_object.power_resource.resource_order;
  658. result = acpi_power_get_state(handle, &state);
  659. if (result)
  660. goto err;
  661. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  662. acpi_device_bid(device), state ? "on" : "off");
  663. device->flags.match_driver = true;
  664. result = acpi_device_add(device, acpi_release_power_resource);
  665. if (result)
  666. goto err;
  667. if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
  668. device->remove = acpi_power_sysfs_remove;
  669. mutex_lock(&power_resource_list_lock);
  670. list_add(&resource->list_node, &acpi_power_resource_list);
  671. mutex_unlock(&power_resource_list_lock);
  672. acpi_device_add_finalize(device);
  673. return 0;
  674. err:
  675. acpi_release_power_resource(&device->dev);
  676. return result;
  677. }
  678. #ifdef CONFIG_ACPI_SLEEP
  679. void acpi_resume_power_resources(void)
  680. {
  681. struct acpi_power_resource *resource;
  682. mutex_lock(&power_resource_list_lock);
  683. list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
  684. int result, state;
  685. mutex_lock(&resource->resource_lock);
  686. result = acpi_power_get_state(resource->device.handle, &state);
  687. if (result) {
  688. mutex_unlock(&resource->resource_lock);
  689. continue;
  690. }
  691. if (state == ACPI_POWER_RESOURCE_STATE_OFF
  692. && resource->ref_count) {
  693. dev_info(&resource->device.dev, "Turning ON\n");
  694. __acpi_power_on(resource);
  695. } else if (state == ACPI_POWER_RESOURCE_STATE_ON
  696. && !resource->ref_count) {
  697. dev_info(&resource->device.dev, "Turning OFF\n");
  698. __acpi_power_off(resource);
  699. }
  700. mutex_unlock(&resource->resource_lock);
  701. }
  702. mutex_unlock(&power_resource_list_lock);
  703. }
  704. #endif