power.c 23 KB

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