power.c 20 KB

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