power.c 19 KB

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