power.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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 <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #include "sleep.h"
  45. #include "internal.h"
  46. #define PREFIX "ACPI: "
  47. #define _COMPONENT ACPI_POWER_COMPONENT
  48. ACPI_MODULE_NAME("power");
  49. #define ACPI_POWER_CLASS "power_resource"
  50. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  51. #define ACPI_POWER_FILE_INFO "info"
  52. #define ACPI_POWER_FILE_STATUS "state"
  53. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  54. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  55. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  56. static int acpi_power_add(struct acpi_device *device);
  57. static int acpi_power_remove(struct acpi_device *device, int type);
  58. static const struct acpi_device_id power_device_ids[] = {
  59. {ACPI_POWER_HID, 0},
  60. {"", 0},
  61. };
  62. MODULE_DEVICE_TABLE(acpi, power_device_ids);
  63. #ifdef CONFIG_PM_SLEEP
  64. static int acpi_power_resume(struct device *dev);
  65. #endif
  66. static SIMPLE_DEV_PM_OPS(acpi_power_pm, NULL, acpi_power_resume);
  67. static struct acpi_driver acpi_power_driver = {
  68. .name = "power",
  69. .class = ACPI_POWER_CLASS,
  70. .ids = power_device_ids,
  71. .ops = {
  72. .add = acpi_power_add,
  73. .remove = acpi_power_remove,
  74. },
  75. .drv.pm = &acpi_power_pm,
  76. };
  77. /*
  78. * A power managed device
  79. * A device may rely on multiple power resources.
  80. * */
  81. struct acpi_power_managed_device {
  82. struct device *dev; /* The physical device */
  83. acpi_handle *handle;
  84. };
  85. struct acpi_power_resource_device {
  86. struct acpi_power_managed_device *device;
  87. struct acpi_power_resource_device *next;
  88. };
  89. struct acpi_power_resource {
  90. struct acpi_device * device;
  91. acpi_bus_id name;
  92. u32 system_level;
  93. u32 order;
  94. unsigned int ref_count;
  95. struct mutex resource_lock;
  96. /* List of devices relying on this power resource */
  97. struct acpi_power_resource_device *devices;
  98. struct mutex devices_lock;
  99. };
  100. static struct list_head acpi_power_resource_list;
  101. /* --------------------------------------------------------------------------
  102. Power Resource Management
  103. -------------------------------------------------------------------------- */
  104. static int
  105. acpi_power_get_context(acpi_handle handle,
  106. struct acpi_power_resource **resource)
  107. {
  108. int result = 0;
  109. struct acpi_device *device = NULL;
  110. if (!resource)
  111. return -ENODEV;
  112. result = acpi_bus_get_device(handle, &device);
  113. if (result) {
  114. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  115. return result;
  116. }
  117. *resource = acpi_driver_data(device);
  118. if (!*resource)
  119. return -ENODEV;
  120. return 0;
  121. }
  122. static int acpi_power_get_state(acpi_handle handle, int *state)
  123. {
  124. acpi_status status = AE_OK;
  125. unsigned long long sta = 0;
  126. char node_name[5];
  127. struct acpi_buffer buffer = { sizeof(node_name), node_name };
  128. if (!handle || !state)
  129. return -EINVAL;
  130. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  131. if (ACPI_FAILURE(status))
  132. return -ENODEV;
  133. *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
  134. ACPI_POWER_RESOURCE_STATE_OFF;
  135. acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
  136. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  137. node_name,
  138. *state ? "on" : "off"));
  139. return 0;
  140. }
  141. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  142. {
  143. int cur_state;
  144. int i = 0;
  145. if (!list || !state)
  146. return -EINVAL;
  147. /* The state of the list is 'on' IFF all resources are 'on'. */
  148. for (i = 0; i < list->count; i++) {
  149. struct acpi_power_resource *resource;
  150. acpi_handle handle = list->handles[i];
  151. int result;
  152. result = acpi_power_get_context(handle, &resource);
  153. if (result)
  154. return result;
  155. mutex_lock(&resource->resource_lock);
  156. result = acpi_power_get_state(handle, &cur_state);
  157. mutex_unlock(&resource->resource_lock);
  158. if (result)
  159. return result;
  160. if (cur_state != ACPI_POWER_RESOURCE_STATE_ON)
  161. break;
  162. }
  163. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  164. cur_state ? "on" : "off"));
  165. *state = cur_state;
  166. return 0;
  167. }
  168. /* Resume the device when all power resources in _PR0 are on */
  169. static void acpi_power_on_device(struct acpi_power_managed_device *device)
  170. {
  171. struct acpi_device *acpi_dev;
  172. acpi_handle handle = device->handle;
  173. int state;
  174. if (acpi_bus_get_device(handle, &acpi_dev))
  175. return;
  176. if(acpi_power_get_inferred_state(acpi_dev, &state))
  177. return;
  178. if (state == ACPI_STATE_D0 && pm_runtime_suspended(device->dev))
  179. pm_request_resume(device->dev);
  180. }
  181. static int __acpi_power_on(struct acpi_power_resource *resource)
  182. {
  183. acpi_status status = AE_OK;
  184. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  185. if (ACPI_FAILURE(status))
  186. return -ENODEV;
  187. /* Update the power resource's _device_ power state */
  188. resource->device->power.state = ACPI_STATE_D0;
  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(acpi_handle handle)
  194. {
  195. int result = 0;
  196. bool resume_device = false;
  197. struct acpi_power_resource *resource = NULL;
  198. struct acpi_power_resource_device *device_list;
  199. result = acpi_power_get_context(handle, &resource);
  200. if (result)
  201. return result;
  202. mutex_lock(&resource->resource_lock);
  203. if (resource->ref_count++) {
  204. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  205. "Power resource [%s] already on",
  206. resource->name));
  207. } else {
  208. result = __acpi_power_on(resource);
  209. if (result)
  210. resource->ref_count--;
  211. else
  212. resume_device = true;
  213. }
  214. mutex_unlock(&resource->resource_lock);
  215. if (!resume_device)
  216. return result;
  217. mutex_lock(&resource->devices_lock);
  218. device_list = resource->devices;
  219. while (device_list) {
  220. acpi_power_on_device(device_list->device);
  221. device_list = device_list->next;
  222. }
  223. mutex_unlock(&resource->devices_lock);
  224. return result;
  225. }
  226. static int acpi_power_off(acpi_handle handle)
  227. {
  228. int result = 0;
  229. acpi_status status = AE_OK;
  230. struct acpi_power_resource *resource = NULL;
  231. result = acpi_power_get_context(handle, &resource);
  232. if (result)
  233. return result;
  234. mutex_lock(&resource->resource_lock);
  235. if (!resource->ref_count) {
  236. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  237. "Power resource [%s] already off",
  238. resource->name));
  239. goto unlock;
  240. }
  241. if (--resource->ref_count) {
  242. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  243. "Power resource [%s] still in use\n",
  244. resource->name));
  245. goto unlock;
  246. }
  247. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  248. if (ACPI_FAILURE(status)) {
  249. result = -ENODEV;
  250. } else {
  251. /* Update the power resource's _device_ power state */
  252. resource->device->power.state = ACPI_STATE_D3;
  253. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  254. "Power resource [%s] turned off\n",
  255. resource->name));
  256. }
  257. unlock:
  258. mutex_unlock(&resource->resource_lock);
  259. return result;
  260. }
  261. static void __acpi_power_off_list(struct acpi_handle_list *list, int num_res)
  262. {
  263. int i;
  264. for (i = num_res - 1; i >= 0 ; i--)
  265. acpi_power_off(list->handles[i]);
  266. }
  267. static void acpi_power_off_list(struct acpi_handle_list *list)
  268. {
  269. __acpi_power_off_list(list, list->count);
  270. }
  271. static int acpi_power_on_list(struct acpi_handle_list *list)
  272. {
  273. int result = 0;
  274. int i;
  275. for (i = 0; i < list->count; i++) {
  276. result = acpi_power_on(list->handles[i]);
  277. if (result) {
  278. __acpi_power_off_list(list, i);
  279. break;
  280. }
  281. }
  282. return result;
  283. }
  284. static void __acpi_power_resource_unregister_device(struct device *dev,
  285. acpi_handle res_handle)
  286. {
  287. struct acpi_power_resource *resource = NULL;
  288. struct acpi_power_resource_device *prev, *curr;
  289. if (acpi_power_get_context(res_handle, &resource))
  290. return;
  291. mutex_lock(&resource->devices_lock);
  292. prev = NULL;
  293. curr = resource->devices;
  294. while (curr) {
  295. if (curr->device->dev == dev) {
  296. if (!prev)
  297. resource->devices = curr->next;
  298. else
  299. prev->next = curr->next;
  300. kfree(curr);
  301. break;
  302. }
  303. prev = curr;
  304. curr = curr->next;
  305. }
  306. mutex_unlock(&resource->devices_lock);
  307. }
  308. /* Unlink dev from all power resources in _PR0 */
  309. void acpi_power_resource_unregister_device(struct device *dev, acpi_handle handle)
  310. {
  311. struct acpi_device *acpi_dev;
  312. struct acpi_handle_list *list;
  313. int i;
  314. if (!dev || !handle)
  315. return;
  316. if (acpi_bus_get_device(handle, &acpi_dev))
  317. return;
  318. list = &acpi_dev->power.states[ACPI_STATE_D0].resources;
  319. for (i = 0; i < list->count; i++)
  320. __acpi_power_resource_unregister_device(dev,
  321. list->handles[i]);
  322. }
  323. EXPORT_SYMBOL_GPL(acpi_power_resource_unregister_device);
  324. static int __acpi_power_resource_register_device(
  325. struct acpi_power_managed_device *powered_device, acpi_handle handle)
  326. {
  327. struct acpi_power_resource *resource = NULL;
  328. struct acpi_power_resource_device *power_resource_device;
  329. int result;
  330. result = acpi_power_get_context(handle, &resource);
  331. if (result)
  332. return result;
  333. power_resource_device = kzalloc(
  334. sizeof(*power_resource_device), GFP_KERNEL);
  335. if (!power_resource_device)
  336. return -ENOMEM;
  337. power_resource_device->device = powered_device;
  338. mutex_lock(&resource->devices_lock);
  339. power_resource_device->next = resource->devices;
  340. resource->devices = power_resource_device;
  341. mutex_unlock(&resource->devices_lock);
  342. return 0;
  343. }
  344. /* Link dev to all power resources in _PR0 */
  345. int acpi_power_resource_register_device(struct device *dev, acpi_handle handle)
  346. {
  347. struct acpi_device *acpi_dev;
  348. struct acpi_handle_list *list;
  349. struct acpi_power_managed_device *powered_device;
  350. int i, ret;
  351. if (!dev || !handle)
  352. return -ENODEV;
  353. ret = acpi_bus_get_device(handle, &acpi_dev);
  354. if (ret)
  355. goto no_power_resource;
  356. if (!acpi_dev->power.flags.power_resources)
  357. goto no_power_resource;
  358. powered_device = kzalloc(sizeof(*powered_device), GFP_KERNEL);
  359. if (!powered_device)
  360. return -ENOMEM;
  361. powered_device->dev = dev;
  362. powered_device->handle = handle;
  363. list = &acpi_dev->power.states[ACPI_STATE_D0].resources;
  364. for (i = 0; i < list->count; i++) {
  365. ret = __acpi_power_resource_register_device(powered_device,
  366. list->handles[i]);
  367. if (ret) {
  368. acpi_power_resource_unregister_device(dev, handle);
  369. break;
  370. }
  371. }
  372. return ret;
  373. no_power_resource:
  374. printk(KERN_DEBUG PREFIX "Invalid Power Resource to register!");
  375. return -ENODEV;
  376. }
  377. EXPORT_SYMBOL_GPL(acpi_power_resource_register_device);
  378. /**
  379. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  380. * ACPI 3.0) _PSW (Power State Wake)
  381. * @dev: Device to handle.
  382. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  383. * @sleep_state: Target sleep state of the system.
  384. * @dev_state: Target power state of the device.
  385. *
  386. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  387. * State Wake) for the device, if present. On failure reset the device's
  388. * wakeup.flags.valid flag.
  389. *
  390. * RETURN VALUE:
  391. * 0 if either _DSW or _PSW has been successfully executed
  392. * 0 if neither _DSW nor _PSW has been found
  393. * -ENODEV if the execution of either _DSW or _PSW has failed
  394. */
  395. int acpi_device_sleep_wake(struct acpi_device *dev,
  396. int enable, int sleep_state, int dev_state)
  397. {
  398. union acpi_object in_arg[3];
  399. struct acpi_object_list arg_list = { 3, in_arg };
  400. acpi_status status = AE_OK;
  401. /*
  402. * Try to execute _DSW first.
  403. *
  404. * Three agruments are needed for the _DSW object:
  405. * Argument 0: enable/disable the wake capabilities
  406. * Argument 1: target system state
  407. * Argument 2: target device state
  408. * When _DSW object is called to disable the wake capabilities, maybe
  409. * the first argument is filled. The values of the other two agruments
  410. * are meaningless.
  411. */
  412. in_arg[0].type = ACPI_TYPE_INTEGER;
  413. in_arg[0].integer.value = enable;
  414. in_arg[1].type = ACPI_TYPE_INTEGER;
  415. in_arg[1].integer.value = sleep_state;
  416. in_arg[2].type = ACPI_TYPE_INTEGER;
  417. in_arg[2].integer.value = dev_state;
  418. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  419. if (ACPI_SUCCESS(status)) {
  420. return 0;
  421. } else if (status != AE_NOT_FOUND) {
  422. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  423. dev->wakeup.flags.valid = 0;
  424. return -ENODEV;
  425. }
  426. /* Execute _PSW */
  427. arg_list.count = 1;
  428. in_arg[0].integer.value = enable;
  429. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  430. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  431. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  432. dev->wakeup.flags.valid = 0;
  433. return -ENODEV;
  434. }
  435. return 0;
  436. }
  437. /*
  438. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  439. * 1. Power on the power resources required for the wakeup device
  440. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  441. * State Wake) for the device, if present
  442. */
  443. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  444. {
  445. int i, err = 0;
  446. if (!dev || !dev->wakeup.flags.valid)
  447. return -EINVAL;
  448. mutex_lock(&acpi_device_lock);
  449. if (dev->wakeup.prepare_count++)
  450. goto out;
  451. /* Open power resource */
  452. for (i = 0; i < dev->wakeup.resources.count; i++) {
  453. int ret = acpi_power_on(dev->wakeup.resources.handles[i]);
  454. if (ret) {
  455. printk(KERN_ERR PREFIX "Transition power state\n");
  456. dev->wakeup.flags.valid = 0;
  457. err = -ENODEV;
  458. goto err_out;
  459. }
  460. }
  461. /*
  462. * Passing 3 as the third argument below means the device may be placed
  463. * in arbitrary power state afterwards.
  464. */
  465. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  466. err_out:
  467. if (err)
  468. dev->wakeup.prepare_count = 0;
  469. out:
  470. mutex_unlock(&acpi_device_lock);
  471. return err;
  472. }
  473. /*
  474. * Shutdown a wakeup device, counterpart of above method
  475. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  476. * State Wake) for the device, if present
  477. * 2. Shutdown down the power resources
  478. */
  479. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  480. {
  481. int i, err = 0;
  482. if (!dev || !dev->wakeup.flags.valid)
  483. return -EINVAL;
  484. mutex_lock(&acpi_device_lock);
  485. if (--dev->wakeup.prepare_count > 0)
  486. goto out;
  487. /*
  488. * Executing the code below even if prepare_count is already zero when
  489. * the function is called may be useful, for example for initialisation.
  490. */
  491. if (dev->wakeup.prepare_count < 0)
  492. dev->wakeup.prepare_count = 0;
  493. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  494. if (err)
  495. goto out;
  496. /* Close power resource */
  497. for (i = 0; i < dev->wakeup.resources.count; i++) {
  498. int ret = acpi_power_off(dev->wakeup.resources.handles[i]);
  499. if (ret) {
  500. printk(KERN_ERR PREFIX "Transition power state\n");
  501. dev->wakeup.flags.valid = 0;
  502. err = -ENODEV;
  503. goto out;
  504. }
  505. }
  506. out:
  507. mutex_unlock(&acpi_device_lock);
  508. return err;
  509. }
  510. /* --------------------------------------------------------------------------
  511. Device Power Management
  512. -------------------------------------------------------------------------- */
  513. int acpi_power_get_inferred_state(struct acpi_device *device, int *state)
  514. {
  515. int result = 0;
  516. struct acpi_handle_list *list = NULL;
  517. int list_state = 0;
  518. int i = 0;
  519. if (!device || !state)
  520. return -EINVAL;
  521. /*
  522. * We know a device's inferred power state when all the resources
  523. * required for a given D-state are 'on'.
  524. */
  525. for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
  526. list = &device->power.states[i].resources;
  527. if (list->count < 1)
  528. continue;
  529. result = acpi_power_get_list_state(list, &list_state);
  530. if (result)
  531. return result;
  532. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  533. *state = i;
  534. return 0;
  535. }
  536. }
  537. *state = ACPI_STATE_D3;
  538. return 0;
  539. }
  540. int acpi_power_on_resources(struct acpi_device *device, int state)
  541. {
  542. if (!device || state < ACPI_STATE_D0 || state > ACPI_STATE_D3)
  543. return -EINVAL;
  544. return acpi_power_on_list(&device->power.states[state].resources);
  545. }
  546. int acpi_power_transition(struct acpi_device *device, int state)
  547. {
  548. int result = 0;
  549. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  550. return -EINVAL;
  551. if (device->power.state == state)
  552. return 0;
  553. if ((device->power.state < ACPI_STATE_D0)
  554. || (device->power.state > ACPI_STATE_D3_COLD))
  555. return -ENODEV;
  556. /* TBD: Resources must be ordered. */
  557. /*
  558. * First we reference all power resources required in the target list
  559. * (e.g. so the device doesn't lose power while transitioning). Then,
  560. * we dereference all power resources used in the current list.
  561. */
  562. if (state < ACPI_STATE_D3_COLD)
  563. result = acpi_power_on_list(
  564. &device->power.states[state].resources);
  565. if (!result && device->power.state < ACPI_STATE_D3_COLD)
  566. acpi_power_off_list(
  567. &device->power.states[device->power.state].resources);
  568. /* We shouldn't change the state unless the above operations succeed. */
  569. device->power.state = result ? ACPI_STATE_UNKNOWN : state;
  570. return result;
  571. }
  572. /* --------------------------------------------------------------------------
  573. Driver Interface
  574. -------------------------------------------------------------------------- */
  575. static int acpi_power_add(struct acpi_device *device)
  576. {
  577. int result = 0, state;
  578. acpi_status status = AE_OK;
  579. struct acpi_power_resource *resource = NULL;
  580. union acpi_object acpi_object;
  581. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  582. if (!device)
  583. return -EINVAL;
  584. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  585. if (!resource)
  586. return -ENOMEM;
  587. resource->device = device;
  588. mutex_init(&resource->resource_lock);
  589. mutex_init(&resource->devices_lock);
  590. strcpy(resource->name, device->pnp.bus_id);
  591. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  592. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  593. device->driver_data = resource;
  594. /* Evalute the object to get the system level and resource order. */
  595. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  596. if (ACPI_FAILURE(status)) {
  597. result = -ENODEV;
  598. goto end;
  599. }
  600. resource->system_level = acpi_object.power_resource.system_level;
  601. resource->order = acpi_object.power_resource.resource_order;
  602. result = acpi_power_get_state(device->handle, &state);
  603. if (result)
  604. goto end;
  605. switch (state) {
  606. case ACPI_POWER_RESOURCE_STATE_ON:
  607. device->power.state = ACPI_STATE_D0;
  608. break;
  609. case ACPI_POWER_RESOURCE_STATE_OFF:
  610. device->power.state = ACPI_STATE_D3;
  611. break;
  612. default:
  613. device->power.state = ACPI_STATE_UNKNOWN;
  614. break;
  615. }
  616. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  617. acpi_device_bid(device), state ? "on" : "off");
  618. end:
  619. if (result)
  620. kfree(resource);
  621. return result;
  622. }
  623. static int acpi_power_remove(struct acpi_device *device, int type)
  624. {
  625. struct acpi_power_resource *resource;
  626. if (!device)
  627. return -EINVAL;
  628. resource = acpi_driver_data(device);
  629. if (!resource)
  630. return -EINVAL;
  631. kfree(resource);
  632. return 0;
  633. }
  634. #ifdef CONFIG_PM_SLEEP
  635. static int acpi_power_resume(struct device *dev)
  636. {
  637. int result = 0, state;
  638. struct acpi_device *device;
  639. struct acpi_power_resource *resource;
  640. if (!dev)
  641. return -EINVAL;
  642. device = to_acpi_device(dev);
  643. resource = acpi_driver_data(device);
  644. if (!resource)
  645. return -EINVAL;
  646. mutex_lock(&resource->resource_lock);
  647. result = acpi_power_get_state(device->handle, &state);
  648. if (result)
  649. goto unlock;
  650. if (state == ACPI_POWER_RESOURCE_STATE_OFF && resource->ref_count)
  651. result = __acpi_power_on(resource);
  652. unlock:
  653. mutex_unlock(&resource->resource_lock);
  654. return result;
  655. }
  656. #endif
  657. int __init acpi_power_init(void)
  658. {
  659. INIT_LIST_HEAD(&acpi_power_resource_list);
  660. return acpi_bus_register_driver(&acpi_power_driver);
  661. }