power.c 21 KB

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