power.c 18 KB

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