power.c 24 KB

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