power.c 22 KB

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