power.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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/proc_fs.h>
  41. #include <linux/seq_file.h>
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #define _COMPONENT ACPI_POWER_COMPONENT
  45. ACPI_MODULE_NAME("acpi_power")
  46. #define ACPI_POWER_COMPONENT 0x00800000
  47. #define ACPI_POWER_CLASS "power_resource"
  48. #define ACPI_POWER_DRIVER_NAME "ACPI Power Resource Driver"
  49. #define ACPI_POWER_DEVICE_NAME "Power Resource"
  50. #define ACPI_POWER_FILE_INFO "info"
  51. #define ACPI_POWER_FILE_STATUS "state"
  52. #define ACPI_POWER_RESOURCE_STATE_OFF 0x00
  53. #define ACPI_POWER_RESOURCE_STATE_ON 0x01
  54. #define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
  55. static int acpi_power_add(struct acpi_device *device);
  56. static int acpi_power_remove(struct acpi_device *device, int type);
  57. static int acpi_power_resume(struct acpi_device *device, int state);
  58. static int acpi_power_open_fs(struct inode *inode, struct file *file);
  59. static struct acpi_driver acpi_power_driver = {
  60. .name = ACPI_POWER_DRIVER_NAME,
  61. .class = ACPI_POWER_CLASS,
  62. .ids = ACPI_POWER_HID,
  63. .ops = {
  64. .add = acpi_power_add,
  65. .remove = acpi_power_remove,
  66. .resume = acpi_power_resume,
  67. },
  68. };
  69. struct acpi_power_reference {
  70. struct list_head node;
  71. struct acpi_device *device;
  72. };
  73. struct acpi_power_resource {
  74. struct acpi_device * device;
  75. acpi_bus_id name;
  76. u32 system_level;
  77. u32 order;
  78. int state;
  79. struct mutex resource_lock;
  80. struct list_head reference;
  81. };
  82. static struct list_head acpi_power_resource_list;
  83. static const struct file_operations acpi_power_fops = {
  84. .open = acpi_power_open_fs,
  85. .read = seq_read,
  86. .llseek = seq_lseek,
  87. .release = single_release,
  88. };
  89. /* --------------------------------------------------------------------------
  90. Power Resource Management
  91. -------------------------------------------------------------------------- */
  92. static int
  93. acpi_power_get_context(acpi_handle handle,
  94. struct acpi_power_resource **resource)
  95. {
  96. int result = 0;
  97. struct acpi_device *device = NULL;
  98. if (!resource)
  99. return -ENODEV;
  100. result = acpi_bus_get_device(handle, &device);
  101. if (result) {
  102. printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
  103. return result;
  104. }
  105. *resource = acpi_driver_data(device);
  106. if (!resource)
  107. return -ENODEV;
  108. return 0;
  109. }
  110. static int acpi_power_get_state(struct acpi_power_resource *resource)
  111. {
  112. acpi_status status = AE_OK;
  113. unsigned long sta = 0;
  114. if (!resource)
  115. return -EINVAL;
  116. status = acpi_evaluate_integer(resource->device->handle, "_STA", NULL, &sta);
  117. if (ACPI_FAILURE(status))
  118. return -ENODEV;
  119. if (sta & 0x01)
  120. resource->state = ACPI_POWER_RESOURCE_STATE_ON;
  121. else
  122. resource->state = ACPI_POWER_RESOURCE_STATE_OFF;
  123. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
  124. resource->name, resource->state ? "on" : "off"));
  125. return 0;
  126. }
  127. static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
  128. {
  129. int result = 0;
  130. struct acpi_power_resource *resource = NULL;
  131. u32 i = 0;
  132. if (!list || !state)
  133. return -EINVAL;
  134. /* The state of the list is 'on' IFF all resources are 'on'. */
  135. for (i = 0; i < list->count; i++) {
  136. result = acpi_power_get_context(list->handles[i], &resource);
  137. if (result)
  138. return result;
  139. result = acpi_power_get_state(resource);
  140. if (result)
  141. return result;
  142. *state = resource->state;
  143. if (*state != ACPI_POWER_RESOURCE_STATE_ON)
  144. break;
  145. }
  146. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  147. *state ? "on" : "off"));
  148. return result;
  149. }
  150. static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
  151. {
  152. int result = 0;
  153. int found = 0;
  154. acpi_status status = AE_OK;
  155. struct acpi_power_resource *resource = NULL;
  156. struct list_head *node, *next;
  157. struct acpi_power_reference *ref;
  158. result = acpi_power_get_context(handle, &resource);
  159. if (result)
  160. return result;
  161. mutex_lock(&resource->resource_lock);
  162. list_for_each_safe(node, next, &resource->reference) {
  163. ref = container_of(node, struct acpi_power_reference, node);
  164. if (dev->handle == ref->device->handle) {
  165. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
  166. dev->pnp.bus_id, resource->name));
  167. found = 1;
  168. break;
  169. }
  170. }
  171. if (!found) {
  172. ref = kmalloc(sizeof (struct acpi_power_reference),
  173. irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
  174. if (!ref) {
  175. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
  176. mutex_unlock(&resource->resource_lock);
  177. return -ENOMEM;
  178. }
  179. list_add_tail(&ref->node, &resource->reference);
  180. ref->device = dev;
  181. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
  182. dev->pnp.bus_id, resource->name));
  183. }
  184. mutex_unlock(&resource->resource_lock);
  185. if (resource->state == ACPI_POWER_RESOURCE_STATE_ON) {
  186. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already on\n",
  187. resource->name));
  188. return 0;
  189. }
  190. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  191. if (ACPI_FAILURE(status))
  192. return -ENODEV;
  193. result = acpi_power_get_state(resource);
  194. if (result)
  195. return result;
  196. if (resource->state != ACPI_POWER_RESOURCE_STATE_ON)
  197. return -ENOEXEC;
  198. /* Update the power resource's _device_ power state */
  199. resource->device->power.state = ACPI_STATE_D0;
  200. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
  201. resource->name));
  202. return 0;
  203. }
  204. static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
  205. {
  206. int result = 0;
  207. acpi_status status = AE_OK;
  208. struct acpi_power_resource *resource = NULL;
  209. struct list_head *node, *next;
  210. struct acpi_power_reference *ref;
  211. result = acpi_power_get_context(handle, &resource);
  212. if (result)
  213. return result;
  214. mutex_lock(&resource->resource_lock);
  215. list_for_each_safe(node, next, &resource->reference) {
  216. ref = container_of(node, struct acpi_power_reference, node);
  217. if (dev->handle == ref->device->handle) {
  218. list_del(&ref->node);
  219. kfree(ref);
  220. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
  221. dev->pnp.bus_id, resource->name));
  222. break;
  223. }
  224. }
  225. if (!list_empty(&resource->reference)) {
  226. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
  227. resource->name));
  228. mutex_unlock(&resource->resource_lock);
  229. return 0;
  230. }
  231. mutex_unlock(&resource->resource_lock);
  232. if (resource->state == ACPI_POWER_RESOURCE_STATE_OFF) {
  233. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] already off\n",
  234. resource->name));
  235. return 0;
  236. }
  237. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  238. if (ACPI_FAILURE(status))
  239. return -ENODEV;
  240. result = acpi_power_get_state(resource);
  241. if (result)
  242. return result;
  243. if (resource->state != ACPI_POWER_RESOURCE_STATE_OFF)
  244. return -ENOEXEC;
  245. /* Update the power resource's _device_ power state */
  246. resource->device->power.state = ACPI_STATE_D3;
  247. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
  248. resource->name));
  249. return 0;
  250. }
  251. /*
  252. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  253. * 1. Power on the power resources required for the wakeup device
  254. * 2. Enable _PSW (power state wake) for the device if present
  255. */
  256. int acpi_enable_wakeup_device_power(struct acpi_device *dev)
  257. {
  258. union acpi_object arg = { ACPI_TYPE_INTEGER };
  259. struct acpi_object_list arg_list = { 1, &arg };
  260. acpi_status status = AE_OK;
  261. int i;
  262. int ret = 0;
  263. if (!dev || !dev->wakeup.flags.valid)
  264. return -1;
  265. arg.integer.value = 1;
  266. /* Open power resource */
  267. for (i = 0; i < dev->wakeup.resources.count; i++) {
  268. ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
  269. if (ret) {
  270. printk(KERN_ERR PREFIX "Transition power state\n");
  271. dev->wakeup.flags.valid = 0;
  272. return -1;
  273. }
  274. }
  275. /* Execute PSW */
  276. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  277. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  278. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  279. dev->wakeup.flags.valid = 0;
  280. ret = -1;
  281. }
  282. return ret;
  283. }
  284. /*
  285. * Shutdown a wakeup device, counterpart of above method
  286. * 1. Disable _PSW (power state wake)
  287. * 2. Shutdown down the power resources
  288. */
  289. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  290. {
  291. union acpi_object arg = { ACPI_TYPE_INTEGER };
  292. struct acpi_object_list arg_list = { 1, &arg };
  293. acpi_status status = AE_OK;
  294. int i;
  295. int ret = 0;
  296. if (!dev || !dev->wakeup.flags.valid)
  297. return -1;
  298. arg.integer.value = 0;
  299. /* Execute PSW */
  300. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  301. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  302. printk(KERN_ERR PREFIX "Evaluate _PSW\n");
  303. dev->wakeup.flags.valid = 0;
  304. return -1;
  305. }
  306. /* Close power resource */
  307. for (i = 0; i < dev->wakeup.resources.count; i++) {
  308. ret = acpi_power_off_device(dev->wakeup.resources.handles[i], dev);
  309. if (ret) {
  310. printk(KERN_ERR PREFIX "Transition power state\n");
  311. dev->wakeup.flags.valid = 0;
  312. return -1;
  313. }
  314. }
  315. return ret;
  316. }
  317. /* --------------------------------------------------------------------------
  318. Device Power Management
  319. -------------------------------------------------------------------------- */
  320. int acpi_power_get_inferred_state(struct acpi_device *device)
  321. {
  322. int result = 0;
  323. struct acpi_handle_list *list = NULL;
  324. int list_state = 0;
  325. int i = 0;
  326. if (!device)
  327. return -EINVAL;
  328. device->power.state = ACPI_STATE_UNKNOWN;
  329. /*
  330. * We know a device's inferred power state when all the resources
  331. * required for a given D-state are 'on'.
  332. */
  333. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  334. list = &device->power.states[i].resources;
  335. if (list->count < 1)
  336. continue;
  337. result = acpi_power_get_list_state(list, &list_state);
  338. if (result)
  339. return result;
  340. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  341. device->power.state = i;
  342. return 0;
  343. }
  344. }
  345. device->power.state = ACPI_STATE_D3;
  346. return 0;
  347. }
  348. int acpi_power_transition(struct acpi_device *device, int state)
  349. {
  350. int result = 0;
  351. struct acpi_handle_list *cl = NULL; /* Current Resources */
  352. struct acpi_handle_list *tl = NULL; /* Target Resources */
  353. int i = 0;
  354. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  355. return -EINVAL;
  356. if ((device->power.state < ACPI_STATE_D0)
  357. || (device->power.state > ACPI_STATE_D3))
  358. return -ENODEV;
  359. cl = &device->power.states[device->power.state].resources;
  360. tl = &device->power.states[state].resources;
  361. device->power.state = ACPI_STATE_UNKNOWN;
  362. if (!cl->count && !tl->count) {
  363. result = -ENODEV;
  364. goto end;
  365. }
  366. /* TBD: Resources must be ordered. */
  367. /*
  368. * First we reference all power resources required in the target list
  369. * (e.g. so the device doesn't lose power while transitioning).
  370. */
  371. for (i = 0; i < tl->count; i++) {
  372. result = acpi_power_on(tl->handles[i], device);
  373. if (result)
  374. goto end;
  375. }
  376. /*
  377. * Then we dereference all power resources used in the current list.
  378. */
  379. for (i = 0; i < cl->count; i++) {
  380. result = acpi_power_off_device(cl->handles[i], device);
  381. if (result)
  382. goto end;
  383. }
  384. /* We shouldn't change the state till all above operations succeed */
  385. device->power.state = state;
  386. end:
  387. if (result)
  388. printk(KERN_WARNING PREFIX "Transitioning device [%s] to D%d\n",
  389. device->pnp.bus_id, state);
  390. return result;
  391. }
  392. /* --------------------------------------------------------------------------
  393. FS Interface (/proc)
  394. -------------------------------------------------------------------------- */
  395. static struct proc_dir_entry *acpi_power_dir;
  396. static int acpi_power_seq_show(struct seq_file *seq, void *offset)
  397. {
  398. int count = 0;
  399. int result = 0;
  400. struct acpi_power_resource *resource = NULL;
  401. struct list_head *node, *next;
  402. struct acpi_power_reference *ref;
  403. resource = seq->private;
  404. if (!resource)
  405. goto end;
  406. result = acpi_power_get_state(resource);
  407. if (result)
  408. goto end;
  409. seq_puts(seq, "state: ");
  410. switch (resource->state) {
  411. case ACPI_POWER_RESOURCE_STATE_ON:
  412. seq_puts(seq, "on\n");
  413. break;
  414. case ACPI_POWER_RESOURCE_STATE_OFF:
  415. seq_puts(seq, "off\n");
  416. break;
  417. default:
  418. seq_puts(seq, "unknown\n");
  419. break;
  420. }
  421. mutex_lock(&resource->resource_lock);
  422. list_for_each_safe(node, next, &resource->reference) {
  423. ref = container_of(node, struct acpi_power_reference, node);
  424. count++;
  425. }
  426. mutex_unlock(&resource->resource_lock);
  427. seq_printf(seq, "system level: S%d\n"
  428. "order: %d\n"
  429. "reference count: %d\n",
  430. resource->system_level,
  431. resource->order, count);
  432. end:
  433. return 0;
  434. }
  435. static int acpi_power_open_fs(struct inode *inode, struct file *file)
  436. {
  437. return single_open(file, acpi_power_seq_show, PDE(inode)->data);
  438. }
  439. static int acpi_power_add_fs(struct acpi_device *device)
  440. {
  441. struct proc_dir_entry *entry = NULL;
  442. if (!device)
  443. return -EINVAL;
  444. if (!acpi_device_dir(device)) {
  445. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  446. acpi_power_dir);
  447. if (!acpi_device_dir(device))
  448. return -ENODEV;
  449. }
  450. /* 'status' [R] */
  451. entry = create_proc_entry(ACPI_POWER_FILE_STATUS,
  452. S_IRUGO, acpi_device_dir(device));
  453. if (!entry)
  454. return -EIO;
  455. else {
  456. entry->proc_fops = &acpi_power_fops;
  457. entry->data = acpi_driver_data(device);
  458. }
  459. return 0;
  460. }
  461. static int acpi_power_remove_fs(struct acpi_device *device)
  462. {
  463. if (acpi_device_dir(device)) {
  464. remove_proc_entry(ACPI_POWER_FILE_STATUS,
  465. acpi_device_dir(device));
  466. remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
  467. acpi_device_dir(device) = NULL;
  468. }
  469. return 0;
  470. }
  471. /* --------------------------------------------------------------------------
  472. Driver Interface
  473. -------------------------------------------------------------------------- */
  474. static int acpi_power_add(struct acpi_device *device)
  475. {
  476. int result = 0;
  477. acpi_status status = AE_OK;
  478. struct acpi_power_resource *resource = NULL;
  479. union acpi_object acpi_object;
  480. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  481. if (!device)
  482. return -EINVAL;
  483. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  484. if (!resource)
  485. return -ENOMEM;
  486. resource->device = device;
  487. mutex_init(&resource->resource_lock);
  488. INIT_LIST_HEAD(&resource->reference);
  489. strcpy(resource->name, device->pnp.bus_id);
  490. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  491. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  492. acpi_driver_data(device) = resource;
  493. /* Evalute the object to get the system level and resource order. */
  494. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  495. if (ACPI_FAILURE(status)) {
  496. result = -ENODEV;
  497. goto end;
  498. }
  499. resource->system_level = acpi_object.power_resource.system_level;
  500. resource->order = acpi_object.power_resource.resource_order;
  501. result = acpi_power_get_state(resource);
  502. if (result)
  503. goto end;
  504. switch (resource->state) {
  505. case ACPI_POWER_RESOURCE_STATE_ON:
  506. device->power.state = ACPI_STATE_D0;
  507. break;
  508. case ACPI_POWER_RESOURCE_STATE_OFF:
  509. device->power.state = ACPI_STATE_D3;
  510. break;
  511. default:
  512. device->power.state = ACPI_STATE_UNKNOWN;
  513. break;
  514. }
  515. result = acpi_power_add_fs(device);
  516. if (result)
  517. goto end;
  518. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  519. acpi_device_bid(device), resource->state ? "on" : "off");
  520. end:
  521. if (result)
  522. kfree(resource);
  523. return result;
  524. }
  525. static int acpi_power_remove(struct acpi_device *device, int type)
  526. {
  527. struct acpi_power_resource *resource = NULL;
  528. struct list_head *node, *next;
  529. if (!device || !acpi_driver_data(device))
  530. return -EINVAL;
  531. resource = acpi_driver_data(device);
  532. acpi_power_remove_fs(device);
  533. mutex_lock(&resource->resource_lock);
  534. list_for_each_safe(node, next, &resource->reference) {
  535. struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
  536. list_del(&ref->node);
  537. kfree(ref);
  538. }
  539. mutex_unlock(&resource->resource_lock);
  540. kfree(resource);
  541. return 0;
  542. }
  543. static int acpi_power_resume(struct acpi_device *device, int state)
  544. {
  545. int result = 0;
  546. struct acpi_power_resource *resource = NULL;
  547. struct acpi_power_reference *ref;
  548. if (!device || !acpi_driver_data(device))
  549. return -EINVAL;
  550. resource = (struct acpi_power_resource *)acpi_driver_data(device);
  551. result = acpi_power_get_state(resource);
  552. if (result)
  553. return result;
  554. mutex_lock(&resource->resource_lock);
  555. if ((resource->state == ACPI_POWER_RESOURCE_STATE_ON) &&
  556. list_empty(&resource->reference)) {
  557. mutex_unlock(&resource->resource_lock);
  558. result = acpi_power_off_device(device->handle, NULL);
  559. return result;
  560. }
  561. if ((resource->state == ACPI_POWER_RESOURCE_STATE_OFF) &&
  562. !list_empty(&resource->reference)) {
  563. ref = container_of(resource->reference.next, struct acpi_power_reference, node);
  564. mutex_unlock(&resource->resource_lock);
  565. result = acpi_power_on(device->handle, ref->device);
  566. return result;
  567. }
  568. mutex_unlock(&resource->resource_lock);
  569. return 0;
  570. }
  571. static int __init acpi_power_init(void)
  572. {
  573. int result = 0;
  574. if (acpi_disabled)
  575. return 0;
  576. INIT_LIST_HEAD(&acpi_power_resource_list);
  577. acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
  578. if (!acpi_power_dir)
  579. return -ENODEV;
  580. result = acpi_bus_register_driver(&acpi_power_driver);
  581. if (result < 0) {
  582. remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
  583. return -ENODEV;
  584. }
  585. return 0;
  586. }
  587. subsys_initcall(acpi_power_init);