power.c 22 KB

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