power.c 21 KB

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