power.c 20 KB

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