power.c 20 KB

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