power.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 void acpi_power_hide_list(struct acpi_device *adev, int state)
  377. {
  378. struct acpi_device_power_state *ps = &adev->power.states[state];
  379. struct acpi_power_resource_entry *entry;
  380. if (list_empty(&ps->resources))
  381. return;
  382. list_for_each_entry_reverse(entry, &ps->resources, node) {
  383. struct acpi_device *res_dev = &entry->resource->device;
  384. sysfs_remove_link_from_group(&adev->dev.kobj,
  385. attr_groups[state].name,
  386. dev_name(&res_dev->dev));
  387. }
  388. sysfs_remove_group(&adev->dev.kobj, &attr_groups[state]);
  389. }
  390. static void acpi_power_expose_list(struct acpi_device *adev, int state)
  391. {
  392. struct acpi_device_power_state *ps = &adev->power.states[state];
  393. struct acpi_power_resource_entry *entry;
  394. int ret;
  395. if (list_empty(&ps->resources))
  396. return;
  397. ret = sysfs_create_group(&adev->dev.kobj, &attr_groups[state]);
  398. if (ret)
  399. return;
  400. list_for_each_entry(entry, &ps->resources, node) {
  401. struct acpi_device *res_dev = &entry->resource->device;
  402. ret = sysfs_add_link_to_group(&adev->dev.kobj,
  403. attr_groups[state].name,
  404. &res_dev->dev.kobj,
  405. dev_name(&res_dev->dev));
  406. if (ret) {
  407. acpi_power_hide_list(adev, state);
  408. break;
  409. }
  410. }
  411. }
  412. void acpi_power_add_remove_device(struct acpi_device *adev, bool add)
  413. {
  414. struct acpi_device_power_state *ps;
  415. struct acpi_power_resource_entry *entry;
  416. int state;
  417. if (!adev->power.flags.power_resources)
  418. return;
  419. ps = &adev->power.states[ACPI_STATE_D0];
  420. list_for_each_entry(entry, &ps->resources, node) {
  421. struct acpi_power_resource *resource = entry->resource;
  422. if (add)
  423. acpi_power_add_dependent(resource, adev);
  424. else
  425. acpi_power_remove_dependent(resource, adev);
  426. }
  427. for (state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) {
  428. if (add)
  429. acpi_power_expose_list(adev, state);
  430. else
  431. acpi_power_hide_list(adev, state);
  432. }
  433. }
  434. int acpi_power_wakeup_list_init(struct list_head *list, int *system_level_p)
  435. {
  436. struct acpi_power_resource_entry *entry;
  437. int system_level = 5;
  438. list_for_each_entry(entry, list, node) {
  439. struct acpi_power_resource *resource = entry->resource;
  440. acpi_handle handle = resource->device.handle;
  441. int result;
  442. int state;
  443. mutex_lock(&resource->resource_lock);
  444. result = acpi_power_get_state(handle, &state);
  445. if (result) {
  446. mutex_unlock(&resource->resource_lock);
  447. return result;
  448. }
  449. if (state == ACPI_POWER_RESOURCE_STATE_ON) {
  450. resource->ref_count++;
  451. resource->wakeup_enabled = true;
  452. }
  453. if (system_level > resource->system_level)
  454. system_level = resource->system_level;
  455. mutex_unlock(&resource->resource_lock);
  456. }
  457. *system_level_p = system_level;
  458. return 0;
  459. }
  460. /* --------------------------------------------------------------------------
  461. Device Power Management
  462. -------------------------------------------------------------------------- */
  463. /**
  464. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  465. * ACPI 3.0) _PSW (Power State Wake)
  466. * @dev: Device to handle.
  467. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  468. * @sleep_state: Target sleep state of the system.
  469. * @dev_state: Target power state of the device.
  470. *
  471. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  472. * State Wake) for the device, if present. On failure reset the device's
  473. * wakeup.flags.valid flag.
  474. *
  475. * RETURN VALUE:
  476. * 0 if either _DSW or _PSW has been successfully executed
  477. * 0 if neither _DSW nor _PSW has been found
  478. * -ENODEV if the execution of either _DSW or _PSW has failed
  479. */
  480. int acpi_device_sleep_wake(struct acpi_device *dev,
  481. int enable, int sleep_state, int dev_state)
  482. {
  483. union acpi_object in_arg[3];
  484. struct acpi_object_list arg_list = { 3, in_arg };
  485. acpi_status status = AE_OK;
  486. /*
  487. * Try to execute _DSW first.
  488. *
  489. * Three agruments are needed for the _DSW object:
  490. * Argument 0: enable/disable the wake capabilities
  491. * Argument 1: target system state
  492. * Argument 2: target device state
  493. * When _DSW object is called to disable the wake capabilities, maybe
  494. * the first argument is filled. The values of the other two agruments
  495. * are meaningless.
  496. */
  497. in_arg[0].type = ACPI_TYPE_INTEGER;
  498. in_arg[0].integer.value = enable;
  499. in_arg[1].type = ACPI_TYPE_INTEGER;
  500. in_arg[1].integer.value = sleep_state;
  501. in_arg[2].type = ACPI_TYPE_INTEGER;
  502. in_arg[2].integer.value = dev_state;
  503. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  504. if (ACPI_SUCCESS(status)) {
  505. return 0;
  506. } else if (status != AE_NOT_FOUND) {
  507. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  508. dev->wakeup.flags.valid = 0;
  509. return -ENODEV;
  510. }
  511. /* Execute _PSW */
  512. arg_list.count = 1;
  513. in_arg[0].integer.value = enable;
  514. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  515. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  516. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  517. dev->wakeup.flags.valid = 0;
  518. return -ENODEV;
  519. }
  520. return 0;
  521. }
  522. /*
  523. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  524. * 1. Power on the power resources required for the wakeup device
  525. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  526. * State Wake) for the device, if present
  527. */
  528. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  529. {
  530. struct acpi_power_resource_entry *entry;
  531. int err = 0;
  532. if (!dev || !dev->wakeup.flags.valid)
  533. return -EINVAL;
  534. mutex_lock(&acpi_device_lock);
  535. if (dev->wakeup.prepare_count++)
  536. goto out;
  537. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  538. struct acpi_power_resource *resource = entry->resource;
  539. mutex_lock(&resource->resource_lock);
  540. if (!resource->wakeup_enabled) {
  541. err = acpi_power_on_unlocked(resource);
  542. if (!err)
  543. resource->wakeup_enabled = true;
  544. }
  545. mutex_unlock(&resource->resource_lock);
  546. if (err) {
  547. dev_err(&dev->dev,
  548. "Cannot turn wakeup power resources on\n");
  549. dev->wakeup.flags.valid = 0;
  550. goto out;
  551. }
  552. }
  553. /*
  554. * Passing 3 as the third argument below means the device may be
  555. * put into arbitrary power state afterward.
  556. */
  557. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  558. if (err)
  559. dev->wakeup.prepare_count = 0;
  560. out:
  561. mutex_unlock(&acpi_device_lock);
  562. return err;
  563. }
  564. /*
  565. * Shutdown a wakeup device, counterpart of above method
  566. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  567. * State Wake) for the device, if present
  568. * 2. Shutdown down the power resources
  569. */
  570. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  571. {
  572. struct acpi_power_resource_entry *entry;
  573. int err = 0;
  574. if (!dev || !dev->wakeup.flags.valid)
  575. return -EINVAL;
  576. mutex_lock(&acpi_device_lock);
  577. if (--dev->wakeup.prepare_count > 0)
  578. goto out;
  579. /*
  580. * Executing the code below even if prepare_count is already zero when
  581. * the function is called may be useful, for example for initialisation.
  582. */
  583. if (dev->wakeup.prepare_count < 0)
  584. dev->wakeup.prepare_count = 0;
  585. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  586. if (err)
  587. goto out;
  588. list_for_each_entry(entry, &dev->wakeup.resources, node) {
  589. struct acpi_power_resource *resource = entry->resource;
  590. mutex_lock(&resource->resource_lock);
  591. if (resource->wakeup_enabled) {
  592. err = acpi_power_off_unlocked(resource);
  593. if (!err)
  594. resource->wakeup_enabled = false;
  595. }
  596. mutex_unlock(&resource->resource_lock);
  597. if (err) {
  598. dev_err(&dev->dev,
  599. "Cannot turn wakeup power resources off\n");
  600. dev->wakeup.flags.valid = 0;
  601. break;
  602. }
  603. }
  604. out:
  605. mutex_unlock(&acpi_device_lock);
  606. return err;
  607. }
  608. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  609. {
  610. int result = 0;
  611. int list_state = 0;
  612. int i = 0;
  613. if (!device || !state)
  614. return -EINVAL;
  615. /*
  616. * We know a device's inferred power state when all the resources
  617. * required for a given D-state are 'on'.
  618. */
  619. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  620. struct list_head *list = &device->power.states[i].resources;
  621. if (list_empty(list))
  622. continue;
  623. result = acpi_power_get_list_state(list, &list_state);
  624. if (result)
  625. return result;
  626. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  627. *state = i;
  628. return 0;
  629. }
  630. }
  631. *state = ACPI_STATE_D3;
  632. return 0;
  633. }
  634. int acpi_power_on_resources(struct acpi_device *device, int state)
  635. {
  636. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3_HOT)
  637. return -EINVAL;
  638. return acpi_power_on_list(&device->power.states[state].resources);
  639. }
  640. int acpi_power_transition(struct acpi_device *device, int state)
  641. {
  642. int result = 0;
  643. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  644. return -EINVAL;
  645. if (device->power.state == state || !device->flags.power_manageable)
  646. return 0;
  647. if ((device->power.state < ACPI_STATE_D0)
  648. || (device->power.state > ACPI_STATE_D3_COLD))
  649. return -ENODEV;
  650. /* TBD: Resources must be ordered. */
  651. /*
  652. * First we reference all power resources required in the target list
  653. * (e.g. so the device doesn't lose power while transitioning). Then,
  654. * we dereference all power resources used in the current list.
  655. */
  656. if (state < ACPI_STATE_D3_COLD)
  657. result = acpi_power_on_list(
  658. &device->power.states[state].resources);
  659. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  660. acpi_power_off_list(
  661. &device->power.states[device->power.state].resources);
  662. /* We shouldn't change the state unless the above operations succeed. */
  663. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  664. return result;
  665. }
  666. static void acpi_release_power_resource(struct device *dev)
  667. {
  668. struct acpi_device *device = to_acpi_device(dev);
  669. struct acpi_power_resource *resource;
  670. resource = container_of(device, struct acpi_power_resource, device);
  671. mutex_lock(&power_resource_list_lock);
  672. list_del(&resource->list_node);
  673. mutex_unlock(&power_resource_list_lock);
  674. acpi_free_ids(device);
  675. kfree(resource);
  676. }
  677. static ssize_t acpi_power_in_use_show(struct device *dev,
  678. struct device_attribute *attr,
  679. char *buf) {
  680. struct acpi_power_resource *resource;
  681. resource = to_power_resource(to_acpi_device(dev));
  682. return sprintf(buf, "%u\n", !!resource->ref_count);
  683. }
  684. static DEVICE_ATTR(resource_in_use, 0444, acpi_power_in_use_show, NULL);
  685. static void acpi_power_sysfs_remove(struct acpi_device *device)
  686. {
  687. device_remove_file(&device->dev, &dev_attr_resource_in_use);
  688. }
  689. int acpi_add_power_resource(acpi_handle handle)
  690. {
  691. struct acpi_power_resource *resource;
  692. struct acpi_device *device = NULL;
  693. union acpi_object acpi_object;
  694. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  695. acpi_status status;
  696. int state, result = -ENODEV;
  697. acpi_bus_get_device(handle, &device);
  698. if (device)
  699. return 0;
  700. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  701. if (!resource)
  702. return -ENOMEM;
  703. device = &resource->device;
  704. acpi_init_device_object(device, handle, ACPI_BUS_TYPE_POWER,
  705. ACPI_STA_DEFAULT);
  706. mutex_init(&resource->resource_lock);
  707. INIT_LIST_HEAD(&resource->dependent);
  708. resource->name = device->pnp.bus_id;
  709. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  710. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  711. device->power.state = ACPI_STATE_UNKNOWN;
  712. /* Evalute the object to get the system level and resource order. */
  713. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  714. if (ACPI_FAILURE(status))
  715. goto err;
  716. resource->system_level = acpi_object.power_resource.system_level;
  717. resource->order = acpi_object.power_resource.resource_order;
  718. result = acpi_power_get_state(handle, &state);
  719. if (result)
  720. goto err;
  721. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  722. acpi_device_bid(device), state ? "on" : "off");
  723. device->flags.match_driver = true;
  724. result = acpi_device_add(device, acpi_release_power_resource);
  725. if (result)
  726. goto err;
  727. if (!device_create_file(&device->dev, &dev_attr_resource_in_use))
  728. device->remove = acpi_power_sysfs_remove;
  729. mutex_lock(&power_resource_list_lock);
  730. list_add(&resource->list_node, &acpi_power_resource_list);
  731. mutex_unlock(&power_resource_list_lock);
  732. acpi_device_add_finalize(device);
  733. return 0;
  734. err:
  735. acpi_release_power_resource(&device->dev);
  736. return result;
  737. }
  738. #ifdef CONFIG_ACPI_SLEEP
  739. void acpi_resume_power_resources(void)
  740. {
  741. struct acpi_power_resource *resource;
  742. mutex_lock(&power_resource_list_lock);
  743. list_for_each_entry(resource, &acpi_power_resource_list, list_node) {
  744. int result, state;
  745. mutex_lock(&resource->resource_lock);
  746. result = acpi_power_get_state(resource->device.handle, &state);
  747. if (result)
  748. continue;
  749. if (state == ACPI_POWER_RESOURCE_STATE_OFF
  750. && resource->ref_count) {
  751. dev_info(&resource->device.dev, "Turning ON\n");
  752. __acpi_power_on(resource);
  753. } else if (state == ACPI_POWER_RESOURCE_STATE_ON
  754. && !resource->ref_count) {
  755. dev_info(&resource->device.dev, "Turning OFF\n");
  756. __acpi_power_off(resource);
  757. }
  758. mutex_unlock(&resource->resource_lock);
  759. }
  760. mutex_unlock(&power_resource_list_lock);
  761. }
  762. #endif