power.c 22 KB

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