power.c 22 KB

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