power.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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/proc_fs.h>
  42. #include <linux/seq_file.h>
  43. #include <acpi/acpi_bus.h>
  44. #include <acpi/acpi_drivers.h>
  45. #include "sleep.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. int acpi_power_nocheck;
  57. module_param_named(power_nocheck, acpi_power_nocheck, bool, 000);
  58. static int acpi_power_add(struct acpi_device *device);
  59. static int acpi_power_remove(struct acpi_device *device, int type);
  60. static int acpi_power_resume(struct acpi_device *device);
  61. static int acpi_power_open_fs(struct inode *inode, struct file *file);
  62. static const struct acpi_device_id power_device_ids[] = {
  63. {ACPI_POWER_HID, 0},
  64. {"", 0},
  65. };
  66. MODULE_DEVICE_TABLE(acpi, power_device_ids);
  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. .resume = acpi_power_resume,
  75. },
  76. };
  77. struct acpi_power_reference {
  78. struct list_head node;
  79. struct acpi_device *device;
  80. };
  81. struct acpi_power_resource {
  82. struct acpi_device * device;
  83. acpi_bus_id name;
  84. u32 system_level;
  85. u32 order;
  86. struct mutex resource_lock;
  87. struct list_head reference;
  88. };
  89. static struct list_head acpi_power_resource_list;
  90. static const struct file_operations acpi_power_fops = {
  91. .owner = THIS_MODULE,
  92. .open = acpi_power_open_fs,
  93. .read = seq_read,
  94. .llseek = seq_lseek,
  95. .release = single_release,
  96. };
  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 result = 0, state1;
  140. u32 i = 0;
  141. if (!list || !state)
  142. return -EINVAL;
  143. /* The state of the list is 'on' IFF all resources are 'on'. */
  144. /* */
  145. for (i = 0; i < list->count; i++) {
  146. /*
  147. * The state of the power resource can be obtained by
  148. * using the ACPI handle. In such case it is unnecessary to
  149. * get the Power resource first and then get its state again.
  150. */
  151. result = acpi_power_get_state(list->handles[i], &state1);
  152. if (result)
  153. return result;
  154. *state = state1;
  155. if (*state != ACPI_POWER_RESOURCE_STATE_ON)
  156. break;
  157. }
  158. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
  159. *state ? "on" : "off"));
  160. return result;
  161. }
  162. static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
  163. {
  164. int result = 0;
  165. int found = 0;
  166. acpi_status status = AE_OK;
  167. struct acpi_power_resource *resource = NULL;
  168. struct list_head *node, *next;
  169. struct acpi_power_reference *ref;
  170. result = acpi_power_get_context(handle, &resource);
  171. if (result)
  172. return result;
  173. mutex_lock(&resource->resource_lock);
  174. list_for_each_safe(node, next, &resource->reference) {
  175. ref = container_of(node, struct acpi_power_reference, node);
  176. if (dev->handle == ref->device->handle) {
  177. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
  178. dev->pnp.bus_id, resource->name));
  179. found = 1;
  180. break;
  181. }
  182. }
  183. if (!found) {
  184. ref = kmalloc(sizeof (struct acpi_power_reference),
  185. irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
  186. if (!ref) {
  187. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
  188. mutex_unlock(&resource->resource_lock);
  189. return -ENOMEM;
  190. }
  191. list_add_tail(&ref->node, &resource->reference);
  192. ref->device = dev;
  193. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
  194. dev->pnp.bus_id, resource->name));
  195. }
  196. mutex_unlock(&resource->resource_lock);
  197. status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
  198. if (ACPI_FAILURE(status))
  199. return -ENODEV;
  200. /* Update the power resource's _device_ power state */
  201. resource->device->power.state = ACPI_STATE_D0;
  202. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
  203. resource->name));
  204. return 0;
  205. }
  206. static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
  207. {
  208. int result = 0;
  209. acpi_status status = AE_OK;
  210. struct acpi_power_resource *resource = NULL;
  211. struct list_head *node, *next;
  212. struct acpi_power_reference *ref;
  213. result = acpi_power_get_context(handle, &resource);
  214. if (result)
  215. return result;
  216. mutex_lock(&resource->resource_lock);
  217. list_for_each_safe(node, next, &resource->reference) {
  218. ref = container_of(node, struct acpi_power_reference, node);
  219. if (dev->handle == ref->device->handle) {
  220. list_del(&ref->node);
  221. kfree(ref);
  222. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
  223. dev->pnp.bus_id, resource->name));
  224. break;
  225. }
  226. }
  227. if (!list_empty(&resource->reference)) {
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
  229. resource->name));
  230. mutex_unlock(&resource->resource_lock);
  231. return 0;
  232. }
  233. mutex_unlock(&resource->resource_lock);
  234. status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
  235. if (ACPI_FAILURE(status))
  236. return -ENODEV;
  237. /* Update the power resource's _device_ power state */
  238. resource->device->power.state = ACPI_STATE_D3;
  239. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
  240. resource->name));
  241. return 0;
  242. }
  243. /**
  244. * acpi_device_sleep_wake - execute _DSW (Device Sleep Wake) or (deprecated in
  245. * ACPI 3.0) _PSW (Power State Wake)
  246. * @dev: Device to handle.
  247. * @enable: 0 - disable, 1 - enable the wake capabilities of the device.
  248. * @sleep_state: Target sleep state of the system.
  249. * @dev_state: Target power state of the device.
  250. *
  251. * Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  252. * State Wake) for the device, if present. On failure reset the device's
  253. * wakeup.flags.valid flag.
  254. *
  255. * RETURN VALUE:
  256. * 0 if either _DSW or _PSW has been successfully executed
  257. * 0 if neither _DSW nor _PSW has been found
  258. * -ENODEV if the execution of either _DSW or _PSW has failed
  259. */
  260. int acpi_device_sleep_wake(struct acpi_device *dev,
  261. int enable, int sleep_state, int dev_state)
  262. {
  263. union acpi_object in_arg[3];
  264. struct acpi_object_list arg_list = { 3, in_arg };
  265. acpi_status status = AE_OK;
  266. /*
  267. * Try to execute _DSW first.
  268. *
  269. * Three agruments are needed for the _DSW object:
  270. * Argument 0: enable/disable the wake capabilities
  271. * Argument 1: target system state
  272. * Argument 2: target device state
  273. * When _DSW object is called to disable the wake capabilities, maybe
  274. * the first argument is filled. The values of the other two agruments
  275. * are meaningless.
  276. */
  277. in_arg[0].type = ACPI_TYPE_INTEGER;
  278. in_arg[0].integer.value = enable;
  279. in_arg[1].type = ACPI_TYPE_INTEGER;
  280. in_arg[1].integer.value = sleep_state;
  281. in_arg[2].type = ACPI_TYPE_INTEGER;
  282. in_arg[2].integer.value = dev_state;
  283. status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
  284. if (ACPI_SUCCESS(status)) {
  285. return 0;
  286. } else if (status != AE_NOT_FOUND) {
  287. printk(KERN_ERR PREFIX "_DSW execution failed\n");
  288. dev->wakeup.flags.valid = 0;
  289. return -ENODEV;
  290. }
  291. /* Execute _PSW */
  292. arg_list.count = 1;
  293. in_arg[0].integer.value = enable;
  294. status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
  295. if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
  296. printk(KERN_ERR PREFIX "_PSW execution failed\n");
  297. dev->wakeup.flags.valid = 0;
  298. return -ENODEV;
  299. }
  300. return 0;
  301. }
  302. /*
  303. * Prepare a wakeup device, two steps (Ref ACPI 2.0:P229):
  304. * 1. Power on the power resources required for the wakeup device
  305. * 2. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  306. * State Wake) for the device, if present
  307. */
  308. int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
  309. {
  310. int i, err = 0;
  311. if (!dev || !dev->wakeup.flags.valid)
  312. return -EINVAL;
  313. mutex_lock(&acpi_device_lock);
  314. if (dev->wakeup.prepare_count++)
  315. goto out;
  316. /* Open power resource */
  317. for (i = 0; i < dev->wakeup.resources.count; i++) {
  318. int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
  319. if (ret) {
  320. printk(KERN_ERR PREFIX "Transition power state\n");
  321. dev->wakeup.flags.valid = 0;
  322. err = -ENODEV;
  323. goto err_out;
  324. }
  325. }
  326. /*
  327. * Passing 3 as the third argument below means the device may be placed
  328. * in arbitrary power state afterwards.
  329. */
  330. err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
  331. err_out:
  332. if (err)
  333. dev->wakeup.prepare_count = 0;
  334. out:
  335. mutex_unlock(&acpi_device_lock);
  336. return err;
  337. }
  338. /*
  339. * Shutdown a wakeup device, counterpart of above method
  340. * 1. Execute _DSW (Device Sleep Wake) or (deprecated in ACPI 3.0) _PSW (Power
  341. * State Wake) for the device, if present
  342. * 2. Shutdown down the power resources
  343. */
  344. int acpi_disable_wakeup_device_power(struct acpi_device *dev)
  345. {
  346. int i, err = 0;
  347. if (!dev || !dev->wakeup.flags.valid)
  348. return -EINVAL;
  349. mutex_lock(&acpi_device_lock);
  350. if (--dev->wakeup.prepare_count > 0)
  351. goto out;
  352. /*
  353. * Executing the code below even if prepare_count is already zero when
  354. * the function is called may be useful, for example for initialisation.
  355. */
  356. if (dev->wakeup.prepare_count < 0)
  357. dev->wakeup.prepare_count = 0;
  358. err = acpi_device_sleep_wake(dev, 0, 0, 0);
  359. if (err)
  360. goto out;
  361. /* Close power resource */
  362. for (i = 0; i < dev->wakeup.resources.count; i++) {
  363. int ret = acpi_power_off_device(
  364. dev->wakeup.resources.handles[i], dev);
  365. if (ret) {
  366. printk(KERN_ERR PREFIX "Transition power state\n");
  367. dev->wakeup.flags.valid = 0;
  368. err = -ENODEV;
  369. goto out;
  370. }
  371. }
  372. out:
  373. mutex_unlock(&acpi_device_lock);
  374. return err;
  375. }
  376. /* --------------------------------------------------------------------------
  377. Device Power Management
  378. -------------------------------------------------------------------------- */
  379. int acpi_power_get_inferred_state(struct acpi_device *device)
  380. {
  381. int result = 0;
  382. struct acpi_handle_list *list = NULL;
  383. int list_state = 0;
  384. int i = 0;
  385. if (!device)
  386. return -EINVAL;
  387. device->power.state = ACPI_STATE_UNKNOWN;
  388. /*
  389. * We know a device's inferred power state when all the resources
  390. * required for a given D-state are 'on'.
  391. */
  392. for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
  393. list = &device->power.states[i].resources;
  394. if (list->count < 1)
  395. continue;
  396. result = acpi_power_get_list_state(list, &list_state);
  397. if (result)
  398. return result;
  399. if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
  400. device->power.state = i;
  401. return 0;
  402. }
  403. }
  404. device->power.state = ACPI_STATE_D3;
  405. return 0;
  406. }
  407. int acpi_power_transition(struct acpi_device *device, int state)
  408. {
  409. int result = 0;
  410. struct acpi_handle_list *cl = NULL; /* Current Resources */
  411. struct acpi_handle_list *tl = NULL; /* Target Resources */
  412. int i = 0;
  413. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
  414. return -EINVAL;
  415. if ((device->power.state < ACPI_STATE_D0)
  416. || (device->power.state > ACPI_STATE_D3))
  417. return -ENODEV;
  418. cl = &device->power.states[device->power.state].resources;
  419. tl = &device->power.states[state].resources;
  420. /* TBD: Resources must be ordered. */
  421. /*
  422. * First we reference all power resources required in the target list
  423. * (e.g. so the device doesn't lose power while transitioning).
  424. */
  425. for (i = 0; i < tl->count; i++) {
  426. result = acpi_power_on(tl->handles[i], device);
  427. if (result)
  428. goto end;
  429. }
  430. if (device->power.state == state) {
  431. goto end;
  432. }
  433. /*
  434. * Then we dereference all power resources used in the current list.
  435. */
  436. for (i = 0; i < cl->count; i++) {
  437. result = acpi_power_off_device(cl->handles[i], device);
  438. if (result)
  439. goto end;
  440. }
  441. end:
  442. if (result)
  443. device->power.state = ACPI_STATE_UNKNOWN;
  444. else {
  445. /* We shouldn't change the state till all above operations succeed */
  446. device->power.state = state;
  447. }
  448. return result;
  449. }
  450. /* --------------------------------------------------------------------------
  451. FS Interface (/proc)
  452. -------------------------------------------------------------------------- */
  453. static struct proc_dir_entry *acpi_power_dir;
  454. static int acpi_power_seq_show(struct seq_file *seq, void *offset)
  455. {
  456. int count = 0;
  457. int result = 0, state;
  458. struct acpi_power_resource *resource = NULL;
  459. struct list_head *node, *next;
  460. struct acpi_power_reference *ref;
  461. resource = seq->private;
  462. if (!resource)
  463. goto end;
  464. result = acpi_power_get_state(resource->device->handle, &state);
  465. if (result)
  466. goto end;
  467. seq_puts(seq, "state: ");
  468. switch (state) {
  469. case ACPI_POWER_RESOURCE_STATE_ON:
  470. seq_puts(seq, "on\n");
  471. break;
  472. case ACPI_POWER_RESOURCE_STATE_OFF:
  473. seq_puts(seq, "off\n");
  474. break;
  475. default:
  476. seq_puts(seq, "unknown\n");
  477. break;
  478. }
  479. mutex_lock(&resource->resource_lock);
  480. list_for_each_safe(node, next, &resource->reference) {
  481. ref = container_of(node, struct acpi_power_reference, node);
  482. count++;
  483. }
  484. mutex_unlock(&resource->resource_lock);
  485. seq_printf(seq, "system level: S%d\n"
  486. "order: %d\n"
  487. "reference count: %d\n",
  488. resource->system_level,
  489. resource->order, count);
  490. end:
  491. return 0;
  492. }
  493. static int acpi_power_open_fs(struct inode *inode, struct file *file)
  494. {
  495. return single_open(file, acpi_power_seq_show, PDE(inode)->data);
  496. }
  497. static int acpi_power_add_fs(struct acpi_device *device)
  498. {
  499. struct proc_dir_entry *entry = NULL;
  500. if (!device)
  501. return -EINVAL;
  502. if (!acpi_device_dir(device)) {
  503. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  504. acpi_power_dir);
  505. if (!acpi_device_dir(device))
  506. return -ENODEV;
  507. }
  508. /* 'status' [R] */
  509. entry = proc_create_data(ACPI_POWER_FILE_STATUS,
  510. S_IRUGO, acpi_device_dir(device),
  511. &acpi_power_fops, acpi_driver_data(device));
  512. if (!entry)
  513. return -EIO;
  514. return 0;
  515. }
  516. static int acpi_power_remove_fs(struct acpi_device *device)
  517. {
  518. if (acpi_device_dir(device)) {
  519. remove_proc_entry(ACPI_POWER_FILE_STATUS,
  520. acpi_device_dir(device));
  521. remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
  522. acpi_device_dir(device) = NULL;
  523. }
  524. return 0;
  525. }
  526. /* --------------------------------------------------------------------------
  527. Driver Interface
  528. -------------------------------------------------------------------------- */
  529. static int acpi_power_add(struct acpi_device *device)
  530. {
  531. int result = 0, state;
  532. acpi_status status = AE_OK;
  533. struct acpi_power_resource *resource = NULL;
  534. union acpi_object acpi_object;
  535. struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
  536. if (!device)
  537. return -EINVAL;
  538. resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
  539. if (!resource)
  540. return -ENOMEM;
  541. resource->device = device;
  542. mutex_init(&resource->resource_lock);
  543. INIT_LIST_HEAD(&resource->reference);
  544. strcpy(resource->name, device->pnp.bus_id);
  545. strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
  546. strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
  547. device->driver_data = resource;
  548. /* Evalute the object to get the system level and resource order. */
  549. status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
  550. if (ACPI_FAILURE(status)) {
  551. result = -ENODEV;
  552. goto end;
  553. }
  554. resource->system_level = acpi_object.power_resource.system_level;
  555. resource->order = acpi_object.power_resource.resource_order;
  556. result = acpi_power_get_state(device->handle, &state);
  557. if (result)
  558. goto end;
  559. switch (state) {
  560. case ACPI_POWER_RESOURCE_STATE_ON:
  561. device->power.state = ACPI_STATE_D0;
  562. break;
  563. case ACPI_POWER_RESOURCE_STATE_OFF:
  564. device->power.state = ACPI_STATE_D3;
  565. break;
  566. default:
  567. device->power.state = ACPI_STATE_UNKNOWN;
  568. break;
  569. }
  570. result = acpi_power_add_fs(device);
  571. if (result)
  572. goto end;
  573. printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
  574. acpi_device_bid(device), state ? "on" : "off");
  575. end:
  576. if (result)
  577. kfree(resource);
  578. return result;
  579. }
  580. static int acpi_power_remove(struct acpi_device *device, int type)
  581. {
  582. struct acpi_power_resource *resource = NULL;
  583. struct list_head *node, *next;
  584. if (!device || !acpi_driver_data(device))
  585. return -EINVAL;
  586. resource = acpi_driver_data(device);
  587. acpi_power_remove_fs(device);
  588. mutex_lock(&resource->resource_lock);
  589. list_for_each_safe(node, next, &resource->reference) {
  590. struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
  591. list_del(&ref->node);
  592. kfree(ref);
  593. }
  594. mutex_unlock(&resource->resource_lock);
  595. kfree(resource);
  596. return 0;
  597. }
  598. static int acpi_power_resume(struct acpi_device *device)
  599. {
  600. int result = 0, state;
  601. struct acpi_power_resource *resource = NULL;
  602. struct acpi_power_reference *ref;
  603. if (!device || !acpi_driver_data(device))
  604. return -EINVAL;
  605. resource = acpi_driver_data(device);
  606. result = acpi_power_get_state(device->handle, &state);
  607. if (result)
  608. return result;
  609. mutex_lock(&resource->resource_lock);
  610. if (state == ACPI_POWER_RESOURCE_STATE_OFF &&
  611. !list_empty(&resource->reference)) {
  612. ref = container_of(resource->reference.next, struct acpi_power_reference, node);
  613. mutex_unlock(&resource->resource_lock);
  614. result = acpi_power_on(device->handle, ref->device);
  615. return result;
  616. }
  617. mutex_unlock(&resource->resource_lock);
  618. return 0;
  619. }
  620. int __init acpi_power_init(void)
  621. {
  622. int result = 0;
  623. INIT_LIST_HEAD(&acpi_power_resource_list);
  624. acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
  625. if (!acpi_power_dir)
  626. return -ENODEV;
  627. result = acpi_bus_register_driver(&acpi_power_driver);
  628. if (result < 0) {
  629. remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
  630. return -ENODEV;
  631. }
  632. return 0;
  633. }